Skip to content

Commit

Permalink
Started adding shortcuts menu
Browse files Browse the repository at this point in the history
  • Loading branch information
samclane committed Sep 18, 2024
1 parent 9d7dc49 commit 2caf0bc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use serde::{Deserialize, Serialize};
// UI and window size constants
pub const MAIN_WINDOW_SIZE: [f32; 2] = [320.0, 800.0];
pub const ABOUT_WINDOW_SIZE: [f32; 2] = [320.0, 480.0];
pub const SETTINGS_WINDOW_SIZE: [f32; 2] = [320.0, 480.0];
pub const MIN_WINDOW_SIZE: [f32; 2] = [300.0, 220.0];

// Color and refresh constants
Expand Down Expand Up @@ -70,6 +71,7 @@ pub struct MantleApp {
#[serde(skip)]
pub listener_handle: Option<JoinHandle<()>>,
pub show_about: bool,
pub show_settings: bool,
pub show_eyedropper: HashMap<u64, bool>,
pub show_subregion: HashMap<u64, bool>,
pub subregion_points: HashMap<u64, Arc<Mutex<ScreenSubregion>>>,
Expand All @@ -91,6 +93,7 @@ impl Default for MantleApp {
input_listener,
listener_handle,
show_about: false,
show_settings: false,
show_eyedropper: HashMap::new(),
show_subregion: HashMap::new(),
subregion_points: HashMap::new(),
Expand Down Expand Up @@ -283,7 +286,7 @@ impl MantleApp {
}
}

fn file_menu_button(&self, ui: &mut Ui) {
fn file_menu_button(&mut self, ui: &mut Ui) {
let close_shortcut = egui::KeyboardShortcut::new(Modifiers::CTRL, egui::Key::Q);
let refresh_shortcut = egui::KeyboardShortcut::new(Modifiers::NONE, egui::Key::F5);
if ui.input_mut(|i| i.consume_shortcut(&close_shortcut)) {
Expand All @@ -305,6 +308,10 @@ impl MantleApp {
self.mgr.refresh();
ui.close_menu();
}
if ui.add(egui::Button::new("Settings")).clicked() {
self.show_settings = true;
ui.close_menu();
}
if ui
.add(
egui::Button::new("Quit")
Expand Down Expand Up @@ -381,6 +388,35 @@ impl MantleApp {
});
}
}

fn show_settings_window(&mut self, ctx: &egui::Context) {
if self.show_settings {
egui::Window::new("Settings")
.default_width(SETTINGS_WINDOW_SIZE[0])
.default_height(SETTINGS_WINDOW_SIZE[1])
.open(&mut self.show_settings)
.resizable([true, false])
.show(ctx, |ui| {
ui.heading("Settings");
ui.add_space(8.0);
// allow user to register keyboard shortcuts
ui.horizontal(|ui| {
ui.label("Keyboard Shortcuts");
ui.separator();
ui.label("Action");
ui.separator();
ui.label("Shortcut");
});
for (action, shortcut) in self.input_listener.get_active_items() {
ui.horizontal(|ui| {
ui.label(format!("{:?}", action));
ui.separator();
ui.label(format!("{:?}", shortcut));
});
}
});
}
}
}

impl eframe::App for MantleApp {
Expand All @@ -397,5 +433,6 @@ impl eframe::App for MantleApp {
self.mgr.refresh();
self.update_ui(_ctx);
self.show_about_window(_ctx);
self.show_settings_window(_ctx);
}
}
21 changes: 21 additions & 0 deletions src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ impl std::hash::Hash for KeyboardShortcut {
}
}

impl std::fmt::Debug for KeyboardShortcut {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "KeyboardShortcut({:?})", self.keys)
}
}

impl std::fmt::Display for KeyboardShortcut {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let keys: Vec<String> = self.keys.iter().map(|k| format!("{:?}", k)).collect();
write!(f, "{}", keys.join(" + "))
}
}

impl KeyboardShortcut {
fn is_matched(&self, keys_pressed: &HashSet<rdev::Key>) -> bool {
self.keys.is_subset(keys_pressed)
Expand Down Expand Up @@ -332,6 +345,14 @@ impl InputListener {
}
})
}

pub fn get_active_items(&self) -> impl Iterator<Item = (rdev::Key, KeyboardShortcut)> {
let key = rdev::Key::KeyA;
let shortcut = KeyboardShortcut {
keys: vec![key].into_iter().collect(),
};
vec![(key, shortcut)].into_iter()
}
}

impl Default for InputListener {
Expand Down

0 comments on commit 2caf0bc

Please sign in to comment.