Add config loading and saving
This commit is contained in:
parent
c8b786d7dc
commit
e4e35af9d3
2 changed files with 71 additions and 18 deletions
|
|
@ -8,6 +8,8 @@ use std::{collections::HashMap, fmt};
|
||||||
|
|
||||||
use crate::{ContextPage, Message};
|
use crate::{ContextPage, Message};
|
||||||
|
|
||||||
|
pub const CONFIG_VERSION: u64 = 1;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
Cut,
|
Cut,
|
||||||
|
|
@ -116,8 +118,8 @@ impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
font_size: 14,
|
font_size: 14,
|
||||||
syntax_theme_dark: "base16-eighties.dark".to_string(),
|
syntax_theme_dark: "gruvbox-dark".to_string(),
|
||||||
syntax_theme_light: "base16-ocean.light".to_string(),
|
syntax_theme_light: "gruvbox-light".to_string(),
|
||||||
vim_bindings: false,
|
vim_bindings: false,
|
||||||
word_wrap: false,
|
word_wrap: false,
|
||||||
keybinds: KeyBind::load(),
|
keybinds: KeyBind::load(),
|
||||||
|
|
|
||||||
83
src/main.rs
83
src/main.rs
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
app::{message, Command, Core, Settings},
|
app::{message, Command, Core, Settings},
|
||||||
|
cosmic_config::{self, CosmicConfigEntry},
|
||||||
executor,
|
executor,
|
||||||
iced::{
|
iced::{
|
||||||
clipboard, event, keyboard, subscription,
|
clipboard, event, keyboard, subscription,
|
||||||
|
|
@ -20,7 +21,7 @@ use std::{
|
||||||
sync::Mutex,
|
sync::Mutex,
|
||||||
};
|
};
|
||||||
|
|
||||||
use config::Config;
|
use config::{Config, CONFIG_VERSION};
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
mod localize;
|
mod localize;
|
||||||
|
|
@ -63,8 +64,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub enum Message {
|
pub enum Message {
|
||||||
Cut,
|
Config(Config),
|
||||||
Copy,
|
Copy,
|
||||||
|
Cut,
|
||||||
DefaultFont(usize),
|
DefaultFont(usize),
|
||||||
DefaultFontSize(usize),
|
DefaultFontSize(usize),
|
||||||
Key(keyboard::Modifiers, keyboard::KeyCode),
|
Key(keyboard::Modifiers, keyboard::KeyCode),
|
||||||
|
|
@ -106,6 +108,7 @@ pub struct App {
|
||||||
core: Core,
|
core: Core,
|
||||||
nav_model: segmented_button::SingleSelectModel,
|
nav_model: segmented_button::SingleSelectModel,
|
||||||
tab_model: segmented_button::SingleSelectModel,
|
tab_model: segmented_button::SingleSelectModel,
|
||||||
|
config_handler: Option<cosmic_config::Config>,
|
||||||
config: Config,
|
config: Config,
|
||||||
font_names: Vec<String>,
|
font_names: Vec<String>,
|
||||||
font_size_names: Vec<String>,
|
font_size_names: Vec<String>,
|
||||||
|
|
@ -239,7 +242,17 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_config(&mut self) {
|
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();
|
self.update_config();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -335,6 +348,24 @@ impl cosmic::Application for App {
|
||||||
|
|
||||||
/// Creates the application, and optionally emits command on initialize.
|
/// Creates the application, and optionally emits command on initialize.
|
||||||
fn init(core: Core, _flags: Self::Flags) -> (Self, Command<Self::Message>) {
|
fn init(core: Core, _flags: Self::Flags) -> (Self, Command<Self::Message>) {
|
||||||
|
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 font_names = {
|
||||||
let mut font_names = Vec::new();
|
let mut font_names = Vec::new();
|
||||||
let font_system = FONT_SYSTEM.lock().unwrap();
|
let font_system = FONT_SYSTEM.lock().unwrap();
|
||||||
|
|
@ -370,7 +401,8 @@ impl cosmic::Application for App {
|
||||||
core,
|
core,
|
||||||
nav_model: nav_bar::Model::builder().build(),
|
nav_model: nav_bar::Model::builder().build(),
|
||||||
tab_model: segmented_button::Model::builder().build(),
|
tab_model: segmented_button::Model::builder().build(),
|
||||||
config: Config::default(),
|
config_handler,
|
||||||
|
config,
|
||||||
font_names,
|
font_names,
|
||||||
font_size_names,
|
font_size_names,
|
||||||
font_sizes,
|
font_sizes,
|
||||||
|
|
@ -469,21 +501,29 @@ impl cosmic::Application for App {
|
||||||
|
|
||||||
fn update(&mut self, message: Message) -> Command<Message> {
|
fn update(&mut self, message: Message) -> Command<Message> {
|
||||||
match message {
|
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) => {
|
Some(tab) => {
|
||||||
let mut editor = tab.editor.lock().unwrap();
|
let editor = tab.editor.lock().unwrap();
|
||||||
let selection_opt = editor.copy_selection();
|
let selection_opt = editor.copy_selection();
|
||||||
editor.delete_selection();
|
|
||||||
if let Some(selection) = selection_opt {
|
if let Some(selection) = selection_opt {
|
||||||
return clipboard::write(selection);
|
return clipboard::write(selection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
},
|
},
|
||||||
Message::Copy => match self.active_tab() {
|
Message::Cut => match self.active_tab() {
|
||||||
Some(tab) => {
|
Some(tab) => {
|
||||||
let editor = tab.editor.lock().unwrap();
|
let mut editor = tab.editor.lock().unwrap();
|
||||||
let selection_opt = editor.copy_selection();
|
let selection_opt = editor.copy_selection();
|
||||||
|
editor.delete_selection();
|
||||||
if let Some(selection) = selection_opt {
|
if let Some(selection) = selection_opt {
|
||||||
return clipboard::write(selection);
|
return clipboard::write(selection);
|
||||||
}
|
}
|
||||||
|
|
@ -850,12 +890,23 @@ impl cosmic::Application for App {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> subscription::Subscription<Message> {
|
fn subscription(&self) -> subscription::Subscription<Message> {
|
||||||
subscription::events_with(|event, _status| match event {
|
subscription::Subscription::batch([
|
||||||
event::Event::Keyboard(keyboard::Event::KeyPressed {
|
subscription::events_with(|event, _status| match event {
|
||||||
modifiers,
|
event::Event::Keyboard(keyboard::Event::KeyPressed {
|
||||||
key_code,
|
modifiers,
|
||||||
}) => Some(Message::Key(modifiers, key_code)),
|
key_code,
|
||||||
_ => None,
|
}) => 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)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue