Implement window closing animations
Browse files at this commitdd011f1012e10b1e3a1dbe100cb603a457bba12aParent: 301a2c012 changed files +683 -24
- niri-config/src/lib.rs +13 -0
- niri-visual-tests/src/test_window.rs +8 -2
- src/handlers/compositor.rs +11 -2
- src/handlers/xdg_shell.rs +6 -0
- src/layout/closing_window.rs +177 -0
- src/layout/mod.rs +46 -1
- src/layout/tile.rs +70 -3
- src/layout/workspace.rs +118 -6
- src/render_helpers/mod.rs +25 -0
- src/render_helpers/surface.rs +116 -0
- src/window/mapped.rs +70 -9
- wiki/Configuration:-Animations.md +23 -1
niri-config/src/lib.rsmodified+13 -0
| @@ -487,6 +487,8 @@ pub struct Animations { | |||
| 487 | pub window_movement: Animation, | 487 | pub window_movement: Animation, |
| 488 | #[knuffel(child, default = Animation::default_window_open())] | 488 | #[knuffel(child, default = Animation::default_window_open())] |
| 489 | pub window_open: Animation, | 489 | pub window_open: Animation, |
| 490 | + #[knuffel(child, default = Animation::default_window_close())] | ||
| 491 | + pub window_close: Animation, | ||
| 490 | #[knuffel(child, default = Animation::default_config_notification_open_close())] | 492 | #[knuffel(child, default = Animation::default_config_notification_open_close())] |
| 491 | pub config_notification_open_close: Animation, | 493 | pub config_notification_open_close: Animation, |
| 492 | } | 494 | } |
| @@ -500,6 +502,7 @@ impl Default for Animations { | |||
| 500 | horizontal_view_movement: Animation::default_horizontal_view_movement(), | 502 | horizontal_view_movement: Animation::default_horizontal_view_movement(), |
| 501 | window_movement: Animation::default_window_movement(), | 503 | window_movement: Animation::default_window_movement(), |
| 502 | window_open: Animation::default_window_open(), | 504 | window_open: Animation::default_window_open(), |
| 505 | + window_close: Animation::default_window_close(), | ||
| 503 | config_notification_open_close: Animation::default_config_notification_open_close(), | 506 | config_notification_open_close: Animation::default_config_notification_open_close(), |
| 504 | } | 507 | } |
| 505 | } | 508 | } |
| @@ -579,6 +582,16 @@ impl Animation { | |||
| 579 | }), | 582 | }), |
| 580 | } | 583 | } |
| 581 | } | 584 | } |
| 585 | + | ||
| 586 | + pub const fn default_window_close() -> Self { | ||
| 587 | + Self { | ||
| 588 | + off: false, | ||
| 589 | + kind: AnimationKind::Easing(EasingParams { | ||
| 590 | + duration_ms: Some(150), | ||
| 591 | + curve: Some(AnimationCurve::EaseOutQuad), | ||
| 592 | + }), | ||
| 593 | + } | ||
| 594 | + } | ||
| 582 | } | 595 | } |
| 583 | 596 | ||
| 584 | #[derive(Debug, Clone, Copy, PartialEq)] | 597 | #[derive(Debug, Clone, Copy, PartialEq)] |
niri-visual-tests/src/test_window.rsmodified+8 -2
| @@ -2,9 +2,11 @@ use std::cell::RefCell; | |||
| 2 | use std::cmp::{max, min}; | 2 | use std::cmp::{max, min}; |
| 3 | use std::rc::Rc; | 3 | use std::rc::Rc; |
| 4 | 4 | ||
| 5 | -use niri::layout::{LayoutElement, LayoutElementRenderElement}; | 5 | +use niri::layout::{ |
| 6 | + LayoutElement, LayoutElementRenderElement, LayoutElementSnapshotRenderElements, | ||
| 7 | +}; | ||
| 6 | use niri::render_helpers::renderer::NiriRenderer; | 8 | use niri::render_helpers::renderer::NiriRenderer; |
| 7 | -use niri::render_helpers::RenderTarget; | 9 | +use niri::render_helpers::{RenderSnapshot, RenderTarget}; |
| 8 | use niri::window::ResolvedWindowRules; | 10 | use niri::window::ResolvedWindowRules; |
| 9 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; | 11 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; |
| 10 | use smithay::backend::renderer::element::{Id, Kind}; | 12 | use smithay::backend::renderer::element::{Id, Kind}; |
| @@ -173,6 +175,10 @@ impl LayoutElement for TestWindow { | |||
| 173 | ] | 175 | ] |
| 174 | } | 176 | } |
| 175 | 177 | ||
| 178 | + fn take_last_render(&self) -> RenderSnapshot<LayoutElementSnapshotRenderElements> { | ||
| 179 | + RenderSnapshot::default() | ||
| 180 | + } | ||
| 181 | + | ||
| 176 | fn request_size(&self, size: Size<i32, Logical>) { | 182 | fn request_size(&self, size: Size<i32, Logical>) { |
| 177 | self.inner.borrow_mut().requested_size = Some(size); | 183 | self.inner.borrow_mut().requested_size = Some(size); |
| 178 | self.inner.borrow_mut().pending_fullscreen = false; | 184 | self.inner.borrow_mut().pending_fullscreen = false; |
src/handlers/compositor.rsmodified+11 -2
| @@ -187,8 +187,6 @@ impl CompositorHandler for State { | |||
| 187 | let window = mapped.window.clone(); | 187 | let window = mapped.window.clone(); |
| 188 | let output = output.clone(); | 188 | let output = output.clone(); |
| 189 | 189 | ||
| 190 | - window.on_commit(); | ||
| 191 | - | ||
| 192 | // This is a commit of a previously-mapped toplevel. | 190 | // This is a commit of a previously-mapped toplevel. |
| 193 | let is_mapped = | 191 | let is_mapped = |
| 194 | with_renderer_surface_state(surface, |state| state.buffer().is_some()) | 192 | with_renderer_surface_state(surface, |state| state.buffer().is_some()) |
| @@ -197,6 +195,17 @@ impl CompositorHandler for State { | |||
| 197 | false | 195 | false |
| 198 | }); | 196 | }); |
| 199 | 197 | ||
| 198 | + // Must start the close animation before window.on_commit(). | ||
| 199 | + if !is_mapped { | ||
| 200 | + self.backend.with_primary_renderer(|renderer| { | ||
| 201 | + self.niri | ||
| 202 | + .layout | ||
| 203 | + .start_close_animation_for_window(renderer, &window); | ||
| 204 | + }); | ||
| 205 | + } | ||
| 206 | + | ||
| 207 | + window.on_commit(); | ||
| 208 | + | ||
| 200 | if !is_mapped { | 209 | if !is_mapped { |
| 201 | // The toplevel got unmapped. | 210 | // The toplevel got unmapped. |
| 202 | // | 211 | // |
src/handlers/xdg_shell.rsmodified+6 -0
| @@ -382,6 +382,12 @@ impl XdgShellHandler for State { | |||
| 382 | let window = mapped.window.clone(); | 382 | let window = mapped.window.clone(); |
| 383 | let output = output.clone(); | 383 | let output = output.clone(); |
| 384 | 384 | ||
| 385 | + self.backend.with_primary_renderer(|renderer| { | ||
| 386 | + self.niri | ||
| 387 | + .layout | ||
| 388 | + .start_close_animation_for_window(renderer, &window); | ||
| 389 | + }); | ||
| 390 | + | ||
| 385 | let active_window = self.niri.layout.active_window().map(|(m, _)| &m.window); | 391 | let active_window = self.niri.layout.active_window().map(|(m, _)| &m.window); |
| 386 | let was_active = active_window == Some(&window); | 392 | let was_active = active_window == Some(&window); |
| 387 | 393 | ||
src/layout/closing_window.rsadded+177 -0
| @@ -0,0 +1,177 @@ | |||
| 1 | +use std::time::Duration; | ||
| 2 | + | ||
| 3 | +use anyhow::Context as _; | ||
| 4 | +use niri_config::BlockOutFrom; | ||
| 5 | +use smithay::backend::allocator::Fourcc; | ||
| 6 | +use smithay::backend::renderer::element::texture::{TextureBuffer, TextureRenderElement}; | ||
| 7 | +use smithay::backend::renderer::element::utils::{ | ||
| 8 | + Relocate, RelocateRenderElement, RescaleRenderElement, | ||
| 9 | +}; | ||
| 10 | +use smithay::backend::renderer::element::{Kind, RenderElement}; | ||
| 11 | +use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | ||
| 12 | +use smithay::utils::{Logical, Point, Scale, Transform}; | ||
| 13 | + | ||
| 14 | +use crate::animation::Animation; | ||
| 15 | +use crate::niri_render_elements; | ||
| 16 | +use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; | ||
| 17 | +use crate::render_helpers::{render_to_texture, RenderSnapshot, RenderTarget}; | ||
| 18 | + | ||
| 19 | +#[derive(Debug)] | ||
| 20 | +pub struct ClosingWindow { | ||
| 21 | + /// Contents of the window. | ||
| 22 | + buffer: TextureBuffer<GlesTexture>, | ||
| 23 | + | ||
| 24 | + /// Blocked-out contents of the window. | ||
| 25 | + blocked_out_buffer: TextureBuffer<GlesTexture>, | ||
| 26 | + | ||
| 27 | + /// Where the window should be blocked out from. | ||
| 28 | + block_out_from: Option<BlockOutFrom>, | ||
| 29 | + | ||
| 30 | + /// Center of the window geometry. | ||
| 31 | + center: Point<i32, Logical>, | ||
| 32 | + | ||
| 33 | + /// Position in the workspace. | ||
| 34 | + pos: Point<i32, Logical>, | ||
| 35 | + | ||
| 36 | + /// How much the buffer should be offset. | ||
| 37 | + buffer_offset: Point<i32, Logical>, | ||
| 38 | + | ||
| 39 | + /// How much the blocked-out buffer should be offset. | ||
| 40 | + blocked_out_buffer_offset: Point<i32, Logical>, | ||
| 41 | + | ||
| 42 | + /// The closing animation. | ||
| 43 | + anim: Animation, | ||
| 44 | + | ||
| 45 | + /// Alpha the animation should start from. | ||
| 46 | + starting_alpha: f32, | ||
| 47 | + | ||
| 48 | + /// Scale the animation should start from. | ||
| 49 | + starting_scale: f64, | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +niri_render_elements! { | ||
| 53 | + ClosingWindowRenderElement => { | ||
| 54 | + Texture = RelocateRenderElement<RescaleRenderElement<PrimaryGpuTextureRenderElement>>, | ||
| 55 | + } | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +impl ClosingWindow { | ||
| 59 | + #[allow(clippy::too_many_arguments)] | ||
| 60 | + pub fn new<E: RenderElement<GlesRenderer>>( | ||
| 61 | + renderer: &mut GlesRenderer, | ||
| 62 | + snapshot: RenderSnapshot<E>, | ||
| 63 | + scale: i32, | ||
| 64 | + center: Point<i32, Logical>, | ||
| 65 | + pos: Point<i32, Logical>, | ||
| 66 | + anim: Animation, | ||
| 67 | + starting_alpha: f32, | ||
| 68 | + starting_scale: f64, | ||
| 69 | + ) -> anyhow::Result<Self> { | ||
| 70 | + let _span = tracy_client::span!("ClosingWindow::new"); | ||
| 71 | + | ||
| 72 | + let mut render_to_buffer = |elements: Vec<E>| -> anyhow::Result<_> { | ||
| 73 | + let geo = elements | ||
| 74 | + .iter() | ||
| 75 | + .map(|ele| ele.geometry(Scale::from(f64::from(scale)))) | ||
| 76 | + .reduce(|a, b| a.merge(b)) | ||
| 77 | + .unwrap_or_default(); | ||
| 78 | + | ||
| 79 | + let elements = elements.iter().rev().map(|ele| { | ||
| 80 | + RelocateRenderElement::from_element( | ||
| 81 | + ele, | ||
| 82 | + (-geo.loc.x, -geo.loc.y), | ||
| 83 | + Relocate::Relative, | ||
| 84 | + ) | ||
| 85 | + }); | ||
| 86 | + let offset = geo.loc.to_logical(scale); | ||
| 87 | + | ||
| 88 | + let (texture, _sync_point) = render_to_texture( | ||
| 89 | + renderer, | ||
| 90 | + geo.size, | ||
| 91 | + Scale::from(scale as f64), | ||
| 92 | + Transform::Normal, | ||
| 93 | + Fourcc::Abgr8888, | ||
| 94 | + elements, | ||
| 95 | + ) | ||
| 96 | + .context("error rendering to texture")?; | ||
| 97 | + | ||
| 98 | + let buffer = | ||
| 99 | + TextureBuffer::from_texture(renderer, texture, scale, Transform::Normal, None); | ||
| 100 | + Ok((buffer, offset)) | ||
| 101 | + }; | ||
| 102 | + | ||
| 103 | + let (buffer, buffer_offset) = | ||
| 104 | + render_to_buffer(snapshot.contents).context("error rendering contents")?; | ||
| 105 | + let (blocked_out_buffer, blocked_out_buffer_offset) = | ||
| 106 | + render_to_buffer(snapshot.blocked_out_contents) | ||
| 107 | + .context("error rendering blocked-out contents")?; | ||
| 108 | + | ||
| 109 | + Ok(Self { | ||
| 110 | + buffer, | ||
| 111 | + blocked_out_buffer, | ||
| 112 | + block_out_from: snapshot.block_out_from, | ||
| 113 | + center, | ||
| 114 | + pos, | ||
| 115 | + buffer_offset, | ||
| 116 | + blocked_out_buffer_offset, | ||
| 117 | + anim, | ||
| 118 | + starting_alpha, | ||
| 119 | + starting_scale, | ||
| 120 | + }) | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + pub fn advance_animations(&mut self, current_time: Duration) { | ||
| 124 | + self.anim.set_current_time(current_time); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + pub fn are_animations_ongoing(&self) -> bool { | ||
| 128 | + !self.anim.is_done() | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + pub fn render( | ||
| 132 | + &self, | ||
| 133 | + view_pos: i32, | ||
| 134 | + scale: Scale<f64>, | ||
| 135 | + target: RenderTarget, | ||
| 136 | + ) -> ClosingWindowRenderElement { | ||
| 137 | + let val = self.anim.value(); | ||
| 138 | + | ||
| 139 | + let block_out = match self.block_out_from { | ||
| 140 | + None => false, | ||
| 141 | + Some(BlockOutFrom::Screencast) => target == RenderTarget::Screencast, | ||
| 142 | + Some(BlockOutFrom::ScreenCapture) => target != RenderTarget::Output, | ||
| 143 | + }; | ||
| 144 | + let (buffer, offset) = if block_out { | ||
| 145 | + (&self.blocked_out_buffer, self.blocked_out_buffer_offset) | ||
| 146 | + } else { | ||
| 147 | + (&self.buffer, self.buffer_offset) | ||
| 148 | + }; | ||
| 149 | + | ||
| 150 | + let elem = TextureRenderElement::from_texture_buffer( | ||
| 151 | + Point::from((0., 0.)), | ||
| 152 | + buffer, | ||
| 153 | + Some(val.clamp(0., 1.) as f32 * self.starting_alpha), | ||
| 154 | + None, | ||
| 155 | + None, | ||
| 156 | + Kind::Unspecified, | ||
| 157 | + ); | ||
| 158 | + | ||
| 159 | + let elem = PrimaryGpuTextureRenderElement(elem); | ||
| 160 | + | ||
| 161 | + let elem = RescaleRenderElement::from_element( | ||
| 162 | + elem, | ||
| 163 | + (self.center - offset).to_physical_precise_round(scale), | ||
| 164 | + ((val / 5. + 0.8) * self.starting_scale).max(0.), | ||
| 165 | + ); | ||
| 166 | + | ||
| 167 | + let mut location = self.pos + offset; | ||
| 168 | + location.x -= view_pos; | ||
| 169 | + let elem = RelocateRenderElement::from_element( | ||
| 170 | + elem, | ||
| 171 | + location.to_physical_precise_round(scale), | ||
| 172 | + Relocate::Relative, | ||
| 173 | + ); | ||
| 174 | + | ||
| 175 | + elem.into() | ||
| 176 | + } | ||
| 177 | +} | ||
src/layout/mod.rsmodified+46 -1
| @@ -39,6 +39,7 @@ use niri_ipc::SizeChange; | |||
| 39 | use smithay::backend::renderer::element::solid::SolidColorRenderElement; | 39 | use smithay::backend::renderer::element::solid::SolidColorRenderElement; |
| 40 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; | 40 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; |
| 41 | use smithay::backend::renderer::element::Id; | 41 | use smithay::backend::renderer::element::Id; |
| 42 | +use smithay::backend::renderer::gles::GlesRenderer; | ||
| 42 | use smithay::output::Output; | 43 | use smithay::output::Output; |
| 43 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 44 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 44 | use smithay::utils::{Logical, Point, Scale, Size, Transform}; | 45 | use smithay::utils::{Logical, Point, Scale, Size, Transform}; |
| @@ -47,11 +48,13 @@ use self::monitor::Monitor; | |||
| 47 | pub use self::monitor::MonitorRenderElement; | 48 | pub use self::monitor::MonitorRenderElement; |
| 48 | use self::workspace::{compute_working_area, Column, ColumnWidth, OutputId, Workspace}; | 49 | use self::workspace::{compute_working_area, Column, ColumnWidth, OutputId, Workspace}; |
| 49 | use crate::niri_render_elements; | 50 | use crate::niri_render_elements; |
| 51 | +use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; | ||
| 50 | use crate::render_helpers::renderer::NiriRenderer; | 52 | use crate::render_helpers::renderer::NiriRenderer; |
| 51 | -use crate::render_helpers::RenderTarget; | 53 | +use crate::render_helpers::{RenderSnapshot, RenderTarget}; |
| 52 | use crate::utils::output_size; | 54 | use crate::utils::output_size; |
| 53 | use crate::window::ResolvedWindowRules; | 55 | use crate::window::ResolvedWindowRules; |
| 54 | 56 | ||
| 57 | +pub mod closing_window; | ||
| 55 | pub mod focus_ring; | 58 | pub mod focus_ring; |
| 56 | pub mod monitor; | 59 | pub mod monitor; |
| 57 | pub mod tile; | 60 | pub mod tile; |
| @@ -64,6 +67,13 @@ niri_render_elements! { | |||
| 64 | } | 67 | } |
| 65 | } | 68 | } |
| 66 | 69 | ||
| 70 | +niri_render_elements! { | ||
| 71 | + LayoutElementSnapshotRenderElements => { | ||
| 72 | + Texture = PrimaryGpuTextureRenderElement, | ||
| 73 | + SolidColor = SolidColorRenderElement, | ||
| 74 | + } | ||
| 75 | +} | ||
| 76 | + | ||
| 67 | pub trait LayoutElement { | 77 | pub trait LayoutElement { |
| 68 | /// Type that can be used as a unique ID of this element. | 78 | /// Type that can be used as a unique ID of this element. |
| 69 | type Id: PartialEq; | 79 | type Id: PartialEq; |
| @@ -100,6 +110,8 @@ pub trait LayoutElement { | |||
| 100 | target: RenderTarget, | 110 | target: RenderTarget, |
| 101 | ) -> Vec<LayoutElementRenderElement<R>>; | 111 | ) -> Vec<LayoutElementRenderElement<R>>; |
| 102 | 112 | ||
| 113 | + fn take_last_render(&self) -> RenderSnapshot<LayoutElementSnapshotRenderElements>; | ||
| 114 | + | ||
| 103 | fn request_size(&self, size: Size<i32, Logical>); | 115 | fn request_size(&self, size: Size<i32, Logical>); |
| 104 | fn request_fullscreen(&self, size: Size<i32, Logical>); | 116 | fn request_fullscreen(&self, size: Size<i32, Logical>); |
| 105 | fn min_size(&self) -> Size<i32, Logical>; | 117 | fn min_size(&self) -> Size<i32, Logical>; |
| @@ -1750,6 +1762,35 @@ impl<W: LayoutElement> Layout<W> { | |||
| 1750 | } | 1762 | } |
| 1751 | } | 1763 | } |
| 1752 | 1764 | ||
| 1765 | + pub fn start_close_animation_for_window( | ||
| 1766 | + &mut self, | ||
| 1767 | + renderer: &mut GlesRenderer, | ||
| 1768 | + window: &W::Id, | ||
| 1769 | + ) { | ||
| 1770 | + let _span = tracy_client::span!("Layout::start_close_animation_for_window"); | ||
| 1771 | + | ||
| 1772 | + match &mut self.monitor_set { | ||
| 1773 | + MonitorSet::Normal { monitors, .. } => { | ||
| 1774 | + for mon in monitors { | ||
| 1775 | + for ws in &mut mon.workspaces { | ||
| 1776 | + if ws.has_window(window) { | ||
| 1777 | + ws.start_close_animation_for_window(renderer, window); | ||
| 1778 | + return; | ||
| 1779 | + } | ||
| 1780 | + } | ||
| 1781 | + } | ||
| 1782 | + } | ||
| 1783 | + MonitorSet::NoOutputs { workspaces, .. } => { | ||
| 1784 | + for ws in workspaces { | ||
| 1785 | + if ws.has_window(window) { | ||
| 1786 | + ws.start_close_animation_for_window(renderer, window); | ||
| 1787 | + return; | ||
| 1788 | + } | ||
| 1789 | + } | ||
| 1790 | + } | ||
| 1791 | + } | ||
| 1792 | + } | ||
| 1793 | + | ||
| 1753 | pub fn refresh(&mut self) { | 1794 | pub fn refresh(&mut self) { |
| 1754 | let _span = tracy_client::span!("Layout::refresh"); | 1795 | let _span = tracy_client::span!("Layout::refresh"); |
| 1755 | 1796 | ||
| @@ -1889,6 +1930,10 @@ mod tests { | |||
| 1889 | vec![] | 1930 | vec![] |
| 1890 | } | 1931 | } |
| 1891 | 1932 | ||
| 1933 | + fn take_last_render(&self) -> RenderSnapshot<LayoutElementSnapshotRenderElements> { | ||
| 1934 | + RenderSnapshot::default() | ||
| 1935 | + } | ||
| 1936 | + | ||
| 1892 | fn request_size(&self, size: Size<i32, Logical>) { | 1937 | fn request_size(&self, size: Size<i32, Logical>) { |
| 1893 | self.0.requested_size.set(Some(size)); | 1938 | self.0.requested_size.set(Some(size)); |
| 1894 | self.0.pending_fullscreen.set(false); | 1939 | self.0.pending_fullscreen.set(false); |
src/layout/tile.rsmodified+70 -3
| @@ -3,17 +3,22 @@ use std::rc::Rc; | |||
| 3 | use std::time::Duration; | 3 | use std::time::Duration; |
| 4 | 4 | ||
| 5 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; | 5 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; |
| 6 | -use smithay::backend::renderer::element::utils::RescaleRenderElement; | 6 | +use smithay::backend::renderer::element::utils::{ |
| 7 | + Relocate, RelocateRenderElement, RescaleRenderElement, | ||
| 8 | +}; | ||
| 7 | use smithay::backend::renderer::element::{Element, Kind}; | 9 | use smithay::backend::renderer::element::{Element, Kind}; |
| 10 | +use smithay::backend::renderer::gles::GlesRenderer; | ||
| 8 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size}; | 11 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size}; |
| 9 | 12 | ||
| 10 | use super::focus_ring::{FocusRing, FocusRingRenderElement}; | 13 | use super::focus_ring::{FocusRing, FocusRingRenderElement}; |
| 11 | -use super::{LayoutElement, LayoutElementRenderElement, Options}; | 14 | +use super::{ |
| 15 | + LayoutElement, LayoutElementRenderElement, LayoutElementSnapshotRenderElements, Options, | ||
| 16 | +}; | ||
| 12 | use crate::animation::Animation; | 17 | use crate::animation::Animation; |
| 13 | use crate::niri_render_elements; | 18 | use crate::niri_render_elements; |
| 14 | use crate::render_helpers::offscreen::OffscreenRenderElement; | 19 | use crate::render_helpers::offscreen::OffscreenRenderElement; |
| 15 | use crate::render_helpers::renderer::NiriRenderer; | 20 | use crate::render_helpers::renderer::NiriRenderer; |
| 16 | -use crate::render_helpers::RenderTarget; | 21 | +use crate::render_helpers::{RenderSnapshot, RenderTarget}; |
| 17 | 22 | ||
| 18 | /// Toplevel window with decorations. | 23 | /// Toplevel window with decorations. |
| 19 | #[derive(Debug)] | 24 | #[derive(Debug)] |
| @@ -58,6 +63,14 @@ niri_render_elements! { | |||
| 58 | } | 63 | } |
| 59 | } | 64 | } |
| 60 | 65 | ||
| 66 | +niri_render_elements! { | ||
| 67 | + TileSnapshotRenderElement => { | ||
| 68 | + LayoutElement = RelocateRenderElement<LayoutElementSnapshotRenderElements>, | ||
| 69 | + FocusRing = FocusRingRenderElement, | ||
| 70 | + SolidColor = SolidColorRenderElement, | ||
| 71 | + } | ||
| 72 | +} | ||
| 73 | + | ||
| 61 | impl<W: LayoutElement> Tile<W> { | 74 | impl<W: LayoutElement> Tile<W> { |
| 62 | pub fn new(window: W, options: Rc<Options>) -> Self { | 75 | pub fn new(window: W, options: Rc<Options>) -> Self { |
| 63 | Self { | 76 | Self { |
| @@ -129,6 +142,10 @@ impl<W: LayoutElement> Tile<W> { | |||
| 129 | )); | 142 | )); |
| 130 | } | 143 | } |
| 131 | 144 | ||
| 145 | + pub fn open_animation(&self) -> &Option<Animation> { | ||
| 146 | + &self.open_animation | ||
| 147 | + } | ||
| 148 | + | ||
| 132 | pub fn window(&self) -> &W { | 149 | pub fn window(&self) -> &W { |
| 133 | &self.window | 150 | &self.window |
| 134 | } | 151 | } |
| @@ -399,4 +416,54 @@ impl<W: LayoutElement> Tile<W> { | |||
| 399 | None.into_iter().chain(Some(elements).into_iter().flatten()) | 416 | None.into_iter().chain(Some(elements).into_iter().flatten()) |
| 400 | } | 417 | } |
| 401 | } | 418 | } |
| 419 | + | ||
| 420 | + pub fn take_snapshot_for_close_anim( | ||
| 421 | + &self, | ||
| 422 | + renderer: &mut GlesRenderer, | ||
| 423 | + scale: Scale<f64>, | ||
| 424 | + view_size: Size<i32, Logical>, | ||
| 425 | + ) -> RenderSnapshot<TileSnapshotRenderElement> { | ||
| 426 | + let snapshot = self.window.take_last_render(); | ||
| 427 | + if snapshot.contents.is_empty() { | ||
| 428 | + return RenderSnapshot::default(); | ||
| 429 | + } | ||
| 430 | + | ||
| 431 | + let mut process = |contents| { | ||
| 432 | + let mut rv = vec![]; | ||
| 433 | + | ||
| 434 | + let buf_pos = | ||
| 435 | + (self.window_loc() + self.window.buf_loc()).to_physical_precise_round(scale); | ||
| 436 | + for elem in contents { | ||
| 437 | + let elem = RelocateRenderElement::from_element(elem, buf_pos, Relocate::Relative); | ||
| 438 | + rv.push(elem.into()); | ||
| 439 | + } | ||
| 440 | + | ||
| 441 | + if let Some(width) = self.effective_border_width() { | ||
| 442 | + rv.extend( | ||
| 443 | + self.border | ||
| 444 | + .render(renderer, Point::from((width, width)), scale, view_size) | ||
| 445 | + .map(Into::into), | ||
| 446 | + ); | ||
| 447 | + } | ||
| 448 | + | ||
| 449 | + if self.is_fullscreen { | ||
| 450 | + let elem = SolidColorRenderElement::from_buffer( | ||
| 451 | + &self.fullscreen_backdrop, | ||
| 452 | + Point::from((0, 0)), | ||
| 453 | + scale, | ||
| 454 | + 1., | ||
| 455 | + Kind::Unspecified, | ||
| 456 | + ); | ||
| 457 | + rv.push(elem.into()); | ||
| 458 | + } | ||
| 459 | + | ||
| 460 | + rv | ||
| 461 | + }; | ||
| 462 | + | ||
| 463 | + RenderSnapshot { | ||
| 464 | + contents: process(snapshot.contents), | ||
| 465 | + blocked_out_contents: process(snapshot.blocked_out_contents), | ||
| 466 | + block_out_from: snapshot.block_out_from, | ||
| 467 | + } | ||
| 468 | + } | ||
| 402 | } | 469 | } |
src/layout/workspace.rsmodified+118 -6
| @@ -5,6 +5,7 @@ use std::time::Duration; | |||
| 5 | 5 | ||
| 6 | use niri_config::{CenterFocusedColumn, PresetWidth, Struts}; | 6 | use niri_config::{CenterFocusedColumn, PresetWidth, Struts}; |
| 7 | use niri_ipc::SizeChange; | 7 | use niri_ipc::SizeChange; |
| 8 | +use smithay::backend::renderer::gles::GlesRenderer; | ||
| 8 | use smithay::desktop::{layer_map_for_output, Window}; | 9 | use smithay::desktop::{layer_map_for_output, Window}; |
| 9 | use smithay::output::Output; | 10 | use smithay::output::Output; |
| 10 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | 11 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| @@ -12,6 +13,7 @@ use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | |||
| 12 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size}; | 13 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size}; |
| 13 | use smithay::wayland::compositor::send_surface_state; | 14 | use smithay::wayland::compositor::send_surface_state; |
| 14 | 15 | ||
| 16 | +use super::closing_window::{ClosingWindow, ClosingWindowRenderElement}; | ||
| 15 | use super::tile::{Tile, TileRenderElement}; | 17 | use super::tile::{Tile, TileRenderElement}; |
| 16 | use super::{LayoutElement, Options}; | 18 | use super::{LayoutElement, Options}; |
| 17 | use crate::animation::Animation; | 19 | use crate::animation::Animation; |
| @@ -76,6 +78,9 @@ pub struct Workspace<W: LayoutElement> { | |||
| 76 | /// The value is the view offset that the previous column had before, to restore it. | 78 | /// The value is the view offset that the previous column had before, to restore it. |
| 77 | activate_prev_column_on_removal: Option<i32>, | 79 | activate_prev_column_on_removal: Option<i32>, |
| 78 | 80 | ||
| 81 | + /// Windows in the closing animation. | ||
| 82 | + closing_windows: Vec<ClosingWindow>, | ||
| 83 | + | ||
| 79 | /// Configurable properties of the layout. | 84 | /// Configurable properties of the layout. |
| 80 | pub options: Rc<Options>, | 85 | pub options: Rc<Options>, |
| 81 | 86 | ||
| @@ -100,6 +105,7 @@ impl WorkspaceId { | |||
| 100 | niri_render_elements! { | 105 | niri_render_elements! { |
| 101 | WorkspaceRenderElement<R> => { | 106 | WorkspaceRenderElement<R> => { |
| 102 | Tile = TileRenderElement<R>, | 107 | Tile = TileRenderElement<R>, |
| 108 | + ClosingWindow = ClosingWindowRenderElement, | ||
| 103 | } | 109 | } |
| 104 | } | 110 | } |
| 105 | 111 | ||
| @@ -243,6 +249,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 243 | view_offset: 0, | 249 | view_offset: 0, |
| 244 | view_offset_adj: None, | 250 | view_offset_adj: None, |
| 245 | activate_prev_column_on_removal: None, | 251 | activate_prev_column_on_removal: None, |
| 252 | + closing_windows: vec![], | ||
| 246 | options, | 253 | options, |
| 247 | id: WorkspaceId::next(), | 254 | id: WorkspaceId::next(), |
| 248 | } | 255 | } |
| @@ -259,6 +266,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 259 | view_offset: 0, | 266 | view_offset: 0, |
| 260 | view_offset_adj: None, | 267 | view_offset_adj: None, |
| 261 | activate_prev_column_on_removal: None, | 268 | activate_prev_column_on_removal: None, |
| 269 | + closing_windows: vec![], | ||
| 262 | options, | 270 | options, |
| 263 | id: WorkspaceId::next(), | 271 | id: WorkspaceId::next(), |
| 264 | } | 272 | } |
| @@ -283,10 +291,17 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 283 | let is_active = is_active && col_idx == self.active_column_idx; | 291 | let is_active = is_active && col_idx == self.active_column_idx; |
| 284 | col.advance_animations(current_time, is_active); | 292 | col.advance_animations(current_time, is_active); |
| 285 | } | 293 | } |
| 294 | + | ||
| 295 | + self.closing_windows.retain_mut(|closing| { | ||
| 296 | + closing.advance_animations(current_time); | ||
| 297 | + closing.are_animations_ongoing() | ||
| 298 | + }); | ||
| 286 | } | 299 | } |
| 287 | 300 | ||
| 288 | pub fn are_animations_ongoing(&self) -> bool { | 301 | pub fn are_animations_ongoing(&self) -> bool { |
| 289 | - self.view_offset_adj.is_some() || self.columns.iter().any(Column::are_animations_ongoing) | 302 | + self.view_offset_adj.is_some() |
| 303 | + || self.columns.iter().any(Column::are_animations_ongoing) | ||
| 304 | + || !self.closing_windows.is_empty() | ||
| 290 | } | 305 | } |
| 291 | 306 | ||
| 292 | pub fn update_config(&mut self, options: Rc<Options>) { | 307 | pub fn update_config(&mut self, options: Rc<Options>) { |
| @@ -897,6 +912,97 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 897 | self.activate_column(column_idx); | 912 | self.activate_column(column_idx); |
| 898 | } | 913 | } |
| 899 | 914 | ||
| 915 | + pub fn start_close_animation_for_window( | ||
| 916 | + &mut self, | ||
| 917 | + renderer: &mut GlesRenderer, | ||
| 918 | + window: &W::Id, | ||
| 919 | + ) { | ||
| 920 | + let (tile, mut tile_pos) = self | ||
| 921 | + .tiles_in_render_order() | ||
| 922 | + .find(|(tile, _)| tile.window().id() == window) | ||
| 923 | + .unwrap(); | ||
| 924 | + | ||
| 925 | + // FIXME: workspaces should probably cache their last used scale so they can be correctly | ||
| 926 | + // rendered even with no outputs connected. | ||
| 927 | + let output_scale = self | ||
| 928 | + .output | ||
| 929 | + .as_ref() | ||
| 930 | + .map(|o| Scale::from(o.current_scale().fractional_scale())) | ||
| 931 | + .unwrap_or(Scale::from(1.)); | ||
| 932 | + | ||
| 933 | + let snapshot = tile.take_snapshot_for_close_anim(renderer, output_scale, self.view_size); | ||
| 934 | + if snapshot.contents.is_empty() { | ||
| 935 | + return; | ||
| 936 | + }; | ||
| 937 | + | ||
| 938 | + let col_idx = self | ||
| 939 | + .columns | ||
| 940 | + .iter() | ||
| 941 | + .position(|col| col.contains(window)) | ||
| 942 | + .unwrap(); | ||
| 943 | + | ||
| 944 | + let removing_last = self.columns[col_idx].tiles.len() == 1; | ||
| 945 | + let offset = self.column_x(col_idx + 1) - self.column_x(col_idx); | ||
| 946 | + | ||
| 947 | + let mut center = Point::from((0, 0)); | ||
| 948 | + center.x += tile.tile_size().w / 2; | ||
| 949 | + center.y += tile.tile_size().h / 2; | ||
| 950 | + | ||
| 951 | + tile_pos.x += self.view_pos(); | ||
| 952 | + | ||
| 953 | + if col_idx < self.active_column_idx && removing_last { | ||
| 954 | + tile_pos.x -= offset; | ||
| 955 | + } | ||
| 956 | + | ||
| 957 | + // FIXME: this is a bit cursed since it's relying on Tile's internal details. | ||
| 958 | + let (starting_alpha, starting_scale) = if let Some(anim) = tile.open_animation() { | ||
| 959 | + let val = anim.value(); | ||
| 960 | + (val.clamp(0., 1.) as f32, (val / 2. + 0.5).max(0.)) | ||
| 961 | + } else { | ||
| 962 | + (1., 1.) | ||
| 963 | + }; | ||
| 964 | + | ||
| 965 | + let anim = Animation::new( | ||
| 966 | + 1., | ||
| 967 | + 0., | ||
| 968 | + 0., | ||
| 969 | + self.options.animations.window_close, | ||
| 970 | + niri_config::Animation::default_window_close(), | ||
| 971 | + ); | ||
| 972 | + | ||
| 973 | + let res = ClosingWindow::new( | ||
| 974 | + renderer, | ||
| 975 | + snapshot, | ||
| 976 | + output_scale.x as i32, | ||
| 977 | + center, | ||
| 978 | + tile_pos, | ||
| 979 | + anim, | ||
| 980 | + starting_alpha, | ||
| 981 | + starting_scale, | ||
| 982 | + ); | ||
| 983 | + match res { | ||
| 984 | + Ok(closing) => { | ||
| 985 | + self.closing_windows.push(closing); | ||
| 986 | + } | ||
| 987 | + Err(err) => { | ||
| 988 | + warn!("error creating a closing window animation: {err:?}"); | ||
| 989 | + } | ||
| 990 | + } | ||
| 991 | + | ||
| 992 | + // Also move the other columns. | ||
| 993 | + if removing_last { | ||
| 994 | + if self.active_column_idx <= col_idx { | ||
| 995 | + for col in &mut self.columns[col_idx + 1..] { | ||
| 996 | + col.animate_move_from(offset); | ||
| 997 | + } | ||
| 998 | + } else { | ||
| 999 | + for col in &mut self.columns[..col_idx] { | ||
| 1000 | + col.animate_move_from(-offset); | ||
| 1001 | + } | ||
| 1002 | + } | ||
| 1003 | + } | ||
| 1004 | + } | ||
| 1005 | + | ||
| 900 | #[cfg(test)] | 1006 | #[cfg(test)] |
| 901 | pub fn verify_invariants(&self) { | 1007 | pub fn verify_invariants(&self) { |
| 902 | assert!(self.view_size.w > 0); | 1008 | assert!(self.view_size.w > 0); |
| @@ -1354,10 +1460,6 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1354 | renderer: &mut R, | 1460 | renderer: &mut R, |
| 1355 | target: RenderTarget, | 1461 | target: RenderTarget, |
| 1356 | ) -> Vec<WorkspaceRenderElement<R>> { | 1462 | ) -> Vec<WorkspaceRenderElement<R>> { |
| 1357 | - if self.columns.is_empty() { | ||
| 1358 | - return vec![]; | ||
| 1359 | - } | ||
| 1360 | - | ||
| 1361 | // FIXME: workspaces should probably cache their last used scale so they can be correctly | 1463 | // FIXME: workspaces should probably cache their last used scale so they can be correctly |
| 1362 | // rendered even with no outputs connected. | 1464 | // rendered even with no outputs connected. |
| 1363 | let output_scale = self | 1465 | let output_scale = self |
| @@ -1367,8 +1469,18 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1367 | .unwrap_or(Scale::from(1.)); | 1469 | .unwrap_or(Scale::from(1.)); |
| 1368 | 1470 | ||
| 1369 | let mut rv = vec![]; | 1471 | let mut rv = vec![]; |
| 1370 | - let mut first = true; | ||
| 1371 | 1472 | ||
| 1473 | + // Draw the closing windows on top. | ||
| 1474 | + let view_pos = self.view_pos(); | ||
| 1475 | + for closing in &self.closing_windows { | ||
| 1476 | + rv.push(closing.render(view_pos, output_scale, target).into()); | ||
| 1477 | + } | ||
| 1478 | + | ||
| 1479 | + if self.columns.is_empty() { | ||
| 1480 | + return rv; | ||
| 1481 | + } | ||
| 1482 | + | ||
| 1483 | + let mut first = true; | ||
| 1372 | for (tile, tile_pos) in self.tiles_in_render_order() { | 1484 | for (tile, tile_pos) in self.tiles_in_render_order() { |
| 1373 | // For the active tile (which comes first), draw the focus ring. | 1485 | // For the active tile (which comes first), draw the focus ring. |
| 1374 | let focus_ring = first; | 1486 | let focus_ring = first; |
src/render_helpers/mod.rsmodified+25 -0
| @@ -1,6 +1,7 @@ | |||
| 1 | use std::ptr; | 1 | use std::ptr; |
| 2 | 2 | ||
| 3 | use anyhow::{ensure, Context}; | 3 | use anyhow::{ensure, Context}; |
| 4 | +use niri_config::BlockOutFrom; | ||
| 4 | use smithay::backend::allocator::Fourcc; | 5 | use smithay::backend::allocator::Fourcc; |
| 5 | use smithay::backend::renderer::element::RenderElement; | 6 | use smithay::backend::renderer::element::RenderElement; |
| 6 | use smithay::backend::renderer::gles::{GlesMapping, GlesRenderer, GlesTexture}; | 7 | use smithay::backend::renderer::gles::{GlesMapping, GlesRenderer, GlesTexture}; |
| @@ -18,6 +19,7 @@ pub mod primary_gpu_texture; | |||
| 18 | pub mod render_elements; | 19 | pub mod render_elements; |
| 19 | pub mod renderer; | 20 | pub mod renderer; |
| 20 | pub mod shaders; | 21 | pub mod shaders; |
| 22 | +pub mod surface; | ||
| 21 | 23 | ||
| 22 | /// What we're rendering for. | 24 | /// What we're rendering for. |
| 23 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 25 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -30,6 +32,29 @@ pub enum RenderTarget { | |||
| 30 | ScreenCapture, | 32 | ScreenCapture, |
| 31 | } | 33 | } |
| 32 | 34 | ||
| 35 | +/// Snapshot of a render. | ||
| 36 | +#[derive(Debug)] | ||
| 37 | +pub struct RenderSnapshot<E> { | ||
| 38 | + /// Contents for a normal render. | ||
| 39 | + pub contents: Vec<E>, | ||
| 40 | + | ||
| 41 | + /// Blocked-out contents. | ||
| 42 | + pub blocked_out_contents: Vec<E>, | ||
| 43 | + | ||
| 44 | + /// Where the contents were blocked out from at the time of the snapshot. | ||
| 45 | + pub block_out_from: Option<BlockOutFrom>, | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +impl<E> Default for RenderSnapshot<E> { | ||
| 49 | + fn default() -> Self { | ||
| 50 | + Self { | ||
| 51 | + contents: Default::default(), | ||
| 52 | + blocked_out_contents: Default::default(), | ||
| 53 | + block_out_from: Default::default(), | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | +} | ||
| 57 | + | ||
| 33 | pub fn render_to_texture( | 58 | pub fn render_to_texture( |
| 34 | renderer: &mut GlesRenderer, | 59 | renderer: &mut GlesRenderer, |
| 35 | size: Size<i32, Physical>, | 60 | size: Size<i32, Physical>, |
src/render_helpers/surface.rsadded+116 -0
| @@ -0,0 +1,116 @@ | |||
| 1 | +use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; | ||
| 2 | +use smithay::backend::renderer::element::texture::{TextureBuffer, TextureRenderElement}; | ||
| 3 | +use smithay::backend::renderer::element::Kind; | ||
| 4 | +use smithay::backend::renderer::gles::GlesRenderer; | ||
| 5 | +use smithay::backend::renderer::utils::{import_surface, RendererSurfaceStateUserData}; | ||
| 6 | +use smithay::backend::renderer::Renderer as _; | ||
| 7 | +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | ||
| 8 | +use smithay::utils::{Physical, Point, Scale}; | ||
| 9 | +use smithay::wayland::compositor::{with_surface_tree_downward, TraversalAction}; | ||
| 10 | + | ||
| 11 | +use super::primary_gpu_texture::PrimaryGpuTextureRenderElement; | ||
| 12 | +use super::renderer::NiriRenderer; | ||
| 13 | +use crate::layout::{LayoutElementRenderElement, LayoutElementSnapshotRenderElements}; | ||
| 14 | + | ||
| 15 | +/// Renders elements from a surface tree, as well as saves them as textures into `storage`. | ||
| 16 | +/// | ||
| 17 | +/// Saved textures are based at (0, 0) to facilitate later offscreening. This is why the location | ||
| 18 | +/// argument is split into `location` and `offset`: the former is ignored for saved textures, but | ||
| 19 | +/// the latter isn't (for things like popups). | ||
| 20 | +#[allow(clippy::too_many_arguments)] | ||
| 21 | +pub fn render_and_save_from_surface_tree<R: NiriRenderer>( | ||
| 22 | + renderer: &mut R, | ||
| 23 | + surface: &WlSurface, | ||
| 24 | + location: Point<f64, Physical>, | ||
| 25 | + offset: Point<f64, Physical>, | ||
| 26 | + scale: Scale<f64>, | ||
| 27 | + alpha: f32, | ||
| 28 | + kind: Kind, | ||
| 29 | + elements: &mut Vec<LayoutElementRenderElement<R>>, | ||
| 30 | + storage: &mut Option<&mut Vec<LayoutElementSnapshotRenderElements>>, | ||
| 31 | +) { | ||
| 32 | + let _span = tracy_client::span!("render_and_save_from_surface_tree"); | ||
| 33 | + | ||
| 34 | + let base_pos = location; | ||
| 35 | + | ||
| 36 | + with_surface_tree_downward( | ||
| 37 | + surface, | ||
| 38 | + location + offset, | ||
| 39 | + |_, states, location| { | ||
| 40 | + let mut location = *location; | ||
| 41 | + let data = states.data_map.get::<RendererSurfaceStateUserData>(); | ||
| 42 | + | ||
| 43 | + if let Some(data) = data { | ||
| 44 | + let data = &*data.borrow(); | ||
| 45 | + | ||
| 46 | + if let Some(view) = data.view() { | ||
| 47 | + location += view.offset.to_f64().to_physical(scale); | ||
| 48 | + TraversalAction::DoChildren(location) | ||
| 49 | + } else { | ||
| 50 | + TraversalAction::SkipChildren | ||
| 51 | + } | ||
| 52 | + } else { | ||
| 53 | + TraversalAction::SkipChildren | ||
| 54 | + } | ||
| 55 | + }, | ||
| 56 | + |surface, states, location| { | ||
| 57 | + let mut location = *location; | ||
| 58 | + let data = states.data_map.get::<RendererSurfaceStateUserData>(); | ||
| 59 | + | ||
| 60 | + if let Some(data) = data { | ||
| 61 | + if let Some(view) = data.borrow().view() { | ||
| 62 | + location += view.offset.to_f64().to_physical(scale); | ||
| 63 | + } else { | ||
| 64 | + return; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + let elem = match WaylandSurfaceRenderElement::from_surface( | ||
| 68 | + renderer, surface, states, location, alpha, kind, | ||
| 69 | + ) { | ||
| 70 | + Ok(elem) => elem, | ||
| 71 | + Err(err) => { | ||
| 72 | + warn!("failed to import surface: {err:?}"); | ||
| 73 | + return; | ||
| 74 | + } | ||
| 75 | + }; | ||
| 76 | + | ||
| 77 | + elements.push(elem.into()); | ||
| 78 | + | ||
| 79 | + if let Some(storage) = storage { | ||
| 80 | + let renderer = renderer.as_gles_renderer(); | ||
| 81 | + // FIXME (possibly in Smithay): this causes a re-upload for shm textures. | ||
| 82 | + if let Err(err) = import_surface(renderer, states) { | ||
| 83 | + warn!("failed to import surface: {err:?}"); | ||
| 84 | + return; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + let data = data.borrow(); | ||
| 88 | + let view = data.view().unwrap(); | ||
| 89 | + let Some(texture) = data.texture::<GlesRenderer>(renderer.id()) else { | ||
| 90 | + return; | ||
| 91 | + }; | ||
| 92 | + | ||
| 93 | + let buffer = TextureBuffer::from_texture( | ||
| 94 | + renderer, | ||
| 95 | + texture.clone(), | ||
| 96 | + data.buffer_scale(), | ||
| 97 | + data.buffer_transform(), | ||
| 98 | + None, | ||
| 99 | + ); | ||
| 100 | + | ||
| 101 | + let elem = TextureRenderElement::from_texture_buffer( | ||
| 102 | + location - base_pos, | ||
| 103 | + &buffer, | ||
| 104 | + Some(alpha), | ||
| 105 | + Some(view.src), | ||
| 106 | + Some(view.dst), | ||
| 107 | + kind, | ||
| 108 | + ); | ||
| 109 | + | ||
| 110 | + storage.push(PrimaryGpuTextureRenderElement(elem).into()); | ||
| 111 | + } | ||
| 112 | + } | ||
| 113 | + }, | ||
| 114 | + |_, _, _| true, | ||
| 115 | + ); | ||
| 116 | +} | ||
src/window/mapped.rsmodified+70 -9
| @@ -3,9 +3,9 @@ use std::cmp::{max, min}; | |||
| 3 | 3 | ||
| 4 | use niri_config::{BlockOutFrom, WindowRule}; | 4 | use niri_config::{BlockOutFrom, WindowRule}; |
| 5 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; | 5 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; |
| 6 | -use smithay::backend::renderer::element::{AsRenderElements as _, Id, Kind}; | 6 | +use smithay::backend::renderer::element::{Id, Kind}; |
| 7 | use smithay::desktop::space::SpaceElement as _; | 7 | use smithay::desktop::space::SpaceElement as _; |
| 8 | -use smithay::desktop::Window; | 8 | +use smithay::desktop::{PopupManager, Window}; |
| 9 | use smithay::output::Output; | 9 | use smithay::output::Output; |
| 10 | use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1; | 10 | use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1; |
| 11 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | 11 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| @@ -15,10 +15,13 @@ use smithay::wayland::compositor::{send_surface_state, with_states}; | |||
| 15 | use smithay::wayland::shell::xdg::{SurfaceCachedState, ToplevelSurface}; | 15 | use smithay::wayland::shell::xdg::{SurfaceCachedState, ToplevelSurface}; |
| 16 | 16 | ||
| 17 | use super::{ResolvedWindowRules, WindowRef}; | 17 | use super::{ResolvedWindowRules, WindowRef}; |
| 18 | -use crate::layout::{LayoutElement, LayoutElementRenderElement}; | 18 | +use crate::layout::{ |
| 19 | + LayoutElement, LayoutElementRenderElement, LayoutElementSnapshotRenderElements, | ||
| 20 | +}; | ||
| 19 | use crate::niri::WindowOffscreenId; | 21 | use crate::niri::WindowOffscreenId; |
| 20 | use crate::render_helpers::renderer::NiriRenderer; | 22 | use crate::render_helpers::renderer::NiriRenderer; |
| 21 | -use crate::render_helpers::RenderTarget; | 23 | +use crate::render_helpers::surface::render_and_save_from_surface_tree; |
| 24 | +use crate::render_helpers::{RenderSnapshot, RenderTarget}; | ||
| 22 | 25 | ||
| 23 | #[derive(Debug)] | 26 | #[derive(Debug)] |
| 24 | pub struct Mapped { | 27 | pub struct Mapped { |
| @@ -38,6 +41,9 @@ pub struct Mapped { | |||
| 38 | 41 | ||
| 39 | /// Buffer to draw instead of the window when it should be blocked out. | 42 | /// Buffer to draw instead of the window when it should be blocked out. |
| 40 | block_out_buffer: RefCell<SolidColorBuffer>, | 43 | block_out_buffer: RefCell<SolidColorBuffer>, |
| 44 | + | ||
| 45 | + /// Snapshot of the last render for use in the close animation. | ||
| 46 | + last_render: RefCell<RenderSnapshot<LayoutElementSnapshotRenderElements>>, | ||
| 41 | } | 47 | } |
| 42 | 48 | ||
| 43 | impl Mapped { | 49 | impl Mapped { |
| @@ -48,6 +54,7 @@ impl Mapped { | |||
| 48 | need_to_recompute_rules: false, | 54 | need_to_recompute_rules: false, |
| 49 | is_focused: false, | 55 | is_focused: false, |
| 50 | block_out_buffer: RefCell::new(SolidColorBuffer::new((0, 0), [0., 0., 0., 1.])), | 56 | block_out_buffer: RefCell::new(SolidColorBuffer::new((0, 0), [0., 0., 0., 1.])), |
| 57 | + last_render: RefCell::new(RenderSnapshot::default()), | ||
| 51 | } | 58 | } |
| 52 | } | 59 | } |
| 53 | 60 | ||
| @@ -124,9 +131,10 @@ impl LayoutElement for Mapped { | |||
| 124 | Some(BlockOutFrom::ScreenCapture) => target != RenderTarget::Output, | 131 | Some(BlockOutFrom::ScreenCapture) => target != RenderTarget::Output, |
| 125 | }; | 132 | }; |
| 126 | 133 | ||
| 134 | + let mut buffer = self.block_out_buffer.borrow_mut(); | ||
| 135 | + buffer.resize(self.window.geometry().size); | ||
| 136 | + | ||
| 127 | if block_out { | 137 | if block_out { |
| 128 | - let mut buffer = self.block_out_buffer.borrow_mut(); | ||
| 129 | - buffer.resize(self.window.geometry().size); | ||
| 130 | let elem = SolidColorRenderElement::from_buffer( | 138 | let elem = SolidColorRenderElement::from_buffer( |
| 131 | &buffer, | 139 | &buffer, |
| 132 | location.to_physical_precise_round(scale), | 140 | location.to_physical_precise_round(scale), |
| @@ -137,15 +145,68 @@ impl LayoutElement for Mapped { | |||
| 137 | vec![elem.into()] | 145 | vec![elem.into()] |
| 138 | } else { | 146 | } else { |
| 139 | let buf_pos = location - self.window.geometry().loc; | 147 | let buf_pos = location - self.window.geometry().loc; |
| 140 | - self.window.render_elements( | 148 | + let buf_pos = buf_pos.to_physical_precise_round(scale); |
| 149 | + | ||
| 150 | + let mut elements = vec![]; | ||
| 151 | + | ||
| 152 | + // If we're rendering for output, save into last_render. | ||
| 153 | + let mut last_render = self.last_render.borrow_mut(); | ||
| 154 | + // FIXME: when preview-render is active, last render contents will never update. | ||
| 155 | + let mut storage = if target == RenderTarget::Output { | ||
| 156 | + last_render.contents.clear(); | ||
| 157 | + last_render.block_out_from = self.rules.block_out_from; | ||
| 158 | + last_render.blocked_out_contents = vec![SolidColorRenderElement::from_buffer( | ||
| 159 | + &buffer, | ||
| 160 | + (0, 0), | ||
| 161 | + scale, | ||
| 162 | + alpha, | ||
| 163 | + Kind::Unspecified, | ||
| 164 | + ) | ||
| 165 | + .into()]; | ||
| 166 | + | ||
| 167 | + Some(&mut last_render.contents) | ||
| 168 | + } else { | ||
| 169 | + None | ||
| 170 | + }; | ||
| 171 | + | ||
| 172 | + let surface = self.toplevel().wl_surface(); | ||
| 173 | + for (popup, popup_offset) in PopupManager::popups_for_surface(surface) { | ||
| 174 | + let offset = (self.window.geometry().loc + popup_offset - popup.geometry().loc) | ||
| 175 | + .to_physical_precise_round(scale); | ||
| 176 | + | ||
| 177 | + render_and_save_from_surface_tree( | ||
| 178 | + renderer, | ||
| 179 | + popup.wl_surface(), | ||
| 180 | + buf_pos, | ||
| 181 | + offset, | ||
| 182 | + scale, | ||
| 183 | + alpha, | ||
| 184 | + Kind::Unspecified, | ||
| 185 | + &mut elements, | ||
| 186 | + &mut storage, | ||
| 187 | + ); | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + render_and_save_from_surface_tree( | ||
| 141 | renderer, | 191 | renderer, |
| 142 | - buf_pos.to_physical_precise_round(scale), | 192 | + surface, |
| 193 | + buf_pos, | ||
| 194 | + Point::from((0., 0.)), | ||
| 143 | scale, | 195 | scale, |
| 144 | alpha, | 196 | alpha, |
| 145 | - ) | 197 | + Kind::Unspecified, |
| 198 | + &mut elements, | ||
| 199 | + &mut storage, | ||
| 200 | + ); | ||
| 201 | + | ||
| 202 | + elements | ||
| 146 | } | 203 | } |
| 147 | } | 204 | } |
| 148 | 205 | ||
| 206 | + fn take_last_render(&self) -> RenderSnapshot<LayoutElementSnapshotRenderElements> { | ||
| 207 | + self.last_render.take() | ||
| 208 | + } | ||
| 209 | + | ||
| 149 | fn request_size(&self, size: Size<i32, Logical>) { | 210 | fn request_size(&self, size: Size<i32, Logical>) { |
| 150 | self.toplevel().with_pending_state(|state| { | 211 | self.toplevel().with_pending_state(|state| { |
| 151 | state.size = Some(size); | 212 | state.size = Some(size); |
wiki/Configuration:-Animations.mdmodified+23 -1
| @@ -33,6 +33,11 @@ animations { | |||
| 33 | curve "ease-out-expo" | 33 | curve "ease-out-expo" |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | + window-close { | ||
| 37 | + duration-ms 150 | ||
| 38 | + curve "ease-out-quad" | ||
| 39 | + } | ||
| 40 | + | ||
| 36 | config-notification-open-close { | 41 | config-notification-open-close { |
| 37 | spring damping-ratio=0.6 stiffness=1000 epsilon=0.001 | 42 | spring damping-ratio=0.6 stiffness=1000 epsilon=0.001 |
| 38 | } | 43 | } |
| @@ -149,7 +154,7 @@ Window movement animations, currently cover only horizontal column movement. | |||
| 149 | This animation runs on actions like `move-column-left` and `move-column-right` to move the windows themselves. | 154 | This animation runs on actions like `move-column-left` and `move-column-right` to move the windows themselves. |
| 150 | It can sometimes run together with the `horizontal-view-movement` animation, if the camera also moves. | 155 | It can sometimes run together with the `horizontal-view-movement` animation, if the camera also moves. |
| 151 | 156 | ||
| 152 | -Since 0.1.5, this is also the animation that moves windows out of the way upon window opening. | 157 | +Since 0.1.5, this is also the animation that moves windows out of the way upon window opening and closing. |
| 153 | 158 | ||
| 154 | ``` | 159 | ``` |
| 155 | animations { | 160 | animations { |
| @@ -161,6 +166,8 @@ animations { | |||
| 161 | 166 | ||
| 162 | #### `window-open` | 167 | #### `window-open` |
| 163 | 168 | ||
| 169 | +<sup>Since: 0.1.5</sup> | ||
| 170 | + | ||
| 164 | Window opening animation. | 171 | Window opening animation. |
| 165 | 172 | ||
| 166 | This one uses an easing type by default. | 173 | This one uses an easing type by default. |
| @@ -174,6 +181,21 @@ animations { | |||
| 174 | } | 181 | } |
| 175 | ``` | 182 | ``` |
| 176 | 183 | ||
| 184 | +#### `window-close` | ||
| 185 | + | ||
| 186 | +Window closing animation. | ||
| 187 | + | ||
| 188 | +This one uses an easing type by default. | ||
| 189 | + | ||
| 190 | +``` | ||
| 191 | +animations { | ||
| 192 | + window-open { | ||
| 193 | + duration-ms 150 | ||
| 194 | + curve "ease-out-quad" | ||
| 195 | + } | ||
| 196 | +} | ||
| 197 | +``` | ||
| 198 | + | ||
| 177 | #### `config-notification-open-close` | 199 | #### `config-notification-open-close` |
| 178 | 200 | ||
| 179 | The open/close animation of the config parse error and new default config notifications. | 201 | The open/close animation of the config parse error and new default config notifications. |