1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::collections::{BTreeMap, VecDeque};
use hyprland::shared::{Address, MonitorId, WorkspaceId};
use crate::ClientData;
/// Sorts clients with complex sorting
///
/// * 'clients' - Vector of clients to sort
/// * 'ignore_workspaces' - Don't split clients into workspaces (treat all clients on monitor as one workspace)
/// * 'ignore_monitors' - Don't split clients into monitors (treat all clients as one monitor)
pub fn sort_clients(
clients: Vec<(Address, ClientData)>,
ignore_workspaces: bool,
ignore_monitors: bool,
) -> Vec<(Address, ClientData)> {
// monitor -> workspace -> clients
let monitors: Vec<Vec<Vec<(Address, ClientData)>>> = match (ignore_workspaces, ignore_monitors)
{
(true, true) => {
panic!(
"Can't ignore workspaces and monitors at the same time (currently not implemented)"
);
// one monitor with one workspace with every client
// vec![vec![clients]]
}
(true, false) => {
// workspace -> clients
let mut monitors: BTreeMap<MonitorId, Vec<(Address, ClientData)>> = BTreeMap::new();
for (addr, client) in clients {
monitors
.entry(client.monitor)
.or_default()
.push((addr, client));
}
monitors.into_values().map(|m| vec![m]).collect()
}
(false, true) => {
// monitor -> workspaces
let mut workspaces: BTreeMap<MonitorId, Vec<WorkspaceId>> = BTreeMap::new();
for (_, client) in clients.iter() {
workspaces
.entry(client.monitor)
.or_default()
.push(client.workspace);
}
// sort workspaces on monitor (and remove duplicates)
for (_, ws) in workspaces.iter_mut() {
ws.sort();
ws.dedup();
ws.reverse();
}
// old (real) workspaceId -> new workspaceId
let mut workspaces_map: BTreeMap<WorkspaceId, WorkspaceId> = BTreeMap::new();
loop {
let mut current_workspaces = vec![];
// get one workspace from each monitor
for (_, ws) in workspaces.iter_mut() {
if let Some(workspace) = ws.pop() {
current_workspaces.push(workspace);
}
}
if current_workspaces.is_empty() {
break;
}
let new_workspace_id = current_workspaces[0];
// debug!("current_workspaces: {:?}, new_workspace_id: {}", current_workspaces, new_workspace_id);
for wss in current_workspaces {
workspaces_map.insert(wss, new_workspace_id);
}
}
let mut new_workspaces: BTreeMap<WorkspaceId, Vec<(Address, ClientData)>> =
BTreeMap::new();
for (addr, client) in clients {
new_workspaces
.entry(
*workspaces_map
.get(&client.workspace)
.expect("Workspace for client not found"),
)
.or_default()
.push((addr, client));
}
new_workspaces.into_values().map(|m| vec![m]).collect()
}
(false, false) => {
// monitor -> workspace -> clients
let mut monitors: BTreeMap<
MonitorId,
BTreeMap<WorkspaceId, Vec<(Address, ClientData)>>,
> = BTreeMap::new();
for (addr, client) in clients {
monitors
.entry(client.monitor)
.or_default()
.entry(client.workspace)
.or_default()
.push((addr, client));
}
monitors
.into_values()
.map(|m| m.into_values().collect())
.collect()
}
};
let mut sorted_clients = Vec::new();
for workspaces in monitors {
for mut clients in workspaces {
clients.sort_by(|(_, a), (_, b)| {
if a.x == b.x {
a.y.cmp(&b.y)
} else {
a.x.cmp(&b.x)
}
});
let mut queue: VecDeque<(Address, ClientData)> = VecDeque::from(clients);
let mut line_start = queue.pop_front();
while let Some((current_addr, current)) = line_start {
let mut current_bottom = current.y + current.height;
sorted_clients.push((current_addr, current));
loop {
let mut next_index = None;
/*
1. Check If Top left of window is higher or lower than bottom left of current
2. Check if any window(not taken) on left top is higher or lower than current Lower (if true take this)
3. Check if any window(not taken) on left bottom is higher than current bottom (if true take this)
=> Take if Top higher than current Bottom and no window on left has higher Top than window Bottom
*/
for (i, (_, client)) in queue.iter().enumerate() {
let client_top = client.y;
let client_bottom = client.y + client.height;
let client_left = client.x;
if client_top < current_bottom {
// 1.
// client top is inside current row
// 2.
let on_left = queue
.iter()
.enumerate()
.find(|(_, (_, c))| c.x < client_left && c.y < client_bottom);
// 3.
let on_left_2 = queue.iter().enumerate().find(|(_, (_, c))| {
c.x < client_left && c.y + c.height < client_bottom
});
match (on_left, on_left_2) {
(Some((idx, (_, c))), _) => {
current_bottom = c.y + c.height;
next_index = Some(idx);
}
(_, Some((idx, (_, c)))) => {
current_bottom = c.y + c.height;
next_index = Some(idx);
}
(None, None) => {
next_index = Some(i);
}
}
break;
}
}
match next_index.and_then(|i| queue.remove(i)) {
Some(next) => {
sorted_clients.push(next);
}
None => {
break;
}
}
}
line_start = queue.pop_front();
}
}
}
sorted_clients
}