mirror of
https://github.com/13hannes11/toolbx-tuner.git
synced 2024-09-03 23:21:00 +02:00
remove all code and start with relm4 0.6
This commit is contained in:
166
src/app.rs
Normal file
166
src/app.rs
Normal file
@@ -0,0 +1,166 @@
|
||||
use relm4::{
|
||||
actions::{RelmAction, RelmActionGroup},
|
||||
adw, gtk, main_application, Component, ComponentController, ComponentParts, ComponentSender,
|
||||
Controller, SimpleComponent,
|
||||
};
|
||||
|
||||
use gtk::prelude::{
|
||||
ApplicationExt, ApplicationWindowExt, GtkWindowExt, OrientableExt, SettingsExt, WidgetExt,
|
||||
};
|
||||
use gtk::{gio, glib};
|
||||
|
||||
use crate::config::{APP_ID, PROFILE};
|
||||
use crate::modals::about::AboutDialog;
|
||||
|
||||
pub(super) struct App {
|
||||
about_dialog: Controller<AboutDialog>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) enum AppMsg {
|
||||
Quit,
|
||||
}
|
||||
|
||||
relm4::new_action_group!(pub(super) WindowActionGroup, "win");
|
||||
//relm4::new_stateless_action!(PreferencesAction, WindowActionGroup, "preferences");
|
||||
relm4::new_stateless_action!(pub(super) ShortcutsAction, WindowActionGroup, "show-help-overlay");
|
||||
relm4::new_stateless_action!(AboutAction, WindowActionGroup, "about");
|
||||
|
||||
#[relm4::component(pub)]
|
||||
impl SimpleComponent for App {
|
||||
type Init = ();
|
||||
type Input = AppMsg;
|
||||
type Output = ();
|
||||
type Widgets = AppWidgets;
|
||||
|
||||
menu! {
|
||||
primary_menu: {
|
||||
section! {
|
||||
//"_Preferences" => PreferencesAction,
|
||||
"_Keyboard" => ShortcutsAction,
|
||||
"_About Toolbox Tuner" => AboutAction,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
view! {
|
||||
main_window = adw::ApplicationWindow::new(&main_application()) {
|
||||
connect_close_request[sender] => move |_| {
|
||||
sender.input(AppMsg::Quit);
|
||||
gtk::Inhibit(true)
|
||||
},
|
||||
|
||||
#[wrap(Some)]
|
||||
set_help_overlay: shortcuts = >k::Builder::from_resource(
|
||||
"/org/kuchelmeister/ToolboxTuner/gtk/help-overlay.ui"
|
||||
)
|
||||
.object::<gtk::ShortcutsWindow>("help_overlay")
|
||||
.unwrap() -> gtk::ShortcutsWindow {
|
||||
set_transient_for: Some(&main_window),
|
||||
set_application: Some(&main_application()),
|
||||
},
|
||||
|
||||
add_css_class?: if PROFILE == "Devel" {
|
||||
Some("devel")
|
||||
} else {
|
||||
None
|
||||
},
|
||||
|
||||
gtk::Box {
|
||||
set_orientation: gtk::Orientation::Vertical,
|
||||
|
||||
adw::HeaderBar {
|
||||
pack_end = >k::MenuButton {
|
||||
set_icon_name: "open-menu-symbolic",
|
||||
set_menu_model: Some(&primary_menu),
|
||||
}
|
||||
},
|
||||
|
||||
gtk::Label {
|
||||
set_label: "Hello world!",
|
||||
add_css_class: "title-header",
|
||||
set_vexpand: true,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn init(
|
||||
_init: Self::Init,
|
||||
root: &Self::Root,
|
||||
sender: ComponentSender<Self>,
|
||||
) -> ComponentParts<Self> {
|
||||
let about_dialog = AboutDialog::builder()
|
||||
.transient_for(root)
|
||||
.launch(())
|
||||
.detach();
|
||||
|
||||
let model = Self { about_dialog };
|
||||
|
||||
let widgets = view_output!();
|
||||
|
||||
let mut actions = RelmActionGroup::<WindowActionGroup>::new();
|
||||
|
||||
let shortcuts_action = {
|
||||
let shortcuts = widgets.shortcuts.clone();
|
||||
RelmAction::<ShortcutsAction>::new_stateless(move |_| {
|
||||
shortcuts.present();
|
||||
})
|
||||
};
|
||||
|
||||
let about_action = {
|
||||
let sender = model.about_dialog.sender().clone();
|
||||
RelmAction::<AboutAction>::new_stateless(move |_| {
|
||||
sender.send(()).unwrap();
|
||||
})
|
||||
};
|
||||
|
||||
actions.add_action(shortcuts_action);
|
||||
actions.add_action(about_action);
|
||||
actions.register_for_widget(&widgets.main_window);
|
||||
|
||||
widgets.load_window_size();
|
||||
|
||||
ComponentParts { model, widgets }
|
||||
}
|
||||
|
||||
fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
|
||||
match message {
|
||||
AppMsg::Quit => main_application().quit(),
|
||||
}
|
||||
}
|
||||
|
||||
fn shutdown(&mut self, widgets: &mut Self::Widgets, _output: relm4::Sender<Self::Output>) {
|
||||
widgets.save_window_size().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl AppWidgets {
|
||||
fn save_window_size(&self) -> Result<(), glib::BoolError> {
|
||||
let settings = gio::Settings::new(APP_ID);
|
||||
let (width, height) = self.main_window.default_size();
|
||||
|
||||
settings.set_int("window-width", width)?;
|
||||
settings.set_int("window-height", height)?;
|
||||
|
||||
settings.set_boolean("is-maximized", self.main_window.is_maximized())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_window_size(&self) {
|
||||
let settings = gio::Settings::new(APP_ID);
|
||||
|
||||
let width = settings.int("window-width");
|
||||
let height = settings.int("window-height");
|
||||
let is_maximized = settings.boolean("is-maximized");
|
||||
|
||||
self.main_window.set_default_size(width, height);
|
||||
|
||||
if is_maximized {
|
||||
self.main_window.maximize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user