Skip to content

Commit

Permalink
Merge pull request #578 from JakeStanger/fix/sway-workspaces
Browse files Browse the repository at this point in the history
fix(workspaces): regression due to #572
  • Loading branch information
JakeStanger authored May 9, 2024
2 parents 4695279 + c45ea02 commit 386955c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/clients/compositor/hyprland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Client {
send!(
tx,
WorkspaceUpdate::Rename {
id: data.workspace_id.to_string(),
id: data.workspace_id as i64,
name: data.workspace_name
}
);
Expand All @@ -169,7 +169,7 @@ impl Client {
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()));
send!(tx, WorkspaceUpdate::Remove(data.workspace_id as i64));
});
}

Expand Down Expand Up @@ -279,7 +279,7 @@ fn create_is_visible() -> impl Fn(&HWorkspace) -> bool {
impl From<(Visibility, HWorkspace)> for Workspace {
fn from((visibility, workspace): (Visibility, HWorkspace)) -> Self {
Self {
id: workspace.id.to_string(),
id: workspace.id as i64,
name: workspace.name,
monitor: workspace.monitor,
visibility,
Expand Down
6 changes: 3 additions & 3 deletions src/clients/compositor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Compositor {
#[derive(Debug, Clone)]
pub struct Workspace {
/// Unique identifier
pub id: String,
pub id: i64,
/// Workspace friendly name
pub name: String,
/// Name of the monitor (output) the workspace is located on
Expand Down Expand Up @@ -119,7 +119,7 @@ pub enum WorkspaceUpdate {
/// This is re-sent to all subscribers when a new subscription is created.
Init(Vec<Workspace>),
Add(Workspace),
Remove(String),
Remove(i64),
Move(Workspace),
/// Declares focus moved from the old workspace to the new.
Focus {
Expand All @@ -128,7 +128,7 @@ pub enum WorkspaceUpdate {
},

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

Expand Down
14 changes: 5 additions & 9 deletions src/clients/compositor/sway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl From<Node> for Workspace {
let visibility = Visibility::from(&node);

Self {
id: node.id.to_string(),
id: node.id,
name: node.name.unwrap_or_default(),
monitor: node.output.unwrap_or_default(),
visibility,
Expand All @@ -103,7 +103,7 @@ impl From<swayipc_async::Workspace> for Workspace {
let visibility = Visibility::from(&workspace);

Self {
id: workspace.id.to_string(),
id: workspace.id,
name: workspace.name,
monitor: workspace.output,
visibility,
Expand Down Expand Up @@ -141,13 +141,9 @@ impl From<WorkspaceEvent> for WorkspaceUpdate {
WorkspaceChange::Init => {
Self::Add(event.current.expect("Missing current workspace").into())
}
WorkspaceChange::Empty => Self::Remove(
event
.current
.expect("Missing current workspace")
.name
.unwrap_or_default(),
),
WorkspaceChange::Empty => {
Self::Remove(event.current.expect("Missing current workspace").id)
}
WorkspaceChange::Focus => Self::Focus {
old: event.old.map(Workspace::from),
new: Workspace::from(event.current.expect("Missing current workspace")),
Expand Down
40 changes: 24 additions & 16 deletions src/modules/workspaces.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::clients::compositor::{Visibility, Workspace, WorkspaceClient, WorkspaceUpdate};
use crate::config::CommonConfig;
use crate::gtk_helpers::IronbarGtkExt;
use crate::image::new_icon_button;
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
use crate::{glib_recv, module_impl, send_async, spawn, try_send};
use crate::{glib_recv, module_impl, send_async, spawn, try_send, Ironbar};
use color_eyre::{Report, Result};
use gtk::prelude::*;
use gtk::{Button, IconTheme};
Expand Down Expand Up @@ -133,9 +134,18 @@ fn reorder_workspaces(container: &gtk::Box) {
}
}

fn find_btn(map: &HashMap<i64, Button>, workspace: &Workspace) -> Option<Button> {
map.get(&workspace.id)
.or_else(|| {
map.values()
.find(|btn| btn.label().unwrap_or_default() == workspace.name)
})
.cloned()
}

impl WorkspacesModule {
fn show_workspace_check(&self, output: &String, work: &Workspace) -> bool {
(work.visibility.is_focused() || !self.hidden.contains(&work.id))
(work.visibility.is_focused() || !self.hidden.contains(&work.name))
&& (self.all_monitors || output == &work.monitor)
}
}
Expand Down Expand Up @@ -193,7 +203,7 @@ impl Module<gtk::Box> for WorkspacesModule {
let favs = self.favorites.clone();
let mut fav_names: Vec<String> = vec![];

let mut button_map: HashMap<String, Button> = HashMap::new();
let mut button_map: HashMap<i64, Button> = HashMap::new();

{
let container = container.clone();
Expand All @@ -213,7 +223,7 @@ impl Module<gtk::Box> for WorkspacesModule {

let mut added = HashSet::new();

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

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

// add workspaces from client
for workspace in &workspaces {
if self.show_workspace_check(&output_name, workspace) {
add_workspace(&workspace.id, &workspace.name, workspace.visibility);
add_workspace(workspace.id, &workspace.name, workspace.visibility);
added.insert(workspace.name.to_string());
}
}
Expand All @@ -244,7 +254,7 @@ impl Module<gtk::Box> for WorkspacesModule {
// 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);
add_workspace(-(Ironbar::unique_id() as i64), name, Visibility::Hidden);
added.insert(name.to_string());
}
}
Expand All @@ -269,20 +279,17 @@ impl Module<gtk::Box> for WorkspacesModule {
}
}
WorkspaceUpdate::Focus { old, new } => {
if let Some(btn) = old.as_ref().and_then(|w| button_map.get(&w.id)) {
if Some(new.monitor) == old.map(|w| w.monitor) {
if let Some(btn) = old.as_ref().and_then(|w| find_btn(&button_map, w)) {
if Some(new.monitor.as_str()) == old.as_ref().map(|w| w.monitor.as_str()) {
btn.style_context().remove_class("visible");
}

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

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");
if let Some(btn) = find_btn(&button_map, &new) {
btn.add_class("visible");
btn.add_class("focused");
}
}
WorkspaceUpdate::Rename { id, name } => {
Expand Down Expand Up @@ -352,7 +359,8 @@ impl Module<gtk::Box> for WorkspacesModule {
WorkspaceUpdate::Remove(workspace) => {
let button = button_map.get(&workspace);
if let Some(item) = button {
if fav_names.contains(&workspace) {
if workspace < 0 {
// if fav_names.contains(&workspace) {
item.style_context().add_class("inactive");
} else {
container.remove(item);
Expand Down
3 changes: 3 additions & 0 deletions test-configs/workspaces.corn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
start = [ { type = "workspaces" }]
}

0 comments on commit 386955c

Please sign in to comment.