From e4e35af9d33a2d3557e779b0c92cb0ee56728bc2 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 3 Nov 2023 18:45:03 -0600 Subject: [PATCH] Add config loading and saving --- src/config.rs | 6 ++-- src/main.rs | 83 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 71 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index 21b3dff..fd083e9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,6 +8,8 @@ use std::{collections::HashMap, fmt}; use crate::{ContextPage, Message}; +pub const CONFIG_VERSION: u64 = 1; + #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] pub enum Action { Cut, @@ -116,8 +118,8 @@ impl Default for Config { fn default() -> Self { Self { font_size: 14, - syntax_theme_dark: "base16-eighties.dark".to_string(), - syntax_theme_light: "base16-ocean.light".to_string(), + syntax_theme_dark: "gruvbox-dark".to_string(), + syntax_theme_light: "gruvbox-light".to_string(), vim_bindings: false, word_wrap: false, keybinds: KeyBind::load(), diff --git a/src/main.rs b/src/main.rs index 3dbb3bf..48fb1a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use cosmic::{ app::{message, Command, Core, Settings}, + cosmic_config::{self, CosmicConfigEntry}, executor, iced::{ clipboard, event, keyboard, subscription, @@ -20,7 +21,7 @@ use std::{ sync::Mutex, }; -use config::Config; +use config::{Config, CONFIG_VERSION}; mod config; mod localize; @@ -63,8 +64,9 @@ fn main() -> Result<(), Box> { #[allow(dead_code)] #[derive(Clone, Debug, Eq, PartialEq)] pub enum Message { - Cut, + Config(Config), Copy, + Cut, DefaultFont(usize), DefaultFontSize(usize), Key(keyboard::Modifiers, keyboard::KeyCode), @@ -106,6 +108,7 @@ pub struct App { core: Core, nav_model: segmented_button::SingleSelectModel, tab_model: segmented_button::SingleSelectModel, + config_handler: Option, config: Config, font_names: Vec, font_size_names: Vec, @@ -239,7 +242,17 @@ impl App { } fn save_config(&mut self) { - //TODO: save config (work lost due to drive failure) + match self.config_handler { + Some(ref config_handler) => match self.config.write_entry(&config_handler) { + Ok(()) => {} + Err(err) => { + log::error!("failed to save config: {}", err); + } + }, + None => { + //TODO: log that there is no handler? + } + } self.update_config(); } @@ -335,6 +348,24 @@ impl cosmic::Application for App { /// Creates the application, and optionally emits command on initialize. fn init(core: Core, _flags: Self::Flags) -> (Self, Command) { + let (config_handler, config) = + match cosmic_config::Config::new(Self::APP_ID, CONFIG_VERSION) { + Ok(config_handler) => { + let config = match Config::get_entry(&config_handler) { + Ok(ok) => ok, + Err((errs, config)) => { + log::warn!("errors loading config: {:?}", errs); + config + } + }; + (Some(config_handler), config) + } + Err(err) => { + log::error!("failed to create config handler: {}", err); + (None, Config::default()) + } + }; + let font_names = { let mut font_names = Vec::new(); let font_system = FONT_SYSTEM.lock().unwrap(); @@ -370,7 +401,8 @@ impl cosmic::Application for App { core, nav_model: nav_bar::Model::builder().build(), tab_model: segmented_button::Model::builder().build(), - config: Config::default(), + config_handler, + config, font_names, font_size_names, font_sizes, @@ -469,21 +501,29 @@ impl cosmic::Application for App { fn update(&mut self, message: Message) -> Command { match message { - Message::Cut => match self.active_tab() { + Message::Config(config) => { + if config != self.config { + log::info!("update config"); + //TODO: update syntax theme by clearing tabs, only if needed + self.config = config; + self.update_config(); + } + } + Message::Copy => match self.active_tab() { Some(tab) => { - let mut editor = tab.editor.lock().unwrap(); + let editor = tab.editor.lock().unwrap(); let selection_opt = editor.copy_selection(); - editor.delete_selection(); if let Some(selection) = selection_opt { return clipboard::write(selection); } } None => {} }, - Message::Copy => match self.active_tab() { + Message::Cut => match self.active_tab() { Some(tab) => { - let editor = tab.editor.lock().unwrap(); + let mut editor = tab.editor.lock().unwrap(); let selection_opt = editor.copy_selection(); + editor.delete_selection(); if let Some(selection) = selection_opt { return clipboard::write(selection); } @@ -850,12 +890,23 @@ impl cosmic::Application for App { } fn subscription(&self) -> subscription::Subscription { - subscription::events_with(|event, _status| match event { - event::Event::Keyboard(keyboard::Event::KeyPressed { - modifiers, - key_code, - }) => Some(Message::Key(modifiers, key_code)), - _ => None, - }) + subscription::Subscription::batch([ + subscription::events_with(|event, _status| match event { + event::Event::Keyboard(keyboard::Event::KeyPressed { + modifiers, + key_code, + }) => Some(Message::Key(modifiers, key_code)), + _ => None, + }), + cosmic_config::config_subscription(0, Self::APP_ID.into(), CONFIG_VERSION).map( + |(_, res)| match res { + Ok(config) => Message::Config(config), + Err((errs, config)) => { + log::warn!("errors loading config: {:#?}", errs); + Message::Config(config) + } + }, + ), + ]) } }