From 987f3d96c087231efb96aefafed688d9944f94ed Mon Sep 17 00:00:00 2001 From: Gabriel Brand Date: Fri, 3 May 2024 15:02:22 +0200 Subject: Port AdwPreferencesWindow to AdwPreferencesDialog --- po/POTFILES | 4 +- src/application.rs | 5 +- src/furtherance.gresource.xml | 2 +- src/gtk/preferences_dialog.ui | 236 +++++++++++++++++++++++++++++++++ src/gtk/preferences_window.ui | 236 --------------------------------- src/meson.build | 2 +- src/ui.rs | 4 +- src/ui/preferences_dialog.rs | 294 +++++++++++++++++++++++++++++++++++++++++ src/ui/preferences_window.rs | 301 ------------------------------------------ 9 files changed, 539 insertions(+), 545 deletions(-) create mode 100644 src/gtk/preferences_dialog.ui delete mode 100644 src/gtk/preferences_window.ui create mode 100644 src/ui/preferences_dialog.rs delete mode 100644 src/ui/preferences_window.rs diff --git a/po/POTFILES b/po/POTFILES index e2abfcd..6489d4e 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -5,7 +5,7 @@ data/com.lakoliu.Furtherance.desktop.in # UI files src/gtk/dialogs.ui src/gtk/history_box.ui -src/gtk/preferences_window.ui +src/gtk/preferences_dialog.ui src/gtk/report.ui src/gtk/task_details.ui src/gtk/task_row.ui @@ -14,7 +14,7 @@ src/gtk/window.ui # Rust code src/application.rs src/database.rs -src/ui/preferences_window.rs +src/ui/preferences_dialog.rs src/ui/report.rs src/ui/task_details.rs src/ui/tasks_page.rs diff --git a/src/application.rs b/src/application.rs index 8484b91..d03fe97 100644 --- a/src/application.rs +++ b/src/application.rs @@ -25,7 +25,7 @@ use std::sync::Mutex; use crate::config; use crate::database; use crate::settings_manager; -use crate::ui::{FurPreferencesWindow, FurReport, FurtheranceWindow}; +use crate::ui::{FurPreferencesDialog, FurReport, FurtheranceWindow}; mod imp { use super::*; @@ -117,7 +117,8 @@ impl FurtheranceApplication { let preferences_action = gio::SimpleAction::new("preferences", None); preferences_action.connect_activate(clone!(@weak self as app => move |_, _| { - FurPreferencesWindow::new().show(); + let window = FurtheranceWindow::default(); + FurPreferencesDialog::new().present(&window); })); self.set_accels_for_action("app.preferences", &["comma"]); self.add_action(&preferences_action); diff --git a/src/furtherance.gresource.xml b/src/furtherance.gresource.xml index bd94858..96f7f67 100644 --- a/src/furtherance.gresource.xml +++ b/src/furtherance.gresource.xml @@ -2,7 +2,7 @@ gtk/history_box.ui - gtk/preferences_window.ui + gtk/preferences_dialog.ui gtk/report.ui gtk/style.css gtk/task_details.ui diff --git a/src/gtk/preferences_dialog.ui b/src/gtk/preferences_dialog.ui new file mode 100644 index 0000000..2971d88 --- /dev/null +++ b/src/gtk/preferences_dialog.ui @@ -0,0 +1,236 @@ + + + + diff --git a/src/gtk/preferences_window.ui b/src/gtk/preferences_window.ui deleted file mode 100644 index 4649c9e..0000000 --- a/src/gtk/preferences_window.ui +++ /dev/null @@ -1,236 +0,0 @@ - - - - diff --git a/src/meson.build b/src/meson.build index b2bde7b..d7afa7a 100644 --- a/src/meson.build +++ b/src/meson.build @@ -31,7 +31,7 @@ run_command( rust_sources = files( 'ui.rs', - 'ui/preferences_window.rs', + 'ui/preferences_dialog.rs', 'ui/report.rs', 'ui/task_details.rs', 'ui/task_row.rs', diff --git a/src/ui.rs b/src/ui.rs index d1ef0d3..ffc2431 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -15,7 +15,7 @@ // along with this program. If not, see . mod history_box; -mod preferences_window; +mod preferences_dialog; mod report; mod task_details; mod task_row; @@ -24,7 +24,7 @@ mod tasks_page; pub mod window; pub use history_box::FurHistoryBox; -pub use preferences_window::FurPreferencesWindow; +pub use preferences_dialog::FurPreferencesDialog; pub use report::FurReport; pub use task_details::FurTaskDetails; pub use task_row::FurTaskRow; diff --git a/src/ui/preferences_dialog.rs b/src/ui/preferences_dialog.rs new file mode 100644 index 0000000..2396aef --- /dev/null +++ b/src/ui/preferences_dialog.rs @@ -0,0 +1,294 @@ +// Furtherance - Track your time without being tracked +// Copyright (C) 2022 Ricky Kresslein +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use adw::prelude::*; +use adw::subclass::prelude::*; +use gettextrs::*; +use glib::clone; +use gtk::glib; +use gtk::CompositeTemplate; + +use crate::settings_manager; +use crate::ui::FurtheranceWindow; +use crate::FurtheranceApplication; +use crate::database; + +mod imp { + use super::*; + use glib::subclass; + + #[derive(Default, Debug, CompositeTemplate)] + #[template(resource = "/com/lakoliu/Furtherance/gtk/preferences_dialog.ui")] + pub struct FurPreferencesDialog { + // General Page + // Appearance Group + #[template_child] + pub appearance_group: TemplateChild, + #[template_child] + pub dark_theme_switch: TemplateChild, + + // Idle Group + #[template_child] + pub notify_of_idle_expander: TemplateChild, + #[template_child] + pub notify_of_idle_spin: TemplateChild, + + // Timer Group + #[template_child] + pub pomodoro_expander: TemplateChild, + #[template_child] + pub pomodoro_spin: TemplateChild, + #[template_child] + pub autosave_expander: TemplateChild, + #[template_child] + pub autosave_spin: TemplateChild, + #[template_child] + pub inclusive_total_switch: TemplateChild, + + // Tasks Page + // Task List Group + #[template_child] + pub delete_confirmation_switch: TemplateChild, + #[template_child] + pub limit_tasks_expander: TemplateChild, + #[template_child] + pub limit_days_spin: TemplateChild, + #[template_child] + pub show_daily_sums_switch: TemplateChild, + #[template_child] + pub show_seconds_switch: TemplateChild, + #[template_child] + pub show_tags_switch: TemplateChild, + + // Task Input Group + #[template_child] + pub autocomplete_switch: TemplateChild, + + // Data Page + // Reports Group + #[template_child] + pub week_start_combo: TemplateChild, + + // Database Group + #[template_child] + pub database_loc_row: TemplateChild, + #[template_child] + pub database_browse_btn: TemplateChild, + } + + #[glib::object_subclass] + impl ObjectSubclass for FurPreferencesDialog { + const NAME: &'static str = "FurPreferencesDialog"; + type ParentType = adw::PreferencesDialog; + type Type = super::FurPreferencesDialog; + + fn class_init(klass: &mut Self::Class) { + Self::bind_template(klass); + } + + fn instance_init(obj: &subclass::InitializingObject) { + obj.init_template(); + } + } + + impl ObjectImpl for FurPreferencesDialog { + fn constructed(&self) { + let obj = self.obj(); + obj.setup_signals(); + obj.setup_widgets(); + + self.parent_constructed(); + } + } + + impl WidgetImpl for FurPreferencesDialog {} + + impl AdwDialogImpl for FurPreferencesDialog {} + + impl PreferencesDialogImpl for FurPreferencesDialog {} +} + +glib::wrapper! { + pub struct FurPreferencesDialog( + ObjectSubclass) + @extends gtk::Widget, adw::Dialog, adw::PreferencesDialog; + +} + +impl FurPreferencesDialog { + pub fn new() -> Self { + glib::Object::new::() + } + + fn setup_widgets(&self) { + let imp = imp::FurPreferencesDialog::from_obj(self); + + let manager = adw::StyleManager::default(); + let support_darkmode = manager.system_supports_color_schemes(); + imp.appearance_group.set_visible(!support_darkmode); + + let db_dir = database::get_directory().to_string_lossy().to_string(); + imp.database_loc_row.set_subtitle(&db_dir); + } + + fn setup_signals(&self) { + let imp = imp::FurPreferencesDialog::from_obj(self); + + settings_manager::bind_property("dark-mode", &*imp.dark_theme_switch, "active"); + + settings_manager::bind_property( + "notify-of-idle", + &*imp.notify_of_idle_expander, + "enable-expansion", + ); + + settings_manager::bind_property("idle-time", &*imp.notify_of_idle_spin, "value"); + + settings_manager::bind_property( + "limit-tasks", + &*imp.limit_tasks_expander, + "enable-expansion", + ); + + settings_manager::bind_property("limit-days", &*imp.limit_days_spin, "value"); + + settings_manager::bind_property( + "delete-confirmation", + &*imp.delete_confirmation_switch, + "active", + ); + + settings_manager::bind_property("show-seconds", &*imp.show_seconds_switch, "active"); + + settings_manager::bind_property("show-daily-sums", &*imp.show_daily_sums_switch, "active"); + + settings_manager::bind_property("show-tags", &*imp.show_tags_switch, "active"); + + settings_manager::bind_property("autocomplete", &*imp.autocomplete_switch, "active"); + + settings_manager::bind_property("pomodoro", &*imp.pomodoro_expander, "enable-expansion"); + + settings_manager::bind_property("pomodoro-time", &*imp.pomodoro_spin, "value"); + + settings_manager::bind_property("autosave", &*imp.autosave_expander, "enable-expansion"); + + settings_manager::bind_property("autosave-time", &*imp.autosave_spin, "value"); + + settings_manager::bind_property("inclusive-total", &*imp.inclusive_total_switch, "active"); + + settings_manager::bind_property("week-starts", &*imp.week_start_combo, "selected"); + + imp.dark_theme_switch.connect_active_notify(move |_| { + let app = FurtheranceApplication::default(); + app.update_light_dark(); + }); + + imp.limit_tasks_expander + .connect_enable_expansion_notify(move |_| { + let window = FurtheranceWindow::default(); + window.reset_history_box(); + }); + + imp.limit_days_spin.connect_value_notify(move |_| { + let window = FurtheranceWindow::default(); + window.reset_history_box(); + }); + + imp.show_seconds_switch.connect_active_notify(move |_| { + let window = FurtheranceWindow::default(); + window.reset_history_box(); + }); + + imp.show_daily_sums_switch.connect_active_notify(move |_| { + let window = FurtheranceWindow::default(); + window.reset_history_box(); + }); + + imp.show_tags_switch.connect_active_notify(move |_| { + let window = FurtheranceWindow::default(); + window.reset_history_box(); + }); + + imp.autocomplete_switch.connect_active_notify(move |_| { + let window = FurtheranceWindow::default(); + window.reset_autocomplete(); + }); + + imp.pomodoro_expander + .connect_enable_expansion_notify(move |_| { + let window = FurtheranceWindow::default(); + window.refresh_timer(); + }); + + imp.pomodoro_spin.connect_value_notify(move |new_val| { + settings_manager::set_int("pomodoro-time", new_val.value() as i32); + let window = FurtheranceWindow::default(); + window.refresh_timer(); + }); + + imp.inclusive_total_switch.connect_active_notify(move |_| { + let window = FurtheranceWindow::default(); + window.reset_history_box(); + }); + + imp.database_browse_btn.connect_clicked(clone!(@weak self as this => move |_| { + let window = FurtheranceApplication::default().active_window().unwrap(); + let dialog = gtk::FileChooserDialog::new( + Some(&gettext("Backup Database")), + Some(&window), + gtk::FileChooserAction::Save, + &[ + (&gettext("Cancel"), gtk::ResponseType::Reject), + (&gettext("Save"), gtk::ResponseType::Accept), + ] + ); + dialog.set_modal(true); + + // Set a filter to show only SQLite files + let filter = gtk::FileFilter::new(); + gtk::FileFilter::set_name(&filter, Some("*.db")); + filter.add_mime_type("application/x-sqlite3"); + dialog.add_filter(&filter); + dialog.set_current_name("furtherance.db"); + + dialog.connect_response( + clone!(@strong dialog, @weak this as this2 => move |filechooser, resp| { + if resp == gtk::ResponseType::Accept { + if let Some(path) = filechooser.file().and_then(|file| file.path()) { + let path = &path.to_string_lossy(); + let _bkup = database::backup_db(path.to_string()); + + let settings = settings_manager::get_settings(); + let _ = settings.set_string("database-loc", &path.to_string()); + + let imp2 = imp::FurPreferencesDialog::from_obj(&this2); + imp2.database_loc_row.set_subtitle(&path.to_string()); + + let window = FurtheranceWindow::default(); + window.reset_history_box(); + } + dialog.close(); + } else { + dialog.close(); + } + }), + ); + + dialog.show(); + + })); + } +} diff --git a/src/ui/preferences_window.rs b/src/ui/preferences_window.rs deleted file mode 100644 index 90a28ff..0000000 --- a/src/ui/preferences_window.rs +++ /dev/null @@ -1,301 +0,0 @@ -// Furtherance - Track your time without being tracked -// Copyright (C) 2022 Ricky Kresslein -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -use adw::prelude::*; -use adw::subclass::prelude::*; -use gettextrs::*; -use glib::clone; -use gtk::glib; -use gtk::CompositeTemplate; - -use crate::settings_manager; -use crate::ui::FurtheranceWindow; -use crate::FurtheranceApplication; -use crate::database; - -mod imp { - use super::*; - use glib::subclass; - - #[derive(Default, Debug, CompositeTemplate)] - #[template(resource = "/com/lakoliu/Furtherance/gtk/preferences_window.ui")] - pub struct FurPreferencesWindow { - // General Page - // Appearance Group - #[template_child] - pub appearance_group: TemplateChild, - #[template_child] - pub dark_theme_switch: TemplateChild, - - // Idle Group - #[template_child] - pub notify_of_idle_expander: TemplateChild, - #[template_child] - pub notify_of_idle_spin: TemplateChild, - - // Timer Group - #[template_child] - pub pomodoro_expander: TemplateChild, - #[template_child] - pub pomodoro_spin: TemplateChild, - #[template_child] - pub autosave_expander: TemplateChild, - #[template_child] - pub autosave_spin: TemplateChild, - #[template_child] - pub inclusive_total_switch: TemplateChild, - - // Tasks Page - // Task List Group - #[template_child] - pub delete_confirmation_switch: TemplateChild, - #[template_child] - pub limit_tasks_expander: TemplateChild, - #[template_child] - pub limit_days_spin: TemplateChild, - #[template_child] - pub show_daily_sums_switch: TemplateChild, - #[template_child] - pub show_seconds_switch: TemplateChild, - #[template_child] - pub show_tags_switch: TemplateChild, - - // Task Input Group - #[template_child] - pub autocomplete_switch: TemplateChild, - - // Data Page - // Reports Group - #[template_child] - pub week_start_combo: TemplateChild, - - // Database Group - #[template_child] - pub database_loc_row: TemplateChild, - #[template_child] - pub database_browse_btn: TemplateChild, - } - - #[glib::object_subclass] - impl ObjectSubclass for FurPreferencesWindow { - const NAME: &'static str = "FurPreferencesWindow"; - type ParentType = adw::PreferencesWindow; - type Type = super::FurPreferencesWindow; - - fn class_init(klass: &mut Self::Class) { - Self::bind_template(klass); - } - - fn instance_init(obj: &subclass::InitializingObject) { - obj.init_template(); - } - } - - impl ObjectImpl for FurPreferencesWindow { - fn constructed(&self) { - let window = FurtheranceWindow::default(); - let obj = self.obj(); - obj.set_transient_for(Some(&window)); - - obj.setup_signals(); - obj.setup_widgets(); - - self.parent_constructed(); - } - } - - impl WidgetImpl for FurPreferencesWindow {} - - impl WindowImpl for FurPreferencesWindow {} - - impl AdwWindowImpl for FurPreferencesWindow {} - - impl PreferencesWindowImpl for FurPreferencesWindow {} -} - -glib::wrapper! { - pub struct FurPreferencesWindow( - ObjectSubclass) - @extends gtk::Widget, gtk::Window, adw::Window, adw::PreferencesWindow; - -} - -impl FurPreferencesWindow { - pub fn new() -> Self { - glib::Object::new::() - } - - fn setup_widgets(&self) { - self.set_search_enabled(false); - - let imp = imp::FurPreferencesWindow::from_obj(self); - - let manager = adw::StyleManager::default(); - let support_darkmode = manager.system_supports_color_schemes(); - imp.appearance_group.set_visible(!support_darkmode); - - let db_dir = database::get_directory().to_string_lossy().to_string(); - imp.database_loc_row.set_subtitle(&db_dir); - } - - fn setup_signals(&self) { - let imp = imp::FurPreferencesWindow::from_obj(self); - - settings_manager::bind_property("dark-mode", &*imp.dark_theme_switch, "active"); - - settings_manager::bind_property( - "notify-of-idle", - &*imp.notify_of_idle_expander, - "enable-expansion", - ); - - settings_manager::bind_property("idle-time", &*imp.notify_of_idle_spin, "value"); - - settings_manager::bind_property( - "limit-tasks", - &*imp.limit_tasks_expander, - "enable-expansion", - ); - - settings_manager::bind_property("limit-days", &*imp.limit_days_spin, "value"); - - settings_manager::bind_property( - "delete-confirmation", - &*imp.delete_confirmation_switch, - "active", - ); - - settings_manager::bind_property("show-seconds", &*imp.show_seconds_switch, "active"); - - settings_manager::bind_property("show-daily-sums", &*imp.show_daily_sums_switch, "active"); - - settings_manager::bind_property("show-tags", &*imp.show_tags_switch, "active"); - - settings_manager::bind_property("autocomplete", &*imp.autocomplete_switch, "active"); - - settings_manager::bind_property("pomodoro", &*imp.pomodoro_expander, "enable-expansion"); - - settings_manager::bind_property("pomodoro-time", &*imp.pomodoro_spin, "value"); - - settings_manager::bind_property("autosave", &*imp.autosave_expander, "enable-expansion"); - - settings_manager::bind_property("autosave-time", &*imp.autosave_spin, "value"); - - settings_manager::bind_property("inclusive-total", &*imp.inclusive_total_switch, "active"); - - settings_manager::bind_property("week-starts", &*imp.week_start_combo, "selected"); - - imp.dark_theme_switch.connect_active_notify(move |_| { - let app = FurtheranceApplication::default(); - app.update_light_dark(); - }); - - imp.limit_tasks_expander - .connect_enable_expansion_notify(move |_| { - let window = FurtheranceWindow::default(); - window.reset_history_box(); - }); - - imp.limit_days_spin.connect_value_notify(move |_| { - let window = FurtheranceWindow::default(); - window.reset_history_box(); - }); - - imp.show_seconds_switch.connect_active_notify(move |_| { - let window = FurtheranceWindow::default(); - window.reset_history_box(); - }); - - imp.show_daily_sums_switch.connect_active_notify(move |_| { - let window = FurtheranceWindow::default(); - window.reset_history_box(); - }); - - imp.show_tags_switch.connect_active_notify(move |_| { - let window = FurtheranceWindow::default(); - window.reset_history_box(); - }); - - imp.autocomplete_switch.connect_active_notify(move |_| { - let window = FurtheranceWindow::default(); - window.reset_autocomplete(); - }); - - imp.pomodoro_expander - .connect_enable_expansion_notify(move |_| { - let window = FurtheranceWindow::default(); - window.refresh_timer(); - }); - - imp.pomodoro_spin.connect_value_notify(move |new_val| { - settings_manager::set_int("pomodoro-time", new_val.value() as i32); - let window = FurtheranceWindow::default(); - window.refresh_timer(); - }); - - imp.inclusive_total_switch.connect_active_notify(move |_| { - let window = FurtheranceWindow::default(); - window.reset_history_box(); - }); - - imp.database_browse_btn.connect_clicked(clone!(@weak self as this => move |_| { - let window = FurtheranceApplication::default().active_window().unwrap(); - let dialog = gtk::FileChooserDialog::new( - Some(&gettext("Backup Database")), - Some(&window), - gtk::FileChooserAction::Save, - &[ - (&gettext("Cancel"), gtk::ResponseType::Reject), - (&gettext("Save"), gtk::ResponseType::Accept), - ] - ); - dialog.set_modal(true); - - // Set a filter to show only SQLite files - let filter = gtk::FileFilter::new(); - gtk::FileFilter::set_name(&filter, Some("*.db")); - filter.add_mime_type("application/x-sqlite3"); - dialog.add_filter(&filter); - dialog.set_current_name("furtherance.db"); - - dialog.connect_response( - clone!(@strong dialog, @weak this as this2 => move |filechooser, resp| { - if resp == gtk::ResponseType::Accept { - if let Some(path) = filechooser.file().and_then(|file| file.path()) { - let path = &path.to_string_lossy(); - let _bkup = database::backup_db(path.to_string()); - - let settings = settings_manager::get_settings(); - let _ = settings.set_string("database-loc", &path.to_string()); - - let imp2 = imp::FurPreferencesWindow::from_obj(&this2); - imp2.database_loc_row.set_subtitle(&path.to_string()); - - let window = FurtheranceWindow::default(); - window.reset_history_box(); - } - dialog.close(); - } else { - dialog.close(); - } - }), - ); - - dialog.show(); - - })); - } -} -- cgit 1.4.1