gtk-macros-0.3.0/.cargo_vcs_info.json0000644000000001120000000000000130740ustar { "git": { "sha1": "d6acfb4199c65e229eb6c28d5669f68b17a1cec8" } } gtk-macros-0.3.0/.gitignore000064400000000000000000000000230000000000000136330ustar 00000000000000/target Cargo.lock gtk-macros-0.3.0/.gitlab-ci.yml000064400000000000000000000006430000000000000143070ustar 00000000000000image: 'registry.fedoraproject.org/fedora:31' before_script: - dnf update -y - dnf install -y rust cargo git - git submodule update --init stages: - test - deploy build: stage: test script: - cargo build --all-features pages: stage: deploy script: - cargo doc -p gtk-macros --all-features - mv target/doc/ public artifacts: paths: - public only: refs: - master gtk-macros-0.3.0/Cargo.toml0000644000000016300000000000000111000ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "gtk-macros" version = "0.3.0" authors = ["Felix Häcker ", "Bilal Elmoussaoui "] description = "Few macros to make gtk-rs development more convenient" keywords = ["gtk", "gtk-rs", "macros", "macro"] license = "GPL-3.0-or-later" repository = "https://gitlab.gnome.org/haecker-felix/gtk-macros" [dependencies] gtk-macros-0.3.0/Cargo.toml.orig000064400000000000000000000006130000000000000145370ustar 00000000000000[package] name = "gtk-macros" description = "Few macros to make gtk-rs development more convenient" license = "GPL-3.0-or-later" keywords = ["gtk", "gtk-rs", "macros", "macro"] version = "0.3.0" authors = ["Felix Häcker ", "Bilal Elmoussaoui "] edition = "2018" repository = "https://gitlab.gnome.org/haecker-felix/gtk-macros" [dependencies] gtk-macros-0.3.0/README.md000064400000000000000000000002700000000000000131260ustar 00000000000000# gtk-macros Few macros to make gtk-rs development more convenient - [Open documentation](https://docs.rs/gtk-macros) - [gtk-macros on crates.io](https://crates.io/crates/gtk_macros) gtk-macros-0.3.0/src/lib.rs000064400000000000000000000177410000000000000135650ustar 00000000000000/// /// Retrieve a widget from a gtk::Builder /// /// - Before: /// /// Example: /// /// ```no_run /// let builder = gtk::Builder::new_frm_resource("/org/gnome/App/ui/widget.ui"); /// let widget: gtk::Label = builder.object("my_label").expect("Failed to find my_label object"); /// widget.set_text("Hello world!"); /// /// ``` /// /// - After: /// /// 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("Hello 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("Hello world!"); /// ``` #[macro_export] macro_rules! get_widget { ($builder:expr, $wtype:ty, @$name:ident) => {{ let $name: $wtype = $builder .object(stringify!($name)) .expect(&format!("Could not find widget \"{}\" of type \"{}\"", stringify!($name), stringify!($wtype))); $name }}; ($builder:expr, $wtype:ty, $name:ident) => { let $name: $wtype = $builder .object(stringify!($name)) .expect(&format!("Could not find widget \"{}\" of type \"{}\"", stringify!($name), stringify!($wtype))); }; } /// Spawn a future /// /// - Before: /// /// Example: /// /// ```no_run /// let ctx = glib::MainContext::default(); /// ctx.spawn_local(async { /// something.await; /// }); /// ``` /// /// - After: /// /// Example: /// /// ```no_run /// spawn!(async { /// something.await; /// }); /// ``` #[macro_export] macro_rules! spawn { ($future:expr) => { let ctx = glib::MainContext::default(); ctx.spawn_local($future); }; } /// Send an event through a glib::Sender /// /// - Before: /// /// Example: /// /// ```no_run /// sender.send(Action::DoThing).expect("Failed to send DoThing through the glib channel?"); /// ``` /// /// - After: /// /// Example: /// /// ```no_run /// send!(self.sender, Action::DoThing); /// ``` #[macro_export] macro_rules! send { ($sender:expr, $action:expr) => { if let Err(err) = $sender.send($action) { error!("Failed to send \"{}\" action due to {}", stringify!($action), err); } }; } /// Create a new action /// /// - Before: /// /// Example: /// /// ```no_run /// let widget = get_widget!(builder, gtk::Window, widget); /// let actions = gio::SimpleActionGroup::new(); /// widget.insert_action_group("export", Some(&actions)); /// /// let action = gio::SimpleAction::new("do", None); /// action.connect_activate(move |action, _| { /// // Do something /// }); /// actions.add_action(&action); /// ``` /// /// - After: /// /// 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) => { let simple_action = gio::SimpleAction::new($name, None); simple_action.connect_activate($callback); $actions_group.add_action(&simple_action); }; ($actions_group:expr, $name:expr, $param_type:expr, $callback:expr) => { let simple_action = gio::SimpleAction::new($name, $param_type); simple_action.connect_activate($callback); $actions_group.add_action(&simple_action); }; } /// Create a new stateful action /// /// - Before: /// /// Example: /// /// ```no_run /// let actions = gio::SimpleActionGroup::new(); /// /// let is_dark_mode = false; /// let action = gio::SimpleAction::new_stateful("dark-mode", None, &is_dark_mode.to_variant()); /// action.connect_activate(move |action, _| { /// let state = action.state().unwrap(); /// let action_state: bool = state.get().unwrap(); /// let is_dark_mode = !action_state; /// action.set_state(&is_dark_mode.to_variant()); /// }); /// actions.add_action(&action); /// ``` /// /// - After: /// /// 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.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) => { let simple_action = gio::SimpleAction::new_stateful($name, None, &$state.to_variant()); simple_action.connect_activate($callback); $actions_group.add_action(&simple_action); }; ($actions_group:expr, $name:expr, $param_type:expr, $state:expr, $callback:expr) => { let simple_action = gio::SimpleAction::new_stateful($name, $param_type, &$state.to_variant()); simple_action.connect_activate($callback); $actions_group.add_action(&simple_action); }; } /// Retrieve an action from a gio::ActionGroup /// /// - Before: /// /// Example: /// /// ```no_run /// let actions = gio::SimpleActionGroup::new(); /// action!( /// actions, /// "delete", /// move |action, _| { /// // Do something /// }, /// ); /// let action = actions.lookup_action("delete") /// .unwrap() /// .downcast::() /// .unwrap(); /// action.set_enabled(false); /// ``` /// /// - After: /// /// Example: /// /// ```no_run /// let actions = gio::SimpleActionGroup::new(); /// action!( /// actions, /// "delete", /// move |action, _| { /// // Do something /// }, /// ); /// get_action!(actions, @delete).set_enabled(false); /// ``` #[macro_export] macro_rules! get_action { ($actions:expr, @$name:ident) => {{ let $name = $actions .lookup_action(stringify!($name)) .expect(&format!("Could not find action \"{}\"", stringify!($name))) .downcast::() .unwrap(); $name }}; ($actions:expr, $name:ident) => { let $name = $actions .lookup_action(stringify!($name)) .expect(&format!("Could not find action \"{}\"", stringify!($name))) .downcast::() .unwrap(); }; } /// Add a style class or list of style classes to a widget /// /// - Before: /// /// Example 1: /// /// ```no_run /// let button = gtk::Button::new(); /// let ctx = button.style_context(); /// ctx.add_class("foo"); /// ``` /// /// Example 2: /// /// ```no_run /// let button = gtk::Button::new(); /// let ctx = button.style_context(); /// ctx.add_class("foo"); /// ctx.add_class("bar"); /// ``` /// /// - After: /// /// Example 1: /// /// ```no_run /// let button = gtk::Button::new(); /// add_style_class!(button, @foo); /// ``` /// /// Example 2: /// /// ```no_run /// let button = gtk::Button::new(); /// add_style_class!(button, &["foo", "bar"]); /// ``` #[macro_export] macro_rules! add_style_class { ($widget:expr, @$name:ident) => {{ let ctx = $widget.style_context(); ctx.add_class(stringify!($name)); }}; ($widget:expr, $names:expr) => {{ let ctx = $widget.style_context(); for name in $names { ctx.add_class(name); } }}; }