diff --git a/crates/komorebic-core/src/args.rs b/crates/komorebic-core/src/args.rs index 49f86001..126184e3 100644 --- a/crates/komorebic-core/src/args.rs +++ b/crates/komorebic-core/src/args.rs @@ -22,11 +22,13 @@ use komorebi_layouts::CycleDirection; use komorebi_layouts::DefaultLayout; use komorebi_layouts::OperationDirection; use komorebi_layouts::Sizing; +use komorebi_protocol::pathext::replace_env_in_path; use komorebi_protocol::types::ApplicationIdentifier; use komorebi_protocol::types::HidingBehaviour; use komorebi_protocol::types::MoveBehaviour; use komorebi_protocol::types::OperationBehaviour; use komorebi_protocol::types::StateQuery; +use std::path::PathBuf; /// `enable` or `disable`, the way most of the subcommands that turn something on or off say it. #[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)] @@ -175,6 +177,211 @@ pub struct ApplicationTarget { pub id: String, } +// The arguments of the subcommands that have a block of their own, and the blocks they share. + +// Which socket to send the event notifications to. +#[derive(Args, Clone, Debug)] +pub struct SubscribeSocket { + /// Name of the socket to send event notifications to + pub socket: String, +} + +// Which socket to stop sending the event notifications to. +#[derive(Args, Clone, Debug)] +pub struct UnsubscribeSocket { + /// Name of the socket to stop sending event notifications to + pub socket: String, +} + +// Where to save the resize layout dimensions to. +#[derive(Args, Clone, Debug)] +pub struct SaveResize { + /// File to which the resize layout dimensions should be saved + #[clap(value_parser = replace_env_in_path)] + pub path: PathBuf, +} + +// Where to load the resize layout dimensions from. +#[derive(Args, Clone, Debug)] +pub struct LoadResize { + /// File from which the resize layout dimensions should be loaded + #[clap(value_parser = replace_env_in_path)] + pub path: PathBuf, +} + +// Where the static configuration of a running komorebi is read from. +#[derive(Args, Clone, Debug)] +pub struct ReplaceConfiguration { + /// Static configuration JSON file from which the configuration should be loaded + #[clap(value_parser = replace_env_in_path)] + pub path: PathBuf, +} + +// The edge of the focused window to resize and whether to grow or shrink it. +#[derive(Args, Clone, Debug)] +pub struct Resize { + #[clap(value_enum)] + pub edge: OperationDirection, + #[clap(value_enum)] + pub sizing: Sizing, +} + +// The axis to resize along and whether to grow or shrink. +#[derive(Args, Clone, Debug)] +pub struct ResizeAxis { + #[clap(value_enum)] + pub axis: Axis, + #[clap(value_enum)] + pub sizing: Sizing, +} + +// The pixels the resize commands change a window by. +#[derive(Args, Clone, Debug)] +pub struct ResizeDelta { + /// The delta of pixels by which to increase or decrease window dimensions when resizing + pub pixels: i32, +} + +// A workspace of another monitor. +#[derive(Args, Clone, Debug)] +pub struct MonitorWorkspace { + /// Target monitor index (zero-indexed) + pub target_monitor: usize, + /// Workspace index on the target monitor (zero-indexed) + pub target_workspace: usize, +} + +// The four sides of a work area offset. +#[derive(Args, Clone, Debug)] +pub struct WorkAreaOffset { + /// Size of the left work area offset (set right to left * 2 to maintain right padding) + pub left: i32, + /// Size of the top work area offset (set bottom to the same value to maintain bottom padding) + pub top: i32, + /// Size of the right work area offset + pub right: i32, + /// Size of the bottom work area offset + pub bottom: i32, +} + +// The work area offset of a monitor. +#[derive(Args, Clone, Debug)] +pub struct MonitorWorkAreaOffset { + /// Monitor index (zero-indexed) + pub monitor: usize, + #[command(flatten)] + pub offset: WorkAreaOffset, +} + +// The work area offset of a workspace. +#[derive(Args, Clone, Debug)] +pub struct WorkspaceWorkAreaOffset { + /// Monitor index (zero-indexed) + pub monitor: usize, + /// Workspace index (zero-indexed) + pub workspace: usize, + #[command(flatten)] + pub offset: WorkAreaOffset, +} + +// A monitor to give an index to. +#[derive(Args, Clone, Debug)] +pub struct DisplayIndexPreference { + /// Preferred monitor index (zero-indexed) + pub index_preference: usize, + /// Display name as identified in komorebic state + pub display: String, +} + +// How many workspaces a monitor has at least. +#[derive(Args, Clone, Debug)] +pub struct EnsureWorkspaces { + /// Monitor index (zero-indexed) + pub monitor: usize, + /// Number of desired workspaces + pub workspace_count: usize, +} + +// The names of the workspaces a monitor has. +#[derive(Args, Clone, Debug)] +pub struct EnsureNamedWorkspaces { + /// Monitor index (zero-indexed) + pub monitor: usize, + /// Names of desired workspaces + pub names: Vec, +} + +// A layout a workspace changes to at a number of containers. +#[derive(Args, Clone, Debug)] +pub struct WorkspaceLayoutRule { + #[command(flatten)] + pub target: WorkspaceTarget, + + /// The number of window containers on-screen required to trigger this layout rule + pub at_container_count: usize, + + #[clap(value_enum)] + pub layout: DefaultLayout, +} + +// A layout a named workspace changes to at a number of containers. +#[derive(Args, Clone, Debug)] +pub struct NamedWorkspaceLayoutRule { + #[command(flatten)] + pub target: NamedTarget, + + /// The number of window containers on-screen required to trigger this layout rule + pub at_container_count: usize, + + #[clap(value_enum)] + pub layout: DefaultLayout, +} + +// A named workspace whose rules are cleared. +#[derive(Args, Clone, Debug)] +pub struct ClearNamedWorkspaceRules { + /// Name of a workspace + pub workspace: String, +} + +// An application and the workspace its windows go to. +#[derive(Args, Clone, Debug)] +pub struct WorkspaceRule { + #[command(flatten)] + pub application: ApplicationTarget, + #[command(flatten)] + pub target: WorkspaceTarget, +} + +// An application and the named workspace its windows go to. +#[derive(Args, Clone, Debug)] +pub struct NamedWorkspaceRule { + #[command(flatten)] + pub application: ApplicationTarget, + /// Name of a workspace + pub workspace: String, +} + +// The width of the border. +#[derive(Args, Clone, Debug)] +pub struct BorderWidth { + /// Desired width of the window border + pub width: i32, +} + +// The offset of the border. +#[derive(Args, Clone, Debug)] +pub struct BorderOffset { + /// Desired offset of the window border + pub offset: i32, +} + +// The frame rate of the animations. +#[derive(Args, Clone, Debug)] +pub struct AnimationFps { + /// Desired animation frames per second + pub fps: u64, +} #[cfg(test)] mod tests { use super::*; diff --git a/crates/komorebic-core/src/cli.rs b/crates/komorebic-core/src/cli.rs new file mode 100644 index 00000000..4042d1bd --- /dev/null +++ b/crates/komorebic-core/src/cli.rs @@ -0,0 +1,94 @@ +//! The command line of one platform: [`Messages`] and the subcommands of the platform. + +use crate::messages::Messages; +use clap::Command; +use clap::CommandFactory; +use clap::FromArgMatches; +use clap::Parser; +use clap::Subcommand; + +/// What the top of the help and `--version` say. It is the platform's, so each `komorebic` passes +/// its own (the package description, the authors and its long version). +#[derive(Clone, Copy, Debug)] +pub struct Meta { + pub name: &'static str, + pub about: &'static str, + pub author: &'static str, + pub version: &'static str, +} + +/// A subcommand: one of [`Messages`], or one that only this platform has, or has differently. +/// +/// The two are flattened, so they are all subcommands of `komorebic` and none is nested. +#[derive(Subcommand, Clone, Debug)] +pub enum Cmd { + #[command(flatten)] + Messages(Messages), + #[command(flatten)] + Platform(P), +} + +/// The whole command line, generic over the subcommands of the platform. +#[derive(Parser, Debug)] +pub struct Opts { + #[command(subcommand)] + pub cmd: Cmd

, +} + +/// The clap command of a platform, with its top-level texts. +pub fn command(meta: &Meta) -> Command { + Opts::

::command() + .name(meta.name) + .about(meta.about) + .author(meta.author) + .version(meta.version) +} + +/// Parse the arguments of the process, and exit with the usage if they are wrong. +pub fn parse(meta: &Meta) -> Opts

{ + let matches = command::

(meta).get_matches(); + Opts::

::from_arg_matches(&matches).unwrap_or_else(|error| error.exit()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Subcommand, Clone, Debug)] + enum Only { + /// A command of one platform + Special, + } + + const META: Meta = Meta { + name: "komorebic", + about: "about", + author: "author", + version: "1.2.3", + }; + + #[test] + fn every_message_builds_next_to_a_platform_command() { + // building the command asserts that the names, aliases and arguments are consistent + command::(&META).debug_assert(); + } + + #[test] + fn a_message_and_a_platform_command_are_both_top_level() { + let focus = Opts::::try_parse_from(["komorebic", "focus", "left"]).unwrap(); + assert!(matches!(focus.cmd, Cmd::Messages(Messages::Focus(_)))); + let special = Opts::::try_parse_from(["komorebic", "special"]).unwrap(); + assert!(matches!(special.cmd, Cmd::Platform(Only::Special))); + } + + #[test] + fn the_top_level_texts_are_the_platforms() { + let cmd = command::(&META); + assert_eq!(cmd.get_name(), "komorebic"); + assert_eq!(cmd.get_version(), Some("1.2.3")); + assert_eq!( + cmd.get_about().map(ToString::to_string).as_deref(), + Some("about") + ); + } +} diff --git a/crates/komorebic-core/src/lib.rs b/crates/komorebic-core/src/lib.rs index a6359bb5..5355ce5c 100644 --- a/crates/komorebic-core/src/lib.rs +++ b/crates/komorebic-core/src/lib.rs @@ -4,7 +4,11 @@ //! platform is a type parameter, so one binary can render the help of all of them. //! //! - [`args`] has the argument blocks the subcommands are built from. +//! - [`cli`] has [`cli::Opts`], the command line of a platform, generic over its own subcommands. +//! - [`messages`] has the subcommands that are one message to the daemon. //! - [`docgen`] renders the pages of `docs/cli`. pub mod args; +pub mod cli; pub mod docgen; +pub mod messages; diff --git a/crates/komorebic-core/src/messages.rs b/crates/komorebic-core/src/messages.rs new file mode 100644 index 00000000..935a895e --- /dev/null +++ b/crates/komorebic-core/src/messages.rs @@ -0,0 +1,668 @@ +//! The subcommands that are one message to the daemon. +//! +//! Every subcommand in [`Messages`] is the same in the three `komorebic`s: the same arguments, the +//! same help, one [`SocketMessage`] written to the socket (or, for a question, written and +//! answered). The conversion to the message is one exhaustive `match`, so a subcommand added here +//! cannot be left without its message. + +use crate::args::*; +use clap::Subcommand; +use komorebi_layouts::Axis; +use komorebi_layouts::CycleDirection; +use komorebi_layouts::DefaultLayout; +use komorebi_layouts::OperationDirection; +use komorebi_layouts::Rect; +use komorebi_protocol::message::SocketMessage; +use komorebi_protocol::types::MoveBehaviour; +use komorebi_protocol::types::StateQuery; + +#[derive(Subcommand, Clone, Debug)] +pub enum Messages { + /// Show a JSON representation of the current window manager state + State, + /// Show a JSON representation of the current global state + GlobalState, + /// Show a JSON representation of visible windows + VisibleWindows, + /// Show information about connected monitors + #[clap(alias = "monitor-info")] + MonitorInformation, + /// Query the current window manager state + #[clap(arg_required_else_help = true)] + Query(OneEnum), + /// Subscribe to komorebi events using a Unix Domain Socket + #[clap(arg_required_else_help = true)] + SubscribeSocket(SubscribeSocket), + /// Unsubscribe from komorebi events + #[clap(arg_required_else_help = true)] + UnsubscribeSocket(UnsubscribeSocket), + /// Quicksave the current resize layout dimensions + #[clap(alias = "quick-save")] + QuickSaveResize, + /// Load the last quicksaved resize layout dimensions + #[clap(alias = "quick-load")] + QuickLoadResize, + /// Save the current resize layout dimensions to a file + #[clap(arg_required_else_help = true)] + #[clap(alias = "save")] + SaveResize(SaveResize), + /// Load the resize layout dimensions from a file + #[clap(arg_required_else_help = true)] + #[clap(alias = "load")] + LoadResize(LoadResize), + /// Change focus to the window in the specified direction + #[clap(arg_required_else_help = true)] + Focus(OneEnum), + /// Move the focused window in the specified direction + #[clap(arg_required_else_help = true)] + Move(OneEnum), + /// Preselect the specified direction for the next window to be spawned on supported layouts + #[clap(arg_required_else_help = true)] + PreselectDirection(OneEnum), + /// Cancel a workspace preselect set by the preselect-direction command, if one exists + CancelPreselect, + /// Minimize the focused window + Minimize, + /// Close the focused window + Close, + /// Change focus to the window in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleFocus(OneEnum), + /// Move the focused window in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleMove(OneEnum), + /// Stack the focused window in the specified direction + #[clap(arg_required_else_help = true)] + Stack(OneEnum), + /// Unstack the focused window + Unstack, + /// Cycle the focused stack in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleStack(OneEnum), + /// Cycle the index of the focused window in the focused stack in the specified cycle direction + #[clap(arg_required_else_help = true)] + CycleStackIndex(OneEnum), + /// Focus the specified window index in the focused stack + #[clap(arg_required_else_help = true)] + FocusStackWindow(Target), + /// Stack all windows on the focused workspace + StackAll, + /// Unstack all windows in the focused container + UnstackAll, + /// Resize the focused window in the specified direction + #[clap(arg_required_else_help = true)] + #[clap(alias = "resize")] + ResizeEdge(Resize), + /// Resize the focused window or primary column along the specified axis + #[clap(arg_required_else_help = true)] + ResizeAxis(ResizeAxis), + /// Move the focused window to the specified monitor + #[clap(arg_required_else_help = true)] + MoveToMonitor(Target), + /// Move the focused window to the monitor in the given cycle direction + #[clap(arg_required_else_help = true)] + CycleMoveToMonitor(OneEnum), + /// Move the focused window to the specified workspace + #[clap(arg_required_else_help = true)] + MoveToWorkspace(Target), + /// Move the focused window to the specified workspace + #[clap(arg_required_else_help = true)] + MoveToNamedWorkspace(NamedTarget), + /// Move the focused window to the workspace in the given cycle direction + #[clap(arg_required_else_help = true)] + CycleMoveToWorkspace(OneEnum), + /// Send the focused window to the specified monitor + #[clap(arg_required_else_help = true)] + SendToMonitor(Target), + /// Send the focused window to the monitor in the given cycle direction + #[clap(arg_required_else_help = true)] + CycleSendToMonitor(OneEnum), + /// Send the focused window to the specified workspace + #[clap(arg_required_else_help = true)] + SendToWorkspace(Target), + /// Send the focused window to the specified workspace + #[clap(arg_required_else_help = true)] + SendToNamedWorkspace(NamedTarget), + /// Send the focused window to the workspace in the given cycle direction + #[clap(arg_required_else_help = true)] + CycleSendToWorkspace(OneEnum), + /// Send the focused window to the specified monitor workspace + #[clap(arg_required_else_help = true)] + SendToMonitorWorkspace(MonitorWorkspace), + /// Move the focused window to the specified monitor workspace + #[clap(arg_required_else_help = true)] + MoveToMonitorWorkspace(MonitorWorkspace), + /// Send the focused window to the last focused monitor workspace + SendToLastWorkspace, + /// Move the focused window to the last focused monitor workspace + MoveToLastWorkspace, + /// Focus the specified monitor + #[clap(arg_required_else_help = true)] + FocusMonitor(Target), + /// Focus the monitor at the current cursor location + FocusMonitorAtCursor, + /// Focus the last focused workspace on the focused monitor + FocusLastWorkspace, + /// Focus the specified workspace on the focused monitor + #[clap(arg_required_else_help = true)] + FocusWorkspace(Target), + /// Focus the specified workspace on all monitors + #[clap(arg_required_else_help = true)] + FocusWorkspaces(Target), + /// Focus the specified workspace on the target monitor + #[clap(arg_required_else_help = true)] + FocusMonitorWorkspace(MonitorWorkspace), + /// Focus the specified workspace + #[clap(arg_required_else_help = true)] + FocusNamedWorkspace(NamedTarget), + /// Close the focused workspace (must be empty and unnamed) + CloseWorkspace, + /// Focus the monitor in the given cycle direction + #[clap(arg_required_else_help = true)] + CycleMonitor(OneEnum), + /// Focus the workspace in the given cycle direction + #[clap(arg_required_else_help = true)] + CycleWorkspace(OneEnum), + /// Focus the next empty workspace in the given cycle direction (if one exists) + #[clap(arg_required_else_help = true)] + CycleEmptyWorkspace(OneEnum), + /// Move the focused workspace to the specified monitor + #[clap(arg_required_else_help = true)] + MoveWorkspaceToMonitor(Target), + /// Move the focused workspace monitor in the given cycle direction + #[clap(arg_required_else_help = true)] + CycleMoveWorkspaceToMonitor(OneEnum), + /// Swap focused monitor workspaces with specified monitor + #[clap(arg_required_else_help = true)] + SwapWorkspacesWithMonitor(Target), + /// Create and append a new workspace on the focused monitor + NewWorkspace, + /// Set the resize delta (used by resize-edge and resize-axis) + #[clap(arg_required_else_help = true)] + ResizeDelta(ResizeDelta), + /// Set offsets to exclude parts of the work area from tiling + #[clap(arg_required_else_help = true)] + GlobalWorkAreaOffset(WorkAreaOffset), + /// Set offsets for a monitor to exclude parts of the work area from tiling + #[clap(arg_required_else_help = true)] + MonitorWorkAreaOffset(MonitorWorkAreaOffset), + /// Set offsets for a workspace to exclude parts of the work area from tiling + #[clap(arg_required_else_help = true)] + WorkspaceWorkAreaOffset(WorkspaceWorkAreaOffset), + /// Toggle application of the window-based work area offset for the focused workspace + ToggleWindowBasedWorkAreaOffset, + /// Set container padding on the focused workspace + #[clap(arg_required_else_help = true)] + FocusedWorkspaceContainerPadding(Size), + /// Set workspace padding on the focused workspace + #[clap(arg_required_else_help = true)] + FocusedWorkspacePadding(Size), + /// Adjust container padding on the focused workspace + #[clap(arg_required_else_help = true)] + AdjustContainerPadding(PaddingAdjustment), + /// Adjust workspace padding on the focused workspace + #[clap(arg_required_else_help = true)] + AdjustWorkspacePadding(PaddingAdjustment), + /// Set the layout on the focused workspace + #[clap(arg_required_else_help = true)] + ChangeLayout(OneEnum), + /// Cycle between available layouts + #[clap(arg_required_else_help = true)] + CycleLayout(OneEnum), + /// Make the focused column of the Scrolling layout fill the room that the columns in view leave + /// on the screen, which does nothing in any other layout + Expand, + /// Flip the layout on the focused workspace + #[clap(arg_required_else_help = true)] + FlipLayout(OneEnum), + /// Promote the focused window to the largest tile via container removal and re-insertion + Promote, + /// Promote the focused window to the largest tile by swapping container indices with the largest tile + PromoteSwap, + /// Promote the window in the specified direction + PromoteWindow(OneEnum), + /// Set the display index preference for a monitor identified using its display name + #[clap(arg_required_else_help = true)] + DisplayIndexPreference(DisplayIndexPreference), + /// Create at least this many workspaces for the specified monitor + #[clap(arg_required_else_help = true)] + EnsureWorkspaces(EnsureWorkspaces), + /// Create these many named workspaces for the specified monitor + #[clap(arg_required_else_help = true)] + EnsureNamedWorkspaces(EnsureNamedWorkspaces), + /// Set the container padding for the specified workspace + #[clap(arg_required_else_help = true)] + ContainerPadding(WorkspacePadding), + /// Set the container padding for the specified workspace + #[clap(arg_required_else_help = true)] + NamedWorkspaceContainerPadding(NamedPadding), + /// Set the workspace padding for the specified workspace + #[clap(arg_required_else_help = true)] + WorkspacePadding(WorkspacePadding), + /// Set the workspace padding for the specified workspace + #[clap(arg_required_else_help = true)] + NamedWorkspacePadding(NamedPadding), + /// Set the layout for the specified workspace + #[clap(arg_required_else_help = true)] + WorkspaceLayout(WorkspaceEnum), + /// Set the layout for the specified workspace + #[clap(arg_required_else_help = true)] + NamedWorkspaceLayout(NamedWorkspaceEnum), + /// Add a dynamic layout rule for the specified workspace + #[clap(arg_required_else_help = true)] + WorkspaceLayoutRule(WorkspaceLayoutRule), + /// Add a dynamic layout rule for the specified workspace + #[clap(arg_required_else_help = true)] + NamedWorkspaceLayoutRule(NamedWorkspaceLayoutRule), + /// Clear all dynamic layout rules for the specified workspace + #[clap(arg_required_else_help = true)] + ClearWorkspaceLayoutRules(WorkspaceTarget), + /// Clear all dynamic layout rules for the specified workspace + #[clap(arg_required_else_help = true)] + ClearNamedWorkspaceLayoutRules(NamedTarget), + /// Enable or disable window tiling for the specified workspace + #[clap(arg_required_else_help = true)] + WorkspaceTiling(WorkspaceEnum), + /// Enable or disable window tiling for the specified workspace + #[clap(arg_required_else_help = true)] + NamedWorkspaceTiling(NamedWorkspaceEnum), + /// Set the workspace name for the specified workspace + #[clap(arg_required_else_help = true)] + WorkspaceName(WorkspaceString), + /// Enable or disable float override, which makes it so every new window opens in floating mode + ToggleFloatOverride, + /// Enable or disable float override, which makes it so every new window opens in floating + /// mode, for the currently focused workspace. If there was no override value set for the + /// workspace previously it takes the opposite of the global value. + ToggleWorkspaceFloatOverride, + /// Toggle between the Tiling and Floating layers on the focused workspace + ToggleWorkspaceLayer, + /// Toggle the paused state for all window tiling + TogglePause, + /// Toggle window tiling on the focused workspace + ToggleTiling, + /// Toggle floating mode for the focused window + ToggleFloat, + /// Toggle monocle mode for the focused container + ToggleMonocle, + /// Toggle a lock for the focused container, ensuring it will not be displaced by any new windows + ToggleLock, + /// Force komorebi to manage the focused window + Manage, + /// Unmanage a window that was forcibly managed + Unmanage, + /// Set the behaviour when moving windows across monitor boundaries + #[clap(arg_required_else_help = true)] + CrossMonitorMoveBehaviour(OneEnum), + /// Toggle the behaviour when moving windows across monitor boundaries + ToggleCrossMonitorMoveBehaviour, + /// Add a rule to float the foreground window for the rest of this session + SessionFloatRule, + /// Show all session float rules + SessionFloatRules, + /// Clear all session float rules + ClearSessionFloatRules, + /// Add a rule to ignore the specified application + #[clap(arg_required_else_help = true)] + #[clap(alias = "float-rule")] + IgnoreRule(ApplicationTarget), + /// Add a rule to always manage the specified application + #[clap(arg_required_else_help = true)] + ManageRule(ApplicationTarget), + /// Add a rule to associate an application with a workspace on first show + #[clap(arg_required_else_help = true)] + InitialWorkspaceRule(WorkspaceRule), + /// Add a rule to associate an application with a named workspace on first show + #[clap(arg_required_else_help = true)] + InitialNamedWorkspaceRule(NamedWorkspaceRule), + /// Add a rule to associate an application with a workspace + #[clap(arg_required_else_help = true)] + WorkspaceRule(WorkspaceRule), + /// Add a rule to associate an application with a named workspace + #[clap(arg_required_else_help = true)] + NamedWorkspaceRule(NamedWorkspaceRule), + /// Remove all application association rules for a workspace by monitor and workspace index + #[clap(arg_required_else_help = true)] + ClearWorkspaceRules(WorkspaceTarget), + /// Remove all application association rules for a named workspace + #[clap(arg_required_else_help = true)] + ClearNamedWorkspaceRules(ClearNamedWorkspaceRules), + /// Remove all application association rules for all workspaces + ClearAllWorkspaceRules, + /// Enforce all workspace rules, including initial workspace rules that have already been applied + EnforceWorkspaceRules, + /// Enable or disable borders + #[clap(arg_required_else_help = true)] + #[clap(alias = "active-window-border")] + Border(OneEnum), + /// Set the border width + #[clap(arg_required_else_help = true)] + #[clap(alias = "active-window-border-width")] + BorderWidth(BorderWidth), + /// Set the border offset + #[clap(arg_required_else_help = true)] + #[clap(alias = "active-window-border-offset")] + BorderOffset(BorderOffset), + /// Set the frames per second for movement animations + #[clap(arg_required_else_help = true)] + AnimationFps(AnimationFps), + /// Enable or disable mouse follows focus on all workspaces + #[clap(arg_required_else_help = true)] + MouseFollowsFocus(OneEnum), + /// Toggle mouse follows focus on all workspaces + ToggleMouseFollowsFocus, + /// Replace the configuration of a running instance of komorebi from a static configuration file + #[clap(arg_required_else_help = true)] + ReplaceConfiguration(ReplaceConfiguration), +} + +impl Messages { + /// Whether the daemon answers the message, and the answer is to be printed, rather than the + /// message being an order. + pub fn is_query(&self) -> bool { + matches!( + self, + Self::State + | Self::GlobalState + | Self::VisibleWindows + | Self::MonitorInformation + | Self::Query(_) + | Self::SessionFloatRules + ) + } +} + +impl From for SocketMessage { + fn from(messages: Messages) -> Self { + match messages { + Messages::State => SocketMessage::State, + Messages::GlobalState => SocketMessage::GlobalState, + Messages::VisibleWindows => SocketMessage::VisibleWindows, + Messages::MonitorInformation => SocketMessage::MonitorInformation, + Messages::Query(args) => SocketMessage::Query(args.value), + Messages::SubscribeSocket(args) => SocketMessage::AddSubscriberSocket(args.socket), + Messages::UnsubscribeSocket(args) => SocketMessage::RemoveSubscriberSocket(args.socket), + Messages::QuickSaveResize => SocketMessage::QuickSave, + Messages::QuickLoadResize => SocketMessage::QuickLoad, + Messages::SaveResize(args) => SocketMessage::Save(args.path), + Messages::LoadResize(args) => SocketMessage::Load(args.path), + Messages::Focus(args) => SocketMessage::FocusWindow(args.value), + Messages::Move(args) => SocketMessage::MoveWindow(args.value), + Messages::PreselectDirection(args) => SocketMessage::PreselectDirection(args.value), + Messages::CancelPreselect => SocketMessage::CancelPreselect, + Messages::Minimize => SocketMessage::Minimize, + Messages::Close => SocketMessage::Close, + Messages::CycleFocus(args) => SocketMessage::CycleFocusWindow(args.value), + Messages::CycleMove(args) => SocketMessage::CycleMoveWindow(args.value), + Messages::Stack(args) => SocketMessage::StackWindow(args.value), + Messages::Unstack => SocketMessage::UnstackWindow, + Messages::CycleStack(args) => SocketMessage::CycleStack(args.value), + Messages::CycleStackIndex(args) => SocketMessage::CycleStackIndex(args.value), + Messages::FocusStackWindow(args) => SocketMessage::FocusStackWindow(args.target), + Messages::StackAll => SocketMessage::StackAll, + Messages::UnstackAll => SocketMessage::UnstackAll, + Messages::ResizeEdge(args) => SocketMessage::ResizeWindowEdge(args.edge, args.sizing), + Messages::ResizeAxis(args) => SocketMessage::ResizeWindowAxis(args.axis, args.sizing), + Messages::MoveToMonitor(args) => { + SocketMessage::MoveContainerToMonitorNumber(args.target) + } + Messages::CycleMoveToMonitor(args) => { + SocketMessage::CycleMoveContainerToMonitor(args.value) + } + Messages::MoveToWorkspace(args) => { + SocketMessage::MoveContainerToWorkspaceNumber(args.target) + } + Messages::MoveToNamedWorkspace(args) => { + SocketMessage::MoveContainerToNamedWorkspace(args.workspace) + } + Messages::CycleMoveToWorkspace(args) => { + SocketMessage::CycleMoveContainerToWorkspace(args.value) + } + Messages::SendToMonitor(args) => { + SocketMessage::SendContainerToMonitorNumber(args.target) + } + Messages::CycleSendToMonitor(args) => { + SocketMessage::CycleSendContainerToMonitor(args.value) + } + Messages::SendToWorkspace(args) => { + SocketMessage::SendContainerToWorkspaceNumber(args.target) + } + Messages::SendToNamedWorkspace(args) => { + SocketMessage::SendContainerToNamedWorkspace(args.workspace) + } + Messages::CycleSendToWorkspace(args) => { + SocketMessage::CycleSendContainerToWorkspace(args.value) + } + Messages::SendToMonitorWorkspace(args) => { + SocketMessage::SendContainerToMonitorWorkspaceNumber( + args.target_monitor, + args.target_workspace, + ) + } + Messages::MoveToMonitorWorkspace(args) => { + SocketMessage::MoveContainerToMonitorWorkspaceNumber( + args.target_monitor, + args.target_workspace, + ) + } + Messages::SendToLastWorkspace => SocketMessage::SendContainerToLastWorkspace, + Messages::MoveToLastWorkspace => SocketMessage::MoveContainerToLastWorkspace, + Messages::FocusMonitor(args) => SocketMessage::FocusMonitorNumber(args.target), + Messages::FocusMonitorAtCursor => SocketMessage::FocusMonitorAtCursor, + Messages::FocusLastWorkspace => SocketMessage::FocusLastWorkspace, + Messages::FocusWorkspace(args) => SocketMessage::FocusWorkspaceNumber(args.target), + Messages::FocusWorkspaces(args) => SocketMessage::FocusWorkspaceNumbers(args.target), + Messages::FocusMonitorWorkspace(args) => SocketMessage::FocusMonitorWorkspaceNumber( + args.target_monitor, + args.target_workspace, + ), + Messages::FocusNamedWorkspace(args) => { + SocketMessage::FocusNamedWorkspace(args.workspace) + } + Messages::CloseWorkspace => SocketMessage::CloseWorkspace, + Messages::CycleMonitor(args) => SocketMessage::CycleFocusMonitor(args.value), + Messages::CycleWorkspace(args) => SocketMessage::CycleFocusWorkspace(args.value), + Messages::CycleEmptyWorkspace(args) => { + SocketMessage::CycleFocusEmptyWorkspace(args.value) + } + Messages::MoveWorkspaceToMonitor(args) => { + SocketMessage::MoveWorkspaceToMonitorNumber(args.target) + } + Messages::CycleMoveWorkspaceToMonitor(args) => { + SocketMessage::CycleMoveWorkspaceToMonitor(args.value) + } + Messages::SwapWorkspacesWithMonitor(args) => { + SocketMessage::SwapWorkspacesToMonitorNumber(args.target) + } + Messages::NewWorkspace => SocketMessage::NewWorkspace, + Messages::ResizeDelta(args) => SocketMessage::ResizeDelta(args.pixels), + Messages::GlobalWorkAreaOffset(args) => SocketMessage::WorkAreaOffset(Rect { + left: args.left, + top: args.top, + right: args.right, + bottom: args.bottom, + }), + Messages::MonitorWorkAreaOffset(args) => SocketMessage::MonitorWorkAreaOffset( + args.monitor, + Rect { + left: args.offset.left, + top: args.offset.top, + right: args.offset.right, + bottom: args.offset.bottom, + }, + ), + Messages::WorkspaceWorkAreaOffset(args) => SocketMessage::WorkspaceWorkAreaOffset( + args.monitor, + args.workspace, + Rect { + left: args.offset.left, + top: args.offset.top, + right: args.offset.right, + bottom: args.offset.bottom, + }, + ), + Messages::ToggleWindowBasedWorkAreaOffset => { + SocketMessage::ToggleWindowBasedWorkAreaOffset + } + Messages::FocusedWorkspaceContainerPadding(args) => { + SocketMessage::FocusedWorkspaceContainerPadding(args.size) + } + Messages::FocusedWorkspacePadding(args) => { + SocketMessage::FocusedWorkspacePadding(args.size) + } + Messages::AdjustContainerPadding(args) => { + SocketMessage::AdjustContainerPadding(args.sizing, args.adjustment) + } + Messages::AdjustWorkspacePadding(args) => { + SocketMessage::AdjustWorkspacePadding(args.sizing, args.adjustment) + } + Messages::ChangeLayout(args) => SocketMessage::ChangeLayout(args.value), + Messages::CycleLayout(args) => SocketMessage::CycleLayout(args.value), + Messages::Expand => SocketMessage::Expand, + Messages::FlipLayout(args) => SocketMessage::FlipLayout(args.value), + Messages::Promote => SocketMessage::Promote, + Messages::PromoteSwap => SocketMessage::PromoteSwap, + Messages::PromoteWindow(args) => SocketMessage::PromoteWindow(args.value), + Messages::DisplayIndexPreference(args) => { + SocketMessage::DisplayIndexPreference(args.index_preference, args.display) + } + Messages::EnsureWorkspaces(args) => { + SocketMessage::EnsureWorkspaces(args.monitor, args.workspace_count) + } + Messages::EnsureNamedWorkspaces(args) => { + SocketMessage::EnsureNamedWorkspaces(args.monitor, args.names) + } + Messages::ContainerPadding(args) => SocketMessage::ContainerPadding( + args.target.monitor, + args.target.workspace, + args.size, + ), + Messages::NamedWorkspaceContainerPadding(args) => { + SocketMessage::NamedWorkspaceContainerPadding(args.target.workspace, args.size) + } + Messages::WorkspacePadding(args) => SocketMessage::WorkspacePadding( + args.target.monitor, + args.target.workspace, + args.size, + ), + Messages::NamedWorkspacePadding(args) => { + SocketMessage::NamedWorkspacePadding(args.target.workspace, args.size) + } + Messages::WorkspaceLayout(args) => SocketMessage::WorkspaceLayout( + args.target.monitor, + args.target.workspace, + args.value, + ), + Messages::NamedWorkspaceLayout(args) => { + SocketMessage::NamedWorkspaceLayout(args.target.workspace, args.value) + } + Messages::WorkspaceLayoutRule(args) => SocketMessage::WorkspaceLayoutRule( + args.target.monitor, + args.target.workspace, + args.at_container_count, + args.layout, + ), + Messages::NamedWorkspaceLayoutRule(args) => SocketMessage::NamedWorkspaceLayoutRule( + args.target.workspace, + args.at_container_count, + args.layout, + ), + Messages::ClearWorkspaceLayoutRules(args) => { + SocketMessage::ClearWorkspaceLayoutRules(args.monitor, args.workspace) + } + Messages::ClearNamedWorkspaceLayoutRules(args) => { + SocketMessage::ClearNamedWorkspaceLayoutRules(args.workspace) + } + Messages::WorkspaceTiling(args) => SocketMessage::WorkspaceTiling( + args.target.monitor, + args.target.workspace, + args.value.into(), + ), + Messages::NamedWorkspaceTiling(args) => { + SocketMessage::NamedWorkspaceTiling(args.target.workspace, args.value.into()) + } + Messages::WorkspaceName(args) => { + SocketMessage::WorkspaceName(args.target.monitor, args.target.workspace, args.value) + } + Messages::ToggleFloatOverride => SocketMessage::ToggleFloatOverride, + Messages::ToggleWorkspaceFloatOverride => SocketMessage::ToggleWorkspaceFloatOverride, + Messages::ToggleWorkspaceLayer => SocketMessage::ToggleWorkspaceLayer, + Messages::TogglePause => SocketMessage::TogglePause, + Messages::ToggleTiling => SocketMessage::ToggleTiling, + Messages::ToggleFloat => SocketMessage::ToggleFloat, + Messages::ToggleMonocle => SocketMessage::ToggleMonocle, + Messages::ToggleLock => SocketMessage::ToggleLock, + Messages::Manage => SocketMessage::ManageFocusedWindow, + Messages::Unmanage => SocketMessage::UnmanageFocusedWindow, + Messages::CrossMonitorMoveBehaviour(args) => { + SocketMessage::CrossMonitorMoveBehaviour(args.value) + } + Messages::ToggleCrossMonitorMoveBehaviour => { + SocketMessage::ToggleCrossMonitorMoveBehaviour + } + Messages::SessionFloatRule => SocketMessage::SessionFloatRule, + Messages::SessionFloatRules => SocketMessage::SessionFloatRules, + Messages::ClearSessionFloatRules => SocketMessage::ClearSessionFloatRules, + Messages::IgnoreRule(args) => SocketMessage::IgnoreRule(args.identifier, args.id), + Messages::ManageRule(args) => SocketMessage::ManageRule(args.identifier, args.id), + Messages::InitialWorkspaceRule(args) => SocketMessage::InitialWorkspaceRule( + args.application.identifier, + args.application.id, + args.target.monitor, + args.target.workspace, + ), + Messages::InitialNamedWorkspaceRule(args) => SocketMessage::InitialNamedWorkspaceRule( + args.application.identifier, + args.application.id, + args.workspace, + ), + Messages::WorkspaceRule(args) => SocketMessage::WorkspaceRule( + args.application.identifier, + args.application.id, + args.target.monitor, + args.target.workspace, + ), + Messages::NamedWorkspaceRule(args) => SocketMessage::NamedWorkspaceRule( + args.application.identifier, + args.application.id, + args.workspace, + ), + Messages::ClearWorkspaceRules(args) => { + SocketMessage::ClearWorkspaceRules(args.monitor, args.workspace) + } + Messages::ClearNamedWorkspaceRules(args) => { + SocketMessage::ClearNamedWorkspaceRules(args.workspace) + } + Messages::ClearAllWorkspaceRules => SocketMessage::ClearAllWorkspaceRules, + Messages::EnforceWorkspaceRules => SocketMessage::EnforceWorkspaceRules, + Messages::Border(args) => SocketMessage::Border(args.value.into()), + Messages::BorderWidth(args) => SocketMessage::BorderWidth(args.width), + Messages::BorderOffset(args) => SocketMessage::BorderOffset(args.offset), + Messages::AnimationFps(args) => SocketMessage::AnimationFps(args.fps), + Messages::MouseFollowsFocus(args) => { + SocketMessage::MouseFollowsFocus(args.value.into()) + } + Messages::ToggleMouseFollowsFocus => SocketMessage::ToggleMouseFollowsFocus, + Messages::ReplaceConfiguration(args) => SocketMessage::ReplaceConfiguration(args.path), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_question_is_printed_and_an_order_is_not() { + assert!(Messages::State.is_query()); + assert!(!Messages::Minimize.is_query()); + } + + #[test] + fn a_message_converts_to_the_socket_message_it_names() { + let message: SocketMessage = Messages::ToggleMonocle.into(); + assert!(matches!(message, SocketMessage::ToggleMonocle)); + let message: SocketMessage = Messages::FocusMonitor(Target { target: 2 }).into(); + assert!(matches!(message, SocketMessage::FocusMonitorNumber(2))); + } +}