Skip to content

Commit

Permalink
changed leader + added blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
jhideki committed Mar 25, 2024
1 parent 901cc4d commit 1e684db
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: LPARAM) -> B
//SW_HIDE and SW_SHOWMINIMIZED
if window.placement.showCmd != 0 && window.placement.showCmd != 2 {
if let Some(title) = window.get_title() {
if title != "Windows Input Experience" {
if !window_manager.blacklist.contains(&title) {
window_manager.set_window(window);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/keybinds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ const KEY_N: u32 = 0x4E;
const KEY_D: u32 = 0x44;
const KEY_P: u32 = 0x50;
const ESC: u32 = 0x1B;
const SHIFT: u32 = 0x10;
const SPACE: u32 = 0x20;
const CAPS: u32 = 0x14;

pub fn handle_hotkey(
wparam: i32,
sender: &Arc<Sender<WindowManagerMessage>>,
leader_pressed: bool,
) -> Result<bool, String> {
if !leader_pressed && wparam == LEADER {
println!("leader pressed");
match register_hotkeys() {
Ok(_) => return Ok(true),
Err(e) => return Err(format!("Error: {}", e)),
Expand Down Expand Up @@ -103,7 +106,7 @@ pub fn handle_hotkey(
}
pub fn register_leader() -> Result<(), Error> {
unsafe {
if let Err(e) = RegisterHotKey(None, LEADER, HOT_KEY_MODIFIERS(4), SPACE) {
if let Err(e) = RegisterHotKey(None, LEADER, HOT_KEY_MODIFIERS(1), CAPS) {
println!("{}", e);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fn main() -> Result<(), Error> {
window_manager.set_windows();
let window_manger_listener = thread::spawn(move || window_manager.start());

unregister_leader();
match register_leader() {
Ok(()) => println!("Leader Registered"),
Err(e) => println!("Failed to registrer leader: {}", e),
Expand Down
22 changes: 15 additions & 7 deletions src/window_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::callbacks::enum_windows_proc;
use crate::window::Window;

use std::collections::HashSet;
use std::sync::mpsc::Receiver;
use windows::Win32::Foundation::{HWND, LPARAM};
use windows::Win32::UI::WindowsAndMessaging::{
Expand All @@ -26,18 +27,23 @@ pub enum WindowManagerMessage {

pub struct WindowManager {
pub current: Window,
pub left: Option<Window>,
pub right: Option<Window>,
pub below: Option<Window>,
pub above: Option<Window>,
left: Option<Window>,
right: Option<Window>,
below: Option<Window>,
above: Option<Window>,
next: Option<Window>,
window_stack: Vec<Window>, // keep track of prevous windows
stack_bottom: Option<HWND>,
pub count: i32, // corresponds to order in window struct
receiver: Receiver<WindowManagerMessage>,
pub blacklist: HashSet<String>, //window titles that will not be managed
}
impl WindowManager {
pub fn new(receiver: Receiver<WindowManagerMessage>) -> WindowManager {
let mut names = HashSet::new();
names.insert("Windows Input Experience".to_string());
names.insert("Program Manager".to_string());
names.insert("Settings".to_string());
let current = unsafe { Window::new(GetForegroundWindow(), 0) };
WindowManager {
current,
Expand All @@ -50,6 +56,7 @@ impl WindowManager {
stack_bottom: None,
count: 0,
receiver: receiver,
blacklist: names,
}
}

Expand All @@ -75,6 +82,7 @@ impl WindowManager {
}

pub fn set_window(&mut self, window: Window) {
window.print_title();
if self.left.is_none() && window.rect.right <= self.current.rect.left {
self.left = Some(window);
} else if self.right.is_none() && window.rect.left >= self.current.rect.right {
Expand Down Expand Up @@ -108,18 +116,16 @@ impl WindowManager {
}
}

pub fn close_window(&mut self) {
fn close_window(&mut self) {
unsafe {
if let Err(e) = CloseWindow(self.current.hwnd) {
println!("Failed to close window {}", e);
}
}
if let Some(window) = &self.next {
self.current = window.to_owned();
self.clear_windows();
self.set_windows();
}
self.print_windows();
}

#[allow(dead_code)]
Expand Down Expand Up @@ -165,6 +171,7 @@ impl WindowManager {
}

pub fn set_windows(&mut self) {
println!("");
unsafe {
let _ = EnumWindows(
Some(enum_windows_proc),
Expand Down Expand Up @@ -227,6 +234,7 @@ impl WindowManager {
}
self.current = window;
};
self.set_windows();
}
}

0 comments on commit 1e684db

Please sign in to comment.