diff --git a/Cargo.lock b/Cargo.lock index 68a33d3c..baf496e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3444,6 +3444,7 @@ dependencies = [ name = "komorebi-config" version = "0.1.42" dependencies = [ + "clap", "komorebi-layouts", "komorebi-protocol", "komorebi-themes", diff --git a/crates/komorebi-config/Cargo.toml b/crates/komorebi-config/Cargo.toml index 56d6295e..36c724de 100644 --- a/crates/komorebi-config/Cargo.toml +++ b/crates/komorebi-config/Cargo.toml @@ -9,6 +9,7 @@ komorebi-layouts = { path = "../../komorebi-layouts" } komorebi-protocol = { path = "../komorebi-protocol", default-features = false } komorebi-themes = { path = "../../komorebi-themes" } +clap = { workspace = true } serde = { workspace = true } serde_json = { workspace = true, features = ["preserve_order"] } strum = { workspace = true } diff --git a/crates/komorebi-config/src/behaviour.rs b/crates/komorebi-config/src/behaviour.rs new file mode 100644 index 00000000..92a71fce --- /dev/null +++ b/crates/komorebi-config/src/behaviour.rs @@ -0,0 +1,152 @@ +//! The small enums that `komorebi.json` uses to choose how a window or a container behaves. +//! +//! Some belong to one platform only (`WindowHidingPosition` to KDE and macOS, +//! `WindowHandlingBehaviour` and `ZOrder` to Windows): they are defined here all the same, +//! and a daemon that does not read the key says so when it loads the file. + +use clap::ValueEnum; +use serde::Deserialize; +use serde::Serialize; +use strum::Display; +use strum::EnumString; + +#[derive( + Clone, Copy, Debug, Default, Serialize, Deserialize, Display, EnumString, ValueEnum, PartialEq, +)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Window container behaviour when a new window is opened +pub enum WindowContainerBehaviour { + /// Create a new container for each new window + #[default] + Create, + /// Append new windows to the focused window container + Append, +} + +#[derive( + Clone, Copy, Debug, Default, Serialize, Deserialize, Display, EnumString, ValueEnum, PartialEq, +)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Floating layer behaviour when a new window is opened +pub enum FloatingLayerBehaviour { + /// Tile new windows (unless they match a float rule or float override is active) + #[default] + Tile, + /// Float new windows + Float, +} + +impl FloatingLayerBehaviour { + pub fn should_float(&self) -> bool { + match self { + FloatingLayerBehaviour::Tile => false, + FloatingLayerBehaviour::Float => true, + } + } +} + +#[derive( + Clone, Copy, Debug, Default, Serialize, Deserialize, Display, EnumString, ValueEnum, PartialEq, +)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Window hiding position +pub enum WindowHidingPosition { + /// Hide windows in the bottom left corner of the screen + #[default] + BottomLeft, + /// Hide windows in the bottom right corner of the screen + BottomRight, +} + +#[derive( + Clone, Copy, Debug, Default, Serialize, Deserialize, Display, EnumString, ValueEnum, PartialEq, +)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Placement behaviour for floating windows +pub enum Placement { + /// Does not change the size or position of the window + #[default] + None, + /// Center the window without changing the size + Center, + /// Center the window and resize it according to the `AspectRatio` + CenterAndResize, +} + +impl Placement { + pub fn should_center(&self) -> bool { + match self { + Placement::None => false, + Placement::Center | Placement::CenterAndResize => true, + } + } + + pub fn should_resize(&self) -> bool { + match self { + Placement::None | Placement::Center => false, + Placement::CenterAndResize => true, + } + } +} + +#[derive( + Clone, Copy, Debug, Default, Serialize, Deserialize, Display, EnumString, ValueEnum, PartialEq, +)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Behaviour when an action would cross a monitor boundary +pub enum CrossBoundaryBehaviour { + /// Attempt to perform actions across a workspace boundary + Workspace, + /// Attempt to perform actions across a monitor boundary + #[default] + Monitor, +} + +#[derive( + Clone, Copy, Debug, Default, Serialize, Deserialize, Display, EnumString, ValueEnum, PartialEq, +)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Window handling behaviour +pub enum WindowHandlingBehaviour { + #[default] + /// Synchronous + Sync, + /// Asynchronous + Async, +} + +#[derive(Debug, Copy, Clone, Display, Serialize, Deserialize, PartialEq)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Z Order (https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos) +pub enum ZOrder { + /// HWND_TOP + /// + /// Places the window at the top of the Z order. + Top, + /// HWND_NOTOPMOST + /// + /// Places the window above all non-topmost windows (that is, behind all topmost windows). + /// This flag has no effect if the window is already a non-topmost window. + NoTopMost, + /// HWND_BOTTOM + /// + /// Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, + /// the window loses its topmost status and is placed at the bottom of all other windows. + Bottom, + /// HWND_TOPMOST + /// + /// Places the window above all non-topmost windows. + /// The window maintains its topmost position even when it is deactivated. + TopMost, +} + +impl From for isize { + fn from(val: ZOrder) -> Self { + match val { + ZOrder::Top => 0, + ZOrder::NoTopMost => -2, + ZOrder::Bottom => 1, + ZOrder::TopMost => -1, + } + } +} diff --git a/crates/komorebi-config/src/lib.rs b/crates/komorebi-config/src/lib.rs index cd41ddfe..c58fc67c 100644 --- a/crates/komorebi-config/src/lib.rs +++ b/crates/komorebi-config/src/lib.rs @@ -10,4 +10,5 @@ #[cfg(feature = "schemars")] pub mod schema; +pub mod behaviour; pub mod matching; diff --git a/crates/komorebi-config/tests/schemas.rs b/crates/komorebi-config/tests/schemas.rs index 58a2311d..633cdc8c 100644 --- a/crates/komorebi-config/tests/schemas.rs +++ b/crates/komorebi-config/tests/schemas.rs @@ -168,3 +168,23 @@ fn the_matching_types_are_the_definitions_of_every_tree() { // The workspace rules are an input of the daemons only: no schema carries them. let _ = generated::(); } + +#[test] +fn the_behaviour_enums_are_the_definitions_of_the_trees_that_have_them() { + use komorebi_config::behaviour::CrossBoundaryBehaviour; + use komorebi_config::behaviour::FloatingLayerBehaviour; + use komorebi_config::behaviour::Placement; + use komorebi_config::behaviour::WindowContainerBehaviour; + use komorebi_config::behaviour::WindowHandlingBehaviour; + use komorebi_config::behaviour::WindowHidingPosition; + use komorebi_config::behaviour::ZOrder; + + let [windows, kde, macos] = &TREES; + assert_same_definitions::(&all(), &["WindowContainerBehaviour"]); + assert_same_definitions::(&all(), &["FloatingLayerBehaviour"]); + assert_same_definitions::(&all(), &["Placement"]); + assert_same_definitions::(&all(), &["CrossBoundaryBehaviour"]); + assert_same_definitions::(&[kde, macos], &["WindowHidingPosition"]); + assert_same_definitions::(&[windows], &["WindowHandlingBehaviour"]); + assert_same_definitions::(&[windows], &["ZOrder"]); +} diff --git a/komorebi-for-kde/Cargo.lock b/komorebi-for-kde/Cargo.lock index 4e8a9d1a..d690a983 100644 --- a/komorebi-for-kde/Cargo.lock +++ b/komorebi-for-kde/Cargo.lock @@ -2550,6 +2550,7 @@ dependencies = [ name = "komorebi-config" version = "0.1.42" dependencies = [ + "clap", "komorebi-layouts", "komorebi-protocol", "komorebi-themes", diff --git a/komorebi-for-mac/Cargo.lock b/komorebi-for-mac/Cargo.lock index edeee297..c21f4eea 100644 --- a/komorebi-for-mac/Cargo.lock +++ b/komorebi-for-mac/Cargo.lock @@ -3260,6 +3260,7 @@ dependencies = [ name = "komorebi-config" version = "0.1.42" dependencies = [ + "clap", "komorebi-layouts", "komorebi-protocol", "komorebi-themes",