From 84b73b0bdc05a7fdce7e952f8863f6c12674c06c Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 3 Jul 2025 10:17:48 -0400 Subject: [PATCH] refactor: move sort names to state --- src/app.rs | 20 +++++++++++------- src/config.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++------- src/lib.rs | 8 ++++++++ 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/src/app.rs b/src/app.rs index 60aa0be..4ab0e73 100644 --- a/src/app.rs +++ b/src/app.rs @@ -67,7 +67,6 @@ use trash::TrashItem; #[cfg(all(feature = "wayland", feature = "desktop-applet"))] use wayland_client::{protocol::wl_output::WlOutput, Proxy}; -use crate::dialog::DialogSettings; use crate::{ clipboard::{ClipboardCopy, ClipboardKind, ClipboardPaste}, config::{ @@ -89,6 +88,7 @@ use crate::{ spawn_detached::spawn_detached, tab::{self, HeadingOptions, ItemMetadata, Location, Tab, HOVER_DURATION}, }; +use crate::{config::State, dialog::DialogSettings}; #[derive(Clone, Debug)] pub enum Mode { @@ -100,6 +100,8 @@ pub enum Mode { pub struct Flags { pub config_handler: Option, pub config: Config, + pub state_handler: Option, + pub state: State, pub mode: Mode, pub locations: Vec, } @@ -564,7 +566,9 @@ pub struct App { nav_model: segmented_button::SingleSelectModel, tab_model: segmented_button::Model, config_handler: Option, + state_handler: Option, config: Config, + state: State, mode: Mode, app_themes: Vec, compio_tx: mpsc::Sender + Send>>>, @@ -879,7 +883,7 @@ impl App { let mut tab = Tab::new( location.clone(), self.config.tab, - Some(&self.config.sort_names), + Some(&self.state.sort_names), ); tab.mode = match self.mode { Mode::App => tab::Mode::App, @@ -1944,7 +1948,9 @@ impl Application for App { nav_model: segmented_button::ModelBuilder::default().build(), tab_model: segmented_button::ModelBuilder::default().build(), config_handler: flags.config_handler, + state_handler: flags.state_handler, config: flags.config, + state: flags.state, mode: flags.mode, app_themes, compio_tx, @@ -3596,7 +3602,7 @@ impl Application for App { } } tab::Command::SetSort(location, heading_options, direction) => { - self.config + self.state .sort_names .insert(location, (heading_options, direction)); if !self.must_save_sort_names { @@ -3630,7 +3636,7 @@ impl Application for App { tab.parent_item_opt = parent_item_opt; tab.set_items(items); let sort = self - .config + .state .sort_names .get(&location.to_string()) .unwrap_or_else(|| &(HeadingOptions::Name, true)); @@ -4230,10 +4236,10 @@ impl Application for App { } Message::SaveSortNames => { self.must_save_sort_names = false; - if let Some(config_handler) = self.config_handler.as_ref() { - if let Err(err) = config_handler.set::>( + if let Some(state_handler) = self.state_handler.as_ref() { + if let Err(err) = state_handler.set::>( "sort_names", - self.config.sort_names.clone(), + self.state.sort_names.clone(), ) { log::warn!("Failed to save sort names: {:?}", err); } diff --git a/src/config.rs b/src/config.rs index 6dc69b8..eb978e3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -104,6 +104,55 @@ pub enum TypeToSearch { EnterPath, } +#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(default)] +pub struct State { + pub sort_names: HashMap, +} + +impl Default for State { + fn default() -> Self { + Self { + sort_names: HashMap::from_iter(dirs::download_dir().into_iter().map(|dir| { + ( + Location::Path(dir).normalize().to_string(), + (HeadingOptions::Modified, false), + ) + })), + } + } +} + +impl State { + pub fn load() -> (Option, Self) { + match cosmic_config::Config::new_state(App::APP_ID, CONFIG_VERSION) { + Ok(config_handler) => { + let config = match State::get_entry(&config_handler) { + Ok(ok) => ok, + Err((errs, config)) => { + log::info!("errors loading config: {:?}", errs); + config + } + }; + (Some(config_handler), config) + } + Err(err) => { + log::error!("failed to create config handler: {}", err); + (None, State::default()) + } + } + } + + pub fn subscription() -> Subscription> { + struct ConfigSubscription; + cosmic_config::config_state_subscription( + TypeId::of::(), + App::APP_ID.into(), + CONFIG_VERSION, + ) + } +} + #[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(default)] pub struct Config { @@ -113,7 +162,6 @@ pub struct Config { pub show_details: bool, pub tab: TabConfig, pub type_to_search: TypeToSearch, - pub sort_names: HashMap, } impl Config { @@ -162,12 +210,6 @@ impl Default for Config { show_details: false, tab: TabConfig::default(), type_to_search: TypeToSearch::Recursive, - sort_names: HashMap::from_iter(dirs::download_dir().into_iter().map(|dir| { - ( - Location::Path(dir).normalize().to_string(), - (HeadingOptions::Modified, false), - ) - })), } } } diff --git a/src/lib.rs b/src/lib.rs index a8596d0..556fbef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,8 @@ mod mouse_area; pub mod operation; mod spawn_detached; use tab::Location; + +use crate::config::State; pub mod tab; mod thumbnailer; @@ -57,6 +59,7 @@ pub fn desktop() -> Result<(), Box> { localize::localize(); let (config_handler, config) = Config::load(); + let (state_handler, state) = State::load(); let mut settings = Settings::default(); settings = settings.theme(config.app_theme.theme()); @@ -72,6 +75,8 @@ pub fn desktop() -> Result<(), Box> { let flags = Flags { config_handler, config, + state_handler, + state, mode: app::Mode::Desktop, locations, }; @@ -88,6 +93,7 @@ pub fn main() -> Result<(), Box> { localize::localize(); let (config_handler, config) = Config::load(); + let (state_handler, state) = State::load(); let mut daemonize = true; let mut locations = Vec::new(); @@ -149,6 +155,8 @@ pub fn main() -> Result<(), Box> { let flags = Flags { config_handler, config, + state_handler, + state, mode: app::Mode::App, locations, };