fix: set active page through state file

Closes #771
This commit is contained in:
Michael Aaron Murphy 2025-02-28 11:31:03 +01:00
parent b4f928d3c0
commit 61f2103b3e
No known key found for this signature in database
GPG key ID: B2732D4240C9212C
2 changed files with 30 additions and 42 deletions

View file

@ -58,6 +58,7 @@ use std::{borrow::Cow, str::FromStr};
#[allow(clippy::struct_excessive_bools)] #[allow(clippy::struct_excessive_bools)]
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
pub struct SettingsApp { pub struct SettingsApp {
last_active_page: Box<str>,
active_page: page::Entity, active_page: page::Entity,
active_context_page: Option<page::Entity>, active_context_page: Option<page::Entity>,
loaded_pages: BTreeSet<page::Entity>, loaded_pages: BTreeSet<page::Entity>,
@ -182,11 +183,14 @@ impl cosmic::Application for SettingsApp {
} }
fn init(core: Core, flags: Self::Flags) -> (Self, Task<Self::Message>) { fn init(core: Core, flags: Self::Flags) -> (Self, Task<Self::Message>) {
let config = Config::new();
let mut app = SettingsApp { let mut app = SettingsApp {
active_page: page::Entity::default(), active_page: page::Entity::default(),
active_context_page: None, active_context_page: None,
last_active_page: config.active_page(),
loaded_pages: BTreeSet::new(), loaded_pages: BTreeSet::new(),
config: Config::new(), config,
core, core,
nav_model: nav_bar::Model::default(), nav_model: nav_bar::Model::default(),
page_sender: None, page_sender: None,
@ -219,7 +223,7 @@ impl cosmic::Application for SettingsApp {
Some(p) => app.subtask_to_page(&p), Some(p) => app.subtask_to_page(&p),
None => app None => app
.pages .pages
.find_page_by_id(&app.config.active_page) .find_page_by_id(&app.last_active_page)
.map(|(id, _info)| id), .map(|(id, _info)| id),
} }
.unwrap_or(desktop_id); .unwrap_or(desktop_id);
@ -849,9 +853,8 @@ impl SettingsApp {
.unwrap_or(iced::Task::none()) .unwrap_or(iced::Task::none())
.map(Message::PageMessage) .map(Message::PageMessage)
.map(Into::into); .map(Into::into);
self.config.active_page = Box::from(&*self.pages.info[page].id); self.last_active_page = Box::from(&*self.pages.info[page].id);
self.config self.config.set_active_page(self.last_active_page.clone());
.set_active_page(Box::from(&*self.pages.info[page].id));
} }
self.search_clear(); self.search_clear();

View file

@ -8,68 +8,53 @@ use cosmic::{
const NAME: &str = "com.system76.CosmicSettings"; const NAME: &str = "com.system76.CosmicSettings";
const ACTIVE_PAGE: &str = "active-page"; const ACTIVE_PAGE: &str = "active_page";
const ACCENT_PALETTE_DARK: &str = "accent_palette_dark"; const ACCENT_PALETTE_DARK: &str = "accent_palette_dark";
const ACCENT_PALETTE_LIGHT: &str = "accent_palette_light"; const ACCENT_PALETTE_LIGHT: &str = "accent_palette_light";
#[must_use] #[must_use]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Config { pub struct Config {
pub cosmic_config: Option<cosmic_config::Config>, config: cosmic_config::Config,
pub active_page: Box<str>, state: cosmic_config::Config,
} }
impl Config { impl Config {
pub fn new() -> Self { pub fn new() -> Self {
let mut config = Self::default(); let config = match cosmic_config::Config::new(NAME, 1) {
Ok(config) => config,
let context = match cosmic_config::Config::new(NAME, 1) {
Ok(context) => context,
Err(why) => { Err(why) => {
tracing::warn!(?why, "failed to get config"); panic!("failed to get {NAME} config: {:?}", why);
return Self::default();
} }
}; };
if let Ok(page) = context.get::<Box<str>>(ACTIVE_PAGE) { let state = match cosmic_config::Config::new_state(NAME, 1) {
config.active_page = page; Ok(state) => state,
} Err(why) => {
panic!("failed to get {NAME} state: {:?}", why);
}
};
config.cosmic_config = Some(context); Self { config, state }
config
} }
pub fn accent_palette_dark(&self) -> Result<Vec<Srgba>, cosmic_config::Error> { pub fn accent_palette_dark(&self) -> Result<Vec<Srgba>, cosmic_config::Error> {
self.cosmic_config self.config.get::<Vec<Srgba>>(ACCENT_PALETTE_DARK)
.as_ref()
.unwrap()
.get::<Vec<Srgba>>(ACCENT_PALETTE_DARK)
} }
pub fn accent_palette_light(&self) -> Result<Vec<Srgba>, cosmic_config::Error> { pub fn accent_palette_light(&self) -> Result<Vec<Srgba>, cosmic_config::Error> {
self.cosmic_config self.config.get::<Vec<Srgba>>(ACCENT_PALETTE_LIGHT)
.as_ref()
.unwrap()
.get::<Vec<Srgba>>(ACCENT_PALETTE_LIGHT)
} }
pub fn set_active_page(&mut self, page: Box<str>) { pub fn active_page(&self) -> Box<str> {
if let Some(context) = self.cosmic_config.as_ref() { self.state
if let Err(why) = context.set::<Box<str>>(ACTIVE_PAGE, page.clone()) { .get::<Box<str>>(ACTIVE_PAGE)
tracing::error!(?why, "failed to store active page ID"); .unwrap_or_else(|_| Box::from("desktop"))
}
}
self.active_page = page;
} }
}
impl Default for Config { pub fn set_active_page(&self, page: Box<str>) {
fn default() -> Self { if let Err(why) = self.state.set::<Box<str>>(ACTIVE_PAGE, page.clone()) {
Self { tracing::error!(?why, "failed to store active page ID");
cosmic_config: None,
active_page: Box::from("desktop"),
} }
} }
} }