RelmAction

Struct 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>

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 76-79)
55    fn init(
56        _init: Self::Init,
57        root: Self::Root,
58        sender: ComponentSender<Self>,
59    ) -> ComponentParts<Self> {
60        let menu_model = gtk::gio::Menu::new();
61        menu_model.append(Some("Stateless"), Some(&ExampleAction::action_name()));
62
63        let model = Self { counter: 0 };
64
65        let widgets = view_output!();
66
67        let app = relm4::main_application();
68        app.set_accelerators_for_action::<ExampleAction>(&["<primary>W"]);
69
70        let action: RelmAction<ExampleAction> = RelmAction::new_stateless(move |_| {
71            println!("Statelesss action!");
72            sender.input(Msg::Increment);
73        });
74
75        let action2: RelmAction<ExampleU8Action> =
76            RelmAction::new_stateful_with_target_value(&0, |_, state, value| {
77                println!("Stateful action -> state: {state}, value: {value}");
78                *state += value;
79            });
80
81        let mut group = RelmActionGroup::<WindowActionGroup>::new();
82        group.add_action(action);
83        group.add_action(action2);
84        group.register_for_widget(&widgets.main_window);
85
86        ComponentParts { model, widgets }
87    }
More examples
Hide additional examples
relm4/examples/menu.rs (lines 137-140)
93    fn init(
94        counter: Self::Init,
95        root: Self::Root,
96        sender: ComponentSender<Self>,
97    ) -> ComponentParts<Self> {
98        // ============================================================
99        //
100        // You can also use menu! outside of the widget macro.
101        // This is the manual equivalent to the the menu! macro above.
102        //
103        // ============================================================
104        //
105        // relm4::menu! {
106        //     main_menu: {
107        //         custom: "my_widget",
108        //         "Example" => ExampleAction,
109        //         "Example2" => ExampleAction,
110        //         "Example toggle" => ExampleU8Action(1_u8),
111        //         section! {
112        //             "Section example" => ExampleAction,
113        //             "Example toggle" => ExampleU8Action(1_u8),
114        //         },
115        //         section! {
116        //             "Example" => ExampleAction,
117        //             "Example2" => ExampleAction,
118        //             "Example Value" => ExampleU8Action(1_u8),
119        //         }
120        //     }
121        // };
122
123        let model = Self { counter };
124        let widgets = view_output!();
125
126        let app = relm4::main_application();
127        app.set_accelerators_for_action::<ExampleAction>(&["<primary>W"]);
128
129        let action: RelmAction<ExampleAction> = {
130            RelmAction::new_stateless(move |_| {
131                println!("Statelesss action!");
132                sender.input(Msg::Increment);
133            })
134        };
135
136        let action2: RelmAction<ExampleU8Action> =
137            RelmAction::new_stateful_with_target_value(&0, |_, state, _value| {
138                *state ^= 1;
139                dbg!(state);
140            });
141
142        let mut group = RelmActionGroup::<WindowActionGroup>::new();
143        group.add_action(action);
144        group.add_action(action2);
145        group.register_for_widget(&widgets.main_window);
146
147        ComponentParts { model, widgets }
148    }
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 70-73)
55    fn init(
56        _init: Self::Init,
57        root: Self::Root,
58        sender: ComponentSender<Self>,
59    ) -> ComponentParts<Self> {
60        let menu_model = gtk::gio::Menu::new();
61        menu_model.append(Some("Stateless"), Some(&ExampleAction::action_name()));
62
63        let model = Self { counter: 0 };
64
65        let widgets = view_output!();
66
67        let app = relm4::main_application();
68        app.set_accelerators_for_action::<ExampleAction>(&["<primary>W"]);
69
70        let action: RelmAction<ExampleAction> = RelmAction::new_stateless(move |_| {
71            println!("Statelesss action!");
72            sender.input(Msg::Increment);
73        });
74
75        let action2: RelmAction<ExampleU8Action> =
76            RelmAction::new_stateful_with_target_value(&0, |_, state, value| {
77                println!("Stateful action -> state: {state}, value: {value}");
78                *state += value;
79            });
80
81        let mut group = RelmActionGroup::<WindowActionGroup>::new();
82        group.add_action(action);
83        group.add_action(action2);
84        group.register_for_widget(&widgets.main_window);
85
86        ComponentParts { model, widgets }
87    }
More examples
Hide additional examples
relm4/examples/menu.rs (lines 130-133)
93    fn init(
94        counter: Self::Init,
95        root: Self::Root,
96        sender: ComponentSender<Self>,
97    ) -> ComponentParts<Self> {
98        // ============================================================
99        //
100        // You can also use menu! outside of the widget macro.
101        // This is the manual equivalent to the the menu! macro above.
102        //
103        // ============================================================
104        //
105        // relm4::menu! {
106        //     main_menu: {
107        //         custom: "my_widget",
108        //         "Example" => ExampleAction,
109        //         "Example2" => ExampleAction,
110        //         "Example toggle" => ExampleU8Action(1_u8),
111        //         section! {
112        //             "Section example" => ExampleAction,
113        //             "Example toggle" => ExampleU8Action(1_u8),
114        //         },
115        //         section! {
116        //             "Example" => ExampleAction,
117        //             "Example2" => ExampleAction,
118        //             "Example Value" => ExampleU8Action(1_u8),
119        //         }
120        //     }
121        // };
122
123        let model = Self { counter };
124        let widgets = view_output!();
125
126        let app = relm4::main_application();
127        app.set_accelerators_for_action::<ExampleAction>(&["<primary>W"]);
128
129        let action: RelmAction<ExampleAction> = {
130            RelmAction::new_stateless(move |_| {
131                println!("Statelesss action!");
132                sender.input(Msg::Increment);
133            })
134        };
135
136        let action2: RelmAction<ExampleU8Action> =
137            RelmAction::new_stateful_with_target_value(&0, |_, state, _value| {
138                *state ^= 1;
139                dbg!(state);
140            });
141
142        let mut group = RelmActionGroup::<WindowActionGroup>::new();
143        group.add_action(action);
144        group.add_action(action2);
145        group.register_for_widget(&widgets.main_window);
146
147        ComponentParts { model, widgets }
148    }
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.

Source§

impl<Name: ActionName> RelmAction<Name>

Source

pub fn set_enabled(&self, enabled: bool)

Sets the action as enabled or disabled.

If disabled, the action cannot be activated anymore.

Source

pub fn gio_action(&self) -> &SimpleAction

Returns the inner gio::SimpleAction.

This method is meant for low level control. Only use it if you know exactly what you are doing.

Trait Implementations§

Source§

impl<Name: ActionName> Clone for RelmAction<Name>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<Name: ActionName> From<RelmAction<Name>> for SimpleAction

Source§

fn from(value: RelmAction<Name>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<Name> Freeze for RelmAction<Name>

§

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 T
where 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 T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

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 T
where U: From<T>,

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, I> Position<(), I> for C

Source§

fn position(&self, _index: &I)

Returns the position. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
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