diff options
| author | srdusr <[email protected]> | 2024-07-29 14:01:00 +0200 |
|---|---|---|
| committer | srdusr <[email protected]> | 2024-07-29 14:01:00 +0200 |
| commit | 5fe599437ab9d1d9cfa749e861ce2f3acc02aa3c (patch) | |
| tree | 2956b9b18af7b6c0741da8b68da18de66ed393e5 /crates/config/src/engine/window.rs | |
| parent | 6fad44b3ecffb22f77a78661ce179b304e9a6ccc (diff) | |
| download | srdwm-5fe599437ab9d1d9cfa749e861ce2f3acc02aa3c.tar.gz srdwm-5fe599437ab9d1d9cfa749e861ce2f3acc02aa3c.zip | |
Split crates/config/src/lib.rs (1441 lines) into engine/
Pure reorganization, no behavior change - verified by diffing the
function-name and struct/enum-name sets before/after (both identical)
plus a full cargo test pass. lib.rs is now a thin shim (mod
declarations + pub use) since a crate root can't itself become a
directory; all the actual content moved into engine/, split along the
Lua API's own srd.*/srd.window.*/srd.layout.*/srd.workspace.*/
srd.theme.* namespace groupings the file's own section comments
already used:
- mod.rs: SharedState, Engine, ConfigError, and Engine's core methods
(new/get/set/dispatch/reload/...).
- register.rs: register_srd_module, which wires every fn_* builder
from every other file into the srd Lua table - the one place that
genuinely needs to see all of them.
- general.rs/window.rs/layout.rs/workspace.rs/theme.rs: the fn_*
builder methods themselves, one file per srd.* sub-namespace.
- support.rs: free functions shared across those (do_reload,
parse_direction, flatten_table_into, validate, default_config) and
the WindowAction enum.
- tests.rs: the ~300-line test module, left unsplit for the same
shared-helper reason manager/tests.rs and udev's tests were.
~40 fn_* methods and the support.rs free functions/enum went from
private to pub(super): called across what are now sibling submodules,
which Rust's privacy model doesn't let see each other's private items.
Diffstat (limited to 'crates/config/src/engine/window.rs')
| -rw-r--r-- | crates/config/src/engine/window.rs | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/crates/config/src/engine/window.rs b/crates/config/src/engine/window.rs new file mode 100644 index 0000000..6193f81 --- /dev/null +++ b/crates/config/src/engine/window.rs @@ -0,0 +1,178 @@ +use super::*; +use super::support::{parse_direction, WindowAction}; + +impl Engine { + // ---- srd.window.* ------------------------------------------------------ + + pub(super) fn fn_window_focused(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |lua, ()| { + let wm = state.borrow().wm.clone(); + let wm = wm.borrow(); + let Some(w) = wm.focused_window() else { return Ok(Value::Nil) }; + let t = lua.create_table()?; + t.set("id", w.id)?; + t.set("title", w.title.clone())?; + t.set("x", w.geometry.x)?; + t.set("y", w.geometry.y)?; + t.set("width", w.geometry.width)?; + t.set("height", w.geometry.height)?; + t.set("floating", w.floating)?; + t.set("maximized", w.maximized)?; + t.set("minimized", w.minimized)?; + t.set("scratchpad", w.scratchpad)?; + Ok(Value::Table(t)) + })?) + } + + pub(super) fn fn_window_action(&self, action: WindowAction) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, ()| { + let wm = state.borrow().wm.clone(); + let mut wm = wm.borrow_mut(); + if let Some(id) = wm.focused_id() { + match action { + WindowAction::Close => wm.close_window(id), + WindowAction::Minimize => wm.minimize_window(id), + WindowAction::Maximize => wm.toggle_maximize(id), + WindowAction::Fullscreen => wm.toggle_fullscreen(id), + WindowAction::ToggleFloating => wm.toggle_floating(id), + WindowAction::TogglePin => wm.toggle_always_on_top(id), + WindowAction::ScratchpadAdd => wm.scratchpad_add(id), + } + } + Ok(()) + })?) + } + + pub(super) fn fn_scratchpad_show(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, ()| { + let wm = state.borrow().wm.clone(); + wm.borrow_mut().scratchpad_show(); + Ok(()) + })?) + } + + /// `srd.window.move("left")` - swap the focused window with its + /// neighbour in that direction (Hyprland's `movewindow l/r/u/d`). + pub(super) fn fn_window_move_direction(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, direction: String| { + let dir = parse_direction(&direction, "srd.window.move")?; + let wm = state.borrow().wm.clone(); + wm.borrow_mut().move_window_direction(dir); + Ok(()) + })?) + } + + /// `srd.window.next()` / `srd.window.prev()` - cycle focus through the + /// windows on the current workspace (Hyprland's `cyclenext`). + pub(super) fn fn_window_cycle(&self, forward: bool) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, ()| { + let wm = state.borrow().wm.clone(); + let mut wm = wm.borrow_mut(); + if forward { + wm.focus_next(); + } else { + wm.focus_previous(); + } + // Bring it to the top, matching the `bringactivetotop` the + // Hyprland binding pairs with `cyclenext`. + if let Some(id) = wm.focused_id() { + wm.raise_window(id); + } + Ok(()) + })?) + } + + pub(super) fn fn_window_focus_direction(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, direction: String| { + let dir = parse_direction(&direction, "srd.window.focus")?; + let wm = state.borrow().wm.clone(); + wm.borrow_mut().focus_direction(dir); + Ok(()) + })?) + } + + pub(super) fn fn_window_set_decorations(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, enabled: bool| { + let wm = state.borrow().wm.clone(); + let mut wm = wm.borrow_mut(); + if let Some(id) = wm.focused_id() { + if let Some(w) = wm.window_mut(id) { + w.decorated = enabled; + } + } + Ok(()) + })?) + } + + pub(super) fn fn_window_set_border_color(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, (r, g, b): (u8, u8, u8)| { + let wm = state.borrow().wm.clone(); + let mut wm = wm.borrow_mut(); + if let Some(id) = wm.focused_id() { + if let Some(w) = wm.window_mut(id) { + w.border_color = (r, g, b); + } + } + Ok(()) + })?) + } + + pub(super) fn fn_window_set_border_width(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, width: u32| { + let wm = state.borrow().wm.clone(); + let mut wm = wm.borrow_mut(); + if let Some(id) = wm.focused_id() { + if let Some(w) = wm.window_mut(id) { + w.border_width = width; + } + } + Ok(()) + })?) + } + + pub(super) fn fn_window_set_opacity(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, opacity: f32| { + let wm = state.borrow().wm.clone(); + let mut wm = wm.borrow_mut(); + if let Some(id) = wm.focused_id() { + if let Some(w) = wm.window_mut(id) { + w.opacity = opacity.clamp(0.0, 1.0); + } + } + Ok(()) + })?) + } + + pub(super) fn fn_window_set_floating(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, floating: bool| { + let wm = state.borrow().wm.clone(); + let mut wm = wm.borrow_mut(); + if let Some(id) = wm.focused_id() { + if let Some(w) = wm.window_mut(id) { + w.floating = floating; + } + } + Ok(()) + })?) + } + + pub(super) fn fn_window_is_floating(&self) -> Result<mlua::Function<'_>> { + let state = self.state.clone(); + Ok(self.lua.create_function(move |_, ()| { + let wm = state.borrow().wm.clone(); + let wm = wm.borrow(); + Ok(wm.focused_id().map(|id| wm.is_floating(id)).unwrap_or(false)) + })?) + } +} |