diff --git a/docs/cli/center-focus.md b/docs/cli/center-focus.md new file mode 100644 index 00000000..5d256376 --- /dev/null +++ b/docs/cli/center-focus.md @@ -0,0 +1,15 @@ +# center-focus + +``` +Centre the user focus on the screen, once + +In the Scrolling layout the screen scrolls so that the focused column is in the middle of it. Only +the Scrolling layout scrolls, so this does nothing in the other layouts. + +Usage: komorebic center-focus + +Options: + -h, --help + Print help (see a summary with '-h') + +``` \ No newline at end of file diff --git a/docs/cli/expand.md b/docs/cli/expand.md new file mode 100644 index 00000000..058c1914 --- /dev/null +++ b/docs/cli/expand.md @@ -0,0 +1,13 @@ +# expand + +``` +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 + +Usage: komorebic expand + +Options: + -h, --help + Print help + +``` \ No newline at end of file diff --git a/docs/cli/scrolling-layout-full-width.md b/docs/cli/scrolling-layout-full-width.md new file mode 100644 index 00000000..c4757329 --- /dev/null +++ b/docs/cli/scrolling-layout-full-width.md @@ -0,0 +1,12 @@ +# scrolling-layout-full-width + +``` +Make the focused column of the Scrolling layout as wide as the screen, or put it back + +Usage: komorebic scrolling-layout-full-width + +Options: + -h, --help + Print help + +``` \ No newline at end of file diff --git a/docs/usage/scrolling-layout.md b/docs/usage/scrolling-layout.md index 7bed3c2b..acf71e49 100644 --- a/docs/usage/scrolling-layout.md +++ b/docs/usage/scrolling-layout.md @@ -51,6 +51,21 @@ screen wide, and is not stretched to fill it. A lone window is a third of the screen wide, at the left edge. To have it fill the screen, or to take it out of the layout for a while, use [`komorebic toggle-monocle`](../cli/toggle-monocle.md). +## Full width and centring + +- [`komorebic scrolling-layout-full-width`](../cli/scrolling-layout-full-width.md) makes the focused column as wide as the + screen, or puts it back to the width that it had. +- [`komorebic center-focus`](../cli/center-focus.md) scrolls the screen so that the focused column is in the middle of + it, once. `center_focused_column` does it every time that focus moves instead. +- [`komorebic expand`](../cli/expand.md) makes the focused column as wide as it can be without moving another column out + of view: it takes up the room that the columns on the screen leave. It does nothing when there is none, and in the + other layouts. With `center_focused_column` set to `"Always"` it makes the column the full width of the screen, or + puts it back, as `scrolling-layout-full-width` does. + +A column that you make the full width of the screen takes the gaps into account, so that it fills the screen exactly. +Nothing else moves to make room for a column that gets wider: the focused column keeps its left edge, and the screen +scrolls only if the column no longer fits. + ## Scrolling The row scrolls only when it has to. @@ -95,6 +110,5 @@ These are not done yet. - A window that will not get as narrow as its column, which is what many apps do, is not noticed, so it can overlap the column next to it. -- There are no commands for stepping through preset widths, or for making a column the full width of the screen. - Every window has a border, including the windows resting at the edges, so you may see a thin line of one at the edge of the screen. diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index c34724d4..eea747e9 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -333,6 +333,9 @@ pub enum SocketMessage { ChangeLayout(DefaultLayout), CycleLayout(CycleDirection), ScrollingLayoutColumns(NonZeroUsize), + ScrollingLayoutFullWidth, + CenterFocus, + Expand, LayoutRatios(Option>, Option>), // ChangeLayoutCustom(#[serde_as(as = "ResolvedPathBuf")] PathBuf), FlipLayout(Axis), @@ -484,3 +487,22 @@ impl FromStr for SocketMessage { serde_json::from_str(s) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_scrolling_command_goes_over_the_socket_as_it_is() { + for message in [ + SocketMessage::ScrollingLayoutFullWidth, + SocketMessage::CenterFocus, + SocketMessage::Expand, + ] { + let bytes = message.as_bytes().unwrap(); + let read: SocketMessage = String::from_utf8(bytes.clone()).unwrap().parse().unwrap(); + + assert_eq!(read.as_bytes().unwrap(), bytes); + } + } +} diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 83e78ef0..dc4e0b2a 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1496,6 +1496,18 @@ impl WindowManager { .set_scrolling_columns(count.into()); self.update_focused_workspace(false, false)?; } + SocketMessage::ScrollingLayoutFullWidth => { + self.focused_workspace_mut()?.toggle_scrolling_full_width(); + self.update_focused_workspace(false, false)?; + } + SocketMessage::CenterFocus => { + self.focused_workspace_mut()?.center_scrolling_column(); + self.update_focused_workspace(false, false)?; + } + SocketMessage::Expand => { + self.focused_workspace_mut()?.expand_scrolling_column(); + self.update_focused_workspace(false, false)?; + } SocketMessage::LayoutRatios(ref columns, ref rows) => { use crate::core::validate_ratios; diff --git a/komorebi/src/scrolling.rs b/komorebi/src/scrolling.rs index 5e1cf062..4a96d310 100644 --- a/komorebi/src/scrolling.rs +++ b/komorebi/src/scrolling.rs @@ -192,6 +192,52 @@ impl Workspace { } } + /// Make the space agree with which container has focus, and give back the id of that + /// container, for a command that is about the focused column. + /// + /// There is nothing to do until a layout has made the space, and in a layout that is not + /// Scrolling. + fn focused_scrolling_column(&mut self) -> Option { + if !self.is_scrolling() || self.scrolling.is_none() { + return None; + } + + self.sync_scrolling_containers(); + + self.focused_container() + .map(|container| container.id.clone()) + } + + /// Make the focused column as wide as the screen, or give it back the width that it had. + pub(crate) fn toggle_scrolling_full_width(&mut self) { + if let Some(id) = self.focused_scrolling_column() + && let Some(space) = self.scrolling.as_mut() + { + space.toggle_full_width(&id); + } + } + + /// Make the focused column as wide as it can be without scrolling anything else out of view, + /// which is to fill the room that the columns in view leave. Only the Scrolling layout has + /// columns to expand. + pub(crate) fn expand_scrolling_column(&mut self) { + if self.is_scrolling() + && self.focused_scrolling_column().is_some() + && let Some(space) = self.scrolling.as_mut() + { + space.expand_to_available_width(); + } + } + + /// Centre the focused column on the screen, this once. + pub(crate) fn center_scrolling_column(&mut self) { + if self.focused_scrolling_column().is_some() + && let Some(space) = self.scrolling.as_mut() + { + space.center_column(); + } + } + /// Make the focused column wider or narrower. Only the horizontal edges mean anything, since /// a column is as tall as the viewport. /// @@ -554,6 +600,84 @@ mod tests { assert!(workspace.scrolling.is_none()); } + /// A workspace with a layout, which is what makes the strip that the width commands act on. + fn laid_out(columns: usize, containers: usize, focus: usize) -> Workspace { + let mut workspace = workspace(columns, containers); + workspace.focus_container(focus); + workspace.layout_scrolling(&AREA, 0); + workspace + } + + #[test] + fn the_full_width_is_toggled_on_and_off() { + let mut workspace = laid_out(3, 3, 1); + + workspace.toggle_scrolling_full_width(); + assert_eq!(widths(&workspace.layout_scrolling(&AREA, 0))[1], 1200); + + workspace.toggle_scrolling_full_width(); + assert_eq!(widths(&workspace.layout_scrolling(&AREA, 0))[1], 400); + } + + #[test] + fn a_column_is_centred_once() { + let mut workspace = laid_out(3, 2, 0); + assert_eq!(workspace.layout_scrolling(&AREA, 0)[0].rect.left, AREA.left); + + workspace.center_scrolling_column(); + + // the work area is 1200 wide and starts at 100, and the column is 400 wide + assert_eq!( + workspace.layout_scrolling(&AREA, 0)[0].rect.left, + AREA.left + 400 + ); + + // and it is centred once, so moving on to the next column does not keep it centred + workspace.focus_container(1); + let lefts: Vec<_> = workspace + .layout_scrolling(&AREA, 0) + .iter() + .map(|placement| placement.rect.left) + .collect(); + assert_eq!(lefts, [500, 900]); + } + + #[test] + fn the_commands_are_about_the_container_that_has_focus_now() { + let mut workspace = laid_out(3, 3, 0); + + // focus moved since the layout, and the next layout has not been made yet + workspace.focus_container(2); + workspace.toggle_scrolling_full_width(); + + assert_eq!( + widths(&workspace.layout_scrolling(&AREA, 0)), + [400, 400, 1200] + ); + } + + #[test] + fn the_commands_do_nothing_before_there_is_a_layout() { + let mut workspace = workspace(3, 3); + + workspace.toggle_scrolling_full_width(); + workspace.center_scrolling_column(); + + assert!(workspace.scrolling.is_none()); + } + + #[test] + fn the_commands_do_nothing_in_a_layout_that_is_not_scrolling() { + let mut workspace = laid_out(3, 3, 1); + let before = workspace.scrolling.clone(); + workspace.layout = Layout::Default(DefaultLayout::BSP); + + workspace.toggle_scrolling_full_width(); + workspace.center_scrolling_column(); + + assert_eq!(workspace.scrolling, before); + } + /// A workspace with a window in every container, which is how closing a window finds a container. fn workspace_of_windows(columns: usize, count: u32) -> Workspace { let mut workspace = Workspace { @@ -675,4 +799,29 @@ mod tests { [300, 300, 300] ); } + + #[test] + fn expanding_fills_the_room_that_the_columns_in_view_leave() { + let mut workspace = workspace(3, 2); + workspace.layout_scrolling(&AREA, 0); + workspace.focus_container(1); + let before = workspace.layout_scrolling(&AREA, 0); + + workspace.expand_scrolling_column(); + let after = workspace.layout_scrolling(&AREA, 0); + + // the focused column takes the third of the screen that was empty, and no more + assert_eq!(after[1].rect.right, before[1].rect.right + (AREA.right / 3)); + assert_eq!(after[0], before[0]); + } + + #[test] + fn expanding_does_nothing_where_there_are_no_columns() { + let mut workspace = workspace(3, 2); + workspace.layout = Layout::Default(DefaultLayout::BSP); + + workspace.expand_scrolling_column(); + + assert!(workspace.scrolling.is_none()); + } } diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index ba10d48c..1b693c47 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -986,6 +986,16 @@ enum SubCommand { /// Set how many columns fit across the Scrolling layout on the focused workspace #[clap(arg_required_else_help = true)] ScrollingLayoutColumns(ScrollingLayoutColumns), + /// Make the focused column of the Scrolling layout as wide as the screen, or put it back + ScrollingLayoutFullWidth, + /// Centre the user focus on the screen, once + /// + /// In the Scrolling layout the screen scrolls so that the focused column is in the middle of + /// it. Only the Scrolling layout scrolls, so this does nothing in the other layouts. + CenterFocus, + /// 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, /// Set the layout column and row ratios for the focused workspace LayoutRatios(LayoutRatios), // /// Load a custom layout from file for the focused workspace @@ -2367,6 +2377,15 @@ exit 1 SubCommand::ScrollingLayoutColumns(args) => { send_message(&SocketMessage::ScrollingLayoutColumns(args.count))?; } + SubCommand::ScrollingLayoutFullWidth => { + send_message(&SocketMessage::ScrollingLayoutFullWidth)?; + } + SubCommand::CenterFocus => { + send_message(&SocketMessage::CenterFocus)?; + } + SubCommand::Expand => { + send_message(&SocketMessage::Expand)?; + } SubCommand::LayoutRatios(args) => { if args.columns.is_none() && args.rows.is_none() { println!( diff --git a/mkdocs.yml b/mkdocs.yml index e9f884a9..1934851f 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -145,10 +145,13 @@ nav: - cli/change-layout.md - cli/cycle-layout.md - cli/scrolling-layout-columns.md + - cli/scrolling-layout-full-width.md - cli/flip-layout.md - cli/promote.md - cli/promote-swap.md - cli/promote-focus.md + - cli/center-focus.md + - cli/expand.md - cli/promote-window.md - cli/retile.md - cli/display-index-preference.md