From ba658884577a89a1f896a753b9e875669591e10d Mon Sep 17 00:00:00 2001 From: Candifloss Date: Mon, 17 Nov 2025 05:02:00 +0530 Subject: [PATCH] Modularity and code cleanup - Split keybindings and mousebindings into separate modules - Remove unwanted MacOS things --- src/keybindings.rs | 47 +++++++++++++++++++ src/main.rs | 109 ++++--------------------------------------- src/mousebindings.rs | 22 +++++++++ 3 files changed, 77 insertions(+), 101 deletions(-) create mode 100644 src/keybindings.rs create mode 100644 src/mousebindings.rs diff --git a/src/keybindings.rs b/src/keybindings.rs new file mode 100644 index 0000000..293c4ba --- /dev/null +++ b/src/keybindings.rs @@ -0,0 +1,47 @@ +use penrose::x11rb::RustConn; +use penrose::{ + builtin::{ + actions::{exit, modify_with, send_layout_message, spawn}, + layout::messages::{ExpandMain, IncMain, ShrinkMain}, + }, + core::bindings::KeyEventHandler, + map, +}; +use std::collections::HashMap; + +// Keybindings +pub fn raw_key_bindings() -> HashMap>> { + let mut raw = map! { + map_keys: |k: &str| k.to_string(); + + "M-j" => modify_with(|cs| cs.focus_down()), + "M-k" => modify_with(|cs| cs.focus_up()), + "M-S-j" => modify_with(|cs| cs.swap_down()), + "M-S-k" => modify_with(|cs| cs.swap_up()), + "M-S-q" => modify_with(|cs| cs.kill_focused()), + "M-Tab" => modify_with(|cs| cs.toggle_tag()), + "M-bracketright" => modify_with(|cs| cs.next_screen()), + "M-bracketleft" => modify_with(|cs| cs.previous_screen()), + "M-grave" => modify_with(|cs| cs.next_layout()), + "M-S-grave" => modify_with(|cs| cs.previous_layout()), + "M-S-Up" => send_layout_message(|| IncMain(1)), + "M-S-Down" => send_layout_message(|| IncMain(-1)), + "M-S-Right" => send_layout_message(|| ExpandMain), + "M-S-Left" => send_layout_message(|| ShrinkMain), + "M-semicolon" => spawn("dmenu_run"), + "M-Return" => spawn("st"), + "M-A-Escape" => exit(), + }; + + for tag in &["1", "2", "3", "4", "5", "6", "7", "8", "9"] { + raw.extend([ + (format!("M-{tag}"), modify_with(move |cs| cs.focus_tag(tag))), + ( + format!("M-S-{tag}"), + modify_with(move |cs| cs.move_focused_to_tag(tag)), + ), + ]); + } + + raw +} diff --git a/src/main.rs b/src/main.rs index d9729d3..8139f82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,89 +1,14 @@ -//! penrose :: minimal configuration -//! -//! This file will give you a functional if incredibly minimal window manager that -//! has multiple workspaces and simple client / workspace movement. -#[cfg(not(target_os = "macos"))] use penrose::x11rb::RustConn; use penrose::{ Result, - builtin::{ - actions::{ - exit, - floating::{MouseDragHandler, MouseResizeHandler, sink_focused}, - modify_with, send_layout_message, spawn, - }, - layout::messages::{ExpandMain, IncMain, ShrinkMain}, - }, - core::{ - Config, WindowManager, - bindings::{ - KeyEventHandler, MouseEventHandler, MouseState, click_handler, - parse_keybindings_with_xmodmap, - }, - }, - map, + core::bindings::parse_keybindings_with_xmodmap, + core::{Config, WindowManager}, }; - -use std::collections::HashMap; use tracing_subscriber::{self, prelude::*}; -#[cfg(not(target_os = "macos"))] -fn raw_key_bindings() -> HashMap>> { - let mut raw_bindings = map! { - map_keys: |k: &str| k.to_string(); +mod keybindings; +mod mousebindings; - "M-j" => modify_with(|cs| cs.focus_down()), - "M-k" => modify_with(|cs| cs.focus_up()), - "M-S-j" => modify_with(|cs| cs.swap_down()), - "M-S-k" => modify_with(|cs| cs.swap_up()), - "M-S-q" => modify_with(|cs| cs.kill_focused()), - "M-Tab" => modify_with(|cs| cs.toggle_tag()), - "M-bracketright" => modify_with(|cs| cs.next_screen()), - "M-bracketleft" => modify_with(|cs| cs.previous_screen()), - "M-grave" => modify_with(|cs| cs.next_layout()), - "M-S-grave" => modify_with(|cs| cs.previous_layout()), - "M-S-Up" => send_layout_message(|| IncMain(1)), - "M-S-Down" => send_layout_message(|| IncMain(-1)), - "M-S-Right" => send_layout_message(|| ExpandMain), - "M-S-Left" => send_layout_message(|| ShrinkMain), - "M-semicolon" => spawn("dmenu_run"), - "M-Return" => spawn("st"), - "M-A-Escape" => exit(), - }; - - for tag in &["1", "2", "3", "4", "5", "6", "7", "8", "9"] { - raw_bindings.extend([ - ( - format!("M-{tag}"), - modify_with(move |client_set| client_set.focus_tag(tag)), - ), - ( - format!("M-S-{tag}"), - modify_with(move |client_set| client_set.move_focused_to_tag(tag)), - ), - ]); - } - - raw_bindings -} - -#[cfg(not(target_os = "macos"))] -fn mouse_bindings() -> HashMap>> { - use penrose::core::bindings::{ - ModifierKey::{Meta, Shift}, - MouseButton::{Left, Middle, Right}, - }; - - map! { - map_keys: |(button, modifiers)| MouseState { button, modifiers }; - - (Left, vec![Shift, Meta]) => MouseDragHandler::boxed_default(), - (Right, vec![Shift, Meta]) => MouseResizeHandler::boxed_default(), - (Middle, vec![Shift, Meta]) => click_handler(sink_focused()), - } -} - -#[cfg(not(target_os = "macos"))] fn main() -> Result<()> { tracing_subscriber::fmt() .with_env_filter("info") @@ -91,28 +16,10 @@ fn main() -> Result<()> { .init(); let conn = RustConn::new()?; - let key_bindings = parse_keybindings_with_xmodmap(raw_key_bindings())?; - let wm = WindowManager::new(Config::default(), key_bindings, mouse_bindings(), conn)?; + let key_bindings = parse_keybindings_with_xmodmap(keybindings::raw_key_bindings())?; + let mouse_bindings = mousebindings::mouse_bindings(); + + let wm = WindowManager::new(Config::default(), key_bindings, mouse_bindings, conn)?; wm.run() } - -#[cfg(target_os = "macos")] -fn main() -> Result<()> { - panic!("not supported on OSX"); -} - -#[cfg(not(target_os = "macos"))] -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn bindings_parse_correctly_with_xmodmap() { - let res = parse_keybindings_with_xmodmap(raw_key_bindings()); - - if let Err(e) = res { - panic!("{e}"); - } - } -} diff --git a/src/mousebindings.rs b/src/mousebindings.rs new file mode 100644 index 0000000..1f0a463 --- /dev/null +++ b/src/mousebindings.rs @@ -0,0 +1,22 @@ +use penrose::x11rb::RustConn; +use penrose::{ + builtin::actions::floating::{MouseDragHandler, MouseResizeHandler, sink_focused}, + core::bindings::{ + ModifierKey::{Meta, Shift}, + MouseButton::{Left, Middle, Right}, + MouseEventHandler, MouseState, click_handler, + }, + map, +}; +use std::collections::HashMap; + +// Mouse bindings +pub fn mouse_bindings() -> HashMap>> { + map! { + map_keys: |(button, modifiers)| MouseState { button, modifiers }; + + (Left, vec![Shift, Meta]) => MouseDragHandler::boxed_default(), + (Right, vec![Shift, Meta]) => MouseResizeHandler::boxed_default(), + (Middle, vec![Shift, Meta]) => click_handler(sink_focused()), + } +}