Struct relm4::actions::RelmAction

source ·
pub struct RelmAction<Name: ActionName> { /* private fields */ }
Expand description

A type safe action that wraps around gio::SimpleAction.

Implementations§

source§

impl<Name: ActionName> RelmAction<Name>where Name::State: ToVariant + FromVariant, Name::Target: ToVariant + FromVariant,

source

pub fn new_stateful_with_target_value<Callback: Fn(&SimpleAction, &mut Name::State, Name::Target) + 'static>( start_value: &Name::State, callback: Callback ) -> Self

Create a new stateful action with target value.

Examples found in repository?
relm4/examples/actions.rs (lines 78-81)
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    fn init(
        _init: Self::Init,
        root: &Self::Root,
        sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        let menu_model = gtk::gio::Menu::new();
        menu_model.append(Some("Stateless"), Some(&ExampleAction::action_name()));

        let model = Self { counter: 0 };

        let widgets = view_output!();

        let app = relm4::main_application();
        app.set_accelerators_for_action::<ExampleAction>(&["<primary>W"]);

        let group = RelmActionGroup::<WindowActionGroup>::new();

        let action: RelmAction<ExampleAction> = RelmAction::new_stateless(move |_| {
            println!("Statelesss action!");
            sender.input(Msg::Increment);
        });

        let action2: RelmAction<ExampleU8Action> =
            RelmAction::new_stateful_with_target_value(&0, |_, state, value| {
                println!("Stateful action -> state: {state}, value: {value}");
                *state += value;
            });

        group.add_action(&action);
        group.add_action(&action2);

        let actions = group.into_action_group();
        widgets
            .main_window
            .insert_action_group(WindowActionGroup::NAME, Some(&actions));

        ComponentParts { model, widgets }
    }
More examples
Hide additional examples
relm4/examples/menu.rs (lines 139-142)
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    fn init(
        counter: Self::Init,
        root: &Self::Root,
        sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        // ============================================================
        //
        // You can also use menu! outside of the widget macro.
        // This is the manual equivalent to the the menu! macro above.
        //
        // ============================================================
        //
        // relm4::menu! {
        //     main_menu: {
        //         custom: "my_widget",
        //         "Example" => ExampleAction,
        //         "Example2" => ExampleAction,
        //         "Example toggle" => ExampleU8Action(1_u8),
        //         section! {
        //             "Section example" => ExampleAction,
        //             "Example toggle" => ExampleU8Action(1_u8),
        //         },
        //         section! {
        //             "Example" => ExampleAction,
        //             "Example2" => ExampleAction,
        //             "Example Value" => ExampleU8Action(1_u8),
        //         }
        //     }
        // };

        let model = Self { counter };
        let widgets = view_output!();

        let app = relm4::main_application();
        app.set_accelerators_for_action::<ExampleAction>(&["<primary>W"]);

        let group = RelmActionGroup::<WindowActionGroup>::new();

        let action: RelmAction<ExampleAction> = {
            RelmAction::new_stateless(move |_| {
                println!("Statelesss action!");
                sender.input(Msg::Increment);
            })
        };

        let action2: RelmAction<ExampleU8Action> =
            RelmAction::new_stateful_with_target_value(&0, |_, state, _value| {
                *state ^= 1;
                dbg!(state);
            });

        group.add_action(&action);
        group.add_action(&action2);

        let actions = group.into_action_group();
        widgets
            .main_window
            .insert_action_group("win", Some(&actions));

        ComponentParts { model, widgets }
    }
source§

impl<Name: ActionName> RelmAction<Name>where Name::State: ToVariant + FromVariant, Name::Target: EmptyType,

source

pub fn new_stateful<Callback: Fn(&SimpleAction, &mut Name::State) + 'static>( start_value: &Name::State, callback: Callback ) -> Self

Create a new stateful action.

source§

impl<Name: ActionName> RelmAction<Name>where Name::State: EmptyType, Name::Target: ToVariant + FromVariant,

source

pub fn new_with_target_value<Callback: Fn(&SimpleAction, Name::Target) + 'static>( callback: Callback ) -> Self

Create a new stateless action with a target value.

source§

impl<Name: ActionName> RelmAction<Name>where Name::Target: EmptyType, Name::State: EmptyType,

source

pub fn new_stateless<Callback: Fn(&SimpleAction) + 'static>( callback: Callback ) -> Self

Create a new stateless action.

Examples found in repository?
relm4/examples/actions.rs (lines 72-75)
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    fn init(
        _init: Self::Init,
        root: &Self::Root,
        sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        let menu_model = gtk::gio::Menu::new();
        menu_model.append(Some("Stateless"), Some(&ExampleAction::action_name()));

        let model = Self { counter: 0 };

        let widgets = view_output!();

        let app = relm4::main_application();
        app.set_accelerators_for_action::<ExampleAction>(&["<primary>W"]);

        let group = RelmActionGroup::<WindowActionGroup>::new();

        let action: RelmAction<ExampleAction> = RelmAction::new_stateless(move |_| {
            println!("Statelesss action!");
            sender.input(Msg::Increment);
        });

        let action2: RelmAction<ExampleU8Action> =
            RelmAction::new_stateful_with_target_value(&0, |_, state, value| {
                println!("Stateful action -> state: {state}, value: {value}");
                *state += value;
            });

        group.add_action(&action);
        group.add_action(&action2);

        let actions = group.into_action_group();
        widgets
            .main_window
            .insert_action_group(WindowActionGroup::NAME, Some(&actions));

        ComponentParts { model, widgets }
    }
More examples
Hide additional examples
relm4/examples/menu.rs (lines 132-135)
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    fn init(
        counter: Self::Init,
        root: &Self::Root,
        sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        // ============================================================
        //
        // You can also use menu! outside of the widget macro.
        // This is the manual equivalent to the the menu! macro above.
        //
        // ============================================================
        //
        // relm4::menu! {
        //     main_menu: {
        //         custom: "my_widget",
        //         "Example" => ExampleAction,
        //         "Example2" => ExampleAction,
        //         "Example toggle" => ExampleU8Action(1_u8),
        //         section! {
        //             "Section example" => ExampleAction,
        //             "Example toggle" => ExampleU8Action(1_u8),
        //         },
        //         section! {
        //             "Example" => ExampleAction,
        //             "Example2" => ExampleAction,
        //             "Example Value" => ExampleU8Action(1_u8),
        //         }
        //     }
        // };

        let model = Self { counter };
        let widgets = view_output!();

        let app = relm4::main_application();
        app.set_accelerators_for_action::<ExampleAction>(&["<primary>W"]);

        let group = RelmActionGroup::<WindowActionGroup>::new();

        let action: RelmAction<ExampleAction> = {
            RelmAction::new_stateless(move |_| {
                println!("Statelesss action!");
                sender.input(Msg::Increment);
            })
        };

        let action2: RelmAction<ExampleU8Action> =
            RelmAction::new_stateful_with_target_value(&0, |_, state, _value| {
                *state ^= 1;
                dbg!(state);
            });

        group.add_action(&action);
        group.add_action(&action2);

        let actions = group.into_action_group();
        widgets
            .main_window
            .insert_action_group("win", Some(&actions));

        ComponentParts { model, widgets }
    }
source§

impl<Name: ActionName> RelmAction<Name>where Name::Target: ToVariant + FromVariant,

source

pub fn to_menu_item_with_target_value( label: &str, target_value: &Name::Target ) -> MenuItem

Create a menu item for this action with the target value sent to the action on activation.

source§

impl<Name: ActionName> RelmAction<Name>where Name::Target: EmptyType,

source

pub fn to_menu_item(label: &str) -> MenuItem

Create a menu item for this action.

Trait Implementations§

source§

impl<Name: Debug + ActionName> Debug for RelmAction<Name>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Name> RefUnwindSafe for RelmAction<Name>where Name: RefUnwindSafe,

§

impl<Name> !Send for RelmAction<Name>

§

impl<Name> !Sync for RelmAction<Name>

§

impl<Name> Unpin for RelmAction<Name>where Name: Unpin,

§

impl<Name> UnwindSafe for RelmAction<Name>where Name: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<C> AsyncPosition<()> for C

source§

fn position(_index: usize)

Returns the position. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<C> Position<()> for C

source§

fn position(&self, _index: usize)

Returns the position. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more