From 14a4a23bc9b7b4cc1122685edf6089d1e4209d2f Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Tue, 30 May 2023 21:56:01 +0200 Subject: [PATCH] feat: use cosmic-config to remember active page --- Cargo.lock | 153 ---------------------------------------------- app/Cargo.toml | 2 - app/src/app.rs | 12 ++-- app/src/config.rs | 96 ++++++++--------------------- 4 files changed, 30 insertions(+), 233 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f491de..ecf64b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -383,18 +383,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6776fc96284a0bb647b615056fc496d1fe1644a7ab01829818a6d91cae888b84" -[[package]] -name = "bitvec" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" -dependencies = [ - "funty", - "radium", - "tap", - "wyz", -] - [[package]] name = "block" version = "0.1.6" @@ -440,28 +428,6 @@ dependencies = [ "utf8-width", ] -[[package]] -name = "bytecheck" -version = "0.6.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627" -dependencies = [ - "bytecheck_derive", - "ptr_meta", - "simdutf8", -] - -[[package]] -name = "bytecheck_derive" -version = "0.6.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7ec4c6f261935ad534c0c22dbef2201b45918860eb1c574b972bd213a76af61" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "bytemuck" version = "1.13.1" @@ -792,7 +758,6 @@ version = "0.1.0" dependencies = [ "apply", "async-channel", - "bytecheck", "color-eyre", "cosmic-panel-config", "cosmic-settings-page", @@ -807,7 +772,6 @@ dependencies = [ "libcosmic", "once_cell", "regex", - "rkyv", "rust-embed", "slotmap", "tokio", @@ -1642,12 +1606,6 @@ dependencies = [ "libc", ] -[[package]] -name = "funty" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" - [[package]] name = "futures" version = "0.3.28" @@ -3430,26 +3388,6 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "332cd62e95873ea4f41f3dfd6bbbfc5b52aec892d7e8d534197c4720a0bbbab2" -[[package]] -name = "ptr_meta" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" -dependencies = [ - "ptr_meta_derive", -] - -[[package]] -name = "ptr_meta_derive" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "qoi" version = "0.4.1" @@ -3477,12 +3415,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "radium" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" - [[package]] name = "rand" version = "0.8.5" @@ -3639,15 +3571,6 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" -[[package]] -name = "rend" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "581008d2099240d37fb08d77ad713bcaec2c4d89d50b5b21a8bb1996bbab68ab" -dependencies = [ - "bytecheck", -] - [[package]] name = "renderdoc-sys" version = "1.0.0" @@ -3681,34 +3604,6 @@ dependencies = [ "bytemuck", ] -[[package]] -name = "rkyv" -version = "0.7.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0200c8230b013893c0b2d6213d6ec64ed2b9be2e0e016682b7224ff82cff5c58" -dependencies = [ - "bitvec", - "bytecheck", - "hashbrown 0.12.3", - "ptr_meta", - "rend", - "rkyv_derive", - "seahash", - "tinyvec", - "uuid", -] - -[[package]] -name = "rkyv_derive" -version = "0.7.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2e06b915b5c230a17d7a736d1e2e63ee753c256a8614ef3f5147b13a4f5541d" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "ron" version = "0.8.0" @@ -3877,12 +3772,6 @@ dependencies = [ "tiny-skia 0.7.0", ] -[[package]] -name = "seahash" -version = "4.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" - [[package]] name = "self_cell" version = "0.10.2" @@ -4006,12 +3895,6 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "238abfbb77c1915110ad968465608b68e869e0772622c9656714e73e5a1a522f" -[[package]] -name = "simdutf8" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" - [[package]] name = "simplecss" version = "0.2.1" @@ -4288,12 +4171,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "tap" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" - [[package]] name = "tempfile" version = "3.5.0" @@ -4427,21 +4304,6 @@ dependencies = [ "zerovec", ] -[[package]] -name = "tinyvec" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - [[package]] name = "tokio" version = "1.28.2" @@ -4772,12 +4634,6 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" -[[package]] -name = "uuid" -version = "1.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "345444e32442451b267fc254ae85a209c64be56d2890e601a0c37ff0c3c5ecd2" - [[package]] name = "valuable" version = "0.1.0" @@ -5515,15 +5371,6 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60e49e42bdb1d5dc76f4cd78102f8f0714d32edfa3efb82286eb0f0b1fc0da0f" -[[package]] -name = "wyz" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" -dependencies = [ - "tap", -] - [[package]] name = "x11-dl" version = "2.21.0" diff --git a/app/Cargo.toml b/app/Cargo.toml index 9b294fe..1f28f33 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -8,7 +8,6 @@ rust-version = "1.65.0" [dependencies] apply = "0.3.0" async-channel = "1.8.0" -bytecheck = "0.6.11" color-eyre = "0.6.2" cosmic-settings-page = { path = "../page" } cosmic-settings-system = { path = "../pages/system" } @@ -20,7 +19,6 @@ i18n-embed-fl = "0.6.7" libcosmic = {workspace = true} once_cell = "1.17.2" regex = "1.8.3" -rkyv = { version = "0.7.42", features = ["validation"]} rust-embed = "6.6.1" slotmap = "1.0.6" tokio = "1.28.2" diff --git a/app/src/app.rs b/app/src/app.rs index 8cf95a6..b038305 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -28,7 +28,7 @@ use cosmic::{ }; use crate::{ - config::{self, Config}, + config::Config, pages::{ desktop::{self, panel}, sound, system, time, @@ -43,7 +43,6 @@ use std::process; pub struct SettingsApp { pub active_page: page::Entity, pub config: Config, - pub config_path: config::PathManager, pub debug: bool, pub is_condensed: bool, pub nav_bar_toggled_condensed: bool, @@ -87,13 +86,10 @@ impl Application for SettingsApp { type Theme = Theme; fn new(_: Self::Flags) -> (Self, Command) { - let mut config_path = config::PathManager::new(); - let mut app = SettingsApp { sharp_corners: false, active_page: page::Entity::default(), - config: config_path.config("main", Config::deserialize), - config_path, + config: Config::new(), debug: false, is_condensed: false, nav_bar: segmented_button::Model::default(), @@ -389,8 +385,8 @@ impl SettingsApp { if current_page != page { self.config.active_page = Box::from(&*self.pages.info[page].id); - self.config_path - .config("main", |path| self.config.serialize(path)); + self.config + .set_active_page(Box::from(&*self.pages.info[page].id)); } self.search_clear(); diff --git a/app/src/config.rs b/app/src/config.rs index 7a8360a..4f0e297 100644 --- a/app/src/config.rs +++ b/app/src/config.rs @@ -1,99 +1,55 @@ // Copyright 2023 System76 // SPDX-License-Identifier: GPL-3.0-only -mod path { - use std::path::Path; +use cosmic::cosmic_config::{self, ConfigGet, ConfigSet}; - #[must_use] +const NAME: &str = "com.system76.CosmicSettings"; - pub struct Manager { - base: String, - } - - impl Manager { - #[allow(clippy::new_without_default)] - pub fn new() -> Self { - let mut base = dirs::config_dir() - .expect("XDG config directory missing") - .join("cosmic-settings") - .into_os_string() - .into_string() - .expect("XDG config path is not UTF-8"); - - base.push('/'); - - if !Path::new(&base).exists() { - let _res = std::fs::create_dir_all(&base); - } - - Self { base } - } - - pub fn config(&mut self, page: &str, with: impl Fn(&Path) -> T) -> T { - let truncate_length = self.base.len(); - - self.base.push_str(page); - self.base.push_str(".config"); - - let output = with(Path::new(&self.base)); - self.base.truncate(truncate_length); - output - } - } -} - -pub use path::Manager as PathManager; - -use bytecheck::CheckBytes; -use rkyv::{ser::Serializer, Archive, Deserialize, Serialize}; -use std::{io::Read, path::Path}; +const ACTIVE_PAGE: &str = "active-page"; #[must_use] -#[repr(C)] -#[derive(Archive, Debug, Deserialize, Serialize)] -#[archive_attr(derive(CheckBytes, Debug))] +#[derive(Debug)] pub struct Config { + pub cosmic_config: Option, pub active_page: Box, } impl Config { - pub fn deserialize(path: &Path) -> Self { - let Ok(mut file) = std::fs::File::open(path) else { - return Self::default(); + pub fn new() -> Self { + let mut config = Self::default(); + + let context = match cosmic_config::Config::new(NAME, 1) { + Ok(context) => context, + Err(why) => { + tracing::warn!(?why, "failed to get config"); + return Self::default(); + } }; - let mut buffer = Vec::with_capacity(128); - if file.read_to_end(&mut buffer).is_err() { - return Self::default(); + if let Ok(page) = context.get::>(ACTIVE_PAGE) { + config.active_page = page; } - buffer.shrink_to_fit(); + config.cosmic_config = Some(context); - let Ok(archived) = rkyv::check_archived_root::(buffer.as_slice()) else { - return Self::default(); - }; - - archived - .deserialize(&mut rkyv::Infallible) - .unwrap_or_default() + config } - pub fn serialize(&self, path: &Path) { - let mut serializer = rkyv::ser::serializers::AllocSerializer::<0>::default(); + pub fn set_active_page(&mut self, page: Box) { + if let Some(context) = self.cosmic_config.as_ref() { + if let Err(why) = context.set::>(ACTIVE_PAGE, page.clone()) { + tracing::error!(?why, "failed to store active page ID"); + } + } - if serializer.serialize_value(self).is_err() { - return; - }; - - let bytes = serializer.into_serializer().into_inner(); - - let _res = std::fs::write(path, &bytes); + self.active_page = page; } } impl Default for Config { fn default() -> Self { Self { + cosmic_config: None, active_page: Box::from("desktop"), } }