cosmic-edit/src/config.rs

111 lines
3 KiB
Rust
Raw Normal View History

use cosmic::iced::keyboard::{KeyCode, Modifiers};
2023-11-03 15:58:26 -06:00
use cosmic_text::Metrics;
use std::{collections::HashMap, fmt};
use crate::{ContextPage, Message};
2023-11-01 08:50:05 -06:00
// Makes key binding definitions simpler
const CTRL: Modifiers = Modifiers::CTRL;
2023-11-01 09:25:46 -06:00
const ALT: Modifiers = Modifiers::ALT;
2023-11-01 08:50:05 -06:00
const SHIFT: Modifiers = Modifiers::SHIFT;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct KeyBind {
pub modifiers: Modifiers,
pub key_code: KeyCode,
}
impl KeyBind {
//TODO: load from config
pub fn load() -> HashMap<KeyBind, Message> {
let mut keybinds = HashMap::new();
macro_rules! bind {
($modifiers:expr, $key_code:ident, $message:expr) => {{
keybinds.insert(
KeyBind {
2023-11-01 08:50:05 -06:00
modifiers: $modifiers,
key_code: KeyCode::$key_code,
},
$message,
);
}};
}
bind!(CTRL, X, Message::Cut);
bind!(CTRL, C, Message::Copy);
bind!(CTRL, V, Message::Paste);
bind!(CTRL, N, Message::NewFile);
bind!(CTRL | SHIFT, N, Message::NewWindow);
bind!(CTRL, O, Message::OpenFileDialog);
bind!(CTRL, S, Message::Save);
bind!(CTRL, Q, Message::Quit);
bind!(
CTRL,
Comma,
Message::ToggleContextPage(ContextPage::Settings)
);
bind!(ALT, Z, Message::ToggleWordWrap);
keybinds
}
}
impl fmt::Display for KeyBind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.modifiers.logo() {
write!(f, "Super + ")?;
}
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)
}
}
2023-11-03 15:58:26 -06:00
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Config {
2023-11-03 15:58:26 -06:00
pub font_size: u16,
pub syntax_theme_dark: String,
pub syntax_theme_light: String,
pub vim_bindings: bool,
2023-11-01 09:44:11 -06:00
pub word_wrap: bool,
pub keybinds: HashMap<KeyBind, Message>,
}
impl Config {
//TODO: load from cosmic-config
pub fn load() -> Self {
Self {
2023-11-03 15:58:26 -06:00
font_size: 14,
syntax_theme_dark: "base16-eighties.dark".to_string(),
syntax_theme_light: "base16-ocean.light".to_string(),
vim_bindings: false,
2023-11-01 09:44:11 -06:00
word_wrap: false,
keybinds: KeyBind::load(),
}
}
2023-11-03 15:58:26 -06:00
// Calculate metrics from font size
pub fn metrics(&self) -> Metrics {
let font_size = self.font_size as f32;
let line_height = (font_size * 1.4).ceil();
Metrics::new(font_size, line_height)
}
// Get current syntax theme based on dark mode
pub fn syntax_theme(&self, dark: bool) -> &str {
if dark {
&self.syntax_theme_dark
} else {
&self.syntax_theme_light
}
}
}