Modularity and code cleanup

- Split keybindings and mousebindings into separate modules
- Remove unwanted MacOS things
This commit is contained in:
Candifloss 2025-11-17 05:02:00 +05:30
parent 8b97874fbe
commit ba65888457
3 changed files with 77 additions and 101 deletions

47
src/keybindings.rs Normal file
View File

@ -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<String, Box<dyn KeyEventHandler<RustConn>>> {
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
}

View File

@ -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::x11rb::RustConn;
use penrose::{ use penrose::{
Result, Result,
builtin::{ core::bindings::parse_keybindings_with_xmodmap,
actions::{ core::{Config, WindowManager},
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,
}; };
use std::collections::HashMap;
use tracing_subscriber::{self, prelude::*}; use tracing_subscriber::{self, prelude::*};
#[cfg(not(target_os = "macos"))] mod keybindings;
fn raw_key_bindings() -> HashMap<String, Box<dyn KeyEventHandler<RustConn>>> { mod mousebindings;
let mut raw_bindings = 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_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<MouseState, Box<dyn MouseEventHandler<RustConn>>> {
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<()> { fn main() -> Result<()> {
tracing_subscriber::fmt() tracing_subscriber::fmt()
.with_env_filter("info") .with_env_filter("info")
@ -91,28 +16,10 @@ fn main() -> Result<()> {
.init(); .init();
let conn = RustConn::new()?; let conn = RustConn::new()?;
let key_bindings = parse_keybindings_with_xmodmap(raw_key_bindings())?; let key_bindings = parse_keybindings_with_xmodmap(keybindings::raw_key_bindings())?;
let wm = WindowManager::new(Config::default(), key_bindings, mouse_bindings(), conn)?; let mouse_bindings = mousebindings::mouse_bindings();
let wm = WindowManager::new(Config::default(), key_bindings, mouse_bindings, conn)?;
wm.run() 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}");
}
}
}

22
src/mousebindings.rs Normal file
View File

@ -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<MouseState, Box<dyn MouseEventHandler<RustConn>>> {
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()),
}
}