2023-10-30 10:19:52 -06:00
|
|
|
use cosmic::iced::keyboard::{KeyCode, Modifiers};
|
|
|
|
|
use std::{collections::HashMap, fmt};
|
|
|
|
|
|
2023-11-01 14:19:39 -06:00
|
|
|
use crate::{ContextPage, Message};
|
2023-10-30 10:19:52 -06:00
|
|
|
|
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;
|
|
|
|
|
|
2023-10-30 10:19:52 -06:00
|
|
|
#[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 {
|
2023-11-01 14:19:39 -06:00
|
|
|
($modifiers:expr, $key_code:ident, $message:expr) => {{
|
2023-10-30 10:19:52 -06:00
|
|
|
keybinds.insert(
|
|
|
|
|
KeyBind {
|
2023-11-01 08:50:05 -06:00
|
|
|
modifiers: $modifiers,
|
2023-10-30 10:19:52 -06:00
|
|
|
key_code: KeyCode::$key_code,
|
|
|
|
|
},
|
2023-11-01 14:19:39 -06:00
|
|
|
$message,
|
2023-10-30 10:19:52 -06:00
|
|
|
);
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 14:19:39 -06:00
|
|
|
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);
|
2023-10-30 10:19:52 -06:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct Config {
|
2023-11-01 14:19:39 -06:00
|
|
|
pub vim_bindings: bool,
|
2023-11-01 09:44:11 -06:00
|
|
|
pub word_wrap: bool,
|
2023-10-30 10:19:52 -06:00
|
|
|
pub keybinds: HashMap<KeyBind, Message>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
|
//TODO: load from cosmic-config
|
|
|
|
|
pub fn load() -> Self {
|
|
|
|
|
Self {
|
2023-11-01 14:19:39 -06:00
|
|
|
vim_bindings: false,
|
2023-11-01 09:44:11 -06:00
|
|
|
word_wrap: false,
|
2023-10-30 10:19:52 -06:00
|
|
|
keybinds: KeyBind::load(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|