Skip to content

Commit

Permalink
fix(operation_direction): adjust for layout flips
Browse files Browse the repository at this point in the history
If the BSP layout was flipped on the X or Y axis (or both),
OperationDirection commands would not adjust their directions
accordingly and prevent the user from focusing, moving etc in a valid
direction on the flipped layout.

This commit addresses that bug by ensuring that we always try to apply
any axis adjustments to an OperationDirection before calling the
is_valid or new_idx functions.
  • Loading branch information
LGUG2Z committed Aug 6, 2021
1 parent f97cdf7 commit ca27730
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
51 changes: 47 additions & 4 deletions komorebi-core/src/operation_direction.rs
Expand Up @@ -5,6 +5,7 @@ use strum::Display;
use strum::EnumString;

use crate::Layout;
use crate::LayoutFlip;

#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString)]
#[strum(serialize_all = "snake_case")]
Expand All @@ -29,8 +30,50 @@ impl OperationDirection {
}
}

pub fn is_valid(&self, layout: Layout, idx: usize, len: usize) -> bool {
match self {
fn flip_direction(
direction: &OperationDirection,
layout_flip: Option<LayoutFlip>,
) -> OperationDirection {
if let Some(flip) = layout_flip {
match direction {
OperationDirection::Left => match flip {
LayoutFlip::Horizontal | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Right
}
_ => *direction,
},
OperationDirection::Right => match flip {
LayoutFlip::Horizontal | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Left
}
_ => *direction,
},
OperationDirection::Up => match flip {
LayoutFlip::Vertical | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Down
}
_ => *direction,
},
OperationDirection::Down => match flip {
LayoutFlip::Vertical | LayoutFlip::HorizontalAndVertical => {
OperationDirection::Up
}
_ => *direction,
},
}
} else {
*direction
}
}

pub fn is_valid(
&self,
layout: Layout,
layout_flip: Option<LayoutFlip>,
idx: usize,
len: usize,
) -> bool {
match OperationDirection::flip_direction(self, layout_flip) {
OperationDirection::Up => match layout {
Layout::BSP => len > 2 && idx != 0 && idx != 1,
Layout::Columns => false,
Expand All @@ -54,8 +97,8 @@ impl OperationDirection {
}
}

pub fn new_idx(&self, layout: Layout, idx: usize) -> usize {
match self {
pub fn new_idx(&self, layout: Layout, layout_flip: Option<LayoutFlip>, idx: usize) -> usize {
match OperationDirection::flip_direction(self, layout_flip) {
OperationDirection::Up => match layout {
Layout::BSP => {
if idx % 2 == 0 {
Expand Down
1 change: 1 addition & 0 deletions komorebi/src/window_manager.rs
Expand Up @@ -204,6 +204,7 @@ impl WindowManager {

let is_valid = direction.is_valid(
workspace.layout(),
workspace.layout_flip(),
workspace.focused_container_idx(),
workspace.containers_mut().len(),
);
Expand Down
9 changes: 7 additions & 2 deletions komorebi/src/workspace.rs
Expand Up @@ -273,11 +273,16 @@ impl Workspace {

pub fn new_idx_for_direction(&self, direction: OperationDirection) -> Option<usize> {
if direction.is_valid(
self.layout,
self.layout(),
self.layout_flip(),
self.focused_container_idx(),
self.containers().len(),
) {
Option::from(direction.new_idx(self.layout, self.containers.focused_idx()))
Option::from(direction.new_idx(
self.layout(),
self.layout_flip(),
self.containers.focused_idx(),
))
} else {
None
}
Expand Down

0 comments on commit ca27730

Please sign in to comment.