Make config serializable
This commit is contained in:
parent
fd42a47684
commit
c8b786d7dc
6 changed files with 104 additions and 73 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -826,6 +826,7 @@ dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"rfd",
|
"rfd",
|
||||||
"rust-embed",
|
"rust-embed",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ fontdb = "0.15.0"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
rfd = "0.12.0"
|
rfd = "0.12.0"
|
||||||
|
serde = { version = "1", features = ["serde_derive"] }
|
||||||
# Internationalization
|
# Internationalization
|
||||||
i18n-embed = { version = "0.13.4", features = ["fluent-system", "desktop-requester"] }
|
i18n-embed = { version = "0.13.4", features = ["fluent-system", "desktop-requester"] }
|
||||||
i18n-embed-fl = "0.6.4"
|
i18n-embed-fl = "0.6.4"
|
||||||
|
|
|
||||||
116
src/config.rs
116
src/config.rs
|
|
@ -1,87 +1,119 @@
|
||||||
use cosmic::iced::keyboard::{KeyCode, Modifiers};
|
use cosmic::{
|
||||||
|
cosmic_config::{self, cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry},
|
||||||
|
iced::keyboard::{KeyCode, Modifiers},
|
||||||
|
};
|
||||||
use cosmic_text::Metrics;
|
use cosmic_text::Metrics;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{collections::HashMap, fmt};
|
use std::{collections::HashMap, fmt};
|
||||||
|
|
||||||
use crate::{ContextPage, Message};
|
use crate::{ContextPage, Message};
|
||||||
|
|
||||||
// Makes key binding definitions simpler
|
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||||
const CTRL: Modifiers = Modifiers::CTRL;
|
pub enum Action {
|
||||||
const ALT: Modifiers = Modifiers::ALT;
|
Cut,
|
||||||
const SHIFT: Modifiers = Modifiers::SHIFT;
|
Copy,
|
||||||
|
Paste,
|
||||||
|
NewFile,
|
||||||
|
NewWindow,
|
||||||
|
OpenFileDialog,
|
||||||
|
Save,
|
||||||
|
Quit,
|
||||||
|
ToggleSettingsPage,
|
||||||
|
ToggleWordWrap,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
impl Action {
|
||||||
|
pub fn message(&self) -> Message {
|
||||||
|
match self {
|
||||||
|
Self::Cut => Message::Cut,
|
||||||
|
Self::Copy => Message::Copy,
|
||||||
|
Self::Paste => Message::Paste,
|
||||||
|
Self::NewFile => Message::NewFile,
|
||||||
|
Self::NewWindow => Message::NewWindow,
|
||||||
|
Self::OpenFileDialog => Message::OpenFileDialog,
|
||||||
|
Self::Save => Message::Save,
|
||||||
|
Self::Quit => Message::Quit,
|
||||||
|
Self::ToggleSettingsPage => Message::ToggleContextPage(ContextPage::Settings),
|
||||||
|
Self::ToggleWordWrap => Message::ToggleWordWrap,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||||
|
pub enum Modifier {
|
||||||
|
Super,
|
||||||
|
Ctrl,
|
||||||
|
Alt,
|
||||||
|
Shift,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||||
pub struct KeyBind {
|
pub struct KeyBind {
|
||||||
pub modifiers: Modifiers,
|
pub modifiers: Vec<Modifier>,
|
||||||
pub key_code: KeyCode,
|
pub key_code: KeyCode,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyBind {
|
impl KeyBind {
|
||||||
//TODO: load from config
|
//TODO: load from config
|
||||||
pub fn load() -> HashMap<KeyBind, Message> {
|
pub fn load() -> HashMap<KeyBind, Action> {
|
||||||
let mut keybinds = HashMap::new();
|
let mut keybinds = HashMap::new();
|
||||||
|
|
||||||
macro_rules! bind {
|
macro_rules! bind {
|
||||||
($modifiers:expr, $key_code:ident, $message:expr) => {{
|
([$($modifier:ident),+ $(,)?], $key_code:ident, $action:ident) => {{
|
||||||
keybinds.insert(
|
keybinds.insert(
|
||||||
KeyBind {
|
KeyBind {
|
||||||
modifiers: $modifiers,
|
modifiers: vec![$(Modifier::$modifier),+],
|
||||||
key_code: KeyCode::$key_code,
|
key_code: KeyCode::$key_code,
|
||||||
},
|
},
|
||||||
$message,
|
Action::$action,
|
||||||
);
|
);
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
bind!(CTRL, X, Message::Cut);
|
bind!([Ctrl], X, Cut);
|
||||||
bind!(CTRL, C, Message::Copy);
|
bind!([Ctrl], C, Copy);
|
||||||
bind!(CTRL, V, Message::Paste);
|
bind!([Ctrl], V, Paste);
|
||||||
bind!(CTRL, N, Message::NewFile);
|
bind!([Ctrl], N, NewFile);
|
||||||
bind!(CTRL | SHIFT, N, Message::NewWindow);
|
bind!([Ctrl, Shift], N, NewWindow);
|
||||||
bind!(CTRL, O, Message::OpenFileDialog);
|
bind!([Ctrl], O, OpenFileDialog);
|
||||||
bind!(CTRL, S, Message::Save);
|
bind!([Ctrl], S, Save);
|
||||||
bind!(CTRL, Q, Message::Quit);
|
bind!([Ctrl], Q, Quit);
|
||||||
bind!(
|
bind!([Ctrl], Comma, ToggleSettingsPage);
|
||||||
CTRL,
|
bind!([Alt], Z, ToggleWordWrap);
|
||||||
Comma,
|
|
||||||
Message::ToggleContextPage(ContextPage::Settings)
|
|
||||||
);
|
|
||||||
bind!(ALT, Z, Message::ToggleWordWrap);
|
|
||||||
|
|
||||||
keybinds
|
keybinds
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn matches(&self, modifiers: Modifiers, key_code: KeyCode) -> bool {
|
||||||
|
self.key_code == key_code
|
||||||
|
&& modifiers.logo() == self.modifiers.contains(&Modifier::Super)
|
||||||
|
&& modifiers.control() == self.modifiers.contains(&Modifier::Ctrl)
|
||||||
|
&& modifiers.alt() == self.modifiers.contains(&Modifier::Alt)
|
||||||
|
&& modifiers.shift() == self.modifiers.contains(&Modifier::Shift)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for KeyBind {
|
impl fmt::Display for KeyBind {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
if self.modifiers.logo() {
|
for modifier in self.modifiers.iter() {
|
||||||
write!(f, "Super + ")?;
|
write!(f, "{:?} + ", modifier)?;
|
||||||
}
|
|
||||||
if self.modifiers.control() {
|
|
||||||
write!(f, "Ctrl + ")?;
|
|
||||||
}
|
|
||||||
if self.modifiers.alt() {
|
|
||||||
write!(f, "Alt + ")?;
|
|
||||||
}
|
|
||||||
if self.modifiers.shift() {
|
|
||||||
write!(f, "Shift + ")?;
|
|
||||||
}
|
}
|
||||||
write!(f, "{:?}", self.key_code)
|
write!(f, "{:?}", self.key_code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub font_size: u16,
|
pub font_size: u16,
|
||||||
pub syntax_theme_dark: String,
|
pub syntax_theme_dark: String,
|
||||||
pub syntax_theme_light: String,
|
pub syntax_theme_light: String,
|
||||||
pub vim_bindings: bool,
|
pub vim_bindings: bool,
|
||||||
pub word_wrap: bool,
|
pub word_wrap: bool,
|
||||||
pub keybinds: HashMap<KeyBind, Message>,
|
pub keybinds: HashMap<KeyBind, Action>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Default for Config {
|
||||||
//TODO: load from cosmic-config
|
fn default() -> Self {
|
||||||
pub fn load() -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
font_size: 14,
|
font_size: 14,
|
||||||
syntax_theme_dark: "base16-eighties.dark".to_string(),
|
syntax_theme_dark: "base16-eighties.dark".to_string(),
|
||||||
|
|
@ -91,7 +123,9 @@ impl Config {
|
||||||
keybinds: KeyBind::load(),
|
keybinds: KeyBind::load(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
// Calculate metrics from font size
|
// Calculate metrics from font size
|
||||||
pub fn metrics(&self) -> Metrics {
|
pub fn metrics(&self) -> Metrics {
|
||||||
let font_size = self.font_size as f32;
|
let font_size = self.font_size as f32;
|
||||||
|
|
|
||||||
48
src/main.rs
48
src/main.rs
|
|
@ -20,7 +20,7 @@ use std::{
|
||||||
sync::Mutex,
|
sync::Mutex,
|
||||||
};
|
};
|
||||||
|
|
||||||
use config::{Config, KeyBind};
|
use config::Config;
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
mod localize;
|
mod localize;
|
||||||
|
|
@ -67,7 +67,7 @@ pub enum Message {
|
||||||
Copy,
|
Copy,
|
||||||
DefaultFont(usize),
|
DefaultFont(usize),
|
||||||
DefaultFontSize(usize),
|
DefaultFontSize(usize),
|
||||||
KeyBind(KeyBind),
|
Key(keyboard::Modifiers, keyboard::KeyCode),
|
||||||
NewFile,
|
NewFile,
|
||||||
NewWindow,
|
NewWindow,
|
||||||
OpenFileDialog,
|
OpenFileDialog,
|
||||||
|
|
@ -370,7 +370,7 @@ 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::load(),
|
config: Config::default(),
|
||||||
font_names,
|
font_names,
|
||||||
font_size_names,
|
font_size_names,
|
||||||
font_sizes,
|
font_sizes,
|
||||||
|
|
@ -511,21 +511,19 @@ impl cosmic::Application for App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Message::DefaultFontSize(index) => {
|
Message::DefaultFontSize(index) => match self.font_sizes.get(index) {
|
||||||
match self.font_sizes.get(index) {
|
Some(font_size) => {
|
||||||
Some(font_size) => {
|
self.config.font_size = *font_size;
|
||||||
self.config.font_size = *font_size;
|
self.save_config();
|
||||||
self.save_config();
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
log::warn!("failed to find font with index {}", index);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
None => {
|
||||||
Message::KeyBind(key_bind) => {
|
log::warn!("failed to find font with index {}", index);
|
||||||
for (config_key_bind, config_message) in self.config.keybinds.iter() {
|
}
|
||||||
if config_key_bind == &key_bind {
|
},
|
||||||
return self.update(config_message.clone());
|
Message::Key(modifiers, key_code) => {
|
||||||
|
for (key_bind, action) in self.config.keybinds.iter() {
|
||||||
|
if key_bind.matches(modifiers, key_code) {
|
||||||
|
return self.update(action.message());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -769,9 +767,11 @@ impl cosmic::Application for App {
|
||||||
)
|
)
|
||||||
.add(
|
.add(
|
||||||
widget::settings::item::builder(fl!("default-font-size")).control(
|
widget::settings::item::builder(fl!("default-font-size")).control(
|
||||||
widget::dropdown(&self.font_size_names, font_size_selected, |index| {
|
widget::dropdown(
|
||||||
Message::DefaultFontSize(index)
|
&self.font_size_names,
|
||||||
}),
|
font_size_selected,
|
||||||
|
|index| Message::DefaultFontSize(index),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.into(),
|
.into(),
|
||||||
|
|
@ -810,7 +810,8 @@ impl cosmic::Application for App {
|
||||||
|
|
||||||
match self.active_tab() {
|
match self.active_tab() {
|
||||||
Some(tab) => {
|
Some(tab) => {
|
||||||
tab_column = tab_column.push(text_box(&tab.editor, self.config.metrics()).padding(8));
|
tab_column =
|
||||||
|
tab_column.push(text_box(&tab.editor, self.config.metrics()).padding(8));
|
||||||
let status = match tab.editor.lock().unwrap().mode() {
|
let status = match tab.editor.lock().unwrap().mode() {
|
||||||
ViMode::Passthrough => {
|
ViMode::Passthrough => {
|
||||||
//TODO: status line
|
//TODO: status line
|
||||||
|
|
@ -853,10 +854,7 @@ impl cosmic::Application for App {
|
||||||
event::Event::Keyboard(keyboard::Event::KeyPressed {
|
event::Event::Keyboard(keyboard::Event::KeyPressed {
|
||||||
modifiers,
|
modifiers,
|
||||||
key_code,
|
key_code,
|
||||||
}) => Some(Message::KeyBind(KeyBind {
|
}) => Some(Message::Key(modifiers, key_code)),
|
||||||
modifiers,
|
|
||||||
key_code,
|
|
||||||
})),
|
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,9 +41,9 @@ pub fn menu_bar<'a>(config: &Config) -> Element<'a, Message> {
|
||||||
|
|
||||||
let find_key = |message: &Message| -> String {
|
let find_key = |message: &Message| -> String {
|
||||||
let mut key = String::new();
|
let mut key = String::new();
|
||||||
for (config_key_bind, config_message) in config.keybinds.iter() {
|
for (key_bind, action) in config.keybinds.iter() {
|
||||||
if config_message == message {
|
if &action.message() == message {
|
||||||
key = config_key_bind.to_string();
|
key = key_bind.to_string();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,7 @@ impl Tab {
|
||||||
let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace);
|
let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace);
|
||||||
|
|
||||||
let editor = SyntaxEditor::new(
|
let editor = SyntaxEditor::new(
|
||||||
Buffer::new(
|
Buffer::new(&mut FONT_SYSTEM.lock().unwrap(), config.metrics()),
|
||||||
&mut FONT_SYSTEM.lock().unwrap(),
|
|
||||||
config.metrics(),
|
|
||||||
),
|
|
||||||
&SYNTAX_SYSTEM,
|
&SYNTAX_SYSTEM,
|
||||||
config.syntax_theme(cosmic::theme::is_dark()),
|
config.syntax_theme(cosmic::theme::is_dark()),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue