Add key shortcut support, use key bindings to determine menu text
This commit is contained in:
parent
b2998f4317
commit
91e3d3e05a
3 changed files with 116 additions and 19 deletions
69
src/config.rs
Normal file
69
src/config.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
use cosmic::iced::keyboard::{KeyCode, Modifiers};
|
||||
use std::{collections::HashMap, fmt};
|
||||
|
||||
use crate::{fl, Message};
|
||||
|
||||
#[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:ident, $key_code:ident, $message:ident) => {{
|
||||
keybinds.insert(
|
||||
KeyBind {
|
||||
modifiers: Modifiers::$modifiers,
|
||||
key_code: KeyCode::$key_code,
|
||||
},
|
||||
Message::$message,
|
||||
);
|
||||
}};
|
||||
}
|
||||
|
||||
bind!(CTRL, N, New);
|
||||
bind!(CTRL, O, OpenFileDialog);
|
||||
bind!(CTRL, S, Save);
|
||||
|
||||
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 {
|
||||
pub wrap: bool,
|
||||
pub keybinds: HashMap<KeyBind, Message>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
//TODO: load from cosmic-config
|
||||
pub fn load() -> Self {
|
||||
Self {
|
||||
wrap: false,
|
||||
keybinds: KeyBind::load(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue