Add toggle-window-rule-opacity action
Browse files at this commita10705fb200b452802a1ba7cd47679536e0ef849Parent: b01b8af13 changed files +115 -2
- niri-config/src/lib.rs +7 -0
- niri-ipc/src/lib.rs +12 -0
- niri-visual-tests/src/test_window.rs +4 -0
- src/input/mod.rs +29 -0
- src/layout/floating.rs +8 -0
- src/layout/mod.rs +5 -0
- src/layout/scrolling.rs +9 -0
- src/layout/tile.rs +1 -1
- src/layout/workspace.rs +8 -0
- src/niri.rs +1 -1
- src/window/mapped.rs +18 -0
- wiki/Configuration:-Key-Bindings.md +11 -0
- wiki/Configuration:-Window-Rules.md +2 -0
niri-config/src/lib.rsmodified+7 -0
| @@ -1425,6 +1425,9 @@ pub enum Action { | |||
| 1425 | x: PositionChange, | 1425 | x: PositionChange, |
| 1426 | y: PositionChange, | 1426 | y: PositionChange, |
| 1427 | }, | 1427 | }, |
| 1428 | + ToggleWindowRuleOpacity, | ||
| 1429 | + #[knuffel(skip)] | ||
| 1430 | + ToggleWindowRuleOpacityById(u64), | ||
| 1428 | } | 1431 | } |
| 1429 | 1432 | ||
| 1430 | impl From<niri_ipc::Action> for Action { | 1433 | impl From<niri_ipc::Action> for Action { |
| @@ -1623,6 +1626,10 @@ impl From<niri_ipc::Action> for Action { | |||
| 1623 | niri_ipc::Action::MoveFloatingWindow { id, x, y } => { | 1626 | niri_ipc::Action::MoveFloatingWindow { id, x, y } => { |
| 1624 | Self::MoveFloatingWindowById { id, x, y } | 1627 | Self::MoveFloatingWindowById { id, x, y } |
| 1625 | } | 1628 | } |
| 1629 | + niri_ipc::Action::ToggleWindowRuleOpacity { id: None } => Self::ToggleWindowRuleOpacity, | ||
| 1630 | + niri_ipc::Action::ToggleWindowRuleOpacity { id: Some(id) } => { | ||
| 1631 | + Self::ToggleWindowRuleOpacityById(id) | ||
| 1632 | + } | ||
| 1626 | } | 1633 | } |
| 1627 | } | 1634 | } |
| 1628 | } | 1635 | } |
niri-ipc/src/lib.rsmodified+12 -0
| @@ -578,6 +578,18 @@ pub enum Action { | |||
| 578 | )] | 578 | )] |
| 579 | y: PositionChange, | 579 | y: PositionChange, |
| 580 | }, | 580 | }, |
| 581 | + /// Toggle the opacity of a window. | ||
| 582 | + #[cfg_attr( | ||
| 583 | + feature = "clap", | ||
| 584 | + clap(about = "Toggle the opacity of the focused window") | ||
| 585 | + )] | ||
| 586 | + ToggleWindowRuleOpacity { | ||
| 587 | + /// Id of the window. | ||
| 588 | + /// | ||
| 589 | + /// If `None`, uses the focused window. | ||
| 590 | + #[cfg_attr(feature = "clap", arg(long))] | ||
| 591 | + id: Option<u64>, | ||
| 592 | + }, | ||
| 581 | } | 593 | } |
| 582 | 594 | ||
| 583 | /// Change in window or column size. | 595 | /// Change in window or column size. |
niri-visual-tests/src/test_window.rsmodified+4 -0
| @@ -224,6 +224,10 @@ impl LayoutElement for TestWindow { | |||
| 224 | 224 | ||
| 225 | fn set_bounds(&self, _bounds: Size<i32, Logical>) {} | 225 | fn set_bounds(&self, _bounds: Size<i32, Logical>) {} |
| 226 | 226 | ||
| 227 | + fn is_ignoring_opacity_window_rule(&self) -> bool { | ||
| 228 | + false | ||
| 229 | + } | ||
| 230 | + | ||
| 227 | fn configure_intent(&self) -> ConfigureIntent { | 231 | fn configure_intent(&self) -> ConfigureIntent { |
| 228 | ConfigureIntent::CanSend | 232 | ConfigureIntent::CanSend |
| 229 | } | 233 | } |
src/input/mod.rsmodified+29 -0
| @@ -39,6 +39,7 @@ use self::move_grab::MoveGrab; | |||
| 39 | use self::resize_grab::ResizeGrab; | 39 | use self::resize_grab::ResizeGrab; |
| 40 | use self::spatial_movement_grab::SpatialMovementGrab; | 40 | use self::spatial_movement_grab::SpatialMovementGrab; |
| 41 | use crate::layout::scrolling::ScrollDirection; | 41 | use crate::layout::scrolling::ScrollDirection; |
| 42 | +use crate::layout::LayoutElement as _; | ||
| 42 | use crate::niri::State; | 43 | use crate::niri::State; |
| 43 | use crate::ui::screenshot_ui::ScreenshotUi; | 44 | use crate::ui::screenshot_ui::ScreenshotUi; |
| 44 | use crate::utils::spawning::spawn; | 45 | use crate::utils::spawning::spawn; |
| @@ -1574,6 +1575,34 @@ impl State { | |||
| 1574 | // FIXME: granular | 1575 | // FIXME: granular |
| 1575 | self.niri.queue_redraw_all(); | 1576 | self.niri.queue_redraw_all(); |
| 1576 | } | 1577 | } |
| 1578 | + Action::ToggleWindowRuleOpacity => { | ||
| 1579 | + let active_window = self | ||
| 1580 | + .niri | ||
| 1581 | + .layout | ||
| 1582 | + .active_workspace_mut() | ||
| 1583 | + .and_then(|ws| ws.active_window_mut()); | ||
| 1584 | + if let Some(window) = active_window { | ||
| 1585 | + if window.rules().opacity.is_some_and(|o| o != 1.) { | ||
| 1586 | + window.toggle_ignore_opacity_window_rule(); | ||
| 1587 | + // FIXME: granular | ||
| 1588 | + self.niri.queue_redraw_all(); | ||
| 1589 | + } | ||
| 1590 | + } | ||
| 1591 | + } | ||
| 1592 | + Action::ToggleWindowRuleOpacityById(id) => { | ||
| 1593 | + let window = self | ||
| 1594 | + .niri | ||
| 1595 | + .layout | ||
| 1596 | + .workspaces_mut() | ||
| 1597 | + .find_map(|ws| ws.windows_mut().find(|w| w.id().get() == id)); | ||
| 1598 | + if let Some(window) = window { | ||
| 1599 | + if window.rules().opacity.is_some_and(|o| o != 1.) { | ||
| 1600 | + window.toggle_ignore_opacity_window_rule(); | ||
| 1601 | + // FIXME: granular | ||
| 1602 | + self.niri.queue_redraw_all(); | ||
| 1603 | + } | ||
| 1604 | + } | ||
| 1605 | + } | ||
| 1577 | } | 1606 | } |
| 1578 | } | 1607 | } |
| 1579 | 1608 | ||
src/layout/floating.rsmodified+8 -0
| @@ -365,6 +365,14 @@ impl<W: LayoutElement> FloatingSpace<W> { | |||
| 365 | .map(Tile::window) | 365 | .map(Tile::window) |
| 366 | } | 366 | } |
| 367 | 367 | ||
| 368 | + pub fn active_window_mut(&mut self) -> Option<&mut W> { | ||
| 369 | + let id = self.active_window_id.as_ref()?; | ||
| 370 | + self.tiles | ||
| 371 | + .iter_mut() | ||
| 372 | + .find(|tile| tile.window().id() == id) | ||
| 373 | + .map(Tile::window_mut) | ||
| 374 | + } | ||
| 375 | + | ||
| 368 | pub fn has_window(&self, id: &W::Id) -> bool { | 376 | pub fn has_window(&self, id: &W::Id) -> bool { |
| 369 | self.tiles.iter().any(|tile| tile.window().id() == id) | 377 | self.tiles.iter().any(|tile| tile.window().id() == id) |
| 370 | } | 378 | } |
src/layout/mod.rsmodified+5 -0
| @@ -190,6 +190,7 @@ pub trait LayoutElement { | |||
| 190 | fn set_active_in_column(&mut self, active: bool); | 190 | fn set_active_in_column(&mut self, active: bool); |
| 191 | fn set_floating(&mut self, floating: bool); | 191 | fn set_floating(&mut self, floating: bool); |
| 192 | fn set_bounds(&self, bounds: Size<i32, Logical>); | 192 | fn set_bounds(&self, bounds: Size<i32, Logical>); |
| 193 | + fn is_ignoring_opacity_window_rule(&self) -> bool; | ||
| 193 | 194 | ||
| 194 | fn configure_intent(&self) -> ConfigureIntent; | 195 | fn configure_intent(&self) -> ConfigureIntent; |
| 195 | fn send_pending_configure(&mut self); | 196 | fn send_pending_configure(&mut self); |
| @@ -4347,6 +4348,10 @@ mod tests { | |||
| 4347 | 4348 | ||
| 4348 | fn set_bounds(&self, _bounds: Size<i32, Logical>) {} | 4349 | fn set_bounds(&self, _bounds: Size<i32, Logical>) {} |
| 4349 | 4350 | ||
| 4351 | + fn is_ignoring_opacity_window_rule(&self) -> bool { | ||
| 4352 | + false | ||
| 4353 | + } | ||
| 4354 | + | ||
| 4350 | fn configure_intent(&self) -> ConfigureIntent { | 4355 | fn configure_intent(&self) -> ConfigureIntent { |
| 4351 | ConfigureIntent::CanSend | 4356 | ConfigureIntent::CanSend |
| 4352 | } | 4357 | } |
src/layout/scrolling.rsmodified+9 -0
| @@ -383,6 +383,15 @@ impl<W: LayoutElement> ScrollingSpace<W> { | |||
| 383 | Some(col.tiles[col.active_tile_idx].window()) | 383 | Some(col.tiles[col.active_tile_idx].window()) |
| 384 | } | 384 | } |
| 385 | 385 | ||
| 386 | + pub fn active_window_mut(&mut self) -> Option<&mut W> { | ||
| 387 | + if self.columns.is_empty() { | ||
| 388 | + return None; | ||
| 389 | + } | ||
| 390 | + | ||
| 391 | + let col = &mut self.columns[self.active_column_idx]; | ||
| 392 | + Some(col.tiles[col.active_tile_idx].window_mut()) | ||
| 393 | + } | ||
| 394 | + | ||
| 386 | pub fn active_tile_mut(&mut self) -> Option<&mut Tile<W>> { | 395 | pub fn active_tile_mut(&mut self) -> Option<&mut Tile<W>> { |
| 387 | if self.columns.is_empty() { | 396 | if self.columns.is_empty() { |
| 388 | return None; | 397 | return None; |
src/layout/tile.rsmodified+1 -1
| @@ -720,7 +720,7 @@ impl<W: LayoutElement> Tile<W> { | |||
| 720 | ) -> impl Iterator<Item = TileRenderElement<R>> + 'a { | 720 | ) -> impl Iterator<Item = TileRenderElement<R>> + 'a { |
| 721 | let _span = tracy_client::span!("Tile::render_inner"); | 721 | let _span = tracy_client::span!("Tile::render_inner"); |
| 722 | 722 | ||
| 723 | - let alpha = if self.is_fullscreen { | 723 | + let alpha = if self.is_fullscreen || self.window.is_ignoring_opacity_window_rule() { |
| 724 | 1. | 724 | 1. |
| 725 | } else { | 725 | } else { |
| 726 | self.window.rules().opacity.unwrap_or(1.).clamp(0., 1.) | 726 | self.window.rules().opacity.unwrap_or(1.).clamp(0., 1.) |
src/layout/workspace.rsmodified+8 -0
| @@ -406,6 +406,14 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 406 | } | 406 | } |
| 407 | } | 407 | } |
| 408 | 408 | ||
| 409 | + pub fn active_window_mut(&mut self) -> Option<&mut W> { | ||
| 410 | + if self.floating_is_active.get() { | ||
| 411 | + self.floating.active_window_mut() | ||
| 412 | + } else { | ||
| 413 | + self.scrolling.active_window_mut() | ||
| 414 | + } | ||
| 415 | + } | ||
| 416 | + | ||
| 409 | pub fn is_active_fullscreen(&self) -> bool { | 417 | pub fn is_active_fullscreen(&self) -> bool { |
| 410 | self.scrolling.is_active_fullscreen() | 418 | self.scrolling.is_active_fullscreen() |
| 411 | } | 419 | } |
src/niri.rsmodified+1 -1
| @@ -4597,7 +4597,7 @@ impl Niri { | |||
| 4597 | let _span = tracy_client::span!("Niri::screenshot_window"); | 4597 | let _span = tracy_client::span!("Niri::screenshot_window"); |
| 4598 | 4598 | ||
| 4599 | let scale = Scale::from(output.current_scale().fractional_scale()); | 4599 | let scale = Scale::from(output.current_scale().fractional_scale()); |
| 4600 | - let alpha = if mapped.is_fullscreen() { | 4600 | + let alpha = if mapped.is_fullscreen() || mapped.is_ignoring_opacity_window_rule() { |
| 4601 | 1. | 4601 | 1. |
| 4602 | } else { | 4602 | } else { |
| 4603 | mapped.rules().opacity.unwrap_or(1.).clamp(0., 1.) | 4603 | mapped.rules().opacity.unwrap_or(1.).clamp(0., 1.) |
src/window/mapped.rsmodified+18 -0
| @@ -69,6 +69,9 @@ pub struct Mapped { | |||
| 69 | /// Whether this window is floating. | 69 | /// Whether this window is floating. |
| 70 | is_floating: bool, | 70 | is_floating: bool, |
| 71 | 71 | ||
| 72 | + /// Whether this window should ignore opacity set through window rules. | ||
| 73 | + ignore_opacity_window_rule: bool, | ||
| 74 | + | ||
| 72 | /// Buffer to draw instead of the window when it should be blocked out. | 75 | /// Buffer to draw instead of the window when it should be blocked out. |
| 73 | block_out_buffer: RefCell<SolidColorBuffer>, | 76 | block_out_buffer: RefCell<SolidColorBuffer>, |
| 74 | 77 | ||
| @@ -167,6 +170,7 @@ impl Mapped { | |||
| 167 | is_focused: false, | 170 | is_focused: false, |
| 168 | is_active_in_column: true, | 171 | is_active_in_column: true, |
| 169 | is_floating: false, | 172 | is_floating: false, |
| 173 | + ignore_opacity_window_rule: false, | ||
| 170 | block_out_buffer: RefCell::new(SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.])), | 174 | block_out_buffer: RefCell::new(SolidColorBuffer::new((0., 0.), [0., 0., 0., 1.])), |
| 171 | animate_next_configure: false, | 175 | animate_next_configure: false, |
| 172 | animate_serials: Vec::new(), | 176 | animate_serials: Vec::new(), |
| @@ -192,6 +196,12 @@ impl Mapped { | |||
| 192 | return false; | 196 | return false; |
| 193 | } | 197 | } |
| 194 | 198 | ||
| 199 | + // If the opacity window rule no longer makes the window semitransparent, reset the ignore | ||
| 200 | + // flag to reduce surprises down the line. | ||
| 201 | + if !new_rules.opacity.is_some_and(|o| o < 1.) { | ||
| 202 | + self.ignore_opacity_window_rule = false; | ||
| 203 | + } | ||
| 204 | + | ||
| 195 | self.rules = new_rules; | 205 | self.rules = new_rules; |
| 196 | true | 206 | true |
| 197 | } | 207 | } |
| @@ -228,6 +238,10 @@ impl Mapped { | |||
| 228 | self.is_floating | 238 | self.is_floating |
| 229 | } | 239 | } |
| 230 | 240 | ||
| 241 | + pub fn toggle_ignore_opacity_window_rule(&mut self) { | ||
| 242 | + self.ignore_opacity_window_rule = !self.ignore_opacity_window_rule; | ||
| 243 | + } | ||
| 244 | + | ||
| 231 | pub fn set_is_focused(&mut self, is_focused: bool) { | 245 | pub fn set_is_focused(&mut self, is_focused: bool) { |
| 232 | if self.is_focused == is_focused { | 246 | if self.is_focused == is_focused { |
| 233 | return; | 247 | return; |
| @@ -839,6 +853,10 @@ impl LayoutElement for Mapped { | |||
| 839 | .with_pending_state(|state| state.states.contains(xdg_toplevel::State::Fullscreen)) | 853 | .with_pending_state(|state| state.states.contains(xdg_toplevel::State::Fullscreen)) |
| 840 | } | 854 | } |
| 841 | 855 | ||
| 856 | + fn is_ignoring_opacity_window_rule(&self) -> bool { | ||
| 857 | + self.ignore_opacity_window_rule | ||
| 858 | + } | ||
| 859 | + | ||
| 842 | fn requested_size(&self) -> Option<Size<i32, Logical>> { | 860 | fn requested_size(&self) -> Option<Size<i32, Logical>> { |
| 843 | self.toplevel().with_pending_state(|state| state.size) | 861 | self.toplevel().with_pending_state(|state| state.size) |
| 844 | } | 862 | } |
wiki/Configuration:-Key-Bindings.mdmodified+11 -0
| @@ -253,3 +253,14 @@ Or, in scripts: | |||
| 253 | ```shell | 253 | ```shell |
| 254 | niri msg action do-screen-transition --delay-ms 100 | 254 | niri msg action do-screen-transition --delay-ms 100 |
| 255 | ``` | 255 | ``` |
| 256 | + | ||
| 257 | +#### `toggle-window-rule-opacity` | ||
| 258 | + | ||
| 259 | +Toggle the opacity window rule of the focused window. | ||
| 260 | +This only has an effect if the window's opacity window rule is already set to semitransparent. | ||
| 261 | + | ||
| 262 | +```kdl | ||
| 263 | +binds { | ||
| 264 | + Mod+O { toggle-window-rule-opacity; } | ||
| 265 | +} | ||
| 266 | +``` | ||
wiki/Configuration:-Window-Rules.mdmodified+2 -0
| @@ -492,6 +492,8 @@ This is applied on top of the window's own opacity, so semitransparent windows w | |||
| 492 | 492 | ||
| 493 | Opacity is applied to every surface of the window individually, so subsurfaces and pop-up menus will show window content behind them. | 493 | Opacity is applied to every surface of the window individually, so subsurfaces and pop-up menus will show window content behind them. |
| 494 | 494 | ||
| 495 | +Opacity can be toggled on or off for a window using the [`toggle-window-rule-opacity`](./Configuration:-Key-Bindings.md) action. | ||
| 496 | + | ||
| 495 |  | 497 |  |
| 496 | 498 | ||
| 497 | Also, focus ring and border with background will show through semitransparent windows (see `prefer-no-csd` and the `draw-border-with-background` window rule below). | 499 | Also, focus ring and border with background will show through semitransparent windows (see `prefer-no-csd` and the `draw-border-with-background` window rule below). |