Skip to content

Commit

Permalink
Merge pull request #572 from JakeStanger/fix/workspace-rename
Browse files Browse the repository at this point in the history
fix(workspaces): add support for hyprland rename event
  • Loading branch information
JakeStanger authored May 7, 2024
2 parents c6aff14 + 5e7f576 commit 4695279
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ libpulse-binding = { version = "2.28.1", optional = true }

# workspaces
swayipc-async = { version = "2.0.1", optional = true }
hyprland = { version = "0.4.0-alpha.1", features = ["silent"], optional = true }
hyprland = { version = "0.4.0-alpha.2", features = ["silent"], optional = true }
futures-util = { version = "0.3.30", optional = true }

# shared
Expand Down
23 changes: 19 additions & 4 deletions src/clients/compositor/hyprland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,27 @@ impl Client {
}

{
event_listener.add_workspace_destroy_handler(move |workspace_type| {
let tx = tx.clone();
let lock = lock.clone();

event_listener.add_workspace_rename_handler(move |data| {
let _lock = lock!(lock);
debug!("Received workspace destroy: {workspace_type:?}");

let name = get_workspace_name(workspace_type);
send!(tx, WorkspaceUpdate::Remove(name));
send!(
tx,
WorkspaceUpdate::Rename {
id: data.workspace_id.to_string(),
name: data.workspace_name
}
);
});
}

{
event_listener.add_workspace_destroy_handler(move |data| {
let _lock = lock!(lock);
debug!("Received workspace destroy: {data:?}");
send!(tx, WorkspaceUpdate::Remove(data.workspace_id.to_string()));
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/clients/compositor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ pub enum WorkspaceUpdate {
old: Option<Workspace>,
new: Workspace,
},

Rename {
id: String,
name: String,
},

/// An update was triggered by the compositor but this was not mapped by Ironbar.
///
/// This is purely used for ergonomics within the compositor clients
Expand Down
32 changes: 21 additions & 11 deletions src/modules/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn reorder_workspaces(container: &gtk::Box) {

impl WorkspacesModule {
fn show_workspace_check(&self, output: &String, work: &Workspace) -> bool {
(work.visibility.is_focused() || !self.hidden.contains(&work.name))
(work.visibility.is_focused() || !self.hidden.contains(&work.id))
&& (self.all_monitors || output == &work.monitor)
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ impl Module<gtk::Box> for WorkspacesModule {

let mut added = HashSet::new();

let mut add_workspace = |name: &str, visibility: Visibility| {
let mut add_workspace = |id: &str, name: &str, visibility: Visibility| {
let item = create_button(
name,
visibility,
Expand All @@ -224,13 +224,13 @@ impl Module<gtk::Box> for WorkspacesModule {
);

container.add(&item);
button_map.insert(name.to_string(), item);
button_map.insert(id.to_string(), item);
};

// add workspaces from client
for workspace in &workspaces {
if self.show_workspace_check(&output_name, workspace) {
add_workspace(&workspace.name, workspace.visibility);
add_workspace(&workspace.id, &workspace.name, workspace.visibility);
added.insert(workspace.name.to_string());
}
}
Expand All @@ -240,7 +240,11 @@ impl Module<gtk::Box> for WorkspacesModule {
fav_names.push(name.to_string());

if !added.contains(name) {
add_workspace(name, Visibility::Hidden);
// Favourites are added with the same name and ID
// as Hyprland will initialize them this way.
// Since existing workspaces are added above,
// this means there shouldn't be any issues with renaming.
add_workspace(name, name, Visibility::Hidden);
added.insert(name.to_string());
}
}
Expand All @@ -265,25 +269,31 @@ impl Module<gtk::Box> for WorkspacesModule {
}
}
WorkspaceUpdate::Focus { old, new } => {
if let Some(btn) = old.as_ref().and_then(|w| button_map.get(&w.name)) {
if let Some(btn) = old.as_ref().and_then(|w| button_map.get(&w.id)) {
if Some(new.monitor) == old.map(|w| w.monitor) {
btn.style_context().remove_class("visible");
}

btn.style_context().remove_class("focused");
}

let new = button_map.get(&new.name);
let new = button_map.get(&new.id);
if let Some(btn) = new {
let style = btn.style_context();

style.add_class("visible");
style.add_class("focused");
}
}
WorkspaceUpdate::Rename { id, name } => {
if let Some(btn) = button_map.get(&id) {
let name = name_map.get(&name).unwrap_or(&name);
btn.set_label(name);
}
}
WorkspaceUpdate::Add(workspace) => {
if fav_names.contains(&workspace.name) {
let btn = button_map.get(&workspace.name);
let btn = button_map.get(&workspace.id);
if let Some(btn) = btn {
btn.style_context().remove_class("inactive");
}
Expand All @@ -306,7 +316,7 @@ impl Module<gtk::Box> for WorkspacesModule {
item.show();

if !name.is_empty() {
button_map.insert(name, item);
button_map.insert(workspace.id, item);
}
}
}
Expand All @@ -332,9 +342,9 @@ impl Module<gtk::Box> for WorkspacesModule {
item.show();

if !name.is_empty() {
button_map.insert(name, item);
button_map.insert(workspace.id, item);
}
} else if let Some(item) = button_map.get(&workspace.name) {
} else if let Some(item) = button_map.get(&workspace.id) {
container.remove(item);
}
}
Expand Down

0 comments on commit 4695279

Please sign in to comment.