Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workspace ordering allowing for names of kind <number>:<_rest> to be deterministic. #802

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/modules/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ fn create_button(
button
}

// Function to extract numeric prefix and remaining string
fn extract_numeric_and_rest(label: &str) -> (Option<i32>, &str) {
if let Some((prefix, rest)) = label.split_once(':') {
if let Ok(number) = prefix.trim().parse::<i32>() {
return (Some(number), rest); // Successfully parsed number
}
}
(None, label) // Return None if no numeric prefix is found
}

fn reorder_workspaces(container: &gtk::Box) {
let mut buttons = container
.children()
Expand All @@ -175,12 +185,16 @@ fn reorder_workspaces(container: &gtk::Box) {
})
.collect::<Vec<_>>();

buttons.sort_by(|(label_a, _), (label_b, _a)| {
match (label_a.parse::<i32>(), label_b.parse::<i32>()) {
(Ok(a), Ok(b)) => a.cmp(&b),
(Ok(_), Err(_)) => Ordering::Less,
(Err(_), Ok(_)) => Ordering::Greater,
(Err(_), Err(_)) => label_a.cmp(label_b),
// Sort by numeric prefix first, then lexicographical order
buttons.sort_by(|(label_a, _), (label_b, _)| {
let (num_a, _rest_a) = extract_numeric_and_rest(label_a);
let (num_b, _rest_b) = extract_numeric_and_rest(label_b);

match (num_a, num_b) {
(Some(a), Some(b)) => a.cmp(&b), // Compare numbers if both are numeric
(Some(_), None) => Ordering::Less, // Numeric comes before non-numeric
(None, Some(_)) => Ordering::Greater, // Non-numeric comes after numeric
(None, None) => label_a.cmp(label_b), // Fallback to lexicographical order
}
});

Expand Down
Loading