forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBb0eb8adIvan Molodetskikh3 months ago2857 commits

scrolling.rs5734 lines, 203.5 KiBHistoryRawDownload
1use std::cmp::{max, min};
2use std::iter::{self, zip};
3use std::rc::Rc;
4use std::time::Duration;
5
6use niri_config::utils::MergeWith as _;
7use niri_config::{CenterFocusedColumn, PresetSize, Struts};
8use niri_ipc::{ColumnDisplay, SizeChange, WindowLayout};
9use ordered_float::NotNan;
10use smithay::backend::renderer::gles::GlesRenderer;
11use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size};
12
13use super::closing_window::{ClosingWindow, ClosingWindowRenderElement};
14use super::monitor::InsertPosition;
15use super::tab_indicator::{TabIndicator, TabIndicatorRenderElement, TabInfo};
16use super::tile::{Tile, TileRenderElement, TileRenderSnapshot};
17use super::workspace::{InteractiveResize, ResolvedSize};
18use super::{ConfigureIntent, HitType, InteractiveResizeData, LayoutElement, Options, RemovedTile};
19use crate::animation::{Animation, Clock};
20use crate::input::swipe_tracker::SwipeTracker;
21use crate::layout::{RenderLayer, SizingMode};
22use crate::niri_render_elements;
23use crate::render_helpers::renderer::NiriRenderer;
24use crate::render_helpers::xray::XrayPos;
25use crate::render_helpers::RenderCtx;
26use crate::utils::id::IdCounter;
27use crate::utils::transaction::{Transaction, TransactionBlocker};
28use crate::utils::ResizeEdge;
29use crate::window::ResolvedWindowRules;
30
31/// Amount of touchpad movement to scroll the view for the width of one working area.
32const VIEW_GESTURE_WORKING_AREA_MOVEMENT: f64 = 1200.;
33
34/// A scrollable-tiling space for windows.
35#[derive(Debug)]
36pub struct ScrollingSpace<W: LayoutElement> {
37 /// Columns of windows on this space.
38 columns: Vec<Column<W>>,
39
40 /// Extra per-column data.
41 data: Vec<ColumnData>,
42
43 /// Index of the currently active column, if any.
44 active_column_idx: usize,
45
46 /// Ongoing interactive resize.
47 interactive_resize: Option<InteractiveResize<W>>,
48
49 /// Offset of the view computed from the active column.
50 ///
51 /// Any gaps, including left padding from work area left exclusive zone, is handled
52 /// with this view offset (rather than added as a constant elsewhere in the code). This allows
53 /// for natural handling of fullscreen windows, which must ignore work area padding.
54 view_offset: ViewOffset,
55
56 /// Whether to activate the previous, rather than the next, column upon column removal.
57 ///
58 /// When a new column is created and removed with no focus changes in-between, it is more
59 /// natural to activate the previously-focused column. This variable tracks that.
60 ///
61 /// Since we only create-and-activate columns immediately to the right of the active column (in
62 /// contrast to tabs in Firefox, for example), we can track this as a bool, rather than an
63 /// index of the previous column to activate.
64 ///
65 /// The value is the view offset that the previous column had before, to restore it.
66 activate_prev_column_on_removal: Option<f64>,
67
68 /// View offset to restore after unfullscreening or unmaximizing.
69 view_offset_to_restore: Option<f64>,
70
71 /// Windows in the closing animation.
72 closing_windows: Vec<ClosingWindow>,
73
74 /// View size for this space.
75 view_size: Size<f64, Logical>,
76
77 /// Working area for this space.
78 ///
79 /// Takes into account layer-shell exclusive zones and niri struts.
80 working_area: Rectangle<f64, Logical>,
81
82 /// Working area for this space excluding struts.
83 ///
84 /// Used for popup unconstraining. Popups can go over struts, but they shouldn't go over
85 /// the layer-shell top layer (which renders on top of popups).
86 parent_area: Rectangle<f64, Logical>,
87
88 /// Scale of the output the space is on (and rounds its sizes to).
89 scale: f64,
90
91 /// Clock for driving animations.
92 clock: Clock,
93
94 /// Configurable properties of the layout.
95 options: Rc<Options>,
96}
97
98niri_render_elements! {
99 ScrollingSpaceRenderElement<R> => {
100 Tile = TileRenderElement<R>,
101 ClosingWindow = ClosingWindowRenderElement,
102 TabIndicator = TabIndicatorRenderElement,
103 }
104}
105
106/// Extra per-column data.
107#[derive(Debug, Clone, Copy, PartialEq)]
108struct ColumnData {
109 /// Cached actual column width.
110 width: f64,
111}
112
113#[derive(Debug)]
114pub(super) enum ViewOffset {
115 /// The view offset is static.
116 Static(f64),
117 /// The view offset is animating.
118 Animation(Animation),
119 /// The view offset is controlled by the ongoing gesture.
120 Gesture(ViewGesture),
121}
122
123#[derive(Debug)]
124pub(super) struct ViewGesture {
125 current_view_offset: f64,
126 /// Animation for the extra offset to the current position.
127 ///
128 /// For example, when we need to activate a specific window during a DnD scroll.
129 animation: Option<Animation>,
130 tracker: SwipeTracker,
131 delta_from_tracker: f64,
132 // The view offset we'll use if needed for activate_prev_column_on_removal.
133 stationary_view_offset: f64,
134 /// Whether the gesture is controlled by the touchpad.
135 is_touchpad: bool,
136
137 // If this gesture is for drag-and-drop scrolling, this is the last event's unadjusted
138 // timestamp.
139 dnd_last_event_time: Option<Duration>,
140 // Time when the drag-and-drop scroll delta became non-zero, used for debouncing.
141 //
142 // If `None` then the scroll delta is currently zero.
143 dnd_nonzero_start_time: Option<Duration>,
144}
145
146static COLUMN_ID_COUNTER: IdCounter = IdCounter::new();
147
148#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
149pub struct ColumnId(u64);
150
151impl ColumnId {
152 fn next() -> ColumnId {
153 ColumnId(COLUMN_ID_COUNTER.next())
154 }
155
156 pub fn get(self) -> u64 {
157 self.0
158 }
159
160 pub fn specific(id: u64) -> Self {
161 Self(id)
162 }
163}
164
165#[derive(Debug)]
166pub struct Column<W: LayoutElement> {
167 /// Tiles in this column.
168 ///
169 /// Must be non-empty.
170 tiles: Vec<Tile<W>>,
171
172 /// Extra per-tile data.
173 ///
174 /// Must have the same number of elements as `tiles`.
175 data: Vec<TileData>,
176
177 /// Index of the currently active tile.
178 active_tile_idx: usize,
179
180 /// Desired width of this column.
181 ///
182 /// If the column is full-width or full-screened, this is the width that should be restored
183 /// upon unfullscreening and untoggling full-width.
184 width: ColumnWidth,
185
186 /// Currently selected preset width index.
187 preset_width_idx: Option<usize>,
188
189 /// Whether this column is full-width.
190 is_full_width: bool,
191
192 /// Whether this column is going to be fullscreen.
193 ///
194 /// This is the compositor-side fullscreen state, so it changes immediately upon
195 /// set_fullscreen(). The actual tiles will take some time to respond to the fullscreen request
196 /// and become fullscreen.
197 ///
198 /// Similarly, unsetting fullscreen will change this value to false immediately, and tiles will
199 /// take some time to catch up and actually unfullscreen.
200 is_pending_fullscreen: bool,
201
202 /// Whether this column is going to be maximized.
203 ///
204 /// Can be `true` together with `is_pending_fullscreen`, which means that the column is
205 /// effectively pending fullscreen, but unfullscreening should go back to maximized state,
206 /// rather than normal.
207 is_pending_maximized: bool,
208
209 /// How this column displays and arranges windows.
210 display_mode: ColumnDisplay,
211
212 /// Tab indicator for the tabbed display mode.
213 tab_indicator: TabIndicator,
214
215 /// Animation of the render offset during window swapping.
216 move_x_animation: Option<MoveAnimation>,
217
218 /// Animation of a column visually moving vertically.
219 move_y_animation: Option<MoveAnimation>,
220
221 /// Latest known view size for this column's workspace.
222 view_size: Size<f64, Logical>,
223
224 /// Latest known working area for this column's workspace.
225 working_area: Rectangle<f64, Logical>,
226
227 /// Working area for this column's workspace excluding struts.
228 ///
229 /// Used for maximize-to-edges.
230 parent_area: Rectangle<f64, Logical>,
231
232 /// Scale of the output the column is on (and rounds its sizes to).
233 scale: f64,
234
235 /// Clock for driving animations.
236 clock: Clock,
237
238 /// Configurable properties of the layout.
239 options: Rc<Options>,
240
241 /// Unique ID of this column.
242 id: ColumnId,
243}
244
245/// Extra per-tile data.
246#[derive(Debug, Clone, Copy, PartialEq)]
247struct TileData {
248 /// Requested height of the window.
249 ///
250 /// This is window height, not tile height, so it excludes tile decorations.
251 height: WindowHeight,
252
253 /// Cached actual size of the tile.
254 size: Size<f64, Logical>,
255
256 /// Cached whether the tile is being interactively resized by its left edge.
257 interactively_resizing_by_left_edge: bool,
258}
259
260/// Width of a column.
261#[derive(Debug, Clone, Copy, PartialEq)]
262pub enum ColumnWidth {
263 /// Proportion of the current view width.
264 Proportion(f64),
265 /// Fixed width in logical pixels.
266 Fixed(f64),
267}
268
269/// Height of a window in a column.
270///
271/// Every window but one in a column must be `Auto`-sized so that the total height can add up to
272/// the workspace height. Resizing a window converts all other windows to `Auto`, weighted to
273/// preserve their visual heights at the moment of the conversion.
274///
275/// In contrast to column widths, proportional height changes are converted to, and stored as,
276/// fixed height right away. With column widths you frequently want e.g. two columns side-by-side
277/// with 50% width each, and you want them to remain this way when moving to a differently sized
278/// monitor. Windows in a column, however, already auto-size to fill the available height, giving
279/// you this behavior. The main reason to set a different window height, then, is when you want
280/// something in the window to fit exactly, e.g. to fit 30 lines in a terminal, which corresponds
281/// to the `Fixed` variant.
282#[derive(Debug, Clone, Copy, PartialEq)]
283pub enum WindowHeight {
284 /// Automatically computed *tile* height, distributed across the column according to weights.
285 ///
286 /// This controls the tile height rather than the window height because it's easier in the auto
287 /// height distribution algorithm.
288 Auto { weight: f64 },
289 /// Fixed *window* height in logical pixels.
290 Fixed(f64),
291 /// One of the preset heights (tile or window).
292 Preset(usize),
293}
294
295/// Horizontal direction for an operation.
296///
297/// As operations often have a symmetrical counterpart, e.g. focus-right/focus-left, methods
298/// on `Scrolling` can sometimes be factored using the direction of the operation as a parameter.
299#[derive(Debug, Clone, Copy, PartialEq)]
300pub enum ScrollDirection {
301 Left,
302 Right,
303}
304
305#[derive(Debug)]
306struct MoveAnimation {
307 anim: Animation,
308 from: f64,
309 /// Whether this animation is for moving the tile between workspaces.
310 ///
311 /// Controls whether the tile is rendered uncropped and above others.
312 is_between_workspaces: bool,
313}
314
315impl<W: LayoutElement> ScrollingSpace<W> {
316 pub fn new(
317 view_size: Size<f64, Logical>,
318 parent_area: Rectangle<f64, Logical>,
319 scale: f64,
320 clock: Clock,
321 options: Rc<Options>,
322 ) -> Self {
323 let working_area = compute_working_area(parent_area, scale, options.layout.struts);
324
325 Self {
326 columns: Vec::new(),
327 data: Vec::new(),
328 active_column_idx: 0,
329 interactive_resize: None,
330 view_offset: ViewOffset::Static(0.),
331 activate_prev_column_on_removal: None,
332 view_offset_to_restore: None,
333 closing_windows: Vec::new(),
334 view_size,
335 working_area,
336 parent_area,
337 scale,
338 clock,
339 options,
340 }
341 }
342
343 pub fn update_config(
344 &mut self,
345 view_size: Size<f64, Logical>,
346 parent_area: Rectangle<f64, Logical>,
347 scale: f64,
348 options: Rc<Options>,
349 ) {
350 let working_area = compute_working_area(parent_area, scale, options.layout.struts);
351
352 for (column, data) in zip(&mut self.columns, &mut self.data) {
353 column.update_config(view_size, working_area, parent_area, scale, options.clone());
354 data.update(column);
355 }
356
357 self.view_size = view_size;
358 self.working_area = working_area;
359 self.parent_area = parent_area;
360 self.scale = scale;
361 self.options = options;
362
363 // Apply always-center and such right away.
364 if !self.columns.is_empty() && !self.view_offset.is_gesture() {
365 self.animate_view_offset_to_column(None, self.active_column_idx, None);
366 }
367 }
368
369 pub fn update_shaders(&mut self) {
370 for col in &mut self.columns {
371 col.update_shaders();
372 }
373 }
374
375 pub fn advance_animations(&mut self) {
376 if let ViewOffset::Animation(anim) = &self.view_offset {
377 if anim.is_done() {
378 self.view_offset = ViewOffset::Static(anim.to());
379 }
380 }
381
382 if let ViewOffset::Gesture(gesture) = &mut self.view_offset {
383 // Make sure the last event time doesn't go too much out of date (for
384 // workspaces not under cursor), causing sudden jumps.
385 //
386 // This happens after any dnd_scroll_gesture_scroll() calls (in
387 // Layout::advance_animations()), so it doesn't mess up the time delta there.
388 if let Some(last_time) = &mut gesture.dnd_last_event_time {
389 let now = self.clock.now_unadjusted();
390 if *last_time != now {
391 *last_time = now;
392
393 // If last_time was already == now, then dnd_scroll_gesture_scroll() must've
394 // updated the gesture already. Therefore, when this code runs, the pointer
395 // must be outside the DnD scrolling zone.
396 gesture.dnd_nonzero_start_time = None;
397 }
398 }
399
400 if let Some(anim) = &mut gesture.animation {
401 if anim.is_done() {
402 gesture.animation = None;
403 }
404 }
405 }
406
407 for col in &mut self.columns {
408 col.advance_animations();
409 }
410
411 self.closing_windows.retain_mut(|closing| {
412 closing.advance_animations();
413 closing.are_animations_ongoing()
414 });
415 }
416
417 pub fn are_animations_ongoing(&self) -> bool {
418 self.view_offset.is_animation_ongoing()
419 || self.columns.iter().any(Column::are_animations_ongoing)
420 || !self.closing_windows.is_empty()
421 }
422
423 pub fn are_transitions_ongoing(&self) -> bool {
424 !self.view_offset.is_static()
425 || self.columns.iter().any(Column::are_transitions_ongoing)
426 || !self.closing_windows.is_empty()
427 }
428
429 pub fn update_render_elements(&mut self, is_active: bool, layer: RenderLayer) {
430 let view_pos = Point::from((self.view_pos(), 0.));
431 let view_size = self.view_size;
432 let active_idx = self.active_column_idx;
433 for (col_idx, (col, col_x)) in self.columns_mut().enumerate() {
434 // Skip columns belonging to a different render layer.
435 if layer.is_normal() == col.is_moving_between_workspaces() {
436 continue;
437 }
438
439 let is_active = is_active && col_idx == active_idx;
440 let col_off = Point::from((col_x, 0.));
441 let col_pos = view_pos - col_off - col.render_offset();
442 let view_rect = Rectangle::new(col_pos, view_size);
443 col.update_render_elements(is_active, view_rect);
444 }
445 }
446
447 pub fn tiles(&self) -> impl Iterator<Item = &Tile<W>> + '_ {
448 self.columns.iter().flat_map(|col| col.tiles.iter())
449 }
450
451 pub fn tiles_mut(&mut self) -> impl Iterator<Item = &mut Tile<W>> + '_ {
452 self.columns.iter_mut().flat_map(|col| col.tiles.iter_mut())
453 }
454
455 pub fn is_empty(&self) -> bool {
456 self.columns.is_empty()
457 }
458
459 pub fn active_window(&self) -> Option<&W> {
460 if self.columns.is_empty() {
461 return None;
462 }
463
464 let col = &self.columns[self.active_column_idx];
465 Some(col.tiles[col.active_tile_idx].window())
466 }
467
468 pub fn active_window_mut(&mut self) -> Option<&mut W> {
469 if self.columns.is_empty() {
470 return None;
471 }
472
473 let col = &mut self.columns[self.active_column_idx];
474 Some(col.tiles[col.active_tile_idx].window_mut())
475 }
476
477 pub fn active_tile_mut(&mut self) -> Option<&mut Tile<W>> {
478 if self.columns.is_empty() {
479 return None;
480 }
481
482 let col = &mut self.columns[self.active_column_idx];
483 Some(&mut col.tiles[col.active_tile_idx])
484 }
485
486 pub fn is_active_pending_fullscreen(&self) -> bool {
487 if self.columns.is_empty() {
488 return false;
489 }
490
491 let col = &self.columns[self.active_column_idx];
492 col.pending_sizing_mode().is_fullscreen()
493 }
494
495 pub fn new_window_toplevel_bounds(&self, rules: &ResolvedWindowRules) -> Size<i32, Logical> {
496 let border_config = self.options.layout.border.merged_with(&rules.border);
497
498 let display_mode = rules
499 .default_column_display
500 .unwrap_or(self.options.layout.default_column_display);
501 let will_tab = display_mode == ColumnDisplay::Tabbed;
502 let extra_size = if will_tab {
503 TabIndicator::new(self.options.layout.tab_indicator).extra_size(1, self.scale)
504 } else {
505 Size::from((0., 0.))
506 };
507
508 compute_toplevel_bounds(
509 border_config,
510 self.working_area.size,
511 extra_size,
512 self.options.layout.gaps,
513 )
514 }
515
516 pub fn new_window_size(
517 &self,
518 width: Option<PresetSize>,
519 height: Option<PresetSize>,
520 rules: &ResolvedWindowRules,
521 ) -> Size<i32, Logical> {
522 let border = self.options.layout.border.merged_with(&rules.border);
523
524 let display_mode = rules
525 .default_column_display
526 .unwrap_or(self.options.layout.default_column_display);
527 let will_tab = display_mode == ColumnDisplay::Tabbed;
528 let extra = if will_tab {
529 TabIndicator::new(self.options.layout.tab_indicator).extra_size(1, self.scale)
530 } else {
531 Size::from((0., 0.))
532 };
533
534 let working_size = self.working_area.size;
535
536 let width = if let Some(size) = width {
537 let size = match resolve_preset_size(size, &self.options, working_size.w, extra.w) {
538 ResolvedSize::Tile(mut size) => {
539 if !border.off {
540 size -= border.width * 2.;
541 }
542 size
543 }
544 ResolvedSize::Window(size) => size,
545 };
546
547 max(1, size.floor() as i32)
548 } else {
549 0
550 };
551
552 let mut full_height = self.working_area.size.h - self.options.layout.gaps * 2.;
553 if !border.off {
554 full_height -= border.width * 2.;
555 }
556
557 let height = if let Some(height) = height {
558 let height = match resolve_preset_size(height, &self.options, working_size.h, extra.h) {
559 ResolvedSize::Tile(mut size) => {
560 if !border.off {
561 size -= border.width * 2.;
562 }
563 size
564 }
565 ResolvedSize::Window(size) => size,
566 };
567 f64::min(height, full_height)
568 } else {
569 full_height
570 };
571
572 Size::from((width, max(height.floor() as i32, 1)))
573 }
574
575 pub fn is_centering_focused_column(&self) -> bool {
576 self.options.layout.center_focused_column == CenterFocusedColumn::Always
577 || (self.options.layout.always_center_single_column && self.columns.len() <= 1)
578 }
579
580 fn compute_new_view_offset_fit(
581 &self,
582 target_x: Option<f64>,
583 col_x: f64,
584 width: f64,
585 mode: SizingMode,
586 ) -> f64 {
587 if mode.is_fullscreen() {
588 return 0.;
589 }
590
591 let (area, padding) = if mode.is_maximized() {
592 (self.parent_area, 0.)
593 } else {
594 (self.working_area, self.options.layout.gaps)
595 };
596
597 let target_x = target_x.unwrap_or_else(|| self.target_view_pos());
598
599 let new_offset =
600 compute_new_view_offset(target_x + area.loc.x, area.size.w, col_x, width, padding);
601
602 // Non-fullscreen windows are always offset at least by the working area position.
603 new_offset - area.loc.x
604 }
605
606 fn compute_new_view_offset_centered(
607 &self,
608 target_x: Option<f64>,
609 col_x: f64,
610 width: f64,
611 mode: SizingMode,
612 ) -> f64 {
613 if mode.is_fullscreen() {
614 return self.compute_new_view_offset_fit(target_x, col_x, width, mode);
615 }
616
617 let area = if mode.is_maximized() {
618 self.parent_area
619 } else {
620 self.working_area
621 };
622
623 // Columns wider than the view are left-aligned (the fit code can deal with that).
624 if area.size.w <= width {
625 return self.compute_new_view_offset_fit(target_x, col_x, width, mode);
626 }
627
628 -(area.size.w - width) / 2. - area.loc.x
629 }
630
631 fn compute_new_view_offset_for_column_fit(&self, target_x: Option<f64>, idx: usize) -> f64 {
632 let col = &self.columns[idx];
633 self.compute_new_view_offset_fit(
634 target_x,
635 self.column_x(idx),
636 col.width(),
637 col.sizing_mode(),
638 )
639 }
640
641 fn compute_new_view_offset_for_column_centered(
642 &self,
643 target_x: Option<f64>,
644 idx: usize,
645 ) -> f64 {
646 let col = &self.columns[idx];
647 self.compute_new_view_offset_centered(
648 target_x,
649 self.column_x(idx),
650 col.width(),
651 col.sizing_mode(),
652 )
653 }
654
655 fn compute_new_view_offset_for_column(
656 &self,
657 target_x: Option<f64>,
658 idx: usize,
659 prev_idx: Option<usize>,
660 ) -> f64 {
661 if self.is_centering_focused_column() {
662 return self.compute_new_view_offset_for_column_centered(target_x, idx);
663 }
664
665 match self.options.layout.center_focused_column {
666 CenterFocusedColumn::Always => {
667 self.compute_new_view_offset_for_column_centered(target_x, idx)
668 }
669 CenterFocusedColumn::OnOverflow => {
670 let Some(prev_idx) = prev_idx else {
671 return self.compute_new_view_offset_for_column_fit(target_x, idx);
672 };
673
674 // Activating the same column.
675 if prev_idx == idx {
676 return self.compute_new_view_offset_for_column_fit(target_x, idx);
677 }
678
679 // Always take the left or right neighbor of the target as the source.
680 let source_idx = if prev_idx > idx {
681 min(idx + 1, self.columns.len() - 1)
682 } else {
683 idx.saturating_sub(1)
684 };
685
686 let source_col_x = self.column_x(source_idx);
687 let source_col_width = self.columns[source_idx].width();
688
689 let target_col_x = self.column_x(idx);
690 let target_col_width = self.columns[idx].width();
691
692 // NOTE: This logic won't work entirely correctly with small fixed-size maximized
693 // windows (they have a different area and padding).
694 let total_width = if source_col_x < target_col_x {
695 // Source is left from target.
696 target_col_x - source_col_x + target_col_width
697 } else {
698 // Source is right from target.
699 source_col_x - target_col_x + source_col_width
700 } + self.options.layout.gaps * 2.;
701
702 // If it fits together, do a normal animation, otherwise center the new column.
703 if total_width <= self.working_area.size.w {
704 self.compute_new_view_offset_for_column_fit(target_x, idx)
705 } else {
706 self.compute_new_view_offset_for_column_centered(target_x, idx)
707 }
708 }
709 CenterFocusedColumn::Never => {
710 self.compute_new_view_offset_for_column_fit(target_x, idx)
711 }
712 }
713 }
714
715 fn animate_view_offset(&mut self, idx: usize, new_view_offset: f64) {
716 self.animate_view_offset_with_config(
717 idx,
718 new_view_offset,
719 self.options.animations.horizontal_view_movement.0,
720 );
721 }
722
723 fn animate_view_offset_with_config(
724 &mut self,
725 idx: usize,
726 new_view_offset: f64,
727 config: niri_config::Animation,
728 ) {
729 let new_col_x = self.column_x(idx);
730 let old_col_x = self.column_x(self.active_column_idx);
731 let offset_delta = old_col_x - new_col_x;
732 self.view_offset.offset(offset_delta);
733
734 let pixel = 1. / self.scale;
735
736 // If our view offset is already this or animating towards this, we don't need to do
737 // anything.
738 let to_diff = new_view_offset - self.view_offset.target();
739 if to_diff.abs() < pixel {
740 // Correct for any inaccuracy.
741 self.view_offset.offset(to_diff);
742 return;
743 }
744
745 match &mut self.view_offset {
746 ViewOffset::Gesture(gesture) if gesture.dnd_last_event_time.is_some() => {
747 gesture.stationary_view_offset = new_view_offset;
748
749 let current_pos = gesture.current_view_offset - gesture.delta_from_tracker;
750 gesture.delta_from_tracker = new_view_offset - current_pos;
751 let offset_delta = new_view_offset - gesture.current_view_offset;
752 gesture.current_view_offset = new_view_offset;
753
754 gesture.animate_from(-offset_delta, self.clock.clone(), config);
755 }
756 _ => {
757 // FIXME: also compute and use current velocity.
758 self.view_offset = ViewOffset::Animation(Animation::new(
759 self.clock.clone(),
760 self.view_offset.current(),
761 new_view_offset,
762 0.,
763 config,
764 ));
765 }
766 }
767 }
768
769 fn animate_view_offset_to_column_centered(
770 &mut self,
771 target_x: Option<f64>,
772 idx: usize,
773 config: niri_config::Animation,
774 ) {
775 let new_view_offset = self.compute_new_view_offset_for_column_centered(target_x, idx);
776 self.animate_view_offset_with_config(idx, new_view_offset, config);
777 }
778
779 fn animate_view_offset_to_column_with_config(
780 &mut self,
781 target_x: Option<f64>,
782 idx: usize,
783 prev_idx: Option<usize>,
784 config: niri_config::Animation,
785 ) {
786 let new_view_offset = self.compute_new_view_offset_for_column(target_x, idx, prev_idx);
787 self.animate_view_offset_with_config(idx, new_view_offset, config);
788 }
789
790 fn animate_view_offset_to_column(
791 &mut self,
792 target_x: Option<f64>,
793 idx: usize,
794 prev_idx: Option<usize>,
795 ) {
796 self.animate_view_offset_to_column_with_config(
797 target_x,
798 idx,
799 prev_idx,
800 self.options.animations.horizontal_view_movement.0,
801 )
802 }
803
804 fn activate_column(&mut self, idx: usize) {
805 self.activate_column_with_anim_config(
806 idx,
807 self.options.animations.horizontal_view_movement.0,
808 );
809 }
810
811 fn activate_column_with_anim_config(&mut self, idx: usize, config: niri_config::Animation) {
812 if self.active_column_idx == idx
813 // During a DnD scroll, animate even when activating the same window, for DnD hold.
814 && (self.columns.is_empty() || !self.view_offset.is_dnd_scroll())
815 {
816 return;
817 }
818
819 self.animate_view_offset_to_column_with_config(
820 None,
821 idx,
822 Some(self.active_column_idx),
823 config,
824 );
825
826 if self.active_column_idx != idx {
827 self.active_column_idx = idx;
828
829 // A different column was activated; reset the flag.
830 self.activate_prev_column_on_removal = None;
831 self.view_offset_to_restore = None;
832 self.interactive_resize = None;
833 }
834 }
835
836 pub(super) fn insert_position(&self, pos: Point<f64, Logical>) -> InsertPosition {
837 if self.columns.is_empty() {
838 return InsertPosition::NewColumn(0);
839 }
840
841 let x = pos.x + self.view_pos();
842
843 // Aim for the center of the gap.
844 let x = x + self.options.layout.gaps / 2.;
845 let y = pos.y + self.options.layout.gaps / 2.;
846
847 // Insert position is before the first column.
848 if x < 0. {
849 return InsertPosition::NewColumn(0);
850 }
851
852 // Find the closest gap between columns.
853 let (closest_col_idx, col_x) = self
854 .column_xs(self.data.iter().copied())
855 .enumerate()
856 .min_by_key(|(_, col_x)| NotNan::new((col_x - x).abs()).unwrap())
857 .unwrap();
858
859 // Find the column containing the position.
860 let (col_idx, _) = self
861 .column_xs(self.data.iter().copied())
862 .enumerate()
863 .take_while(|(_, col_x)| *col_x <= x)
864 .last()
865 .unwrap_or((0, 0.));
866
867 // Insert position is past the last column.
868 if col_idx == self.columns.len() {
869 return InsertPosition::NewColumn(closest_col_idx);
870 }
871
872 // Find the closest gap between tiles.
873 let col = &self.columns[col_idx];
874
875 let (closest_tile_idx, tile_y) = if col.display_mode == ColumnDisplay::Tabbed {
876 // In tabbed mode, there's only one tile visible, and we want to check its top and
877 // bottom.
878 let top = col.tile_offsets().nth(col.active_tile_idx).unwrap().y;
879 let bottom = top + col.data[col.active_tile_idx].size.h;
880 if (top - y).abs() <= (bottom - y).abs() {
881 (col.active_tile_idx, top)
882 } else {
883 (col.active_tile_idx + 1, bottom)
884 }
885 } else {
886 col.tile_offsets()
887 .map(|tile_off| tile_off.y)
888 .enumerate()
889 .min_by_key(|(_, tile_y)| NotNan::new((tile_y - y).abs()).unwrap())
890 .unwrap()
891 };
892
893 // Return the closest among the vertical and the horizontal gap.
894 let vert_dist = (col_x - x).abs();
895 let hor_dist = (tile_y - y).abs();
896 if vert_dist <= hor_dist {
897 InsertPosition::NewColumn(closest_col_idx)
898 } else {
899 InsertPosition::InColumn(col_idx, closest_tile_idx)
900 }
901 }
902
903 pub fn add_tile(
904 &mut self,
905 col_idx: Option<usize>,
906 tile: Tile<W>,
907 activate: bool,
908 width: ColumnWidth,
909 is_full_width: bool,
910 anim: Option<niri_config::Animation>,
911 ) {
912 let column = Column::new_with_tile(
913 tile,
914 self.view_size,
915 self.working_area,
916 self.parent_area,
917 self.scale,
918 width,
919 is_full_width,
920 );
921
922 self.add_column(col_idx, column, activate, anim);
923 }
924
925 pub fn add_tile_to_column(
926 &mut self,
927 col_idx: usize,
928 tile_idx: Option<usize>,
929 tile: Tile<W>,
930 activate: bool,
931 ) {
932 let prev_next_x = self.column_x(col_idx + 1);
933
934 let target_column = &mut self.columns[col_idx];
935 let tile_idx = tile_idx.unwrap_or(target_column.tiles.len());
936 let mut prev_active_tile_idx = target_column.active_tile_idx;
937
938 target_column.add_tile_at(tile_idx, tile);
939 self.data[col_idx].update(target_column);
940
941 if tile_idx <= prev_active_tile_idx {
942 target_column.active_tile_idx += 1;
943 prev_active_tile_idx += 1;
944 }
945
946 if activate {
947 target_column.activate_idx(tile_idx);
948 if self.active_column_idx != col_idx {
949 self.activate_column(col_idx);
950 }
951 }
952
953 let target_column = &mut self.columns[col_idx];
954 if target_column.display_mode == ColumnDisplay::Tabbed {
955 if target_column.active_tile_idx == tile_idx {
956 // Fade out the previously active tile.
957 let tile = &mut target_column.tiles[prev_active_tile_idx];
958 tile.animate_alpha(1., 0., self.options.animations.window_movement.0);
959 } else {
960 // Fade out when adding into a tabbed column into the background.
961 let tile = &mut target_column.tiles[tile_idx];
962 tile.animate_alpha(1., 0., self.options.animations.window_movement.0);
963 }
964 }
965
966 // Adding a wider window into a column increases its width now (even if the window will
967 // shrink later). Move the columns to account for this.
968 let offset = self.column_x(col_idx + 1) - prev_next_x;
969 if self.active_column_idx <= col_idx {
970 for col in &mut self.columns[col_idx + 1..] {
971 col.animate_move_x_from(-offset);
972 }
973 } else {
974 for col in &mut self.columns[..=col_idx] {
975 col.animate_move_x_from(offset);
976 }
977 }
978 }
979
980 pub fn add_tile_right_of(
981 &mut self,
982 right_of: &W::Id,
983 tile: Tile<W>,
984 activate: bool,
985 width: ColumnWidth,
986 is_full_width: bool,
987 anim: Option<niri_config::Animation>,
988 ) {
989 let right_of_idx = self
990 .columns
991 .iter()
992 .position(|col| col.contains(right_of))
993 .unwrap();
994 let col_idx = right_of_idx + 1;
995
996 self.add_tile(Some(col_idx), tile, activate, width, is_full_width, anim);
997 }
998
999 pub fn add_column(
1000 &mut self,
1001 idx: Option<usize>,
1002 mut column: Column<W>,
1003 activate: bool,
1004 anim_config: Option<niri_config::Animation>,
1005 ) {
1006 let was_empty = self.columns.is_empty();
1007
1008 let idx = idx.unwrap_or_else(|| {
1009 if was_empty {
1010 0
1011 } else {
1012 self.active_column_idx + 1
1013 }
1014 });
1015
1016 column.update_config(
1017 self.view_size,
1018 self.working_area,
1019 self.parent_area,
1020 self.scale,
1021 self.options.clone(),
1022 );
1023 self.data.insert(idx, ColumnData::new(&column));
1024 self.columns.insert(idx, column);
1025
1026 if !was_empty && idx <= self.active_column_idx {
1027 self.active_column_idx += 1;
1028 }
1029
1030 // Animate movement of other columns.
1031 let offset = self.column_x(idx + 1) - self.column_x(idx);
1032 let config = anim_config.unwrap_or(self.options.animations.window_movement.0);
1033 if self.active_column_idx <= idx {
1034 for col in &mut self.columns[idx + 1..] {
1035 col.animate_move_x_from_with_config(-offset, config);
1036 }
1037 } else {
1038 for col in &mut self.columns[..idx] {
1039 col.animate_move_x_from_with_config(offset, config);
1040 }
1041 }
1042
1043 if activate {
1044 // If this is the first window on an empty workspace, remove the effect of whatever
1045 // view_offset was left over and skip the animation.
1046 if was_empty {
1047 self.view_offset = ViewOffset::Static(0.);
1048 self.view_offset =
1049 ViewOffset::Static(self.compute_new_view_offset_for_column(None, idx, None));
1050 }
1051
1052 let prev_offset = (!was_empty && idx == self.active_column_idx + 1)
1053 .then(|| self.view_offset.stationary());
1054
1055 let anim_config =
1056 anim_config.unwrap_or(self.options.animations.horizontal_view_movement.0);
1057 self.activate_column_with_anim_config(idx, anim_config);
1058 self.activate_prev_column_on_removal = prev_offset;
1059 }
1060 }
1061
1062 pub fn remove_tile(&mut self, window: &W::Id, transaction: Transaction) -> RemovedTile<W> {
1063 let column_idx = self
1064 .columns
1065 .iter()
1066 .position(|col| col.contains(window))
1067 .unwrap();
1068 let column = &self.columns[column_idx];
1069
1070 let tile_idx = column.position(window).unwrap();
1071 self.remove_tile_by_idx(column_idx, tile_idx, transaction, None)
1072 }
1073
1074 pub fn remove_tile_by_idx(
1075 &mut self,
1076 column_idx: usize,
1077 tile_idx: usize,
1078 transaction: Transaction,
1079 anim_config: Option<niri_config::Animation>,
1080 ) -> RemovedTile<W> {
1081 // If this is the only tile in the column, remove the whole column.
1082 if self.columns[column_idx].tiles.len() == 1 {
1083 let mut column = self.remove_column_by_idx(column_idx, anim_config);
1084 return RemovedTile {
1085 tile: column.tiles.remove(tile_idx),
1086 width: column.width,
1087 is_full_width: column.is_full_width,
1088 is_floating: false,
1089 };
1090 }
1091
1092 let column = &mut self.columns[column_idx];
1093 let prev_width = self.data[column_idx].width;
1094
1095 let movement_config = anim_config.unwrap_or(self.options.animations.window_movement.0);
1096
1097 // Animate movement of other tiles.
1098 // FIXME: tiles can move by X too, in a centered or resizing layout with one window smaller
1099 // than the others.
1100 let offset_y = column.tile_offset(tile_idx + 1).y - column.tile_offset(tile_idx).y;
1101 for tile in &mut column.tiles[tile_idx + 1..] {
1102 tile.animate_move_y_from(offset_y);
1103 }
1104
1105 if column.display_mode == ColumnDisplay::Tabbed && tile_idx != column.active_tile_idx {
1106 // Fade in when removing background tab from a tabbed column.
1107 let tile = &mut column.tiles[tile_idx];
1108 tile.animate_alpha(0., 1., movement_config);
1109 }
1110
1111 let was_normal = column.sizing_mode().is_normal();
1112
1113 let tile = column.tiles.remove(tile_idx);
1114 column.data.remove(tile_idx);
1115
1116 // If an active column became non-fullscreen after removing the tile, clear the stored
1117 // unfullscreen offset.
1118 if column_idx == self.active_column_idx && !was_normal && column.sizing_mode().is_normal() {
1119 self.view_offset_to_restore = None;
1120 }
1121
1122 // If one window is left, reset its weight to 1.
1123 if column.data.len() == 1 {
1124 if let WindowHeight::Auto { weight } = &mut column.data[0].height {
1125 *weight = 1.;
1126 }
1127 }
1128
1129 // Stop interactive resize.
1130 if let Some(resize) = &self.interactive_resize {
1131 if tile.window().id() == &resize.window {
1132 self.interactive_resize = None;
1133 }
1134 }
1135
1136 let tile = RemovedTile {
1137 tile,
1138 width: column.width,
1139 is_full_width: column.is_full_width,
1140 is_floating: false,
1141 };
1142
1143 #[allow(clippy::comparison_chain)] // What do you even want here?
1144 if tile_idx < column.active_tile_idx {
1145 // A tile above was removed; preserve the current position.
1146 column.active_tile_idx -= 1;
1147 } else if tile_idx == column.active_tile_idx {
1148 // The active tile was removed, so the active tile index shifted to the next tile.
1149 if tile_idx == column.tiles.len() {
1150 // The bottom tile was removed and it was active, update active idx to remain valid.
1151 column.activate_idx(tile_idx - 1);
1152 } else {
1153 // Ensure the newly active tile animates to opaque.
1154 column.tiles[tile_idx].ensure_alpha_animates_to_1();
1155 }
1156 }
1157
1158 column.update_tile_sizes_with_transaction(true, transaction);
1159 self.data[column_idx].update(column);
1160 let offset = prev_width - column.width();
1161
1162 // Animate movement of the other columns.
1163 if self.active_column_idx <= column_idx {
1164 for col in &mut self.columns[column_idx + 1..] {
1165 col.animate_move_x_from_with_config(offset, movement_config);
1166 }
1167 } else {
1168 for col in &mut self.columns[..=column_idx] {
1169 col.animate_move_x_from_with_config(-offset, movement_config);
1170 }
1171 }
1172
1173 tile
1174 }
1175
1176 pub fn active_column(&self) -> Option<&Column<W>> {
1177 if self.columns.is_empty() {
1178 return None;
1179 }
1180
1181 Some(&self.columns[self.active_column_idx])
1182 }
1183
1184 pub fn remove_active_column(&mut self) -> Option<Column<W>> {
1185 if self.columns.is_empty() {
1186 return None;
1187 }
1188
1189 Some(self.remove_column_by_idx(self.active_column_idx, None))
1190 }
1191
1192 pub fn remove_column_by_idx(
1193 &mut self,
1194 column_idx: usize,
1195 anim_config: Option<niri_config::Animation>,
1196 ) -> Column<W> {
1197 // Animate movement of the other columns.
1198 let movement_config = anim_config.unwrap_or(self.options.animations.window_movement.0);
1199 let offset = self.column_x(column_idx + 1) - self.column_x(column_idx);
1200 if self.active_column_idx <= column_idx {
1201 for col in &mut self.columns[column_idx + 1..] {
1202 col.animate_move_x_from_with_config(offset, movement_config);
1203 }
1204 } else {
1205 for col in &mut self.columns[..column_idx] {
1206 col.animate_move_x_from_with_config(-offset, movement_config);
1207 }
1208 }
1209
1210 let column = self.columns.remove(column_idx);
1211 self.data.remove(column_idx);
1212
1213 // Stop interactive resize.
1214 if let Some(resize) = &self.interactive_resize {
1215 if column
1216 .tiles
1217 .iter()
1218 .any(|tile| tile.window().id() == &resize.window)
1219 {
1220 self.interactive_resize = None;
1221 }
1222 }
1223
1224 if column_idx + 1 == self.active_column_idx {
1225 // The previous column, that we were going to activate upon removal of the active
1226 // column, has just been itself removed.
1227 self.activate_prev_column_on_removal = None;
1228 }
1229
1230 if column_idx == self.active_column_idx {
1231 self.view_offset_to_restore = None;
1232 }
1233
1234 if self.columns.is_empty() {
1235 return column;
1236 }
1237
1238 let view_config = anim_config.unwrap_or(self.options.animations.horizontal_view_movement.0);
1239
1240 if column_idx < self.active_column_idx {
1241 // A column to the left was removed; preserve the current position.
1242 // FIXME: preserve activate_prev_column_on_removal.
1243 self.active_column_idx -= 1;
1244 self.activate_prev_column_on_removal = None;
1245 } else if column_idx == self.active_column_idx
1246 && self.activate_prev_column_on_removal.is_some()
1247 {
1248 // The active column was removed, and we needed to activate the previous column.
1249 if 0 < column_idx {
1250 let prev_offset = self.activate_prev_column_on_removal.unwrap();
1251
1252 self.activate_column_with_anim_config(self.active_column_idx - 1, view_config);
1253
1254 // Restore the view offset but make sure to scroll the view in case the
1255 // previous window had resized.
1256 self.animate_view_offset_with_config(
1257 self.active_column_idx,
1258 prev_offset,
1259 view_config,
1260 );
1261 self.animate_view_offset_to_column_with_config(
1262 None,
1263 self.active_column_idx,
1264 None,
1265 view_config,
1266 );
1267 }
1268 } else {
1269 self.activate_column_with_anim_config(
1270 min(self.active_column_idx, self.columns.len() - 1),
1271 view_config,
1272 );
1273 }
1274
1275 column
1276 }
1277
1278 pub fn update_window(&mut self, window: &W::Id, serial: Option<Serial>) {
1279 let (col_idx, column) = self
1280 .columns
1281 .iter_mut()
1282 .enumerate()
1283 .find(|(_, col)| col.contains(window))
1284 .unwrap();
1285 let was_normal = column.sizing_mode().is_normal();
1286 let prev_origin = column.tiles_origin();
1287
1288 let (tile_idx, tile) = column
1289 .tiles
1290 .iter_mut()
1291 .enumerate()
1292 .find(|(_, tile)| tile.window().id() == window)
1293 .unwrap();
1294
1295 let resize = tile.window_mut().interactive_resize_data();
1296
1297 // Do this before calling update_window() so it can get up-to-date info.
1298 if let Some(serial) = serial {
1299 tile.window_mut().on_commit(serial);
1300 }
1301
1302 let prev_width = self.data[col_idx].width;
1303
1304 column.update_window(window);
1305 self.data[col_idx].update(column);
1306 column.update_tile_sizes(false);
1307
1308 let offset = prev_width - self.data[col_idx].width;
1309
1310 // Move other columns in tandem with resizing.
1311 let ongoing_resize_anim = column.tiles[tile_idx].resize_animation().is_some();
1312 if offset != 0. {
1313 if self.active_column_idx <= col_idx {
1314 for col in &mut self.columns[col_idx + 1..] {
1315 // If there's a resize animation on the tile (that may have just started in
1316 // column.update_window()), then the apparent size change is smooth with no
1317 // sudden jumps. This corresponds to adding an X animation to adjacent columns.
1318 //
1319 // There could also be no resize animation with nonzero offset. This could
1320 // happen for example:
1321 // - if the window resized on its own, which we don't animate
1322 // - if the window resized by less than 10 px (the resize threshold)
1323 //
1324 // The latter case could also cancel an ongoing resize animation.
1325 //
1326 // Now, stationary columns shouldn't react to this offset change in any way,
1327 // i.e. their apparent X position should jump together with the resize.
1328 // However, adjacent columns that are already animating an X movement should
1329 // offset their animations to avoid the jump.
1330 //
1331 // Notably, this is necessary to fix the animation jump when resizing width back
1332 // and forth in quick succession (in a way that cancels the resize animation).
1333 if ongoing_resize_anim {
1334 col.animate_move_x_from_with_config(
1335 offset,
1336 self.options.animations.window_resize.anim,
1337 );
1338 } else {
1339 col.offset_move_anim_current(offset);
1340 }
1341 }
1342 } else {
1343 for col in &mut self.columns[..=col_idx] {
1344 if ongoing_resize_anim {
1345 col.animate_move_x_from_with_config(
1346 -offset,
1347 self.options.animations.window_resize.anim,
1348 );
1349 } else {
1350 col.offset_move_anim_current(-offset);
1351 }
1352 }
1353 }
1354 }
1355
1356 // When a column goes between fullscreen and non-fullscreen, the tiles origin can change.
1357 // The change comes from things like ignoring struts and hiding the tab indicator in
1358 // fullscreen, so both in X and Y directions.
1359 let column = &mut self.columns[col_idx];
1360 let new_origin = column.tiles_origin();
1361 let origin_delta = prev_origin - new_origin;
1362 if origin_delta != Point::new(0., 0.) {
1363 for (tile, _pos) in column.tiles_mut() {
1364 tile.animate_move_from(origin_delta);
1365 }
1366 }
1367
1368 if col_idx == self.active_column_idx {
1369 // If offset == 0, then don't mess with the view or the gesture. Some clients (Firefox,
1370 // Chromium, Electron) currently don't commit after the ack of a configure that drops
1371 // the Resizing state, which can trigger this code path for a while.
1372 let resize = if offset != 0. { resize } else { None };
1373 if let Some(resize) = resize {
1374 // Don't bother with the gesture.
1375 self.view_offset.cancel_gesture();
1376
1377 // If this is an interactive resize commit of an active window, then we need to
1378 // either preserve the view offset or adjust it accordingly.
1379 let centered = self.is_centering_focused_column();
1380
1381 let width = self.data[col_idx].width;
1382 let offset = if centered {
1383 // FIXME: when view_offset becomes fractional, this can be made additive too.
1384 let new_offset =
1385 -(self.working_area.size.w - width) / 2. - self.working_area.loc.x;
1386 new_offset - self.view_offset.target()
1387 } else if resize.edges.contains(ResizeEdge::LEFT) {
1388 -offset
1389 } else {
1390 0.
1391 };
1392
1393 self.view_offset.offset(offset);
1394 }
1395
1396 // When the active column goes fullscreen, store the view offset to restore later.
1397 let is_normal = self.columns[col_idx].sizing_mode().is_normal();
1398 if was_normal && !is_normal {
1399 self.view_offset_to_restore = Some(self.view_offset.stationary());
1400 }
1401
1402 // Upon unfullscreening, restore the view offset.
1403 //
1404 // In tabbed display mode, there can be multiple tiles in a fullscreen column. They
1405 // will unfullscreen one by one, and the column width will shrink only when the
1406 // last tile unfullscreens. This is when we want to restore the view offset,
1407 // otherwise it will immediately reset back by the animate_view_offset below.
1408 let unfullscreen_offset = if !was_normal && is_normal {
1409 // Take the value unconditionally, even if the view is currently frozen by
1410 // a view gesture. It shouldn't linger around because it's only valid for this
1411 // particular unfullscreen.
1412 self.view_offset_to_restore.take()
1413 } else {
1414 None
1415 };
1416
1417 // We might need to move the view to ensure the resized window is still visible. But
1418 // only do it when the view isn't frozen by an interactive resize or a view gesture.
1419 if self.interactive_resize.is_none() && !self.view_offset.is_gesture() {
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 ongoing_resize_anim {
1423 self.options.animations.window_resize.anim
1424 } else {
1425 self.options.animations.horizontal_view_movement.0
1426 };
1427
1428 // Restore the view offset upon unfullscreening if needed.
1429 if let Some(prev_offset) = unfullscreen_offset {
1430 self.animate_view_offset_with_config(col_idx, prev_offset, config);
1431 }
1432
1433 // FIXME: we will want to skip the animation in some cases here to make continuously
1434 // resizing windows not look janky.
1435 self.animate_view_offset_to_column_with_config(None, col_idx, None, config);
1436 }
1437 }
1438 }
1439
1440 pub fn scroll_amount_to_activate(&self, window: &W::Id) -> f64 {
1441 let column_idx = self
1442 .columns
1443 .iter()
1444 .position(|col| col.contains(window))
1445 .unwrap();
1446
1447 if self.active_column_idx == column_idx {
1448 return 0.;
1449 }
1450
1451 // Consider the end of an ongoing animation because that's what compute to fit does too.
1452 let target_x = self.target_view_pos();
1453 let new_view_offset = self.compute_new_view_offset_for_column(
1454 Some(target_x),
1455 column_idx,
1456 Some(self.active_column_idx),
1457 );
1458
1459 let new_col_x = self.column_x(column_idx);
1460 let from_view_offset = target_x - new_col_x;
1461
1462 (from_view_offset - new_view_offset).abs() / self.working_area.size.w
1463 }
1464
1465 pub fn activate_window(&mut self, window: &W::Id) -> bool {
1466 let column_idx = self.columns.iter().position(|col| col.contains(window));
1467 let Some(column_idx) = column_idx else {
1468 return false;
1469 };
1470 let column = &mut self.columns[column_idx];
1471
1472 column.activate_window(window);
1473 self.activate_column(column_idx);
1474
1475 true
1476 }
1477
1478 pub fn start_close_animation_for_window(
1479 &mut self,
1480 renderer: &mut GlesRenderer,
1481 window: &W::Id,
1482 blocker: TransactionBlocker,
1483 ) {
1484 let (tile, mut tile_pos) = self
1485 .tiles_with_render_positions_mut(false)
1486 .find(|(tile, _)| tile.window().id() == window)
1487 .unwrap();
1488
1489 let Some(snapshot) = tile.take_unmap_snapshot() else {
1490 return;
1491 };
1492
1493 let tile_size = tile.tile_size();
1494
1495 let (col_idx, tile_idx) = self
1496 .columns
1497 .iter()
1498 .enumerate()
1499 .find_map(|(col_idx, col)| {
1500 col.tiles
1501 .iter()
1502 .position(|tile| tile.window().id() == window)
1503 .map(move |tile_idx| (col_idx, tile_idx))
1504 })
1505 .unwrap();
1506
1507 let col = &self.columns[col_idx];
1508 let removing_last = col.tiles.len() == 1;
1509
1510 // Skip closing animation for invisible tiles in a tabbed column.
1511 if col.display_mode == ColumnDisplay::Tabbed && tile_idx != col.active_tile_idx {
1512 return;
1513 }
1514
1515 tile_pos.x += self.view_pos();
1516
1517 if col_idx < self.active_column_idx {
1518 let offset = if removing_last {
1519 self.column_x(col_idx + 1) - self.column_x(col_idx)
1520 } else {
1521 self.data[col_idx].width
1522 - col
1523 .data
1524 .iter()
1525 .enumerate()
1526 .filter_map(|(idx, data)| {
1527 (idx != tile_idx).then_some(NotNan::new(data.size.w).unwrap())
1528 })
1529 .max()
1530 .map(NotNan::into_inner)
1531 .unwrap()
1532 };
1533 tile_pos.x -= offset;
1534 }
1535
1536 self.start_close_animation_for_tile(renderer, snapshot, tile_size, tile_pos, blocker);
1537 }
1538
1539 fn start_close_animation_for_tile(
1540 &mut self,
1541 renderer: &mut GlesRenderer,
1542 snapshot: TileRenderSnapshot,
1543 tile_size: Size<f64, Logical>,
1544 tile_pos: Point<f64, Logical>,
1545 blocker: TransactionBlocker,
1546 ) {
1547 let anim = Animation::new(
1548 self.clock.clone(),
1549 0.,
1550 1.,
1551 0.,
1552 self.options.animations.window_close.anim,
1553 );
1554
1555 let blocker = if self.options.disable_transactions {
1556 TransactionBlocker::completed()
1557 } else {
1558 blocker
1559 };
1560
1561 let scale = Scale::from(self.scale);
1562 let res = ClosingWindow::new(
1563 renderer, snapshot, scale, tile_size, tile_pos, blocker, anim,
1564 );
1565 match res {
1566 Ok(closing) => {
1567 self.closing_windows.push(closing);
1568 }
1569 Err(err) => {
1570 warn!("error creating a closing window animation: {err:?}");
1571 }
1572 }
1573 }
1574
1575 pub fn start_open_animation(&mut self, id: &W::Id) -> bool {
1576 self.columns
1577 .iter_mut()
1578 .any(|col| col.start_open_animation(id))
1579 }
1580
1581 pub fn focus_left(&mut self) -> bool {
1582 if self.active_column_idx == 0 {
1583 return false;
1584 }
1585 self.activate_column(self.active_column_idx - 1);
1586 true
1587 }
1588
1589 pub fn focus_right(&mut self) -> bool {
1590 if self.active_column_idx + 1 >= self.columns.len() {
1591 return false;
1592 }
1593
1594 self.activate_column(self.active_column_idx + 1);
1595 true
1596 }
1597
1598 pub fn focus_column_first(&mut self) {
1599 self.activate_column(0);
1600 }
1601
1602 pub fn focus_column_last(&mut self) {
1603 if self.columns.is_empty() {
1604 return;
1605 }
1606
1607 self.activate_column(self.columns.len() - 1);
1608 }
1609
1610 pub fn focus_column(&mut self, index: usize) {
1611 if self.columns.is_empty() {
1612 return;
1613 }
1614
1615 self.activate_column(index.saturating_sub(1).min(self.columns.len() - 1));
1616 }
1617
1618 pub fn focus_window_in_column(&mut self, index: u8) {
1619 if self.columns.is_empty() {
1620 return;
1621 }
1622
1623 self.columns[self.active_column_idx].focus_index(index);
1624 }
1625
1626 pub fn focus_down(&mut self) -> bool {
1627 if self.columns.is_empty() {
1628 return false;
1629 }
1630
1631 self.columns[self.active_column_idx].focus_down()
1632 }
1633
1634 pub fn focus_up(&mut self) -> bool {
1635 if self.columns.is_empty() {
1636 return false;
1637 }
1638
1639 self.columns[self.active_column_idx].focus_up()
1640 }
1641
1642 pub fn focus_down_or_left(&mut self) {
1643 if self.columns.is_empty() {
1644 return;
1645 }
1646
1647 let column = &mut self.columns[self.active_column_idx];
1648 if !column.focus_down() {
1649 self.focus_left();
1650 }
1651 }
1652
1653 pub fn focus_down_or_right(&mut self) {
1654 if self.columns.is_empty() {
1655 return;
1656 }
1657
1658 let column = &mut self.columns[self.active_column_idx];
1659 if !column.focus_down() {
1660 self.focus_right();
1661 }
1662 }
1663
1664 pub fn focus_up_or_left(&mut self) {
1665 if self.columns.is_empty() {
1666 return;
1667 }
1668
1669 let column = &mut self.columns[self.active_column_idx];
1670 if !column.focus_up() {
1671 self.focus_left();
1672 }
1673 }
1674
1675 pub fn focus_up_or_right(&mut self) {
1676 if self.columns.is_empty() {
1677 return;
1678 }
1679
1680 let column = &mut self.columns[self.active_column_idx];
1681 if !column.focus_up() {
1682 self.focus_right();
1683 }
1684 }
1685
1686 pub fn focus_top(&mut self) {
1687 if self.columns.is_empty() {
1688 return;
1689 }
1690
1691 self.columns[self.active_column_idx].focus_top()
1692 }
1693
1694 pub fn focus_bottom(&mut self) {
1695 if self.columns.is_empty() {
1696 return;
1697 }
1698
1699 self.columns[self.active_column_idx].focus_bottom()
1700 }
1701
1702 pub fn move_column_to_index(&mut self, index: usize) {
1703 if self.columns.is_empty() {
1704 return;
1705 }
1706
1707 self.move_column_to(index.saturating_sub(1).min(self.columns.len() - 1));
1708 }
1709
1710 fn move_column_to(&mut self, new_idx: usize) {
1711 if self.active_column_idx == new_idx {
1712 return;
1713 }
1714
1715 let current_col_x = self.column_x(self.active_column_idx);
1716 let next_col_x = self.column_x(self.active_column_idx + 1);
1717
1718 let mut column = self.columns.remove(self.active_column_idx);
1719 let data = self.data.remove(self.active_column_idx);
1720 cancel_resize_for_column(&mut self.interactive_resize, &mut column);
1721 self.columns.insert(new_idx, column);
1722 self.data.insert(new_idx, data);
1723
1724 // Preserve the camera position when moving to the left.
1725 let view_offset_delta = -self.column_x(self.active_column_idx) + current_col_x;
1726 self.view_offset.offset(view_offset_delta);
1727
1728 // The column we just moved is offset by the difference between its new and old position.
1729 let new_col_x = self.column_x(new_idx);
1730 self.columns[new_idx].animate_move_x_from(current_col_x - new_col_x);
1731
1732 // All columns in between moved by the width of the column that we just moved.
1733 let others_x_offset = next_col_x - current_col_x;
1734 if self.active_column_idx < new_idx {
1735 for col in &mut self.columns[self.active_column_idx..new_idx] {
1736 col.animate_move_x_from(others_x_offset);
1737 }
1738 } else {
1739 for col in &mut self.columns[new_idx + 1..=self.active_column_idx] {
1740 col.animate_move_x_from(-others_x_offset);
1741 }
1742 }
1743
1744 self.activate_column_with_anim_config(new_idx, self.options.animations.window_movement.0);
1745 }
1746
1747 pub fn move_left(&mut self) -> bool {
1748 if self.active_column_idx == 0 {
1749 return false;
1750 }
1751
1752 self.move_column_to(self.active_column_idx - 1);
1753 true
1754 }
1755
1756 pub fn move_right(&mut self) -> bool {
1757 let new_idx = self.active_column_idx + 1;
1758 if new_idx >= self.columns.len() {
1759 return false;
1760 }
1761
1762 self.move_column_to(new_idx);
1763 true
1764 }
1765
1766 pub fn move_column_to_first(&mut self) {
1767 self.move_column_to(0);
1768 }
1769
1770 pub fn move_column_to_last(&mut self) {
1771 if self.columns.is_empty() {
1772 return;
1773 }
1774
1775 let new_idx = self.columns.len() - 1;
1776 self.move_column_to(new_idx);
1777 }
1778
1779 pub fn move_down(&mut self) -> bool {
1780 if self.columns.is_empty() {
1781 return false;
1782 }
1783
1784 self.columns[self.active_column_idx].move_down()
1785 }
1786
1787 pub fn move_up(&mut self) -> bool {
1788 if self.columns.is_empty() {
1789 return false;
1790 }
1791
1792 self.columns[self.active_column_idx].move_up()
1793 }
1794
1795 pub fn consume_or_expel_window_left(&mut self, window: Option<&W::Id>) {
1796 if self.columns.is_empty() {
1797 return;
1798 }
1799
1800 let (source_col_idx, source_tile_idx) = if let Some(window) = window {
1801 self.columns
1802 .iter_mut()
1803 .enumerate()
1804 .find_map(|(col_idx, col)| {
1805 col.tiles
1806 .iter()
1807 .position(|tile| tile.window().id() == window)
1808 .map(|tile_idx| (col_idx, tile_idx))
1809 })
1810 .unwrap()
1811 } else {
1812 let source_col_idx = self.active_column_idx;
1813 let source_tile_idx = self.columns[self.active_column_idx].active_tile_idx;
1814 (source_col_idx, source_tile_idx)
1815 };
1816
1817 let source_column = &self.columns[source_col_idx];
1818 let prev_off = source_column.tile_offset(source_tile_idx);
1819
1820 let source_tile_was_active = self.active_column_idx == source_col_idx
1821 && source_column.active_tile_idx == source_tile_idx;
1822
1823 if source_column.tiles.len() == 1 {
1824 if source_col_idx == 0 {
1825 return;
1826 }
1827
1828 // Move into adjacent column.
1829 let target_column_idx = source_col_idx - 1;
1830
1831 let offset = if self.active_column_idx <= source_col_idx {
1832 // Tiles to the right animate from the following column.
1833 self.column_x(source_col_idx) - self.column_x(target_column_idx)
1834 } else {
1835 // Tiles to the left animate to preserve their right edge position.
1836 f64::max(
1837 0.,
1838 self.data[target_column_idx].width - self.data[source_col_idx].width,
1839 )
1840 };
1841 let mut offset = Point::from((offset, 0.));
1842
1843 if source_tile_was_active {
1844 // Make sure the previous (target) column is activated so the animation looks right.
1845 //
1846 // However, if it was already going to be activated, leave the offset as is. This
1847 // improves the workflow that has become common with tabbed columns: open a new
1848 // window, then immediately consume it left as a new tab.
1849 self.activate_prev_column_on_removal
1850 .get_or_insert(self.view_offset.stationary() + offset.x);
1851 }
1852
1853 offset += self.columns[source_col_idx].render_offset();
1854 let RemovedTile { tile, .. } = self.remove_tile_by_idx(
1855 source_col_idx,
1856 0,
1857 Transaction::new(),
1858 Some(self.options.animations.window_movement.0),
1859 );
1860 self.add_tile_to_column(target_column_idx, None, tile, source_tile_was_active);
1861
1862 let target_column = &mut self.columns[target_column_idx];
1863 offset -= target_column.render_offset();
1864 offset += prev_off - target_column.tile_offset(target_column.tiles.len() - 1);
1865
1866 let new_tile = target_column.tiles.last_mut().unwrap();
1867 new_tile.animate_move_from(offset);
1868 } else {
1869 // Move out of column.
1870 let mut offset = source_column.render_offset();
1871
1872 let removed =
1873 self.remove_tile_by_idx(source_col_idx, source_tile_idx, Transaction::new(), None);
1874
1875 // We're inserting into the source column position.
1876 let target_column_idx = source_col_idx;
1877
1878 self.add_tile(
1879 Some(target_column_idx),
1880 removed.tile,
1881 source_tile_was_active,
1882 removed.width,
1883 removed.is_full_width,
1884 Some(self.options.animations.window_movement.0),
1885 );
1886
1887 if source_tile_was_active {
1888 // We added to the left, don't activate even further left on removal.
1889 self.activate_prev_column_on_removal = None;
1890 }
1891
1892 if target_column_idx <= self.active_column_idx {
1893 // Tiles to the left animate from the following column.
1894 offset.x += self.column_x(target_column_idx + 1) - self.column_x(target_column_idx);
1895 }
1896
1897 let new_col = &mut self.columns[target_column_idx];
1898 offset += prev_off - new_col.tile_offset(0);
1899 new_col.tiles[0].animate_move_from(offset);
1900 }
1901 }
1902
1903 pub fn consume_or_expel_window_right(&mut self, window: Option<&W::Id>) {
1904 if self.columns.is_empty() {
1905 return;
1906 }
1907
1908 let (source_col_idx, source_tile_idx) = if let Some(window) = window {
1909 self.columns
1910 .iter_mut()
1911 .enumerate()
1912 .find_map(|(col_idx, col)| {
1913 col.tiles
1914 .iter()
1915 .position(|tile| tile.window().id() == window)
1916 .map(|tile_idx| (col_idx, tile_idx))
1917 })
1918 .unwrap()
1919 } else {
1920 let source_col_idx = self.active_column_idx;
1921 let source_tile_idx = self.columns[self.active_column_idx].active_tile_idx;
1922 (source_col_idx, source_tile_idx)
1923 };
1924
1925 let cur_x = self.column_x(source_col_idx);
1926
1927 let source_column = &self.columns[source_col_idx];
1928 let mut offset = source_column.render_offset();
1929 let prev_off = source_column.tile_offset(source_tile_idx);
1930
1931 let source_tile_was_active = self.active_column_idx == source_col_idx
1932 && source_column.active_tile_idx == source_tile_idx;
1933
1934 if source_column.tiles.len() == 1 {
1935 if source_col_idx + 1 == self.columns.len() {
1936 return;
1937 }
1938
1939 // Move into adjacent column.
1940 let target_column_idx = source_col_idx;
1941
1942 offset.x += cur_x - self.column_x(source_col_idx + 1);
1943 offset -= self.columns[source_col_idx + 1].render_offset();
1944
1945 if source_tile_was_active {
1946 // Make sure the target column gets activated.
1947 self.activate_prev_column_on_removal = None;
1948 }
1949
1950 let RemovedTile { tile, .. } = self.remove_tile_by_idx(
1951 source_col_idx,
1952 0,
1953 Transaction::new(),
1954 Some(self.options.animations.window_movement.0),
1955 );
1956 self.add_tile_to_column(target_column_idx, None, tile, source_tile_was_active);
1957
1958 let target_column = &mut self.columns[target_column_idx];
1959 offset += prev_off - target_column.tile_offset(target_column.tiles.len() - 1);
1960
1961 let new_tile = target_column.tiles.last_mut().unwrap();
1962 new_tile.animate_move_from(offset);
1963 } else {
1964 // Move out of column.
1965 let prev_width = self.data[source_col_idx].width;
1966
1967 let removed =
1968 self.remove_tile_by_idx(source_col_idx, source_tile_idx, Transaction::new(), None);
1969
1970 let target_column_idx = source_col_idx + 1;
1971
1972 self.add_tile(
1973 Some(target_column_idx),
1974 removed.tile,
1975 source_tile_was_active,
1976 removed.width,
1977 removed.is_full_width,
1978 Some(self.options.animations.window_movement.0),
1979 );
1980
1981 offset.x += if self.active_column_idx <= target_column_idx {
1982 // Tiles to the right animate to the following column.
1983 cur_x - self.column_x(target_column_idx)
1984 } else {
1985 // Tiles to the left animate for a change in width.
1986 -f64::max(0., prev_width - self.data[target_column_idx].width)
1987 };
1988
1989 let new_col = &mut self.columns[target_column_idx];
1990 offset += prev_off - new_col.tile_offset(0);
1991 new_col.tiles[0].animate_move_from(offset);
1992 }
1993 }
1994
1995 pub fn consume_into_column(&mut self) {
1996 if self.columns.len() < 2 {
1997 return;
1998 }
1999
2000 if self.active_column_idx == self.columns.len() - 1 {
2001 return;
2002 }
2003
2004 let target_column_idx = self.active_column_idx;
2005 let source_column_idx = self.active_column_idx + 1;
2006
2007 let mut offset = self.columns[source_column_idx].render_offset();
2008 offset.x += self.column_x(source_column_idx);
2009 offset.x -= self.column_x(target_column_idx);
2010 let prev_off = self.columns[source_column_idx].tile_offset(0);
2011
2012 let removed = self.remove_tile_by_idx(source_column_idx, 0, Transaction::new(), None);
2013 self.add_tile_to_column(target_column_idx, None, removed.tile, false);
2014
2015 let target_column = &mut self.columns[target_column_idx];
2016 offset += prev_off - target_column.tile_offset(target_column.tiles.len() - 1);
2017 offset -= target_column.render_offset();
2018
2019 let new_tile = target_column.tiles.last_mut().unwrap();
2020 new_tile.animate_move_from(offset);
2021 }
2022
2023 pub fn expel_from_column(&mut self) {
2024 if self.columns.is_empty() {
2025 return;
2026 }
2027
2028 let source_col_idx = self.active_column_idx;
2029 let target_col_idx = self.active_column_idx + 1;
2030 let cur_x = self.column_x(source_col_idx);
2031
2032 let source_column = &self.columns[self.active_column_idx];
2033 if source_column.tiles.len() == 1 {
2034 return;
2035 }
2036
2037 let source_tile_idx = source_column.tiles.len() - 1;
2038
2039 let mut offset = source_column.render_offset();
2040 let prev_off = source_column.tile_offset(source_tile_idx);
2041
2042 let removed =
2043 self.remove_tile_by_idx(source_col_idx, source_tile_idx, Transaction::new(), None);
2044
2045 self.add_tile(
2046 Some(target_col_idx),
2047 removed.tile,
2048 false,
2049 removed.width,
2050 removed.is_full_width,
2051 Some(self.options.animations.window_movement.0),
2052 );
2053
2054 offset.x += cur_x - self.column_x(target_col_idx);
2055
2056 let new_col = &mut self.columns[target_col_idx];
2057 offset += prev_off - new_col.tile_offset(0);
2058 new_col.tiles[0].animate_move_from(offset);
2059 }
2060
2061 pub fn swap_window_in_direction(&mut self, direction: ScrollDirection) {
2062 if self.columns.is_empty() {
2063 return;
2064 }
2065
2066 // if this is the first (resp. last column), then this operation is equivalent
2067 // to an `consume_or_expel_window_left` (resp. `consume_or_expel_window_right`)
2068 match direction {
2069 ScrollDirection::Left => {
2070 if self.active_column_idx == 0 {
2071 return;
2072 }
2073 }
2074 ScrollDirection::Right => {
2075 if self.active_column_idx == self.columns.len() - 1 {
2076 return;
2077 }
2078 }
2079 }
2080
2081 let source_column_idx = self.active_column_idx;
2082 let target_column_idx = self.active_column_idx.wrapping_add_signed(match direction {
2083 ScrollDirection::Left => -1,
2084 ScrollDirection::Right => 1,
2085 });
2086
2087 // if both source and target columns contain a single tile, then the operation is equivalent
2088 // to a simple column move
2089 if self.columns[source_column_idx].tiles.len() == 1
2090 && self.columns[target_column_idx].tiles.len() == 1
2091 {
2092 return self.move_column_to(target_column_idx);
2093 }
2094
2095 let source_tile_idx = self.columns[source_column_idx].active_tile_idx;
2096 let target_tile_idx = self.columns[target_column_idx].active_tile_idx;
2097 let source_column_drained = self.columns[source_column_idx].tiles.len() == 1;
2098
2099 // capture the original positions of the tiles
2100 let (mut source_pt, mut target_pt) = (
2101 self.columns[source_column_idx].render_offset()
2102 + self.columns[source_column_idx].tile_offset(source_tile_idx),
2103 self.columns[target_column_idx].render_offset()
2104 + self.columns[target_column_idx].tile_offset(target_tile_idx),
2105 );
2106 source_pt.x += self.column_x(source_column_idx);
2107 target_pt.x += self.column_x(target_column_idx);
2108
2109 let transaction = Transaction::new();
2110
2111 // If the source column contains a single tile, this will also remove the column.
2112 // When this happens `source_column_drained` will be set and the column will need to be
2113 // recreated with `add_tile`
2114 let source_removed = self.remove_tile_by_idx(
2115 source_column_idx,
2116 source_tile_idx,
2117 transaction.clone(),
2118 None,
2119 );
2120
2121 {
2122 // special case when the source column disappears after removing its last tile
2123 let adjusted_target_column_idx =
2124 if direction == ScrollDirection::Right && source_column_drained {
2125 target_column_idx - 1
2126 } else {
2127 target_column_idx
2128 };
2129
2130 self.add_tile_to_column(
2131 adjusted_target_column_idx,
2132 Some(target_tile_idx),
2133 source_removed.tile,
2134 false,
2135 );
2136
2137 let RemovedTile {
2138 tile: target_tile, ..
2139 } = self.remove_tile_by_idx(
2140 adjusted_target_column_idx,
2141 target_tile_idx + 1,
2142 transaction.clone(),
2143 None,
2144 );
2145
2146 if source_column_drained {
2147 // recreate the drained column with only the target tile
2148 self.add_tile(
2149 Some(source_column_idx),
2150 target_tile,
2151 true,
2152 source_removed.width,
2153 source_removed.is_full_width,
2154 None,
2155 )
2156 } else {
2157 // simply add the removed target tile to the source column
2158 self.add_tile_to_column(
2159 source_column_idx,
2160 Some(source_tile_idx),
2161 target_tile,
2162 false,
2163 );
2164 }
2165 }
2166
2167 // update the active tile in the modified columns
2168 self.columns[source_column_idx].active_tile_idx = source_tile_idx;
2169 self.columns[target_column_idx].active_tile_idx = target_tile_idx;
2170
2171 // Animations
2172 self.columns[target_column_idx].tiles[target_tile_idx]
2173 .animate_move_from(source_pt - target_pt);
2174 self.columns[target_column_idx].tiles[target_tile_idx].ensure_alpha_animates_to_1();
2175
2176 // FIXME: this stop_move_animations() causes the target tile animation to "reset" when
2177 // swapping. It's here as a workaround to stop the unwanted animation of moving the source
2178 // tile down when adding the target tile above it. This code needs to be written in some
2179 // other way not to trigger that animation, or to cancel it properly, so that swap doesn't
2180 // cancel all ongoing target tile animations.
2181 self.columns[source_column_idx].tiles[source_tile_idx].stop_move_animations();
2182 self.columns[source_column_idx].tiles[source_tile_idx]
2183 .animate_move_from(target_pt - source_pt);
2184 self.columns[source_column_idx].tiles[source_tile_idx].ensure_alpha_animates_to_1();
2185
2186 self.activate_column(target_column_idx);
2187 }
2188
2189 pub fn toggle_column_tabbed_display(&mut self) {
2190 if self.columns.is_empty() {
2191 return;
2192 }
2193
2194 let col = &mut self.columns[self.active_column_idx];
2195 let display = match col.display_mode {
2196 ColumnDisplay::Normal => ColumnDisplay::Tabbed,
2197 ColumnDisplay::Tabbed => ColumnDisplay::Normal,
2198 };
2199
2200 self.set_column_display(display);
2201 }
2202
2203 pub fn set_column_display(&mut self, display: ColumnDisplay) {
2204 if self.columns.is_empty() {
2205 return;
2206 }
2207
2208 let col = &mut self.columns[self.active_column_idx];
2209 if col.display_mode == display {
2210 return;
2211 }
2212
2213 cancel_resize_for_column(&mut self.interactive_resize, col);
2214 col.set_column_display(display);
2215
2216 // With place_within_column, the tab indicator changes the column size immediately.
2217 self.data[self.active_column_idx].update(col);
2218 col.update_tile_sizes(true);
2219
2220 // Disable fullscreen if needed.
2221 if col.display_mode != ColumnDisplay::Tabbed && col.tiles.len() > 1 {
2222 let window = col.tiles[col.active_tile_idx].window().id().clone();
2223 self.set_fullscreen(&window, false);
2224 self.set_maximized(&window, false);
2225 }
2226 }
2227
2228 pub fn center_column(&mut self) {
2229 if self.columns.is_empty() {
2230 return;
2231 }
2232
2233 self.animate_view_offset_to_column_centered(
2234 None,
2235 self.active_column_idx,
2236 self.options.animations.horizontal_view_movement.0,
2237 );
2238
2239 let col = &mut self.columns[self.active_column_idx];
2240 cancel_resize_for_column(&mut self.interactive_resize, col);
2241 }
2242
2243 pub fn center_window(&mut self, window: Option<&W::Id>) {
2244 if self.columns.is_empty() {
2245 return;
2246 }
2247
2248 let col_idx = if let Some(window) = window {
2249 self.columns
2250 .iter()
2251 .position(|col| col.contains(window))
2252 .unwrap()
2253 } else {
2254 self.active_column_idx
2255 };
2256
2257 // We can reasonably center only the active column.
2258 if col_idx != self.active_column_idx {
2259 return;
2260 }
2261
2262 self.center_column();
2263 }
2264
2265 pub fn center_visible_columns(&mut self) {
2266 if self.columns.is_empty() {
2267 return;
2268 }
2269
2270 if self.is_centering_focused_column() {
2271 return;
2272 }
2273
2274 // Consider the end of an ongoing animation because that's what compute to fit does too.
2275 let view_x = self.target_view_pos();
2276 let working_x = self.working_area.loc.x;
2277 let working_w = self.working_area.size.w;
2278
2279 // Count all columns that are fully visible inside the working area.
2280 let mut width_taken = 0.;
2281 let mut leftmost_col_x = None;
2282 let mut active_col_x = None;
2283
2284 let gap = self.options.layout.gaps;
2285 let col_xs = self.column_xs(self.data.iter().copied());
2286 for (idx, col_x) in col_xs.take(self.columns.len()).enumerate() {
2287 if col_x < view_x + working_x + gap {
2288 // Column goes off-screen to the left.
2289 continue;
2290 }
2291
2292 leftmost_col_x.get_or_insert(col_x);
2293
2294 let width = self.data[idx].width;
2295 if view_x + working_x + working_w < col_x + width + gap {
2296 // Column goes off-screen to the right. We can stop here.
2297 break;
2298 }
2299
2300 if idx == self.active_column_idx {
2301 active_col_x = Some(col_x);
2302 }
2303
2304 width_taken += width + gap;
2305 }
2306
2307 if active_col_x.is_none() {
2308 // The active column wasn't fully on screen, so we can't meaningfully do anything.
2309 return;
2310 }
2311
2312 let col = &mut self.columns[self.active_column_idx];
2313 cancel_resize_for_column(&mut self.interactive_resize, col);
2314
2315 let free_space = working_w - width_taken + gap;
2316 let new_view_x = leftmost_col_x.unwrap() - free_space / 2. - working_x;
2317
2318 self.animate_view_offset(self.active_column_idx, new_view_x - active_col_x.unwrap());
2319 // Just in case.
2320 self.animate_view_offset_to_column(None, self.active_column_idx, None);
2321 }
2322
2323 pub fn view_pos(&self) -> f64 {
2324 self.column_x(self.active_column_idx) + self.view_offset.current()
2325 }
2326
2327 pub fn target_view_pos(&self) -> f64 {
2328 self.column_x(self.active_column_idx) + self.view_offset.target()
2329 }
2330
2331 // HACK: pass a self.data iterator in manually as a workaround for the lack of method partial
2332 // borrowing. Note that this method's return value does not borrow the entire &Self!
2333 fn column_xs(&self, data: impl Iterator<Item = ColumnData>) -> impl Iterator<Item = f64> {
2334 let gaps = self.options.layout.gaps;
2335 let mut x = 0.;
2336
2337 // Chain with a dummy value to be able to get one past all columns' X.
2338 let dummy = ColumnData { width: 0. };
2339 let data = data.chain(iter::once(dummy));
2340
2341 data.map(move |data| {
2342 let rv = x;
2343 x += data.width + gaps;
2344 rv
2345 })
2346 }
2347
2348 fn column_x(&self, column_idx: usize) -> f64 {
2349 self.column_xs(self.data.iter().copied())
2350 .nth(column_idx)
2351 .unwrap()
2352 }
2353
2354 fn column_xs_in_render_order(
2355 &self,
2356 data: impl Iterator<Item = ColumnData>,
2357 ) -> impl Iterator<Item = f64> {
2358 let active_idx = self.active_column_idx;
2359 let active_pos = self.column_x(active_idx);
2360 let offsets = self
2361 .column_xs(data)
2362 .enumerate()
2363 .filter_map(move |(idx, pos)| (idx != active_idx).then_some(pos));
2364 iter::once(active_pos).chain(offsets)
2365 }
2366
2367 pub fn columns(&self) -> impl Iterator<Item = &Column<W>> {
2368 self.columns.iter()
2369 }
2370
2371 fn columns_mut(&mut self) -> impl Iterator<Item = (&mut Column<W>, f64)> + '_ {
2372 let offsets = self.column_xs(self.data.iter().copied());
2373 zip(&mut self.columns, offsets)
2374 }
2375
2376 fn columns_in_render_order(&self) -> impl Iterator<Item = (&Column<W>, f64)> + '_ {
2377 let offsets = self.column_xs_in_render_order(self.data.iter().copied());
2378
2379 let (first, active, rest) = if self.columns.is_empty() {
2380 (&[][..], &[][..], &[][..])
2381 } else {
2382 let (first, rest) = self.columns.split_at(self.active_column_idx);
2383 let (active, rest) = rest.split_at(1);
2384 (first, active, rest)
2385 };
2386
2387 let columns = active.iter().chain(first).chain(rest);
2388 zip(columns, offsets)
2389 }
2390
2391 fn columns_in_render_order_mut(&mut self) -> impl Iterator<Item = (&mut Column<W>, f64)> + '_ {
2392 let offsets = self.column_xs_in_render_order(self.data.iter().copied());
2393
2394 let (first, active, rest) = if self.columns.is_empty() {
2395 (&mut [][..], &mut [][..], &mut [][..])
2396 } else {
2397 let (first, rest) = self.columns.split_at_mut(self.active_column_idx);
2398 let (active, rest) = rest.split_at_mut(1);
2399 (first, active, rest)
2400 };
2401
2402 let columns = active.iter_mut().chain(first).chain(rest);
2403 zip(columns, offsets)
2404 }
2405
2406 pub fn columns_with_render_positions(
2407 &self,
2408 ) -> impl Iterator<Item = (&Column<W>, Point<f64, Logical>)> {
2409 let view_off = Point::from((-self.view_pos(), 0.));
2410 self.columns_in_render_order().map(move |(col, col_x)| {
2411 let col_off = Point::from((col_x, 0.));
2412 let col_render_off = col.render_offset();
2413 let pos = view_off + col_off + col_render_off;
2414 (col, pos)
2415 })
2416 }
2417
2418 pub fn columns_with_render_positions_mut(
2419 &mut self,
2420 ) -> impl Iterator<Item = (&mut Column<W>, Point<f64, Logical>)> {
2421 let view_off = Point::from((-self.view_pos(), 0.));
2422 self.columns_in_render_order_mut().map(move |(col, col_x)| {
2423 let col_off = Point::from((col_x, 0.));
2424 let col_render_off = col.render_offset();
2425 let pos = view_off + col_off + col_render_off;
2426 (col, pos)
2427 })
2428 }
2429
2430 pub fn tiles_with_render_positions(
2431 &self,
2432 ) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>, bool)> {
2433 let scale = self.scale;
2434 self.columns_with_render_positions()
2435 .flat_map(move |(col, col_pos)| {
2436 col.tiles_in_render_order()
2437 .map(move |(tile, tile_off, visible)| {
2438 let pos = col_pos + tile_off + tile.render_offset();
2439 // Round to physical pixels.
2440 let pos = pos.to_physical_precise_round(scale).to_logical(scale);
2441 (tile, pos, visible)
2442 })
2443 })
2444 }
2445
2446 pub fn tiles_with_render_positions_mut(
2447 &mut self,
2448 round: bool,
2449 ) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> {
2450 let scale = self.scale;
2451 self.columns_with_render_positions_mut()
2452 .flat_map(move |(col, col_pos)| {
2453 col.tiles_in_render_order_mut()
2454 .map(move |(tile, tile_off)| {
2455 let mut pos = col_pos + tile_off + tile.render_offset();
2456 // Round to physical pixels.
2457 if round {
2458 pos = pos.to_physical_precise_round(scale).to_logical(scale);
2459 }
2460 (tile, pos)
2461 })
2462 })
2463 }
2464
2465 pub fn tiles_with_ipc_layouts(&self) -> impl Iterator<Item = (&Tile<W>, WindowLayout)> {
2466 self.columns
2467 .iter()
2468 .enumerate()
2469 .flat_map(move |(col_idx, col)| {
2470 col.tiles().enumerate().map(move |(tile_idx, (tile, _))| {
2471 let layout = WindowLayout {
2472 // Our indices are 1-based, consistent with the actions.
2473 pos_in_scrolling_layout: Some((col_idx + 1, tile_idx + 1)),
2474 ..tile.ipc_layout_template()
2475 };
2476 (tile, layout)
2477 })
2478 })
2479 }
2480
2481 pub(super) fn insert_hint_area(
2482 &self,
2483 position: InsertPosition,
2484 ) -> Option<Rectangle<f64, Logical>> {
2485 let mut hint_area = match position {
2486 InsertPosition::NewColumn(column_index) => {
2487 if column_index == 0 || column_index == self.columns.len() {
2488 let size = Size::from((
2489 300.,
2490 self.working_area.size.h - self.options.layout.gaps * 2.,
2491 ));
2492 let mut loc = Point::from((
2493 self.column_x(column_index),
2494 self.working_area.loc.y + self.options.layout.gaps,
2495 ));
2496 if column_index == 0 && !self.columns.is_empty() {
2497 loc.x -= size.w + self.options.layout.gaps;
2498 }
2499 Rectangle::new(loc, size)
2500 } else if column_index > self.columns.len() {
2501 error!("insert hint column index is out of range");
2502 return None;
2503 } else {
2504 let size = Size::from((
2505 300.,
2506 self.working_area.size.h - self.options.layout.gaps * 2.,
2507 ));
2508 let loc = Point::from((
2509 self.column_x(column_index) - size.w / 2. - self.options.layout.gaps / 2.,
2510 self.working_area.loc.y + self.options.layout.gaps,
2511 ));
2512 Rectangle::new(loc, size)
2513 }
2514 }
2515 InsertPosition::InColumn(column_index, tile_index) => {
2516 if column_index > self.columns.len() {
2517 error!("insert hint column index is out of range");
2518 return None;
2519 }
2520
2521 let col = &self.columns[column_index];
2522 if tile_index > col.tiles.len() {
2523 error!("insert hint tile index is out of range");
2524 return None;
2525 }
2526
2527 let is_tabbed = col.display_mode == ColumnDisplay::Tabbed;
2528
2529 let (height, y) = if is_tabbed {
2530 // In tabbed mode, there's only one tile visible, and we want to draw the hint
2531 // at its top or bottom.
2532 let top = col.tile_offset(col.active_tile_idx).y;
2533 let bottom = top + col.data[col.active_tile_idx].size.h;
2534
2535 if tile_index <= col.active_tile_idx {
2536 (150., top)
2537 } else {
2538 (150., bottom - 150.)
2539 }
2540 } else {
2541 let top = col.tile_offset(tile_index).y;
2542
2543 if tile_index == 0 {
2544 (150., top)
2545 } else if tile_index == col.tiles.len() {
2546 (150., top - self.options.layout.gaps - 150.)
2547 } else {
2548 (300., top - self.options.layout.gaps / 2. - 150.)
2549 }
2550 };
2551
2552 // Adjust for place-within-column tab indicator.
2553 let origin_x = col.tiles_origin().x;
2554 let extra_w = if is_tabbed && col.sizing_mode().is_normal() {
2555 col.tab_indicator.extra_size(col.tiles.len(), col.scale).w
2556 } else {
2557 0.
2558 };
2559
2560 let size = Size::from((self.data[column_index].width - extra_w, height));
2561 let loc = Point::from((self.column_x(column_index) + origin_x, y));
2562 Rectangle::new(loc, size)
2563 }
2564 InsertPosition::Floating => return None,
2565 };
2566
2567 // First window on an empty workspace will cancel out any view offset. Replicate this
2568 // effect here.
2569 if self.columns.is_empty() {
2570 let view_offset = if self.is_centering_focused_column() {
2571 self.compute_new_view_offset_centered(
2572 Some(0.),
2573 0.,
2574 hint_area.size.w,
2575 SizingMode::Normal,
2576 )
2577 } else {
2578 self.compute_new_view_offset_fit(Some(0.), 0., hint_area.size.w, SizingMode::Normal)
2579 };
2580 hint_area.loc.x -= view_offset;
2581 } else {
2582 hint_area.loc.x -= self.view_pos();
2583 }
2584
2585 Some(hint_area)
2586 }
2587
2588 /// Returns the geometry of the active window relative to and clamped to the view.
2589 ///
2590 /// During animations, assumes the final view position.
2591 pub fn active_window_visual_rectangle(&self) -> Option<Rectangle<f64, Logical>> {
2592 let col = self.columns.get(self.active_column_idx)?;
2593
2594 let final_view_offset = self.view_offset.target();
2595 let view_off = Point::from((-final_view_offset, 0.));
2596
2597 let (tile, tile_off) = col.tiles().nth(col.active_tile_idx).unwrap();
2598
2599 let window_pos = view_off + tile_off + tile.window_loc();
2600 let window_size = tile.window_size();
2601 let window_rect = Rectangle::new(window_pos, window_size);
2602
2603 let view = Rectangle::from_size(self.view_size);
2604 view.intersection(window_rect)
2605 }
2606
2607 pub fn popup_target_rect(&self, id: &W::Id) -> Option<Rectangle<f64, Logical>> {
2608 for col in &self.columns {
2609 for (tile, pos) in col.tiles() {
2610 if tile.window().id() == id {
2611 // In the scrolling layout, we try to position popups horizontally within the
2612 // window geometry (so they remain visible even if the window scrolls flush with
2613 // the left/right edge of the screen), and vertically within the whole parent
2614 // working area.
2615 let width = tile.window_size().w;
2616 let height = self.parent_area.size.h;
2617
2618 let mut target = Rectangle::from_size(Size::from((width, height)));
2619 target.loc.y += self.parent_area.loc.y;
2620 target.loc.y -= pos.y;
2621 target.loc.y -= tile.window_loc().y;
2622
2623 return Some(target);
2624 }
2625 }
2626 }
2627 None
2628 }
2629
2630 pub fn toggle_width(&mut self, forwards: bool) {
2631 if self.columns.is_empty() {
2632 return;
2633 }
2634
2635 let col = &mut self.columns[self.active_column_idx];
2636 col.toggle_width(None, forwards);
2637
2638 cancel_resize_for_column(&mut self.interactive_resize, col);
2639 }
2640
2641 pub fn toggle_full_width(&mut self) {
2642 if self.columns.is_empty() {
2643 return;
2644 }
2645
2646 let col = &mut self.columns[self.active_column_idx];
2647 col.toggle_full_width();
2648
2649 cancel_resize_for_column(&mut self.interactive_resize, col);
2650 }
2651
2652 pub fn set_window_width(&mut self, window: Option<&W::Id>, change: SizeChange) {
2653 if self.columns.is_empty() {
2654 return;
2655 }
2656
2657 let (col, tile_idx) = if let Some(window) = window {
2658 self.columns
2659 .iter_mut()
2660 .find_map(|col| {
2661 col.tiles
2662 .iter()
2663 .position(|tile| tile.window().id() == window)
2664 .map(|tile_idx| (col, Some(tile_idx)))
2665 })
2666 .unwrap()
2667 } else {
2668 (&mut self.columns[self.active_column_idx], None)
2669 };
2670
2671 col.set_column_width(change, tile_idx, true);
2672
2673 cancel_resize_for_column(&mut self.interactive_resize, col);
2674 }
2675
2676 pub fn set_window_height(&mut self, window: Option<&W::Id>, change: SizeChange) {
2677 if self.columns.is_empty() {
2678 return;
2679 }
2680
2681 let (col, tile_idx) = if let Some(window) = window {
2682 self.columns
2683 .iter_mut()
2684 .find_map(|col| {
2685 col.tiles
2686 .iter()
2687 .position(|tile| tile.window().id() == window)
2688 .map(|tile_idx| (col, Some(tile_idx)))
2689 })
2690 .unwrap()
2691 } else {
2692 (&mut self.columns[self.active_column_idx], None)
2693 };
2694
2695 col.set_window_height(change, tile_idx, true);
2696
2697 cancel_resize_for_column(&mut self.interactive_resize, col);
2698 }
2699
2700 pub fn reset_window_height(&mut self, window: Option<&W::Id>) {
2701 if self.columns.is_empty() {
2702 return;
2703 }
2704
2705 let (col, tile_idx) = if let Some(window) = window {
2706 self.columns
2707 .iter_mut()
2708 .find_map(|col| {
2709 col.tiles
2710 .iter()
2711 .position(|tile| tile.window().id() == window)
2712 .map(|tile_idx| (col, Some(tile_idx)))
2713 })
2714 .unwrap()
2715 } else {
2716 (&mut self.columns[self.active_column_idx], None)
2717 };
2718
2719 col.reset_window_height(tile_idx);
2720
2721 cancel_resize_for_column(&mut self.interactive_resize, col);
2722 }
2723
2724 pub fn toggle_window_width(&mut self, window: Option<&W::Id>, forwards: bool) {
2725 if self.columns.is_empty() {
2726 return;
2727 }
2728
2729 let (col, tile_idx) = if let Some(window) = window {
2730 self.columns
2731 .iter_mut()
2732 .find_map(|col| {
2733 col.tiles
2734 .iter()
2735 .position(|tile| tile.window().id() == window)
2736 .map(|tile_idx| (col, Some(tile_idx)))
2737 })
2738 .unwrap()
2739 } else {
2740 (&mut self.columns[self.active_column_idx], None)
2741 };
2742
2743 col.toggle_width(tile_idx, forwards);
2744
2745 cancel_resize_for_column(&mut self.interactive_resize, col);
2746 }
2747
2748 pub fn toggle_window_height(&mut self, window: Option<&W::Id>, forwards: bool) {
2749 if self.columns.is_empty() {
2750 return;
2751 }
2752
2753 let (col, tile_idx) = if let Some(window) = window {
2754 self.columns
2755 .iter_mut()
2756 .find_map(|col| {
2757 col.tiles
2758 .iter()
2759 .position(|tile| tile.window().id() == window)
2760 .map(|tile_idx| (col, Some(tile_idx)))
2761 })
2762 .unwrap()
2763 } else {
2764 (&mut self.columns[self.active_column_idx], None)
2765 };
2766
2767 col.toggle_window_height(tile_idx, forwards);
2768
2769 cancel_resize_for_column(&mut self.interactive_resize, col);
2770 }
2771
2772 pub fn expand_column_to_available_width(&mut self) {
2773 if self.columns.is_empty() {
2774 return;
2775 }
2776
2777 let col = &mut self.columns[self.active_column_idx];
2778 if !col.pending_sizing_mode().is_normal() || col.is_full_width {
2779 return;
2780 }
2781
2782 if self.is_centering_focused_column() {
2783 // Always-centered mode is different since the active window position cannot be
2784 // controlled (it's always at the center). I guess you could come up with different
2785 // logic here that computes the width in such a way so as to leave nearby columns fully
2786 // on screen while taking into account that the active column will remain centered
2787 // after resizing. But I'm not sure it's that useful? So let's do the simple thing.
2788 let col = &mut self.columns[self.active_column_idx];
2789 col.toggle_full_width();
2790 cancel_resize_for_column(&mut self.interactive_resize, col);
2791 return;
2792 }
2793
2794 // NOTE: This logic won't work entirely correctly with small fixed-size maximized windows
2795 // (they have a different area and padding).
2796
2797 // Consider the end of an ongoing animation because that's what compute to fit does too.
2798 let view_x = self.target_view_pos();
2799 let working_x = self.working_area.loc.x;
2800 let working_w = self.working_area.size.w;
2801
2802 // Count all columns that are fully visible inside the working area.
2803 let mut width_taken = 0.;
2804 let mut leftmost_col_x = None;
2805 let mut active_col_x = None;
2806 let mut counted_non_active_column = false;
2807
2808 let gap = self.options.layout.gaps;
2809 let col_xs = self.column_xs(self.data.iter().copied());
2810 for (idx, col_x) in col_xs.take(self.columns.len()).enumerate() {
2811 if col_x < view_x + working_x + gap {
2812 // Column goes off-screen to the left.
2813 continue;
2814 }
2815
2816 leftmost_col_x.get_or_insert(col_x);
2817
2818 let width = self.data[idx].width;
2819 if view_x + working_x + working_w < col_x + width + gap {
2820 // Column goes off-screen to the right. We can stop here.
2821 break;
2822 }
2823
2824 if idx == self.active_column_idx {
2825 active_col_x = Some(col_x);
2826 } else {
2827 counted_non_active_column = true;
2828 }
2829
2830 width_taken += width + gap;
2831 }
2832
2833 if active_col_x.is_none() {
2834 // The active column wasn't fully on screen, so we can't meaningfully do anything.
2835 return;
2836 }
2837
2838 let col = &mut self.columns[self.active_column_idx];
2839
2840 let available_width = working_w - gap - width_taken - col.extra_size().w;
2841 if available_width <= 0. {
2842 // Nowhere to expand.
2843 return;
2844 }
2845
2846 cancel_resize_for_column(&mut self.interactive_resize, col);
2847
2848 if !counted_non_active_column {
2849 // Only the active column was fully on-screen (maybe it's the only column), so we're
2850 // about to set its width to 100% of the working area. Let's do it via
2851 // toggle_full_width() as it lets you back out of it more intuitively.
2852 col.toggle_full_width();
2853 return;
2854 }
2855
2856 let active_width = self.data[self.active_column_idx].width;
2857 col.width = ColumnWidth::Fixed(active_width + available_width);
2858 col.preset_width_idx = None;
2859 col.is_full_width = false;
2860 col.update_tile_sizes(true);
2861
2862 // Put the leftmost window into the view.
2863 let new_view_x = leftmost_col_x.unwrap() - gap - working_x;
2864 self.animate_view_offset(self.active_column_idx, new_view_x - active_col_x.unwrap());
2865 // Just in case.
2866 self.animate_view_offset_to_column(None, self.active_column_idx, None);
2867 }
2868
2869 pub fn set_fullscreen(&mut self, window: &W::Id, is_fullscreen: bool) -> bool {
2870 let mut col_idx = self
2871 .columns
2872 .iter()
2873 .position(|col| col.contains(window))
2874 .unwrap();
2875
2876 if is_fullscreen == self.columns[col_idx].is_pending_fullscreen {
2877 return false;
2878 }
2879
2880 let mut col = &mut self.columns[col_idx];
2881 let is_tabbed = col.display_mode == ColumnDisplay::Tabbed;
2882
2883 cancel_resize_for_column(&mut self.interactive_resize, col);
2884
2885 if is_fullscreen && (col.tiles.len() > 1 && !is_tabbed) {
2886 // This wasn't the only window in its column; extract it into a separate column.
2887 self.consume_or_expel_window_right(Some(window));
2888 col_idx += 1;
2889 col = &mut self.columns[col_idx];
2890 }
2891
2892 col.set_fullscreen(is_fullscreen);
2893
2894 // With place_within_column, the tab indicator changes the column size immediately.
2895 self.data[col_idx].update(col);
2896
2897 true
2898 }
2899
2900 pub fn set_maximized(&mut self, window: &W::Id, maximize: bool) -> bool {
2901 let mut col_idx = self
2902 .columns
2903 .iter()
2904 .position(|col| col.contains(window))
2905 .unwrap();
2906
2907 if maximize == self.columns[col_idx].is_pending_maximized {
2908 return false;
2909 }
2910
2911 let mut col = &mut self.columns[col_idx];
2912 let is_tabbed = col.display_mode == ColumnDisplay::Tabbed;
2913
2914 cancel_resize_for_column(&mut self.interactive_resize, col);
2915
2916 if maximize && (col.tiles.len() > 1 && !is_tabbed) {
2917 // This wasn't the only window in its column; extract it into a separate column.
2918 self.consume_or_expel_window_right(Some(window));
2919 col_idx += 1;
2920 col = &mut self.columns[col_idx];
2921 }
2922
2923 col.set_maximized(maximize);
2924
2925 // With place_within_column, the tab indicator changes the column size immediately.
2926 self.data[col_idx].update(col);
2927
2928 true
2929 }
2930
2931 pub fn render_above_top_layer(&self) -> bool {
2932 // Render above the top layer if we're on a fullscreen window and the view is stationary.
2933 if self.columns.is_empty() {
2934 return false;
2935 }
2936
2937 if !self.view_offset.is_static() {
2938 return false;
2939 }
2940
2941 self.columns[self.active_column_idx]
2942 .sizing_mode()
2943 .is_fullscreen()
2944 }
2945
2946 pub fn render<R: NiriRenderer>(
2947 &self,
2948 mut ctx: RenderCtx<R>,
2949 xray_pos: XrayPos,
2950 focus_ring: bool,
2951 layer: RenderLayer,
2952 push: &mut dyn FnMut(ScrollingSpaceRenderElement<R>),
2953 ) {
2954 let scale = Scale::from(self.scale);
2955
2956 // Draw the closing windows on top of the other windows.
2957 if layer.is_normal() {
2958 let view_rect = Rectangle::new(Point::from((self.view_pos(), 0.)), self.view_size);
2959 for closing in self.closing_windows.iter().rev() {
2960 let elem = closing.render(ctx.as_gles(), view_rect, scale);
2961 push(elem.into());
2962 }
2963 }
2964
2965 if self.columns.is_empty() {
2966 return;
2967 }
2968
2969 let mut first = true;
2970
2971 // This matches self.tiles_with_render_positions().
2972 for (col, col_pos) in self.columns_with_render_positions() {
2973 // Skip columns belonging to a different render layer.
2974 if layer.is_normal() == col.is_moving_between_workspaces() {
2975 first = false;
2976 continue;
2977 }
2978
2979 // Draw the tab indicator on top.
2980 {
2981 let pos = col_pos.to_physical_precise_round(scale).to_logical(scale);
2982 col.tab_indicator
2983 .render(ctx.renderer, pos, &mut |elem| push(elem.into()));
2984 }
2985
2986 for (tile, tile_off, visible) in col.tiles_in_render_order() {
2987 let tile_pos = col_pos + tile_off + tile.render_offset();
2988 // Round to physical pixels.
2989 let tile_pos = tile_pos.to_physical_precise_round(scale).to_logical(scale);
2990
2991 // And now the drawing logic.
2992
2993 // For the active tile (which comes first), draw the focus ring.
2994 let focus_ring = focus_ring && first;
2995 first = false;
2996
2997 // In the scrolling layout, we currently use visible only for hidden tabs in the
2998 // tabbed mode. We want to animate their opacity when going in and out of tabbed
2999 // mode, so we don't want to apply "visible" immediately. However, "visible" is
3000 // also used for input handling, and there we *do* want to apply it immediately.
3001 // So, let's just selectively ignore "visible" here when animating alpha.
3002 let visible = visible || tile.alpha_animation.is_some();
3003 if !visible {
3004 continue;
3005 }
3006
3007 let xray_pos = xray_pos.offset(tile_pos);
3008 tile.render(ctx.r(), tile_pos, xray_pos, focus_ring, &mut |elem| {
3009 push(elem.into())
3010 });
3011 }
3012 }
3013 }
3014
3015 pub fn window_under(&self, pos: Point<f64, Logical>) -> Option<(&W, HitType)> {
3016 // This matches self.tiles_with_render_positions().
3017 let scale = self.scale;
3018 for (col, col_pos) in self.columns_with_render_positions() {
3019 // Hit the tab indicator.
3020 if col.display_mode == ColumnDisplay::Tabbed && col.sizing_mode().is_normal() {
3021 let col_pos = col_pos.to_physical_precise_round(scale).to_logical(scale);
3022
3023 if let Some(idx) = col.tab_indicator.hit(
3024 col.tab_indicator_area(),
3025 col.tiles.len(),
3026 scale,
3027 pos - col_pos,
3028 ) {
3029 let hit = HitType::Activate {
3030 is_tab_indicator: true,
3031 };
3032 return Some((col.tiles[idx].window(), hit));
3033 }
3034 }
3035
3036 for (tile, tile_off, visible) in col.tiles_in_render_order() {
3037 if !visible {
3038 continue;
3039 }
3040
3041 let tile_pos = col_pos + tile_off + tile.render_offset();
3042 // Round to physical pixels.
3043 let tile_pos = tile_pos.to_physical_precise_round(scale).to_logical(scale);
3044
3045 if let Some(rv) = HitType::hit_tile(tile, tile_pos, pos) {
3046 return Some(rv);
3047 }
3048 }
3049 }
3050
3051 None
3052 }
3053
3054 pub fn view_offset_gesture_begin(&mut self, is_touchpad: bool) {
3055 if self.columns.is_empty() {
3056 return;
3057 }
3058
3059 if self.interactive_resize.is_some() {
3060 return;
3061 }
3062
3063 let gesture = ViewGesture {
3064 current_view_offset: self.view_offset.current(),
3065 animation: None,
3066 tracker: SwipeTracker::new(),
3067 delta_from_tracker: self.view_offset.current(),
3068 stationary_view_offset: self.view_offset.stationary(),
3069 is_touchpad,
3070 dnd_last_event_time: None,
3071 dnd_nonzero_start_time: None,
3072 };
3073 self.view_offset = ViewOffset::Gesture(gesture);
3074 }
3075
3076 pub fn dnd_scroll_gesture_begin(&mut self) {
3077 if let ViewOffset::Gesture(ViewGesture {
3078 dnd_last_event_time: Some(_),
3079 ..
3080 }) = &self.view_offset
3081 {
3082 // Already active.
3083 return;
3084 }
3085
3086 let gesture = ViewGesture {
3087 current_view_offset: self.view_offset.current(),
3088 animation: None,
3089 tracker: SwipeTracker::new(),
3090 delta_from_tracker: self.view_offset.current(),
3091 stationary_view_offset: self.view_offset.stationary(),
3092 is_touchpad: false,
3093 dnd_last_event_time: Some(self.clock.now_unadjusted()),
3094 dnd_nonzero_start_time: None,
3095 };
3096 self.view_offset = ViewOffset::Gesture(gesture);
3097
3098 self.interactive_resize = None;
3099 }
3100
3101 pub fn view_offset_gesture_update(
3102 &mut self,
3103 delta_x: f64,
3104 timestamp: Duration,
3105 is_touchpad: bool,
3106 ) -> Option<bool> {
3107 let ViewOffset::Gesture(gesture) = &mut self.view_offset else {
3108 return None;
3109 };
3110
3111 if gesture.is_touchpad != is_touchpad || gesture.dnd_last_event_time.is_some() {
3112 return None;
3113 }
3114
3115 gesture.tracker.push(delta_x, timestamp);
3116
3117 let norm_factor = if gesture.is_touchpad {
3118 self.working_area.size.w / VIEW_GESTURE_WORKING_AREA_MOVEMENT
3119 } else {
3120 1.
3121 };
3122 let pos = gesture.tracker.pos() * norm_factor;
3123 let view_offset = pos + gesture.delta_from_tracker;
3124 gesture.current_view_offset = view_offset;
3125
3126 Some(true)
3127 }
3128
3129 pub fn dnd_scroll_gesture_scroll(&mut self, delta: f64) -> bool {
3130 let ViewOffset::Gesture(gesture) = &mut self.view_offset else {
3131 return false;
3132 };
3133
3134 let Some(last_time) = gesture.dnd_last_event_time else {
3135 // Not a DnD scroll.
3136 return false;
3137 };
3138
3139 let config = &self.options.gestures.dnd_edge_view_scroll;
3140
3141 let now = self.clock.now_unadjusted();
3142 gesture.dnd_last_event_time = Some(now);
3143
3144 if delta == 0. {
3145 // We're outside the scrolling zone.
3146 gesture.dnd_nonzero_start_time = None;
3147 return false;
3148 }
3149
3150 let nonzero_start = *gesture.dnd_nonzero_start_time.get_or_insert(now);
3151
3152 // Delay starting the gesture a bit to avoid unwanted movement when dragging across
3153 // monitors.
3154 let delay = Duration::from_millis(u64::from(config.delay_ms));
3155 if now.saturating_sub(nonzero_start) < delay {
3156 return true;
3157 }
3158
3159 let time_delta = now.saturating_sub(last_time).as_secs_f64();
3160
3161 let delta = delta * time_delta * config.max_speed;
3162
3163 gesture.tracker.push(delta, now);
3164
3165 let view_offset = gesture.tracker.pos() + gesture.delta_from_tracker;
3166
3167 // Clamp it so that it doesn't go too much out of bounds.
3168 let (leftmost, rightmost) = if self.columns.is_empty() {
3169 (0., 0.)
3170 } else {
3171 let gaps = self.options.layout.gaps;
3172
3173 let mut leftmost = -self.working_area.size.w;
3174
3175 let last_col_idx = self.columns.len() - 1;
3176 let last_col_x = self
3177 .columns
3178 .iter()
3179 .take(last_col_idx)
3180 .fold(0., |col_x, col| col_x + col.width() + gaps);
3181 let last_col_width = self.data[last_col_idx].width;
3182 let mut rightmost = last_col_x + last_col_width - self.working_area.loc.x;
3183
3184 let active_col_x = self
3185 .columns
3186 .iter()
3187 .take(self.active_column_idx)
3188 .fold(0., |col_x, col| col_x + col.width() + gaps);
3189 leftmost -= active_col_x;
3190 rightmost -= active_col_x;
3191
3192 (leftmost, rightmost)
3193 };
3194 let min_offset = f64::min(leftmost, rightmost);
3195 let max_offset = f64::max(leftmost, rightmost);
3196 let clamped_offset = view_offset.clamp(min_offset, max_offset);
3197
3198 gesture.delta_from_tracker += clamped_offset - view_offset;
3199 gesture.current_view_offset = clamped_offset;
3200 true
3201 }
3202
3203 pub fn view_offset_gesture_end(&mut self, is_touchpad: Option<bool>) -> bool {
3204 let ViewOffset::Gesture(gesture) = &mut self.view_offset else {
3205 return false;
3206 };
3207
3208 if is_touchpad.is_some_and(|x| gesture.is_touchpad != x) {
3209 return false;
3210 }
3211
3212 // We do not handle cancelling, just like GNOME Shell doesn't. For this gesture, proper
3213 // cancelling would require keeping track of the original active column, and then updating
3214 // it in all the right places (adding columns, removing columns, etc.) -- quite a bit of
3215 // effort and bug potential.
3216
3217 // Take into account any idle time between the last event and now.
3218 let now = self.clock.now_unadjusted();
3219 gesture.tracker.push(0., now);
3220
3221 let norm_factor = if gesture.is_touchpad {
3222 self.working_area.size.w / VIEW_GESTURE_WORKING_AREA_MOVEMENT
3223 } else {
3224 1.
3225 };
3226 let velocity = gesture.tracker.velocity() * norm_factor;
3227 let pos = gesture.tracker.pos() * norm_factor;
3228 let current_view_offset = pos + gesture.delta_from_tracker;
3229
3230 if self.columns.is_empty() {
3231 self.view_offset = ViewOffset::Static(current_view_offset);
3232 return true;
3233 }
3234
3235 // Figure out where the gesture would stop after deceleration.
3236 let end_pos = gesture.tracker.projected_end_pos() * norm_factor;
3237 let target_view_offset = end_pos + gesture.delta_from_tracker;
3238
3239 // Compute the snapping points. These are where the view aligns with column boundaries on
3240 // either side.
3241 struct Snap {
3242 // View position relative to x = 0 (the first column).
3243 view_pos: f64,
3244 // Column to activate for this snapping point.
3245 col_idx: usize,
3246 }
3247
3248 let mut snapping_points = Vec::new();
3249
3250 if self.is_centering_focused_column() {
3251 let mut col_x = 0.;
3252 for (col_idx, col) in self.columns.iter().enumerate() {
3253 let col_w = col.width();
3254 let mode = col.sizing_mode();
3255
3256 let area = if mode.is_maximized() {
3257 self.parent_area
3258 } else {
3259 self.working_area
3260 };
3261
3262 let left_strut = area.loc.x;
3263
3264 let view_pos = if mode.is_fullscreen() {
3265 col_x
3266 } else if area.size.w <= col_w {
3267 col_x - left_strut
3268 } else {
3269 col_x - (area.size.w - col_w) / 2. - left_strut
3270 };
3271 snapping_points.push(Snap { view_pos, col_idx });
3272
3273 col_x += col_w + self.options.layout.gaps;
3274 }
3275 } else {
3276 let center_on_overflow = matches!(
3277 self.options.layout.center_focused_column,
3278 CenterFocusedColumn::OnOverflow
3279 );
3280
3281 let view_width = self.view_size.w;
3282 let gaps = self.options.layout.gaps;
3283
3284 let snap_points =
3285 |col_x, col: &Column<W>, prev_col_w: Option<f64>, next_col_w: Option<f64>| {
3286 let col_w = col.width();
3287 let mode = col.sizing_mode();
3288
3289 let area = if mode.is_maximized() {
3290 self.parent_area
3291 } else {
3292 self.working_area
3293 };
3294
3295 let left_strut = area.loc.x;
3296 let right_strut = self.view_size.w - area.size.w - area.loc.x;
3297
3298 // Normal columns align with the working area, but fullscreen columns align with
3299 // the view size.
3300 if mode.is_fullscreen() {
3301 let left = col_x;
3302 let right = left + col_w;
3303 (left, right)
3304 } else {
3305 // Logic from compute_new_view_offset.
3306 let padding = if mode.is_maximized() {
3307 0.
3308 } else {
3309 ((area.size.w - col_w) / 2.).clamp(0., gaps)
3310 };
3311
3312 let center = if area.size.w <= col_w {
3313 col_x - left_strut
3314 } else {
3315 col_x - (area.size.w - col_w) / 2. - left_strut
3316 };
3317 let is_overflowing = |adj_col_w: Option<f64>| {
3318 center_on_overflow
3319 && adj_col_w
3320 .filter(|adj_col_w| {
3321 // NOTE: This logic won't work entirely correctly with small
3322 // fixed-size maximized windows (they have a different area
3323 // and padding).
3324 center_on_overflow
3325 && adj_col_w + 3.0 * gaps + col_w > area.size.w
3326 })
3327 .is_some()
3328 };
3329
3330 let left = if is_overflowing(next_col_w) {
3331 center
3332 } else {
3333 col_x - padding - left_strut
3334 };
3335 let right = if is_overflowing(prev_col_w) {
3336 center + view_width
3337 } else {
3338 col_x + col_w + padding + right_strut
3339 };
3340 (left, right)
3341 }
3342 };
3343
3344 // Prevent the gesture from snapping further than the first/last column, as this is
3345 // generally undesired.
3346 //
3347 // It's ok if leftmost_snap is > rightmost_snap (this happens if the columns on a
3348 // workspace total up to less than the workspace width).
3349
3350 // The first column's left snap isn't actually guaranteed to be the *leftmost* snap.
3351 // With weird enough left strut and perhaps a maximized small fixed-size window, you
3352 // can make the second window's left snap be further to the left than the first
3353 // window's. The same goes for the rightmost snap.
3354 //
3355 // This isn't actually a big problem because it's very much an obscure edge case. Just
3356 // need to make sure the code doesn't panic when that happens.
3357 let leftmost_snap = snap_points(
3358 0.,
3359 &self.columns[0],
3360 None,
3361 self.columns.get(1).map(|c| c.width()),
3362 )
3363 .0;
3364 let last_col_idx = self.columns.len() - 1;
3365 let last_col_x = self
3366 .columns
3367 .iter()
3368 .take(last_col_idx)
3369 .fold(0., |col_x, col| col_x + col.width() + gaps);
3370 let rightmost_snap = snap_points(
3371 last_col_x,
3372 &self.columns[last_col_idx],
3373 last_col_idx
3374 .checked_sub(1)
3375 .and_then(|idx| self.columns.get(idx).map(|c| c.width())),
3376 None,
3377 )
3378 .1 - view_width;
3379
3380 snapping_points.push(Snap {
3381 view_pos: leftmost_snap,
3382 col_idx: 0,
3383 });
3384 snapping_points.push(Snap {
3385 view_pos: rightmost_snap,
3386 col_idx: last_col_idx,
3387 });
3388
3389 let mut push = |col_idx, left, right| {
3390 if leftmost_snap < left && left < rightmost_snap {
3391 snapping_points.push(Snap {
3392 view_pos: left,
3393 col_idx,
3394 });
3395 }
3396
3397 let right = right - view_width;
3398 if leftmost_snap < right && right < rightmost_snap {
3399 snapping_points.push(Snap {
3400 view_pos: right,
3401 col_idx,
3402 });
3403 }
3404 };
3405
3406 let mut col_x = 0.;
3407 for (col_idx, col) in self.columns.iter().enumerate() {
3408 let (left, right) = snap_points(
3409 col_x,
3410 col,
3411 col_idx
3412 .checked_sub(1)
3413 .and_then(|idx| self.columns.get(idx).map(|c| c.width())),
3414 self.columns.get(col_idx + 1).map(|c| c.width()),
3415 );
3416 push(col_idx, left, right);
3417
3418 col_x += col.width() + gaps;
3419 }
3420 }
3421
3422 // Find the closest snapping point.
3423 snapping_points.sort_by_key(|snap| NotNan::new(snap.view_pos).unwrap());
3424
3425 let active_col_x = self.column_x(self.active_column_idx);
3426 let target_view_pos = active_col_x + target_view_offset;
3427 let target_snap = snapping_points
3428 .iter()
3429 .min_by_key(|snap| NotNan::new((snap.view_pos - target_view_pos).abs()).unwrap())
3430 .unwrap();
3431
3432 let mut new_col_idx = target_snap.col_idx;
3433
3434 if !self.is_centering_focused_column() {
3435 // Focus the furthest window towards the direction of the gesture.
3436 if target_view_offset >= current_view_offset {
3437 for col_idx in (new_col_idx + 1)..self.columns.len() {
3438 let col = &self.columns[col_idx];
3439 let col_x = self.column_x(col_idx);
3440 let col_w = col.width();
3441 let mode = col.sizing_mode();
3442
3443 let area = if mode.is_maximized() {
3444 self.parent_area
3445 } else {
3446 self.working_area
3447 };
3448
3449 let left_strut = area.loc.x;
3450
3451 if mode.is_fullscreen() {
3452 if target_snap.view_pos + self.view_size.w < col_x + col_w {
3453 break;
3454 }
3455 } else {
3456 let padding = if mode.is_maximized() {
3457 0.
3458 } else {
3459 ((area.size.w - col_w) / 2.).clamp(0., self.options.layout.gaps)
3460 };
3461
3462 if target_snap.view_pos + left_strut + area.size.w < col_x + col_w + padding
3463 {
3464 break;
3465 }
3466 }
3467
3468 new_col_idx = col_idx;
3469 }
3470 } else {
3471 for col_idx in (0..new_col_idx).rev() {
3472 let col = &self.columns[col_idx];
3473 let col_x = self.column_x(col_idx);
3474 let col_w = col.width();
3475 let mode = col.sizing_mode();
3476
3477 let area = if mode.is_maximized() {
3478 self.parent_area
3479 } else {
3480 self.working_area
3481 };
3482
3483 let left_strut = area.loc.x;
3484
3485 if mode.is_fullscreen() {
3486 if col_x < target_snap.view_pos {
3487 break;
3488 }
3489 } else {
3490 let padding = if mode.is_maximized() {
3491 0.
3492 } else {
3493 ((area.size.w - col_w) / 2.).clamp(0., self.options.layout.gaps)
3494 };
3495
3496 if col_x - padding < target_snap.view_pos + left_strut {
3497 break;
3498 }
3499 }
3500
3501 new_col_idx = col_idx;
3502 }
3503 }
3504 }
3505
3506 let new_col_x = self.column_x(new_col_idx);
3507 let delta = active_col_x - new_col_x;
3508
3509 if self.active_column_idx != new_col_idx {
3510 self.view_offset_to_restore = None;
3511 }
3512
3513 self.active_column_idx = new_col_idx;
3514
3515 let target_view_offset = target_snap.view_pos - new_col_x;
3516
3517 self.view_offset = ViewOffset::Animation(Animation::new(
3518 self.clock.clone(),
3519 current_view_offset + delta,
3520 target_view_offset,
3521 velocity,
3522 self.options.animations.horizontal_view_movement.0,
3523 ));
3524
3525 // HACK: deal with things like snapping to the right edge of a larger-than-view window.
3526 self.animate_view_offset_to_column(None, new_col_idx, None);
3527
3528 true
3529 }
3530
3531 pub fn dnd_scroll_gesture_end(&mut self) {
3532 let ViewOffset::Gesture(gesture) = &mut self.view_offset else {
3533 return;
3534 };
3535
3536 if gesture.dnd_last_event_time.is_some() && gesture.tracker.pos() == 0. {
3537 // DnD didn't scroll anything, so preserve the current view position (rather than
3538 // snapping the window).
3539
3540 // If there's an ongoing animation within the gesture (e.g. from a window being removed
3541 // during DnD), preserve it.
3542 if let Some(mut anim) = gesture.animation.take() {
3543 anim.offset(gesture.current_view_offset);
3544 self.view_offset = ViewOffset::Animation(anim);
3545 } else {
3546 self.view_offset = ViewOffset::Static(gesture.delta_from_tracker);
3547 }
3548
3549 if !self.columns.is_empty() {
3550 // Just in case, make sure the active window remains on screen.
3551 self.animate_view_offset_to_column(None, self.active_column_idx, None);
3552 }
3553 return;
3554 }
3555
3556 self.view_offset_gesture_end(None);
3557 }
3558
3559 pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool {
3560 if self.interactive_resize.is_some() {
3561 return false;
3562 }
3563
3564 let col = self
3565 .columns
3566 .iter_mut()
3567 .find(|col| col.contains(&window))
3568 .unwrap();
3569
3570 if !col.pending_sizing_mode().is_normal() {
3571 return false;
3572 }
3573
3574 let tile = col
3575 .tiles
3576 .iter_mut()
3577 .find(|tile| tile.window().id() == &window)
3578 .unwrap();
3579
3580 let original_window_size = tile.window_size();
3581
3582 let resize = InteractiveResize {
3583 window,
3584 original_window_size,
3585 data: InteractiveResizeData { edges },
3586 };
3587 self.interactive_resize = Some(resize);
3588
3589 self.view_offset.stop_anim_and_gesture();
3590
3591 true
3592 }
3593
3594 pub fn interactive_resize_update(
3595 &mut self,
3596 window: &W::Id,
3597 delta: Point<f64, Logical>,
3598 ) -> bool {
3599 let Some(resize) = &self.interactive_resize else {
3600 return false;
3601 };
3602
3603 if window != &resize.window {
3604 return false;
3605 }
3606
3607 let is_centering = self.is_centering_focused_column();
3608
3609 let col = self
3610 .columns
3611 .iter_mut()
3612 .find(|col| col.contains(window))
3613 .unwrap();
3614
3615 let tile_idx = col
3616 .tiles
3617 .iter()
3618 .position(|tile| tile.window().id() == window)
3619 .unwrap();
3620
3621 if resize.data.edges.intersects(ResizeEdge::LEFT_RIGHT) {
3622 let mut dx = delta.x;
3623 if resize.data.edges.contains(ResizeEdge::LEFT) {
3624 dx = -dx;
3625 };
3626
3627 if is_centering {
3628 dx *= 2.;
3629 }
3630
3631 let window_width = (resize.original_window_size.w + dx).round() as i32;
3632 col.set_column_width(SizeChange::SetFixed(window_width), Some(tile_idx), false);
3633 }
3634
3635 if resize.data.edges.intersects(ResizeEdge::TOP_BOTTOM) {
3636 // Prevent the simplest case of weird resizing (top edge when this is the topmost
3637 // window).
3638 if !(resize.data.edges.contains(ResizeEdge::TOP) && tile_idx == 0) {
3639 let mut dy = delta.y;
3640 if resize.data.edges.contains(ResizeEdge::TOP) {
3641 dy = -dy;
3642 };
3643
3644 // FIXME: some smarter height distribution would be nice here so that vertical
3645 // resizes work as expected in more cases.
3646
3647 let window_height = (resize.original_window_size.h + dy).round() as i32;
3648 col.set_window_height(SizeChange::SetFixed(window_height), Some(tile_idx), false);
3649 }
3650 }
3651
3652 true
3653 }
3654
3655 pub fn interactive_resize_end(&mut self, window: Option<&W::Id>) {
3656 let Some(resize) = &self.interactive_resize else {
3657 return;
3658 };
3659
3660 if let Some(window) = window {
3661 if window != &resize.window {
3662 return;
3663 }
3664
3665 // Animate the active window into view right away.
3666 if self.columns[self.active_column_idx].contains(window) {
3667 self.animate_view_offset_to_column(None, self.active_column_idx, None);
3668 }
3669 }
3670
3671 self.interactive_resize = None;
3672 }
3673
3674 pub fn refresh(&mut self, is_active: bool, is_focused: bool) {
3675 for (col_idx, col) in self.columns.iter_mut().enumerate() {
3676 let mut col_resize_data = None;
3677 if let Some(resize) = &self.interactive_resize {
3678 if col.contains(&resize.window) {
3679 col_resize_data = Some(resize.data);
3680 }
3681 }
3682
3683 let is_tabbed = col.display_mode == ColumnDisplay::Tabbed;
3684 let extra_size = col.extra_size();
3685
3686 // If transactions are disabled, also disable combined throttling, for more intuitive
3687 // behavior. In tabbed display mode, only one window is visible, so individual
3688 // throttling makes more sense.
3689 let individual_throttling = self.options.disable_transactions || is_tabbed;
3690
3691 let intent = if self.options.disable_resize_throttling {
3692 ConfigureIntent::CanSend
3693 } else if individual_throttling {
3694 // In this case, we don't use combined throttling, but rather compute throttling
3695 // individually below.
3696 ConfigureIntent::CanSend
3697 } else {
3698 col.tiles
3699 .iter()
3700 .fold(ConfigureIntent::NotNeeded, |intent, tile| {
3701 match (intent, tile.window().configure_intent()) {
3702 (_, ConfigureIntent::ShouldSend) => ConfigureIntent::ShouldSend,
3703 (ConfigureIntent::NotNeeded, tile_intent) => tile_intent,
3704 (ConfigureIntent::CanSend, ConfigureIntent::Throttled) => {
3705 ConfigureIntent::Throttled
3706 }
3707 (intent, _) => intent,
3708 }
3709 })
3710 };
3711
3712 for (tile_idx, tile) in col.tiles.iter_mut().enumerate() {
3713 let win = tile.window_mut();
3714
3715 let active_in_column = col.active_tile_idx == tile_idx;
3716 win.set_active_in_column(active_in_column);
3717 win.set_floating(false);
3718
3719 let mut active = is_active && self.active_column_idx == col_idx;
3720 if self.options.deactivate_unfocused_windows {
3721 active &= active_in_column && is_focused;
3722 } else {
3723 // In tabbed mode, all tabs have activated state to reduce unnecessary
3724 // animations when switching tabs.
3725 active &= active_in_column || is_tabbed;
3726 }
3727 win.set_activated(active);
3728
3729 win.set_interactive_resize(col_resize_data);
3730
3731 let border_config = self.options.layout.border.merged_with(&win.rules().border);
3732 let bounds = compute_toplevel_bounds(
3733 border_config,
3734 self.working_area.size,
3735 extra_size,
3736 self.options.layout.gaps,
3737 );
3738 win.set_bounds(bounds);
3739
3740 let intent = if individual_throttling {
3741 win.configure_intent()
3742 } else {
3743 intent
3744 };
3745
3746 if matches!(
3747 intent,
3748 ConfigureIntent::CanSend | ConfigureIntent::ShouldSend
3749 ) {
3750 win.send_pending_configure();
3751 }
3752
3753 win.refresh();
3754 }
3755 }
3756 }
3757
3758 #[cfg(test)]
3759 pub fn view_size(&self) -> Size<f64, Logical> {
3760 self.view_size
3761 }
3762
3763 #[cfg(test)]
3764 pub fn parent_area(&self) -> Rectangle<f64, Logical> {
3765 self.parent_area
3766 }
3767
3768 #[cfg(test)]
3769 pub fn clock(&self) -> &Clock {
3770 &self.clock
3771 }
3772
3773 #[cfg(test)]
3774 pub fn options(&self) -> &Rc<Options> {
3775 &self.options
3776 }
3777
3778 #[cfg(test)]
3779 pub fn active_column_idx(&self) -> usize {
3780 self.active_column_idx
3781 }
3782
3783 #[cfg(test)]
3784 pub(super) fn view_offset(&self) -> &ViewOffset {
3785 &self.view_offset
3786 }
3787
3788 #[cfg(test)]
3789 pub fn verify_invariants(&self) {
3790 assert!(self.view_size.w > 0.);
3791 assert!(self.view_size.h > 0.);
3792 assert!(self.scale > 0.);
3793 assert!(self.scale.is_finite());
3794 assert_eq!(self.columns.len(), self.data.len());
3795 assert_eq!(
3796 self.working_area,
3797 compute_working_area(self.parent_area, self.scale, self.options.layout.struts)
3798 );
3799
3800 if !self.columns.is_empty() {
3801 assert!(self.active_column_idx < self.columns.len());
3802
3803 for (column, data) in zip(&self.columns, &self.data) {
3804 assert!(Rc::ptr_eq(&self.options, &column.options));
3805 assert_eq!(self.clock, column.clock);
3806 assert_eq!(self.scale, column.scale);
3807 column.verify_invariants();
3808
3809 let mut data2 = *data;
3810 data2.update(column);
3811 assert_eq!(data, &data2, "column data must be up to date");
3812 }
3813
3814 let col = &self.columns[self.active_column_idx];
3815
3816 if self.view_offset_to_restore.is_some() {
3817 assert!(
3818 !col.sizing_mode().is_normal(),
3819 "when view_offset_to_restore is set, \
3820 the active column must be fullscreen or maximized"
3821 );
3822 }
3823 }
3824
3825 if let Some(resize) = &self.interactive_resize {
3826 assert!(
3827 self.columns
3828 .iter()
3829 .flat_map(|col| &col.tiles)
3830 .any(|tile| tile.window().id() == &resize.window),
3831 "interactive resize window must be present in the layout"
3832 );
3833 }
3834 }
3835}
3836
3837impl ViewOffset {
3838 /// Returns the current view offset.
3839 pub fn current(&self) -> f64 {
3840 match self {
3841 ViewOffset::Static(offset) => *offset,
3842 ViewOffset::Animation(anim) => anim.value(),
3843 ViewOffset::Gesture(gesture) => {
3844 gesture.current_view_offset
3845 + gesture.animation.as_ref().map_or(0., |anim| anim.value())
3846 }
3847 }
3848 }
3849
3850 /// Returns the target view offset suitable for computing the new view offset.
3851 pub fn target(&self) -> f64 {
3852 match self {
3853 ViewOffset::Static(offset) => *offset,
3854 ViewOffset::Animation(anim) => anim.to(),
3855 // This can be used for example if a gesture is interrupted.
3856 ViewOffset::Gesture(gesture) => gesture.current_view_offset,
3857 }
3858 }
3859
3860 /// Returns a view offset value suitable for saving and later restoration.
3861 ///
3862 /// This means that it shouldn't return an in-progress animation or gesture value.
3863 fn stationary(&self) -> f64 {
3864 match self {
3865 ViewOffset::Static(offset) => *offset,
3866 // For animations we can return the final value.
3867 ViewOffset::Animation(anim) => anim.to(),
3868 ViewOffset::Gesture(gesture) => gesture.stationary_view_offset,
3869 }
3870 }
3871
3872 pub fn is_static(&self) -> bool {
3873 matches!(self, Self::Static(_))
3874 }
3875
3876 pub fn is_gesture(&self) -> bool {
3877 matches!(self, Self::Gesture(_))
3878 }
3879
3880 pub fn is_dnd_scroll(&self) -> bool {
3881 matches!(&self, ViewOffset::Gesture(gesture) if gesture.dnd_last_event_time.is_some())
3882 }
3883
3884 pub fn is_animation_ongoing(&self) -> bool {
3885 match self {
3886 ViewOffset::Static(_) => false,
3887 ViewOffset::Animation(_) => true,
3888 ViewOffset::Gesture(gesture) => gesture.animation.is_some(),
3889 }
3890 }
3891
3892 pub fn offset(&mut self, delta: f64) {
3893 match self {
3894 ViewOffset::Static(offset) => *offset += delta,
3895 ViewOffset::Animation(anim) => anim.offset(delta),
3896 ViewOffset::Gesture(gesture) => {
3897 gesture.stationary_view_offset += delta;
3898 gesture.delta_from_tracker += delta;
3899 gesture.current_view_offset += delta;
3900 }
3901 }
3902 }
3903
3904 pub fn cancel_gesture(&mut self) {
3905 if let ViewOffset::Gesture(gesture) = self {
3906 *self = ViewOffset::Static(gesture.current_view_offset);
3907 }
3908 }
3909
3910 pub fn stop_anim_and_gesture(&mut self) {
3911 *self = ViewOffset::Static(self.current());
3912 }
3913}
3914
3915impl ViewGesture {
3916 fn animate_from(&mut self, from: f64, clock: Clock, config: niri_config::Animation) {
3917 let current = self.animation.as_ref().map_or(0., Animation::value);
3918 self.animation = Some(Animation::new(clock, from + current, 0., 0., config));
3919 }
3920}
3921
3922impl ColumnData {
3923 pub fn new<W: LayoutElement>(column: &Column<W>) -> Self {
3924 let mut rv = Self { width: 0. };
3925 rv.update(column);
3926 rv
3927 }
3928
3929 pub fn update<W: LayoutElement>(&mut self, column: &Column<W>) {
3930 self.width = column.width();
3931 }
3932}
3933
3934impl TileData {
3935 pub fn new<W: LayoutElement>(tile: &Tile<W>, height: WindowHeight) -> Self {
3936 let mut rv = Self {
3937 height,
3938 size: Size::default(),
3939 interactively_resizing_by_left_edge: false,
3940 };
3941 rv.update(tile);
3942 rv
3943 }
3944
3945 pub fn update<W: LayoutElement>(&mut self, tile: &Tile<W>) {
3946 self.size = tile.tile_size();
3947 self.interactively_resizing_by_left_edge = tile
3948 .window()
3949 .interactive_resize_data()
3950 .is_some_and(|data| data.edges.contains(ResizeEdge::LEFT));
3951 }
3952}
3953
3954impl From<PresetSize> for ColumnWidth {
3955 fn from(value: PresetSize) -> Self {
3956 match value {
3957 PresetSize::Proportion(p) => Self::Proportion(p.clamp(0., 10000.)),
3958 PresetSize::Fixed(f) => Self::Fixed(f64::from(f.clamp(1, 100000))),
3959 }
3960 }
3961}
3962
3963impl WindowHeight {
3964 const fn auto_1() -> Self {
3965 Self::Auto { weight: 1. }
3966 }
3967}
3968
3969impl<W: LayoutElement> Column<W> {
3970 #[allow(clippy::too_many_arguments)]
3971 fn new_with_tile(
3972 tile: Tile<W>,
3973 view_size: Size<f64, Logical>,
3974 working_area: Rectangle<f64, Logical>,
3975 parent_area: Rectangle<f64, Logical>,
3976 scale: f64,
3977 width: ColumnWidth,
3978 is_full_width: bool,
3979 ) -> Self {
3980 let options = tile.options.clone();
3981
3982 let display_mode = tile
3983 .window()
3984 .rules()
3985 .default_column_display
3986 .unwrap_or(options.layout.default_column_display);
3987
3988 // Try to match width to a preset width. Consider the following case: a terminal (foot)
3989 // sizes itself to the terminal grid. We open it with default-column-width 0.5. It shrinks
3990 // by a few pixels to evenly match the terminal grid. Then we press
3991 // switch-preset-column-width intending to go to proportion 0.667, but the preset width
3992 // matching code picks the proportion 0.5 preset because it's the next smallest width after
3993 // the current foot's window width. Effectively, this makes the first
3994 // switch-preset-column-width press ignored.
3995 //
3996 // However, here, we do know that width = proportion 0.5 (regardless of what the window
3997 // opened with), and we can match it to a preset right away, if one exists.
3998 let preset_width_idx = options
3999 .layout
4000 .preset_column_widths
4001 .iter()
4002 .position(|preset| width == ColumnWidth::from(*preset));
4003
4004 let mut rv = Self {
4005 tiles: vec![],
4006 data: vec![],
4007 active_tile_idx: 0,
4008 width,
4009 preset_width_idx,
4010 is_full_width,
4011 is_pending_maximized: false,
4012 is_pending_fullscreen: false,
4013 display_mode,
4014 tab_indicator: TabIndicator::new(options.layout.tab_indicator),
4015 move_x_animation: None,
4016 move_y_animation: None,
4017 view_size,
4018 working_area,
4019 parent_area,
4020 scale,
4021 clock: tile.clock.clone(),
4022 options,
4023 id: ColumnId::next(),
4024 };
4025
4026 let pending_sizing_mode = tile.window().pending_sizing_mode();
4027
4028 rv.add_tile_at(0, tile);
4029
4030 match pending_sizing_mode {
4031 SizingMode::Normal => (),
4032 SizingMode::Maximized => rv.set_maximized(true),
4033 SizingMode::Fullscreen => rv.set_fullscreen(true),
4034 }
4035
4036 // Animate the tab indicator for new columns.
4037 if display_mode == ColumnDisplay::Tabbed
4038 && !rv.options.layout.tab_indicator.hide_when_single_tab
4039 && rv.sizing_mode().is_normal()
4040 {
4041 // Usually new columns are created together with window movement actions. For new
4042 // windows, we handle that in start_open_animation().
4043 rv.tab_indicator
4044 .start_open_animation(rv.clock.clone(), rv.options.animations.window_movement.0);
4045 }
4046
4047 rv
4048 }
4049
4050 pub fn id(&self) -> ColumnId {
4051 self.id
4052 }
4053
4054 fn update_config(
4055 &mut self,
4056 view_size: Size<f64, Logical>,
4057 working_area: Rectangle<f64, Logical>,
4058 parent_area: Rectangle<f64, Logical>,
4059 scale: f64,
4060 options: Rc<Options>,
4061 ) {
4062 let mut update_sizes = false;
4063
4064 if self.view_size != view_size
4065 || self.working_area != working_area
4066 || self.parent_area != parent_area
4067 {
4068 update_sizes = true;
4069 }
4070
4071 // If preset widths changed, clear our stored preset index.
4072 if self.options.layout.preset_column_widths != options.layout.preset_column_widths {
4073 self.preset_width_idx = None;
4074 }
4075
4076 // If preset heights changed, make our heights non-preset.
4077 if self.options.layout.preset_window_heights != options.layout.preset_window_heights {
4078 self.convert_heights_to_auto();
4079 update_sizes = true;
4080 }
4081
4082 if self.options.layout.gaps != options.layout.gaps {
4083 update_sizes = true;
4084 }
4085
4086 if self.options.layout.border.off != options.layout.border.off
4087 || self.options.layout.border.width != options.layout.border.width
4088 {
4089 update_sizes = true;
4090 }
4091
4092 if self.options.layout.tab_indicator != options.layout.tab_indicator {
4093 update_sizes = true;
4094 }
4095
4096 for (tile, data) in zip(&mut self.tiles, &mut self.data) {
4097 tile.update_config(view_size, scale, options.clone());
4098 data.update(tile);
4099 }
4100
4101 self.tab_indicator
4102 .update_config(options.layout.tab_indicator);
4103 self.view_size = view_size;
4104 self.working_area = working_area;
4105 self.parent_area = parent_area;
4106 self.scale = scale;
4107 self.options = options;
4108
4109 if update_sizes {
4110 self.update_tile_sizes(false);
4111 }
4112 }
4113
4114 pub fn update_shaders(&mut self) {
4115 for tile in &mut self.tiles {
4116 tile.update_shaders();
4117 }
4118
4119 self.tab_indicator.update_shaders();
4120 }
4121
4122 pub fn advance_animations(&mut self) {
4123 if let Some(move_) = &mut self.move_x_animation {
4124 if move_.anim.is_done() {
4125 self.move_x_animation = None;
4126 }
4127 }
4128 if let Some(move_) = &mut self.move_y_animation {
4129 if move_.anim.is_done() {
4130 self.move_y_animation = None;
4131 }
4132 }
4133
4134 for tile in &mut self.tiles {
4135 tile.advance_animations();
4136 }
4137
4138 self.tab_indicator.advance_animations();
4139 }
4140
4141 pub fn are_animations_ongoing(&self) -> bool {
4142 self.move_x_animation.is_some()
4143 || self.move_y_animation.is_some()
4144 || self.tab_indicator.are_animations_ongoing()
4145 || self.tiles.iter().any(Tile::are_animations_ongoing)
4146 }
4147
4148 pub fn are_transitions_ongoing(&self) -> bool {
4149 self.move_x_animation.is_some()
4150 || self.move_y_animation.is_some()
4151 || self.tab_indicator.are_animations_ongoing()
4152 || self.tiles.iter().any(Tile::are_transitions_ongoing)
4153 }
4154
4155 pub fn is_moving_between_workspaces(&self) -> bool {
4156 self.move_y_animation
4157 .as_ref()
4158 .is_some_and(|anim| anim.is_between_workspaces)
4159 || self
4160 .move_x_animation
4161 .as_ref()
4162 .is_some_and(|anim| anim.is_between_workspaces)
4163 // When moving a window between workspaces into the scrolling layout, it will be
4164 // immediately put into a new column, however the Y move animation and the
4165 // moving-between-workspaces flag will be set on the tile rather than on that new
4166 // column. Since the scrolling layout rendering checks moving between workspaces
4167 // per-column rather than per-tile, we need to include the tiles here.
4168 //
4169 // This is not always visually correct: for example, interactive-moving a tile into an
4170 // otherwise stationary column will now cause that entire column to draw unculled. I'm
4171 // not sure there's a good solution overall for all edge cases here. Consider that the
4172 // column tab indicator needs to draw on top of the tiles, but in this counterexample
4173 // above, the column is stationary, so the single moving tile would instead need to draw
4174 // on top of the tab indicator.
4175 //
4176 // I'm going with this simple check for now that should look ok in most cases.
4177 || self
4178 .tiles
4179 .iter()
4180 .any(|tile| tile.is_moving_between_workspaces())
4181 }
4182
4183 pub fn update_render_elements(&mut self, is_active: bool, view_rect: Rectangle<f64, Logical>) {
4184 let active_idx = self.active_tile_idx;
4185 for (tile_idx, (tile, tile_off)) in self.tiles_mut().enumerate() {
4186 let is_active = is_active && tile_idx == active_idx;
4187
4188 let mut tile_view_rect = view_rect;
4189 tile_view_rect.loc -= tile_off + tile.render_offset();
4190 tile.update_render_elements(is_active, tile_view_rect);
4191 }
4192
4193 let config = self.tab_indicator.config();
4194 let offsets = self.tile_offsets_iter(self.data.iter().copied());
4195 let tabs = zip(&self.tiles, offsets)
4196 .enumerate()
4197 .map(|(tile_idx, (tile, tile_off))| {
4198 let is_active = tile_idx == active_idx;
4199 let is_urgent = tile.window().is_urgent();
4200 let tile_pos = tile_off + tile.render_offset();
4201 TabInfo::from_tile(tile, tile_pos, is_active, is_urgent, &config)
4202 });
4203
4204 // Hide the tab indicator in fullscreen. If you have it configured to overlap the window,
4205 // you don't want that to happen in fullscreen. Also, laying things out correctly when the
4206 // tab indicator is within the column and the column goes fullscreen, would require too
4207 // many changes to the code for too little benefit (it's mostly invisible anyway).
4208 let enabled = self.display_mode == ColumnDisplay::Tabbed && self.sizing_mode().is_normal();
4209
4210 self.tab_indicator.update_render_elements(
4211 enabled,
4212 self.tab_indicator_area(),
4213 view_rect,
4214 self.tiles.len(),
4215 tabs,
4216 is_active,
4217 self.scale,
4218 );
4219 }
4220
4221 pub fn is_pending_fullscreen(&self) -> bool {
4222 self.is_pending_fullscreen
4223 }
4224
4225 pub fn is_pending_maximized(&self) -> bool {
4226 self.is_pending_maximized
4227 }
4228
4229 pub fn pending_sizing_mode(&self) -> SizingMode {
4230 if self.is_pending_fullscreen {
4231 SizingMode::Fullscreen
4232 } else if self.is_pending_maximized {
4233 SizingMode::Maximized
4234 } else {
4235 SizingMode::Normal
4236 }
4237 }
4238
4239 pub fn render_offset(&self) -> Point<f64, Logical> {
4240 let mut offset = Point::from((0., 0.));
4241
4242 if let Some(move_) = &self.move_x_animation {
4243 offset.x += move_.from * move_.anim.value();
4244 }
4245 if let Some(move_) = &self.move_y_animation {
4246 offset.y += move_.from * move_.anim.value();
4247 }
4248
4249 offset
4250 }
4251
4252 pub fn animate_move_from(&mut self, from: Point<f64, Logical>) {
4253 self.animate_move_from_with_config(from, self.options.animations.window_movement.0);
4254 }
4255
4256 pub fn animate_move_from_with_config(
4257 &mut self,
4258 from: Point<f64, Logical>,
4259 config: niri_config::Animation,
4260 ) {
4261 self.animate_move_x_from_with_config(from.x, config);
4262 self.animate_move_y_from_with_config(from.y, config);
4263 }
4264
4265 pub fn animate_move_x_from(&mut self, from_x_offset: f64) {
4266 self.animate_move_x_from_with_config(
4267 from_x_offset,
4268 self.options.animations.window_movement.0,
4269 );
4270 }
4271
4272 pub fn animate_move_x_from_with_config(
4273 &mut self,
4274 from_x_offset: f64,
4275 config: niri_config::Animation,
4276 ) {
4277 let (current_offset, current_between) =
4278 self.move_x_animation.as_ref().map_or((0., false), |move_| {
4279 (move_.from * move_.anim.value(), move_.is_between_workspaces)
4280 });
4281
4282 let anim = Animation::new(self.clock.clone(), 1., 0., 0., config);
4283 self.move_x_animation = Some(MoveAnimation {
4284 anim,
4285 from: from_x_offset + current_offset,
4286 is_between_workspaces: current_between,
4287 });
4288 }
4289
4290 pub fn animate_move_y_from(&mut self, from_y_offset: f64) {
4291 self.animate_move_y_from_with_config(
4292 from_y_offset,
4293 self.options.animations.window_movement.0,
4294 );
4295 }
4296
4297 pub fn animate_move_y_from_with_config(
4298 &mut self,
4299 from_y_offset: f64,
4300 config: niri_config::Animation,
4301 ) {
4302 let (current_offset, current_between) =
4303 self.move_y_animation.as_ref().map_or((0., false), |move_| {
4304 (move_.from * move_.anim.value(), move_.is_between_workspaces)
4305 });
4306
4307 let anim = Animation::new(self.clock.clone(), 1., 0., 0., config);
4308 self.move_y_animation = Some(MoveAnimation {
4309 anim,
4310 from: from_y_offset + current_offset,
4311 is_between_workspaces: current_between,
4312 });
4313 }
4314
4315 pub fn offset_move_anim_current(&mut self, offset: f64) {
4316 if let Some(move_) = self.move_x_animation.as_mut() {
4317 // If the anim is almost done, there's little point trying to offset it; we can let
4318 // things jump. If it turns out like a bad idea, we could restart the anim instead.
4319 let value = move_.anim.value();
4320 if value > 0.001 {
4321 move_.from += offset / value;
4322 }
4323 }
4324 }
4325
4326 pub fn set_anim_y_between_workspaces(&mut self) {
4327 if let Some(anim) = &mut self.move_y_animation {
4328 anim.is_between_workspaces = true;
4329 }
4330 }
4331
4332 /// Returns whether this column is currently fullscreen.
4333 ///
4334 /// As in, if it contains one currently-fullscreen tile, or in tabbed mode, if it contains at
4335 /// least one currently-fullscreen tile.
4336 ///
4337 /// This will lag behind is_pending_fullscreen, depending on when the tiles actually respond to
4338 /// the un/fullscreen request. But, it's possible for is_fullscreen() to flip instantly, for
4339 /// example when consuming a fullscreen tile into a non-pending-fullscreen column.
4340 ///
4341 /// This controls things like:
4342 ///
4343 /// - whether the column draws at the top of the screen or at the start of the working area
4344 /// - whether the column draws above the top layer-shell layer
4345 /// - whether the tab indicator is shown
4346 /// - restoring view_offset_before_fullscreen
4347 ///
4348 /// Edge cases to watch out for:
4349 ///
4350 /// - Consuming a fullscreen tile into a non-tabbed column will keep that tile fullscreen until
4351 /// it responds to the unfullscreen request. This tile may be anywhere in the column,
4352 /// including at the active position.
4353 ///
4354 /// - Changing a fullscreen tabbed column into normal mode is an easy way to get randomly
4355 /// delayed unfullscreening tiles in a normal column.
4356 ///
4357 /// - is_fullscreen() can suddenly change when consuming/expelling a fullscreen tile into/from a
4358 /// non-fullscreen column. This can influence the code that saves/restores the unfullscreen
4359 /// view offset.
4360 fn sizing_mode(&self) -> SizingMode {
4361 // Behaviors that we want:
4362 //
4363 // 1. The common case: single tile in a column. Assume no animations. Fullscreening the tile
4364 // should make it jump to the top-left of the screen only when the tile finishes
4365 // fullscreening. Similarly, unfullscreening should keep it at the top-left until the
4366 // tile had unfullscreened.
4367 //
4368 // 2. Unfullscreening a tabbed column with multiple tiles should restore the view offset
4369 // correctly. This means waiting for *all* tiles to unfullscreen, because otherwise the
4370 // restored view offset will immediately get overwritten by the still screen-wide column
4371 // (it uses the largest tile's width).
4372 //
4373 // 3. Changing a fullscreen tabbed column to normal should probably also restore the view
4374 // offset correctly. Same problem as above, but now for normal columns (since display
4375 // mode change applies instantly).
4376 //
4377 // The logic that satisfies these behaviors is to check if *any* tile is fullscreen.
4378 let mut any_fullscreen = false;
4379 let mut any_maximized = false;
4380 for tile in &self.tiles {
4381 match tile.sizing_mode() {
4382 SizingMode::Normal => (),
4383 SizingMode::Maximized => any_maximized = true,
4384 SizingMode::Fullscreen => any_fullscreen = true,
4385 }
4386 }
4387
4388 if any_fullscreen {
4389 SizingMode::Fullscreen
4390 } else if any_maximized {
4391 SizingMode::Maximized
4392 } else {
4393 SizingMode::Normal
4394 }
4395 }
4396
4397 pub fn contains(&self, window: &W::Id) -> bool {
4398 self.tiles
4399 .iter()
4400 .map(Tile::window)
4401 .any(|win| win.id() == window)
4402 }
4403
4404 pub fn position(&self, window: &W::Id) -> Option<usize> {
4405 self.tiles
4406 .iter()
4407 .map(Tile::window)
4408 .position(|win| win.id() == window)
4409 }
4410
4411 fn activate_idx(&mut self, idx: usize) -> bool {
4412 if self.active_tile_idx == idx {
4413 return false;
4414 }
4415
4416 self.active_tile_idx = idx;
4417
4418 self.tiles[idx].ensure_alpha_animates_to_1();
4419
4420 true
4421 }
4422
4423 fn activate_window(&mut self, window: &W::Id) {
4424 let idx = self.position(window).unwrap();
4425 self.activate_idx(idx);
4426 }
4427
4428 fn add_tile_at(&mut self, idx: usize, mut tile: Tile<W>) {
4429 tile.update_config(self.view_size, self.scale, self.options.clone());
4430
4431 // Inserting a tile pushes down all tiles below it, but also in always-centering mode it
4432 // will affect the X position of all tiles in the column.
4433 let mut prev_offsets = Vec::with_capacity(self.tiles.len() + 1);
4434 prev_offsets.extend(self.tile_offsets().take(self.tiles.len()));
4435
4436 if self.display_mode != ColumnDisplay::Tabbed {
4437 self.is_pending_fullscreen = false;
4438 self.is_pending_maximized = false;
4439 }
4440
4441 self.data
4442 .insert(idx, TileData::new(&tile, WindowHeight::auto_1()));
4443 self.tiles.insert(idx, tile);
4444 self.update_tile_sizes(true);
4445
4446 // Animate tiles according to the offset changes.
4447 prev_offsets.insert(idx, Point::default());
4448 for (i, ((tile, offset), prev)) in zip(self.tiles_mut(), prev_offsets).enumerate() {
4449 if i == idx {
4450 continue;
4451 }
4452
4453 tile.animate_move_from(prev - offset);
4454 }
4455 }
4456
4457 fn update_window(&mut self, window: &W::Id) {
4458 let (tile_idx, tile) = self
4459 .tiles
4460 .iter_mut()
4461 .enumerate()
4462 .find(|(_, tile)| tile.window().id() == window)
4463 .unwrap();
4464
4465 let prev_height = self.data[tile_idx].size.h;
4466
4467 tile.update_window();
4468 self.data[tile_idx].update(tile);
4469
4470 let offset = prev_height - self.data[tile_idx].size.h;
4471
4472 let is_tabbed = self.display_mode == ColumnDisplay::Tabbed;
4473
4474 // Move windows below in tandem with resizing.
4475 //
4476 // FIXME: in always-centering mode, window resizing will affect the offsets of all other
4477 // windows in the column, so they should all be animated. How should this interact with
4478 // animated vs. non-animated resizes? For example, an animated +20 resize followed by two
4479 // non-animated -10 resizes.
4480 if !is_tabbed && offset != 0. {
4481 if tile.resize_animation().is_some() {
4482 // If there's a resize animation (that may have just started in
4483 // tile.update_window()), then the apparent size change is smooth with no sudden
4484 // jumps. This corresponds to adding an Y animation to tiles below.
4485 for tile in &mut self.tiles[tile_idx + 1..] {
4486 tile.animate_move_y_from_with_config(
4487 offset,
4488 self.options.animations.window_resize.anim,
4489 );
4490 }
4491 } else {
4492 // There's no resize animation, but the offset is nonzero. This could happen for
4493 // example:
4494 // - if the window resized on its own, which we don't animate
4495 // - if the window resized by less than 10 px (the resize threshold)
4496 //
4497 // The latter case could also cancel an ongoing resize animation.
4498 //
4499 // Now, stationary tiles below shouldn't react to this offset change in any way,
4500 // i.e. their apparent Y position should jump together with the resize. However,
4501 // tiles below that are already animating an Y movement should offset their
4502 // animations to avoid the jump.
4503 //
4504 // Notably, this is necessary to fix the animation jump when resizing height back
4505 // and forth in quick succession (in a way that cancels the resize animation).
4506 for tile in &mut self.tiles[tile_idx + 1..] {
4507 tile.offset_move_y_anim_current(offset);
4508 }
4509 }
4510 }
4511 }
4512
4513 /// Extra size taken up by elements in the column such as the tab indicator.
4514 fn extra_size(&self) -> Size<f64, Logical> {
4515 if self.display_mode == ColumnDisplay::Tabbed {
4516 self.tab_indicator.extra_size(self.tiles.len(), self.scale)
4517 } else {
4518 Size::from((0., 0.))
4519 }
4520 }
4521
4522 fn resolve_preset_width(&self, preset: PresetSize) -> ResolvedSize {
4523 let extra = self.extra_size();
4524 resolve_preset_size(preset, &self.options, self.working_area.size.w, extra.w)
4525 }
4526
4527 fn resolve_preset_height(&self, preset: PresetSize) -> ResolvedSize {
4528 let extra = self.extra_size();
4529 resolve_preset_size(preset, &self.options, self.working_area.size.h, extra.h)
4530 }
4531
4532 fn resolve_column_width(&self, width: ColumnWidth) -> f64 {
4533 let working_size = self.working_area.size;
4534 let gaps = self.options.layout.gaps;
4535 let extra = self.extra_size();
4536
4537 match width {
4538 ColumnWidth::Proportion(proportion) => {
4539 (working_size.w - gaps) * proportion - gaps - extra.w
4540 }
4541 ColumnWidth::Fixed(width) => width,
4542 }
4543 }
4544
4545 fn update_tile_sizes(&mut self, animate: bool) {
4546 self.update_tile_sizes_with_transaction(animate, Transaction::new());
4547 }
4548
4549 fn update_tile_sizes_with_transaction(&mut self, animate: bool, transaction: Transaction) {
4550 let sizing_mode = self.pending_sizing_mode();
4551 if matches!(sizing_mode, SizingMode::Fullscreen | SizingMode::Maximized) {
4552 for (tile_idx, tile) in self.tiles.iter_mut().enumerate() {
4553 // In tabbed mode, only the visible window participates in the transaction.
4554 let is_active = tile_idx == self.active_tile_idx;
4555 let transaction = if self.display_mode == ColumnDisplay::Tabbed && !is_active {
4556 None
4557 } else {
4558 Some(transaction.clone())
4559 };
4560
4561 if matches!(sizing_mode, SizingMode::Fullscreen) {
4562 tile.request_fullscreen(animate, transaction);
4563 } else {
4564 tile.request_maximized(self.parent_area.size, animate, transaction);
4565 }
4566 }
4567 return;
4568 }
4569
4570 let is_tabbed = self.display_mode == ColumnDisplay::Tabbed;
4571
4572 let min_size: Vec<_> = self
4573 .tiles
4574 .iter()
4575 .map(Tile::min_size_nonfullscreen)
4576 .map(|mut size| {
4577 size.w = size.w.max(1.);
4578 size.h = size.h.max(1.);
4579 size
4580 })
4581 .collect();
4582 let max_size: Vec<_> = self
4583 .tiles
4584 .iter()
4585 .map(Tile::max_size_nonfullscreen)
4586 .collect();
4587
4588 // Compute the column width.
4589 let min_width = min_size
4590 .iter()
4591 .map(|size| NotNan::new(size.w).unwrap())
4592 .max()
4593 .map(NotNan::into_inner)
4594 .unwrap();
4595 let max_width = max_size
4596 .iter()
4597 .filter_map(|size| {
4598 let w = size.w;
4599 if w == 0. {
4600 None
4601 } else {
4602 Some(NotNan::new(w).unwrap())
4603 }
4604 })
4605 .min()
4606 .map(NotNan::into_inner)
4607 .unwrap_or(f64::from(i32::MAX));
4608 let max_width = f64::max(max_width, min_width);
4609
4610 let width = if self.is_full_width {
4611 ColumnWidth::Proportion(1.)
4612 } else {
4613 self.width
4614 };
4615
4616 let working_size = self.working_area.size;
4617 let extra_size = self.extra_size();
4618
4619 let width = self.resolve_column_width(width);
4620 let width = f64::max(f64::min(width, max_width), min_width);
4621 let max_tile_height = working_size.h - self.options.layout.gaps * 2. - extra_size.h;
4622
4623 // If there are multiple windows in a column, clamp the non-auto window's height according
4624 // to other windows' min sizes.
4625 let mut max_non_auto_window_height = None;
4626 if self.tiles.len() > 1 && !is_tabbed {
4627 if let Some(non_auto_idx) = self
4628 .data
4629 .iter()
4630 .position(|data| !matches!(data.height, WindowHeight::Auto { .. }))
4631 {
4632 let min_height_taken = min_size
4633 .iter()
4634 .enumerate()
4635 .filter(|(idx, _)| *idx != non_auto_idx)
4636 .map(|(_, min_size)| min_size.h + self.options.layout.gaps)
4637 .sum::<f64>();
4638
4639 let tile = &self.tiles[non_auto_idx];
4640 let height_left = max_tile_height - min_height_taken;
4641 max_non_auto_window_height = Some(f64::max(
4642 1.,
4643 tile.window_height_for_tile_height(height_left).round(),
4644 ));
4645 }
4646 }
4647
4648 // Compute the tile heights. Start by converting window heights to tile heights.
4649 let mut heights = zip(&self.tiles, &self.data)
4650 .map(|(tile, data)| match data.height {
4651 auto @ WindowHeight::Auto { .. } => auto,
4652 WindowHeight::Fixed(height) => {
4653 let mut window_height = height.round().max(1.);
4654 if let Some(max) = max_non_auto_window_height {
4655 window_height = f64::min(window_height, max);
4656 } else {
4657 // In any case, clamp to the working area height.
4658 let max = tile.window_height_for_tile_height(max_tile_height).round();
4659 window_height = f64::min(window_height, max);
4660 }
4661
4662 WindowHeight::Fixed(tile.tile_height_for_window_height(window_height))
4663 }
4664 WindowHeight::Preset(idx) => {
4665 let preset = self.options.layout.preset_window_heights[idx];
4666 let window_height = match self.resolve_preset_height(preset) {
4667 ResolvedSize::Tile(h) => tile.window_height_for_tile_height(h),
4668 ResolvedSize::Window(h) => h,
4669 };
4670
4671 let mut window_height = window_height.round().clamp(1., 100000.);
4672 if let Some(max) = max_non_auto_window_height {
4673 window_height = f64::min(window_height, max);
4674 }
4675
4676 let tile_height = tile.tile_height_for_window_height(window_height);
4677 WindowHeight::Fixed(tile_height)
4678 }
4679 })
4680 .collect::<Vec<_>>();
4681
4682 // In tabbed display mode, fill fixed heights right away.
4683 if is_tabbed {
4684 // All tiles have the same height, equal to the height of the only fixed tile (if any).
4685 let tabbed_height = heights
4686 .iter()
4687 .find_map(|h| {
4688 if let WindowHeight::Fixed(h) = h {
4689 Some(*h)
4690 } else {
4691 None
4692 }
4693 })
4694 .unwrap_or(max_tile_height);
4695
4696 // We also take min height of all tabs into account.
4697 let min_height = min_size
4698 .iter()
4699 .map(|size| NotNan::new(size.h).unwrap())
4700 .max()
4701 .map(NotNan::into_inner)
4702 .unwrap();
4703 // But, if there's a larger-than-workspace tab, we don't want to force all tabs to that
4704 // size.
4705 let min_height = f64::min(max_tile_height, min_height);
4706 let tabbed_height = f64::max(tabbed_height, min_height);
4707
4708 heights.fill(WindowHeight::Fixed(tabbed_height));
4709
4710 // The following logic will apply individual min/max height, etc.
4711 }
4712
4713 let gaps_left = self.options.layout.gaps * (self.tiles.len() + 1) as f64;
4714 let mut height_left = working_size.h - gaps_left;
4715 let mut auto_tiles_left = self.tiles.len();
4716
4717 // Subtract all fixed-height tiles.
4718 for (h, (min_size, max_size)) in zip(&mut heights, zip(&min_size, &max_size)) {
4719 // Check if the tile has an exact height constraint.
4720 if min_size.h == max_size.h {
4721 *h = WindowHeight::Fixed(min_size.h);
4722 }
4723
4724 if let WindowHeight::Fixed(h) = h {
4725 if max_size.h > 0. {
4726 *h = f64::min(*h, max_size.h);
4727 }
4728 *h = f64::max(*h, min_size.h);
4729
4730 height_left -= *h;
4731 auto_tiles_left -= 1;
4732 }
4733 }
4734
4735 let mut total_weight: f64 = heights
4736 .iter()
4737 .filter_map(|h| {
4738 if let WindowHeight::Auto { weight } = *h {
4739 Some(weight)
4740 } else {
4741 None
4742 }
4743 })
4744 .sum();
4745
4746 // Iteratively try to distribute the remaining height, checking against tile min heights.
4747 // Pick an auto height according to the current sizes, then check if it satisfies all
4748 // remaining min heights. If not, allocate fixed height to those tiles and repeat the
4749 // loop. On each iteration the auto height will get smaller.
4750 //
4751 // NOTE: we do not respect max height here. Doing so would complicate things: if the current
4752 // auto height is above some tile's max height, then the auto height can become larger.
4753 // Combining this with the min height loop is where the complexity appears.
4754 //
4755 // However, most max height uses are for fixed-size dialogs, where min height == max_height.
4756 // This case is separately handled above.
4757 'outer: while auto_tiles_left > 0 {
4758 // Wayland requires us to round the requested size for a window to integer logical
4759 // pixels, therefore we compute the remaining auto height dynamically.
4760 let mut height_left_2 = height_left;
4761 let mut total_weight_2 = total_weight;
4762 for ((h, tile), min_size) in zip(zip(&mut heights, &self.tiles), &min_size) {
4763 let weight = match *h {
4764 WindowHeight::Auto { weight } => weight,
4765 WindowHeight::Fixed(_) => continue,
4766 WindowHeight::Preset(_) => unreachable!(),
4767 };
4768 let factor = weight / total_weight_2;
4769
4770 // Compute the current auto height.
4771 let mut auto = height_left_2 * factor;
4772
4773 // Check if the auto height satisfies the min height.
4774 if min_size.h > auto {
4775 auto = min_size.h;
4776 *h = WindowHeight::Fixed(auto);
4777 height_left -= auto;
4778 total_weight -= weight;
4779 auto_tiles_left -= 1;
4780
4781 // If a min height was unsatisfied, then we allocate the tile more than the
4782 // auto height, which means that the remaining auto tiles now have less height
4783 // to work with, and the loop must run again.
4784 //
4785 // If we keep going in this loop and break out later, we may allocate less
4786 // height to the subsequent tiles than would be available next iteration and
4787 // potentially trip their min height check earlier than necessary, leading to
4788 // visible snapping.
4789 continue 'outer;
4790 }
4791
4792 auto = tile.tile_height_for_window_height(
4793 tile.window_height_for_tile_height(auto).round().max(1.),
4794 );
4795
4796 height_left_2 -= auto;
4797 total_weight_2 -= weight;
4798 }
4799
4800 // All min heights were satisfied, fill them in.
4801 for (h, tile) in zip(&mut heights, &self.tiles) {
4802 let weight = match *h {
4803 WindowHeight::Auto { weight } => weight,
4804 WindowHeight::Fixed(_) => continue,
4805 WindowHeight::Preset(_) => unreachable!(),
4806 };
4807 let factor = weight / total_weight;
4808
4809 // Compute the current auto height.
4810 let auto = height_left * factor;
4811 let auto = tile.tile_height_for_window_height(
4812 tile.window_height_for_tile_height(auto).round().max(1.),
4813 );
4814
4815 *h = WindowHeight::Fixed(auto);
4816 height_left -= auto;
4817 total_weight -= weight;
4818 auto_tiles_left -= 1;
4819 }
4820
4821 assert_eq!(auto_tiles_left, 0);
4822 }
4823
4824 for (tile_idx, (tile, h)) in zip(&mut self.tiles, heights).enumerate() {
4825 let WindowHeight::Fixed(height) = h else {
4826 unreachable!()
4827 };
4828
4829 let size = Size::from((width, height));
4830
4831 // In tabbed mode, only the visible window participates in the transaction.
4832 let is_active = tile_idx == self.active_tile_idx;
4833 let transaction = if self.display_mode == ColumnDisplay::Tabbed && !is_active {
4834 None
4835 } else {
4836 Some(transaction.clone())
4837 };
4838
4839 tile.request_tile_size(size, animate, transaction);
4840 }
4841 }
4842
4843 fn width(&self) -> f64 {
4844 let mut tiles_width = self
4845 .data
4846 .iter()
4847 .map(|data| NotNan::new(data.size.w).unwrap())
4848 .max()
4849 .map(NotNan::into_inner)
4850 .unwrap();
4851
4852 if self.display_mode == ColumnDisplay::Tabbed && self.sizing_mode().is_normal() {
4853 let extra_size = self.tab_indicator.extra_size(self.tiles.len(), self.scale);
4854 tiles_width += extra_size.w;
4855 }
4856
4857 tiles_width
4858 }
4859
4860 fn focus_index(&mut self, index: u8) {
4861 let idx = min(usize::from(index.saturating_sub(1)), self.tiles.len() - 1);
4862 self.activate_idx(idx);
4863 }
4864
4865 fn focus_up(&mut self) -> bool {
4866 self.activate_idx(self.active_tile_idx.saturating_sub(1))
4867 }
4868
4869 fn focus_down(&mut self) -> bool {
4870 self.activate_idx(min(self.active_tile_idx + 1, self.tiles.len() - 1))
4871 }
4872
4873 fn focus_top(&mut self) {
4874 self.activate_idx(0);
4875 }
4876
4877 fn focus_bottom(&mut self) {
4878 self.activate_idx(self.tiles.len() - 1);
4879 }
4880
4881 fn move_up(&mut self) -> bool {
4882 let new_idx = self.active_tile_idx.saturating_sub(1);
4883 if self.active_tile_idx == new_idx {
4884 return false;
4885 }
4886
4887 let mut ys = self.tile_offsets().skip(self.active_tile_idx);
4888 let active_y = ys.next().unwrap().y;
4889 let next_y = ys.next().unwrap().y;
4890 drop(ys);
4891
4892 self.tiles.swap(self.active_tile_idx, new_idx);
4893 self.data.swap(self.active_tile_idx, new_idx);
4894 self.active_tile_idx = new_idx;
4895
4896 // Animate the movement.
4897 let new_active_y = self.tile_offset(new_idx).y;
4898 self.tiles[new_idx].animate_move_y_from(active_y - new_active_y);
4899 self.tiles[new_idx + 1].animate_move_y_from(active_y - next_y);
4900
4901 true
4902 }
4903
4904 fn move_down(&mut self) -> bool {
4905 let new_idx = min(self.active_tile_idx + 1, self.tiles.len() - 1);
4906 if self.active_tile_idx == new_idx {
4907 return false;
4908 }
4909
4910 let mut ys = self.tile_offsets().skip(self.active_tile_idx);
4911 let active_y = ys.next().unwrap().y;
4912 let next_y = ys.next().unwrap().y;
4913 drop(ys);
4914
4915 self.tiles.swap(self.active_tile_idx, new_idx);
4916 self.data.swap(self.active_tile_idx, new_idx);
4917 self.active_tile_idx = new_idx;
4918
4919 // Animate the movement.
4920 let new_active_y = self.tile_offset(new_idx).y;
4921 self.tiles[new_idx].animate_move_y_from(active_y - new_active_y);
4922 self.tiles[new_idx - 1].animate_move_y_from(next_y - active_y);
4923
4924 true
4925 }
4926
4927 fn toggle_width(&mut self, tile_idx: Option<usize>, forwards: bool) {
4928 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
4929
4930 let preset_idx = if self.is_full_width || self.is_pending_maximized {
4931 None
4932 } else {
4933 self.preset_width_idx
4934 };
4935
4936 let len = self.options.layout.preset_column_widths.len();
4937 let preset_idx = if let Some(idx) = preset_idx {
4938 (idx + if forwards { 1 } else { len - 1 }) % len
4939 } else {
4940 let tile = &self.tiles[tile_idx];
4941 let current_window = tile.window_expected_or_current_size().w;
4942 let current_tile = tile.tile_expected_or_current_size().w;
4943
4944 let mut it = self
4945 .options
4946 .layout
4947 .preset_column_widths
4948 .iter()
4949 .map(|preset| self.resolve_preset_width(*preset));
4950
4951 if forwards {
4952 it.position(|resolved| {
4953 match resolved {
4954 // Some allowance for fractional scaling purposes.
4955 ResolvedSize::Tile(resolved) => current_tile + 1. < resolved,
4956 ResolvedSize::Window(resolved) => current_window + 1. < resolved,
4957 }
4958 })
4959 .unwrap_or(0)
4960 } else {
4961 it.rposition(|resolved| {
4962 match resolved {
4963 // Some allowance for fractional scaling purposes.
4964 ResolvedSize::Tile(resolved) => resolved + 1. < current_tile,
4965 ResolvedSize::Window(resolved) => resolved + 1. < current_window,
4966 }
4967 })
4968 .unwrap_or(len - 1)
4969 }
4970 };
4971
4972 let preset = self.options.layout.preset_column_widths[preset_idx];
4973 self.set_column_width(SizeChange::from(preset), Some(tile_idx), true);
4974
4975 self.preset_width_idx = Some(preset_idx);
4976 }
4977
4978 fn toggle_full_width(&mut self) {
4979 if self.is_pending_maximized {
4980 // Treat it as unmaximize.
4981 self.is_pending_maximized = false;
4982 self.is_full_width = false;
4983 } else {
4984 self.is_full_width = !self.is_full_width;
4985 }
4986
4987 self.update_tile_sizes(true);
4988 }
4989
4990 fn set_column_width(&mut self, change: SizeChange, tile_idx: Option<usize>, animate: bool) {
4991 let current = if self.is_full_width || self.is_pending_maximized {
4992 ColumnWidth::Proportion(1.)
4993 } else {
4994 self.width
4995 };
4996
4997 let current_px = self.resolve_column_width(current);
4998
4999 // FIXME: fix overflows then remove limits.
5000 const MAX_PX: f64 = 100000.;
5001 const MAX_F: f64 = 10000.;
5002
5003 let width = match (current, change) {
5004 (_, SizeChange::SetFixed(fixed)) => {
5005 // As a special case, setting a fixed column width will compute it in such a way
5006 // that the specified (usually active) window gets that width. This is the
5007 // intention behind the ability to set a fixed size.
5008 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
5009 let tile = &self.tiles[tile_idx];
5010 ColumnWidth::Fixed(
5011 tile.tile_width_for_window_width(f64::from(fixed))
5012 .clamp(1., MAX_PX),
5013 )
5014 }
5015 (_, SizeChange::SetProportion(proportion)) => {
5016 ColumnWidth::Proportion((proportion / 100.).clamp(0., MAX_F))
5017 }
5018 (_, SizeChange::AdjustFixed(delta)) => {
5019 let width = (current_px + f64::from(delta)).clamp(1., MAX_PX);
5020 ColumnWidth::Fixed(width)
5021 }
5022 (ColumnWidth::Proportion(current), SizeChange::AdjustProportion(delta)) => {
5023 let proportion = (current + delta / 100.).clamp(0., MAX_F);
5024 ColumnWidth::Proportion(proportion)
5025 }
5026 (ColumnWidth::Fixed(_), SizeChange::AdjustProportion(delta)) => {
5027 let full = self.working_area.size.w - self.options.layout.gaps;
5028 let current = if full == 0. {
5029 1.
5030 } else {
5031 (current_px + self.options.layout.gaps + self.extra_size().w) / full
5032 };
5033 let proportion = (current + delta / 100.).clamp(0., MAX_F);
5034 ColumnWidth::Proportion(proportion)
5035 }
5036 };
5037
5038 self.width = width;
5039 self.preset_width_idx = None;
5040 self.is_full_width = false;
5041 self.is_pending_maximized = false;
5042 self.update_tile_sizes(animate);
5043 }
5044
5045 fn set_window_height(&mut self, change: SizeChange, tile_idx: Option<usize>, animate: bool) {
5046 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
5047
5048 // Start by converting all heights to automatic, since only one window in the column can be
5049 // non-auto-height. If the current tile is already non-auto, however, we can skip that
5050 // step. Which is not only for optimization, but also preserves automatic weights in case
5051 // one window is resized in such a way that other windows hit their min size, and then
5052 // back.
5053 if matches!(self.data[tile_idx].height, WindowHeight::Auto { .. }) {
5054 self.convert_heights_to_auto();
5055 }
5056
5057 let current = self.data[tile_idx].height;
5058 let tile = &self.tiles[tile_idx];
5059 let current_window_px = match current {
5060 WindowHeight::Auto { .. } | WindowHeight::Preset(_) => tile.window_size().h,
5061 WindowHeight::Fixed(height) => height,
5062 };
5063 let current_tile_px = tile.tile_height_for_window_height(current_window_px);
5064
5065 let working_size = self.working_area.size.h;
5066 let gaps = self.options.layout.gaps;
5067 let extra_size = self.extra_size().h;
5068 let full = working_size - gaps;
5069 let current_prop = if full == 0. {
5070 1.
5071 } else {
5072 (current_tile_px + gaps) / full
5073 };
5074
5075 // FIXME: fix overflows then remove limits.
5076 const MAX_PX: f64 = 100000.;
5077
5078 let mut window_height = match change {
5079 SizeChange::SetFixed(fixed) => f64::from(fixed),
5080 SizeChange::SetProportion(proportion) => {
5081 let tile_height = (working_size - gaps) * (proportion / 100.) - gaps - extra_size;
5082 tile.window_height_for_tile_height(tile_height)
5083 }
5084 SizeChange::AdjustFixed(delta) => current_window_px + f64::from(delta),
5085 SizeChange::AdjustProportion(delta) => {
5086 let proportion = current_prop + delta / 100.;
5087 let tile_height = (working_size - gaps) * proportion - gaps - extra_size;
5088 tile.window_height_for_tile_height(tile_height)
5089 }
5090 };
5091
5092 // Clamp the height according to other windows' min sizes, or simply to working area height.
5093 let min_height_taken = if self.display_mode == ColumnDisplay::Tabbed {
5094 0.
5095 } else {
5096 self.tiles
5097 .iter()
5098 .enumerate()
5099 .filter(|(idx, _)| *idx != tile_idx)
5100 .map(|(_, tile)| f64::max(1., tile.min_size_nonfullscreen().h) + gaps)
5101 .sum::<f64>()
5102 };
5103 let height_left = working_size - extra_size - gaps - min_height_taken - gaps;
5104 let height_left = f64::max(1., tile.window_height_for_tile_height(height_left));
5105 window_height = f64::min(height_left, window_height);
5106
5107 // Clamp it against the window height constraints.
5108 let win = &self.tiles[tile_idx].window();
5109 let min_h = win.min_size().h;
5110 let max_h = win.max_size().h;
5111
5112 if max_h > 0 {
5113 window_height = f64::min(window_height, f64::from(max_h));
5114 }
5115 if min_h > 0 {
5116 window_height = f64::max(window_height, f64::from(min_h));
5117 }
5118
5119 self.data[tile_idx].height = WindowHeight::Fixed(window_height.clamp(1., MAX_PX));
5120 self.is_pending_maximized = false;
5121 self.update_tile_sizes(animate);
5122 }
5123
5124 fn reset_window_height(&mut self, tile_idx: Option<usize>) {
5125 if self.display_mode == ColumnDisplay::Tabbed {
5126 // When tabbed, reset window height should work on any window, not just the fixed-size
5127 // one.
5128 for data in &mut self.data {
5129 data.height = WindowHeight::auto_1();
5130 }
5131 } else {
5132 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
5133 self.data[tile_idx].height = WindowHeight::auto_1();
5134 }
5135
5136 self.update_tile_sizes(true);
5137 }
5138
5139 fn toggle_window_height(&mut self, tile_idx: Option<usize>, forwards: bool) {
5140 let tile_idx = tile_idx.unwrap_or(self.active_tile_idx);
5141
5142 // Start by converting all heights to automatic, since only one window in the column can be
5143 // non-auto-height. If the current tile is already non-auto, however, we can skip that
5144 // step. Which is not only for optimization, but also preserves automatic weights in case
5145 // one window is resized in such a way that other windows hit their min size, and then
5146 // back.
5147 if matches!(self.data[tile_idx].height, WindowHeight::Auto { .. }) {
5148 self.convert_heights_to_auto();
5149 }
5150
5151 let len = self.options.layout.preset_window_heights.len();
5152 let preset_idx = match self.data[tile_idx].height {
5153 WindowHeight::Preset(idx) if !self.is_pending_maximized => {
5154 (idx + if forwards { 1 } else { len - 1 }) % len
5155 }
5156 _ => {
5157 let current = self.data[tile_idx].size.h;
5158 let tile = &self.tiles[tile_idx];
5159
5160 let mut it = self
5161 .options
5162 .layout
5163 .preset_window_heights
5164 .iter()
5165 .copied()
5166 .map(|preset| {
5167 let window_height = match self.resolve_preset_height(preset) {
5168 ResolvedSize::Tile(h) => tile.window_height_for_tile_height(h),
5169 ResolvedSize::Window(h) => h,
5170 };
5171 tile.tile_height_for_window_height(window_height.round().clamp(1., 100000.))
5172 });
5173
5174 if forwards {
5175 it.position(|resolved| {
5176 // Some allowance for fractional scaling purposes.
5177 current + 1. < resolved
5178 })
5179 .unwrap_or(0)
5180 } else {
5181 it.rposition(|resolved| {
5182 // Some allowance for fractional scaling purposes.
5183 resolved + 1. < current
5184 })
5185 .unwrap_or(len - 1)
5186 }
5187 }
5188 };
5189 self.data[tile_idx].height = WindowHeight::Preset(preset_idx);
5190 self.is_pending_maximized = false;
5191 self.update_tile_sizes(true);
5192 }
5193
5194 /// Converts all heights in the column to automatic, preserving the apparent heights.
5195 ///
5196 /// All weights are recomputed to preserve the current tile heights while "centering" the
5197 /// weights at the median window height (it gets weight = 1).
5198 ///
5199 /// One case where apparent heights will not be preserved is when the column is taller than the
5200 /// working area.
5201 fn convert_heights_to_auto(&mut self) {
5202 let heights: Vec<_> = self.tiles.iter().map(|tile| tile.tile_size().h).collect();
5203
5204 // Weights are invariant to multiplication: a column with weights 2, 2, 1 is equivalent to
5205 // a column with weights 4, 4, 2. So we find the median window height and use that as 1.
5206 let mut sorted = heights.clone();
5207 sorted.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
5208 let median = sorted[sorted.len() / 2];
5209
5210 for (data, height) in zip(&mut self.data, heights) {
5211 let weight = height / median;
5212 data.height = WindowHeight::Auto { weight };
5213 }
5214 }
5215
5216 fn set_fullscreen(&mut self, is_fullscreen: bool) {
5217 if self.is_pending_fullscreen == is_fullscreen {
5218 return;
5219 }
5220
5221 if is_fullscreen {
5222 assert!(self.tiles.len() == 1 || self.display_mode == ColumnDisplay::Tabbed);
5223 }
5224
5225 self.is_pending_fullscreen = is_fullscreen;
5226 self.update_tile_sizes(true);
5227 }
5228
5229 fn set_maximized(&mut self, maximize: bool) {
5230 if self.is_pending_maximized == maximize {
5231 return;
5232 }
5233
5234 if maximize {
5235 assert!(self.tiles.len() == 1 || self.display_mode == ColumnDisplay::Tabbed);
5236 }
5237
5238 self.is_pending_maximized = maximize;
5239 self.update_tile_sizes(true);
5240 }
5241
5242 fn set_column_display(&mut self, display: ColumnDisplay) {
5243 if self.display_mode == display {
5244 return;
5245 }
5246
5247 // Animate the movement.
5248 //
5249 // We're doing some shortcuts here because we know that currently normal vs. tabbed can
5250 // only cause a vertical shift + a shift to the origin.
5251 //
5252 // Doing it this way to avoid storing all tile positions in a vector. If more display modes
5253 // are added it might be simpler to just collect everything into a smallvec.
5254 let prev_origin = self.tiles_origin();
5255 self.display_mode = display;
5256 let new_origin = self.tiles_origin();
5257 let origin_delta = prev_origin - new_origin;
5258
5259 // When need to walk the tiles in the normal display mode to get the right offsets.
5260 self.display_mode = ColumnDisplay::Normal;
5261 for (tile, pos) in self.tiles_mut() {
5262 let mut y_delta = pos.y - prev_origin.y;
5263
5264 // Invert the Y motion when transitioning *to* normal display mode.
5265 if display == ColumnDisplay::Normal {
5266 y_delta *= -1.;
5267 }
5268
5269 let mut delta = origin_delta;
5270 delta.y += y_delta;
5271 tile.animate_move_from(delta);
5272 }
5273
5274 // Animate the opacity.
5275 for (idx, tile) in self.tiles.iter_mut().enumerate() {
5276 let is_active = idx == self.active_tile_idx;
5277 if !is_active {
5278 let (from, to) = if display == ColumnDisplay::Tabbed {
5279 (1., 0.)
5280 } else {
5281 (0., 1.)
5282 };
5283 tile.animate_alpha(from, to, self.options.animations.window_movement.0);
5284 }
5285 }
5286
5287 // Animate the appearance of the tab indicator.
5288 if display == ColumnDisplay::Tabbed {
5289 self.tab_indicator.start_open_animation(
5290 self.clock.clone(),
5291 self.options.animations.window_movement.0,
5292 );
5293 }
5294
5295 // Now switch the display mode for real.
5296 self.display_mode = display;
5297 self.update_tile_sizes(true);
5298 }
5299
5300 fn tiles_origin(&self) -> Point<f64, Logical> {
5301 let mut origin = Point::from((0., 0.));
5302
5303 match self.sizing_mode() {
5304 SizingMode::Normal => (),
5305 SizingMode::Maximized => {
5306 origin.y += self.parent_area.loc.y;
5307 return origin;
5308 }
5309 SizingMode::Fullscreen => return origin,
5310 }
5311
5312 origin.y += self.working_area.loc.y + self.options.layout.gaps;
5313
5314 if self.display_mode == ColumnDisplay::Tabbed {
5315 origin += self
5316 .tab_indicator
5317 .content_offset(self.tiles.len(), self.scale);
5318 }
5319
5320 origin
5321 }
5322
5323 // HACK: pass a self.data iterator in manually as a workaround for the lack of method partial
5324 // borrowing. Note that this method's return value does not borrow the entire &Self!
5325 fn tile_offsets_iter(
5326 &self,
5327 data: impl Iterator<Item = TileData>,
5328 ) -> impl Iterator<Item = Point<f64, Logical>> {
5329 // FIXME: this should take into account always-center-single-column, which means that
5330 // Column should somehow know when it is being centered due to being the single column on
5331 // the workspace or some other reason.
5332 let center = self.options.layout.center_focused_column == CenterFocusedColumn::Always;
5333 let gaps = self.options.layout.gaps;
5334 let tabbed = self.display_mode == ColumnDisplay::Tabbed;
5335
5336 // Does not include extra size from the tab indicator.
5337 let tiles_width = self
5338 .data
5339 .iter()
5340 .map(|data| NotNan::new(data.size.w).unwrap())
5341 .max()
5342 .map(NotNan::into_inner)
5343 .unwrap_or(0.);
5344
5345 let mut origin = self.tiles_origin();
5346
5347 // Chain with a dummy value to be able to get one past all tiles' Y.
5348 let dummy = TileData {
5349 height: WindowHeight::auto_1(),
5350 size: Size::default(),
5351 interactively_resizing_by_left_edge: false,
5352 };
5353 let data = data.chain(iter::once(dummy));
5354
5355 data.map(move |data| {
5356 let mut pos = origin;
5357
5358 if center {
5359 pos.x += (tiles_width - data.size.w) / 2.;
5360 } else if data.interactively_resizing_by_left_edge {
5361 pos.x += tiles_width - data.size.w;
5362 }
5363
5364 if !tabbed {
5365 origin.y += data.size.h + gaps;
5366 }
5367
5368 pos
5369 })
5370 }
5371
5372 fn tile_offsets(&self) -> impl Iterator<Item = Point<f64, Logical>> + '_ {
5373 self.tile_offsets_iter(self.data.iter().copied())
5374 }
5375
5376 fn tile_offset(&self, tile_idx: usize) -> Point<f64, Logical> {
5377 self.tile_offsets().nth(tile_idx).unwrap()
5378 }
5379
5380 fn tile_offsets_in_render_order(
5381 &self,
5382 data: impl Iterator<Item = TileData>,
5383 ) -> impl Iterator<Item = Point<f64, Logical>> {
5384 let active_idx = self.active_tile_idx;
5385 let active_pos = self.tile_offset(active_idx);
5386 let offsets = self
5387 .tile_offsets_iter(data)
5388 .enumerate()
5389 .filter_map(move |(idx, pos)| (idx != active_idx).then_some(pos));
5390 iter::once(active_pos).chain(offsets)
5391 }
5392
5393 pub fn tiles(&self) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>)> + '_ {
5394 let offsets = self.tile_offsets_iter(self.data.iter().copied());
5395 zip(&self.tiles, offsets)
5396 }
5397
5398 fn tiles_mut(&mut self) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> + '_ {
5399 let offsets = self.tile_offsets_iter(self.data.iter().copied());
5400 zip(&mut self.tiles, offsets)
5401 }
5402
5403 fn tiles_in_render_order(
5404 &self,
5405 ) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>, bool)> + '_ {
5406 let offsets = self.tile_offsets_in_render_order(self.data.iter().copied());
5407
5408 let (first, rest) = self.tiles.split_at(self.active_tile_idx);
5409 let (active, rest) = rest.split_at(1);
5410
5411 let active = active.iter().map(|tile| (tile, true));
5412
5413 let rest_visible = self.display_mode != ColumnDisplay::Tabbed;
5414 let rest = first.iter().chain(rest);
5415 let rest = rest.map(move |tile| (tile, rest_visible));
5416
5417 let tiles = active.chain(rest);
5418 zip(tiles, offsets).map(|((tile, visible), pos)| (tile, pos, visible))
5419 }
5420
5421 fn tiles_in_render_order_mut(
5422 &mut self,
5423 ) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> + '_ {
5424 let offsets = self.tile_offsets_in_render_order(self.data.iter().copied());
5425
5426 let (first, rest) = self.tiles.split_at_mut(self.active_tile_idx);
5427 let (active, rest) = rest.split_at_mut(1);
5428
5429 let tiles = active.iter_mut().chain(first).chain(rest);
5430 zip(tiles, offsets)
5431 }
5432
5433 fn tab_indicator_area(&self) -> Rectangle<f64, Logical> {
5434 // We'd like to use the active tile's animated size for the tab indicator, however we need
5435 // to be mindful of the case where the active tile is smaller than some other tile in the
5436 // column. The column assumes the size of the largest tile.
5437 //
5438 // We expect users to mainly resize tabbed columns by width, so matching the animated size
5439 // is more important here. Besides, we always try to resize all windows in a column to the
5440 // same width when possible, and also the animation for going into tabbed mode doesn't move
5441 // tiles horizontally as much.
5442 //
5443 // For height though, it's a different story. First, users probably aren't resizing a
5444 // tabbed column by height. Second, we don't match windows by height, so it's easy to have
5445 // a smaller active tile than the rest of the column, e.g. by adding a fixed-size dialog.
5446 // Then, switching to that dialog and back should ideally keep the tab indicator position
5447 // fixed. Third, the animation for making a column tabbed moves tiles vertically, and using
5448 // the active tile's animated size in this case only works for the topmost tile, and looks
5449 // broken otherwise.
5450 let mut max_height = 0.;
5451 for tile in &self.tiles {
5452 max_height = f64::max(max_height, tile.tile_size().h);
5453 }
5454
5455 let tile = &self.tiles[self.active_tile_idx];
5456 let area_size = Size::from((tile.animated_tile_size().w, max_height));
5457
5458 Rectangle::new(self.tiles_origin(), area_size)
5459 }
5460
5461 pub fn start_open_animation(&mut self, id: &W::Id) -> bool {
5462 for tile in &mut self.tiles {
5463 if tile.window().id() == id {
5464 tile.start_open_animation();
5465
5466 // Animate the appearance of the tab indicator.
5467 if self.display_mode == ColumnDisplay::Tabbed
5468 && self.sizing_mode().is_normal()
5469 && self.tiles.len() == 1
5470 && !self.tab_indicator.config().hide_when_single_tab
5471 {
5472 self.tab_indicator.start_open_animation(
5473 self.clock.clone(),
5474 self.options.animations.window_open.anim,
5475 );
5476 }
5477
5478 return true;
5479 }
5480 }
5481
5482 false
5483 }
5484
5485 #[cfg(test)]
5486 fn verify_invariants(&self) {
5487 assert!(!self.tiles.is_empty(), "columns can't be empty");
5488 assert!(self.active_tile_idx < self.tiles.len());
5489 assert_eq!(self.tiles.len(), self.data.len());
5490
5491 if !self.pending_sizing_mode().is_normal() {
5492 assert!(self.tiles.len() == 1 || self.display_mode == ColumnDisplay::Tabbed);
5493 }
5494
5495 if let Some(idx) = self.preset_width_idx {
5496 assert!(idx < self.options.layout.preset_column_widths.len());
5497 }
5498
5499 let is_tabbed = self.display_mode == ColumnDisplay::Tabbed;
5500
5501 let tile_count = self.tiles.len();
5502 if tile_count == 1 {
5503 if let WindowHeight::Auto { weight } = self.data[0].height {
5504 assert_eq!(
5505 weight, 1.,
5506 "auto height weight must reset to 1 for a single window"
5507 );
5508 }
5509 }
5510
5511 let working_size = self.working_area.size;
5512 let extra_size = self.extra_size();
5513 let gaps = self.options.layout.gaps;
5514
5515 let mut found_fixed = false;
5516 let mut total_height = 0.;
5517 let mut total_min_height = 0.;
5518 for (tile, data) in zip(&self.tiles, &self.data) {
5519 assert!(Rc::ptr_eq(&self.options, &tile.options));
5520 assert_eq!(self.clock, tile.clock);
5521 assert_eq!(self.scale, tile.scale());
5522 assert_eq!(
5523 self.pending_sizing_mode(),
5524 tile.window().pending_sizing_mode()
5525 );
5526 assert_eq!(self.view_size, tile.view_size());
5527 tile.verify_invariants();
5528
5529 let mut data2 = *data;
5530 data2.update(tile);
5531 assert_eq!(data, &data2, "tile data must be up to date");
5532
5533 if matches!(data.height, WindowHeight::Fixed(_)) {
5534 assert!(
5535 !found_fixed,
5536 "there can only be one fixed-height window in a column"
5537 );
5538 found_fixed = true;
5539 }
5540
5541 if let WindowHeight::Preset(idx) = data.height {
5542 assert!(self.options.layout.preset_window_heights.len() > idx);
5543 }
5544
5545 let requested_size = tile.window().requested_size().unwrap();
5546 let requested_tile_height =
5547 tile.tile_height_for_window_height(f64::from(requested_size.h));
5548 let min_tile_height = f64::max(1., tile.min_size_nonfullscreen().h);
5549
5550 if !is_tabbed
5551 && self.pending_sizing_mode().is_normal()
5552 && self.scale.round() == self.scale
5553 && working_size.h.round() == working_size.h
5554 && gaps.round() == gaps
5555 {
5556 let total_height = requested_tile_height + gaps * 2. + extra_size.h;
5557 let total_min_height = min_tile_height + gaps * 2. + extra_size.h;
5558 let max_height = f64::max(total_min_height, working_size.h);
5559 assert!(
5560 total_height <= max_height,
5561 "each tile in a column mustn't go beyond working area height \
5562 (tile height {total_height} > max height {max_height})"
5563 );
5564 }
5565
5566 total_height += requested_tile_height;
5567 total_min_height += min_tile_height;
5568 }
5569
5570 if !is_tabbed
5571 && tile_count > 1
5572 && self.scale.round() == self.scale
5573 && working_size.h.round() == working_size.h
5574 && gaps.round() == gaps
5575 {
5576 total_height += gaps * (tile_count + 1) as f64 + extra_size.h;
5577 total_min_height += gaps * (tile_count + 1) as f64 + extra_size.h;
5578 let max_height = f64::max(total_min_height, working_size.h);
5579 assert!(
5580 total_height <= max_height,
5581 "multiple tiles in a column mustn't go beyond working area height \
5582 (total height {total_height} > max height {max_height})"
5583 );
5584 }
5585 }
5586}
5587
5588fn compute_new_view_offset(
5589 cur_x: f64,
5590 view_width: f64,
5591 new_col_x: f64,
5592 new_col_width: f64,
5593 gaps: f64,
5594) -> f64 {
5595 // If the column is wider than the view, always left-align it.
5596 if view_width <= new_col_width {
5597 return 0.;
5598 }
5599
5600 // Compute the padding in case it needs to be smaller due to large tile width.
5601 let padding = ((view_width - new_col_width) / 2.).clamp(0., gaps);
5602
5603 // Compute the desired new X with padding.
5604 let new_x = new_col_x - padding;
5605 let new_right_x = new_col_x + new_col_width + padding;
5606
5607 // If the column is already fully visible, leave the view as is.
5608 if cur_x <= new_x && new_right_x <= cur_x + view_width {
5609 return -(new_col_x - cur_x);
5610 }
5611
5612 // Otherwise, prefer the alignment that results in less motion from the current position.
5613 let dist_to_left = (cur_x - new_x).abs();
5614 let dist_to_right = ((cur_x + view_width) - new_right_x).abs();
5615 if dist_to_left <= dist_to_right {
5616 -padding
5617 } else {
5618 -(view_width - padding - new_col_width)
5619 }
5620}
5621
5622fn compute_working_area(
5623 parent_area: Rectangle<f64, Logical>,
5624 scale: f64,
5625 struts: Struts,
5626) -> Rectangle<f64, Logical> {
5627 let mut working_area = parent_area;
5628
5629 // Add struts.
5630 working_area.size.w = f64::max(0., working_area.size.w - struts.left.0 - struts.right.0);
5631 working_area.loc.x += struts.left.0;
5632
5633 working_area.size.h = f64::max(0., working_area.size.h - struts.top.0 - struts.bottom.0);
5634 working_area.loc.y += struts.top.0;
5635
5636 // Round location to start at a physical pixel.
5637 let loc = working_area
5638 .loc
5639 .to_physical_precise_ceil(scale)
5640 .to_logical(scale);
5641
5642 let mut size_diff = (loc - working_area.loc).to_size();
5643 size_diff.w = f64::min(working_area.size.w, size_diff.w);
5644 size_diff.h = f64::min(working_area.size.h, size_diff.h);
5645
5646 working_area.size -= size_diff;
5647 working_area.loc = loc;
5648
5649 working_area
5650}
5651
5652fn compute_toplevel_bounds(
5653 border_config: niri_config::Border,
5654 working_area_size: Size<f64, Logical>,
5655 extra_size: Size<f64, Logical>,
5656 gaps: f64,
5657) -> Size<i32, Logical> {
5658 let mut border = 0.;
5659 if !border_config.off {
5660 border = border_config.width * 2.;
5661 }
5662
5663 Size::from((
5664 f64::max(working_area_size.w - gaps * 2. - extra_size.w - border, 1.),
5665 f64::max(working_area_size.h - gaps * 2. - extra_size.h - border, 1.),
5666 ))
5667 .to_i32_floor()
5668}
5669
5670fn cancel_resize_for_column<W: LayoutElement>(
5671 interactive_resize: &mut Option<InteractiveResize<W>>,
5672 column: &mut Column<W>,
5673) {
5674 if let Some(resize) = interactive_resize {
5675 if column.contains(&resize.window) {
5676 *interactive_resize = None;
5677 }
5678 }
5679
5680 for tile in &mut column.tiles {
5681 tile.window_mut().cancel_interactive_resize();
5682 }
5683}
5684
5685fn resolve_preset_size(
5686 preset: PresetSize,
5687 options: &Options,
5688 view_size: f64,
5689 extra_size: f64,
5690) -> ResolvedSize {
5691 match preset {
5692 PresetSize::Proportion(proportion) => ResolvedSize::Tile(
5693 (view_size - options.layout.gaps) * proportion - options.layout.gaps - extra_size,
5694 ),
5695 PresetSize::Fixed(width) => ResolvedSize::Window(f64::from(width)),
5696 }
5697}
5698
5699#[cfg(test)]
5700mod tests {
5701 use niri_config::FloatOrInt;
5702
5703 use super::*;
5704 use crate::utils::round_logical_in_physical;
5705
5706 #[test]
5707 fn working_area_starts_at_physical_pixel() {
5708 let struts = Struts {
5709 left: FloatOrInt(0.5),
5710 right: FloatOrInt(1.),
5711 top: FloatOrInt(0.75),
5712 bottom: FloatOrInt(1.),
5713 };
5714
5715 let parent_area = Rectangle::from_size(Size::from((1280., 720.)));
5716 let area = compute_working_area(parent_area, 1., struts);
5717
5718 assert_eq!(round_logical_in_physical(1., area.loc.x), area.loc.x);
5719 assert_eq!(round_logical_in_physical(1., area.loc.y), area.loc.y);
5720 }
5721
5722 #[test]
5723 fn large_fractional_strut() {
5724 let struts = Struts {
5725 left: FloatOrInt(0.),
5726 right: FloatOrInt(0.),
5727 top: FloatOrInt(50000.5),
5728 bottom: FloatOrInt(0.),
5729 };
5730
5731 let parent_area = Rectangle::from_size(Size::from((1280., 720.)));
5732 compute_working_area(parent_area, 1., struts);
5733 }
5734}