refactor: apply limit on the number of persisted sort names
This commit is contained in:
parent
84b73b0bdc
commit
e4ffec63a1
5 changed files with 61 additions and 16 deletions
51
src/app.rs
51
src/app.rs
|
|
@ -86,7 +86,9 @@ use crate::{
|
|||
ReplaceResult,
|
||||
},
|
||||
spawn_detached::spawn_detached,
|
||||
tab::{self, HeadingOptions, ItemMetadata, Location, Tab, HOVER_DURATION},
|
||||
tab::{
|
||||
self, HeadingOptions, ItemMetadata, Location, Tab, HOVER_DURATION, SORT_OPTION_FALLBACK,
|
||||
},
|
||||
};
|
||||
use crate::{config::State, dialog::DialogSettings};
|
||||
|
||||
|
|
@ -3602,10 +3604,35 @@ impl Application for App {
|
|||
}
|
||||
}
|
||||
tab::Command::SetSort(location, heading_options, direction) => {
|
||||
self.state
|
||||
.sort_names
|
||||
.insert(location, (heading_options, direction));
|
||||
if !self.must_save_sort_names {
|
||||
let default_sort = tab::SORT_OPTION_FALLBACK
|
||||
.get(&location)
|
||||
.cloned()
|
||||
.unwrap_or((HeadingOptions::Name, true));
|
||||
let changed = if default_sort == (heading_options, direction) {
|
||||
self.state.sort_names.remove(&location).is_some()
|
||||
} else {
|
||||
// force reordering of inserted values so new settings are not dropped in the truncation step
|
||||
_ = self.state.sort_names.remove(&location);
|
||||
_ = self
|
||||
.state
|
||||
.sort_names
|
||||
.insert(location, (heading_options, direction))
|
||||
.is_none_or(|old| old != (heading_options, direction));
|
||||
|
||||
const MAX_SORT_NAMES: usize = 999;
|
||||
// TODO potentially configurable limit on max size?
|
||||
if self.state.sort_names.len() > MAX_SORT_NAMES {
|
||||
// truncate is not a good fit because it drops the items at the end, which are newest...
|
||||
self.state.sort_names = self
|
||||
.state
|
||||
.sort_names
|
||||
.split_off(self.state.sort_names.len() - MAX_SORT_NAMES);
|
||||
}
|
||||
|
||||
true
|
||||
};
|
||||
|
||||
if !self.must_save_sort_names & changed {
|
||||
self.must_save_sort_names = true;
|
||||
return cosmic::Task::perform(
|
||||
async move {
|
||||
|
|
@ -3635,10 +3662,12 @@ impl Application for App {
|
|||
if location == tab.location {
|
||||
tab.parent_item_opt = parent_item_opt;
|
||||
tab.set_items(items);
|
||||
let location_str = location.to_string();
|
||||
let sort = self
|
||||
.state
|
||||
.sort_names
|
||||
.get(&location.to_string())
|
||||
.get(&location_str)
|
||||
.or_else(|| SORT_OPTION_FALLBACK.get(&location_str))
|
||||
.unwrap_or_else(|| &(HeadingOptions::Name, true));
|
||||
|
||||
tab.sort_name = sort.0;
|
||||
|
|
@ -4237,10 +4266,12 @@ impl Application for App {
|
|||
Message::SaveSortNames => {
|
||||
self.must_save_sort_names = false;
|
||||
if let Some(state_handler) = self.state_handler.as_ref() {
|
||||
if let Err(err) = state_handler.set::<HashMap<String, (HeadingOptions, bool)>>(
|
||||
"sort_names",
|
||||
self.state.sort_names.clone(),
|
||||
) {
|
||||
if let Err(err) =
|
||||
state_handler.set::<ordermap::OrderMap<String, (HeadingOptions, bool)>>(
|
||||
"sort_names",
|
||||
self.state.sort_names.clone(),
|
||||
)
|
||||
{
|
||||
log::warn!("Failed to save sort names: {:?}", err);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use cosmic::{
|
|||
iced::Subscription,
|
||||
theme, Application,
|
||||
};
|
||||
use ordermap::OrderMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
|
|
@ -107,13 +108,13 @@ pub enum TypeToSearch {
|
|||
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
#[serde(default)]
|
||||
pub struct State {
|
||||
pub sort_names: HashMap<String, (HeadingOptions, bool)>,
|
||||
pub sort_names: ordermap::OrderMap<String, (HeadingOptions, bool)>,
|
||||
}
|
||||
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sort_names: HashMap::from_iter(dirs::download_dir().into_iter().map(|dir| {
|
||||
sort_names: OrderMap::from_iter(dirs::download_dir().into_iter().map(|dir| {
|
||||
(
|
||||
Location::Path(dir).normalize().to_string(),
|
||||
(HeadingOptions::Modified, false),
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ use icu::datetime::{
|
|||
};
|
||||
use mime_guess::{mime, Mime};
|
||||
use once_cell::sync::Lazy;
|
||||
use ordermap::OrderMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
|
|
@ -91,7 +92,7 @@ const THUMBNAIL_SIZE: u32 = (ICON_SIZE_GRID as u32) * (ICON_SCALE_MAX as u32);
|
|||
|
||||
const DRAG_SCROLL_DISTANCE: f32 = 15.0;
|
||||
|
||||
static SORT_OPTION_FALLBACK: LazyLock<HashMap<String, (HeadingOptions, bool)>> =
|
||||
pub(crate) static SORT_OPTION_FALLBACK: LazyLock<HashMap<String, (HeadingOptions, bool)>> =
|
||||
LazyLock::new(|| {
|
||||
HashMap::from_iter(dirs::download_dir().into_iter().map(|dir| {
|
||||
(
|
||||
|
|
@ -2366,7 +2367,7 @@ impl Tab {
|
|||
pub fn new(
|
||||
location: Location,
|
||||
config: TabConfig,
|
||||
sorting_options: Option<&HashMap<String, (HeadingOptions, bool)>>,
|
||||
sorting_options: Option<&OrderMap<String, (HeadingOptions, bool)>>,
|
||||
) -> Self {
|
||||
let location_str = location.to_string();
|
||||
let (sort_name, sort_direction) = sorting_options
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue