Skip to content

Commit c828166

Browse files
authored
sort icons according to window position (hyprland-community#130)
1 parent 902981f commit c828166

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

src/renamer/formatter.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,32 @@ impl Renamer {
3030
let mut counted =
3131
generate_counted_clients(workspace.clients.clone(), config.format.dedup);
3232

33+
if config.format.dedup {
34+
let mut ordered = Vec::with_capacity(counted.len());
35+
let mut used = vec![false; counted.len()];
36+
37+
for client in &workspace.clients {
38+
if let Some((idx, _)) = counted
39+
.iter()
40+
.enumerate()
41+
.find(|(idx, (c, _))| !used[*idx] && c == client)
42+
{
43+
ordered.push(counted[idx].clone());
44+
used[idx] = true;
45+
}
46+
}
47+
48+
ordered.extend(counted.into_iter().enumerate().filter_map(|(idx, entry)| {
49+
if used[idx] {
50+
None
51+
} else {
52+
Some(entry)
53+
}
54+
}));
55+
56+
counted = ordered;
57+
}
58+
3359
let workspace_output = counted
3460
.iter_mut()
3561
.map(|(client, counter)| self.handle_new_client(client, *counter, config))

src/renamer/mod.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ impl Renamer {
151151
active_client: String,
152152
config: &ConfigFile,
153153
) -> Result<Vec<AppWorkspace>, Box<dyn Error + '_>> {
154-
let mut workspaces = self
154+
let mut workspaces: HashMap<i32, Vec<(AppClient, (i16, i16))>> = self
155155
.known_workspaces
156156
.lock()?
157157
.iter()
158158
.map(|&i| (i, Vec::new()))
159-
.collect::<HashMap<i32, Vec<AppClient>>>();
159+
.collect();
160160

161161
let is_dedup_inactive_fullscreen = config.format.dedup_inactive_fullscreen;
162162

@@ -167,24 +167,33 @@ impl Renamer {
167167
workspaces
168168
.entry(workspace_id)
169169
.or_insert_with(Vec::new)
170-
.push(AppClient::new(
171-
client.clone(),
172-
is_active,
173-
is_dedup_inactive_fullscreen,
174-
self.parse_icon(
175-
client.initial_class,
176-
client.class,
177-
client.initial_title,
178-
client.title,
170+
.push((
171+
AppClient::new(
172+
client.clone(),
179173
is_active,
180-
config,
174+
is_dedup_inactive_fullscreen,
175+
self.parse_icon(
176+
client.initial_class,
177+
client.class,
178+
client.initial_title,
179+
client.title,
180+
is_active,
181+
config,
182+
),
181183
),
184+
client.at,
182185
));
183186
}
184187

185188
Ok(workspaces
186-
.iter()
187-
.map(|(&id, clients)| AppWorkspace::new(id, clients.to_vec()))
189+
.into_iter()
190+
.map(|(id, mut clients)| {
191+
clients.sort_by(|a, b| a.1 .0.cmp(&b.1 .0).then_with(|| a.1 .1.cmp(&b.1 .1)));
192+
193+
let clients = clients.into_iter().map(|(client, _)| client).collect();
194+
195+
AppWorkspace::new(id, clients)
196+
})
188197
.collect())
189198
}
190199

@@ -1384,7 +1393,7 @@ mod tests {
13841393
},
13851394
);
13861395

1387-
let expected = [(1, "*term* term4".to_string())].into_iter().collect();
1396+
let expected = [(1, "term4 *term*".to_string())].into_iter().collect();
13881397

13891398
let actual = renamer.generate_workspaces_string(
13901399
vec![AppWorkspace {
@@ -1509,7 +1518,7 @@ mod tests {
15091518
},
15101519
);
15111520

1512-
let expected = [(1, "[term] term4".to_string())].into_iter().collect();
1521+
let expected = [(1, "term4 [term]".to_string())].into_iter().collect();
15131522

15141523
let actual = renamer.generate_workspaces_string(
15151524
vec![AppWorkspace {
@@ -1637,7 +1646,7 @@ mod tests {
16371646
},
16381647
);
16391648

1640-
let expected = [(1, "[*term*] term4".to_string())].into_iter().collect();
1649+
let expected = [(1, "term4 [*term*]".to_string())].into_iter().collect();
16411650

16421651
let actual = renamer.generate_workspaces_string(
16431652
vec![AppWorkspace {

0 commit comments

Comments
 (0)