From 74014f3312c5c34a0e101df999b66027865fbc94 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Sat, 14 Mar 2020 11:33:07 +0100 Subject: [PATCH] Add basic examples --- src/lib.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 939a23a..a41600f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,19 @@ +/// +/// `get_widget!` allows retrieving a widget from a gtk::Builder +/// +/// Example 1: +/// +/// ```no_run +/// let builder = gtk::Builder::new_from_resource("/org/gnome/App/ui/widget.ui"); +/// get_widget!(builder, gtk::Label, my_label); +/// my_label.set_text("Salut world"); +/// ``` +/// Example 2: +/// +/// ```no_run +/// let builder = gtk::Builder::new_from_resource("/org/gnome/App/ui/widget.ui"); +/// get_widget!(builder, gtk::Label, @my_label).set_text("Salut world"); +/// ``` #[macro_export] macro_rules! get_widget { ($builder:expr, $wtype:ty, @$name:ident) => {{ @@ -9,6 +25,13 @@ macro_rules! get_widget { }; } + +/// Example: +/// ```no_run +/// spawn!(async { +/// something.await; +/// }); +/// ``` #[macro_export] macro_rules! spawn { ($future:expr) => { @@ -26,6 +49,20 @@ macro_rules! send { }; } + +/// Example: +/// ```no_run +/// let widget = get_widget!(builder, gtk::Window, widget); +/// let actions = gio::SimpleActionGroup::new(); +/// widget.insert_action_group("export", Some(&actions)); +/// action!( +/// actions, +/// "do", +/// move |action, _| { +/// // do something +/// }, +///); +/// ``` #[macro_export] macro_rules! action { ($actions_group:expr, $name:expr, $callback:expr) => { @@ -40,6 +77,19 @@ macro_rules! action { }; } +/// Example: +/// ```no_run +/// let actions = gio::SimpleActionGroup::new(); +/// let is_dark_mode = false; +/// stateful_action!(actions, "dark-mode", is_dark_mode, move |action, _| { +/// let state = action.get_state().unwrap(); +/// let action_state: bool = state.get().unwrap(); +/// let is_dark_mode = !action_state; +/// action.set_state(&is_dark_mode.to_variant()); +/// +/// // Store the state using gsettings for example +/// }); +/// ``` #[macro_export] macro_rules! stateful_action { ($actions_group:expr, $name:expr, $state:expr, $callback:expr) => { @@ -55,6 +105,19 @@ macro_rules! stateful_action { } +/// Example: +/// ```no_run +/// let actions = gio::SimpleActionGroup::new(); +/// widget.insert_action_group("export", Some(&actions)); +/// action!( +/// actions, +/// "do", +/// move |action, _| { +/// // do something +/// }, +///); +/// get_action!(actions, @delete).set_enabled(false); +/// ``` #[macro_export] macro_rules! get_action { ($actions:expr, @$name:ident) => {{ -- GitLab