diff --git a/daemon/manager.rs b/daemon/manager.rs index ac3bd42..9d1f310 100644 --- a/daemon/manager.rs +++ b/daemon/manager.rs @@ -16,6 +16,7 @@ use std::collections::HashMap; pub struct NiriTag { tags: HashMap, windows: HashMap, + active_ws: Vec, state: EventStreamState, socket: BufReader, ev_tx: channel::Sender, @@ -33,6 +34,7 @@ impl NiriTag { windows: HashMap::new(), state: EventStreamState::default(), socket: create_niri_socket().await?, + active_ws: Vec::new(), ev_tx, }) } @@ -308,8 +310,45 @@ impl NiriTag { self.windows.remove(&id); Ok(()) } - // WorkspaceActivated { .. } => (), - // WorkspacesChanged { .. } => (), + WorkspaceActivated { id, .. } => { + if !self.active_ws.contains(&id) { + let q = query(&mut self.socket, Request::Workspaces).await?; + let wsid = if let Reply::Ok(Response::Workspaces(workspaces)) = q { + let new_ws = workspaces + .iter() + .find(|ws| ws.id == id) + .expect("Activated workspace not found in workspace query"); + workspaces + .iter() + .find(|ws| { + ws.output == new_ws.output + && ws.id != new_ws.id + && self.active_ws.contains(&ws.id) + }) + .expect("Could not find a valid niri-tag workspace to return to") + .id + } else { + return Err(anyhow!("Invalid response to workspace query")); + }; + tell( + &mut self.socket, + Request::Action(Action::FocusWorkspace { + reference: WorkspaceReferenceArg::Id(wsid), + }), + ) + .await + } else { + Ok(()) + } + } + WorkspacesChanged { workspaces } => { + self.active_ws = workspaces + .into_iter() + .filter(|ws| ws.is_active) + .map(|ws| ws.id) + .collect(); + Ok(()) + } // WorkspaceUrgencyChanged { .. } => (), // WindowsChanged { .. } => (), // WindowUrgencyChanged { .. } => (),