forge
Explore
Log in

Don't you dare go Hollow

Implement window close transaction

Browse files at this commit

c8839f7658588bfd7f0f41bf2c2726e526366abfIvan MolodetskikhParent: dfe3580
Mainly visible with disabled animations.
src/handlers/compositor.rsmodified+5 -2
@@ -20,6 +20,7 @@ use smithay::{delegate_compositor, delegate_shm};
2020 use super::xdg_shell::add_mapped_toplevel_pre_commit_hook;
2121 use crate::niri::{ClientState, State};
2222 use crate::utils::send_scale_transform;
23+use crate::utils::transaction::Transaction;
2324 use crate::window::{InitialConfigureState, Mapped, ResolvedWindowRules, Unmapped};
2425
2526 impl CompositorHandler for State {
@@ -193,11 +194,13 @@ impl CompositorHandler for State {
193194 });
194195
195196 // Must start the close animation before window.on_commit().
197+ let transaction = Transaction::new();
196198 if !is_mapped {
199+ let blocker = transaction.blocker();
197200 self.backend.with_primary_renderer(|renderer| {
198201 self.niri
199202 .layout
200- .start_close_animation_for_window(renderer, &window);
203+ .start_close_animation_for_window(renderer, &window, blocker);
201204 });
202205 }
203206
@@ -216,7 +219,7 @@ impl CompositorHandler for State {
216219 id: u64::from(id.get()),
217220 });
218221
219- self.niri.layout.remove_window(&window);
222+ self.niri.layout.remove_window(&window, transaction);
220223 self.add_default_dmabuf_pre_commit_hook(surface);
221224
222225 if was_active {
src/handlers/xdg_shell.rsmodified+6 -2
@@ -40,6 +40,7 @@ use crate::input::resize_grab::ResizeGrab;
4040 use crate::input::DOUBLE_CLICK_TIME;
4141 use crate::layout::workspace::ColumnWidth;
4242 use crate::niri::{PopupGrabState, State};
43+use crate::utils::transaction::Transaction;
4344 use crate::utils::{get_monotonic_time, send_scale_transform, ResizeEdge};
4445 use crate::window::{InitialConfigureState, ResolvedWindowRules, Unmapped, WindowRef};
4546
@@ -485,16 +486,19 @@ impl XdgShellHandler for State {
485486 self.backend.with_primary_renderer(|renderer| {
486487 self.niri.layout.store_unmap_snapshot(renderer, &window);
487488 });
489+
490+ let transaction = Transaction::new();
491+ let blocker = transaction.blocker();
488492 self.backend.with_primary_renderer(|renderer| {
489493 self.niri
490494 .layout
491- .start_close_animation_for_window(renderer, &window);
495+ .start_close_animation_for_window(renderer, &window, blocker);
492496 });
493497
494498 let active_window = self.niri.layout.active_window().map(|(m, _)| &m.window);
495499 let was_active = active_window == Some(&window);
496500
497- self.niri.layout.remove_window(&window);
501+ self.niri.layout.remove_window(&window, transaction);
498502 self.add_default_dmabuf_pre_commit_hook(surface.wl_surface());
499503
500504 if was_active {
src/layout/closing_window.rsmodified+72 -7
@@ -12,6 +12,7 @@ use smithay::backend::renderer::element::{Kind, RenderElement};
1212 use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture, Uniform};
1313 use smithay::backend::renderer::Texture;
1414 use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform};
15+use smithay::wayland::compositor::{Blocker, BlockerState};
1516
1617 use crate::animation::Animation;
1718 use crate::niri_render_elements;
@@ -21,6 +22,7 @@ use crate::render_helpers::shaders::{mat3_uniform, ProgramType, Shaders};
2122 use crate::render_helpers::snapshot::RenderSnapshot;
2223 use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement};
2324 use crate::render_helpers::{render_to_encompassing_texture, RenderTarget};
25+use crate::utils::transaction::TransactionBlocker;
2426
2527 #[derive(Debug)]
2628 pub struct ClosingWindow {
@@ -46,7 +48,7 @@ pub struct ClosingWindow {
4648 blocked_out_buffer_offset: Point<f64, Logical>,
4749
4850 /// The closing animation.
49- anim: Animation,
51+ anim_state: AnimationState,
5052
5153 /// Random seed for the shader.
5254 random_seed: f32,
@@ -59,6 +61,29 @@ niri_render_elements! {
5961 }
6062 }
6163
64+#[derive(Debug)]
65+enum AnimationState {
66+ Waiting {
67+ /// Blocker for a transaction before starting the animation.
68+ blocker: TransactionBlocker,
69+ anim: Animation,
70+ },
71+ Animating(Animation),
72+}
73+
74+impl AnimationState {
75+ pub fn new(blocker: TransactionBlocker, anim: Animation) -> Self {
76+ if blocker.state() == BlockerState::Pending {
77+ Self::Waiting { blocker, anim }
78+ } else {
79+ // This actually doesn't normally happen because the window is removed only after the
80+ // closing animation is created. Though, it does happen with disable-transactions debug
81+ // flag.
82+ Self::Animating(anim)
83+ }
84+ }
85+}
86+
6287 impl ClosingWindow {
6388 pub fn new<E: RenderElement<GlesRenderer>>(
6489 renderer: &mut GlesRenderer,
@@ -66,6 +91,7 @@ impl ClosingWindow {
6691 scale: Scale<f64>,
6792 geo_size: Size<f64, Logical>,
6893 pos: Point<f64, Logical>,
94+ blocker: TransactionBlocker,
6995 anim: Animation,
7096 ) -> anyhow::Result<Self> {
7197 let _span = tracy_client::span!("ClosingWindow::new");
@@ -107,17 +133,29 @@ impl ClosingWindow {
107133 pos,
108134 buffer_offset,
109135 blocked_out_buffer_offset,
110- anim,
136+ anim_state: AnimationState::new(blocker, anim),
111137 random_seed: fastrand::f32(),
112138 })
113139 }
114140
115141 pub fn advance_animations(&mut self, current_time: Duration) {
116- self.anim.set_current_time(current_time);
142+ match &mut self.anim_state {
143+ AnimationState::Waiting { blocker, anim } => {
144+ if blocker.state() != BlockerState::Pending {
145+ let mut anim = anim.restarted(0., 1., 0.);
146+ anim.set_current_time(current_time);
147+ self.anim_state = AnimationState::Animating(anim);
148+ }
149+ }
150+ AnimationState::Animating(anim) => anim.set_current_time(current_time),
151+ }
117152 }
118153
119154 pub fn are_animations_ongoing(&self) -> bool {
120- !self.anim.is_done()
155+ match &self.anim_state {
156+ AnimationState::Waiting { .. } => true,
157+ AnimationState::Animating(anim) => !anim.is_done(),
158+ }
121159 }
122160
123161 pub fn render(
@@ -127,15 +165,42 @@ impl ClosingWindow {
127165 scale: Scale<f64>,
128166 target: RenderTarget,
129167 ) -> ClosingWindowRenderElement {
130- let progress = self.anim.value();
131- let clamped_progress = self.anim.clamped_value().clamp(0., 1.);
132-
133168 let (buffer, offset) = if target.should_block_out(self.block_out_from) {
134169 (&self.blocked_out_buffer, self.blocked_out_buffer_offset)
135170 } else {
136171 (&self.buffer, self.buffer_offset)
137172 };
138173
174+ let anim = match &self.anim_state {
175+ AnimationState::Waiting { .. } => {
176+ let elem = TextureRenderElement::from_texture_buffer(
177+ buffer.clone(),
178+ Point::from((0., 0.)),
179+ 1.,
180+ None,
181+ None,
182+ Kind::Unspecified,
183+ );
184+
185+ let elem = PrimaryGpuTextureRenderElement(elem);
186+ let elem = RescaleRenderElement::from_element(elem, Point::from((0, 0)), 1.);
187+
188+ let mut location = self.pos + offset;
189+ location.x -= view_rect.loc.x;
190+ let elem = RelocateRenderElement::from_element(
191+ elem,
192+ location.to_physical_precise_round(scale),
193+ Relocate::Relative,
194+ );
195+
196+ return elem.into();
197+ }
198+ AnimationState::Animating(anim) => anim,
199+ };
200+
201+ let progress = anim.value();
202+ let clamped_progress = anim.clamped_value().clamp(0., 1.);
203+
139204 if Shaders::get(renderer).program(ProgramType::Close).is_some() {
140205 let area_loc = Vec2::new(view_rect.loc.x as f32, view_rect.loc.y as f32);
141206 let area_size = Vec2::new(view_rect.size.w as f32, view_rect.size.h as f32);
src/layout/mod.rsmodified+18 -14
@@ -52,7 +52,7 @@ use crate::render_helpers::snapshot::RenderSnapshot;
5252 use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement};
5353 use crate::render_helpers::texture::TextureBuffer;
5454 use crate::render_helpers::{BakedBuffer, RenderTarget, SplitElements};
55-use crate::utils::transaction::Transaction;
55+use crate::utils::transaction::{Transaction, TransactionBlocker};
5656 use crate::utils::{output_size, round_logical_in_physical_max1, ResizeEdge};
5757 use crate::window::ResolvedWindowRules;
5858
@@ -752,15 +752,13 @@ impl<W: LayoutElement> Layout<W> {
752752 );
753753 }
754754
755- pub fn remove_window(&mut self, window: &W::Id) -> Option<W> {
756- let mut rv = None;
757-
755+ pub fn remove_window(&mut self, window: &W::Id, transaction: Transaction) -> Option<W> {
758756 match &mut self.monitor_set {
759757 MonitorSet::Normal { monitors, .. } => {
760758 for mon in monitors {
761759 for (idx, ws) in mon.workspaces.iter_mut().enumerate() {
762760 if ws.has_window(window) {
763- rv = Some(ws.remove_window(window));
761+ let win = ws.remove_window(window, transaction);
764762
765763 // Clean up empty workspaces that are not active and not last.
766764 if !ws.has_windows()
@@ -776,7 +774,7 @@ impl<W: LayoutElement> Layout<W> {
776774 }
777775 }
778776
779- break;
777+ return Some(win);
780778 }
781779 }
782780 }
@@ -784,20 +782,20 @@ impl<W: LayoutElement> Layout<W> {
784782 MonitorSet::NoOutputs { workspaces, .. } => {
785783 for (idx, ws) in workspaces.iter_mut().enumerate() {
786784 if ws.has_window(window) {
787- rv = Some(ws.remove_window(window));
785+ let win = ws.remove_window(window, transaction);
788786
789787 // Clean up empty workspaces.
790788 if !ws.has_windows() && workspaces[idx].name.is_none() {
791789 workspaces.remove(idx);
792790 }
793791
794- break;
792+ return Some(win);
795793 }
796794 }
797795 }
798796 }
799797
800- rv
798+ None
801799 }
802800
803801 pub fn update_window(&mut self, window: &W::Id, serial: Option<Serial>) {
@@ -1970,7 +1968,12 @@ impl<W: LayoutElement> Layout<W> {
19701968 let width = column.width;
19711969 let is_full_width = column.is_full_width;
19721970 let window = ws
1973- .remove_tile_by_idx(ws.active_column_idx, column.active_tile_idx, None)
1971+ .remove_tile_by_idx(
1972+ ws.active_column_idx,
1973+ column.active_tile_idx,
1974+ Transaction::new(),
1975+ None,
1976+ )
19741977 .into_window();
19751978
19761979 let workspace_idx = monitors[new_idx].active_workspace_idx;
@@ -2022,7 +2025,7 @@ impl<W: LayoutElement> Layout<W> {
20222025
20232026 let Some(width) = width else { return };
20242027
2025- let window = self.remove_window(window).unwrap();
2028+ let window = self.remove_window(window, Transaction::new()).unwrap();
20262029
20272030 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
20282031 let new_idx = monitors
@@ -2425,6 +2428,7 @@ impl<W: LayoutElement> Layout<W> {
24252428 &mut self,
24262429 renderer: &mut GlesRenderer,
24272430 window: &W::Id,
2431+ blocker: TransactionBlocker,
24282432 ) {
24292433 let _span = tracy_client::span!("Layout::start_close_animation_for_window");
24302434
@@ -2433,7 +2437,7 @@ impl<W: LayoutElement> Layout<W> {
24332437 for mon in monitors {
24342438 for ws in &mut mon.workspaces {
24352439 if ws.has_window(window) {
2436- ws.start_close_animation_for_window(renderer, window);
2440+ ws.start_close_animation_for_window(renderer, window, blocker);
24372441 return;
24382442 }
24392443 }
@@ -2442,7 +2446,7 @@ impl<W: LayoutElement> Layout<W> {
24422446 MonitorSet::NoOutputs { workspaces, .. } => {
24432447 for ws in workspaces {
24442448 if ws.has_window(window) {
2445- ws.start_close_animation_for_window(renderer, window);
2449+ ws.start_close_animation_for_window(renderer, window, blocker);
24462450 return;
24472451 }
24482452 }
@@ -3143,7 +3147,7 @@ mod tests {
31433147 layout.add_window_to_named_workspace(&ws_name, win, None, false);
31443148 }
31453149 Op::CloseWindow(id) => {
3146- layout.remove_window(&id);
3150+ layout.remove_window(&id, Transaction::new());
31473151 }
31483152 Op::FullscreenWindow(id) => {
31493153 layout.toggle_fullscreen(&id);
src/layout/monitor.rsmodified+19 -3
@@ -19,6 +19,7 @@ use crate::input::swipe_tracker::SwipeTracker;
1919 use crate::render_helpers::renderer::NiriRenderer;
2020 use crate::render_helpers::RenderTarget;
2121 use crate::rubber_band::RubberBand;
22+use crate::utils::transaction::Transaction;
2223 use crate::utils::{output_size, to_physical_precise_round, ResizeEdge};
2324
2425 /// Amount of touchpad movement to scroll the height of one workspace.
@@ -442,7 +443,12 @@ impl<W: LayoutElement> Monitor<W> {
442443 let width = column.width;
443444 let is_full_width = column.is_full_width;
444445 let window = workspace
445- .remove_tile_by_idx(workspace.active_column_idx, column.active_tile_idx, None)
446+ .remove_tile_by_idx(
447+ workspace.active_column_idx,
448+ column.active_tile_idx,
449+ Transaction::new(),
450+ None,
451+ )
446452 .into_window();
447453
448454 self.add_window(new_idx, window, true, width, is_full_width);
@@ -465,7 +471,12 @@ impl<W: LayoutElement> Monitor<W> {
465471 let width = column.width;
466472 let is_full_width = column.is_full_width;
467473 let window = workspace
468- .remove_tile_by_idx(workspace.active_column_idx, column.active_tile_idx, None)
474+ .remove_tile_by_idx(
475+ workspace.active_column_idx,
476+ column.active_tile_idx,
477+ Transaction::new(),
478+ None,
479+ )
469480 .into_window();
470481
471482 self.add_window(new_idx, window, true, width, is_full_width);
@@ -488,7 +499,12 @@ impl<W: LayoutElement> Monitor<W> {
488499 let width = column.width;
489500 let is_full_width = column.is_full_width;
490501 let window = workspace
491- .remove_tile_by_idx(workspace.active_column_idx, column.active_tile_idx, None)
502+ .remove_tile_by_idx(
503+ workspace.active_column_idx,
504+ column.active_tile_idx,
505+ Transaction::new(),
506+ None,
507+ )
492508 .into_window();
493509
494510 self.add_window(new_idx, window, true, width, is_full_width);
src/layout/workspace.rsmodified+46 -11
@@ -22,7 +22,7 @@ use crate::niri_render_elements;
2222 use crate::render_helpers::renderer::NiriRenderer;
2323 use crate::render_helpers::RenderTarget;
2424 use crate::utils::id::IdCounter;
25-use crate::utils::transaction::Transaction;
25+use crate::utils::transaction::{Transaction, TransactionBlocker};
2626 use crate::utils::{output_size, send_scale_transform, ResizeEdge};
2727 use crate::window::ResolvedWindowRules;
2828
@@ -1098,6 +1098,7 @@ impl<W: LayoutElement> Workspace<W> {
10981098 &mut self,
10991099 column_idx: usize,
11001100 window_idx: usize,
1101+ transaction: Transaction,
11011102 anim_config: Option<niri_config::Animation>,
11021103 ) -> Tile<W> {
11031104 let offset = self.column_x(column_idx + 1) - self.column_x(column_idx);
@@ -1139,7 +1140,7 @@ impl<W: LayoutElement> Workspace<W> {
11391140 offset
11401141 } else {
11411142 column.active_tile_idx = min(column.active_tile_idx, column.tiles.len() - 1);
1142- column.update_tile_sizes(true);
1143+ column.update_tile_sizes_with_transaction(true, transaction);
11431144 self.data[column_idx].update(column);
11441145
11451146 prev_width - column.width()
@@ -1294,7 +1295,7 @@ impl<W: LayoutElement> Workspace<W> {
12941295 column
12951296 }
12961297
1297- pub fn remove_window(&mut self, window: &W::Id) -> W {
1298+ pub fn remove_window(&mut self, window: &W::Id, transaction: Transaction) -> W {
12981299 let column_idx = self
12991300 .columns
13001301 .iter()
@@ -1303,7 +1304,7 @@ impl<W: LayoutElement> Workspace<W> {
13031304 let column = &self.columns[column_idx];
13041305
13051306 let window_idx = column.position(window).unwrap();
1306- self.remove_tile_by_idx(column_idx, window_idx, None)
1307+ self.remove_tile_by_idx(column_idx, window_idx, transaction, None)
13071308 .into_window()
13081309 }
13091310
@@ -1490,6 +1491,7 @@ impl<W: LayoutElement> Workspace<W> {
14901491 &mut self,
14911492 renderer: &mut GlesRenderer,
14921493 window: &W::Id,
1494+ blocker: TransactionBlocker,
14931495 ) {
14941496 let output_scale = Scale::from(self.scale.fractional_scale());
14951497
@@ -1542,7 +1544,21 @@ impl<W: LayoutElement> Workspace<W> {
15421544
15431545 let anim = Animation::new(0., 1., 0., self.options.animations.window_close.anim);
15441546
1545- let res = ClosingWindow::new(renderer, snapshot, output_scale, tile_size, tile_pos, anim);
1547+ let blocker = if self.options.disable_transactions {
1548+ TransactionBlocker::completed()
1549+ } else {
1550+ blocker
1551+ };
1552+
1553+ let res = ClosingWindow::new(
1554+ renderer,
1555+ snapshot,
1556+ output_scale,
1557+ tile_size,
1558+ tile_pos,
1559+ blocker,
1560+ anim,
1561+ );
15461562 match res {
15471563 Ok(closing) => {
15481564 self.closing_windows.push(closing);
@@ -1781,6 +1797,7 @@ impl<W: LayoutElement> Workspace<W> {
17811797 let tile = self.remove_tile_by_idx(
17821798 source_col_idx,
17831799 0,
1800+ Transaction::new(),
17841801 Some(self.options.animations.window_movement.0),
17851802 );
17861803 self.enter_output_for_window(tile.window());
@@ -1813,7 +1830,12 @@ impl<W: LayoutElement> Workspace<W> {
18131830
18141831 let mut offset = Point::from((source_column.render_offset().x, 0.));
18151832
1816- let tile = self.remove_tile_by_idx(source_col_idx, source_column.active_tile_idx, None);
1833+ let tile = self.remove_tile_by_idx(
1834+ source_col_idx,
1835+ source_column.active_tile_idx,
1836+ Transaction::new(),
1837+ None,
1838+ );
18171839
18181840 self.add_tile_at(
18191841 self.active_column_idx,
@@ -1861,6 +1883,7 @@ impl<W: LayoutElement> Workspace<W> {
18611883 let tile = self.remove_tile_by_idx(
18621884 source_col_idx,
18631885 0,
1886+ Transaction::new(),
18641887 Some(self.options.animations.window_movement.0),
18651888 );
18661889 self.enter_output_for_window(tile.window());
@@ -1888,7 +1911,12 @@ impl<W: LayoutElement> Workspace<W> {
18881911 let width = source_column.width;
18891912 let is_full_width = source_column.is_full_width;
18901913
1891- let tile = self.remove_tile_by_idx(source_col_idx, source_column.active_tile_idx, None);
1914+ let tile = self.remove_tile_by_idx(
1915+ source_col_idx,
1916+ source_column.active_tile_idx,
1917+ Transaction::new(),
1918+ None,
1919+ );
18921920
18931921 self.add_tile(
18941922 tile,
@@ -1921,7 +1949,7 @@ impl<W: LayoutElement> Workspace<W> {
19211949 let mut offset = Point::from((offset, 0.));
19221950 let prev_off = self.columns[source_column_idx].tile_offset(0);
19231951
1924- let tile = self.remove_tile_by_idx(source_column_idx, 0, None);
1952+ let tile = self.remove_tile_by_idx(source_column_idx, 0, Transaction::new(), None);
19251953 self.enter_output_for_window(tile.window());
19261954
19271955 let prev_next_x = self.column_x(self.active_column_idx + 1);
@@ -1970,8 +1998,12 @@ impl<W: LayoutElement> Workspace<W> {
19701998
19711999 let width = source_column.width;
19722000 let is_full_width = source_column.is_full_width;
1973- let tile =
1974- self.remove_tile_by_idx(self.active_column_idx, source_column.active_tile_idx, None);
2001+ let tile = self.remove_tile_by_idx(
2002+ self.active_column_idx,
2003+ source_column.active_tile_idx,
2004+ Transaction::new(),
2005+ None,
2006+ );
19752007
19762008 self.add_tile(
19772009 tile,
@@ -3031,6 +3063,10 @@ impl<W: LayoutElement> Column<W> {
30313063 }
30323064
30333065 fn update_tile_sizes(&mut self, animate: bool) {
3066+ self.update_tile_sizes_with_transaction(animate, Transaction::new());
3067+ }
3068+
3069+ fn update_tile_sizes_with_transaction(&mut self, animate: bool, transaction: Transaction) {
30343070 if self.is_fullscreen {
30353071 self.tiles[0].request_fullscreen(self.view_size);
30363072 return;
@@ -3195,7 +3231,6 @@ impl<W: LayoutElement> Column<W> {
31953231 assert_eq!(auto_tiles_left, 0);
31963232 }
31973233
3198- let transaction = Transaction::new();
31993234 for (tile, h) in zip(&mut self.tiles, heights) {
32003235 let WindowHeight::Fixed(height) = h else {
32013236 unreachable!()
src/utils/transaction.rsmodified+6 -0
@@ -148,6 +148,12 @@ impl Drop for Transaction {
148148 }
149149 }
150150
151+impl TransactionBlocker {
152+ pub fn completed() -> Self {
153+ Self(Weak::new())
154+ }
155+}
156+
151157 impl Blocker for TransactionBlocker {
152158 fn state(&self) -> BlockerState {
153159 if self.0.upgrade().map_or(true, |x| x.is_completed()) {