forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB64a9351Ivan Molodetskikh2 years ago1319 commits

workspace.rs3761 lines, 127.3 KiBHistoryRawDownload
1use std::cmp::{max, min};
2use std::iter::{self, zip};
3use std::rc::Rc;
4use std::time::Duration;
5
6use niri_config::{CenterFocusedColumn, PresetWidth, Struts, Workspace as WorkspaceConfig};
7use niri_ipc::SizeChange;
8use ordered_float::NotNan;
9use smithay::backend::renderer::gles::GlesRenderer;
10use smithay::desktop::{layer_map_for_output, Window};
11use smithay::output::Output;
12use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
13use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
14use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size, Transform};
15
16use super::closing_window::{ClosingWindow, ClosingWindowRenderElement};
17use super::tile::{Tile, TileRenderElement};
18use super::{ConfigureIntent, InteractiveResizeData, LayoutElement, Options};
19use crate::animation::Animation;
20use crate::input::swipe_tracker::SwipeTracker;
21use crate::niri_render_elements;
22use crate::render_helpers::renderer::NiriRenderer;
23use crate::render_helpers::RenderTarget;
24use crate::utils::id::IdCounter;
25use crate::utils::transaction::{Transaction, TransactionBlocker};
26use crate::utils::{output_size, send_scale_transform, ResizeEdge};
27use crate::window::ResolvedWindowRules;
28
29/// Amount of touchpad movement to scroll the view for the width of one working area.
30const VIEW_GESTURE_WORKING_AREA_MOVEMENT: f64 = 1200.;
31
32#[derive(Debug)]
33pub struct Workspace<W: LayoutElement> {
34 /// The original output of this workspace.
35 ///
36 /// Most of the time this will be the workspace's current output, however, after an output
37 /// disconnection, it may remain pointing to the disconnected output.
38 pub original_output: OutputId,
39
40 /// Current output of this workspace.
41 output: Option<Output>,
42
43 /// Latest known output scale for this workspace.
44 ///
45 /// This should be set from the current workspace output, or, if all outputs have been
46 /// disconnected, preserved until a new output is connected.
47 scale: smithay::output::Scale,
48
49 /// Latest known output transform for this workspace.
50 ///
51 /// This should be set from the current workspace output, or, if all outputs have been
52 /// disconnected, preserved until a new output is connected.
53 transform: Transform,
54
55 /// Latest known view size for this workspace.
56 ///
57 /// This should be computed from the current workspace output size, or, if all outputs have
58 /// been disconnected, preserved until a new output is connected.
59 view_size: Size<f64, Logical>,
60
61 /// Latest known working area for this workspace.
62 ///
63 /// This is similar to view size, but takes into account things like layer shell exclusive
64 /// zones.
65 working_area: Rectangle<f64, Logical>,
66
67 /// Columns of windows on this workspace.
68 pub columns: Vec<Column<W>>,
69
70 /// Extra per-column data.
71 data: Vec<ColumnData>,
72
73 /// Index of the currently active column, if any.
74 pub active_column_idx: usize,
75
76 /// Ongoing interactive resize.
77 interactive_resize: Option<InteractiveResize<W>>,
78
79 /// Offset of the view computed from the active column.
80 ///
81 /// Any gaps, including left padding from work area left exclusive zone, is handled
82 /// with this view offset (rather than added as a constant elsewhere in the code). This allows
83 /// for natural handling of fullscreen windows, which must ignore work area padding.
84 view_offset: f64,
85
86 /// Adjustment of the view offset, if one is currently ongoing.
87 view_offset_adj: Option<ViewOffsetAdjustment>,
88
89 /// Whether to activate the previous, rather than the next, column upon column removal.
90 ///
91 /// When a new column is created and removed with no focus changes in-between, it is more
92 /// natural to activate the previously-focused column. This variable tracks that.
93 ///
94 /// Since we only create-and-activate columns immediately to the right of the active column (in
95 /// contrast to tabs in Firefox, for example), we can track this as a bool, rather than an
96 /// index of the previous column to activate.
97 ///
98 /// The value is the view offset that the previous column had before, to restore it.
99 activate_prev_column_on_removal: Option<f64>,
100
101 /// View offset to restore after unfullscreening.
102 view_offset_before_fullscreen: Option<f64>,
103
104 /// Windows in the closing animation.
105 closing_windows: Vec<ClosingWindow>,
106
107 /// Configurable properties of the layout as received from the parent monitor.
108 pub base_options: Rc<Options>,
109
110 /// Configurable properties of the layout with logical sizes adjusted for the current `scale`.
111 pub options: Rc<Options>,
112
113 /// Optional name of this workspace.
114 pub name: Option<String>,
115
116 /// Unique ID of this workspace.
117 id: WorkspaceId,
118}
119
120#[derive(Debug, Clone, PartialEq, Eq)]
121pub struct OutputId(String);
122
123static WORKSPACE_ID_COUNTER: IdCounter = IdCounter::new();
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
126pub struct WorkspaceId(u64);
127
128impl WorkspaceId {
129 fn next() -> WorkspaceId {
130 WorkspaceId(WORKSPACE_ID_COUNTER.next())
131 }
132
133 pub fn get(self) -> u64 {
134 self.0
135 }
136}
137
138niri_render_elements! {
139 WorkspaceRenderElement<R> => {
140 Tile = TileRenderElement<R>,
141 ClosingWindow = ClosingWindowRenderElement,
142 }
143}
144
145/// Extra per-column data.
146#[derive(Debug, Clone, Copy, PartialEq)]
147struct ColumnData {
148 /// Cached actual column width.
149 width: f64,
150}
151
152#[derive(Debug)]
153enum ViewOffsetAdjustment {
154 Animation(Animation),
155 Gesture(ViewGesture),
156}
157
158#[derive(Debug)]
159struct ViewGesture {
160 current_view_offset: f64,
161 tracker: SwipeTracker,
162 delta_from_tracker: f64,
163 // The view offset we'll use if needed for activate_prev_column_on_removal.
164 static_view_offset: f64,
165 /// Whether the gesture is controlled by the touchpad.
166 is_touchpad: bool,
167}
168
169#[derive(Debug)]
170struct InteractiveResize<W: LayoutElement> {
171 window: W::Id,
172 original_window_size: Size<f64, Logical>,
173 data: InteractiveResizeData,
174}
175
176/// Width of a column.
177#[derive(Debug, Clone, Copy, PartialEq)]
178pub enum ColumnWidth {
179 /// Proportion of the current view width.
180 Proportion(f64),
181 /// One of the proportion presets.
182 ///
183 /// This is separate from Proportion in order to be able to reliably cycle between preset
184 /// proportions.
185 Preset(usize),
186 /// Fixed width in logical pixels.
187 Fixed(f64),
188}
189
190/// Height of a window in a column.
191///
192/// Proportional height is intentionally omitted. With column widths you frequently want e.g. two
193/// columns side-by-side with 50% width each, and you want them to remain this way when moving to a
194/// differently sized monitor. Windows in a column, however, already auto-size to fill the available
195/// height, giving you this behavior. The only reason to set a different window height, then, is
196/// when you want something in the window to fit exactly, e.g. to fit 30 lines in a terminal, which
197/// corresponds to the `Fixed` variant.
198///
199/// This does not preclude the usual set of binds to set or resize a window proportionally. Just,
200/// they are converted to, and stored as fixed height right away, so that once you resize a window
201/// to fit the desired content, it can never become smaller than that when moving between monitors.
202#[derive(Debug, Clone, Copy, PartialEq)]
203pub enum WindowHeight {
204 /// Automatically computed *tile* height, distributed across the column according to weights.
205 ///
206 /// This controls the tile height rather than the window height because it's easier in the auto
207 /// height distribution algorithm.
208 Auto { weight: f64 },
209 /// Fixed *window* height in logical pixels.
210 Fixed(f64),
211}
212
213#[derive(Debug)]
214pub struct Column<W: LayoutElement> {
215 /// Tiles in this column.
216 ///
217 /// Must be non-empty.
218 pub tiles: Vec<Tile<W>>,
219
220 /// Extra per-tile data.
221 ///
222 /// Must have the same number of elements as `tiles`.
223 data: Vec<TileData>,
224
225 /// Index of the currently active tile.
226 pub active_tile_idx: usize,
227
228 /// Desired width of this column.
229 ///
230 /// If the column is full-width or full-screened, this is the width that should be restored
231 /// upon unfullscreening and untoggling full-width.
232 pub width: ColumnWidth,
233
234 /// Whether this column is full-width.
235 pub is_full_width: bool,
236
237 /// Whether this column contains a single full-screened window.
238 pub is_fullscreen: bool,
239
240 /// Animation of the render offset during window swapping.
241 move_animation: Option<Animation>,
242
243 /// Latest known view size for this column's workspace.
244 view_size: Size<f64, Logical>,
245
246 /// Latest known working area for this column's workspace.
247 working_area: Rectangle<f64, Logical>,
248
249 /// Scale of the output the column is on (and rounds its sizes to).
250 scale: f64,
251
252 /// Configurable properties of the layout.
253 options: Rc<Options>,
254}
255
256/// Extra per-tile data.
257#[derive(Debug, Clone, Copy, PartialEq)]
258struct TileData {
259 /// Requested height of the window.
260 ///
261 /// This is window height, not tile height, so it excludes tile decorations.
262 height: WindowHeight,
263
264 /// Cached actual size of the tile.
265 size: Size<f64, Logical>,
266
267 /// Cached whether the tile is being interactively resized by its left edge.
268 interactively_resizing_by_left_edge: bool,
269}
270
271impl OutputId {
272 pub fn new(output: &Output) -> Self {
273 Self(output.name())
274 }
275}
276
277impl ViewOffsetAdjustment {
278 pub fn is_animation(&self) -> bool {
279 matches!(self, Self::Animation(_))
280 }
281
282 pub fn target_view_offset(&self) -> f64 {
283 match self {
284 ViewOffsetAdjustment::Animation(anim) => anim.to(),
285 ViewOffsetAdjustment::Gesture(gesture) => gesture.current_view_offset,
286 }
287 }
288}
289
290impl ColumnData {
291 pub fn new<W: LayoutElement>(column: &Column<W>) -> Self {
292 let mut rv = Self { width: 0. };
293 rv.update(column);
294 rv
295 }
296
297 pub fn update<W: LayoutElement>(&mut self, column: &Column<W>) {
298 self.width = column.width();
299 }
300}
301
302impl ColumnWidth {
303 fn resolve(self, options: &Options, view_width: f64) -> f64 {
304 match self {
305 ColumnWidth::Proportion(proportion) => {
306 (view_width - options.gaps) * proportion - options.gaps
307 }
308 ColumnWidth::Preset(idx) => options.preset_widths[idx].resolve(options, view_width),
309 ColumnWidth::Fixed(width) => width,
310 }
311 }
312}
313
314impl From<PresetWidth> for ColumnWidth {
315 fn from(value: PresetWidth) -> Self {
316 match value {
317 PresetWidth::Proportion(p) => Self::Proportion(p.clamp(0., 10000.)),
318 PresetWidth::Fixed(f) => Self::Fixed(f64::from(f.clamp(1, 100000))),
319 }
320 }
321}
322
323impl WindowHeight {
324 const fn auto_1() -> Self {
325 Self::Auto { weight: 1. }
326 }
327}
328
329impl TileData {
330 pub fn new<W: LayoutElement>(tile: &Tile<W>, height: WindowHeight) -> Self {
331 let mut rv = Self {
332 height,
333 size: Size::default(),
334 interactively_resizing_by_left_edge: false,
335 };
336 rv.update(tile);
337 rv
338 }
339
340 pub fn update<W: LayoutElement>(&mut self, tile: &Tile<W>) {
341 self.size = tile.tile_size();
342 self.interactively_resizing_by_left_edge = tile
343 .window()
344 .interactive_resize_data()
345 .map_or(false, |data| data.edges.contains(ResizeEdge::LEFT));
346 }
347}
348
349impl<W: LayoutElement> Workspace<W> {
350 pub fn new(output: Output, options: Rc<Options>) -> Self {
351 Self::new_with_config(output, None, options)
352 }
353
354 pub fn new_with_config(
355 output: Output,
356 config: Option<WorkspaceConfig>,
357 base_options: Rc<Options>,
358 ) -> Self {
359 let original_output = config
360 .as_ref()
361 .and_then(|c| c.open_on_output.clone())
362 .map(OutputId)
363 .unwrap_or(OutputId::new(&output));
364
365 let scale = output.current_scale();
366 let options =
367 Rc::new(Options::clone(&base_options).adjusted_for_scale(scale.fractional_scale()));
368
369 let working_area = compute_working_area(&output, options.struts);
370
371 Self {
372 original_output,
373 scale,
374 transform: output.current_transform(),
375 view_size: output_size(&output),
376 working_area,
377 output: Some(output),
378 columns: vec![],
379 data: vec![],
380 active_column_idx: 0,
381 interactive_resize: None,
382 view_offset: 0.,
383 view_offset_adj: None,
384 activate_prev_column_on_removal: None,
385 view_offset_before_fullscreen: None,
386 closing_windows: vec![],
387 base_options,
388 options,
389 name: config.map(|c| c.name.0),
390 id: WorkspaceId::next(),
391 }
392 }
393
394 pub fn new_with_config_no_outputs(
395 config: Option<WorkspaceConfig>,
396 base_options: Rc<Options>,
397 ) -> Self {
398 let original_output = OutputId(
399 config
400 .clone()
401 .and_then(|c| c.open_on_output)
402 .unwrap_or_default(),
403 );
404
405 let scale = smithay::output::Scale::Integer(1);
406 let options =
407 Rc::new(Options::clone(&base_options).adjusted_for_scale(scale.fractional_scale()));
408
409 Self {
410 output: None,
411 scale,
412 transform: Transform::Normal,
413 original_output,
414 view_size: Size::from((1280., 720.)),
415 working_area: Rectangle::from_loc_and_size((0., 0.), (1280., 720.)),
416 columns: vec![],
417 data: vec![],
418 active_column_idx: 0,
419 interactive_resize: None,
420 view_offset: 0.,
421 view_offset_adj: None,
422 activate_prev_column_on_removal: None,
423 view_offset_before_fullscreen: None,
424 closing_windows: vec![],
425 base_options,
426 options,
427 name: config.map(|c| c.name.0),
428 id: WorkspaceId::next(),
429 }
430 }
431
432 pub fn new_no_outputs(options: Rc<Options>) -> Self {
433 Self::new_with_config_no_outputs(None, options)
434 }
435
436 pub fn id(&self) -> WorkspaceId {
437 self.id
438 }
439
440 pub fn unname(&mut self) {
441 self.name = None;
442 }
443
444 pub fn scale(&self) -> smithay::output::Scale {
445 self.scale
446 }
447
448 pub fn advance_animations(&mut self, current_time: Duration) {
449 if let Some(ViewOffsetAdjustment::Animation(anim)) = &mut self.view_offset_adj {
450 anim.set_current_time(current_time);
451 self.view_offset = anim.value();
452 if anim.is_done() {
453 self.view_offset_adj = None;
454 }
455 } else if let Some(ViewOffsetAdjustment::Gesture(gesture)) = &self.view_offset_adj {
456 self.view_offset = gesture.current_view_offset;
457 }
458
459 for col in &mut self.columns {
460 col.advance_animations(current_time);
461 }
462
463 self.closing_windows.retain_mut(|closing| {
464 closing.advance_animations(current_time);
465 closing.are_animations_ongoing()
466 });
467 }
468
469 pub fn are_animations_ongoing(&self) -> bool {
470 self.view_offset_adj
471 .as_ref()
472 .is_some_and(|s| s.is_animation())
473 || self.columns.iter().any(Column::are_animations_ongoing)
474 || !self.closing_windows.is_empty()
475 }
476
477 pub fn are_transitions_ongoing(&self) -> bool {
478 self.view_offset_adj.is_some()
479 || self.columns.iter().any(Column::are_animations_ongoing)
480 || !self.closing_windows.is_empty()
481 }
482
483 pub fn update_render_elements(&mut self, is_active: bool) {
484 let view_pos = Point::from((self.view_pos(), 0.));
485 let view_size = self.view_size();
486 let active_idx = self.active_column_idx;
487 for (col_idx, (col, col_x)) in self.columns_mut().enumerate() {
488 let is_active = is_active && col_idx == active_idx;
489 let col_off = Point::from((col_x, 0.));
490 let col_pos = view_pos - col_off - col.render_offset();
491 let view_rect = Rectangle::from_loc_and_size(col_pos, view_size);
492 col.update_render_elements(is_active, view_rect);
493 }
494 }
495
496 pub fn update_config(&mut self, base_options: Rc<Options>) {
497 let scale = self.scale.fractional_scale();
498 let options = Rc::new(Options::clone(&base_options).adjusted_for_scale(scale));
499
500 for (column, data) in zip(&mut self.columns, &mut self.data) {
501 column.update_config(scale, options.clone());
502 data.update(column);
503 }
504
505 self.base_options = base_options;
506 self.options = options;
507 }
508
509 pub fn update_shaders(&mut self) {
510 for col in &mut self.columns {
511 for tile in &mut col.tiles {
512 tile.update_shaders();
513 }
514 }
515 }
516
517 pub fn windows(&self) -> impl Iterator<Item = &W> + '_ {
518 self.columns
519 .iter()
520 .flat_map(|col| col.tiles.iter())
521 .map(Tile::window)
522 }
523
524 pub fn windows_mut(&mut self) -> impl Iterator<Item = &mut W> + '_ {
525 self.columns
526 .iter_mut()
527 .flat_map(|col| col.tiles.iter_mut())
528 .map(Tile::window_mut)
529 }
530
531 pub fn current_output(&self) -> Option<&Output> {
532 self.output.as_ref()
533 }
534
535 pub fn active_window(&self) -> Option<&W> {
536 if self.columns.is_empty() {
537 return None;
538 }
539
540 let col = &self.columns[self.active_column_idx];
541 Some(col.tiles[col.active_tile_idx].window())
542 }
543
544 pub fn set_output(&mut self, output: Option<Output>) {
545 if self.output == output {
546 return;
547 }
548
549 if let Some(output) = self.output.take() {
550 for win in self.windows() {
551 win.output_leave(&output);
552 }
553 }
554
555 self.output = output;
556
557 if let Some(output) = &self.output {
558 let scale = output.current_scale();
559 let transform = output.current_transform();
560 let working_area = compute_working_area(output, self.options.struts);
561 self.set_view_size(scale, transform, output_size(output), working_area);
562
563 for win in self.windows() {
564 self.enter_output_for_window(win);
565 }
566 }
567 }
568
569 fn enter_output_for_window(&self, window: &W) {
570 if let Some(output) = &self.output {
571 window.set_preferred_scale_transform(self.scale, self.transform);
572 window.output_enter(output);
573 }
574 }
575
576 pub fn set_view_size(
577 &mut self,
578 scale: smithay::output::Scale,
579 transform: Transform,
580 size: Size<f64, Logical>,
581 working_area: Rectangle<f64, Logical>,
582 ) {
583 let scale_transform_changed = self.transform != transform
584 || self.scale.integer_scale() != scale.integer_scale()
585 || self.scale.fractional_scale() != scale.fractional_scale();
586 if !scale_transform_changed && self.view_size == size && self.working_area == working_area {
587 return;
588 }
589
590 let fractional_scale_changed = self.scale.fractional_scale() != scale.fractional_scale();
591
592 self.scale = scale;
593 self.transform = transform;
594 self.view_size = size;
595 self.working_area = working_area;
596
597 if fractional_scale_changed {
598 // Options need to be recomputed for the new scale.
599 self.update_config(self.base_options.clone());
600 }
601
602 for col in &mut self.columns {
603 col.set_view_size(self.view_size, self.working_area);
604 }
605
606 if scale_transform_changed {
607 for window in self.windows() {
608 window.set_preferred_scale_transform(self.scale, self.transform);
609 }
610 }
611 }
612
613 pub fn view_size(&self) -> Size<f64, Logical> {
614 self.view_size
615 }
616
617 fn toplevel_bounds(&self, rules: &ResolvedWindowRules) -> Size<i32, Logical> {
618 let border_config = rules.border.resolve_against(self.options.border);
619 compute_toplevel_bounds(border_config, self.working_area.size, self.options.gaps)
620 }
621
622 pub fn resolve_default_width(
623 &self,
624 default_width: Option<Option<ColumnWidth>>,
625 ) -> Option<ColumnWidth> {
626 match default_width {
627 Some(Some(width)) => Some(width),
628 Some(None) => None,
629 None => self.options.default_width,
630 }
631 }
632
633 pub fn new_window_size(
634 &self,
635 width: Option<ColumnWidth>,
636 rules: &ResolvedWindowRules,
637 ) -> Size<i32, Logical> {
638 let border = rules.border.resolve_against(self.options.border);
639
640 let width = if let Some(width) = width {
641 let is_fixed = matches!(width, ColumnWidth::Fixed(_));
642
643 let mut width = width.resolve(&self.options, self.working_area.size.w);
644
645 if !is_fixed && !border.off {
646 width -= border.width.0 * 2.;
647 }
648
649 max(1, width.floor() as i32)
650 } else {
651 0
652 };
653
654 let mut height = self.working_area.size.h - self.options.gaps * 2.;
655 if !border.off {
656 height -= border.width.0 * 2.;
657 }
658
659 Size::from((width, max(height.floor() as i32, 1)))
660 }
661
662 pub fn configure_new_window(
663 &self,
664 window: &Window,
665 width: Option<ColumnWidth>,
666 rules: &ResolvedWindowRules,
667 ) {
668 window.with_surfaces(|surface, data| {
669 send_scale_transform(surface, data, self.scale, self.transform);
670 });
671
672 window
673 .toplevel()
674 .expect("no x11 support")
675 .with_pending_state(|state| {
676 if state.states.contains(xdg_toplevel::State::Fullscreen) {
677 state.size = Some(self.view_size.to_i32_round());
678 } else {
679 state.size = Some(self.new_window_size(width, rules));
680 }
681
682 state.bounds = Some(self.toplevel_bounds(rules));
683 });
684 }
685
686 fn compute_new_view_offset_for_column_fit(&self, current_x: f64, idx: usize) -> f64 {
687 let col = &self.columns[idx];
688 if col.is_fullscreen {
689 return 0.;
690 }
691
692 let new_col_x = self.column_x(idx);
693
694 let final_x = if let Some(ViewOffsetAdjustment::Animation(anim)) = &self.view_offset_adj {
695 current_x - self.view_offset + anim.to()
696 } else {
697 current_x
698 };
699
700 let new_offset = compute_new_view_offset(
701 final_x + self.working_area.loc.x,
702 self.working_area.size.w,
703 new_col_x,
704 col.width(),
705 self.options.gaps,
706 );
707
708 // Non-fullscreen windows are always offset at least by the working area position.
709 new_offset - self.working_area.loc.x
710 }
711
712 fn compute_new_view_offset_for_column_centered(&self, current_x: f64, idx: usize) -> f64 {
713 let col = &self.columns[idx];
714 if col.is_fullscreen {
715 return self.compute_new_view_offset_for_column_fit(current_x, idx);
716 }
717
718 let width = col.width();
719
720 // Columns wider than the view are left-aligned (the fit code can deal with that).
721 if self.working_area.size.w <= width {
722 return self.compute_new_view_offset_for_column_fit(current_x, idx);
723 }
724
725 -(self.working_area.size.w - width) / 2. - self.working_area.loc.x
726 }
727
728 fn compute_new_view_offset_for_column(
729 &self,
730 current_x: f64,
731 idx: usize,
732 prev_idx: Option<usize>,
733 ) -> f64 {
734 match self.options.center_focused_column {
735 CenterFocusedColumn::Always => {
736 self.compute_new_view_offset_for_column_centered(current_x, idx)
737 }
738 CenterFocusedColumn::OnOverflow => {
739 let Some(prev_idx) = prev_idx else {
740 return self.compute_new_view_offset_for_column_fit(current_x, idx);
741 };
742
743 // Always take the left or right neighbor of the target as the source.
744 let source_idx = if prev_idx > idx {
745 min(idx + 1, self.columns.len() - 1)
746 } else {
747 idx.saturating_sub(1)
748 };
749
750 let source_x = self.column_x(source_idx);
751 let source_width = self.columns[source_idx].width();
752
753 let target_x = self.column_x(idx);
754 let target_width = self.columns[idx].width();
755
756 let total_width = if source_x < target_x {
757 // Source is left from target.
758 target_x - source_x + target_width
759 } else {
760 // Source is right from target.
761 source_x - target_x + source_width
762 } + self.options.gaps * 2.;
763
764 // If it fits together, do a normal animation, otherwise center the new column.
765 if total_width <= self.working_area.size.w {
766 self.compute_new_view_offset_for_column_fit(current_x, idx)
767 } else {
768 self.compute_new_view_offset_for_column_centered(current_x, idx)
769 }
770 }
771 CenterFocusedColumn::Never => {
772 self.compute_new_view_offset_for_column_fit(current_x, idx)
773 }
774 }
775 }
776
777 fn animate_view_offset(&mut self, current_x: f64, idx: usize, new_view_offset: f64) {
778 self.animate_view_offset_with_config(
779 current_x,
780 idx,
781 new_view_offset,
782 self.options.animations.horizontal_view_movement.0,
783 );
784 }
785
786 fn animate_view_offset_with_config(
787 &mut self,
788 current_x: f64,
789 idx: usize,
790 new_view_offset: f64,
791 config: niri_config::Animation,
792 ) {
793 let new_col_x = self.column_x(idx);
794 let old_col_x = current_x - self.view_offset;
795 let offset_delta = old_col_x - new_col_x;
796 self.view_offset += offset_delta;
797
798 let pixel = 1. / self.scale.fractional_scale();
799
800 // If we're already animating towards that, don't restart it.
801 if let Some(ViewOffsetAdjustment::Animation(anim)) = &mut self.view_offset_adj {
802 // Offset the animation for the active column change.
803 anim.offset(offset_delta);
804
805 let to_diff = new_view_offset - anim.to();
806 if (anim.value() - self.view_offset).abs() < pixel && to_diff.abs() < pixel {
807 // Correct for any inaccuracy.
808 anim.offset(to_diff);
809 return;
810 }
811 }
812
813 // If our view offset is already this, we don't need to do anything.
814 if (self.view_offset - new_view_offset).abs() < pixel {
815 // Correct for any inaccuracy.
816 self.view_offset = new_view_offset;
817 self.view_offset_adj = None;
818 return;
819 }
820
821 // FIXME: also compute and use current velocity.
822 self.view_offset_adj = Some(ViewOffsetAdjustment::Animation(Animation::new(
823 self.view_offset,
824 new_view_offset,
825 0.,
826 config,
827 )));
828 }
829
830 fn animate_view_offset_to_column_centered(
831 &mut self,
832 current_x: f64,
833 idx: usize,
834 config: niri_config::Animation,
835 ) {
836 let new_view_offset = self.compute_new_view_offset_for_column_centered(current_x, idx);
837 self.animate_view_offset_with_config(current_x, idx, new_view_offset, config);
838 }
839
840 fn animate_view_offset_to_column_with_config(
841 &mut self,
842 current_x: f64,
843 idx: usize,
844 prev_idx: Option<usize>,
845 config: niri_config::Animation,
846 ) {
847 let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx, prev_idx);
848 self.animate_view_offset_with_config(current_x, idx, new_view_offset, config);
849 }
850
851 fn animate_view_offset_to_column(
852 &mut self,
853 current_x: f64,
854 idx: usize,
855 prev_idx: Option<usize>,
856 ) {
857 self.animate_view_offset_to_column_with_config(
858 current_x,
859 idx,
860 prev_idx,
861 self.options.animations.horizontal_view_movement.0,
862 )
863 }
864
865 fn activate_column(&mut self, idx: usize) {
866 self.activate_column_with_anim_config(
867 idx,
868 self.options.animations.horizontal_view_movement.0,
869 );
870 }
871
872 fn activate_column_with_anim_config(&mut self, idx: usize, config: niri_config::Animation) {
873 if self.active_column_idx == idx {
874 return;
875 }
876
877 let current_x = self.view_pos();
878 self.animate_view_offset_to_column_with_config(
879 current_x,
880 idx,
881 Some(self.active_column_idx),
882 config,
883 );
884
885 self.active_column_idx = idx;
886
887 // A different column was activated; reset the flag.
888 self.activate_prev_column_on_removal = None;
889 self.view_offset_before_fullscreen = None;
890 self.interactive_resize = None;
891 }
892
893 pub fn has_windows(&self) -> bool {
894 self.windows().next().is_some()
895 }
896
897 pub fn has_window(&self, window: &W::Id) -> bool {
898 self.windows().any(|win| win.id() == window)
899 }
900
901 pub fn find_wl_surface(&self, wl_surface: &WlSurface) -> Option<&W> {
902 self.windows().find(|win| win.is_wl_surface(wl_surface))
903 }
904
905 pub fn find_wl_surface_mut(&mut self, wl_surface: &WlSurface) -> Option<&mut W> {
906 self.windows_mut().find(|win| win.is_wl_surface(wl_surface))
907 }
908
909 pub fn add_window_at(
910 &mut self,
911 col_idx: usize,
912 window: W,
913 activate: bool,
914 width: ColumnWidth,
915 is_full_width: bool,
916 ) {
917 let tile = Tile::new(window, self.scale.fractional_scale(), self.options.clone());
918 self.add_tile_at(col_idx, tile, activate, width, is_full_width, None);
919 }
920
921 fn add_tile_at(
922 &mut self,
923 col_idx: usize,
924 tile: Tile<W>,
925 activate: bool,
926 width: ColumnWidth,
927 is_full_width: bool,
928 anim_config: Option<niri_config::Animation>,
929 ) {
930 self.enter_output_for_window(tile.window());
931
932 let was_empty = self.columns.is_empty();
933
934 let column = Column::new_with_tile(
935 tile,
936 self.view_size,
937 self.working_area,
938 self.scale.fractional_scale(),
939 self.options.clone(),
940 width,
941 is_full_width,
942 true,
943 );
944 self.data.insert(col_idx, ColumnData::new(&column));
945 self.columns.insert(col_idx, column);
946
947 if activate {
948 // If this is the first window on an empty workspace, remove the effect of whatever
949 // view_offset was left over and skip the animation.
950 if was_empty {
951 self.view_offset = 0.;
952 self.view_offset_adj = None;
953 self.view_offset =
954 self.compute_new_view_offset_for_column(self.view_pos(), col_idx, None);
955 }
956
957 let prev_offset = (!was_empty).then(|| self.static_view_offset());
958
959 let anim_config =
960 anim_config.unwrap_or(self.options.animations.horizontal_view_movement.0);
961 self.activate_column_with_anim_config(col_idx, anim_config);
962 self.activate_prev_column_on_removal = prev_offset;
963 }
964
965 // Animate movement of other columns.
966 let offset = self.column_x(col_idx + 1) - self.column_x(col_idx);
967 let config = anim_config.unwrap_or(self.options.animations.window_movement.0);
968 if self.active_column_idx <= col_idx {
969 for col in &mut self.columns[col_idx + 1..] {
970 col.animate_move_from_with_config(-offset, config);
971 }
972 } else {
973 for col in &mut self.columns[..col_idx] {
974 col.animate_move_from_with_config(offset, config);
975 }
976 }
977 }
978
979 pub fn add_window(
980 &mut self,
981 window: W,
982 activate: bool,
983 width: ColumnWidth,
984 is_full_width: bool,
985 ) {
986 let col_idx = if self.columns.is_empty() {
987 0
988 } else {
989 self.active_column_idx + 1
990 };
991
992 self.add_window_at(col_idx, window, activate, width, is_full_width);
993 }
994
995 fn add_tile(
996 &mut self,
997 tile: Tile<W>,
998 activate: bool,
999 width: ColumnWidth,
1000 is_full_width: bool,
1001 anim_config: Option<niri_config::Animation>,
1002 ) {
1003 let col_idx = if self.columns.is_empty() {
1004 0
1005 } else {
1006 self.active_column_idx + 1
1007 };
1008
1009 self.add_tile_at(col_idx, tile, activate, width, is_full_width, anim_config);
1010 }
1011
1012 pub fn add_window_right_of(
1013 &mut self,
1014 right_of: &W::Id,
1015 window: W,
1016 width: ColumnWidth,
1017 is_full_width: bool,
1018 ) {
1019 self.enter_output_for_window(&window);
1020
1021 let right_of_idx = self
1022 .columns
1023 .iter()
1024 .position(|col| col.contains(right_of))
1025 .unwrap();
1026 let idx = right_of_idx + 1;
1027
1028 let column = Column::new(
1029 window,
1030 self.view_size,
1031 self.working_area,
1032 self.scale.fractional_scale(),
1033 self.options.clone(),
1034 width,
1035 is_full_width,
1036 true,
1037 );
1038 self.data.insert(idx, ColumnData::new(&column));
1039 self.columns.insert(idx, column);
1040
1041 // Activate the new window if right_of was active.
1042 if self.active_column_idx == right_of_idx {
1043 let prev_offset = self.static_view_offset();
1044 self.activate_column(idx);
1045 self.activate_prev_column_on_removal = Some(prev_offset);
1046 } else if idx <= self.active_column_idx {
1047 self.active_column_idx += 1;
1048 }
1049
1050 // Animate movement of other columns.
1051 let offset = self.column_x(idx + 1) - self.column_x(idx);
1052 if self.active_column_idx <= idx {
1053 for col in &mut self.columns[idx + 1..] {
1054 col.animate_move_from(-offset);
1055 }
1056 } else {
1057 for col in &mut self.columns[..idx] {
1058 col.animate_move_from(offset);
1059 }
1060 }
1061 }
1062
1063 pub fn add_column(&mut self, mut column: Column<W>, activate: bool) {
1064 for tile in &column.tiles {
1065 self.enter_output_for_window(tile.window());
1066 }
1067
1068 let was_empty = self.columns.is_empty();
1069
1070 let idx = if self.columns.is_empty() {
1071 0
1072 } else {
1073 self.active_column_idx + 1
1074 };
1075
1076 column.update_config(self.scale.fractional_scale(), self.options.clone());
1077 column.set_view_size(self.view_size, self.working_area);
1078 self.data.insert(idx, ColumnData::new(&column));
1079 self.columns.insert(idx, column);
1080
1081 if activate {
1082 // If this is the first window on an empty workspace, remove the effect of whatever
1083 // view_offset was left over and skip the animation.
1084 if was_empty {
1085 self.view_offset = 0.;
1086 self.view_offset_adj = None;
1087 self.view_offset =
1088 self.compute_new_view_offset_for_column(self.view_pos(), idx, None);
1089 }
1090
1091 let prev_offset = (!was_empty).then(|| self.static_view_offset());
1092
1093 self.activate_column(idx);
1094 self.activate_prev_column_on_removal = prev_offset;
1095 }
1096
1097 // Animate movement of other columns.
1098 let offset = self.column_x(idx + 1) - self.column_x(idx);
1099 if self.active_column_idx <= idx {
1100 for col in &mut self.columns[idx + 1..] {
1101 col.animate_move_from(-offset);
1102 }
1103 } else {
1104 for col in &mut self.columns[..idx] {
1105 col.animate_move_from(offset);
1106 }
1107 }
1108 }
1109
1110 pub fn remove_tile_by_idx(
1111 &mut self,
1112 column_idx: usize,
1113 window_idx: usize,
1114 transaction: Transaction,
1115 anim_config: Option<niri_config::Animation>,
1116 ) -> Tile<W> {
1117 let offset = self.column_x(column_idx + 1) - self.column_x(column_idx);
1118
1119 let column = &mut self.columns[column_idx];
1120 let prev_width = self.data[column_idx].width;
1121
1122 // Animate movement of other tiles.
1123 // FIXME: tiles can move by X too, in a centered or resizing layout with one window smaller
1124 // than the others.
1125 let offset_y = column.tile_offset(window_idx + 1).y - column.tile_offset(window_idx).y;
1126 for tile in &mut column.tiles[window_idx + 1..] {
1127 tile.animate_move_y_from(offset_y);
1128 }
1129
1130 let tile = column.tiles.remove(window_idx);
1131 column.data.remove(window_idx);
1132
1133 // If one window is left, reset its weight to 1.
1134 if column.data.len() == 1 {
1135 if let WindowHeight::Auto { weight } = &mut column.data[0].height {
1136 *weight = 1.;
1137 }
1138 }
1139
1140 if let Some(output) = &self.output {
1141 tile.window().output_leave(output);
1142 }
1143
1144 // Stop interactive resize.
1145 if let Some(resize) = &self.interactive_resize {
1146 if tile.window().id() == &resize.window {
1147 self.interactive_resize = None;
1148 }
1149 }
1150
1151 let became_empty = column.tiles.is_empty();
1152 let offset = if became_empty {
1153 offset
1154 } else {
1155 column.active_tile_idx = min(column.active_tile_idx, column.tiles.len() - 1);
1156 column.update_tile_sizes_with_transaction(true, transaction);
1157 self.data[column_idx].update(column);
1158
1159 prev_width - column.width()
1160 };
1161
1162 // Animate movement of the other columns.
1163 let movement_config = anim_config.unwrap_or(self.options.animations.window_movement.0);
1164 if self.active_column_idx <= column_idx {
1165 for col in &mut self.columns[column_idx + 1..] {
1166 col.animate_move_from_with_config(offset, movement_config);
1167 }
1168 } else {
1169 for col in &mut self.columns[..=column_idx] {
1170 col.animate_move_from_with_config(-offset, movement_config);
1171 }
1172 }
1173
1174 if became_empty {
1175 if column_idx + 1 == self.active_column_idx {
1176 // The previous column, that we were going to activate upon removal of the active
1177 // column, has just been itself removed.
1178 self.activate_prev_column_on_removal = None;
1179 }
1180
1181 if column_idx == self.active_column_idx {
1182 self.view_offset_before_fullscreen = None;
1183 }
1184
1185 self.columns.remove(column_idx);
1186 self.data.remove(column_idx);
1187 if self.columns.is_empty() {
1188 return tile;
1189 }
1190
1191 let view_config =
1192 anim_config.unwrap_or(self.options.animations.horizontal_view_movement.0);
1193
1194 if column_idx < self.active_column_idx {
1195 // A column to the left was removed; preserve the current position.
1196 // FIXME: preserve activate_prev_column_on_removal.
1197 self.active_column_idx -= 1;
1198 self.activate_prev_column_on_removal = None;
1199 } else if column_idx == self.active_column_idx
1200 && self.activate_prev_column_on_removal.is_some()
1201 {
1202 // The active column was removed, and we needed to activate the previous column.
1203 if 0 < column_idx {
1204 let prev_offset = self.activate_prev_column_on_removal.unwrap();
1205
1206 self.activate_column_with_anim_config(self.active_column_idx - 1, view_config);
1207
1208 // Restore the view offset but make sure to scroll the view in case the
1209 // previous window had resized.
1210 let current_x = self.view_pos();
1211 self.animate_view_offset_with_config(
1212 current_x,
1213 self.active_column_idx,
1214 prev_offset,
1215 view_config,
1216 );
1217 self.animate_view_offset_to_column_with_config(
1218 current_x,
1219 self.active_column_idx,
1220 None,
1221 view_config,
1222 );
1223 }
1224 } else {
1225 self.activate_column_with_anim_config(
1226 min(self.active_column_idx, self.columns.len() - 1),
1227 view_config,
1228 );
1229 }
1230
1231 return tile;
1232 }
1233
1234 tile
1235 }
1236
1237 pub fn remove_column_by_idx(&mut self, column_idx: usize) -> Column<W> {
1238 // Animate movement of the other columns.
1239 let offset = self.column_x(column_idx + 1) - self.column_x(column_idx);
1240 if self.active_column_idx <= column_idx {
1241 for col in &mut self.columns[column_idx + 1..] {
1242 col.animate_move_from(offset);
1243 }
1244 } else {
1245 for col in &mut self.columns[..column_idx] {
1246 col.animate_move_from(-offset);
1247 }
1248 }
1249
1250 let column = self.columns.remove(column_idx);
1251 self.data.remove(column_idx);
1252
1253 if let Some(output) = &self.output {
1254 for tile in &column.tiles {
1255 tile.window().output_leave(output);
1256 }
1257 }
1258
1259 // Stop interactive resize.
1260 if let Some(resize) = &self.interactive_resize {
1261 if column
1262 .tiles
1263 .iter()
1264 .any(|tile| tile.window().id() == &resize.window)
1265 {
1266 self.interactive_resize = None;
1267 }
1268 }
1269
1270 if column_idx + 1 == self.active_column_idx {
1271 // The previous column, that we were going to activate upon removal of the active
1272 // column, has just been itself removed.
1273 self.activate_prev_column_on_removal = None;
1274 }
1275
1276 if column_idx == self.active_column_idx {
1277 self.view_offset_before_fullscreen = None;
1278 }
1279
1280 if self.columns.is_empty() {
1281 return column;
1282 }
1283
1284 if column_idx < self.active_column_idx {
1285 // A column to the left was removed; preserve the current position.
1286 // FIXME: preserve activate_prev_column_on_removal.
1287 self.active_column_idx -= 1;
1288 self.activate_prev_column_on_removal = None;
1289 } else if column_idx == self.active_column_idx
1290 && self.activate_prev_column_on_removal.is_some()
1291 {
1292 // The active column was removed, and we needed to activate the previous column.
1293 if 0 < column_idx {
1294 let prev_offset = self.activate_prev_column_on_removal.unwrap();
1295
1296 self.activate_column(self.active_column_idx - 1);
1297
1298 // Restore the view offset but make sure to scroll the view in case the
1299 // previous window had resized.
1300 let current_x = self.view_pos();
1301 self.animate_view_offset(current_x, self.active_column_idx, prev_offset);
1302 self.animate_view_offset_to_column(current_x, self.active_column_idx, None);
1303 }
1304 } else {
1305 self.activate_column(min(self.active_column_idx, self.columns.len() - 1));
1306 }
1307
1308 column
1309 }
1310
1311 pub fn remove_window(&mut self, window: &W::Id, transaction: Transaction) -> W {
1312 let column_idx = self
1313 .columns
1314 .iter()
1315 .position(|col| col.contains(window))
1316 .unwrap();
1317 let column = &self.columns[column_idx];
1318
1319 let window_idx = column.position(window).unwrap();
1320 self.remove_tile_by_idx(column_idx, window_idx, transaction, None)
1321 .into_window()
1322 }
1323
1324 pub fn update_window(&mut self, window: &W::Id, serial: Option<Serial>) {
1325 let (col_idx, column) = self
1326 .columns
1327 .iter_mut()
1328 .enumerate()
1329 .find(|(_, col)| col.contains(window))
1330 .unwrap();
1331 let (tile_idx, tile) = column
1332 .tiles
1333 .iter_mut()
1334 .enumerate()
1335 .find(|(_, tile)| tile.window().id() == window)
1336 .unwrap();
1337
1338 let was_fullscreen = tile.is_fullscreen();
1339 let resize = tile.window_mut().interactive_resize_data();
1340
1341 // Do this before calling update_window() so it can get up-to-date info.
1342 if let Some(serial) = serial {
1343 tile.window_mut().update_interactive_resize(serial);
1344 }
1345
1346 let prev_width = self.data[col_idx].width;
1347
1348 column.update_window(window);
1349 self.data[col_idx].update(column);
1350 column.update_tile_sizes(false);
1351
1352 let offset = prev_width - self.data[col_idx].width;
1353
1354 // Move other columns in tandem with resizing.
1355 let started_resize_anim =
1356 column.tiles[tile_idx].resize_animation().is_some() && offset != 0.;
1357 if started_resize_anim {
1358 if self.active_column_idx <= col_idx {
1359 for col in &mut self.columns[col_idx + 1..] {
1360 col.animate_move_from_with_config(
1361 offset,
1362 self.options.animations.window_resize.anim,
1363 );
1364 }
1365 } else {
1366 for col in &mut self.columns[..=col_idx] {
1367 col.animate_move_from_with_config(
1368 -offset,
1369 self.options.animations.window_resize.anim,
1370 );
1371 }
1372 }
1373 }
1374
1375 if col_idx == self.active_column_idx {
1376 // If offset == 0, then don't mess with the view or the gesture. Some clients (Firefox,
1377 // Chromium, Electron) currently don't commit after the ack of a configure that drops
1378 // the Resizing state, which can trigger this code path for a while.
1379 let resize = if offset != 0. { resize } else { None };
1380 if let Some(resize) = resize {
1381 // If this is an interactive resize commit of an active window, then we need to
1382 // either preserve the view offset or adjust it accordingly.
1383 let centered = self.options.center_focused_column == CenterFocusedColumn::Always;
1384
1385 let width = self.data[col_idx].width;
1386 let offset = if centered {
1387 // FIXME: when view_offset becomes fractional, this can be made additive too.
1388 let new_offset =
1389 -(self.working_area.size.w - width) / 2. - self.working_area.loc.x;
1390 new_offset - self.view_offset
1391 } else if resize.edges.contains(ResizeEdge::LEFT) {
1392 -offset
1393 } else {
1394 0.
1395 };
1396
1397 self.view_offset += offset;
1398 if let Some(ViewOffsetAdjustment::Animation(anim)) = &mut self.view_offset_adj {
1399 anim.offset(offset);
1400 } else {
1401 // Don't bother with the gesture.
1402 self.view_offset_adj = None;
1403 }
1404 }
1405
1406 if self.interactive_resize.is_none()
1407 && !matches!(self.view_offset_adj, Some(ViewOffsetAdjustment::Gesture(_)))
1408 {
1409 // We might need to move the view to ensure the resized window is still visible.
1410 let current_x = self.view_pos();
1411
1412 // Upon unfullscreening, restore the view offset.
1413 let is_fullscreen = self.columns[col_idx].tiles[tile_idx].is_fullscreen();
1414 if was_fullscreen && !is_fullscreen {
1415 if let Some(prev_offset) = self.view_offset_before_fullscreen.take() {
1416 self.animate_view_offset(current_x, col_idx, prev_offset);
1417 }
1418 }
1419
1420 // Synchronize the horizontal view movement with the resize so that it looks nice.
1421 // This is especially important for always-centered view.
1422 let config = if started_resize_anim {
1423 self.options.animations.window_resize.anim
1424 } else {
1425 self.options.animations.horizontal_view_movement.0
1426 };
1427
1428 // FIXME: we will want to skip the animation in some cases here to make continuously
1429 // resizing windows not look janky.
1430 self.animate_view_offset_to_column_with_config(current_x, col_idx, None, config);
1431 }
1432 }
1433 }
1434
1435 pub fn scroll_amount_to_activate(&self, window: &W::Id) -> f64 {
1436 let column_idx = self
1437 .columns
1438 .iter()
1439 .position(|col| col.contains(window))
1440 .unwrap();
1441
1442 if self.active_column_idx == column_idx {
1443 return 0.;
1444 }
1445
1446 let current_x = self.view_pos();
1447 let new_view_offset = self.compute_new_view_offset_for_column(
1448 current_x,
1449 column_idx,
1450 Some(self.active_column_idx),
1451 );
1452
1453 // Consider the end of an ongoing animation because that's what compute to fit does too.
1454 let final_x = if let Some(ViewOffsetAdjustment::Animation(anim)) = &self.view_offset_adj {
1455 current_x - self.view_offset + anim.to()
1456 } else {
1457 current_x
1458 };
1459
1460 let new_col_x = self.column_x(column_idx);
1461 let from_view_offset = final_x - new_col_x;
1462
1463 (from_view_offset - new_view_offset).abs() / self.working_area.size.w
1464 }
1465
1466 pub fn activate_window(&mut self, window: &W::Id) {
1467 let column_idx = self
1468 .columns
1469 .iter()
1470 .position(|col| col.contains(window))
1471 .unwrap();
1472 let column = &mut self.columns[column_idx];
1473
1474 column.activate_window(window);
1475 self.activate_column(column_idx);
1476 }
1477
1478 pub fn store_unmap_snapshot_if_empty(&mut self, renderer: &mut GlesRenderer, window: &W::Id) {
1479 let output_scale = Scale::from(self.scale.fractional_scale());
1480 let view_size = self.view_size();
1481 for (tile, tile_pos) in self.tiles_with_render_positions_mut(false) {
1482 if tile.window().id() == window {
1483 let view_pos = Point::from((-tile_pos.x, -tile_pos.y));
1484 let view_rect = Rectangle::from_loc_and_size(view_pos, view_size);
1485 tile.update(false, view_rect);
1486 tile.store_unmap_snapshot_if_empty(renderer, output_scale);
1487 return;
1488 }
1489 }
1490 }
1491
1492 pub fn clear_unmap_snapshot(&mut self, window: &W::Id) {
1493 for col in &mut self.columns {
1494 for tile in &mut col.tiles {
1495 if tile.window().id() == window {
1496 let _ = tile.take_unmap_snapshot();
1497 return;
1498 }
1499 }
1500 }
1501 }
1502
1503 pub fn start_close_animation_for_window(
1504 &mut self,
1505 renderer: &mut GlesRenderer,
1506 window: &W::Id,
1507 blocker: TransactionBlocker,
1508 ) {
1509 let output_scale = Scale::from(self.scale.fractional_scale());
1510
1511 let (tile, mut tile_pos) = self
1512 .tiles_with_render_positions_mut(false)
1513 .find(|(tile, _)| tile.window().id() == window)
1514 .unwrap();
1515
1516 let Some(snapshot) = tile.take_unmap_snapshot() else {
1517 return;
1518 };
1519
1520 let tile_size = tile.tile_size();
1521
1522 let (col_idx, tile_idx) = self
1523 .columns
1524 .iter()
1525 .enumerate()
1526 .find_map(|(col_idx, col)| {
1527 col.tiles
1528 .iter()
1529 .position(|tile| tile.window().id() == window)
1530 .map(move |tile_idx| (col_idx, tile_idx))
1531 })
1532 .unwrap();
1533
1534 let col = &self.columns[col_idx];
1535 let removing_last = col.tiles.len() == 1;
1536
1537 tile_pos.x += self.view_pos();
1538
1539 if col_idx < self.active_column_idx {
1540 let offset = if removing_last {
1541 self.column_x(col_idx + 1) - self.column_x(col_idx)
1542 } else {
1543 self.data[col_idx].width
1544 - col
1545 .data
1546 .iter()
1547 .enumerate()
1548 .filter_map(|(idx, data)| {
1549 (idx != tile_idx).then_some(NotNan::new(data.size.w).unwrap())
1550 })
1551 .max()
1552 .map(NotNan::into_inner)
1553 .unwrap()
1554 };
1555 tile_pos.x -= offset;
1556 }
1557
1558 let anim = Animation::new(0., 1., 0., self.options.animations.window_close.anim);
1559
1560 let blocker = if self.options.disable_transactions {
1561 TransactionBlocker::completed()
1562 } else {
1563 blocker
1564 };
1565
1566 let res = ClosingWindow::new(
1567 renderer,
1568 snapshot,
1569 output_scale,
1570 tile_size,
1571 tile_pos,
1572 blocker,
1573 anim,
1574 );
1575 match res {
1576 Ok(closing) => {
1577 self.closing_windows.push(closing);
1578 }
1579 Err(err) => {
1580 warn!("error creating a closing window animation: {err:?}");
1581 }
1582 }
1583 }
1584
1585 #[cfg(test)]
1586 pub fn verify_invariants(&self) {
1587 use approx::assert_abs_diff_eq;
1588
1589 let scale = self.scale.fractional_scale();
1590 assert!(self.view_size.w > 0.);
1591 assert!(self.view_size.h > 0.);
1592 assert!(scale > 0.);
1593 assert!(scale.is_finite());
1594 assert_eq!(self.columns.len(), self.data.len());
1595
1596 if !self.columns.is_empty() {
1597 assert!(self.active_column_idx < self.columns.len());
1598
1599 for (column, data) in zip(&self.columns, &self.data) {
1600 assert!(Rc::ptr_eq(&self.options, &column.options));
1601 assert_eq!(self.scale.fractional_scale(), column.scale);
1602 column.verify_invariants();
1603
1604 let mut data2 = *data;
1605 data2.update(column);
1606 assert_eq!(data, &data2, "column data must be up to date");
1607 }
1608
1609 let col = &self.columns[self.active_column_idx];
1610
1611 // When we have an unfullscreen view offset stored, the active column should have a
1612 // fullscreen tile.
1613 if self.view_offset_before_fullscreen.is_some() {
1614 assert!(
1615 col.is_fullscreen
1616 || col.tiles.iter().any(|tile| {
1617 tile.is_fullscreen() || tile.window().is_pending_fullscreen()
1618 })
1619 );
1620 }
1621
1622 for (_, tile_pos) in self.tiles_with_render_positions() {
1623 let rounded_pos = tile_pos.to_physical_precise_round(scale).to_logical(scale);
1624
1625 // Tile positions must be rounded to physical pixels.
1626 assert_abs_diff_eq!(tile_pos.x, rounded_pos.x, epsilon = 1e-5);
1627 assert_abs_diff_eq!(tile_pos.y, rounded_pos.y, epsilon = 1e-5);
1628 }
1629 }
1630
1631 if let Some(resize) = &self.interactive_resize {
1632 assert!(
1633 self.columns
1634 .iter()
1635 .flat_map(|col| &col.tiles)
1636 .any(|tile| tile.window().id() == &resize.window),
1637 "interactive resize window must be present on the workspace"
1638 );
1639 }
1640 }
1641
1642 pub fn focus_left(&mut self) {
1643 self.activate_column(self.active_column_idx.saturating_sub(1));
1644 }
1645
1646 pub fn focus_right(&mut self) {
1647 if self.columns.is_empty() {
1648 return;
1649 }
1650
1651 self.activate_column(min(self.active_column_idx + 1, self.columns.len() - 1));
1652 }
1653
1654 pub fn focus_column_first(&mut self) {
1655 self.activate_column(0);
1656 }
1657
1658 pub fn focus_column_last(&mut self) {
1659 if self.columns.is_empty() {
1660 return;
1661 }
1662
1663 self.activate_column(self.columns.len() - 1);
1664 }
1665
1666 pub fn focus_column_right_or_first(&mut self) {
1667 if self.columns.is_empty() {
1668 return;
1669 }
1670
1671 let column_idx = (self.active_column_idx + 1) % self.columns.len();
1672 self.activate_column(column_idx);
1673 }
1674
1675 pub fn focus_column_left_or_last(&mut self) {
1676 if self.columns.is_empty() {
1677 return;
1678 }
1679
1680 let column_idx = if self.active_column_idx == 0 {
1681 self.columns.len() - 1
1682 } else {
1683 self.active_column_idx - 1
1684 };
1685 self.activate_column(column_idx);
1686 }
1687
1688 pub fn focus_down(&mut self) {
1689 if self.columns.is_empty() {
1690 return;
1691 }
1692
1693 self.columns[self.active_column_idx].focus_down();
1694 }
1695
1696 pub fn focus_up(&mut self) {
1697 if self.columns.is_empty() {
1698 return;
1699 }
1700
1701 self.columns[self.active_column_idx].focus_up();
1702 }
1703
1704 fn move_column_to(&mut self, new_idx: usize) {
1705 if self.active_column_idx == new_idx {
1706 return;
1707 }
1708
1709 let current_col_x = self.column_x(self.active_column_idx);
1710 let next_col_x = self.column_x(self.active_column_idx + 1);
1711
1712 let mut column = self.columns.remove(self.active_column_idx);
1713 let data = self.data.remove(self.active_column_idx);
1714 cancel_resize_for_column(&mut self.interactive_resize, &mut column);
1715 self.columns.insert(new_idx, column);
1716 self.data.insert(new_idx, data);
1717
1718 // Preserve the camera position when moving to the left.
1719 let view_offset_delta = -self.column_x(self.active_column_idx) + current_col_x;
1720 self.view_offset += view_offset_delta;
1721 if let Some(ViewOffsetAdjustment::Animation(anim)) = &mut self.view_offset_adj {
1722 anim.offset(view_offset_delta);
1723 }
1724
1725 // The column we just moved is offset by the difference between its new and old position.
1726 let new_col_x = self.column_x(new_idx);
1727 self.columns[new_idx].animate_move_from(current_col_x - new_col_x);
1728
1729 // All columns in between moved by the width of the column that we just moved.
1730 let others_x_offset = next_col_x - current_col_x;
1731 if self.active_column_idx < new_idx {
1732 for col in &mut self.columns[self.active_column_idx..new_idx] {
1733 col.animate_move_from(others_x_offset);
1734 }
1735 } else {
1736 for col in &mut self.columns[new_idx + 1..=self.active_column_idx] {
1737 col.animate_move_from(-others_x_offset);
1738 }
1739 }
1740
1741 self.activate_column_with_anim_config(new_idx, self.options.animations.window_movement.0);
1742 }
1743
1744 pub fn move_left(&mut self) {
1745 let new_idx = self.active_column_idx.saturating_sub(1);
1746 self.move_column_to(new_idx);
1747 }
1748
1749 pub fn move_right(&mut self) {
1750 if self.columns.is_empty() {
1751 return;
1752 }
1753
1754 let new_idx = min(self.active_column_idx + 1, self.columns.len() - 1);
1755 self.move_column_to(new_idx);
1756 }
1757
1758 pub fn move_column_to_first(&mut self) {
1759 self.move_column_to(0);
1760 }
1761
1762 pub fn move_column_to_last(&mut self) {
1763 if self.columns.is_empty() {
1764 return;
1765 }
1766
1767 let new_idx = self.columns.len() - 1;
1768 self.move_column_to(new_idx);
1769 }
1770
1771 pub fn move_down(&mut self) {
1772 if self.columns.is_empty() {
1773 return;
1774 }
1775
1776 self.columns[self.active_column_idx].move_down();
1777 }
1778
1779 pub fn move_up(&mut self) {
1780 if self.columns.is_empty() {
1781 return;
1782 }
1783
1784 self.columns[self.active_column_idx].move_up();
1785 }
1786
1787 pub fn consume_or_expel_window_left(&mut self) {
1788 if self.columns.is_empty() {
1789 return;
1790 }
1791
1792 let source_col_idx = self.active_column_idx;
1793 let source_column = &self.columns[source_col_idx];
1794 let prev_off = source_column.tile_offset(source_column.active_tile_idx);
1795
1796 if source_column.tiles.len() == 1 {
1797 if self.active_column_idx == 0 {
1798 return;
1799 }
1800
1801 let offset = self.column_x(source_col_idx) - self.column_x(source_col_idx - 1);
1802 let mut offset = Point::from((offset, 0.));
1803
1804 // Move into adjacent column.
1805 let target_column_idx = source_col_idx - 1;
1806
1807 // Make sure the previous (target) column is activated so the animation looks right.
1808 self.activate_prev_column_on_removal = Some(self.static_view_offset() + offset.x);
1809 offset.x += self.columns[source_col_idx].render_offset().x;
1810 let tile = self.remove_tile_by_idx(
1811 source_col_idx,
1812 0,
1813 Transaction::new(),
1814 Some(self.options.animations.window_movement.0),
1815 );
1816 self.enter_output_for_window(tile.window());
1817
1818 let next_col_idx = source_col_idx;
1819 let prev_next_x = self.column_x(next_col_idx);
1820
1821 let target_column = &mut self.columns[target_column_idx];
1822 offset.x -= target_column.render_offset().x;
1823
1824 target_column.add_tile(tile, true);
1825 self.data[target_column_idx].update(target_column);
1826 target_column.focus_last();
1827
1828 offset += prev_off - target_column.tile_offset(target_column.tiles.len() - 1);
1829
1830 let new_tile = target_column.tiles.last_mut().unwrap();
1831 new_tile.animate_move_from(offset);
1832
1833 // Consuming a window into a column could've increased its width if the new window had a
1834 // larger min width. Move the next columns to account for this.
1835 let offset_next = prev_next_x - self.column_x(next_col_idx);
1836 for col in &mut self.columns[next_col_idx..] {
1837 col.animate_move_from(offset_next);
1838 }
1839 } else {
1840 // Move out of column.
1841 let width = source_column.width;
1842 let is_full_width = source_column.is_full_width;
1843
1844 let mut offset = Point::from((source_column.render_offset().x, 0.));
1845
1846 let tile = self.remove_tile_by_idx(
1847 source_col_idx,
1848 source_column.active_tile_idx,
1849 Transaction::new(),
1850 None,
1851 );
1852
1853 self.add_tile_at(
1854 self.active_column_idx,
1855 tile,
1856 true,
1857 width,
1858 is_full_width,
1859 Some(self.options.animations.window_movement.0),
1860 );
1861
1862 // We added to the left, don't activate even further left on removal.
1863 self.activate_prev_column_on_removal = None;
1864
1865 let new_col = &mut self.columns[self.active_column_idx];
1866 offset += prev_off - new_col.tile_offset(0);
1867 new_col.tiles[0].animate_move_from(offset);
1868 }
1869 }
1870
1871 pub fn consume_or_expel_window_right(&mut self) {
1872 if self.columns.is_empty() {
1873 return;
1874 }
1875
1876 let source_col_idx = self.active_column_idx;
1877 let offset = self.column_x(source_col_idx) - self.column_x(source_col_idx + 1);
1878 let mut offset = Point::from((offset, 0.));
1879
1880 let source_column = &self.columns[source_col_idx];
1881 offset.x += source_column.render_offset().x;
1882 let prev_off = source_column.tile_offset(source_column.active_tile_idx);
1883
1884 if source_column.tiles.len() == 1 {
1885 if self.active_column_idx + 1 == self.columns.len() {
1886 return;
1887 }
1888
1889 // Move into adjacent column.
1890 let target_column_idx = source_col_idx;
1891
1892 offset.x -= self.columns[source_col_idx + 1].render_offset().x;
1893
1894 // Make sure the target column gets activated.
1895 self.activate_prev_column_on_removal = None;
1896 let tile = self.remove_tile_by_idx(
1897 source_col_idx,
1898 0,
1899 Transaction::new(),
1900 Some(self.options.animations.window_movement.0),
1901 );
1902 self.enter_output_for_window(tile.window());
1903
1904 let prev_next_x = self.column_x(target_column_idx + 1);
1905
1906 let target_column = &mut self.columns[target_column_idx];
1907 target_column.add_tile(tile, true);
1908 self.data[target_column_idx].update(target_column);
1909 target_column.focus_last();
1910
1911 offset += prev_off - target_column.tile_offset(target_column.tiles.len() - 1);
1912
1913 let new_tile = target_column.tiles.last_mut().unwrap();
1914 new_tile.animate_move_from(offset);
1915
1916 // Consuming a window into a column could've increased its width if the new window had a
1917 // larger min width. Move the next columns to account for this.
1918 let offset_next = prev_next_x - self.column_x(target_column_idx + 1);
1919 for col in &mut self.columns[target_column_idx + 1..] {
1920 col.animate_move_from(offset_next);
1921 }
1922 } else {
1923 // Move out of column.
1924 let width = source_column.width;
1925 let is_full_width = source_column.is_full_width;
1926
1927 let tile = self.remove_tile_by_idx(
1928 source_col_idx,
1929 source_column.active_tile_idx,
1930 Transaction::new(),
1931 None,
1932 );
1933
1934 self.add_tile(
1935 tile,
1936 true,
1937 width,
1938 is_full_width,
1939 Some(self.options.animations.window_movement.0),
1940 );
1941
1942 let new_col = &mut self.columns[self.active_column_idx];
1943 offset += prev_off - new_col.tile_offset(0);
1944 new_col.tiles[0].animate_move_from(offset);
1945 }
1946 }
1947
1948 pub fn consume_into_column(&mut self) {
1949 if self.columns.len() < 2 {
1950 return;
1951 }
1952
1953 if self.active_column_idx == self.columns.len() - 1 {
1954 return;
1955 }
1956
1957 let source_column_idx = self.active_column_idx + 1;
1958
1959 let offset = self.column_x(source_column_idx)
1960 + self.columns[source_column_idx].render_offset().x
1961 - self.column_x(self.active_column_idx);
1962 let mut offset = Point::from((offset, 0.));
1963 let prev_off = self.columns[source_column_idx].tile_offset(0);
1964
1965 let tile = self.remove_tile_by_idx(source_column_idx, 0, Transaction::new(), None);
1966 self.enter_output_for_window(tile.window());
1967
1968 let prev_next_x = self.column_x(self.active_column_idx + 1);
1969
1970 let target_column = &mut self.columns[self.active_column_idx];
1971 let was_fullscreen = target_column.tiles[target_column.active_tile_idx].is_fullscreen();
1972
1973 target_column.add_tile(tile, true);
1974 self.data[self.active_column_idx].update(target_column);
1975
1976 offset += prev_off - target_column.tile_offset(target_column.tiles.len() - 1);
1977
1978 if !was_fullscreen {
1979 self.view_offset_before_fullscreen = None;
1980 }
1981
1982 offset.x -= target_column.render_offset().x;
1983
1984 let new_tile = target_column.tiles.last_mut().unwrap();
1985 new_tile.animate_move_from(offset);
1986
1987 // Consuming a window into a column could've increased its width if the new window had a
1988 // larger min width. Move the next columns to account for this.
1989 let offset_next = prev_next_x - self.column_x(self.active_column_idx + 1);
1990 for col in &mut self.columns[self.active_column_idx + 1..] {
1991 col.animate_move_from(offset_next);
1992 }
1993 }
1994
1995 pub fn expel_from_column(&mut self) {
1996 if self.columns.is_empty() {
1997 return;
1998 }
1999
2000 let offset =
2001 self.column_x(self.active_column_idx) - self.column_x(self.active_column_idx + 1);
2002 let mut offset = Point::from((offset, 0.));
2003
2004 let source_column = &self.columns[self.active_column_idx];
2005 if source_column.tiles.len() == 1 {
2006 return;
2007 }
2008
2009 offset.x += source_column.render_offset().x;
2010 let prev_off = source_column.tile_offset(source_column.active_tile_idx);
2011
2012 let width = source_column.width;
2013 let is_full_width = source_column.is_full_width;
2014 let tile = self.remove_tile_by_idx(
2015 self.active_column_idx,
2016 source_column.active_tile_idx,
2017 Transaction::new(),
2018 None,
2019 );
2020
2021 self.add_tile(
2022 tile,
2023 true,
2024 width,
2025 is_full_width,
2026 Some(self.options.animations.window_movement.0),
2027 );
2028
2029 let new_col = &mut self.columns[self.active_column_idx];
2030 offset += prev_off - new_col.tile_offset(0);
2031 new_col.tiles[0].animate_move_from(offset);
2032 }
2033
2034 pub fn center_column(&mut self) {
2035 if self.columns.is_empty() {
2036 return;
2037 }
2038
2039 let center_x = self.view_pos();
2040 self.animate_view_offset_to_column_centered(
2041 center_x,
2042 self.active_column_idx,
2043 self.options.animations.horizontal_view_movement.0,
2044 );
2045
2046 let col = &mut self.columns[self.active_column_idx];
2047 cancel_resize_for_column(&mut self.interactive_resize, col);
2048 }
2049
2050 fn view_pos(&self) -> f64 {
2051 self.column_x(self.active_column_idx) + self.view_offset
2052 }
2053
2054 /// Returns a view offset value suitable for saving and later restoration.
2055 ///
2056 /// This means that it shouldn't return an in-progress animation or gesture value.
2057 fn static_view_offset(&self) -> f64 {
2058 match &self.view_offset_adj {
2059 // For animations we can return the final value.
2060 Some(ViewOffsetAdjustment::Animation(anim)) => anim.to(),
2061 Some(ViewOffsetAdjustment::Gesture(gesture)) => gesture.static_view_offset,
2062 _ => self.view_offset,
2063 }
2064 }
2065
2066 // HACK: pass a self.data iterator in manually as a workaround for the lack of method partial
2067 // borrowing. Note that this method's return value does not borrow the entire &Self!
2068 fn column_xs(&self, data: impl Iterator<Item = ColumnData>) -> impl Iterator<Item = f64> {
2069 let gaps = self.options.gaps;
2070 let mut x = 0.;
2071
2072 // Chain with a dummy value to be able to get one past all columns' X.
2073 let dummy = ColumnData { width: 0. };
2074 let data = data.chain(iter::once(dummy));
2075
2076 data.map(move |data| {
2077 let rv = x;
2078 x += data.width + gaps;
2079 rv
2080 })
2081 }
2082
2083 fn column_x(&self, column_idx: usize) -> f64 {
2084 self.column_xs(self.data.iter().copied())
2085 .nth(column_idx)
2086 .unwrap()
2087 }
2088
2089 fn column_xs_in_render_order(
2090 &self,
2091 data: impl Iterator<Item = ColumnData>,
2092 ) -> impl Iterator<Item = f64> {
2093 let active_idx = self.active_column_idx;
2094 let active_pos = self.column_x(active_idx);
2095 let offsets = self
2096 .column_xs(data)
2097 .enumerate()
2098 .filter_map(move |(idx, pos)| (idx != active_idx).then_some(pos));
2099 iter::once(active_pos).chain(offsets)
2100 }
2101
2102 fn columns_mut(&mut self) -> impl Iterator<Item = (&mut Column<W>, f64)> + '_ {
2103 let offsets = self.column_xs(self.data.iter().copied());
2104 zip(&mut self.columns, offsets)
2105 }
2106
2107 fn columns_in_render_order(&self) -> impl Iterator<Item = (&Column<W>, f64)> + '_ {
2108 let offsets = self.column_xs_in_render_order(self.data.iter().copied());
2109
2110 let (first, rest) = self.columns.split_at(self.active_column_idx);
2111 let (active, rest) = rest.split_at(1);
2112
2113 let tiles = active.iter().chain(first).chain(rest);
2114 zip(tiles, offsets)
2115 }
2116
2117 fn columns_in_render_order_mut(&mut self) -> impl Iterator<Item = (&mut Column<W>, f64)> + '_ {
2118 let offsets = self.column_xs_in_render_order(self.data.iter().copied());
2119
2120 let (first, rest) = self.columns.split_at_mut(self.active_column_idx);
2121 let (active, rest) = rest.split_at_mut(1);
2122
2123 let tiles = active.iter_mut().chain(first).chain(rest);
2124 zip(tiles, offsets)
2125 }
2126
2127 fn tiles_with_render_positions(&self) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>)> {
2128 let scale = self.scale.fractional_scale();
2129 let view_off = Point::from((-self.view_pos(), 0.));
2130 self.columns_in_render_order()
2131 .flat_map(move |(col, col_x)| {
2132 let col_off = Point::from((col_x, 0.));
2133 let col_render_off = col.render_offset();
2134 col.tiles_in_render_order().map(move |(tile, tile_off)| {
2135 let pos = view_off + col_off + col_render_off + tile_off + tile.render_offset();
2136 // Round to physical pixels.
2137 let pos = pos.to_physical_precise_round(scale).to_logical(scale);
2138 (tile, pos)
2139 })
2140 })
2141 }
2142
2143 fn tiles_with_render_positions_mut(
2144 &mut self,
2145 round: bool,
2146 ) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> {
2147 let scale = self.scale.fractional_scale();
2148 let view_off = Point::from((-self.view_pos(), 0.));
2149 self.columns_in_render_order_mut()
2150 .flat_map(move |(col, col_x)| {
2151 let col_off = Point::from((col_x, 0.));
2152 let col_render_off = col.render_offset();
2153 col.tiles_in_render_order_mut()
2154 .map(move |(tile, tile_off)| {
2155 let mut pos =
2156 view_off + col_off + col_render_off + tile_off + tile.render_offset();
2157 // Round to physical pixels.
2158 if round {
2159 pos = pos.to_physical_precise_round(scale).to_logical(scale);
2160 }
2161 (tile, pos)
2162 })
2163 })
2164 }
2165
2166 /// Returns the geometry of the active tile relative to and clamped to the view.
2167 ///
2168 /// During animations, assumes the final view position.
2169 pub fn active_tile_visual_rectangle(&self) -> Option<Rectangle<f64, Logical>> {
2170 let col = self.columns.get(self.active_column_idx)?;
2171
2172 let final_view_offset = self
2173 .view_offset_adj
2174 .as_ref()
2175 .map_or(self.view_offset, |adj| adj.target_view_offset());
2176 let view_off = Point::from((-final_view_offset, 0.));
2177
2178 let (tile, tile_off) = col.tiles().nth(col.active_tile_idx).unwrap();
2179
2180 let tile_pos = view_off + tile_off;
2181 let tile_size = tile.tile_size();
2182 let tile_rect = Rectangle::from_loc_and_size(tile_pos, tile_size);
2183
2184 let view = Rectangle::from_loc_and_size((0., 0.), self.view_size);
2185 view.intersection(tile_rect)
2186 }
2187
2188 pub fn window_under(
2189 &self,
2190 pos: Point<f64, Logical>,
2191 ) -> Option<(&W, Option<Point<f64, Logical>>)> {
2192 if self.columns.is_empty() {
2193 return None;
2194 }
2195
2196 self.tiles_with_render_positions()
2197 .find_map(|(tile, tile_pos)| {
2198 let pos_within_tile = pos - tile_pos;
2199
2200 if tile.is_in_input_region(pos_within_tile) {
2201 let pos_within_surface = tile_pos + tile.buf_loc();
2202 return Some((tile.window(), Some(pos_within_surface)));
2203 } else if tile.is_in_activation_region(pos_within_tile) {
2204 return Some((tile.window(), None));
2205 }
2206
2207 None
2208 })
2209 }
2210
2211 pub fn resize_edges_under(&self, pos: Point<f64, Logical>) -> Option<ResizeEdge> {
2212 if self.columns.is_empty() {
2213 return None;
2214 }
2215
2216 self.tiles_with_render_positions()
2217 .find_map(|(tile, tile_pos)| {
2218 let pos_within_tile = pos - tile_pos;
2219
2220 // This logic should be consistent with window_under() in when it returns Some vs.
2221 // None.
2222 if tile.is_in_input_region(pos_within_tile)
2223 || tile.is_in_activation_region(pos_within_tile)
2224 {
2225 let size = tile.tile_size().to_f64();
2226
2227 let mut edges = ResizeEdge::empty();
2228 if pos_within_tile.x < size.w / 3. {
2229 edges |= ResizeEdge::LEFT;
2230 } else if 2. * size.w / 3. < pos_within_tile.x {
2231 edges |= ResizeEdge::RIGHT;
2232 }
2233 if pos_within_tile.y < size.h / 3. {
2234 edges |= ResizeEdge::TOP;
2235 } else if 2. * size.h / 3. < pos_within_tile.y {
2236 edges |= ResizeEdge::BOTTOM;
2237 }
2238 return Some(edges);
2239 }
2240
2241 None
2242 })
2243 }
2244
2245 pub fn toggle_width(&mut self) {
2246 if self.columns.is_empty() {
2247 return;
2248 }
2249
2250 let col = &mut self.columns[self.active_column_idx];
2251 col.toggle_width();
2252
2253 cancel_resize_for_column(&mut self.interactive_resize, col);
2254 }
2255
2256 pub fn toggle_full_width(&mut self) {
2257 if self.columns.is_empty() {
2258 return;
2259 }
2260
2261 let col = &mut self.columns[self.active_column_idx];
2262 col.toggle_full_width();
2263
2264 cancel_resize_for_column(&mut self.interactive_resize, col);
2265 }
2266
2267 pub fn set_column_width(&mut self, change: SizeChange) {
2268 if self.columns.is_empty() {
2269 return;
2270 }
2271
2272 let col = &mut self.columns[self.active_column_idx];
2273 col.set_column_width(change, None, true);
2274
2275 cancel_resize_for_column(&mut self.interactive_resize, col);
2276 }
2277
2278 pub fn set_window_height(&mut self, change: SizeChange) {
2279 if self.columns.is_empty() {
2280 return;
2281 }
2282
2283 let col = &mut self.columns[self.active_column_idx];
2284 col.set_window_height(change, None, true);
2285
2286 cancel_resize_for_column(&mut self.interactive_resize, col);
2287 }
2288
2289 pub fn reset_window_height(&mut self) {
2290 if self.columns.is_empty() {
2291 return;
2292 }
2293
2294 let col = &mut self.columns[self.active_column_idx];
2295 col.reset_window_height(None, true);
2296
2297 cancel_resize_for_column(&mut self.interactive_resize, col);
2298 }
2299
2300 pub fn set_fullscreen(&mut self, window: &W::Id, is_fullscreen: bool) {
2301 let (mut col_idx, tile_idx) = self
2302 .columns
2303 .iter()
2304 .enumerate()
2305 .find_map(|(col_idx, col)| col.position(window).map(|tile_idx| (col_idx, tile_idx)))
2306 .unwrap();
2307
2308 if is_fullscreen == self.columns[col_idx].is_fullscreen {
2309 return;
2310 }
2311
2312 if is_fullscreen
2313 && col_idx == self.active_column_idx
2314 && self.columns[col_idx].tiles.len() == 1
2315 {
2316 self.view_offset_before_fullscreen = Some(self.static_view_offset());
2317 }
2318
2319 let mut col = &mut self.columns[col_idx];
2320
2321 cancel_resize_for_column(&mut self.interactive_resize, col);
2322
2323 if is_fullscreen && col.tiles.len() > 1 {
2324 // This wasn't the only window in its column; extract it into a separate column.
2325 let target_window_was_focused =
2326 self.active_column_idx == col_idx && col.active_tile_idx == tile_idx;
2327 let window = col.tiles.remove(tile_idx).into_window();
2328 col.data.remove(tile_idx);
2329 col.active_tile_idx = min(col.active_tile_idx, col.tiles.len() - 1);
2330
2331 // If one window is left, reset its weight to 1.
2332 if col.data.len() == 1 {
2333 if let WindowHeight::Auto { weight } = &mut col.data[0].height {
2334 *weight = 1.;
2335 }
2336 }
2337
2338 col.update_tile_sizes(false);
2339 self.data[col_idx].update(col);
2340 let width = col.width;
2341 let is_full_width = col.is_full_width;
2342
2343 col_idx += 1;
2344 let column = Column::new(
2345 window,
2346 self.view_size,
2347 self.working_area,
2348 self.scale.fractional_scale(),
2349 self.options.clone(),
2350 width,
2351 is_full_width,
2352 false,
2353 );
2354 self.data.insert(col_idx, ColumnData::new(&column));
2355 self.columns.insert(col_idx, column);
2356
2357 if target_window_was_focused {
2358 self.activate_column(col_idx);
2359 self.view_offset_before_fullscreen = Some(self.static_view_offset());
2360 } else if self.active_column_idx >= col_idx {
2361 self.active_column_idx += 1;
2362 }
2363
2364 col = &mut self.columns[col_idx];
2365 }
2366
2367 col.set_fullscreen(is_fullscreen);
2368
2369 // If we quickly fullscreen and unfullscreen before any window has a chance to receive the
2370 // request, we need to reset the offset.
2371 if col_idx == self.active_column_idx
2372 && !is_fullscreen
2373 && !col
2374 .tiles
2375 .iter()
2376 .any(|tile| tile.is_fullscreen() || tile.window().is_pending_fullscreen())
2377 {
2378 self.view_offset_before_fullscreen = None;
2379 }
2380 }
2381
2382 pub fn toggle_fullscreen(&mut self, window: &W::Id) {
2383 let col = self
2384 .columns
2385 .iter_mut()
2386 .find(|col| col.contains(window))
2387 .unwrap();
2388 let value = !col.is_fullscreen;
2389 self.set_fullscreen(window, value);
2390 }
2391
2392 pub fn render_above_top_layer(&self) -> bool {
2393 // Render above the top layer if we're on a fullscreen window and the view is stationary.
2394 if self.columns.is_empty() {
2395 return false;
2396 }
2397
2398 if self.view_offset_adj.is_some() {
2399 return false;
2400 }
2401
2402 self.columns[self.active_column_idx].is_fullscreen
2403 }
2404
2405 pub fn render_elements<R: NiriRenderer>(
2406 &self,
2407 renderer: &mut R,
2408 target: RenderTarget,
2409 ) -> Vec<WorkspaceRenderElement<R>> {
2410 let output_scale = Scale::from(self.scale.fractional_scale());
2411
2412 let mut rv = vec![];
2413
2414 // Draw the closing windows on top.
2415 let view_rect = Rectangle::from_loc_and_size((self.view_pos(), 0.), self.view_size);
2416 for closing in self.closing_windows.iter().rev() {
2417 let elem = closing.render(renderer.as_gles_renderer(), view_rect, output_scale, target);
2418 rv.push(elem.into());
2419 }
2420
2421 if self.columns.is_empty() {
2422 return rv;
2423 }
2424
2425 let mut first = true;
2426 for (tile, tile_pos) in self.tiles_with_render_positions() {
2427 // For the active tile (which comes first), draw the focus ring.
2428 let focus_ring = first;
2429 first = false;
2430
2431 rv.extend(
2432 tile.render(renderer, tile_pos, output_scale, focus_ring, target)
2433 .map(Into::into),
2434 );
2435 }
2436
2437 rv
2438 }
2439
2440 pub fn view_offset_gesture_begin(&mut self, is_touchpad: bool) {
2441 if self.columns.is_empty() {
2442 return;
2443 }
2444
2445 if self.interactive_resize.is_some() {
2446 return;
2447 }
2448
2449 let gesture = ViewGesture {
2450 current_view_offset: self.view_offset,
2451 tracker: SwipeTracker::new(),
2452 delta_from_tracker: self.view_offset,
2453 static_view_offset: self.static_view_offset(),
2454 is_touchpad,
2455 };
2456 self.view_offset_adj = Some(ViewOffsetAdjustment::Gesture(gesture));
2457 }
2458
2459 pub fn view_offset_gesture_update(
2460 &mut self,
2461 delta_x: f64,
2462 timestamp: Duration,
2463 is_touchpad: bool,
2464 ) -> Option<bool> {
2465 let Some(ViewOffsetAdjustment::Gesture(gesture)) = &mut self.view_offset_adj else {
2466 return None;
2467 };
2468
2469 if gesture.is_touchpad != is_touchpad {
2470 return None;
2471 }
2472
2473 gesture.tracker.push(delta_x, timestamp);
2474
2475 let norm_factor = if gesture.is_touchpad {
2476 self.working_area.size.w / VIEW_GESTURE_WORKING_AREA_MOVEMENT
2477 } else {
2478 1.
2479 };
2480 let pos = gesture.tracker.pos() * norm_factor;
2481 let view_offset = pos + gesture.delta_from_tracker;
2482 gesture.current_view_offset = view_offset;
2483
2484 Some(true)
2485 }
2486
2487 pub fn view_offset_gesture_end(&mut self, _cancelled: bool, is_touchpad: Option<bool>) -> bool {
2488 let Some(ViewOffsetAdjustment::Gesture(gesture)) = &self.view_offset_adj else {
2489 return false;
2490 };
2491
2492 if is_touchpad.map_or(false, |x| gesture.is_touchpad != x) {
2493 return false;
2494 }
2495
2496 // We do not handle cancelling, just like GNOME Shell doesn't. For this gesture, proper
2497 // cancelling would require keeping track of the original active column, and then updating
2498 // it in all the right places (adding columns, removing columns, etc.) -- quite a bit of
2499 // effort and bug potential.
2500
2501 let norm_factor = if gesture.is_touchpad {
2502 self.working_area.size.w / VIEW_GESTURE_WORKING_AREA_MOVEMENT
2503 } else {
2504 1.
2505 };
2506 let velocity = gesture.tracker.velocity() * norm_factor;
2507 let pos = gesture.tracker.pos() * norm_factor;
2508 let current_view_offset = pos + gesture.delta_from_tracker;
2509
2510 if self.columns.is_empty() {
2511 self.view_offset = current_view_offset;
2512 self.view_offset_adj = None;
2513 return true;
2514 }
2515
2516 // Figure out where the gesture would stop after deceleration.
2517 let end_pos = gesture.tracker.projected_end_pos() * norm_factor;
2518 let target_view_offset = end_pos + gesture.delta_from_tracker;
2519
2520 // Compute the snapping points. These are where the view aligns with column boundaries on
2521 // either side.
2522 struct Snap {
2523 // View position relative to x = 0 (the first column).
2524 view_pos: f64,
2525 // Column to activate for this snapping point.
2526 col_idx: usize,
2527 }
2528
2529 let mut snapping_points = Vec::new();
2530
2531 let left_strut = self.working_area.loc.x;
2532 let right_strut = self.view_size.w - self.working_area.size.w - self.working_area.loc.x;
2533
2534 if self.options.center_focused_column == CenterFocusedColumn::Always {
2535 let mut col_x = 0.;
2536 for (col_idx, col) in self.columns.iter().enumerate() {
2537 let col_w = col.width();
2538
2539 let view_pos = if col.is_fullscreen {
2540 col_x
2541 } else if self.working_area.size.w <= col_w {
2542 col_x - left_strut
2543 } else {
2544 col_x - (self.working_area.size.w - col_w) / 2. - left_strut
2545 };
2546 snapping_points.push(Snap { view_pos, col_idx });
2547
2548 col_x += col_w + self.options.gaps;
2549 }
2550 } else {
2551 let view_width = self.view_size.w;
2552 let mut push = |col_idx, left, right| {
2553 snapping_points.push(Snap {
2554 view_pos: left,
2555 col_idx,
2556 });
2557 snapping_points.push(Snap {
2558 view_pos: right - view_width,
2559 col_idx,
2560 });
2561 };
2562
2563 let mut col_x = 0.;
2564 for (col_idx, col) in self.columns.iter().enumerate() {
2565 let col_w = col.width();
2566
2567 // Normal columns align with the working area, but fullscreen columns align with the
2568 // view size.
2569 if col.is_fullscreen {
2570 let left = col_x;
2571 let right = col_x + col_w;
2572 push(col_idx, left, right);
2573 } else {
2574 // Logic from compute_new_view_offset.
2575 let padding =
2576 ((self.working_area.size.w - col_w) / 2.).clamp(0., self.options.gaps);
2577 let left = col_x - padding - left_strut;
2578 let right = col_x + col_w + padding + right_strut;
2579 push(col_idx, left, right);
2580 }
2581
2582 col_x += col_w + self.options.gaps;
2583 }
2584 }
2585
2586 // Find the closest snapping point.
2587 snapping_points.sort_by_key(|snap| NotNan::new(snap.view_pos).unwrap());
2588
2589 let active_col_x = self.column_x(self.active_column_idx);
2590 let target_view_pos = active_col_x + target_view_offset;
2591 let target_snap = snapping_points
2592 .iter()
2593 .min_by_key(|snap| NotNan::new((snap.view_pos - target_view_pos).abs()).unwrap())
2594 .unwrap();
2595
2596 let mut new_col_idx = target_snap.col_idx;
2597
2598 if self.options.center_focused_column != CenterFocusedColumn::Always {
2599 // Focus the furthest window towards the direction of the gesture.
2600 if target_view_offset >= current_view_offset {
2601 for col_idx in (new_col_idx + 1)..self.columns.len() {
2602 let col = &self.columns[col_idx];
2603 let col_x = self.column_x(col_idx);
2604 let col_w = col.width();
2605
2606 if col.is_fullscreen {
2607 if target_snap.view_pos + self.view_size.w < col_x + col_w {
2608 break;
2609 }
2610 } else {
2611 let padding =
2612 ((self.working_area.size.w - col_w) / 2.).clamp(0., self.options.gaps);
2613 if target_snap.view_pos + left_strut + self.working_area.size.w
2614 < col_x + col_w + padding
2615 {
2616 break;
2617 }
2618 }
2619
2620 new_col_idx = col_idx;
2621 }
2622 } else {
2623 for col_idx in (0..new_col_idx).rev() {
2624 let col = &self.columns[col_idx];
2625 let col_x = self.column_x(col_idx);
2626 let col_w = col.width();
2627
2628 if col.is_fullscreen {
2629 if col_x < target_snap.view_pos {
2630 break;
2631 }
2632 } else {
2633 let padding =
2634 ((self.working_area.size.w - col_w) / 2.).clamp(0., self.options.gaps);
2635 if col_x - padding < target_snap.view_pos + left_strut {
2636 break;
2637 }
2638 }
2639
2640 new_col_idx = col_idx;
2641 }
2642 }
2643 }
2644
2645 let new_col_x = self.column_x(new_col_idx);
2646 let delta = active_col_x - new_col_x;
2647 self.view_offset = current_view_offset + delta;
2648
2649 if self.active_column_idx != new_col_idx {
2650 self.view_offset_before_fullscreen = None;
2651 }
2652
2653 self.active_column_idx = new_col_idx;
2654
2655 let target_view_offset = target_snap.view_pos - new_col_x;
2656
2657 self.view_offset_adj = Some(ViewOffsetAdjustment::Animation(Animation::new(
2658 current_view_offset + delta,
2659 target_view_offset,
2660 velocity,
2661 self.options.animations.horizontal_view_movement.0,
2662 )));
2663
2664 // HACK: deal with things like snapping to the right edge of a larger-than-view window.
2665 self.animate_view_offset_to_column(self.view_pos(), new_col_idx, None);
2666
2667 true
2668 }
2669
2670 pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool {
2671 let col = self
2672 .columns
2673 .iter_mut()
2674 .find(|col| col.contains(&window))
2675 .unwrap();
2676
2677 if col.is_fullscreen {
2678 return false;
2679 }
2680
2681 let tile = col
2682 .tiles
2683 .iter_mut()
2684 .find(|tile| tile.window().id() == &window)
2685 .unwrap();
2686
2687 let original_window_size = tile.window_size();
2688
2689 let resize = InteractiveResize {
2690 window,
2691 original_window_size,
2692 data: InteractiveResizeData { edges },
2693 };
2694 self.interactive_resize = Some(resize);
2695
2696 // Stop ongoing animation.
2697 self.view_offset_adj = None;
2698
2699 true
2700 }
2701
2702 pub fn interactive_resize_update(
2703 &mut self,
2704 window: &W::Id,
2705 delta: Point<f64, Logical>,
2706 ) -> bool {
2707 let Some(resize) = &self.interactive_resize else {
2708 return false;
2709 };
2710
2711 if window != &resize.window {
2712 return false;
2713 }
2714
2715 let col = self
2716 .columns
2717 .iter_mut()
2718 .find(|col| col.contains(window))
2719 .unwrap();
2720
2721 let tile_idx = col
2722 .tiles
2723 .iter()
2724 .position(|tile| tile.window().id() == window)
2725 .unwrap();
2726
2727 if resize.data.edges.intersects(ResizeEdge::LEFT_RIGHT) {
2728 let mut dx = delta.x;
2729 if resize.data.edges.contains(ResizeEdge::LEFT) {
2730 dx = -dx;
2731 };
2732
2733 if self.options.center_focused_column == CenterFocusedColumn::Always {
2734 dx *= 2.;
2735 }
2736
2737 let window_width = (resize.original_window_size.w + dx).round() as i32;
2738 col.set_column_width(SizeChange::SetFixed(window_width), Some(tile_idx), false);
2739 }
2740
2741 if resize.data.edges.intersects(ResizeEdge::TOP_BOTTOM) {
2742 // Prevent the simplest case of weird resizing (top edge when this is the topmost
2743 // window).
2744 if !(resize.data.edges.contains(ResizeEdge::TOP) && tile_idx == 0) {
2745 let mut dy = delta.y;
2746 if resize.data.edges.contains(ResizeEdge::TOP) {
2747 dy = -dy;
2748 };
2749
2750 // FIXME: some smarter height distribution would be nice here so that vertical
2751 // resizes work as expected in more cases.
2752
2753 let window_height = (resize.original_window_size.h + dy).round() as i32;
2754 col.set_window_height(SizeChange::SetFixed(window_height), Some(tile_idx), false);
2755 }
2756 }
2757
2758 true
2759 }
2760
2761 pub fn interactive_resize_end(&mut self, window: Option<&W::Id>) {
2762 let Some(resize) = &self.interactive_resize else {
2763 return;
2764 };
2765
2766 if let Some(window) = window {
2767 if window != &resize.window {
2768 return;
2769 }
2770
2771 // Animate the active window into view right away.
2772 if self.columns[self.active_column_idx].contains(window) {
2773 self.animate_view_offset_to_column(self.view_pos(), self.active_column_idx, None);
2774 }
2775 }
2776
2777 self.interactive_resize = None;
2778 }
2779
2780 pub fn refresh(&mut self, is_active: bool) {
2781 for (col_idx, col) in self.columns.iter_mut().enumerate() {
2782 let mut col_resize_data = None;
2783 if let Some(resize) = &self.interactive_resize {
2784 if col.contains(&resize.window) {
2785 col_resize_data = Some(resize.data);
2786 }
2787 }
2788
2789 let intent = if self.options.disable_resize_throttling {
2790 ConfigureIntent::CanSend
2791 } else if self.options.disable_transactions {
2792 // When transactions are disabled, we don't use combined throttling, but rather
2793 // compute throttling individually below.
2794 ConfigureIntent::CanSend
2795 } else {
2796 col.tiles
2797 .iter()
2798 .fold(ConfigureIntent::NotNeeded, |intent, tile| {
2799 match (intent, tile.window().configure_intent()) {
2800 (_, ConfigureIntent::ShouldSend) => ConfigureIntent::ShouldSend,
2801 (ConfigureIntent::NotNeeded, tile_intent) => tile_intent,
2802 (ConfigureIntent::CanSend, ConfigureIntent::Throttled) => {
2803 ConfigureIntent::Throttled
2804 }
2805 (intent, _) => intent,
2806 }
2807 })
2808 };
2809
2810 for (tile_idx, tile) in col.tiles.iter_mut().enumerate() {
2811 let win = tile.window_mut();
2812
2813 let active_in_column = col.active_tile_idx == tile_idx;
2814 win.set_active_in_column(active_in_column);
2815
2816 let active = is_active && self.active_column_idx == col_idx && active_in_column;
2817 win.set_activated(active);
2818
2819 win.set_interactive_resize(col_resize_data);
2820
2821 let border_config = win.rules().border.resolve_against(self.options.border);
2822 let bounds = compute_toplevel_bounds(
2823 border_config,
2824 self.working_area.size,
2825 self.options.gaps,
2826 );
2827 win.set_bounds(bounds);
2828
2829 // If transactions are disabled, also disable combined throttling, for more
2830 // intuitive behavior.
2831 let intent = if self.options.disable_transactions {
2832 win.configure_intent()
2833 } else {
2834 intent
2835 };
2836
2837 if matches!(
2838 intent,
2839 ConfigureIntent::CanSend | ConfigureIntent::ShouldSend
2840 ) {
2841 win.send_pending_configure();
2842 }
2843
2844 win.refresh();
2845 }
2846 }
2847 }
2848}
2849
2850impl<W: LayoutElement> Column<W> {
2851 #[allow(clippy::too_many_arguments)]
2852 fn new(
2853 window: W,
2854 view_size: Size<f64, Logical>,
2855 working_area: Rectangle<f64, Logical>,
2856 scale: f64,
2857 options: Rc<Options>,
2858 width: ColumnWidth,
2859 is_full_width: bool,
2860 animate_resize: bool,
2861 ) -> Self {
2862 let tile = Tile::new(window, scale, options.clone());
2863 Self::new_with_tile(
2864 tile,
2865 view_size,
2866 working_area,
2867 scale,
2868 options,
2869 width,
2870 is_full_width,
2871 animate_resize,
2872 )
2873 }
2874
2875 #[allow(clippy::too_many_arguments)]
2876 fn new_with_tile(
2877 tile: Tile<W>,
2878 view_size: Size<f64, Logical>,
2879 working_area: Rectangle<f64, Logical>,
2880 scale: f64,
2881 options: Rc<Options>,
2882 width: ColumnWidth,
2883 is_full_width: bool,
2884 animate_resize: bool,
2885 ) -> Self {
2886 let mut rv = Self {
2887 tiles: vec![],
2888 data: vec![],
2889 active_tile_idx: 0,
2890 width,
2891 is_full_width,
2892 is_fullscreen: false,
2893 move_animation: None,
2894 view_size,
2895 working_area,
2896 scale,
2897 options,
2898 };
2899
2900 let is_pending_fullscreen = tile.window().is_pending_fullscreen();
2901
2902 rv.add_tile(tile, animate_resize);
2903
2904 if is_pending_fullscreen {
2905 rv.set_fullscreen(true);
2906 }
2907
2908 rv
2909 }
2910
2911 fn set_view_size(&mut self, size: Size<f64, Logical>, working_area: Rectangle<f64, Logical>) {
2912 if self.view_size == size && self.working_area == working_area {
2913 return;
2914 }
2915
2916 self.view_size = size;
2917 self.working_area = working_area;
2918
2919 self.update_tile_sizes(false);
2920 }
2921
2922 fn update_config(&mut self, scale: f64, options: Rc<Options>) {
2923 let mut update_sizes = false;
2924
2925 // If preset widths changed, make our width non-preset.
2926 if self.options.preset_widths != options.preset_widths {
2927 if let ColumnWidth::Preset(idx) = self.width {
2928 self.width = self.options.preset_widths[idx];
2929 }
2930 }
2931
2932 if self.options.gaps != options.gaps {
2933 update_sizes = true;
2934 }
2935
2936 if self.options.border.off != options.border.off
2937 || self.options.border.width != options.border.width
2938 {
2939 update_sizes = true;
2940 }
2941
2942 for (tile, data) in zip(&mut self.tiles, &mut self.data) {
2943 tile.update_config(scale, options.clone());
2944 data.update(tile);
2945 }
2946
2947 self.scale = scale;
2948 self.options = options;
2949
2950 if update_sizes {
2951 self.update_tile_sizes(false);
2952 }
2953 }
2954
2955 fn set_width(&mut self, width: ColumnWidth, animate: bool) {
2956 self.width = width;
2957 self.is_full_width = false;
2958 self.update_tile_sizes(animate);
2959 }
2960
2961 pub fn advance_animations(&mut self, current_time: Duration) {
2962 match &mut self.move_animation {
2963 Some(anim) => {
2964 anim.set_current_time(current_time);
2965 if anim.is_done() {
2966 self.move_animation = None;
2967 }
2968 }
2969 None => (),
2970 }
2971
2972 for tile in &mut self.tiles {
2973 tile.advance_animations(current_time);
2974 }
2975 }
2976
2977 pub fn are_animations_ongoing(&self) -> bool {
2978 self.move_animation.is_some() || self.tiles.iter().any(Tile::are_animations_ongoing)
2979 }
2980
2981 pub fn update_render_elements(&mut self, is_active: bool, view_rect: Rectangle<f64, Logical>) {
2982 let active_idx = self.active_tile_idx;
2983 for (tile_idx, (tile, tile_off)) in self.tiles_mut().enumerate() {
2984 let is_active = is_active && tile_idx == active_idx;
2985
2986 let mut tile_view_rect = view_rect;
2987 tile_view_rect.loc -= tile_off + tile.render_offset();
2988 tile.update(is_active, tile_view_rect);
2989 }
2990 }
2991
2992 pub fn render_offset(&self) -> Point<f64, Logical> {
2993 let mut offset = Point::from((0., 0.));
2994
2995 if let Some(anim) = &self.move_animation {
2996 offset.x += anim.value();
2997 }
2998
2999 offset
3000 }
3001
3002 pub fn animate_move_from(&mut self, from_x_offset: f64) {
3003 self.animate_move_from_with_config(
3004 from_x_offset,
3005 self.options.animations.window_movement.0,
3006 );
3007 }
3008
3009 pub fn animate_move_from_with_config(
3010 &mut self,
3011 from_x_offset: f64,
3012 config: niri_config::Animation,
3013 ) {
3014 let current_offset = self.move_animation.as_ref().map_or(0., Animation::value);
3015
3016 self.move_animation = Some(Animation::new(
3017 from_x_offset + current_offset,
3018 0.,
3019 0.,
3020 config,
3021 ));
3022 }
3023
3024 pub fn contains(&self, window: &W::Id) -> bool {
3025 self.tiles
3026 .iter()
3027 .map(Tile::window)
3028 .any(|win| win.id() == window)
3029 }
3030
3031 pub fn position(&self, window: &W::Id) -> Option<usize> {
3032 self.tiles
3033 .iter()
3034 .map(Tile::window)
3035 .position(|win| win.id() == window)
3036 }
3037
3038 fn activate_window(&mut self, window: &W::Id) {
3039 let idx = self.position(window).unwrap();
3040 self.active_tile_idx = idx;
3041 }
3042
3043 fn add_tile(&mut self, tile: Tile<W>, animate: bool) {
3044 self.is_fullscreen = false;
3045 self.data.push(TileData::new(&tile, WindowHeight::auto_1()));
3046 self.tiles.push(tile);
3047 self.update_tile_sizes(animate);
3048 }
3049
3050 fn update_window(&mut self, window: &W::Id) {
3051 let (tile_idx, tile) = self
3052 .tiles
3053 .iter_mut()
3054 .enumerate()
3055 .find(|(_, tile)| tile.window().id() == window)
3056 .unwrap();
3057
3058 let height = f64::from(tile.window().size().h);
3059 let offset = tile
3060 .window()
3061 .animation_snapshot()
3062 .map_or(0., |from| from.size.h - height);
3063
3064 tile.update_window();
3065 self.data[tile_idx].update(tile);
3066
3067 // Move windows below in tandem with resizing.
3068 if tile.resize_animation().is_some() && offset != 0. {
3069 for tile in &mut self.tiles[tile_idx + 1..] {
3070 tile.animate_move_y_from_with_config(
3071 offset,
3072 self.options.animations.window_resize.anim,
3073 );
3074 }
3075 }
3076 }
3077
3078 fn update_tile_sizes(&mut self, animate: bool) {
3079 self.update_tile_sizes_with_transaction(animate, Transaction::new());
3080 }
3081
3082 fn update_tile_sizes_with_transaction(&mut self, animate: bool, transaction: Transaction) {
3083 if self.is_fullscreen {
3084 self.tiles[0].request_fullscreen(self.view_size);
3085 return;
3086 }
3087
3088 let min_size: Vec<_> = self
3089 .tiles
3090 .iter()
3091 .map(Tile::min_size)
3092 .map(|mut size| {
3093 size.w = size.w.max(1.);
3094 size.h = size.h.max(1.);
3095 size
3096 })
3097 .collect();
3098 let max_size: Vec<_> = self.tiles.iter().map(Tile::max_size).collect();
3099
3100 // Compute the column width.
3101 let min_width = min_size
3102 .iter()
3103 .map(|size| NotNan::new(size.w).unwrap())
3104 .max()
3105 .map(NotNan::into_inner)
3106 .unwrap();
3107 let max_width = max_size
3108 .iter()
3109 .filter_map(|size| {
3110 let w = size.w;
3111 if w == 0. {
3112 None
3113 } else {
3114 Some(NotNan::new(w).unwrap())
3115 }
3116 })
3117 .min()
3118 .map(NotNan::into_inner)
3119 .unwrap_or(f64::from(i32::MAX));
3120 let max_width = f64::max(max_width, min_width);
3121
3122 let width = if self.is_full_width {
3123 ColumnWidth::Proportion(1.)
3124 } else {
3125 self.width
3126 };
3127
3128 let width = width.resolve(&self.options, self.working_area.size.w);
3129 let width = f64::max(f64::min(width, max_width), min_width);
3130
3131 // Compute the tile heights. Start by converting window heights to tile heights.
3132 let mut heights = zip(&self.tiles, &self.data)
3133 .map(|(tile, data)| match data.height {
3134 auto @ WindowHeight::Auto { .. } => auto,
3135 WindowHeight::Fixed(height) => {
3136 WindowHeight::Fixed(tile.tile_height_for_window_height(height.round().max(1.)))
3137 }
3138 })
3139 .collect::<Vec<_>>();
3140 let gaps_left = self.options.gaps * (self.tiles.len() + 1) as f64;
3141 let mut height_left = self.working_area.size.h - gaps_left;
3142 let mut auto_tiles_left = self.tiles.len();
3143
3144 // Subtract all fixed-height tiles.
3145 for (h, (min_size, max_size)) in zip(&mut heights, zip(&min_size, &max_size)) {
3146 // Check if the tile has an exact height constraint.
3147 if min_size.h == max_size.h {
3148 *h = WindowHeight::Fixed(min_size.h);
3149 }
3150
3151 if let WindowHeight::Fixed(h) = h {
3152 if max_size.h > 0. {
3153 *h = f64::min(*h, max_size.h);
3154 }
3155 *h = f64::max(*h, min_size.h);
3156
3157 height_left -= *h;
3158 auto_tiles_left -= 1;
3159 }
3160 }
3161
3162 let mut total_weight: f64 = heights
3163 .iter()
3164 .filter_map(|h| {
3165 if let WindowHeight::Auto { weight } = *h {
3166 Some(weight)
3167 } else {
3168 None
3169 }
3170 })
3171 .sum();
3172
3173 // Iteratively try to distribute the remaining height, checking against tile min heights.
3174 // Pick an auto height according to the current sizes, then check if it satisfies all
3175 // remaining min heights. If not, allocate fixed height to those tiles and repeat the
3176 // loop. On each iteration the auto height will get smaller.
3177 //
3178 // NOTE: we do not respect max height here. Doing so would complicate things: if the current
3179 // auto height is above some tile's max height, then the auto height can become larger.
3180 // Combining this with the min height loop is where the complexity appears.
3181 //
3182 // However, most max height uses are for fixed-size dialogs, where min height == max_height.
3183 // This case is separately handled above.
3184 'outer: while auto_tiles_left > 0 {
3185 // Wayland requires us to round the requested size for a window to integer logical
3186 // pixels, therefore we compute the remaining auto height dynamically.
3187 let mut height_left_2 = height_left;
3188 let mut total_weight_2 = total_weight;
3189 for ((h, tile), min_size) in zip(zip(&mut heights, &self.tiles), &min_size) {
3190 let weight = match *h {
3191 WindowHeight::Auto { weight } => weight,
3192 WindowHeight::Fixed(_) => continue,
3193 };
3194 let factor = weight / total_weight_2;
3195
3196 // Compute the current auto height.
3197 let mut auto = height_left_2 * factor;
3198
3199 // Check if the auto height satisfies the min height.
3200 if min_size.h > auto {
3201 auto = min_size.h;
3202 *h = WindowHeight::Fixed(auto);
3203 height_left -= auto;
3204 total_weight -= weight;
3205 auto_tiles_left -= 1;
3206
3207 // If a min height was unsatisfied, then we allocate the tile more than the
3208 // auto height, which means that the remaining auto tiles now have less height
3209 // to work with, and the loop must run again.
3210 //
3211 // If we keep going in this loop and break out later, we may allocate less
3212 // height to the subsequent tiles than would be available next iteration and
3213 // potentially trip their min height check earlier than necessary, leading to
3214 // visible snapping.
3215 continue 'outer;
3216 }
3217
3218 auto = tile.tile_height_for_window_height(
3219 tile.window_height_for_tile_height(auto).round().max(1.),
3220 );
3221
3222 height_left_2 -= auto;
3223 total_weight_2 -= weight;
3224 }
3225
3226 // All min heights were satisfied, fill them in.
3227 for (h, tile) in zip(&mut heights, &self.tiles) {
3228 let weight = match *h {
3229 WindowHeight::Auto { weight } => weight,
3230 WindowHeight::Fixed(_) => continue,
3231 };
3232 let factor = weight / total_weight;
3233
3234 // Compute the current auto height.
3235 let auto = height_left * factor;
3236 let auto = tile.tile_height_for_window_height(
3237 tile.window_height_for_tile_height(auto).round().max(1.),
3238 );
3239
3240 *h = WindowHeight::Fixed(auto);
3241 height_left -= auto;
3242 total_weight -= weight;
3243 auto_tiles_left -= 1;
3244 }
3245
3246 assert_eq!(auto_tiles_left, 0);
3247 }
3248
3249 for (tile, h) in zip(&mut self.tiles, heights) {
3250 let WindowHeight::Fixed(height) = h else {
3251 unreachable!()
3252 };
3253
3254 let size = Size::from((width, height));
3255 tile.request_tile_size(size, animate, Some(transaction.clone()));
3256 }
3257 }
3258
3259 fn width(&self) -> f64 {
3260 self.data
3261 .iter()
3262 .map(|data| NotNan::new(data.size.w).unwrap())
3263 .max()
3264 .map(NotNan::into_inner)
3265 .unwrap()
3266 }
3267
3268 fn focus_up(&mut self) {
3269 self.active_tile_idx = self.active_tile_idx.saturating_sub(1);
3270 }
3271
3272 fn focus_down(&mut self) {
3273 self.active_tile_idx = min(self.active_tile_idx + 1, self.tiles.len() - 1);
3274 }
3275
3276 fn focus_last(&mut self) {
3277 self.active_tile_idx = self.tiles.len() - 1;
3278 }
3279
3280 fn move_up(&mut self) {
3281 let new_idx = self.active_tile_idx.saturating_sub(1);
3282 if self.active_tile_idx == new_idx {
3283 return;
3284 }
3285
3286 let mut ys = self.tile_offsets().skip(self.active_tile_idx);
3287 let active_y = ys.next().unwrap().y;
3288 let next_y = ys.next().unwrap().y;
3289 drop(ys);
3290
3291 self.tiles.swap(self.active_tile_idx, new_idx);
3292 self.data.swap(self.active_tile_idx, new_idx);
3293 self.active_tile_idx = new_idx;
3294
3295 // Animate the movement.
3296 let new_active_y = self.tile_offset(new_idx).y;
3297 self.tiles[new_idx].animate_move_y_from(active_y - new_active_y);
3298 self.tiles[new_idx + 1].animate_move_y_from(active_y - next_y);
3299 }
3300
3301 fn move_down(&mut self) {
3302 let new_idx = min(self.active_tile_idx + 1, self.tiles.len() - 1);
3303 if self.active_tile_idx == new_idx {
3304 return;
3305 }
3306
3307 let mut ys = self.tile_offsets().skip(self.active_tile_idx);
3308 let active_y = ys.next().unwrap().y;
3309 let next_y = ys.next().unwrap().y;
3310 drop(ys);
3311
3312 self.tiles.swap(self.active_tile_idx, new_idx);
3313 self.data.swap(self.active_tile_idx, new_idx);
3314 self.active_tile_idx = new_idx;
3315
3316 // Animate the movement.
3317 let new_active_y = self.tile_offset(new_idx).y;
3318 self.tiles[new_idx].animate_move_y_from(active_y - new_active_y);
3319 self.tiles[new_idx - 1].animate_move_y_from(next_y - active_y);
3320 }
3321
3322 #[cfg(test)]
3323 fn verify_invariants(&self) {
3324 use approx::assert_abs_diff_eq;
3325
3326 assert!(!self.tiles.is_empty(), "columns can't be empty");
3327 assert!(self.active_tile_idx < self.tiles.len());
3328 assert_eq!(self.tiles.len(), self.data.len());
3329
3330 if self.is_fullscreen {
3331 assert_eq!(self.tiles.len(), 1);
3332 }
3333
3334 if self.tiles.len() == 1 {
3335 if let WindowHeight::Auto { weight } = self.data[0].height {
3336 assert_eq!(
3337 weight, 1.,
3338 "auto height weight must reset to 1 for a single window"
3339 );
3340 }
3341 }
3342
3343 let mut found_fixed = false;
3344 for (tile, data) in zip(&self.tiles, &self.data) {
3345 assert!(Rc::ptr_eq(&self.options, &tile.options));
3346 assert_eq!(self.scale, tile.scale());
3347 assert_eq!(self.is_fullscreen, tile.window().is_pending_fullscreen());
3348
3349 let mut data2 = *data;
3350 data2.update(tile);
3351 assert_eq!(data, &data2, "tile data must be up to date");
3352
3353 let scale = tile.scale();
3354 let size = tile.tile_size();
3355 let rounded = size.to_physical_precise_round(scale).to_logical(scale);
3356 assert_abs_diff_eq!(size.w, rounded.w, epsilon = 1e-5);
3357 assert_abs_diff_eq!(size.h, rounded.h, epsilon = 1e-5);
3358
3359 if matches!(data.height, WindowHeight::Fixed(_)) {
3360 assert!(
3361 !found_fixed,
3362 "there can only be one fixed-height window in a column"
3363 );
3364 found_fixed = true;
3365 }
3366 }
3367 }
3368
3369 fn toggle_width(&mut self) {
3370 let width = if self.is_full_width {
3371 ColumnWidth::Proportion(1.)
3372 } else {
3373 self.width
3374 };
3375
3376 let idx = match width {
3377 ColumnWidth::Preset(idx) => (idx + 1) % self.options.preset_widths.len(),
3378 _ => {
3379 let current = self.width();
3380 self.options
3381 .preset_widths
3382 .iter()
3383 .position(|prop| {
3384 let resolved = prop.resolve(&self.options, self.working_area.size.w);
3385 // Some allowance for fractional scaling purposes.
3386 current + 1. < resolved
3387 })
3388 .unwrap_or(0)
3389 }
3390 };
3391 let width = ColumnWidth::Preset(idx);
3392 self.set_width(width, true);
3393 }
3394
3395 fn toggle_full_width(&mut self) {
3396 self.is_full_width = !self.is_full_width;
3397 self.update_tile_sizes(true);
3398 }
3399
3400 fn set_column_width(&mut self, change: SizeChange, tile_idx: Option<usize>, animate: bool) {
3401 let width = if self.is_full_width {
3402 ColumnWidth::Proportion(1.)
3403 } else {
3404 self.width
3405 };
3406
3407 let current_px = width.resolve(&self.options, self.working_area.size.w);
3408
3409 let current = match width {
3410 ColumnWidth::Preset(idx) => self.options.preset_widths[idx],
3411 current => current,
3412 };
3413
3414 // FIXME: fix overflows then remove limits.
3415 const MAX_PX: f64 = 100000.;
3416 const MAX_F: f64 = 10000.;
3417
3418 let width = match (current, change) {
3419 (_, SizeChange::SetFixed(fixed)) => {
3420 // As a special case, setting a fixed column width will compute it in such a way
3421 // that the specified (usually active) window gets that width. This is the
3422 // intention behind the ability to set a fixed size.
3423 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
3424 let tile = &self.tiles[tile_idx];
3425 ColumnWidth::Fixed(
3426 tile.tile_width_for_window_width(f64::from(fixed))
3427 .clamp(1., MAX_PX),
3428 )
3429 }
3430 (_, SizeChange::SetProportion(proportion)) => {
3431 ColumnWidth::Proportion((proportion / 100.).clamp(0., MAX_F))
3432 }
3433 (_, SizeChange::AdjustFixed(delta)) => {
3434 let width = (current_px + f64::from(delta)).clamp(1., MAX_PX);
3435 ColumnWidth::Fixed(width)
3436 }
3437 (ColumnWidth::Proportion(current), SizeChange::AdjustProportion(delta)) => {
3438 let proportion = (current + delta / 100.).clamp(0., MAX_F);
3439 ColumnWidth::Proportion(proportion)
3440 }
3441 (ColumnWidth::Fixed(_), SizeChange::AdjustProportion(delta)) => {
3442 let full = self.working_area.size.w - self.options.gaps;
3443 let current = if full == 0. {
3444 1.
3445 } else {
3446 (current_px + self.options.gaps) / full
3447 };
3448 let proportion = (current + delta / 100.).clamp(0., MAX_F);
3449 ColumnWidth::Proportion(proportion)
3450 }
3451 (ColumnWidth::Preset(_), _) => unreachable!(),
3452 };
3453
3454 self.set_width(width, animate);
3455 }
3456
3457 fn set_window_height(&mut self, change: SizeChange, tile_idx: Option<usize>, animate: bool) {
3458 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
3459
3460 // Start by converting all heights to automatic, since only one window in the column can be
3461 // fixed-height. If the current tile is already fixed, however, we can skip that step.
3462 // Which is not only for optimization, but also preserves automatic weights in case one
3463 // window is resized in such a way that other windows hit their min size, and then back.
3464 if !matches!(self.data[tile_idx].height, WindowHeight::Fixed(_)) {
3465 self.convert_heights_to_auto();
3466 }
3467
3468 let current = self.data[tile_idx].height;
3469 let tile = &self.tiles[tile_idx];
3470 let current_window_px = match current {
3471 WindowHeight::Auto { .. } => tile.window_size().h,
3472 WindowHeight::Fixed(height) => height,
3473 };
3474 let current_tile_px = tile.tile_height_for_window_height(current_window_px);
3475
3476 let full = self.working_area.size.h - self.options.gaps;
3477 let current_prop = if full == 0. {
3478 1.
3479 } else {
3480 (current_tile_px + self.options.gaps) / (full)
3481 };
3482
3483 // FIXME: fix overflows then remove limits.
3484 const MAX_PX: f64 = 100000.;
3485
3486 let mut window_height = match change {
3487 SizeChange::SetFixed(fixed) => f64::from(fixed),
3488 SizeChange::SetProportion(proportion) => {
3489 let tile_height =
3490 (self.working_area.size.h - self.options.gaps) * proportion - self.options.gaps;
3491 tile.window_height_for_tile_height(tile_height)
3492 }
3493 SizeChange::AdjustFixed(delta) => current_window_px + f64::from(delta),
3494 SizeChange::AdjustProportion(delta) => {
3495 let proportion = current_prop + delta / 100.;
3496 let tile_height =
3497 (self.working_area.size.h - self.options.gaps) * proportion - self.options.gaps;
3498 tile.window_height_for_tile_height(tile_height)
3499 }
3500 };
3501
3502 // If there are multiple windows in a column, clamp the height according to other windows'
3503 // min sizes.
3504 let min_height_taken = self
3505 .tiles
3506 .iter()
3507 .enumerate()
3508 .filter(|(idx, _)| *idx != tile_idx)
3509 .map(|(_, tile)| f64::max(1., tile.min_size().h) + self.options.gaps)
3510 .sum::<f64>();
3511 if min_height_taken > 0. {
3512 let height_left =
3513 self.working_area.size.h - self.options.gaps - min_height_taken - self.options.gaps;
3514 let height_left = f64::max(1., tile.window_height_for_tile_height(height_left));
3515 window_height = f64::min(height_left, window_height);
3516 }
3517
3518 // Clamp it against the window height constraints.
3519 let win = &self.tiles[tile_idx].window();
3520 let min_h = win.min_size().h;
3521 let max_h = win.max_size().h;
3522
3523 if max_h > 0 {
3524 window_height = f64::min(window_height, f64::from(max_h));
3525 }
3526 if min_h > 0 {
3527 window_height = f64::max(window_height, f64::from(min_h));
3528 }
3529
3530 self.data[tile_idx].height = WindowHeight::Fixed(window_height.clamp(1., MAX_PX));
3531 self.update_tile_sizes(animate);
3532 }
3533
3534 fn reset_window_height(&mut self, tile_idx: Option<usize>, animate: bool) {
3535 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
3536 self.data[tile_idx].height = WindowHeight::auto_1();
3537 self.update_tile_sizes(animate);
3538 }
3539
3540 /// Converts all heights in the column to automatic, preserving the apparent heights.
3541 ///
3542 /// All weights are recomputed to preserve the current tile heights while "centering" the
3543 /// weights at the median window height (it gets weight = 1).
3544 ///
3545 /// One case where apparent heights will not be preserved is when the column is taller than the
3546 /// working area.
3547 fn convert_heights_to_auto(&mut self) {
3548 let heights: Vec<_> = self.tiles.iter().map(|tile| tile.tile_size().h).collect();
3549
3550 // Weights are invariant to multiplication: a column with weights 2, 2, 1 is equivalent to
3551 // a column with weights 4, 4, 2. So we find the median window height and use that as 1.
3552 let mut sorted = heights.clone();
3553 sorted.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
3554 let median = sorted[sorted.len() / 2];
3555
3556 for (data, height) in zip(&mut self.data, heights) {
3557 let weight = height / median;
3558 data.height = WindowHeight::Auto { weight };
3559 }
3560 }
3561
3562 fn set_fullscreen(&mut self, is_fullscreen: bool) {
3563 if self.is_fullscreen == is_fullscreen {
3564 return;
3565 }
3566
3567 assert_eq!(self.tiles.len(), 1);
3568 self.is_fullscreen = is_fullscreen;
3569 self.update_tile_sizes(false);
3570 }
3571
3572 /// Returns the static window location, not taking the render offset into account.
3573 pub fn window_loc(&self, tile_idx: usize) -> Point<f64, Logical> {
3574 let (tile, pos) = self.tiles().nth(tile_idx).unwrap();
3575 pos + tile.window_loc()
3576 }
3577
3578 // HACK: pass a self.data iterator in manually as a workaround for the lack of method partial
3579 // borrowing. Note that this method's return value does not borrow the entire &Self!
3580 fn tile_offsets_iter(
3581 &self,
3582 data: impl Iterator<Item = TileData>,
3583 ) -> impl Iterator<Item = Point<f64, Logical>> {
3584 let center = self.options.center_focused_column == CenterFocusedColumn::Always;
3585 let gaps = self.options.gaps;
3586 let col_width = self.width();
3587 let mut y = 0.;
3588
3589 if !self.is_fullscreen {
3590 y = self.working_area.loc.y + self.options.gaps;
3591 }
3592
3593 // Chain with a dummy value to be able to get one past all tiles' Y.
3594 let dummy = TileData {
3595 height: WindowHeight::auto_1(),
3596 size: Size::default(),
3597 interactively_resizing_by_left_edge: false,
3598 };
3599 let data = data.chain(iter::once(dummy));
3600
3601 data.map(move |data| {
3602 let mut pos = Point::from((0., y));
3603
3604 if center {
3605 pos.x = (col_width - data.size.w) / 2.;
3606 } else if data.interactively_resizing_by_left_edge {
3607 pos.x = col_width - data.size.w;
3608 }
3609
3610 y += data.size.h + gaps;
3611 pos
3612 })
3613 }
3614
3615 fn tile_offsets(&self) -> impl Iterator<Item = Point<f64, Logical>> + '_ {
3616 self.tile_offsets_iter(self.data.iter().copied())
3617 }
3618
3619 fn tile_offset(&self, tile_idx: usize) -> Point<f64, Logical> {
3620 self.tile_offsets().nth(tile_idx).unwrap()
3621 }
3622
3623 fn tile_offsets_in_render_order(
3624 &self,
3625 data: impl Iterator<Item = TileData>,
3626 ) -> impl Iterator<Item = Point<f64, Logical>> {
3627 let active_idx = self.active_tile_idx;
3628 let active_pos = self.tile_offset(active_idx);
3629 let offsets = self
3630 .tile_offsets_iter(data)
3631 .enumerate()
3632 .filter_map(move |(idx, pos)| (idx != active_idx).then_some(pos));
3633 iter::once(active_pos).chain(offsets)
3634 }
3635
3636 fn tiles(&self) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>)> + '_ {
3637 let offsets = self.tile_offsets_iter(self.data.iter().copied());
3638 zip(&self.tiles, offsets)
3639 }
3640
3641 fn tiles_mut(&mut self) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> + '_ {
3642 let offsets = self.tile_offsets_iter(self.data.iter().copied());
3643 zip(&mut self.tiles, offsets)
3644 }
3645
3646 fn tiles_in_render_order(&self) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>)> + '_ {
3647 let offsets = self.tile_offsets_in_render_order(self.data.iter().copied());
3648
3649 let (first, rest) = self.tiles.split_at(self.active_tile_idx);
3650 let (active, rest) = rest.split_at(1);
3651
3652 let tiles = active.iter().chain(first).chain(rest);
3653 zip(tiles, offsets)
3654 }
3655
3656 fn tiles_in_render_order_mut(
3657 &mut self,
3658 ) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> + '_ {
3659 let offsets = self.tile_offsets_in_render_order(self.data.iter().copied());
3660
3661 let (first, rest) = self.tiles.split_at_mut(self.active_tile_idx);
3662 let (active, rest) = rest.split_at_mut(1);
3663
3664 let tiles = active.iter_mut().chain(first).chain(rest);
3665 zip(tiles, offsets)
3666 }
3667}
3668
3669fn compute_new_view_offset(
3670 cur_x: f64,
3671 view_width: f64,
3672 new_col_x: f64,
3673 new_col_width: f64,
3674 gaps: f64,
3675) -> f64 {
3676 // If the column is wider than the view, always left-align it.
3677 if view_width <= new_col_width {
3678 return 0.;
3679 }
3680
3681 // Compute the padding in case it needs to be smaller due to large tile width.
3682 let padding = ((view_width - new_col_width) / 2.).clamp(0., gaps);
3683
3684 // Compute the desired new X with padding.
3685 let new_x = new_col_x - padding;
3686 let new_right_x = new_col_x + new_col_width + padding;
3687
3688 // If the column is already fully visible, leave the view as is.
3689 if cur_x <= new_x && new_right_x <= cur_x + view_width {
3690 return -(new_col_x - cur_x);
3691 }
3692
3693 // Otherwise, prefer the alignment that results in less motion from the current position.
3694 let dist_to_left = (cur_x - new_x).abs();
3695 let dist_to_right = ((cur_x + view_width) - new_right_x).abs();
3696 if dist_to_left <= dist_to_right {
3697 -padding
3698 } else {
3699 -(view_width - padding - new_col_width)
3700 }
3701}
3702
3703pub fn compute_working_area(output: &Output, struts: Struts) -> Rectangle<f64, Logical> {
3704 // Start with the layer-shell non-exclusive zone.
3705 let mut working_area = layer_map_for_output(output).non_exclusive_zone().to_f64();
3706
3707 // Add struts.
3708 working_area.size.w = f64::max(0., working_area.size.w - struts.left.0 - struts.right.0);
3709 working_area.loc.x += struts.left.0;
3710
3711 working_area.size.h = f64::max(0., working_area.size.h - struts.top.0 - struts.bottom.0);
3712 working_area.loc.y += struts.top.0;
3713
3714 // Round location to start at a physical pixel.
3715 let scale = output.current_scale().fractional_scale();
3716 let loc = working_area
3717 .loc
3718 .to_physical_precise_ceil(scale)
3719 .to_logical(scale);
3720
3721 let mut size_diff = (loc - working_area.loc).to_size();
3722 size_diff.w = f64::min(working_area.size.w, size_diff.w);
3723 size_diff.h = f64::min(working_area.size.h, size_diff.h);
3724
3725 working_area.size -= size_diff;
3726 working_area.loc = loc;
3727
3728 working_area
3729}
3730
3731fn compute_toplevel_bounds(
3732 border_config: niri_config::Border,
3733 working_area_size: Size<f64, Logical>,
3734 gaps: f64,
3735) -> Size<i32, Logical> {
3736 let mut border = 0.;
3737 if !border_config.off {
3738 border = border_config.width.0 * 2.;
3739 }
3740
3741 Size::from((
3742 f64::max(working_area_size.w - gaps * 2. - border, 1.),
3743 f64::max(working_area_size.h - gaps * 2. - border, 1.),
3744 ))
3745 .to_i32_floor()
3746}
3747
3748fn cancel_resize_for_column<W: LayoutElement>(
3749 interactive_resize: &mut Option<InteractiveResize<W>>,
3750 column: &mut Column<W>,
3751) {
3752 if let Some(resize) = interactive_resize {
3753 if column.contains(&resize.window) {
3754 *interactive_resize = None;
3755 }
3756 }
3757
3758 for tile in &mut column.tiles {
3759 tile.window_mut().cancel_interactive_resize();
3760 }
3761}