Skip to content

Commit

Permalink
Merge pull request #20 from leftwm/flip
Browse files Browse the repository at this point in the history
fix #19, update changelog, bump version
  • Loading branch information
hertg authored Jun 4, 2023
2 parents 4b017ee + ccbb42a commit c37d0c5
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## :lady_beetle: Fixes

- Fix issue where more rects were returned than windows requested ([#18](https://github.com/leftwm/leftwm-layouts/issues/18))
- Fix issue where the `flip()` method was switched up, now behaves as documented and is properly tested ([#19](https://github.com/leftwm/leftwm-layouts/issues/19))
23 changes: 21 additions & 2 deletions demo-ascii/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ascii_canvas::{
};
use leftwm_layouts::{
geometry::{Flip, Rect, Reserve, Rotation, Split},
layouts::{Columns, Main, Stack},
layouts::{Columns, Main, SecondStack, Stack},
Layout,
};

Expand Down Expand Up @@ -67,7 +67,7 @@ fn draw(layout: &Layout, windows: usize, w: usize, h: usize) -> String {
}

fn demo_layout() -> Layout {
leftwm_layouts::Layout {
/*leftwm_layouts::Layout {
name: "Demo".to_string(),
flip: Flip::None,
rotate: Rotation::North,
Expand All @@ -85,5 +85,24 @@ fn demo_layout() -> Layout {
},
second_stack: None,
},
}*/
leftwm_layouts::Layout {
name: "Demo".to_string(),
flip: Flip::None,
rotate: Rotation::North,
reserve: Reserve::Reserve,
columns: Columns {
flip: Flip::Vertical,
rotate: Rotation::North,
main: Some(Main {
..Default::default()
}),
stack: Stack {
..Default::default()
},
second_stack: Some(SecondStack {
..Default::default()
}),
},
}
}
2 changes: 1 addition & 1 deletion leftwm-layouts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leftwm-layouts"
version = "0.8.2"
version = "0.8.3"
edition = "2021"

license = "BSD-3-Clause"
Expand Down
160 changes: 152 additions & 8 deletions leftwm-layouts/src/geometry/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,23 @@ pub fn remainderless_division(a: usize, b: usize) -> Vec<usize> {

/// Flip an array of [`Rect`] inside the container, according to the provided `flip` parameter
pub fn flip(rects: &mut [Rect], flip: Flip, container: &Rect) {
if flip == Flip::None {
return;
}

for rect in rects.iter_mut() {
if flip.is_flipped_horizontal() {
// from left edge as far away as right side is from right edge before being flipped
let right_window_edge = rect.x + rect.w as i32;
let right_container_edge = container.x + container.w as i32;
rect.x = right_container_edge - right_window_edge;
}
if flip.is_flipped_vertical() {
// from top edge as far away as bottom side was from bottom edge before being flipped
let bottom_window_edge = rect.y + rect.h as i32;
let bottom_container_edge = container.y + container.h as i32;
rect.y = bottom_container_edge - bottom_window_edge;
}
if flip.is_flipped_vertical() {
// from left edge as far away as right side is from right edge before being flipped
let right_window_edge = rect.x + rect.w as i32;
let right_container_edge = container.x + container.w as i32;
rect.x = right_container_edge - right_window_edge;
}
}
}

Expand All @@ -66,6 +70,10 @@ pub fn flip(rects: &mut [Rect], flip: Flip, container: &Rect) {
/// have gaps either. Similarly, if the array has no overlaps (i.e. pixels that are part of multiple [`Rect`]s
/// in the array), neither will the result.
pub fn rotate(rects: &mut [Rect], rotation: Rotation, container: &Rect) {
if rotation == Rotation::North {
return;
}

for rect in rects.iter_mut() {
rotate_single_rect(rect, rotation, container);
}
Expand Down Expand Up @@ -188,8 +196,8 @@ pub fn split(rect: &Rect, amount: usize, axis: Option<Split>) -> Vec<Rect> {
#[cfg(test)]
mod tests {
use crate::{
geometry::calc::{divrem, remainderless_division, split},
geometry::{Rect, Rotation, Split},
geometry::calc::{divrem, flip, remainderless_division, split},
geometry::{Flip, Rect, Rotation, Split},
};

use super::rotate;
Expand Down Expand Up @@ -244,6 +252,142 @@ mod tests {
assert!(rects[0].eq(&CONTAINER));
}

#[test]
fn flip_none() {
let container = Rect::new(0, 0, 400, 200);

// +---------------+
// | |
// +-------+-------+ 0°
// +-------+ |
// +-------+-------+
let mut rects = vec![
Rect::new(0, 0, 400, 100),
Rect::new(200, 100, 200, 100),
Rect::new(0, 150, 200, 50),
Rect::new(0, 100, 200, 50),
];

flip(&mut rects, Flip::None, &container);

// +---------------+
// | |
// +-------+-------+ 0°
// +-------+ |
// +-------+-------+
assert_eq!(
rects,
vec![
Rect::new(0, 0, 400, 100),
Rect::new(200, 100, 200, 100),
Rect::new(0, 150, 200, 50),
Rect::new(0, 100, 200, 50),
]
);
}

#[test]
fn flip_horizontal() {
let container = Rect::new(0, 0, 400, 200);

// +---------------+
// | |
// +-------+-------+
// +-------+ |
// +-------+-------+
let mut rects = vec![
Rect::new(0, 0, 400, 100),
Rect::new(200, 100, 200, 100),
Rect::new(0, 150, 200, 50),
Rect::new(0, 100, 200, 50),
];

flip(&mut rects, Flip::Horizontal, &container);

// +-------+-------+
// +-------+ |
// +-------+-------+
// | |
// +-------+-------+
assert_eq!(
rects,
vec![
Rect::new(0, 100, 400, 100),
Rect::new(200, 0, 200, 100),
Rect::new(0, 0, 200, 50),
Rect::new(0, 50, 200, 50),
]
);
}

#[test]
fn flip_vertical() {
let container = Rect::new(0, 0, 400, 200);

// +---------------+
// | |
// +-------+-------+
// +-------+ |
// +-------+-------+
let mut rects = vec![
Rect::new(0, 0, 400, 100),
Rect::new(200, 100, 200, 100),
Rect::new(0, 150, 200, 50),
Rect::new(0, 100, 200, 50),
];

flip(&mut rects, Flip::Vertical, &container);

// +---------------+
// | |
// +-------+-------+
// | +-------+
// +-------+-------+
assert_eq!(
rects,
vec![
Rect::new(0, 0, 400, 100),
Rect::new(0, 100, 200, 100),
Rect::new(200, 150, 200, 50),
Rect::new(200, 100, 200, 50),
]
);
}

#[test]
fn flip_both() {
let container = Rect::new(0, 0, 400, 200);

// +---------------+
// | |
// +-------+-------+ 0°
// +-------+ |
// +-------+-------+
let mut rects = vec![
Rect::new(0, 0, 400, 100),
Rect::new(200, 100, 200, 100),
Rect::new(0, 150, 200, 50),
Rect::new(0, 100, 200, 50),
];

flip(&mut rects, Flip::Both, &container);

// +-------+-------+
// | +-------+
// +-------+-------+ 0°
// | |
// +---------------+
assert_eq!(
rects,
vec![
Rect::new(0, 100, 400, 100),
Rect::new(0, 0, 200, 100),
Rect::new(200, 0, 200, 50),
Rect::new(200, 50, 200, 50),
]
);
}

#[test]
fn rotate_0_degrees() {
let container = Rect::new(0, 0, 400, 200);
Expand Down
23 changes: 11 additions & 12 deletions leftwm-layouts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn stack_main_stack(
(1, cmp::max(0, stack_window_count.saturating_sub(1)))
};

let (left_column, main_column, right_column) = three_column(
let (mut left_column, mut main_column, mut right_column) = three_column(
window_count,
container,
main_window_count,
Expand All @@ -132,21 +132,20 @@ fn stack_main_stack(
balance_stacks,
);

// prepare columns to rotate / flip
let mut columns = vec![];
if let Some(col) = left_column {
columns.push(col);
}
if let Some(col) = main_column {
columns.push(col);
}
if let Some(col) = right_column {
columns.push(col);
}

// root rotation
columns.push(left_column.unwrap_or(Rect::new(0, 0, 0, 0)));
columns.push(main_column.unwrap_or(Rect::new(0, 0, 0, 0)));
columns.push(right_column.unwrap_or(Rect::new(0, 0, 0, 0)));
geometry::rotate(&mut columns, definition.columns.rotate, container);
geometry::flip(&mut columns, definition.columns.flip, container);

// copy rotated/flipped columns into the variables
let non_empty = |rect: &&Rect| rect.surface_area() > 0;
left_column = columns.get(0).filter(non_empty).copied();
main_column = columns.get(1).filter(non_empty).copied();
right_column = columns.get(2).filter(non_empty).copied();

let mut main_tiles = vec![];
if let Some(tile) = main_column {
main_tiles.append(&mut geometry::split(&tile, main_window_count, main.split));
Expand Down

0 comments on commit c37d0c5

Please sign in to comment.