Rebase on new libcosmic and iced

This commit is contained in:
Jeremy Soller 2024-02-09 07:09:51 -07:00
parent 88089fecb4
commit cb4bd84ea5
6 changed files with 611 additions and 654 deletions

View file

@ -1,4 +1,7 @@
use cosmic::iced::keyboard::{KeyCode, Modifiers};
use cosmic::{
iced::keyboard::{Key, Modifiers},
iced_core::keyboard::key::Named,
};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt};
@ -15,12 +18,12 @@ pub enum Modifier {
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct KeyBind {
pub modifiers: Vec<Modifier>,
pub key_code: KeyCode,
pub key: Key,
}
impl KeyBind {
pub fn matches(&self, modifiers: Modifiers, key_code: KeyCode) -> bool {
self.key_code == key_code
pub fn matches(&self, modifiers: Modifiers, key: &Key) -> bool {
key == &self.key
&& modifiers.logo() == self.modifiers.contains(&Modifier::Super)
&& modifiers.control() == self.modifiers.contains(&Modifier::Ctrl)
&& modifiers.alt() == self.modifiers.contains(&Modifier::Alt)
@ -33,7 +36,11 @@ impl fmt::Display for KeyBind {
for modifier in self.modifiers.iter() {
write!(f, "{:?} + ", modifier)?;
}
write!(f, "{:?}", self.key_code)
match &self.key {
Key::Character(c) => write!(f, "{}", c.to_uppercase()),
Key::Named(named) => write!(f, "{:?}", named),
other => write!(f, "{:?}", other),
}
}
}
@ -42,30 +49,30 @@ pub fn key_binds() -> HashMap<KeyBind, Action> {
let mut key_binds = HashMap::new();
macro_rules! bind {
([$($modifier:ident),+ $(,)?], $key_code:ident, $action:ident) => {{
([$($modifier:ident),+ $(,)?], $key:expr, $action:ident) => {{
key_binds.insert(
KeyBind {
modifiers: vec![$(Modifier::$modifier),+],
key_code: KeyCode::$key_code,
key: $key,
},
Action::$action,
);
}};
}
bind!([Ctrl], C, Copy);
bind!([Ctrl], X, Cut);
bind!([Alt], Right, HistoryNext);
bind!([Alt], Left, HistoryPrevious);
bind!([Alt], Up, LocationUp);
bind!([Ctrl], V, Paste);
bind!([Ctrl], A, SelectAll);
bind!([Ctrl], W, TabClose);
bind!([Ctrl], T, TabNew);
bind!([Ctrl], Tab, TabNext);
bind!([Ctrl, Shift], Tab, TabPrev);
bind!([Ctrl], Q, WindowClose);
bind!([Ctrl], N, WindowNew);
bind!([Ctrl], Key::Character("c".into()), Copy);
bind!([Ctrl], Key::Character("x".into()), Cut);
bind!([Alt], Key::Named(Named::ArrowRight), HistoryNext);
bind!([Alt], Key::Named(Named::ArrowLeft), HistoryPrevious);
bind!([Alt], Key::Named(Named::ArrowUp), LocationUp);
bind!([Ctrl], Key::Character("v".into()), Paste);
bind!([Ctrl], Key::Character("a".into()), SelectAll);
bind!([Ctrl], Key::Character("w".into()), TabClose);
bind!([Ctrl], Key::Character("t".into()), TabNew);
bind!([Ctrl], Key::Named(Named::Tab), TabNext);
bind!([Ctrl, Shift], Key::Named(Named::Tab), TabPrev);
bind!([Ctrl], Key::Character("q".into()), WindowClose);
bind!([Ctrl], Key::Character("n".into()), WindowNew);
key_binds
}