Skip to content
Draft
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions flake.lock

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

1 change: 1 addition & 0 deletions niri-config/src/binds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ pub enum Action {
ExpandColumnToAvailableWidth,
SwitchLayout(#[knuffel(argument, str)] LayoutSwitchTarget),
ShowHotkeyOverlay,
ToggleWorkspaceVisibility(#[knuffel(argument, str)] String),
MoveWorkspaceToMonitorLeft,
MoveWorkspaceToMonitorRight,
MoveWorkspaceToMonitorDown,
Expand Down
2 changes: 2 additions & 0 deletions niri-config/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub struct Workspace {
pub name: WorkspaceName,
#[knuffel(child, unwrap(argument))]
pub open_on_output: Option<String>,
#[knuffel(child, unwrap(argument))]
pub hidden: Option<bool>,
#[knuffel(child)]
pub layout: Option<WorkspaceLayoutPart>,
}
Expand Down
5 changes: 5 additions & 0 deletions niri-ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub enum Request {
Outputs,
/// Request information about workspaces.
Workspaces,
/// Request information for workspaces, including hidden.
WorkspacesWithHidden,
/// Request information about open windows.
Windows,
/// Request information about layer-shell surfaces.
Expand Down Expand Up @@ -121,6 +123,7 @@ pub enum Request {
Casts,
}


/// Reply from niri to client.
///
/// Every request gets one reply.
Expand Down Expand Up @@ -1435,6 +1438,8 @@ pub struct Workspace {
pub is_focused: bool,
/// Id of the active window on this workspace, if any.
pub active_window_id: Option<u64>,
/// Is this workspace hidden
pub is_hidden: bool,
}

/// Configured keyboard layouts.
Expand Down
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum Msg {
Outputs,
/// List workspaces.
Workspaces,
/// List all workspaces, including hidden
WorkspacesWithHidden,
/// List open windows.
Windows,
/// List open layer-shell surfaces.
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ impl ExtWorkspaceHandler for State {
if let Some(output) = output {
self.niri.layout.focus_output(&output);
}
self.niri.layout.switch_workspace(index);
self.niri.layout.switch_workspace(index, false);
// No mouse warp: assuming the layer-shell bar workspaces use-case.

// FIXME: granular
Expand Down
11 changes: 8 additions & 3 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::time::Duration;
use calloop::timer::{TimeoutAction, Timer};
use input::event::gesture::GestureEventCoordinates as _;
use niri_config::{
Action, Bind, Binds, Config, Key, ModKey, Modifiers, MruDirection, SwitchBinds, Trigger, Xkb,
Action, Bind, Binds, Config, Key, ModKey, Modifiers, MruDirection, SwitchBinds, Trigger,
WorkspaceReference, Xkb,
};
use niri_ipc::LayoutSwitchTarget;
use smithay::backend::input::{
Expand Down Expand Up @@ -1482,6 +1483,7 @@ impl State {
}
}
Action::FocusWorkspace(reference) => {
let is_name = matches!(reference, WorkspaceReference::Name(_));
if let Some((mut output, index)) =
self.niri.find_output_and_workspace_index(reference)
{
Expand All @@ -1493,7 +1495,7 @@ impl State {

if let Some(output) = output {
self.niri.layout.focus_output(&output);
self.niri.layout.switch_workspace(index);
self.niri.layout.switch_workspace(index, is_name);
if !self.maybe_warp_cursor_to_focus_centered() {
self.move_cursor_to_output(&output);
}
Expand All @@ -1502,7 +1504,7 @@ impl State {
if config.borrow().input.workspace_auto_back_and_forth {
self.niri.layout.switch_workspace_auto_back_and_forth(index);
} else {
self.niri.layout.switch_workspace(index);
self.niri.layout.switch_workspace(index, is_name);
}
self.maybe_warp_cursor_to_focus();
}
Expand Down Expand Up @@ -2407,6 +2409,9 @@ impl State {
self.niri.queue_redraw_mru_output();
}
}
Action::ToggleWorkspaceVisibility(workspace_name) => {
self.niri.layout.toggle_workspace_visibility(workspace_name);
}
}
}

Expand Down
52 changes: 52 additions & 0 deletions src/ipc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> {
Msg::EventStream => Request::EventStream,
Msg::RequestError => Request::ReturnError,
Msg::OverviewState => Request::OverviewState,
Msg::WorkspacesWithHidden => Request::WorkspacesWithHidden,
Msg::Casts => Request::Casts,
};

Expand Down Expand Up @@ -339,6 +340,54 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> {
println!("The change will apply when it is connected.");
}
}
Msg::WorkspacesWithHidden => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I had added options to Workspaces for different filtering options with Hidden workspaces, but this method seems better since it won't break projects that use the rust_ipc crate as it's opt in.

let Response::Workspaces(mut response) = response else {
bail!("unexpected response: expected Workspaces, got {response:?}");
};

if json {
let response =
serde_json::to_string(&response).context("error formatting response")?;
println!("{response}");
return Ok(());
}

if response.is_empty() {
println!("No workspaces.");
return Ok(());
}

response.sort_by_key(|ws| ws.idx);
response.sort_by(|a, b| a.output.cmp(&b.output));

let mut current_output = if let Some(output) = response[0].output.as_deref() {
println!("Output \"{output}\":");
Some(output)
} else {
println!("No output:");
None
};

for ws in &response {
if ws.output.as_deref() != current_output {
let output = ws.output.as_deref().context(
"invalid response: workspace with no output \
following a workspace with an output",
)?;
current_output = Some(output);
println!("\nOutput \"{output}\":");
}

let is_active = if ws.is_active { " * " } else { " " };
let idx = ws.idx;
let name = if let Some(name) = ws.name.as_deref() {
format!(" \"{name}\"")
} else {
String::new()
};
println!("{is_active}{idx}{name}");
}
}
Msg::Workspaces => {
let Response::Workspaces(mut response) = response else {
bail!("unexpected response: expected Workspaces, got {response:?}");
Expand Down Expand Up @@ -368,6 +417,9 @@ pub fn handle_msg(mut msg: Msg, json: bool) -> anyhow::Result<()> {
};

for ws in &response {
if ws.is_hidden {
continue;
}
if ws.output.as_deref() != current_output {
let output = ws.output.as_deref().context(
"invalid response: workspace with no output \
Expand Down
86 changes: 76 additions & 10 deletions src/ipc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@ async fn process(ctx: &ClientCtx, request: Request) -> Reply {
Response::Outputs(outputs.collect())
}
Request::Workspaces => {
let state = ctx.event_stream_state.borrow();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not include hidden workspaces in normal workspace requests. If consumers want to access hidden workspaces, they'll need to use the new endpoint.

let workspaces = state
.workspaces
.workspaces
.values()
.filter(|workspace| !workspace.is_hidden)
.cloned()
.collect();
Response::Workspaces(workspaces)
}

Request::WorkspacesWithHidden => {
let state = ctx.event_stream_state.borrow();
let workspaces = state.workspaces.workspaces.values().cloned().collect();
Response::Workspaces(workspaces)
Expand Down Expand Up @@ -595,21 +607,32 @@ impl State {
// Check for workspace changes.
let mut seen = HashSet::new();
let mut need_workspaces_changed = false;
let mut last_output: Option<String> = None;
let mut visible_idx = 0usize;
for (mon, ws_idx, ws) in layout.workspaces() {
let id = ws.id().get();
seen.insert(id);

if ws.hidden {
continue;
}

let output_name = mon.map(|mon| mon.output_name().clone());
if last_output != output_name {
visible_idx = 0;
last_output = output_name.clone();
}

let Some(ipc_ws) = state.workspaces.get(&id) else {
// A new workspace was added.
need_workspaces_changed = true;
break;
};

// Check for any changes that we can't signal as individual events.
let output_name = mon.map(|mon| mon.output_name());
if ipc_ws.idx != u8::try_from(ws_idx + 1).unwrap_or(u8::MAX)
if ipc_ws.idx != u8::try_from(visible_idx + 1).unwrap_or(u8::MAX)
|| ipc_ws.name.as_ref() != ws.name()
|| ipc_ws.output.as_ref() != output_name
|| ipc_ws.output.as_ref() != output_name.as_ref()
{
need_workspaces_changed = true;
break;
Expand All @@ -633,6 +656,7 @@ impl State {
let is_focused = Some(id) == focused_ws_id;
if is_focused && !ipc_ws.is_focused {
events.push(Event::WorkspaceActivated { id, focused: true });
visible_idx += 1;
continue;
}

Expand All @@ -641,30 +665,52 @@ impl State {
if is_active && !ipc_ws.is_active {
events.push(Event::WorkspaceActivated { id, focused: false });
}

visible_idx += 1;
}

// Check if any workspaces were removed.
if !need_workspaces_changed && state.workspaces.keys().any(|id| !seen.contains(id)) {
// Check if any workspaces were removed that weren't also hidden.
if !need_workspaces_changed
&& state.workspaces.keys().any(|id| {
!seen.contains(id) && state.workspaces.get(id).is_some_and(|ws| !ws.is_hidden)
})
{
need_workspaces_changed = true;
}

if need_workspaces_changed {
events.clear();

let mut last_output: Option<String> = None;
let mut visible_idx = 0usize;
let workspaces = layout
.workspaces()
.map(|(mon, ws_idx, ws)| {
.filter_map(|(mon, ws_idx, ws)| {
if ws.hidden {
return None;
}

let output_name = mon.map(|mon| mon.output_name().clone());
if last_output != output_name {
visible_idx = 0;
last_output = output_name.clone();
}

let id = ws.id().get();
Workspace {
let result = Workspace {
id,
idx: u8::try_from(ws_idx + 1).unwrap_or(u8::MAX),
idx: u8::try_from(visible_idx + 1).unwrap_or(u8::MAX),
name: ws.name().cloned(),
output: mon.map(|mon| mon.output_name().clone()),
output: output_name,
is_urgent: ws.is_urgent(),
is_active: mon.is_some_and(|mon| mon.active_workspace_idx() == ws_idx),
is_focused: Some(id) == focused_ws_id,
is_hidden: ws.hidden,
active_window_id: ws.active_window().map(|win| win.id().get()),
}
};

visible_idx += 1;
Some(result)
})
.collect();

Expand All @@ -675,6 +721,26 @@ impl State {
state.apply(event.clone());
server.send_event(event);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played around with different methods to keep hidden workspaces out of event stream while still keeping it for the state, this just ended up being the only one I could get to work without horrible things happening.

for (mon, ws_idx, ws) in layout.workspaces() {
if !ws.hidden {
continue;
}
let id = ws.id().get();
let output_name = mon.map(|mon| mon.output_name().clone());
let hidden_ws = Workspace {
id,
idx: u8::try_from(ws_idx + 1).unwrap_or(u8::MAX),
name: ws.name().cloned(),
output: output_name,
is_urgent: ws.is_urgent(),
is_active: false,
is_focused: false,
is_hidden: true,
active_window_id: ws.active_window().map(|win| win.id().get()),
};
state.workspaces.insert(id, hidden_ws);
}
}

fn ipc_refresh_windows(&mut self) {
Expand Down
Loading