refactor: move sort names to state

This commit is contained in:
Ashley Wulber 2025-07-03 10:17:48 -04:00 committed by Jeremy Soller
parent 499ce1a7ec
commit 84b73b0bdc
3 changed files with 70 additions and 14 deletions

View file

@ -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<cosmic_config::Config>,
pub config: Config,
pub state_handler: Option<cosmic_config::Config>,
pub state: State,
pub mode: Mode,
pub locations: Vec<Location>,
}
@ -564,7 +566,9 @@ pub struct App {
nav_model: segmented_button::SingleSelectModel,
tab_model: segmented_button::Model<segmented_button::SingleSelect>,
config_handler: Option<cosmic_config::Config>,
state_handler: Option<cosmic_config::Config>,
config: Config,
state: State,
mode: Mode,
app_themes: Vec<String>,
compio_tx: mpsc::Sender<Pin<Box<dyn Future<Output = ()> + 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::<HashMap<String, (HeadingOptions, bool)>>(
if let Some(state_handler) = self.state_handler.as_ref() {
if let Err(err) = state_handler.set::<HashMap<String, (HeadingOptions, bool)>>(
"sort_names",
self.config.sort_names.clone(),
self.state.sort_names.clone(),
) {
log::warn!("Failed to save sort names: {:?}", err);
}

View file

@ -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<String, (HeadingOptions, bool)>,
}
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<cosmic_config::Config>, 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<cosmic_config::Update<Self>> {
struct ConfigSubscription;
cosmic_config::config_state_subscription(
TypeId::of::<ConfigSubscription>(),
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<String, (HeadingOptions, bool)>,
}
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),
)
})),
}
}
}

View file

@ -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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
let flags = Flags {
config_handler,
config,
state_handler,
state,
mode: app::Mode::Desktop,
locations,
};
@ -88,6 +93,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
let flags = Flags {
config_handler,
config,
state_handler,
state,
mode: app::Mode::App,
locations,
};