Macro gtk_macros::stateful_action[][src]

macro_rules! stateful_action {
    ($actions_group:expr, $name:expr, $state:expr, $callback:expr) => { ... };
    ($actions_group:expr, $name:expr, $param_type:expr, $state:expr, $callback:expr) => { ... };
}

Create a new stateful action

  • Before:

    Example:

    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:

    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
    });