From 529b56cd4a8a16283677eee9cd3a1b38b24a98f8 Mon Sep 17 00:00:00 2001 From: luxluth Date: Wed, 18 Dec 2024 11:05:33 +0100 Subject: [PATCH] Making config non reloadable --- src/conf.rs | 21 ---------------- src/main.rs | 72 ++++++++++++++--------------------------------------- 2 files changed, 19 insertions(+), 74 deletions(-) diff --git a/src/conf.rs b/src/conf.rs index 0cc33d9..2faed49 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -33,24 +33,9 @@ impl Default for GeneralConf { pub struct Config { pub general: GeneralConf, pub css: String, - pub css_path: PathBuf, - pub _conf_path: PathBuf, } impl Config { - fn reload_css(&mut self) { - let mut css = DEFAULT_CSS.to_string(); - let css_path = self.css_path.clone(); - if css_path.exists() { - if let Ok(mut f) = std::fs::File::open(&css_path) { - css = String::new(); - let _ = f.read_to_string(&mut css); - } - } - - self.css = css; - } - pub fn get_conf(conf_path: &PathBuf) -> GeneralConf { let mut general = GeneralConf::default(); if let Ok(mut f) = std::fs::File::open(conf_path) { @@ -112,10 +97,6 @@ impl Config { return general; } - pub fn reload(&mut self) { - self.reload_css(); - } - pub fn parse(path: std::path::PathBuf) -> Self { let mut css = DEFAULT_CSS.to_string(); let css_path = path.parent().unwrap().join("style.css"); @@ -133,8 +114,6 @@ impl Config { return Self { general: Self::get_conf(&path), css, - css_path, - _conf_path: path, }; } } diff --git a/src/main.rs b/src/main.rs index ffd5b34..082aae8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,9 @@ -use std::sync::Arc; -use tokio::runtime::Runtime; -use tokio::sync::RwLock; - use gtk::glib; use gtk::prelude::*; use gtk::{Application, ApplicationWindow}; use rust_i18n::t; use search::SearchManager; +use tokio::runtime::Runtime; mod app; mod bus; @@ -19,15 +16,10 @@ mod ui; rust_i18n::i18n!("locales", fallback = "en"); -async fn activate( - current_css_provider: Arc>, - config: Arc>, - app: &Application, -) { +fn activate(config: conf::Config, app: &Application) { let settings = gtk::Settings::default().expect("Failed to create GTK settings."); - settings.set_gtk_icon_theme_name(Some(&config.read().await.general.theme)); + settings.set_gtk_icon_theme_name(Some(&config.general.theme)); - let rt = Runtime::new().expect("Unable to create Runtime"); let window = ApplicationWindow::builder() .application(app) .title("seekr") @@ -52,34 +44,17 @@ async fn activate( .hexpand(true) .css_name("input") .activates_default(true) - .placeholder_text(&config.read().await.general.search_placeholder) + .placeholder_text(&config.general.search_placeholder) .build(); let represent_action = gtk::gio::SimpleAction::new("represent", None); - let conf_arc_clone = config.clone(); represent_action.connect_activate(glib::clone!( #[weak] window, #[strong] tomanager, - #[weak] - entry, move |_, _| { let _ = tomanager.send(search::SearchEvent::Represent); - let rt = Runtime::new().expect("Unable to create Runtime"); - rt.block_on(async { - let mut conf = conf_arc_clone.write().await; - let ccp = current_css_provider.read().await; - conf.reload(); - entry.set_placeholder_text(Some(&conf.general.search_placeholder.clone())); - let new_css_provider = load_css(conf.css.clone(), Some(ccp.clone())); - drop(ccp); - drop(conf); - { - let mut ccp = current_css_provider.write().await; - *ccp = new_css_provider; - } - }); window.present(); } )); @@ -239,9 +214,8 @@ async fn activate( entries_box.append(&title); } - let cfg = rt.block_on(async { config.clone().read().await.clone() }); for entry in entries { - let button = ui::EntryButton(&cfg, entry, &tomanager); + let button = ui::EntryButton(&config, entry, &tomanager); entries_box.append(&button); } @@ -267,7 +241,7 @@ async fn activate( } } -fn load_css(css: String, previous_provider: Option) -> gtk::CssProvider { +fn load_css(css: String, previous_provider: Option) { let provider = gtk::CssProvider::new(); provider.load_from_string(&css); @@ -283,38 +257,30 @@ fn load_css(css: String, previous_provider: Option) -> gtk::Cs &provider, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION, ); - - return provider; } fn main() { - let rt = Runtime::new().expect("Unable to create Runtime"); - let _enter = rt.enter(); - rust_i18n::set_locale(&locale::get_locale()); - tracing_subscriber::fmt() - .with_max_level(tracing::Level::DEBUG) - .with_thread_ids(true) - .with_timer(tracing_subscriber::fmt::time::time()) - .init(); - - let config = conf::Config::parse(conf::init_config_dir()); if bus::app_is_running() { bus::send_represent_event(); } else { + let rt = Runtime::new().expect("Unable to create Runtime"); + let _enter = rt.enter(); + rust_i18n::set_locale(&locale::get_locale()); + tracing_subscriber::fmt() + .with_max_level(tracing::Level::DEBUG) + .with_thread_ids(true) + .with_timer(tracing_subscriber::fmt::time::time()) + .init(); + + let config = conf::Config::parse(conf::init_config_dir()); + gtk::init().expect("Unable to init gtk"); - let current_css_provider = load_css(config.css.clone(), None); + load_css(config.css.clone(), None); let application = Application::new(Some(conf::APP_ID), Default::default()); application.connect_activate(move |app| { - rt.block_on(async { - activate( - Arc::new(RwLock::new(current_css_provider.clone())), - Arc::new(RwLock::new(config.clone())), - app, - ) - .await - }); + activate(config.clone(), app); }); application.run();