#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Chord {
pub key: egui::Key,
pub ctrl: bool,
pub shift: bool,
}
impl Chord {
pub const fn plain(key: egui::Key) -> Self {
Self {
key,
ctrl: false,
shift: false,
}
}
pub const fn ctrl(key: egui::Key) -> Self {
Self {
key,
ctrl: true,
shift: false,
}
}
pub const fn ctrl_shift(key: egui::Key) -> Self {
Self {
key,
ctrl: true,
shift: true,
}
}
pub fn spell(self) -> String {
let key = match self.key {
egui::Key::Comma => ",",
egui::Key::Equals => "=",
egui::Key::Minus => "-",
egui::Key::Escape => "Esc",
egui::Key::Backslash => "\\",
key => key.name(),
};
let ctrl = if self.ctrl { "Ctrl+" } else { "" };
let shift = if self.shift { "Shift+" } else { "" };
format!("{ctrl}{shift}{key}")
}
pub fn pressed(self, ctx: &egui::Context) -> bool {
ctx.input(|i| {
i.events.iter().any(|event| {
matches!(
event,
egui::Event::Key {
key,
pressed: true,
modifiers,
..
} if *key == self.key
&& modifiers.command == self.ctrl
&& modifiers.shift == self.shift
)
})
})
}
}