Function relm4::main_application

source ·
pub fn main_application() -> Application
Expand description

Returns the global gtk::Application that’s used internally by RelmApp.

Retrieving this value can be useful for graceful shutdown by calling ApplicationExt::quit() on it.

Note: The global application can be overwritten by calling RelmApp::with_app().

Examples found in repository?
relm4/examples/components.rs (line 210)
201
202
203
204
205
206
207
208
209
210
211
212
213
    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
        match msg {
            AppMsg::SetMode(mode) => {
                self.mode = mode;
            }
            AppMsg::CloseRequest => {
                self.dialog.emit(DialogMsg::Show);
            }
            AppMsg::Close => {
                relm4::main_application().quit();
            }
        }
    }
More examples
Hide additional examples
relm4-components/examples/alert.rs (line 77)
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
93
94
95
    fn update(&mut self, msg: AppMsg, _sender: ComponentSender<Self>) {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
            AppMsg::CloseRequest => {
                if self.counter == 42 {
                    relm4::main_application().quit();
                } else {
                    self.alert_toggle = !self.alert_toggle;
                    if self.alert_toggle {
                        self.dialog.emit(AlertMsg::Show);
                    } else {
                        self.second_dialog.emit(AlertMsg::Show);
                    }
                }
            }
            AppMsg::Save => {
                println!("* Open save dialog here *");
            }
            AppMsg::Close => {
                relm4::main_application().quit();
            }
            AppMsg::Ignore => (),
        }
    }
relm4/examples/actions.rs (line 67)
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 }
    }
relm4/examples/menu.rs (line 126)
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 }
    }