Skip to content

Commit

Permalink
added new sorting algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
H3rmt committed Feb 23, 2024
1 parent 20816cb commit 6076a49
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/sort_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::{BTreeMap, VecDeque};
use std::fmt::Debug;

use hyprland::shared::WorkspaceId;
use log::debug;

use crate::MonitorId;
use crate::sort::SortableClient;
Expand Down Expand Up @@ -54,6 +53,7 @@ pub fn sort_clients<SC>(

for workspaces in monitors {
for mut clients in workspaces {

clients.sort_by(|a, b| {
if a.x() == b.x() {
a.y().cmp(&b.y())
Expand All @@ -64,10 +64,44 @@ pub fn sort_clients<SC>(
println!("sorted clients: {:?}", clients);
let mut queue: VecDeque<SC> = VecDeque::from(clients);

while let Some(current) = queue.pop_front() {
let mut line_start = queue.pop_front();
while let Some(current) = line_start {
let current_top = current.y();
let current_bottom = current.y() + current.h();
sorted_clients.push(current);

loop {
let mut next_index = None;
for (i, client) in queue.iter().enumerate() {
let client_top = client.y();
let client_bottom = client.y() + client.h();
let client_left = client.x();

println!("{:?} current_top: {}, client_top: {}, client_bottom: {}", client.identifier(), current_top, client_top, client_bottom);
if current_top <= client_top && client_top < current_bottom {
// client top is inside current row
println!("{:?} inside", client.identifier());

let on_left = queue.iter().find(|c| c.x() < client_left && c.y() < client_bottom);
println!("{:?} on_left: {:?}", client.identifier(), on_left);
if on_left.is_none() {
// client has no window on left with its top higher than current bottom
next_index = Some(i);
break;
}
}
}
match next_index.and_then(|i| queue.remove(i)) {
Some(next) => {
println!("next: {:?}", next);
sorted_clients.push(next);
},
None => break,
}
}

line_start = queue.pop_front();
println!("line_start: {:?}", line_start);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/simple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ fn order_1() {
MockClient(3, 5, 1, 2, 0, 0, "3".to_string()),
MockClient(5, 5, 1, 2, 0, 0, "4".to_string()),
];
let len = clients.len();

let mut monitor_data: HashMap<MonitorId, MonitorData> = HashMap::new();
monitor_data.insert(0, mon(0, 0, 6, 7));
Expand All @@ -268,6 +269,7 @@ fn order_1() {
println!("{clients:?} ({:?})", start.elapsed());
create_svg_from_client_tests(&clients, function!(), monitor_data);

assert_eq!(clients.len(), len);
assert!(is_sorted(&clients));
}

Expand All @@ -291,6 +293,7 @@ fn order_2() {
MockClient(5, 1, 1, 3, 0, 0, "3".to_string()),
MockClient(5, 5, 1, 2, 0, 0, "4".to_string()),
];
let len = clients.len();

let mut monitor_data: HashMap<MonitorId, MonitorData> = HashMap::new();
monitor_data.insert(0, mon(0, 0, 6, 7));
Expand All @@ -306,6 +309,7 @@ fn order_2() {
println!("{clients:?} ({:?})", start.elapsed());
create_svg_from_client_tests(&clients, function!(), monitor_data);

assert_eq!(clients.len(), len);
assert!(is_sorted(&clients));
}

Expand Down

0 comments on commit 6076a49

Please sign in to comment.