Function 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::from_app().

Examples found in repository?
examples/components.rs (line 214)
205    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
206        match msg {
207            AppMsg::SetMode(mode) => {
208                self.mode = mode;
209            }
210            AppMsg::CloseRequest => {
211                self.dialog.emit(DialogMsg::Show);
212            }
213            AppMsg::Close => {
214                relm4::main_application().quit();
215            }
216        }
217    }
More examples
Hide additional examples
examples/multi_window.rs (line 80)
71    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
72        match msg {
73            Msg::Increment => {
74                self.counter = self.counter.wrapping_add(1);
75            }
76            Msg::Decrement => {
77                self.counter = self.counter.wrapping_sub(1);
78            }
79            Msg::NewWindow => {
80                let app = relm4::main_application();
81                let builder = Self::builder();
82
83                // Add window to the GTK application.
84                // This ensures that the app will live as long
85                // as at least one window exists.
86                app.add_window(&builder.root);
87
88                builder.launch(self.counter).detach_runtime();
89            }
90        }
91    }
examples/actions.rs (line 67)
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    }
examples/menu.rs (line 126)
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    }