refactor: move sort names to state
This commit is contained in:
parent
499ce1a7ec
commit
84b73b0bdc
3 changed files with 70 additions and 14 deletions
20
src/app.rs
20
src/app.rs
|
|
@ -67,7 +67,6 @@ use trash::TrashItem;
|
||||||
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
|
#[cfg(all(feature = "wayland", feature = "desktop-applet"))]
|
||||||
use wayland_client::{protocol::wl_output::WlOutput, Proxy};
|
use wayland_client::{protocol::wl_output::WlOutput, Proxy};
|
||||||
|
|
||||||
use crate::dialog::DialogSettings;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
clipboard::{ClipboardCopy, ClipboardKind, ClipboardPaste},
|
clipboard::{ClipboardCopy, ClipboardKind, ClipboardPaste},
|
||||||
config::{
|
config::{
|
||||||
|
|
@ -89,6 +88,7 @@ use crate::{
|
||||||
spawn_detached::spawn_detached,
|
spawn_detached::spawn_detached,
|
||||||
tab::{self, HeadingOptions, ItemMetadata, Location, Tab, HOVER_DURATION},
|
tab::{self, HeadingOptions, ItemMetadata, Location, Tab, HOVER_DURATION},
|
||||||
};
|
};
|
||||||
|
use crate::{config::State, dialog::DialogSettings};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Mode {
|
pub enum Mode {
|
||||||
|
|
@ -100,6 +100,8 @@ pub enum Mode {
|
||||||
pub struct Flags {
|
pub struct Flags {
|
||||||
pub config_handler: Option<cosmic_config::Config>,
|
pub config_handler: Option<cosmic_config::Config>,
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
|
pub state_handler: Option<cosmic_config::Config>,
|
||||||
|
pub state: State,
|
||||||
pub mode: Mode,
|
pub mode: Mode,
|
||||||
pub locations: Vec<Location>,
|
pub locations: Vec<Location>,
|
||||||
}
|
}
|
||||||
|
|
@ -564,7 +566,9 @@ pub struct App {
|
||||||
nav_model: segmented_button::SingleSelectModel,
|
nav_model: segmented_button::SingleSelectModel,
|
||||||
tab_model: segmented_button::Model<segmented_button::SingleSelect>,
|
tab_model: segmented_button::Model<segmented_button::SingleSelect>,
|
||||||
config_handler: Option<cosmic_config::Config>,
|
config_handler: Option<cosmic_config::Config>,
|
||||||
|
state_handler: Option<cosmic_config::Config>,
|
||||||
config: Config,
|
config: Config,
|
||||||
|
state: State,
|
||||||
mode: Mode,
|
mode: Mode,
|
||||||
app_themes: Vec<String>,
|
app_themes: Vec<String>,
|
||||||
compio_tx: mpsc::Sender<Pin<Box<dyn Future<Output = ()> + Send>>>,
|
compio_tx: mpsc::Sender<Pin<Box<dyn Future<Output = ()> + Send>>>,
|
||||||
|
|
@ -879,7 +883,7 @@ impl App {
|
||||||
let mut tab = Tab::new(
|
let mut tab = Tab::new(
|
||||||
location.clone(),
|
location.clone(),
|
||||||
self.config.tab,
|
self.config.tab,
|
||||||
Some(&self.config.sort_names),
|
Some(&self.state.sort_names),
|
||||||
);
|
);
|
||||||
tab.mode = match self.mode {
|
tab.mode = match self.mode {
|
||||||
Mode::App => tab::Mode::App,
|
Mode::App => tab::Mode::App,
|
||||||
|
|
@ -1944,7 +1948,9 @@ impl Application for App {
|
||||||
nav_model: segmented_button::ModelBuilder::default().build(),
|
nav_model: segmented_button::ModelBuilder::default().build(),
|
||||||
tab_model: segmented_button::ModelBuilder::default().build(),
|
tab_model: segmented_button::ModelBuilder::default().build(),
|
||||||
config_handler: flags.config_handler,
|
config_handler: flags.config_handler,
|
||||||
|
state_handler: flags.state_handler,
|
||||||
config: flags.config,
|
config: flags.config,
|
||||||
|
state: flags.state,
|
||||||
mode: flags.mode,
|
mode: flags.mode,
|
||||||
app_themes,
|
app_themes,
|
||||||
compio_tx,
|
compio_tx,
|
||||||
|
|
@ -3596,7 +3602,7 @@ impl Application for App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tab::Command::SetSort(location, heading_options, direction) => {
|
tab::Command::SetSort(location, heading_options, direction) => {
|
||||||
self.config
|
self.state
|
||||||
.sort_names
|
.sort_names
|
||||||
.insert(location, (heading_options, direction));
|
.insert(location, (heading_options, direction));
|
||||||
if !self.must_save_sort_names {
|
if !self.must_save_sort_names {
|
||||||
|
|
@ -3630,7 +3636,7 @@ impl Application for App {
|
||||||
tab.parent_item_opt = parent_item_opt;
|
tab.parent_item_opt = parent_item_opt;
|
||||||
tab.set_items(items);
|
tab.set_items(items);
|
||||||
let sort = self
|
let sort = self
|
||||||
.config
|
.state
|
||||||
.sort_names
|
.sort_names
|
||||||
.get(&location.to_string())
|
.get(&location.to_string())
|
||||||
.unwrap_or_else(|| &(HeadingOptions::Name, true));
|
.unwrap_or_else(|| &(HeadingOptions::Name, true));
|
||||||
|
|
@ -4230,10 +4236,10 @@ impl Application for App {
|
||||||
}
|
}
|
||||||
Message::SaveSortNames => {
|
Message::SaveSortNames => {
|
||||||
self.must_save_sort_names = false;
|
self.must_save_sort_names = false;
|
||||||
if let Some(config_handler) = self.config_handler.as_ref() {
|
if let Some(state_handler) = self.state_handler.as_ref() {
|
||||||
if let Err(err) = config_handler.set::<HashMap<String, (HeadingOptions, bool)>>(
|
if let Err(err) = state_handler.set::<HashMap<String, (HeadingOptions, bool)>>(
|
||||||
"sort_names",
|
"sort_names",
|
||||||
self.config.sort_names.clone(),
|
self.state.sort_names.clone(),
|
||||||
) {
|
) {
|
||||||
log::warn!("Failed to save sort names: {:?}", err);
|
log::warn!("Failed to save sort names: {:?}", err);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,55 @@ pub enum TypeToSearch {
|
||||||
EnterPath,
|
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)]
|
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
|
@ -113,7 +162,6 @@ pub struct Config {
|
||||||
pub show_details: bool,
|
pub show_details: bool,
|
||||||
pub tab: TabConfig,
|
pub tab: TabConfig,
|
||||||
pub type_to_search: TypeToSearch,
|
pub type_to_search: TypeToSearch,
|
||||||
pub sort_names: HashMap<String, (HeadingOptions, bool)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
|
@ -162,12 +210,6 @@ impl Default for Config {
|
||||||
show_details: false,
|
show_details: false,
|
||||||
tab: TabConfig::default(),
|
tab: TabConfig::default(),
|
||||||
type_to_search: TypeToSearch::Recursive,
|
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),
|
|
||||||
)
|
|
||||||
})),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ mod mouse_area;
|
||||||
pub mod operation;
|
pub mod operation;
|
||||||
mod spawn_detached;
|
mod spawn_detached;
|
||||||
use tab::Location;
|
use tab::Location;
|
||||||
|
|
||||||
|
use crate::config::State;
|
||||||
pub mod tab;
|
pub mod tab;
|
||||||
mod thumbnailer;
|
mod thumbnailer;
|
||||||
|
|
||||||
|
|
@ -57,6 +59,7 @@ pub fn desktop() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
localize::localize();
|
localize::localize();
|
||||||
|
|
||||||
let (config_handler, config) = Config::load();
|
let (config_handler, config) = Config::load();
|
||||||
|
let (state_handler, state) = State::load();
|
||||||
|
|
||||||
let mut settings = Settings::default();
|
let mut settings = Settings::default();
|
||||||
settings = settings.theme(config.app_theme.theme());
|
settings = settings.theme(config.app_theme.theme());
|
||||||
|
|
@ -72,6 +75,8 @@ pub fn desktop() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let flags = Flags {
|
let flags = Flags {
|
||||||
config_handler,
|
config_handler,
|
||||||
config,
|
config,
|
||||||
|
state_handler,
|
||||||
|
state,
|
||||||
mode: app::Mode::Desktop,
|
mode: app::Mode::Desktop,
|
||||||
locations,
|
locations,
|
||||||
};
|
};
|
||||||
|
|
@ -88,6 +93,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
localize::localize();
|
localize::localize();
|
||||||
|
|
||||||
let (config_handler, config) = Config::load();
|
let (config_handler, config) = Config::load();
|
||||||
|
let (state_handler, state) = State::load();
|
||||||
|
|
||||||
let mut daemonize = true;
|
let mut daemonize = true;
|
||||||
let mut locations = Vec::new();
|
let mut locations = Vec::new();
|
||||||
|
|
@ -149,6 +155,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let flags = Flags {
|
let flags = Flags {
|
||||||
config_handler,
|
config_handler,
|
||||||
config,
|
config,
|
||||||
|
state_handler,
|
||||||
|
state,
|
||||||
mode: app::Mode::App,
|
mode: app::Mode::App,
|
||||||
locations,
|
locations,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue