-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ensure monitors are sorted after display setting changes
- Loading branch information
1 parent
b379aba
commit 4049c14
Showing
4 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod add_monitor; | ||
mod remove_monitor; | ||
mod sort_monitors; | ||
mod update_monitor; | ||
|
||
pub use add_monitor::*; | ||
pub use remove_monitor::*; | ||
pub use sort_monitors::*; | ||
pub use update_monitor::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::containers::{ | ||
traits::{CommonGetters, PositionGetters}, | ||
RootContainer, | ||
}; | ||
|
||
/// Sorts the root container's monitors from left-to-right and | ||
/// top-to-bottom. | ||
pub fn sort_monitors(root: RootContainer) -> anyhow::Result<()> { | ||
let monitors = root.monitors(); | ||
|
||
// Create a tuple of monitors and their rects. | ||
let mut monitors_with_rect = monitors | ||
.into_iter() | ||
.map(|monitor| { | ||
let rect = monitor.to_rect()?.clone(); | ||
anyhow::Ok((monitor, rect)) | ||
}) | ||
.try_collect::<Vec<_>>()?; | ||
|
||
// Sort monitors from left-to-right, top-to-bottom. | ||
monitors_with_rect.sort_by(|(_, rect_a), (_, rect_b)| { | ||
if rect_a.x() == rect_b.x() { | ||
rect_a.y().cmp(&rect_b.y()) | ||
} else { | ||
rect_a.x().cmp(&rect_b.x()) | ||
} | ||
}); | ||
|
||
*root.borrow_children_mut() = monitors_with_rect | ||
.into_iter() | ||
.map(|(monitor, _)| monitor.into()) | ||
.collect(); | ||
|
||
Ok(()) | ||
} |