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