Skip to content

Commit

Permalink
Key bindings, and changing them.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbwilding committed Jan 30, 2024
1 parent 3e8e404 commit fc2189a
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 35 deletions.
19 changes: 19 additions & 0 deletions .run/Clippy.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Clippy" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="command" value="clippy" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs />
<option name="emulateTerminal" value="true" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="true" />
<option name="allFeatures" value="false" />
<option name="withSudo" value="false" />
<option name="buildTarget" value="REMOTE" />
<option name="backtrace" value="SHORT" />
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
</component>
17 changes: 17 additions & 0 deletions .run/Reset Config.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Reset Config" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="Remove-Item -Path &quot;$env:APPDATA\Tile-RS&quot; -Recurse -Force" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>
4 changes: 2 additions & 2 deletions src/action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Deserialize, Serialize)]
pub enum Action {
Stop,
Start,
}
}
42 changes: 36 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
#[serde(default)]
pub struct App {
pub settings: Settings,
pub key_bindings: BTreeMap<Keys, Action>,
pub key_bindings: Vec<(Action, Keys)>,

#[serde(skip)]
pub windows_manager: WindowsManager,
Expand All @@ -27,24 +27,24 @@ pub struct App {

impl Default for App {
fn default() -> Self {
let key_bindings = BTreeMap::from([
let key_bindings = Vec::from([
(
Action::Start,
Keys {
shift: true,
ctrl: true,
key: VirtualKey::A,
..Default::default()
},
Action::Start,
),
(
Action::Stop,
Keys {
shift: true,
ctrl: true,
key: VirtualKey::S,
..Default::default()
},
Action::Stop,
),
]);

Expand Down Expand Up @@ -119,13 +119,16 @@ impl eframe::App for App {
ui.heading("Layout");
ui.with_layout(egui::Layout::right_to_left(Align::Center), |ui| {
let response = egui::ComboBox::new("layout_engine_type", "")
.selected_text(self.settings.layout_engine_type.to_string())
.selected_text(format!(
"{:?}",
self.settings.layout_engine_type
))
.show_ui(ui, |ui| {
for option in LayoutEngineType::variants() {
ui.selectable_value(
&mut self.settings.layout_engine_type,
option,
option.to_string(),
format!("{:?}", option),
);
}
});
Expand All @@ -137,6 +140,33 @@ impl eframe::App for App {
});
});
});

egui::containers::collapsing_header::CollapsingHeader::new("Bindings")
.default_open(true)
.show(ui, |ui| {
self.key_bindings.iter_mut().for_each(|(action, keys)| {
ui.heading(format!("{:?}", action));

egui::ComboBox::new(format!("bindings_{:?}", action), "Key")
.selected_text(format!("{:?}", keys.key))
.show_ui(ui, |ui| {
for option in VirtualKey::variants() {
ui.selectable_value(
&mut keys.key,
option,
format!("{:?}", option),
);
}
});

ui.horizontal(|ui| {
ui.checkbox(&mut keys.shift, "Shift");
ui.checkbox(&mut keys.ctrl, "Ctrl");
ui.checkbox(&mut keys.alt, "Alt");
ui.checkbox(&mut keys.win, "Win");
});
});
});
});

egui::CentralPanel::default().show(ctx, |ui| {
Expand Down
156 changes: 154 additions & 2 deletions src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::layout_engines::LayoutEngineType;
use log::trace;
use serde::{Deserialize, Serialize};
use windows::Win32::Foundation::{LPARAM, WPARAM};
Expand All @@ -7,7 +8,7 @@ use windows::Win32::UI::Input::KeyboardAndMouse::{
use windows::Win32::UI::WindowsAndMessaging::KBDLLHOOKSTRUCT;
use windows::Win32::UI::WindowsAndMessaging::{WM_KEYDOWN, WM_SYSKEYDOWN};

#[derive(Debug, Default, PartialEq, PartialOrd, Ord, Eq, Serialize, Deserialize)]
#[derive(Debug, Default, PartialEq, PartialOrd, Eq, Ord, Serialize, Deserialize)]
pub struct Keys {
pub shift: bool,
pub ctrl: bool,
Expand Down Expand Up @@ -53,7 +54,7 @@ impl Keys {
}
}

#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[derive(Debug, Default, PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Serialize, Deserialize)]
pub enum VirtualKey {
#[default]
Unknown = 0x00,
Expand Down Expand Up @@ -352,3 +353,154 @@ impl VirtualKey {
}
}
}

impl VirtualKey {
pub fn variants() -> [VirtualKey; 144] {
[
VirtualKey::Unknown,
VirtualKey::Backspace,
VirtualKey::Tab,
VirtualKey::Clear,
VirtualKey::Enter,
VirtualKey::Shift,
VirtualKey::Ctrl,
VirtualKey::Alt,
VirtualKey::Pause,
VirtualKey::Capslock,
VirtualKey::Esc,
VirtualKey::Space,
VirtualKey::PageUp,
VirtualKey::PageDown,
VirtualKey::End,
VirtualKey::Home,
VirtualKey::Left,
VirtualKey::Up,
VirtualKey::Right,
VirtualKey::Down,
VirtualKey::Select,
VirtualKey::Print,
VirtualKey::Execute,
VirtualKey::PrintScreen,
VirtualKey::Insert,
VirtualKey::Delete,
VirtualKey::Help,
VirtualKey::Key0,
VirtualKey::Key1,
VirtualKey::Key2,
VirtualKey::Key3,
VirtualKey::Key4,
VirtualKey::Key5,
VirtualKey::Key6,
VirtualKey::Key7,
VirtualKey::Key8,
VirtualKey::Key9,
VirtualKey::A,
VirtualKey::B,
VirtualKey::C,
VirtualKey::D,
VirtualKey::E,
VirtualKey::F,
VirtualKey::G,
VirtualKey::H,
VirtualKey::I,
VirtualKey::J,
VirtualKey::K,
VirtualKey::L,
VirtualKey::M,
VirtualKey::N,
VirtualKey::O,
VirtualKey::P,
VirtualKey::Q,
VirtualKey::R,
VirtualKey::S,
VirtualKey::T,
VirtualKey::U,
VirtualKey::V,
VirtualKey::W,
VirtualKey::X,
VirtualKey::Y,
VirtualKey::Z,
VirtualKey::LeftWin,
VirtualKey::RightWin,
VirtualKey::Apps,
VirtualKey::Sleep,
VirtualKey::Num0,
VirtualKey::Num1,
VirtualKey::Num2,
VirtualKey::Num3,
VirtualKey::Num4,
VirtualKey::Num5,
VirtualKey::Num6,
VirtualKey::Num7,
VirtualKey::Num8,
VirtualKey::Num9,
VirtualKey::Multiply,
VirtualKey::Add,
VirtualKey::Separator,
VirtualKey::Subtract,
VirtualKey::Decimal,
VirtualKey::Divide,
VirtualKey::F1,
VirtualKey::F2,
VirtualKey::F3,
VirtualKey::F4,
VirtualKey::F5,
VirtualKey::F6,
VirtualKey::F7,
VirtualKey::F8,
VirtualKey::F9,
VirtualKey::F10,
VirtualKey::F11,
VirtualKey::F12,
VirtualKey::F13,
VirtualKey::F14,
VirtualKey::F15,
VirtualKey::F16,
VirtualKey::F17,
VirtualKey::F18,
VirtualKey::F19,
VirtualKey::F20,
VirtualKey::F21,
VirtualKey::F22,
VirtualKey::F23,
VirtualKey::F24,
VirtualKey::NumLock,
VirtualKey::ScrollLock,
VirtualKey::LeftShift,
VirtualKey::RightShift,
VirtualKey::LeftControl,
VirtualKey::RightControl,
VirtualKey::LeftAlt,
VirtualKey::RightAlt,
VirtualKey::BrowserBack,
VirtualKey::BrowserForward,
VirtualKey::BrowserRefresh,
VirtualKey::BrowserStop,
VirtualKey::BrowserSearch,
VirtualKey::BrowserFavorites,
VirtualKey::BrowserHome,
VirtualKey::VolumeMute,
VirtualKey::VolumeDown,
VirtualKey::VolumeUp,
VirtualKey::MediaNextTrack,
VirtualKey::MediaPreviousTrack,
VirtualKey::MediaStop,
VirtualKey::MediaPlayPause,
VirtualKey::LaunchMail,
VirtualKey::LaunchMediaSelect,
VirtualKey::LaunchApp1,
VirtualKey::LaunchApp2,
VirtualKey::Semicolon,
VirtualKey::Equals,
VirtualKey::Comma,
VirtualKey::Minus,
VirtualKey::Period,
VirtualKey::Slash,
VirtualKey::BackTick,
VirtualKey::LeftBracket,
VirtualKey::Backslash,
VirtualKey::RightBracket,
VirtualKey::Quote,
]
}
}
13 changes: 0 additions & 13 deletions src/layout_engines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ impl LayoutEngineType {
}
}

impl Display for LayoutEngineType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LayoutEngineType::Dwindle => write!(f, "Dwindle"),
LayoutEngineType::Focus => write!(f, "Focus"),
LayoutEngineType::Full => write!(f, "Full"),
LayoutEngineType::Grid => write!(f, "Grid"),
// LayoutEngineType::Panel => write!(f, "Panel"),
// LayoutEngineType::Tall => write!(f, "Tall"),
}
}
}

pub trait LayoutEngine {
// the name of the layout engine
fn name(&self) -> &str;
Expand Down
23 changes: 11 additions & 12 deletions src/windows_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use lazy_static::lazy_static;
use log::{debug, error, info, trace};
use std::collections::{BTreeMap, HashMap};
use std::sync::Mutex;
use windows::core::PCWSTR;
use windows::Win32::Foundation::{BOOL, HMODULE, HWND, LPARAM, LRESULT, TRUE, WPARAM};
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::UI::Accessibility::{SetWinEventHook, UnhookWinEvent, HWINEVENTHOOK};
Expand Down Expand Up @@ -53,7 +52,7 @@ impl WindowsManager {
info!("Initializing hooks");

let module_handle = unsafe {
GetModuleHandleW(PCWSTR::null()).unwrap_or_else(|e| {
GetModuleHandleW(None).unwrap_or_else(|e| {
error!("Failed GetModuleHandleW: {:?}", e);
std::process::exit(69);
})
Expand Down Expand Up @@ -179,19 +178,19 @@ impl WindowsManager {
}
}

pub fn handle_keys(&mut self, mappings: &BTreeMap<Keys, Action>) {
pub fn handle_keys(&mut self, mappings: &Vec<(Action, Keys)>) {
if let Ok(keys) = KEYS.1.try_recv() {
if let Some(action) = mappings.get(&keys) {
match action {
Action::Stop => {
debug!("Stopping");
}
Action::Start => {
debug!("Starting");
for (action, key) in mappings {
if *key == keys {
match action {
Action::Start => {
info!("action: Start");
}
Action::Stop => {
info!("action: Stop");
}
}
}
} else {
trace!("{:?}", keys);
}
}
}
Expand Down

0 comments on commit fc2189a

Please sign in to comment.