feat(cli): add cycle-layout command (#556)

* Command to ToggleLayout

* Just improve logic of figuring out next layout

* Addr review: rename to "Cycle" instead of toggle, and add a small comment

* As per review comments, implement cycle method on DefaultLayout

* I forgot to remove this, my bad

* feat(cli): fixups for cycle-layout cmd

* Update komorebic/src/main.rs

Co-authored-by: Kushashwa Ravi Shrimali <kushashwaravishrimali@gmail.com>

---------

Co-authored-by: LGUG2Z <jadeiqbal@fastmail.com>
Co-authored-by: جاد <LGUG2Z@users.noreply.github.com>
This commit is contained in:
Kushashwa Ravi Shrimali
2023-10-27 05:03:27 +05:30
committed by LGUG2Z
parent 557d1962a7
commit e9fb9297e9
6 changed files with 62 additions and 0 deletions
+25
View File
@@ -20,6 +20,7 @@ pub enum DefaultLayout {
VerticalStack, VerticalStack,
HorizontalStack, HorizontalStack,
UltrawideVerticalStack, UltrawideVerticalStack,
// NOTE: If any new layout is added, please make sure to register the same in `DefaultLayout::cycle`
} }
impl DefaultLayout { impl DefaultLayout {
@@ -125,4 +126,28 @@ impl DefaultLayout {
Option::from(r) Option::from(r)
} }
} }
#[must_use]
pub const fn cycle_next(self) -> Self {
match self {
Self::BSP => Self::Columns,
Self::Columns => Self::Rows,
Self::Rows => Self::VerticalStack,
Self::VerticalStack => Self::HorizontalStack,
Self::HorizontalStack => Self::UltrawideVerticalStack,
Self::UltrawideVerticalStack => Self::BSP,
}
}
#[must_use]
pub const fn cycle_previous(self) -> Self {
match self {
Self::BSP => Self::UltrawideVerticalStack,
Self::UltrawideVerticalStack => Self::HorizontalStack,
Self::HorizontalStack => Self::VerticalStack,
Self::VerticalStack => Self::Rows,
Self::Rows => Self::Columns,
Self::Columns => Self::BSP,
}
}
} }
+1
View File
@@ -77,6 +77,7 @@ pub enum SocketMessage {
AdjustContainerPadding(Sizing, i32), AdjustContainerPadding(Sizing, i32),
AdjustWorkspacePadding(Sizing, i32), AdjustWorkspacePadding(Sizing, i32),
ChangeLayout(DefaultLayout), ChangeLayout(DefaultLayout),
CycleLayout(CycleDirection),
ChangeLayoutCustom(PathBuf), ChangeLayoutCustom(PathBuf),
FlipLayout(Axis), FlipLayout(Axis),
// Monitor and Workspace Commands // Monitor and Workspace Commands
+2
View File
@@ -448,6 +448,7 @@ impl WindowManager {
SocketMessage::Retile => self.retile_all(false)?, SocketMessage::Retile => self.retile_all(false)?,
SocketMessage::FlipLayout(layout_flip) => self.flip_layout(layout_flip)?, SocketMessage::FlipLayout(layout_flip) => self.flip_layout(layout_flip)?,
SocketMessage::ChangeLayout(layout) => self.change_workspace_layout_default(layout)?, SocketMessage::ChangeLayout(layout) => self.change_workspace_layout_default(layout)?,
SocketMessage::CycleLayout(direction) => self.cycle_layout(direction)?,
SocketMessage::ChangeLayoutCustom(ref path) => { SocketMessage::ChangeLayoutCustom(ref path) => {
self.change_workspace_custom_layout(path.clone())?; self.change_workspace_custom_layout(path.clone())?;
} }
@@ -1254,6 +1255,7 @@ impl WindowManager {
match message { match message {
SocketMessage::ChangeLayout(_) SocketMessage::ChangeLayout(_)
| SocketMessage::CycleLayout(_)
| SocketMessage::ChangeLayoutCustom(_) | SocketMessage::ChangeLayoutCustom(_)
| SocketMessage::FlipLayout(_) | SocketMessage::FlipLayout(_)
| SocketMessage::ManageFocusedWindow | SocketMessage::ManageFocusedWindow
+23
View File
@@ -1705,6 +1705,29 @@ impl WindowManager {
self.update_focused_workspace(self.mouse_follows_focus) self.update_focused_workspace(self.mouse_follows_focus)
} }
#[tracing::instrument(skip(self))]
pub fn cycle_layout(&mut self, direction: CycleDirection) -> Result<()> {
tracing::info!("cycling layout");
let workspace = self.focused_workspace_mut()?;
let current_layout = workspace.layout();
match current_layout {
Layout::Default(current) => {
let new_layout = match direction {
CycleDirection::Previous => current.cycle_previous(),
CycleDirection::Next => current.cycle_next(),
};
tracing::info!("next layout: {new_layout}");
workspace.set_layout(Layout::Default(new_layout));
}
Layout::Custom(_) => {}
}
self.update_focused_workspace(self.mouse_follows_focus)
}
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn change_workspace_custom_layout(&mut self, path: PathBuf) -> Result<()> { pub fn change_workspace_custom_layout(&mut self, path: PathBuf) -> Result<()> {
tracing::info!("changing layout"); tracing::info!("changing layout");
+4
View File
@@ -196,6 +196,10 @@ ChangeLayout(default_layout) {
RunWait("komorebic.exe change-layout " default_layout, , "Hide") RunWait("komorebic.exe change-layout " default_layout, , "Hide")
} }
CycleLayout(operation_direction) {
RunWait("komorebic.exe cycle-layout " operation_direction, , "Hide")
}
LoadCustomLayout(path) { LoadCustomLayout(path) {
RunWait("komorebic.exe load-custom-layout " path, , "Hide") RunWait("komorebic.exe load-custom-layout " path, , "Hide")
} }
+7
View File
@@ -125,6 +125,7 @@ gen_enum_subcommand_args! {
CycleStack: CycleDirection, CycleStack: CycleDirection,
FlipLayout: Axis, FlipLayout: Axis,
ChangeLayout: DefaultLayout, ChangeLayout: DefaultLayout,
CycleLayout: CycleDirection,
WatchConfiguration: BooleanState, WatchConfiguration: BooleanState,
MouseFollowsFocus: BooleanState, MouseFollowsFocus: BooleanState,
Query: StateQuery, Query: StateQuery,
@@ -865,6 +866,9 @@ enum SubCommand {
/// Set the layout on the focused workspace /// Set the layout on the focused workspace
#[clap(arg_required_else_help = true)] #[clap(arg_required_else_help = true)]
ChangeLayout(ChangeLayout), ChangeLayout(ChangeLayout),
/// Cycle between available layouts
#[clap(arg_required_else_help = true)]
CycleLayout(CycleLayout),
/// Load a custom layout from file for the focused workspace /// Load a custom layout from file for the focused workspace
#[clap(arg_required_else_help = true)] #[clap(arg_required_else_help = true)]
LoadCustomLayout(LoadCustomLayout), LoadCustomLayout(LoadCustomLayout),
@@ -1678,6 +1682,9 @@ Stop-Process -Name:whkd -ErrorAction SilentlyContinue
SubCommand::ChangeLayout(arg) => { SubCommand::ChangeLayout(arg) => {
send_message(&SocketMessage::ChangeLayout(arg.default_layout).as_bytes()?)?; send_message(&SocketMessage::ChangeLayout(arg.default_layout).as_bytes()?)?;
} }
SubCommand::CycleLayout(arg) => {
send_message(&SocketMessage::CycleLayout(arg.cycle_direction).as_bytes()?)?;
}
SubCommand::LoadCustomLayout(arg) => { SubCommand::LoadCustomLayout(arg) => {
send_message( send_message(
&SocketMessage::ChangeLayoutCustom(resolve_windows_path(&arg.path)?).as_bytes()?, &SocketMessage::ChangeLayoutCustom(resolve_windows_path(&arg.path)?).as_bytes()?,