feat(wm): migrate workspace and layout code

This commit migrates big chunks of code from komorebi for Windows which
deal with state related to the Workspace struct, Layouts and the
Arrangement trait.

Layout remains an enum, but only the Layout::Default variant with
DefaultLayout has been migrated because there are no plans to support
EOL features in komorebi for Mac (custom layouts).

The code for updating the layout in main.rs has been moved to
Workspace.update, which is now called from main.rs instead.

As part of this initial migration of code, this project has been
restructured a little to match the layout of komorebi for Windows to
minimize my own confusion.
This commit is contained in:
LGUG2Z committed 2025-09-17 13:31:39 -07:00
1 parent 326c419666
commit b6d19e6a12
16 files changed
+2043 -216

No files matched your search

+5 -1
View File
@@ -31,4 +31,8 @@ trace target $RUST_LOG="trace":
depgen:
cargo deny check
cargo deny list --format json | jq 'del(.unlicensed)' > dependencies.json
cargo deny list --format json | jq 'del(.unlicensed)' > dependencies.json
push:
git push origin master
git push komocorp master
File diff suppressed because it is too large. Load diff
+200
View File
@@ -0,0 +1,200 @@
use crate::core::Sizing;
use crate::core::operation_direction::OperationDirection;
use crate::core::rect::Rect;
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub enum DefaultLayout {
BSP,
Columns,
Rows,
VerticalStack,
HorizontalStack,
UltrawideVerticalStack,
Grid,
RightMainVerticalStack,
Scrolling,
// NOTE: If any new layout is added, please make sure to register the same in `DefaultLayout::cycle`
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct LayoutOptions {
/// Options related to the Scrolling layout
pub scrolling: Option<ScrollingLayoutOptions>,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct ScrollingLayoutOptions {
/// Desired number of visible columns (default: 3)
pub columns: usize,
}
impl DefaultLayout {
pub fn leftmost_index(&self, len: usize) -> usize {
match self {
Self::UltrawideVerticalStack | Self::RightMainVerticalStack => match len {
n if n > 1 => 1,
_ => 0,
},
Self::Scrolling => 0,
DefaultLayout::BSP
| DefaultLayout::Columns
| DefaultLayout::Rows
| DefaultLayout::VerticalStack
| DefaultLayout::HorizontalStack
| DefaultLayout::Grid => 0,
}
}
pub fn rightmost_index(&self, len: usize) -> usize {
match self {
DefaultLayout::BSP
| DefaultLayout::Columns
| DefaultLayout::Rows
| DefaultLayout::VerticalStack
| DefaultLayout::HorizontalStack
| DefaultLayout::Grid => len.saturating_sub(1),
DefaultLayout::UltrawideVerticalStack => match len {
2 => 0,
_ => len.saturating_sub(1),
},
DefaultLayout::RightMainVerticalStack => 0,
DefaultLayout::Scrolling => len.saturating_sub(1),
}
}
#[must_use]
#[allow(clippy::cast_precision_loss, clippy::only_used_in_recursion)]
pub fn resize(
&self,
unaltered: &Rect,
resize: &Option<Rect>,
edge: OperationDirection,
sizing: Sizing,
delta: i32,
) -> Option<Rect> {
if !matches!(
self,
Self::BSP
| Self::Columns
| Self::Rows
| Self::VerticalStack
| Self::RightMainVerticalStack
| Self::HorizontalStack
| Self::UltrawideVerticalStack
| Self::Scrolling
) {
return None;
};
let mut r = resize.unwrap_or_default();
let resize_delta = delta;
match edge {
OperationDirection::Left => match sizing {
Sizing::Increase => {
// Some final checks to make sure the user can't infinitely resize to
// the point of pushing other windows out of bounds
// Note: These checks cannot take into account the changes made to the
// edges of adjacent windows at operation time, so it is still possible
// to push windows out of bounds by maxing out an Increase Left on a
// Window with index 1, and then maxing out a Decrease Right on a Window
// with index 0. I don't think it's worth trying to defensively program
// against this; if people end up in this situation they are better off
// just hitting the retile command
let diff = ((r.left + -resize_delta) as f32).abs();
if diff < unaltered.right as f32 {
r.left += -resize_delta;
}
}
Sizing::Decrease => {
let diff = ((r.left - -resize_delta) as f32).abs();
if diff < unaltered.right as f32 {
r.left -= -resize_delta;
}
}
},
OperationDirection::Up => match sizing {
Sizing::Increase => {
let diff = ((r.top + resize_delta) as f32).abs();
if diff < unaltered.bottom as f32 {
r.top += -resize_delta;
}
}
Sizing::Decrease => {
let diff = ((r.top - resize_delta) as f32).abs();
if diff < unaltered.bottom as f32 {
r.top -= -resize_delta;
}
}
},
OperationDirection::Right => match sizing {
Sizing::Increase => {
let diff = ((r.right + resize_delta) as f32).abs();
if diff < unaltered.right as f32 {
r.right += resize_delta;
}
}
Sizing::Decrease => {
let diff = ((r.right - resize_delta) as f32).abs();
if diff < unaltered.right as f32 {
r.right -= resize_delta;
}
}
},
OperationDirection::Down => match sizing {
Sizing::Increase => {
let diff = ((r.bottom + resize_delta) as f32).abs();
if diff < unaltered.bottom as f32 {
r.bottom += resize_delta;
}
}
Sizing::Decrease => {
let diff = ((r.bottom - resize_delta) as f32).abs();
if diff < unaltered.bottom as f32 {
r.bottom -= resize_delta;
}
}
},
};
if r.eq(&Rect::default()) {
None
} else {
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::Grid,
Self::Grid => Self::RightMainVerticalStack,
Self::RightMainVerticalStack => Self::Scrolling,
Self::Scrolling => Self::BSP,
}
}
#[must_use]
pub const fn cycle_previous(self) -> Self {
match self {
Self::Scrolling => Self::RightMainVerticalStack,
Self::RightMainVerticalStack => Self::Grid,
Self::Grid => Self::UltrawideVerticalStack,
Self::UltrawideVerticalStack => Self::HorizontalStack,
Self::HorizontalStack => Self::VerticalStack,
Self::VerticalStack => Self::Rows,
Self::Rows => Self::Columns,
Self::Columns => Self::BSP,
Self::BSP => Self::RightMainVerticalStack,
}
}
}
+347
View File
@@ -0,0 +1,347 @@
use crate::core::default_layout::DefaultLayout;
use crate::core::operation_direction::OperationDirection;
pub trait Direction {
fn index_in_direction(
&self,
op_direction: OperationDirection,
idx: usize,
count: usize,
) -> Option<usize>;
fn is_valid_direction(
&self,
op_direction: OperationDirection,
idx: usize,
count: usize,
) -> bool;
fn up_index(
&self,
op_direction: Option<OperationDirection>,
idx: usize,
count: Option<usize>,
) -> usize;
fn down_index(
&self,
op_direction: Option<OperationDirection>,
idx: usize,
count: Option<usize>,
) -> usize;
fn left_index(
&self,
op_direction: Option<OperationDirection>,
idx: usize,
count: Option<usize>,
) -> usize;
fn right_index(
&self,
op_direction: Option<OperationDirection>,
idx: usize,
count: Option<usize>,
) -> usize;
}
impl Direction for DefaultLayout {
fn index_in_direction(
&self,
op_direction: OperationDirection,
idx: usize,
count: usize,
) -> Option<usize> {
match op_direction {
OperationDirection::Left => {
if self.is_valid_direction(op_direction, idx, count) {
Option::from(self.left_index(Some(op_direction), idx, Some(count)))
} else {
None
}
}
OperationDirection::Right => {
if self.is_valid_direction(op_direction, idx, count) {
Option::from(self.right_index(Some(op_direction), idx, Some(count)))
} else {
None
}
}
OperationDirection::Up => {
if self.is_valid_direction(op_direction, idx, count) {
Option::from(self.up_index(Some(op_direction), idx, Some(count)))
} else {
None
}
}
OperationDirection::Down => {
if self.is_valid_direction(op_direction, idx, count) {
Option::from(self.down_index(Some(op_direction), idx, Some(count)))
} else {
None
}
}
}
}
fn is_valid_direction(
&self,
op_direction: OperationDirection,
idx: usize,
count: usize,
) -> bool {
if count < 2 {
return false;
}
match op_direction {
OperationDirection::Up => match self {
Self::BSP => idx != 0 && idx != 1,
Self::Columns => false,
Self::Rows | Self::HorizontalStack => idx != 0,
Self::VerticalStack | Self::RightMainVerticalStack => idx != 0 && idx != 1,
Self::UltrawideVerticalStack => idx > 2,
Self::Grid => !is_grid_edge(op_direction, idx, count),
Self::Scrolling => false,
},
OperationDirection::Down => match self {
Self::BSP => idx != count - 1 && idx % 2 != 0,
Self::Columns => false,
Self::Rows => idx != count - 1,
Self::VerticalStack | Self::RightMainVerticalStack => idx != 0 && idx != count - 1,
Self::HorizontalStack => idx == 0,
Self::UltrawideVerticalStack => idx > 1 && idx != count - 1,
Self::Grid => !is_grid_edge(op_direction, idx, count),
Self::Scrolling => false,
},
OperationDirection::Left => match self {
Self::BSP => idx != 0,
Self::Columns | Self::VerticalStack => idx != 0,
Self::RightMainVerticalStack => idx == 0,
Self::Rows => false,
Self::HorizontalStack => idx != 0 && idx != 1,
Self::UltrawideVerticalStack => idx != 1,
Self::Grid => !is_grid_edge(op_direction, idx, count),
Self::Scrolling => idx != 0,
},
OperationDirection::Right => match self {
Self::BSP => idx % 2 == 0 && idx != count - 1,
Self::Columns => idx != count - 1,
Self::Rows => false,
Self::VerticalStack => idx == 0,
Self::RightMainVerticalStack => idx != 0,
Self::HorizontalStack => idx != 0 && idx != count - 1,
Self::UltrawideVerticalStack => match count {
2 => idx != 0,
_ => idx < 2,
},
Self::Grid => !is_grid_edge(op_direction, idx, count),
Self::Scrolling => idx != count - 1,
},
}
}
fn up_index(
&self,
op_direction: Option<OperationDirection>,
idx: usize,
count: Option<usize>,
) -> usize {
match self {
Self::BSP => {
if idx % 2 == 0 {
idx - 1
} else {
idx - 2
}
}
Self::Columns => unreachable!(),
Self::Rows
| Self::VerticalStack
| Self::UltrawideVerticalStack
| Self::RightMainVerticalStack => idx - 1,
Self::HorizontalStack => 0,
Self::Grid => grid_neighbor(op_direction, idx, count),
Self::Scrolling => unreachable!(),
}
}
fn down_index(
&self,
op_direction: Option<OperationDirection>,
idx: usize,
count: Option<usize>,
) -> usize {
match self {
Self::BSP
| Self::Rows
| Self::VerticalStack
| Self::UltrawideVerticalStack
| Self::RightMainVerticalStack => idx + 1,
Self::Columns => unreachable!(),
Self::HorizontalStack => 1,
Self::Grid => grid_neighbor(op_direction, idx, count),
Self::Scrolling => unreachable!(),
}
}
fn left_index(
&self,
op_direction: Option<OperationDirection>,
idx: usize,
count: Option<usize>,
) -> usize {
match self {
Self::BSP => {
if idx % 2 == 0 {
idx - 2
} else {
idx - 1
}
}
Self::Columns | Self::HorizontalStack => idx - 1,
Self::Rows => unreachable!(),
Self::VerticalStack => 0,
Self::RightMainVerticalStack => 1,
Self::UltrawideVerticalStack => match idx {
0 => 1,
1 => unreachable!(),
_ => 0,
},
Self::Grid => grid_neighbor(op_direction, idx, count),
Self::Scrolling => idx - 1,
}
}
fn right_index(
&self,
op_direction: Option<OperationDirection>,
idx: usize,
count: Option<usize>,
) -> usize {
match self {
Self::BSP | Self::Columns | Self::HorizontalStack => idx + 1,
Self::Rows => unreachable!(),
Self::VerticalStack => 1,
Self::RightMainVerticalStack => 0,
Self::UltrawideVerticalStack => match idx {
1 => 0,
0 => 2,
_ => unreachable!(),
},
Self::Grid => grid_neighbor(op_direction, idx, count),
Self::Scrolling => idx + 1,
}
}
}
struct GridItem {
state: GridItemState,
row: usize,
num_rows: usize,
touching_edges: GridTouchingEdges,
}
enum GridItemState {
Valid,
Invalid,
}
#[allow(clippy::struct_excessive_bools)]
struct GridTouchingEdges {
left: bool,
right: bool,
up: bool,
down: bool,
}
#[allow(
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::cast_sign_loss
)]
fn get_grid_item(idx: usize, count: usize) -> GridItem {
let num_cols = (count as f32).sqrt().ceil() as usize;
let mut iter = 0;
for col in 0..num_cols {
let remaining_windows = count - iter;
let remaining_columns = num_cols - col;
let num_rows_in_this_col = remaining_windows / remaining_columns;
for row in 0..num_rows_in_this_col {
if iter == idx {
return GridItem {
state: GridItemState::Valid,
row: row + 1,
num_rows: num_rows_in_this_col,
touching_edges: GridTouchingEdges {
left: col == 0,
right: col == num_cols - 1,
up: row == 0,
down: row == num_rows_in_this_col - 1,
},
};
}
iter += 1;
}
}
GridItem {
state: GridItemState::Invalid,
row: 0,
num_rows: 0,
touching_edges: GridTouchingEdges {
left: true,
right: true,
up: true,
down: true,
},
}
}
fn is_grid_edge(op_direction: OperationDirection, idx: usize, count: usize) -> bool {
let item = get_grid_item(idx, count);
match item.state {
GridItemState::Invalid => false,
GridItemState::Valid => match op_direction {
OperationDirection::Left => item.touching_edges.left,
OperationDirection::Right => item.touching_edges.right,
OperationDirection::Up => item.touching_edges.up,
OperationDirection::Down => item.touching_edges.down,
},
}
}
fn grid_neighbor(
op_direction: Option<OperationDirection>,
idx: usize,
count: Option<usize>,
) -> usize {
let Some(op_direction) = op_direction else {
return 0;
};
let Some(count) = count else {
return 0;
};
let item = get_grid_item(idx, count);
match op_direction {
OperationDirection::Left => {
let item_from_prev_col = get_grid_item(idx - item.row, count);
if item.touching_edges.up && item.num_rows != item_from_prev_col.num_rows {
return idx - (item.num_rows - 1);
}
if item.num_rows != item_from_prev_col.num_rows && !item.touching_edges.down {
return idx - (item.num_rows - 1);
}
idx - item.num_rows
}
OperationDirection::Right => idx + item.num_rows,
OperationDirection::Up => idx - 1,
OperationDirection::Down => idx + 1,
}
}
+26
View File
@@ -0,0 +1,26 @@
use crate::core::arrangement::Arrangement;
use crate::core::default_layout::DefaultLayout;
use crate::core::direction::Direction;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Layout {
Default(DefaultLayout),
}
impl Layout {
#[must_use]
pub fn as_boxed_direction(&self) -> Box<dyn Direction> {
match self {
Layout::Default(layout) => Box::new(*layout),
}
}
#[must_use]
pub fn as_boxed_arrangement(&self) -> Box<dyn Arrangement> {
match self {
Layout::Default(layout) => Box::new(*layout),
}
}
}
+15
View File
@@ -0,0 +1,15 @@
use serde::Deserialize;
use serde::Serialize;
pub mod arrangement;
pub mod default_layout;
pub mod direction;
pub mod layout;
pub mod operation_direction;
pub mod rect;
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Sizing {
Increase,
Decrease,
}
+58
View File
@@ -0,0 +1,58 @@
use std::num::NonZeroUsize;
use crate::core::arrangement::Axis;
use crate::core::direction::Direction;
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum OperationDirection {
Left,
Right,
Up,
Down,
}
impl OperationDirection {
#[must_use]
pub const fn opposite(self) -> Self {
match self {
Self::Left => Self::Right,
Self::Right => Self::Left,
Self::Up => Self::Down,
Self::Down => Self::Up,
}
}
fn flip(self, layout_flip: Option<Axis>) -> Self {
layout_flip.map_or(self, |flip| match self {
Self::Left => match flip {
Axis::Horizontal | Axis::HorizontalAndVertical => Self::Right,
Axis::Vertical => self,
},
Self::Right => match flip {
Axis::Horizontal | Axis::HorizontalAndVertical => Self::Left,
Axis::Vertical => self,
},
Self::Up => match flip {
Axis::Vertical | Axis::HorizontalAndVertical => Self::Down,
Axis::Horizontal => self,
},
Self::Down => match flip {
Axis::Vertical | Axis::HorizontalAndVertical => Self::Up,
Axis::Horizontal => self,
},
})
}
#[must_use]
pub fn destination(
self,
layout: &dyn Direction,
layout_flip: Option<Axis>,
idx: usize,
len: NonZeroUsize,
) -> Option<usize> {
layout.index_in_direction(self.flip(layout_flip), idx, len.get())
}
}
+105
View File
@@ -0,0 +1,105 @@
use crate::core_graphics::CoreGraphicsApi;
use crate::window::WindowBounds;
use objc2_core_foundation::CGFloat;
use objc2_core_foundation::CGPoint;
use objc2_core_foundation::CGRect;
use objc2_core_foundation::CGSize;
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct Rect {
pub left: i32,
pub top: i32,
pub right: i32,
pub bottom: i32,
}
impl From<CGSize> for Rect {
fn from(value: CGSize) -> Self {
Self {
left: 0,
top: 0,
right: value.width as i32,
bottom: value.height as i32,
}
}
}
impl From<CGRect> for Rect {
fn from(value: CGRect) -> Self {
Self {
left: value.origin.x as i32,
top: value.origin.y as i32,
right: value.size.width as i32,
bottom: value.size.height as i32,
}
}
}
impl From<&Rect> for CGRect {
fn from(value: &Rect) -> Self {
Self {
origin: CGPoint {
x: value.left as CGFloat,
y: value.top as CGFloat,
},
size: CGSize {
width: value.right as CGFloat,
height: value.bottom as CGFloat,
},
}
}
}
impl From<WindowBounds> for Rect {
fn from(value: WindowBounds) -> Self {
Self {
left: value.x as i32,
top: value.y as i32,
right: value.width as i32,
bottom: value.height as i32,
}
}
}
impl Rect {
pub fn contains(&self, other: &Rect) -> bool {
CoreGraphicsApi::contains_rect(other.into(), self.into())
}
/// Decrease the size of self by the padding amount.
pub fn add_padding<T>(&mut self, padding: T)
where
T: Into<Option<i32>>,
{
if let Some(padding) = padding.into() {
self.left += padding;
self.top += padding;
self.right -= padding * 2;
self.bottom -= padding * 2;
}
}
/// Increase the size of self by the margin amount.
pub fn add_margin(&mut self, margin: i32) {
self.left -= margin;
self.top -= margin;
self.right += margin * 2;
self.bottom += margin * 2;
}
pub fn left_padding(&mut self, padding: i32) {
self.left += padding;
}
pub fn right_padding(&mut self, padding: i32) {
self.right -= padding;
}
#[must_use]
pub const fn contains_point(&self, point: (i32, i32)) -> bool {
point.0 >= self.left
&& point.0 <= self.left + self.right
&& point.1 >= self.top
&& point.1 <= self.top + self.bottom
}
}
-82
View File
@@ -1,82 +0,0 @@
use crate::rect::Rect;
pub fn recursive_fibonacci(
idx: usize,
count: usize,
area: &Rect,
resize_adjustments: Vec<Option<Rect>>,
) -> Vec<Rect> {
let mut a = *area;
let resized = if let Some(Some(r)) = resize_adjustments.get(idx) {
a.left += r.left;
a.top += r.top;
a.right += r.right;
a.bottom += r.bottom;
a
} else {
*area
};
// let half_width = area.right / 2.0;
// let half_height = area.bottom / 2.0;
let half_resized_width = resized.right / 2.0;
let half_resized_height = resized.bottom / 2.0;
let (main_x, alt_x, alt_y, main_y);
main_x = resized.left;
alt_x = resized.left + half_resized_width;
main_y = resized.top;
alt_y = resized.top + half_resized_height;
#[allow(clippy::if_not_else)]
if count == 0 {
vec![]
} else if count == 1 {
vec![Rect {
left: resized.left,
top: resized.top,
right: resized.right,
bottom: resized.bottom,
}]
} else if idx % 2 != 0 {
let mut res = vec![Rect {
left: resized.left,
top: main_y,
right: resized.right,
bottom: half_resized_height,
}];
res.append(&mut recursive_fibonacci(
idx + 1,
count - 1,
&Rect {
left: area.left,
top: alt_y,
right: area.right,
bottom: area.bottom - half_resized_height,
},
resize_adjustments,
));
res
} else {
let mut res = vec![Rect {
left: main_x,
top: resized.top,
right: half_resized_width,
bottom: resized.bottom,
}];
res.append(&mut recursive_fibonacci(
idx + 1,
count - 1,
&Rect {
left: alt_x,
top: area.top,
right: area.right - half_resized_width,
bottom: area.bottom,
},
resize_adjustments,
));
res
}
}
+2 -2
View File
@@ -12,12 +12,12 @@ pub mod ring;
pub mod accessibility;
pub mod application;
pub mod core;
pub mod core_graphics;
pub mod layout;
pub mod macos_api;
pub mod rect;
pub mod window;
pub mod window_manager;
pub mod workspace;
pub fn cf_array_as<T>(array: &CFArray) -> impl Iterator<Item = NonNull<T>> + use<'_, T> {
let count = CFArray::count(array);
+1 -1
View File
@@ -1,8 +1,8 @@
use crate::LibraryError;
use crate::application::Application;
use crate::cf_array_as;
use crate::core::rect::Rect;
use crate::core_graphics::CoreGraphicsApi;
use crate::rect::Rect;
use crate::ring::Ring;
use crate::window::WindowInfo;
use crate::window_manager::Container;
+6 -53
View File
@@ -2,8 +2,6 @@
use color_eyre::eyre;
use color_eyre::eyre::eyre;
use komorebi::layout;
use komorebi::rect::Rect;
use komorebi::window_manager::WindowManager;
use objc2::rc::autoreleasepool;
use objc2_application_services::AXIsProcessTrusted;
@@ -99,57 +97,12 @@ fn main() -> eyre::Result<()> {
let mut wm = WindowManager::new(&run_loop);
wm.init()?;
let containers = wm
.monitors
.focused()
.expect("must have a monitor at this point")
.focused_workspace()
.expect("must have a workspace at this point")
.containers();
let layouts =
layout::recursive_fibonacci(0, containers.len(), &Rect::from(display_size), vec![]);
containers
.iter()
.zip(layouts)
.for_each(|(container, layout)| {
let window = container
.focused_window()
.expect("must have a window at this point");
if let Err(error) = window.set_position(&layout) {
tracing::error!(
"failed to position window: {} ({error})",
window
.title()
.unwrap_or_else(|| String::from("<NO TITLE FOUND>"))
);
}
// initial example to ensure that window focusing works
// if let Some(window_title) = window.title()
// && window_title.contains("Komorebi Community Server")
// {
// match window.focus() {
// Err(error) => {
// tracing::error!(
// "failed to focus window: {} ({error})",
// window
// .title()
// .unwrap_or_else(|| String::from("<NO TITLE FOUND>"))
// );
// }
// Ok(()) => {
// tracing::info!(
// "focused window: {}",
// window
// .title()
// .unwrap_or_else(|| String::from("<NO TITLE FOUND>"))
// );
// }
// }
// }
});
wm.monitors
.focused_mut()
.unwrap()
.focused_workspace_mut()
.unwrap()
.update()?;
let quit_ctrlc = Arc::new(AtomicBool::new(false));
let quit_thread = quit_ctrlc.clone();
-68
View File
@@ -1,68 +0,0 @@
use crate::core_graphics::CoreGraphicsApi;
use crate::window::WindowBounds;
use objc2_core_foundation::CGFloat;
use objc2_core_foundation::CGPoint;
use objc2_core_foundation::CGRect;
use objc2_core_foundation::CGSize;
#[derive(Debug, Copy, Clone)]
pub struct Rect {
pub left: f32,
pub top: f32,
pub right: f32,
pub bottom: f32,
}
impl From<CGSize> for Rect {
fn from(value: CGSize) -> Self {
Self {
left: 0.0,
top: 0.0,
right: value.width as f32,
bottom: value.height as f32,
}
}
}
impl From<CGRect> for Rect {
fn from(value: CGRect) -> Self {
Self {
left: value.origin.x as f32,
top: value.origin.y as f32,
right: value.size.width as f32,
bottom: value.size.height as f32,
}
}
}
impl From<&Rect> for CGRect {
fn from(value: &Rect) -> Self {
Self {
origin: CGPoint {
x: value.left as CGFloat,
y: value.top as CGFloat,
},
size: CGSize {
width: value.right as CGFloat,
height: value.bottom as CGFloat,
},
}
}
}
impl From<WindowBounds> for Rect {
fn from(value: WindowBounds) -> Self {
Self {
left: value.x,
top: value.y,
right: value.width,
bottom: value.height,
}
}
}
impl Rect {
pub fn contains(&self, other: &Rect) -> bool {
CoreGraphicsApi::contains_rect(other.into(), self.into())
}
}
+1 -1
View File
@@ -12,7 +12,7 @@ use crate::accessibility::notification_constants::kAXWindowMovedNotification;
use crate::accessibility::notification_constants::kAXWindowResizedNotification;
use crate::application::Application;
use crate::cf_dictionary_value;
use crate::rect::Rect;
use crate::core::rect::Rect;
use objc2_app_kit::NSApplicationActivationOptions;
use objc2_app_kit::NSRunningApplication;
use objc2_application_services::AXObserver;
+15 -8
View File
@@ -1,9 +1,10 @@
use crate::LibraryError;
use crate::application::Application;
use crate::core::rect::Rect;
use crate::macos_api::MacosApi;
use crate::rect::Rect;
use crate::ring::Ring;
use crate::window::Window;
use crate::workspace::Workspace;
use objc2_core_foundation::CFRetained;
use objc2_core_foundation::CFRunLoop;
use std::collections::HashMap;
@@ -41,9 +42,21 @@ pub struct Monitor {
}
impl Monitor {
#[allow(clippy::field_reassign_with_default)]
pub fn new(id: u32, size: Rect) -> Self {
let mut workspaces = Ring::default();
workspaces.elements_mut().push_back(Workspace::default());
let mut workspace = Workspace::default();
workspace.work_area = size;
workspace.container_padding = Some(20);
workspace.workspace_padding = Some(20);
workspace.work_area_offset = Some(Rect {
left: 0,
top: 0,
right: 0,
bottom: 40,
});
workspaces.elements_mut().push_back(workspace);
Self {
id,
@@ -53,12 +66,6 @@ impl Monitor {
}
}
impl_ring_elements!(Workspace, Container);
#[derive(Debug, Default)]
pub struct Workspace {
pub containers: Ring<Container>,
}
impl_ring_elements!(Container, Window);
#[derive(Debug)]
pub struct Container {
+133
View File
@@ -0,0 +1,133 @@
use crate::core::arrangement::Axis;
use crate::core::default_layout::DefaultLayout;
use crate::core::default_layout::LayoutOptions;
use crate::core::layout::Layout;
use crate::core::rect::Rect;
use crate::ring::Ring;
use crate::window_manager::Container;
use color_eyre::eyre;
use color_eyre::eyre::eyre;
use std::num::NonZeroUsize;
impl_ring_elements!(Workspace, Container);
#[derive(Debug)]
pub struct Workspace {
pub containers: Ring<Container>,
pub workspace_padding: Option<i32>,
pub container_padding: Option<i32>,
pub resize_dimensions: Vec<Option<Rect>>,
pub layout: Layout,
pub work_area: Rect,
pub work_area_offset: Option<Rect>,
pub latest_layout: Vec<Rect>,
pub layout_flip: Option<Axis>,
pub layout_options: Option<LayoutOptions>,
pub globals: WorkspaceGlobals,
pub tile: bool,
}
impl Default for Workspace {
fn default() -> Self {
Self {
containers: Default::default(),
workspace_padding: None,
container_padding: None,
resize_dimensions: vec![],
layout: Layout::Default(DefaultLayout::UltrawideVerticalStack),
work_area: Default::default(),
work_area_offset: None,
latest_layout: vec![],
layout_flip: None,
layout_options: None,
globals: Default::default(),
tile: true,
}
}
}
#[derive(Debug, Default, Copy, Clone, PartialEq)]
/// Settings setup either by the parent monitor or by the `WindowManager`
pub struct WorkspaceGlobals {
pub container_padding: Option<i32>,
pub workspace_padding: Option<i32>,
pub border_width: i32,
pub border_offset: i32,
pub work_area: Rect,
pub work_area_offset: Option<Rect>,
pub window_based_work_area_offset: Option<Rect>,
pub window_based_work_area_offset_limit: isize,
}
impl Workspace {
pub fn update(&mut self) -> eyre::Result<()> {
// make sure we are never holding on to empty containers
self.containers_mut().retain(|c| !c.windows().is_empty());
let container_padding = self
.container_padding
.or(self.globals.container_padding)
.unwrap_or_default();
let workspace_padding = self
.workspace_padding
.or(self.globals.workspace_padding)
.unwrap_or_default();
// let border_width = self.globals.border_width;
// let border_offset = self.globals.border_offset;
let work_area = self.globals.work_area;
let work_area_offset = self.work_area_offset.or(self.globals.work_area_offset);
// let window_based_work_area_offset = self.globals.window_based_work_area_offset;
// let window_based_work_area_offset_limit =
// self.globals.window_based_work_area_offset_limit;
let mut adjusted_work_area = work_area_offset.map_or_else(
|| self.work_area,
|offset| {
let mut with_offset = work_area;
with_offset.left += offset.left;
with_offset.top += offset.top;
with_offset.right -= offset.right;
with_offset.bottom -= offset.bottom;
with_offset
},
);
adjusted_work_area.add_padding(workspace_padding);
#[allow(clippy::collapsible_if)]
if self.tile {
if !self.containers().is_empty() {
let mut layouts = self.layout.as_boxed_arrangement().calculate(
&adjusted_work_area,
NonZeroUsize::new(self.containers().len()).ok_or_else(|| {
eyre!(
"there must be at least one container to calculate a workspace layout"
)
})?,
Some(container_padding),
self.layout_flip,
&self.resize_dimensions,
self.focused_container_idx(),
self.layout_options,
&self.latest_layout,
);
let containers = self.containers_mut();
for (i, container) in containers.iter_mut().enumerate() {
if let Some(layout) = layouts.get_mut(i) {
layout.add_padding(container_padding);
for window in container.windows() {
if let Err(error) = window.set_position(layout) {
tracing::warn!("failed to set window position: {error}");
}
}
}
}
}
}
Ok(())
}
}