From de54ec676f9eea35d520cb82cd0ef5fafeb68ded Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 21 Mar 2024 17:01:40 -0400 Subject: [PATCH] Make sure the settings get saved --- kifu/gtk/src/main.rs | 36 +++++++++++++++++++++++++++++++--- kifu/gtk/src/views/settings.rs | 6 ++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/kifu/gtk/src/main.rs b/kifu/gtk/src/main.rs index 2b6da10..0eaf729 100644 --- a/kifu/gtk/src/main.rs +++ b/kifu/gtk/src/main.rs @@ -1,6 +1,8 @@ use adw::prelude::*; +use async_std::channel::Receiver; +use async_std::task::spawn; use gio::ActionEntry; -use kifu_core::{Config, ConfigOption, Core}; +use kifu_core::{Config, ConfigOption, Core, CoreNotification, LibraryPath, Observable}; use kifu_gtk::{ perftrace, // ui::{ConfigurationPage, Home, PlayingField}, @@ -14,6 +16,29 @@ const APP_ID_PROD: &str = "com.luminescent-dreams.kifu-gtk"; const RESOURCE_BASE_PATH: &str = "/com/luminescent-dreams/kifu-gtk/"; +async fn handler(notifications: Receiver, app_id: String) { + loop { + let msg = notifications.recv().await; + match msg { + Ok(CoreNotification::ConfigurationUpdated(cfg)) => { + println!("commiting configuration"); + let settings = gio::Settings::new(&app_id); + if let Some(LibraryPath(library_path)) = cfg.get() { + let _ = settings.set_string( + "library-path", + &library_path.into_os_string().into_string().unwrap(), + ); + } + } + Ok(_) => println!("discarding message"), + Err(err) => { + println!("shutting down handler with error: {:?}", err); + return; + } + } + } +} + /* fn handle_response(api: CoreApi, app_window: &AppWindow, message: CoreResponse) { let playing_field = Arc::new(RwLock::new(None)); @@ -107,6 +132,12 @@ fn main() { let core = Core::new(config); + spawn({ + let notifier = core.subscribe(); + let app_id = app_id.to_owned(); + handler(notifier, app_id) + }); + /* let core_handle = runtime.spawn({ let core = core.clone(); @@ -121,14 +152,13 @@ fn main() { .resource_base_path("/com/luminescent-dreams/kifu-gtk") .build(); - app.connect_activate({ let runtime = runtime.clone(); move |app| { let mut app_window = AppWindow::new(app, core.clone()); match *core.library() { - Some(_) => {}, + Some(_) => {} None => app_window.open_settings(), } diff --git a/kifu/gtk/src/views/settings.rs b/kifu/gtk/src/views/settings.rs index 9b6338b..4244bed 100644 --- a/kifu/gtk/src/views/settings.rs +++ b/kifu/gtk/src/views/settings.rs @@ -14,7 +14,7 @@ General Public License for more details. You should have received a copy of the GNU General Public License along with Kifu. If not, see . */ -use std::{cell::RefCell, path::Path, rc::Rc}; +use std::{cell::RefCell, path::Path, rc::Rc, borrow::Cow}; use adw::prelude::*; use glib::Object; @@ -23,6 +23,7 @@ use kifu_core::{Config, ConfigOption, LibraryPath}; fn library_chooser_row( parent: &impl IsA, + library_path: Option, on_library_chosen: Rc, ) -> adw::ActionRow { let dialog = gtk::FileDialog::builder().build(); @@ -36,7 +37,7 @@ fn library_chooser_row( let library_row = adw::ActionRow::builder() .title("Library Path") - .subtitle("No library set") + .subtitle(library_path.map(|LibraryPath(path)| path.to_string_lossy().into_owned()).unwrap_or("No library set".to_owned())) .css_classes(["preference-item"]) .build(); @@ -104,6 +105,7 @@ impl SettingsView { let library_row = library_chooser_row( parent, + config.borrow().get(), Rc::new({ let config = config.clone(); move |library_path| {