niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9ff2f83·Ivan Molodetskikh·1 year ago·2099 commits
| 1 | use std::cmp::min; |
| 2 | use std::iter::zip; |
| 3 | use std::rc::Rc; |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | use niri_config::CornerRadius; |
| 7 | use smithay::backend::renderer::element::utils::{ |
| 8 | CropRenderElement, Relocate, RelocateRenderElement, RescaleRenderElement, |
| 9 | }; |
| 10 | use smithay::output::Output; |
| 11 | use smithay::utils::{Logical, Point, Rectangle, Size}; |
| 12 | |
| 13 | use super::insert_hint_element::{InsertHintElement, InsertHintRenderElement}; |
| 14 | use super::scrolling::{Column, ColumnWidth}; |
| 15 | use super::tile::Tile; |
| 16 | use super::workspace::{ |
| 17 | compute_working_area, OutputId, Workspace, WorkspaceAddWindowTarget, WorkspaceId, |
| 18 | WorkspaceRenderElement, |
| 19 | }; |
| 20 | use super::{compute_overview_zoom, ActivateWindow, HitType, LayoutElement, Options}; |
| 21 | use crate::animation::{Animation, Clock}; |
| 22 | use crate::input::swipe_tracker::SwipeTracker; |
| 23 | use crate::niri_render_elements; |
| 24 | use crate::render_helpers::renderer::NiriRenderer; |
| 25 | use crate::render_helpers::shadow::ShadowRenderElement; |
| 26 | use crate::render_helpers::RenderTarget; |
| 27 | use crate::rubber_band::RubberBand; |
| 28 | use crate::utils::transaction::Transaction; |
| 29 | use crate::utils::{ |
| 30 | output_size, round_logical_in_physical, round_logical_in_physical_max1, ResizeEdge, |
| 31 | }; |
| 32 | |
| 33 | /// Amount of touchpad movement to scroll the height of one workspace. |
| 34 | const WORKSPACE_GESTURE_MOVEMENT: f64 = 300.; |
| 35 | |
| 36 | const WORKSPACE_GESTURE_RUBBER_BAND: RubberBand = RubberBand { |
| 37 | stiffness: 0.5, |
| 38 | limit: 0.05, |
| 39 | }; |
| 40 | |
| 41 | /// Amount of DnD edge scrolling to scroll the height of one workspace. |
| 42 | /// |
| 43 | /// This constant is tied to the default dnd-edge-workspace-switch max-speed setting. |
| 44 | const WORKSPACE_DND_EDGE_SCROLL_MOVEMENT: f64 = 1500.; |
| 45 | |
| 46 | #[derive(Debug)] |
| 47 | pub struct Monitor<W: LayoutElement> { |
| 48 | /// Output for this monitor. |
| 49 | pub(super) output: Output, |
| 50 | /// Cached name of the output. |
| 51 | output_name: String, |
| 52 | /// Latest known scale for this output. |
| 53 | scale: smithay::output::Scale, |
| 54 | /// Latest known size for this output. |
| 55 | view_size: Size<f64, Logical>, |
| 56 | /// Latest known working area for this output. |
| 57 | /// |
| 58 | /// Not rounded to physical pixels. |
| 59 | // FIXME: since this is used for things like DnD scrolling edges in the overview, ideally this |
| 60 | // should only consider overlay and top layer-shell surfaces. However, Smithay doesn't easily |
| 61 | // let you do this at the moment. |
| 62 | working_area: Rectangle<f64, Logical>, |
| 63 | // Must always contain at least one. |
| 64 | pub(super) workspaces: Vec<Workspace<W>>, |
| 65 | /// Index of the currently active workspace. |
| 66 | pub(super) active_workspace_idx: usize, |
| 67 | /// ID of the previously active workspace. |
| 68 | pub(super) previous_workspace_id: Option<WorkspaceId>, |
| 69 | /// In-progress switch between workspaces. |
| 70 | pub(super) workspace_switch: Option<WorkspaceSwitch>, |
| 71 | /// Indication where an interactively-moved window is about to be placed. |
| 72 | pub(super) insert_hint: Option<InsertHint>, |
| 73 | /// Insert hint element for rendering. |
| 74 | insert_hint_element: InsertHintElement, |
| 75 | /// Location to render the insert hint element. |
| 76 | insert_hint_render_loc: Option<InsertHintRenderLoc>, |
| 77 | /// Whether the overview is open. |
| 78 | pub(super) overview_open: bool, |
| 79 | /// Progress of the overview zoom animation, 1 is fully in overview. |
| 80 | overview_progress: Option<OverviewProgress>, |
| 81 | /// Clock for driving animations. |
| 82 | pub(super) clock: Clock, |
| 83 | /// Configurable properties of the layout. |
| 84 | pub(super) options: Rc<Options>, |
| 85 | } |
| 86 | |
| 87 | #[derive(Debug)] |
| 88 | pub enum WorkspaceSwitch { |
| 89 | Animation(Animation), |
| 90 | Gesture(WorkspaceSwitchGesture), |
| 91 | } |
| 92 | |
| 93 | #[derive(Debug)] |
| 94 | pub struct WorkspaceSwitchGesture { |
| 95 | /// Index of the workspace where the gesture was started. |
| 96 | center_idx: usize, |
| 97 | /// Fractional workspace index where the gesture was started. |
| 98 | /// |
| 99 | /// Can differ from center_idx when starting a gesture in the middle between workspaces, for |
| 100 | /// example by "catching" an animation. |
| 101 | start_idx: f64, |
| 102 | /// Current, fractional workspace index. |
| 103 | pub(super) current_idx: f64, |
| 104 | /// Animation for the extra offset to the current position. |
| 105 | /// |
| 106 | /// For example, if there's a workspace switch during a DnD scroll. |
| 107 | animation: Option<Animation>, |
| 108 | tracker: SwipeTracker, |
| 109 | /// Whether the gesture is controlled by the touchpad. |
| 110 | is_touchpad: bool, |
| 111 | /// Whether the gesture is clamped to +-1 workspace around the center. |
| 112 | is_clamped: bool, |
| 113 | |
| 114 | // If this gesture is for drag-and-drop scrolling, this is the last event's unadjusted |
| 115 | // timestamp. |
| 116 | dnd_last_event_time: Option<Duration>, |
| 117 | // Time when the drag-and-drop scroll delta became non-zero, used for debouncing. |
| 118 | // |
| 119 | // If `None` then the scroll delta is currently zero. |
| 120 | dnd_nonzero_start_time: Option<Duration>, |
| 121 | } |
| 122 | |
| 123 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 124 | pub(super) enum InsertPosition { |
| 125 | NewColumn(usize), |
| 126 | InColumn(usize, usize), |
| 127 | Floating, |
| 128 | } |
| 129 | |
| 130 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 131 | pub(super) enum InsertWorkspace { |
| 132 | Existing(WorkspaceId), |
| 133 | NewAt(usize), |
| 134 | } |
| 135 | |
| 136 | #[derive(Debug)] |
| 137 | pub(super) struct InsertHint { |
| 138 | pub workspace: InsertWorkspace, |
| 139 | pub position: InsertPosition, |
| 140 | pub corner_radius: CornerRadius, |
| 141 | } |
| 142 | |
| 143 | #[derive(Debug, Clone, Copy)] |
| 144 | struct InsertHintRenderLoc { |
| 145 | workspace: InsertWorkspace, |
| 146 | location: Point<f64, Logical>, |
| 147 | } |
| 148 | |
| 149 | #[derive(Debug)] |
| 150 | pub(super) enum OverviewProgress { |
| 151 | Animation(Animation), |
| 152 | Value(f64), |
| 153 | } |
| 154 | |
| 155 | /// Where to put a newly added window. |
| 156 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 157 | pub enum MonitorAddWindowTarget<'a, W: LayoutElement> { |
| 158 | /// No particular preference. |
| 159 | #[default] |
| 160 | Auto, |
| 161 | /// On this workspace. |
| 162 | Workspace { |
| 163 | /// Id of the target workspace. |
| 164 | id: WorkspaceId, |
| 165 | /// Override where the window will open as a new column. |
| 166 | column_idx: Option<usize>, |
| 167 | }, |
| 168 | /// Next to this existing window. |
| 169 | NextTo(&'a W::Id), |
| 170 | } |
| 171 | |
| 172 | niri_render_elements! { |
| 173 | MonitorInnerRenderElement<R> => { |
| 174 | Workspace = CropRenderElement<WorkspaceRenderElement<R>>, |
| 175 | InsertHint = CropRenderElement<InsertHintRenderElement>, |
| 176 | UncroppedInsertHint = InsertHintRenderElement, |
| 177 | Shadow = ShadowRenderElement, |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | pub type MonitorRenderElement<R> = |
| 182 | RelocateRenderElement<RescaleRenderElement<MonitorInnerRenderElement<R>>>; |
| 183 | |
| 184 | impl WorkspaceSwitch { |
| 185 | pub fn current_idx(&self) -> f64 { |
| 186 | match self { |
| 187 | WorkspaceSwitch::Animation(anim) => anim.value(), |
| 188 | WorkspaceSwitch::Gesture(gesture) => { |
| 189 | gesture.current_idx + gesture.animation.as_ref().map_or(0., |anim| anim.value()) |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | pub fn target_idx(&self) -> f64 { |
| 195 | match self { |
| 196 | WorkspaceSwitch::Animation(anim) => anim.to(), |
| 197 | WorkspaceSwitch::Gesture(gesture) => gesture.current_idx, |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | pub fn offset(&mut self, delta: isize) { |
| 202 | match self { |
| 203 | WorkspaceSwitch::Animation(anim) => anim.offset(delta as f64), |
| 204 | WorkspaceSwitch::Gesture(gesture) => { |
| 205 | if delta >= 0 { |
| 206 | gesture.center_idx += delta as usize; |
| 207 | } else { |
| 208 | gesture.center_idx -= (-delta) as usize; |
| 209 | } |
| 210 | gesture.start_idx += delta as f64; |
| 211 | gesture.current_idx += delta as f64; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | fn is_animation_ongoing(&self) -> bool { |
| 217 | match self { |
| 218 | WorkspaceSwitch::Animation(_) => true, |
| 219 | WorkspaceSwitch::Gesture(gesture) => gesture.animation.is_some(), |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | impl WorkspaceSwitchGesture { |
| 225 | fn min_max(&self, workspace_count: usize) -> (f64, f64) { |
| 226 | if self.is_clamped { |
| 227 | let min = self.center_idx.saturating_sub(1) as f64; |
| 228 | let max = (self.center_idx + 1).min(workspace_count - 1) as f64; |
| 229 | (min, max) |
| 230 | } else { |
| 231 | (0., (workspace_count - 1) as f64) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | fn animate_from(&mut self, from: f64, clock: Clock, config: niri_config::Animation) { |
| 236 | let current = self.animation.as_ref().map_or(0., Animation::value); |
| 237 | self.animation = Some(Animation::new(clock, from + current, 0., 0., config)); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | impl InsertWorkspace { |
| 242 | fn existing_id(self) -> Option<WorkspaceId> { |
| 243 | match self { |
| 244 | InsertWorkspace::Existing(id) => Some(id), |
| 245 | InsertWorkspace::NewAt(_) => None, |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | impl OverviewProgress { |
| 251 | pub fn value(&self) -> f64 { |
| 252 | match self { |
| 253 | OverviewProgress::Animation(anim) => anim.value(), |
| 254 | OverviewProgress::Value(v) => *v, |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | pub fn clamped_value(&self) -> f64 { |
| 259 | match self { |
| 260 | OverviewProgress::Animation(anim) => anim.clamped_value(), |
| 261 | OverviewProgress::Value(v) => *v, |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | impl From<&super::OverviewProgress> for OverviewProgress { |
| 267 | fn from(value: &super::OverviewProgress) -> Self { |
| 268 | match value { |
| 269 | super::OverviewProgress::Animation(anim) => Self::Animation(anim.clone()), |
| 270 | super::OverviewProgress::Gesture(gesture) => Self::Value(gesture.value), |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | impl<W: LayoutElement> Monitor<W> { |
| 276 | pub fn new( |
| 277 | output: Output, |
| 278 | workspaces: Vec<Workspace<W>>, |
| 279 | clock: Clock, |
| 280 | options: Rc<Options>, |
| 281 | ) -> Self { |
| 282 | let scale = output.current_scale(); |
| 283 | let view_size = output_size(&output); |
| 284 | let working_area = compute_working_area(&output); |
| 285 | |
| 286 | Self { |
| 287 | output_name: output.name(), |
| 288 | output, |
| 289 | scale, |
| 290 | view_size, |
| 291 | working_area, |
| 292 | workspaces, |
| 293 | active_workspace_idx: 0, |
| 294 | previous_workspace_id: None, |
| 295 | insert_hint: None, |
| 296 | insert_hint_element: InsertHintElement::new(options.insert_hint), |
| 297 | insert_hint_render_loc: None, |
| 298 | overview_open: false, |
| 299 | overview_progress: None, |
| 300 | workspace_switch: None, |
| 301 | clock, |
| 302 | options, |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | pub fn output(&self) -> &Output { |
| 307 | &self.output |
| 308 | } |
| 309 | |
| 310 | pub fn output_name(&self) -> &String { |
| 311 | &self.output_name |
| 312 | } |
| 313 | |
| 314 | pub fn active_workspace_idx(&self) -> usize { |
| 315 | self.active_workspace_idx |
| 316 | } |
| 317 | |
| 318 | pub fn active_workspace_ref(&self) -> &Workspace<W> { |
| 319 | &self.workspaces[self.active_workspace_idx] |
| 320 | } |
| 321 | |
| 322 | pub fn find_named_workspace(&self, workspace_name: &str) -> Option<&Workspace<W>> { |
| 323 | self.workspaces.iter().find(|ws| { |
| 324 | ws.name |
| 325 | .as_ref() |
| 326 | .is_some_and(|name| name.eq_ignore_ascii_case(workspace_name)) |
| 327 | }) |
| 328 | } |
| 329 | |
| 330 | pub fn find_named_workspace_index(&self, workspace_name: &str) -> Option<usize> { |
| 331 | self.workspaces.iter().position(|ws| { |
| 332 | ws.name |
| 333 | .as_ref() |
| 334 | .is_some_and(|name| name.eq_ignore_ascii_case(workspace_name)) |
| 335 | }) |
| 336 | } |
| 337 | |
| 338 | pub fn active_workspace(&mut self) -> &mut Workspace<W> { |
| 339 | &mut self.workspaces[self.active_workspace_idx] |
| 340 | } |
| 341 | |
| 342 | pub fn windows(&self) -> impl Iterator<Item = &W> { |
| 343 | self.workspaces.iter().flat_map(|ws| ws.windows()) |
| 344 | } |
| 345 | |
| 346 | pub fn has_window(&self, window: &W::Id) -> bool { |
| 347 | self.windows().any(|win| win.id() == window) |
| 348 | } |
| 349 | |
| 350 | pub fn add_workspace_at(&mut self, idx: usize) { |
| 351 | let ws = Workspace::new( |
| 352 | self.output.clone(), |
| 353 | self.clock.clone(), |
| 354 | self.options.clone(), |
| 355 | ); |
| 356 | |
| 357 | self.workspaces.insert(idx, ws); |
| 358 | if idx <= self.active_workspace_idx { |
| 359 | self.active_workspace_idx += 1; |
| 360 | } |
| 361 | |
| 362 | if let Some(switch) = &mut self.workspace_switch { |
| 363 | if idx as f64 <= switch.target_idx() { |
| 364 | switch.offset(1); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | pub fn add_workspace_top(&mut self) { |
| 370 | self.add_workspace_at(0); |
| 371 | } |
| 372 | |
| 373 | pub fn add_workspace_bottom(&mut self) { |
| 374 | self.add_workspace_at(self.workspaces.len()); |
| 375 | } |
| 376 | |
| 377 | pub fn activate_workspace(&mut self, idx: usize) { |
| 378 | self.activate_workspace_with_anim_config(idx, None); |
| 379 | } |
| 380 | |
| 381 | pub fn activate_workspace_with_anim_config( |
| 382 | &mut self, |
| 383 | idx: usize, |
| 384 | config: Option<niri_config::Animation>, |
| 385 | ) { |
| 386 | // FIXME: also compute and use current velocity. |
| 387 | let current_idx = self.workspace_render_idx(); |
| 388 | |
| 389 | if self.active_workspace_idx != idx { |
| 390 | self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id()); |
| 391 | } |
| 392 | |
| 393 | let prev_active_idx = self.active_workspace_idx; |
| 394 | self.active_workspace_idx = idx; |
| 395 | |
| 396 | let config = config.unwrap_or(self.options.animations.workspace_switch.0); |
| 397 | |
| 398 | match &mut self.workspace_switch { |
| 399 | // During a DnD scroll, we want to visually animate even if idx matches the active idx. |
| 400 | Some(WorkspaceSwitch::Gesture(gesture)) if gesture.dnd_last_event_time.is_some() => { |
| 401 | gesture.center_idx = idx; |
| 402 | |
| 403 | // Adjust start_idx to make current_idx point at idx. |
| 404 | let current_pos = gesture.current_idx - gesture.start_idx; |
| 405 | gesture.start_idx = idx as f64 - current_pos; |
| 406 | let prev_current_idx = gesture.current_idx; |
| 407 | gesture.current_idx = idx as f64; |
| 408 | |
| 409 | let current_idx_delta = gesture.current_idx - prev_current_idx; |
| 410 | gesture.animate_from(-current_idx_delta, self.clock.clone(), config); |
| 411 | } |
| 412 | _ => { |
| 413 | // Don't animate if nothing changed. |
| 414 | if prev_active_idx == idx { |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new( |
| 419 | self.clock.clone(), |
| 420 | current_idx, |
| 421 | idx as f64, |
| 422 | 0., |
| 423 | config, |
| 424 | ))); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | pub fn add_window( |
| 430 | &mut self, |
| 431 | window: W, |
| 432 | target: MonitorAddWindowTarget<W>, |
| 433 | activate: ActivateWindow, |
| 434 | width: ColumnWidth, |
| 435 | is_full_width: bool, |
| 436 | is_floating: bool, |
| 437 | ) { |
| 438 | // Currently, everything a workspace sets on a Tile is the same across all workspaces of a |
| 439 | // monitor. So we can use any workspace, not necessarily the exact target workspace. |
| 440 | let tile = self.workspaces[0].make_tile(window); |
| 441 | |
| 442 | self.add_tile( |
| 443 | tile, |
| 444 | target, |
| 445 | activate, |
| 446 | true, |
| 447 | width, |
| 448 | is_full_width, |
| 449 | is_floating, |
| 450 | ); |
| 451 | } |
| 452 | |
| 453 | pub fn add_column(&mut self, mut workspace_idx: usize, column: Column<W>, activate: bool) { |
| 454 | let workspace = &mut self.workspaces[workspace_idx]; |
| 455 | |
| 456 | workspace.add_column(column, activate); |
| 457 | |
| 458 | // After adding a new window, workspace becomes this output's own. |
| 459 | if workspace.name().is_none() { |
| 460 | workspace.original_output = OutputId::new(&self.output); |
| 461 | } |
| 462 | |
| 463 | if workspace_idx == self.workspaces.len() - 1 { |
| 464 | self.add_workspace_bottom(); |
| 465 | } |
| 466 | if self.options.empty_workspace_above_first && workspace_idx == 0 { |
| 467 | self.add_workspace_top(); |
| 468 | workspace_idx += 1; |
| 469 | } |
| 470 | |
| 471 | if activate { |
| 472 | self.activate_workspace(workspace_idx); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | #[allow(clippy::too_many_arguments)] |
| 477 | pub fn add_tile( |
| 478 | &mut self, |
| 479 | tile: Tile<W>, |
| 480 | target: MonitorAddWindowTarget<W>, |
| 481 | activate: ActivateWindow, |
| 482 | // FIXME: Refactor ActivateWindow enum to make this better. |
| 483 | allow_to_activate_workspace: bool, |
| 484 | width: ColumnWidth, |
| 485 | is_full_width: bool, |
| 486 | is_floating: bool, |
| 487 | ) { |
| 488 | let (mut workspace_idx, target) = match target { |
| 489 | MonitorAddWindowTarget::Auto => { |
| 490 | (self.active_workspace_idx, WorkspaceAddWindowTarget::Auto) |
| 491 | } |
| 492 | MonitorAddWindowTarget::Workspace { id, column_idx } => { |
| 493 | let idx = self.workspaces.iter().position(|ws| ws.id() == id).unwrap(); |
| 494 | let target = if let Some(column_idx) = column_idx { |
| 495 | WorkspaceAddWindowTarget::NewColumnAt(column_idx) |
| 496 | } else { |
| 497 | WorkspaceAddWindowTarget::Auto |
| 498 | }; |
| 499 | (idx, target) |
| 500 | } |
| 501 | MonitorAddWindowTarget::NextTo(win_id) => { |
| 502 | let idx = self |
| 503 | .workspaces |
| 504 | .iter_mut() |
| 505 | .position(|ws| ws.has_window(win_id)) |
| 506 | .unwrap(); |
| 507 | (idx, WorkspaceAddWindowTarget::NextTo(win_id)) |
| 508 | } |
| 509 | }; |
| 510 | |
| 511 | let workspace = &mut self.workspaces[workspace_idx]; |
| 512 | |
| 513 | workspace.add_tile(tile, target, activate, width, is_full_width, is_floating); |
| 514 | |
| 515 | // After adding a new window, workspace becomes this output's own. |
| 516 | if workspace.name().is_none() { |
| 517 | workspace.original_output = OutputId::new(&self.output); |
| 518 | } |
| 519 | |
| 520 | if workspace_idx == self.workspaces.len() - 1 { |
| 521 | // Insert a new empty workspace. |
| 522 | self.add_workspace_bottom(); |
| 523 | } |
| 524 | |
| 525 | if self.options.empty_workspace_above_first && workspace_idx == 0 { |
| 526 | self.add_workspace_top(); |
| 527 | workspace_idx += 1; |
| 528 | } |
| 529 | |
| 530 | if allow_to_activate_workspace && activate.map_smart(|| false) { |
| 531 | self.activate_workspace(workspace_idx); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | pub fn add_tile_to_column( |
| 536 | &mut self, |
| 537 | workspace_idx: usize, |
| 538 | column_idx: usize, |
| 539 | tile_idx: Option<usize>, |
| 540 | tile: Tile<W>, |
| 541 | activate: bool, |
| 542 | // FIXME: Refactor ActivateWindow enum to make this better. |
| 543 | allow_to_activate_workspace: bool, |
| 544 | ) { |
| 545 | let workspace = &mut self.workspaces[workspace_idx]; |
| 546 | |
| 547 | workspace.add_tile_to_column(column_idx, tile_idx, tile, activate); |
| 548 | |
| 549 | // After adding a new window, workspace becomes this output's own. |
| 550 | if workspace.name().is_none() { |
| 551 | workspace.original_output = OutputId::new(&self.output); |
| 552 | } |
| 553 | |
| 554 | // Since we're adding window to an existing column, the workspace isn't empty, and |
| 555 | // therefore cannot be the last one, so we never need to insert a new empty workspace. |
| 556 | |
| 557 | if allow_to_activate_workspace && activate { |
| 558 | self.activate_workspace(workspace_idx); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | pub fn clean_up_workspaces(&mut self) { |
| 563 | assert!(self.workspace_switch.is_none()); |
| 564 | |
| 565 | let range_start = if self.options.empty_workspace_above_first { |
| 566 | 1 |
| 567 | } else { |
| 568 | 0 |
| 569 | }; |
| 570 | for idx in (range_start..self.workspaces.len() - 1).rev() { |
| 571 | if self.active_workspace_idx == idx { |
| 572 | continue; |
| 573 | } |
| 574 | |
| 575 | if !self.workspaces[idx].has_windows_or_name() { |
| 576 | self.workspaces.remove(idx); |
| 577 | if self.active_workspace_idx > idx { |
| 578 | self.active_workspace_idx -= 1; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | // Special case handling when empty_workspace_above_first is set and all workspaces |
| 584 | // are empty. |
| 585 | if self.options.empty_workspace_above_first && self.workspaces.len() == 2 { |
| 586 | assert!(!self.workspaces[0].has_windows_or_name()); |
| 587 | assert!(!self.workspaces[1].has_windows_or_name()); |
| 588 | self.workspaces.remove(1); |
| 589 | self.active_workspace_idx = 0; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | pub fn unname_workspace(&mut self, id: WorkspaceId) -> bool { |
| 594 | let Some(ws) = self.workspaces.iter_mut().find(|ws| ws.id() == id) else { |
| 595 | return false; |
| 596 | }; |
| 597 | |
| 598 | ws.unname(); |
| 599 | true |
| 600 | } |
| 601 | |
| 602 | pub fn move_down_or_to_workspace_down(&mut self) { |
| 603 | if !self.active_workspace().move_down() { |
| 604 | self.move_to_workspace_down(); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | pub fn move_up_or_to_workspace_up(&mut self) { |
| 609 | if !self.active_workspace().move_up() { |
| 610 | self.move_to_workspace_up(); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | pub fn focus_window_or_workspace_down(&mut self) { |
| 615 | if !self.active_workspace().focus_down() { |
| 616 | self.switch_workspace_down(); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | pub fn focus_window_or_workspace_up(&mut self) { |
| 621 | if !self.active_workspace().focus_up() { |
| 622 | self.switch_workspace_up(); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | pub fn move_to_workspace_up(&mut self) { |
| 627 | let source_workspace_idx = self.active_workspace_idx; |
| 628 | |
| 629 | let new_idx = source_workspace_idx.saturating_sub(1); |
| 630 | if new_idx == source_workspace_idx { |
| 631 | return; |
| 632 | } |
| 633 | let new_id = self.workspaces[new_idx].id(); |
| 634 | |
| 635 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 636 | let Some(removed) = workspace.remove_active_tile(Transaction::new()) else { |
| 637 | return; |
| 638 | }; |
| 639 | |
| 640 | self.add_tile( |
| 641 | removed.tile, |
| 642 | MonitorAddWindowTarget::Workspace { |
| 643 | id: new_id, |
| 644 | column_idx: None, |
| 645 | }, |
| 646 | ActivateWindow::Yes, |
| 647 | true, |
| 648 | removed.width, |
| 649 | removed.is_full_width, |
| 650 | removed.is_floating, |
| 651 | ); |
| 652 | } |
| 653 | |
| 654 | pub fn move_to_workspace_down(&mut self) { |
| 655 | let source_workspace_idx = self.active_workspace_idx; |
| 656 | |
| 657 | let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1); |
| 658 | if new_idx == source_workspace_idx { |
| 659 | return; |
| 660 | } |
| 661 | let new_id = self.workspaces[new_idx].id(); |
| 662 | |
| 663 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 664 | let Some(removed) = workspace.remove_active_tile(Transaction::new()) else { |
| 665 | return; |
| 666 | }; |
| 667 | |
| 668 | self.add_tile( |
| 669 | removed.tile, |
| 670 | MonitorAddWindowTarget::Workspace { |
| 671 | id: new_id, |
| 672 | column_idx: None, |
| 673 | }, |
| 674 | ActivateWindow::Yes, |
| 675 | true, |
| 676 | removed.width, |
| 677 | removed.is_full_width, |
| 678 | removed.is_floating, |
| 679 | ); |
| 680 | } |
| 681 | |
| 682 | pub fn move_to_workspace( |
| 683 | &mut self, |
| 684 | window: Option<&W::Id>, |
| 685 | idx: usize, |
| 686 | activate: ActivateWindow, |
| 687 | ) { |
| 688 | let source_workspace_idx = if let Some(window) = window { |
| 689 | self.workspaces |
| 690 | .iter() |
| 691 | .position(|ws| ws.has_window(window)) |
| 692 | .unwrap() |
| 693 | } else { |
| 694 | self.active_workspace_idx |
| 695 | }; |
| 696 | |
| 697 | let new_idx = min(idx, self.workspaces.len() - 1); |
| 698 | if new_idx == source_workspace_idx { |
| 699 | return; |
| 700 | } |
| 701 | let new_id = self.workspaces[new_idx].id(); |
| 702 | |
| 703 | let activate = activate.map_smart(|| { |
| 704 | window.map_or(true, |win| { |
| 705 | self.active_window().map(|win| win.id()) == Some(win) |
| 706 | }) |
| 707 | }); |
| 708 | |
| 709 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 710 | let transaction = Transaction::new(); |
| 711 | let removed = if let Some(window) = window { |
| 712 | workspace.remove_tile(window, transaction) |
| 713 | } else if let Some(removed) = workspace.remove_active_tile(transaction) { |
| 714 | removed |
| 715 | } else { |
| 716 | return; |
| 717 | }; |
| 718 | |
| 719 | self.add_tile( |
| 720 | removed.tile, |
| 721 | MonitorAddWindowTarget::Workspace { |
| 722 | id: new_id, |
| 723 | column_idx: None, |
| 724 | }, |
| 725 | if activate { |
| 726 | ActivateWindow::Yes |
| 727 | } else { |
| 728 | ActivateWindow::No |
| 729 | }, |
| 730 | true, |
| 731 | removed.width, |
| 732 | removed.is_full_width, |
| 733 | removed.is_floating, |
| 734 | ); |
| 735 | |
| 736 | if self.workspace_switch.is_none() { |
| 737 | self.clean_up_workspaces(); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | pub fn move_column_to_workspace_up(&mut self, activate: bool) { |
| 742 | let source_workspace_idx = self.active_workspace_idx; |
| 743 | |
| 744 | let new_idx = source_workspace_idx.saturating_sub(1); |
| 745 | if new_idx == source_workspace_idx { |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 750 | if workspace.floating_is_active() { |
| 751 | self.move_to_workspace_up(); |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | let Some(column) = workspace.remove_active_column() else { |
| 756 | return; |
| 757 | }; |
| 758 | |
| 759 | self.add_column(new_idx, column, activate); |
| 760 | } |
| 761 | |
| 762 | pub fn move_column_to_workspace_down(&mut self, activate: bool) { |
| 763 | let source_workspace_idx = self.active_workspace_idx; |
| 764 | |
| 765 | let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1); |
| 766 | if new_idx == source_workspace_idx { |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 771 | if workspace.floating_is_active() { |
| 772 | self.move_to_workspace_down(); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | let Some(column) = workspace.remove_active_column() else { |
| 777 | return; |
| 778 | }; |
| 779 | |
| 780 | self.add_column(new_idx, column, activate); |
| 781 | } |
| 782 | |
| 783 | pub fn move_column_to_workspace(&mut self, idx: usize, activate: bool) { |
| 784 | let source_workspace_idx = self.active_workspace_idx; |
| 785 | |
| 786 | let new_idx = min(idx, self.workspaces.len() - 1); |
| 787 | if new_idx == source_workspace_idx { |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 792 | if workspace.floating_is_active() { |
| 793 | self.move_to_workspace(None, idx, ActivateWindow::Smart); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | let Some(column) = workspace.remove_active_column() else { |
| 798 | return; |
| 799 | }; |
| 800 | |
| 801 | self.add_column(new_idx, column, activate); |
| 802 | } |
| 803 | |
| 804 | pub fn switch_workspace_up(&mut self) { |
| 805 | let new_idx = match &self.workspace_switch { |
| 806 | // During a DnD scroll, select the prev apparent workspace. |
| 807 | Some(WorkspaceSwitch::Gesture(gesture)) if gesture.dnd_last_event_time.is_some() => { |
| 808 | let current = gesture.current_idx; |
| 809 | let new = current.ceil() - 1.; |
| 810 | new.clamp(0., (self.workspaces.len() - 1) as f64) as usize |
| 811 | } |
| 812 | _ => self.active_workspace_idx.saturating_sub(1), |
| 813 | }; |
| 814 | |
| 815 | self.activate_workspace(new_idx); |
| 816 | } |
| 817 | |
| 818 | pub fn switch_workspace_down(&mut self) { |
| 819 | let new_idx = match &self.workspace_switch { |
| 820 | // During a DnD scroll, select the next apparent workspace. |
| 821 | Some(WorkspaceSwitch::Gesture(gesture)) if gesture.dnd_last_event_time.is_some() => { |
| 822 | let current = gesture.current_idx; |
| 823 | let new = current.floor() + 1.; |
| 824 | new.clamp(0., (self.workspaces.len() - 1) as f64) as usize |
| 825 | } |
| 826 | _ => min(self.active_workspace_idx + 1, self.workspaces.len() - 1), |
| 827 | }; |
| 828 | |
| 829 | self.activate_workspace(new_idx); |
| 830 | } |
| 831 | |
| 832 | fn previous_workspace_idx(&self) -> Option<usize> { |
| 833 | let id = self.previous_workspace_id?; |
| 834 | self.workspaces.iter().position(|w| w.id() == id) |
| 835 | } |
| 836 | |
| 837 | pub fn switch_workspace(&mut self, idx: usize) { |
| 838 | self.activate_workspace(min(idx, self.workspaces.len() - 1)); |
| 839 | } |
| 840 | |
| 841 | pub fn switch_workspace_auto_back_and_forth(&mut self, idx: usize) { |
| 842 | let idx = min(idx, self.workspaces.len() - 1); |
| 843 | |
| 844 | if idx == self.active_workspace_idx { |
| 845 | if let Some(prev_idx) = self.previous_workspace_idx() { |
| 846 | self.switch_workspace(prev_idx); |
| 847 | } |
| 848 | } else { |
| 849 | self.switch_workspace(idx); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | pub fn switch_workspace_previous(&mut self) { |
| 854 | if let Some(idx) = self.previous_workspace_idx() { |
| 855 | self.switch_workspace(idx); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | pub fn active_window(&self) -> Option<&W> { |
| 860 | self.active_workspace_ref().active_window() |
| 861 | } |
| 862 | |
| 863 | pub fn advance_animations(&mut self) { |
| 864 | match &mut self.workspace_switch { |
| 865 | Some(WorkspaceSwitch::Animation(anim)) => { |
| 866 | if anim.is_done() { |
| 867 | self.workspace_switch = None; |
| 868 | self.clean_up_workspaces(); |
| 869 | } |
| 870 | } |
| 871 | Some(WorkspaceSwitch::Gesture(gesture)) => { |
| 872 | // Make sure the last event time doesn't go too much out of date (for |
| 873 | // monitors not under cursor), causing sudden jumps. |
| 874 | // |
| 875 | // This happens after any dnd_scroll_gesture_scroll() calls (in |
| 876 | // Layout::advance_animations()), so it doesn't mess up the time delta there. |
| 877 | if let Some(last_time) = &mut gesture.dnd_last_event_time { |
| 878 | let now = self.clock.now_unadjusted(); |
| 879 | if *last_time != now { |
| 880 | *last_time = now; |
| 881 | |
| 882 | // If last_time was already == now, then dnd_scroll_gesture_scroll() must've |
| 883 | // updated the gesture already. Therefore, when this code runs, the pointer |
| 884 | // must be outside the DnD scrolling zone. |
| 885 | gesture.dnd_nonzero_start_time = None; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | if let Some(anim) = &mut gesture.animation { |
| 890 | if anim.is_done() { |
| 891 | gesture.animation = None; |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | None => (), |
| 896 | } |
| 897 | |
| 898 | for ws in &mut self.workspaces { |
| 899 | ws.advance_animations(); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | pub(super) fn are_animations_ongoing(&self) -> bool { |
| 904 | self.workspace_switch |
| 905 | .as_ref() |
| 906 | .is_some_and(|s| s.is_animation_ongoing()) |
| 907 | || self.workspaces.iter().any(|ws| ws.are_animations_ongoing()) |
| 908 | } |
| 909 | |
| 910 | pub fn are_transitions_ongoing(&self) -> bool { |
| 911 | self.workspace_switch.is_some() |
| 912 | || self |
| 913 | .workspaces |
| 914 | .iter() |
| 915 | .any(|ws| ws.are_transitions_ongoing()) |
| 916 | } |
| 917 | |
| 918 | pub fn update_render_elements(&mut self, is_active: bool) { |
| 919 | let mut insert_hint_ws_geo = None; |
| 920 | let insert_hint_ws_id = self |
| 921 | .insert_hint |
| 922 | .as_ref() |
| 923 | .and_then(|hint| hint.workspace.existing_id()); |
| 924 | |
| 925 | for (ws, geo) in self.workspaces_with_render_geo_mut(true) { |
| 926 | ws.update_render_elements(is_active); |
| 927 | |
| 928 | if Some(ws.id()) == insert_hint_ws_id { |
| 929 | insert_hint_ws_geo = Some(geo); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | self.insert_hint_render_loc = None; |
| 934 | if let Some(hint) = &self.insert_hint { |
| 935 | match hint.workspace { |
| 936 | InsertWorkspace::Existing(ws_id) => { |
| 937 | if let Some(ws) = self.workspaces.iter().find(|ws| ws.id() == ws_id) { |
| 938 | if let Some(mut area) = ws.insert_hint_area(hint.position) { |
| 939 | let scale = ws.scale().fractional_scale(); |
| 940 | let view_size = ws.view_size(); |
| 941 | |
| 942 | // Make sure the hint is at least partially visible. |
| 943 | if matches!(hint.position, InsertPosition::NewColumn(_)) { |
| 944 | let zoom = self.overview_zoom(); |
| 945 | let geo = insert_hint_ws_geo.unwrap(); |
| 946 | let geo = geo.downscale(zoom); |
| 947 | |
| 948 | area.loc.x = area.loc.x.max(-geo.loc.x - area.size.w / 2.); |
| 949 | area.loc.x = |
| 950 | area.loc.x.min(geo.loc.x + geo.size.w - area.size.w / 2.); |
| 951 | } |
| 952 | |
| 953 | // Round to physical pixels. |
| 954 | area = area.to_physical_precise_round(scale).to_logical(scale); |
| 955 | |
| 956 | let view_rect = Rectangle::new(area.loc.upscale(-1.), view_size); |
| 957 | self.insert_hint_element.update_render_elements( |
| 958 | area.size, |
| 959 | view_rect, |
| 960 | hint.corner_radius, |
| 961 | scale, |
| 962 | ); |
| 963 | self.insert_hint_render_loc = Some(InsertHintRenderLoc { |
| 964 | workspace: hint.workspace, |
| 965 | location: area.loc, |
| 966 | }); |
| 967 | } |
| 968 | } else { |
| 969 | error!("insert hint workspace missing from monitor"); |
| 970 | } |
| 971 | } |
| 972 | InsertWorkspace::NewAt(ws_idx) => { |
| 973 | let scale = self.scale.fractional_scale(); |
| 974 | let zoom = self.overview_zoom(); |
| 975 | let gap = self.workspace_gap(zoom); |
| 976 | |
| 977 | let hint_gap = round_logical_in_physical(scale, gap * 0.1); |
| 978 | let hint_height = gap - hint_gap * 2.; |
| 979 | |
| 980 | let next_ws_geo = self.workspaces_render_geo().nth(ws_idx).unwrap(); |
| 981 | let hint_loc_diff = Point::from((0., hint_height + hint_gap)); |
| 982 | let hint_loc = next_ws_geo.loc - hint_loc_diff; |
| 983 | let hint_size = Size::from((next_ws_geo.size.w, hint_height)); |
| 984 | |
| 985 | // FIXME: sometimes the hint ends up 1 px wider than necessary and/or 1 px |
| 986 | // narrower than necessary. The values here seem correct. Might have to do with |
| 987 | // how zooming out currently doesn't round to output scale properly. |
| 988 | |
| 989 | // Compute view rect as if we're above the next workspace (rather than below |
| 990 | // the previous one). |
| 991 | let view_rect = Rectangle::new(hint_loc_diff, next_ws_geo.size); |
| 992 | |
| 993 | self.insert_hint_element.update_render_elements( |
| 994 | hint_size, |
| 995 | view_rect, |
| 996 | CornerRadius::default(), |
| 997 | scale, |
| 998 | ); |
| 999 | self.insert_hint_render_loc = Some(InsertHintRenderLoc { |
| 1000 | workspace: hint.workspace, |
| 1001 | location: hint_loc, |
| 1002 | }); |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | pub fn update_config(&mut self, options: Rc<Options>) { |
| 1009 | if self.options.empty_workspace_above_first != options.empty_workspace_above_first |
| 1010 | && self.workspaces.len() > 1 |
| 1011 | { |
| 1012 | if options.empty_workspace_above_first { |
| 1013 | self.add_workspace_top(); |
| 1014 | } else if self.workspace_switch.is_none() && self.active_workspace_idx != 0 { |
| 1015 | self.workspaces.remove(0); |
| 1016 | self.active_workspace_idx = self.active_workspace_idx.saturating_sub(1); |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | for ws in &mut self.workspaces { |
| 1021 | ws.update_config(options.clone()); |
| 1022 | } |
| 1023 | |
| 1024 | self.insert_hint_element.update_config(options.insert_hint); |
| 1025 | |
| 1026 | self.options = options; |
| 1027 | } |
| 1028 | |
| 1029 | pub fn update_shaders(&mut self) { |
| 1030 | for ws in &mut self.workspaces { |
| 1031 | ws.update_shaders(); |
| 1032 | } |
| 1033 | |
| 1034 | self.insert_hint_element.update_shaders(); |
| 1035 | } |
| 1036 | |
| 1037 | pub fn update_output_size(&mut self) { |
| 1038 | self.scale = self.output.current_scale(); |
| 1039 | self.view_size = output_size(&self.output); |
| 1040 | self.working_area = compute_working_area(&self.output); |
| 1041 | |
| 1042 | for ws in &mut self.workspaces { |
| 1043 | ws.update_output_size(); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | pub fn move_workspace_down(&mut self) { |
| 1048 | let mut new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1); |
| 1049 | if new_idx == self.active_workspace_idx { |
| 1050 | return; |
| 1051 | } |
| 1052 | |
| 1053 | self.workspaces.swap(self.active_workspace_idx, new_idx); |
| 1054 | |
| 1055 | if new_idx == self.workspaces.len() - 1 { |
| 1056 | // Insert a new empty workspace. |
| 1057 | self.add_workspace_bottom(); |
| 1058 | } |
| 1059 | |
| 1060 | if self.options.empty_workspace_above_first && self.active_workspace_idx == 0 { |
| 1061 | self.add_workspace_top(); |
| 1062 | new_idx += 1; |
| 1063 | } |
| 1064 | |
| 1065 | let previous_workspace_id = self.previous_workspace_id; |
| 1066 | self.activate_workspace(new_idx); |
| 1067 | self.workspace_switch = None; |
| 1068 | self.previous_workspace_id = previous_workspace_id; |
| 1069 | |
| 1070 | self.clean_up_workspaces(); |
| 1071 | } |
| 1072 | |
| 1073 | pub fn move_workspace_up(&mut self) { |
| 1074 | let mut new_idx = self.active_workspace_idx.saturating_sub(1); |
| 1075 | if new_idx == self.active_workspace_idx { |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | self.workspaces.swap(self.active_workspace_idx, new_idx); |
| 1080 | |
| 1081 | if self.active_workspace_idx == self.workspaces.len() - 1 { |
| 1082 | // Insert a new empty workspace. |
| 1083 | self.add_workspace_bottom(); |
| 1084 | } |
| 1085 | |
| 1086 | if self.options.empty_workspace_above_first && new_idx == 0 { |
| 1087 | self.add_workspace_top(); |
| 1088 | new_idx += 1; |
| 1089 | } |
| 1090 | |
| 1091 | let previous_workspace_id = self.previous_workspace_id; |
| 1092 | self.activate_workspace(new_idx); |
| 1093 | self.workspace_switch = None; |
| 1094 | self.previous_workspace_id = previous_workspace_id; |
| 1095 | |
| 1096 | self.clean_up_workspaces(); |
| 1097 | } |
| 1098 | |
| 1099 | pub fn move_workspace_to_idx(&mut self, old_idx: usize, new_idx: usize) { |
| 1100 | if self.workspaces.len() <= old_idx { |
| 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | let mut new_idx = new_idx.clamp(0, self.workspaces.len() - 1); |
| 1105 | if old_idx == new_idx { |
| 1106 | return; |
| 1107 | } |
| 1108 | |
| 1109 | let ws = self.workspaces.remove(old_idx); |
| 1110 | self.workspaces.insert(new_idx, ws); |
| 1111 | |
| 1112 | if new_idx > old_idx { |
| 1113 | if new_idx == self.workspaces.len() - 1 { |
| 1114 | // Insert a new empty workspace. |
| 1115 | self.add_workspace_bottom(); |
| 1116 | } |
| 1117 | |
| 1118 | if self.options.empty_workspace_above_first && old_idx == 0 { |
| 1119 | self.add_workspace_top(); |
| 1120 | new_idx += 1; |
| 1121 | } |
| 1122 | } else { |
| 1123 | if old_idx == self.workspaces.len() - 1 { |
| 1124 | // Insert a new empty workspace. |
| 1125 | self.add_workspace_bottom(); |
| 1126 | } |
| 1127 | |
| 1128 | if self.options.empty_workspace_above_first && new_idx == 0 { |
| 1129 | self.add_workspace_top(); |
| 1130 | new_idx += 1; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | // Only refocus the workspace if it was already focused |
| 1135 | if self.active_workspace_idx == old_idx { |
| 1136 | self.active_workspace_idx = new_idx; |
| 1137 | // If the workspace order was switched so that the current workspace moved down the |
| 1138 | // workspace stack, focus correctly |
| 1139 | } else if new_idx <= self.active_workspace_idx && old_idx > self.active_workspace_idx { |
| 1140 | self.active_workspace_idx += 1; |
| 1141 | } else if new_idx >= self.active_workspace_idx && old_idx < self.active_workspace_idx { |
| 1142 | self.active_workspace_idx = self.active_workspace_idx.saturating_sub(1); |
| 1143 | } |
| 1144 | |
| 1145 | self.workspace_switch = None; |
| 1146 | |
| 1147 | self.clean_up_workspaces(); |
| 1148 | } |
| 1149 | |
| 1150 | /// Returns the geometry of the active tile relative to and clamped to the output. |
| 1151 | /// |
| 1152 | /// During animations, assumes the final view position. |
| 1153 | pub fn active_tile_visual_rectangle(&self) -> Option<Rectangle<f64, Logical>> { |
| 1154 | if self.overview_open { |
| 1155 | return None; |
| 1156 | } |
| 1157 | |
| 1158 | self.active_workspace_ref().active_tile_visual_rectangle() |
| 1159 | } |
| 1160 | |
| 1161 | fn workspace_size(&self, zoom: f64) -> Size<f64, Logical> { |
| 1162 | let ws_size = self.view_size.upscale(zoom); |
| 1163 | let scale = self.scale.fractional_scale(); |
| 1164 | ws_size.to_physical_precise_ceil(scale).to_logical(scale) |
| 1165 | } |
| 1166 | |
| 1167 | fn workspace_gap(&self, zoom: f64) -> f64 { |
| 1168 | let scale = self.scale.fractional_scale(); |
| 1169 | let gap = self.view_size.h * 0.1 * zoom; |
| 1170 | round_logical_in_physical_max1(scale, gap) |
| 1171 | } |
| 1172 | |
| 1173 | fn workspace_size_with_gap(&self, zoom: f64) -> Size<f64, Logical> { |
| 1174 | let gap = self.workspace_gap(zoom); |
| 1175 | self.workspace_size(zoom) + Size::from((0., gap)) |
| 1176 | } |
| 1177 | |
| 1178 | pub fn overview_zoom(&self) -> f64 { |
| 1179 | let progress = self.overview_progress.as_ref().map(|p| p.value()); |
| 1180 | compute_overview_zoom(&self.options, progress) |
| 1181 | } |
| 1182 | |
| 1183 | pub(super) fn set_overview_progress(&mut self, progress: Option<&super::OverviewProgress>) { |
| 1184 | let prev_render_idx = self.workspace_render_idx(); |
| 1185 | self.overview_progress = progress.map(OverviewProgress::from); |
| 1186 | let new_render_idx = self.workspace_render_idx(); |
| 1187 | |
| 1188 | // If the view jumped (can happen when going from corrected to uncorrected render_idx, for |
| 1189 | // example when toggling the overview in the middle of an overview animation), then restart |
| 1190 | // the workspace switch to avoid jumps. |
| 1191 | if prev_render_idx != new_render_idx { |
| 1192 | if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch { |
| 1193 | // FIXME: maintain velocity. |
| 1194 | *anim = anim.restarted(prev_render_idx, anim.to(), 0.); |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | #[cfg(test)] |
| 1200 | pub(super) fn overview_progress_value(&self) -> Option<f64> { |
| 1201 | self.overview_progress.as_ref().map(|p| p.value()) |
| 1202 | } |
| 1203 | |
| 1204 | pub fn workspace_render_idx(&self) -> f64 { |
| 1205 | // If workspace switch and overview progress are matching animations, then compute a |
| 1206 | // correction term to make the movement appear monotonic. |
| 1207 | if let ( |
| 1208 | Some(WorkspaceSwitch::Animation(switch_anim)), |
| 1209 | Some(OverviewProgress::Animation(progress_anim)), |
| 1210 | ) = (&self.workspace_switch, &self.overview_progress) |
| 1211 | { |
| 1212 | if switch_anim.start_time() == progress_anim.start_time() |
| 1213 | && (switch_anim.duration().as_secs_f64() - progress_anim.duration().as_secs_f64()) |
| 1214 | .abs() |
| 1215 | <= 0.001 |
| 1216 | { |
| 1217 | #[rustfmt::skip] |
| 1218 | // How this was derived: |
| 1219 | // |
| 1220 | // - Assume we're animating a zoom + switch. Consider switch "from" and "to". |
| 1221 | // These are render_idx values, so first workspace to second would have switch |
| 1222 | // from = 0. and to = 1. regardless of the zoom level. |
| 1223 | // |
| 1224 | // - At the start, the point at "from" is at Y = 0. We're moving the point at "to" |
| 1225 | // to Y = 0. We want this to be a monotonic motion in apparent coordinates (after |
| 1226 | // zoom). |
| 1227 | // |
| 1228 | // - Height at the start: |
| 1229 | // from_height = (size.h + gap) * from_zoom. |
| 1230 | // |
| 1231 | // - Current height: |
| 1232 | // current_height = (size.h + gap) * zoom. |
| 1233 | // |
| 1234 | // - We're moving the "to" point to Y = 0: |
| 1235 | // to_y = 0. |
| 1236 | // |
| 1237 | // - The initial position of the point we're moving: |
| 1238 | // from_y = (to - from) * from_height. |
| 1239 | // |
| 1240 | // - We want this point to travel monotonically in apparent coordinates: |
| 1241 | // current_y = from_y + (to_y - from_y) * progress, |
| 1242 | // where progress is from 0 to 1, equals to the animation progress (switch and |
| 1243 | // zoom are the same since they are synchronized). |
| 1244 | // |
| 1245 | // - Derive the Y of the first workspace from this: |
| 1246 | // first_y = current_y - to * current_height. |
| 1247 | // |
| 1248 | // Now, let's substitute and rearrange the terms. |
| 1249 | // |
| 1250 | // - current_y = from_y + (0 - (to - from) * from_height) * progress |
| 1251 | // - progress = (switch_anim.value() - from) / (to - from) |
| 1252 | // - current_y = from_y - (to - from) * from_height * (switch_anim.value() - from) / (to - from) |
| 1253 | // - current_y = from_y - from_height * (switch_anim.value() - from) |
| 1254 | // - first_y = from_y - from_height * (switch_anim.value() - from) - to * current_height |
| 1255 | // - first_y = (to - from) * from_height - from_height * (switch_anim.value() - from) - to * current_height |
| 1256 | // - first_y = to * from_height - switch_anim.value() * from_height - to * current_height |
| 1257 | // - first_y = -switch_anim.value() * from_height + to * (from_height - current_height) |
| 1258 | let from = progress_anim.from(); |
| 1259 | let from_zoom = compute_overview_zoom(&self.options, Some(from)); |
| 1260 | let from_ws_height_with_gap = self.workspace_size_with_gap(from_zoom).h; |
| 1261 | |
| 1262 | let zoom = self.overview_zoom(); |
| 1263 | let ws_height_with_gap = self.workspace_size_with_gap(zoom).h; |
| 1264 | |
| 1265 | let first_ws_y = -switch_anim.value() * from_ws_height_with_gap |
| 1266 | + switch_anim.to() * (from_ws_height_with_gap - ws_height_with_gap); |
| 1267 | |
| 1268 | return -first_ws_y / ws_height_with_gap; |
| 1269 | } |
| 1270 | }; |
| 1271 | |
| 1272 | if let Some(switch) = &self.workspace_switch { |
| 1273 | switch.current_idx() |
| 1274 | } else { |
| 1275 | self.active_workspace_idx as f64 |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | pub fn workspaces_render_geo(&self) -> impl Iterator<Item = Rectangle<f64, Logical>> { |
| 1280 | let scale = self.scale.fractional_scale(); |
| 1281 | let zoom = self.overview_zoom(); |
| 1282 | |
| 1283 | let ws_size = self.workspace_size(zoom); |
| 1284 | let gap = self.workspace_gap(zoom); |
| 1285 | let ws_height_with_gap = ws_size.h + gap; |
| 1286 | |
| 1287 | let static_offset = (self.view_size.to_point() - ws_size.to_point()).downscale(2.); |
| 1288 | let static_offset = static_offset |
| 1289 | .to_physical_precise_round(scale) |
| 1290 | .to_logical(scale); |
| 1291 | |
| 1292 | let first_ws_y = -self.workspace_render_idx() * ws_height_with_gap; |
| 1293 | let first_ws_y = round_logical_in_physical(scale, first_ws_y); |
| 1294 | |
| 1295 | // Return position for one-past-last workspace too. |
| 1296 | (0..=self.workspaces.len()).map(move |idx| { |
| 1297 | let y = first_ws_y + idx as f64 * ws_height_with_gap; |
| 1298 | let loc = Point::from((0., y)) + static_offset; |
| 1299 | Rectangle::new(loc, ws_size) |
| 1300 | }) |
| 1301 | } |
| 1302 | |
| 1303 | pub fn workspaces_with_render_geo( |
| 1304 | &self, |
| 1305 | ) -> impl Iterator<Item = (&Workspace<W>, Rectangle<f64, Logical>)> { |
| 1306 | let output_geo = Rectangle::from_size(self.view_size); |
| 1307 | |
| 1308 | let geo = self.workspaces_render_geo(); |
| 1309 | zip(self.workspaces.iter(), geo) |
| 1310 | // Cull out workspaces outside the output. |
| 1311 | .filter(move |(_ws, geo)| geo.intersection(output_geo).is_some()) |
| 1312 | } |
| 1313 | |
| 1314 | pub fn workspaces_with_render_geo_idx( |
| 1315 | &self, |
| 1316 | ) -> impl Iterator<Item = ((usize, &Workspace<W>), Rectangle<f64, Logical>)> { |
| 1317 | let output_geo = Rectangle::from_size(self.view_size); |
| 1318 | |
| 1319 | let geo = self.workspaces_render_geo(); |
| 1320 | zip(self.workspaces.iter().enumerate(), geo) |
| 1321 | // Cull out workspaces outside the output. |
| 1322 | .filter(move |(_ws, geo)| geo.intersection(output_geo).is_some()) |
| 1323 | } |
| 1324 | |
| 1325 | pub fn workspaces_with_render_geo_mut( |
| 1326 | &mut self, |
| 1327 | cull: bool, |
| 1328 | ) -> impl Iterator<Item = (&mut Workspace<W>, Rectangle<f64, Logical>)> { |
| 1329 | let output_geo = Rectangle::from_size(self.view_size); |
| 1330 | |
| 1331 | let geo = self.workspaces_render_geo(); |
| 1332 | zip(self.workspaces.iter_mut(), geo) |
| 1333 | // Cull out workspaces outside the output. |
| 1334 | .filter(move |(_ws, geo)| !cull || geo.intersection(output_geo).is_some()) |
| 1335 | } |
| 1336 | |
| 1337 | pub fn workspace_under( |
| 1338 | &self, |
| 1339 | pos_within_output: Point<f64, Logical>, |
| 1340 | ) -> Option<(&Workspace<W>, Rectangle<f64, Logical>)> { |
| 1341 | let (ws, geo) = self.workspaces_with_render_geo().find_map(|(ws, geo)| { |
| 1342 | // Extend width to entire output. |
| 1343 | let loc = Point::from((0., geo.loc.y)); |
| 1344 | let size = Size::from((self.view_size.w, geo.size.h)); |
| 1345 | let bounds = Rectangle::new(loc, size); |
| 1346 | |
| 1347 | bounds.contains(pos_within_output).then_some((ws, geo)) |
| 1348 | })?; |
| 1349 | Some((ws, geo)) |
| 1350 | } |
| 1351 | |
| 1352 | pub fn workspace_under_narrow( |
| 1353 | &self, |
| 1354 | pos_within_output: Point<f64, Logical>, |
| 1355 | ) -> Option<&Workspace<W>> { |
| 1356 | self.workspaces_with_render_geo() |
| 1357 | .find_map(|(ws, geo)| geo.contains(pos_within_output).then_some(ws)) |
| 1358 | } |
| 1359 | |
| 1360 | pub fn window_under(&self, pos_within_output: Point<f64, Logical>) -> Option<(&W, HitType)> { |
| 1361 | let (ws, geo) = self.workspace_under(pos_within_output)?; |
| 1362 | |
| 1363 | if self.overview_progress.is_some() { |
| 1364 | let zoom = self.overview_zoom(); |
| 1365 | let pos_within_workspace = (pos_within_output - geo.loc).downscale(zoom); |
| 1366 | let (win, hit) = ws.window_under(pos_within_workspace)?; |
| 1367 | // During the overview animation, we cannot do input hits because we cannot really |
| 1368 | // represent scaled windows properly. |
| 1369 | Some((win, hit.to_activate())) |
| 1370 | } else { |
| 1371 | let (win, hit) = ws.window_under(pos_within_output - geo.loc)?; |
| 1372 | Some((win, hit.offset_win_pos(geo.loc))) |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | pub fn resize_edges_under(&self, pos_within_output: Point<f64, Logical>) -> Option<ResizeEdge> { |
| 1377 | if self.overview_progress.is_some() { |
| 1378 | return None; |
| 1379 | } |
| 1380 | |
| 1381 | let (ws, geo) = self.workspace_under(pos_within_output)?; |
| 1382 | ws.resize_edges_under(pos_within_output - geo.loc) |
| 1383 | } |
| 1384 | |
| 1385 | pub(super) fn insert_position( |
| 1386 | &self, |
| 1387 | pos_within_output: Point<f64, Logical>, |
| 1388 | ) -> (InsertWorkspace, Rectangle<f64, Logical>) { |
| 1389 | let mut iter = self.workspaces_with_render_geo_idx(); |
| 1390 | |
| 1391 | let dummy = Rectangle::default(); |
| 1392 | |
| 1393 | // Monitors always have at least one workspace. |
| 1394 | let ((idx, ws), geo) = iter.next().unwrap(); |
| 1395 | |
| 1396 | // Check if above first. |
| 1397 | if pos_within_output.y < geo.loc.y { |
| 1398 | return (InsertWorkspace::NewAt(idx), dummy); |
| 1399 | } |
| 1400 | |
| 1401 | let contains = move |geo: Rectangle<f64, Logical>| { |
| 1402 | geo.loc.y <= pos_within_output.y && pos_within_output.y < geo.loc.y + geo.size.h |
| 1403 | }; |
| 1404 | |
| 1405 | // Check first. |
| 1406 | if contains(geo) { |
| 1407 | return (InsertWorkspace::Existing(ws.id()), geo); |
| 1408 | } |
| 1409 | |
| 1410 | let mut last_geo = geo; |
| 1411 | let mut last_idx = idx; |
| 1412 | for ((idx, ws), geo) in iter { |
| 1413 | // Check gap above. |
| 1414 | let gap_loc = Point::from((last_geo.loc.x, last_geo.loc.y + last_geo.size.h)); |
| 1415 | let gap_size = Size::from((geo.size.w, geo.loc.y - gap_loc.y)); |
| 1416 | let gap_geo = Rectangle::new(gap_loc, gap_size); |
| 1417 | if contains(gap_geo) { |
| 1418 | return (InsertWorkspace::NewAt(idx), dummy); |
| 1419 | } |
| 1420 | |
| 1421 | // Check workspace itself. |
| 1422 | if contains(geo) { |
| 1423 | return (InsertWorkspace::Existing(ws.id()), geo); |
| 1424 | } |
| 1425 | |
| 1426 | last_geo = geo; |
| 1427 | last_idx = idx; |
| 1428 | } |
| 1429 | |
| 1430 | // Anything below. |
| 1431 | (InsertWorkspace::NewAt(last_idx + 1), dummy) |
| 1432 | } |
| 1433 | |
| 1434 | pub fn render_above_top_layer(&self) -> bool { |
| 1435 | // Render above the top layer only if the view is stationary. |
| 1436 | if self.workspace_switch.is_some() || self.overview_progress.is_some() { |
| 1437 | return false; |
| 1438 | } |
| 1439 | |
| 1440 | let ws = &self.workspaces[self.active_workspace_idx]; |
| 1441 | ws.render_above_top_layer() |
| 1442 | } |
| 1443 | |
| 1444 | pub fn render_insert_hint_between_workspaces<R: NiriRenderer>( |
| 1445 | &self, |
| 1446 | renderer: &mut R, |
| 1447 | ) -> impl Iterator<Item = MonitorRenderElement<R>> { |
| 1448 | let mut rv = None; |
| 1449 | |
| 1450 | if !self.options.insert_hint.off { |
| 1451 | if let Some(render_loc) = self.insert_hint_render_loc { |
| 1452 | if let InsertWorkspace::NewAt(_) = render_loc.workspace { |
| 1453 | let iter = self |
| 1454 | .insert_hint_element |
| 1455 | .render(renderer, render_loc.location) |
| 1456 | .map(MonitorInnerRenderElement::UncroppedInsertHint); |
| 1457 | rv = Some(iter); |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | rv.into_iter().flatten().map(|elem| { |
| 1463 | let elem = RescaleRenderElement::from_element(elem, Point::default(), 1.); |
| 1464 | RelocateRenderElement::from_element(elem, Point::default(), Relocate::Relative) |
| 1465 | }) |
| 1466 | } |
| 1467 | |
| 1468 | pub fn render_elements<'a, R: NiriRenderer>( |
| 1469 | &'a self, |
| 1470 | renderer: &'a mut R, |
| 1471 | target: RenderTarget, |
| 1472 | focus_ring: bool, |
| 1473 | ) -> impl Iterator< |
| 1474 | Item = ( |
| 1475 | Rectangle<f64, Logical>, |
| 1476 | impl Iterator<Item = MonitorRenderElement<R>> + 'a, |
| 1477 | ), |
| 1478 | > { |
| 1479 | let _span = tracy_client::span!("Monitor::render_elements"); |
| 1480 | |
| 1481 | let scale = self.scale.fractional_scale(); |
| 1482 | // Ceil the height in physical pixels. |
| 1483 | let height = (self.view_size.h * scale).ceil() as i32; |
| 1484 | |
| 1485 | // Crop the elements to prevent them overflowing, currently visible during a workspace |
| 1486 | // switch. |
| 1487 | // |
| 1488 | // HACK: crop to infinite bounds at least horizontally where we |
| 1489 | // know there's no workspace joining or monitor bounds, otherwise |
| 1490 | // it will cut pixel shaders and mess up the coordinate space. |
| 1491 | // There's also a damage tracking bug which causes glitched |
| 1492 | // rendering for maximized GTK windows. |
| 1493 | // |
| 1494 | // FIXME: use proper bounds after fixing the Crop element. |
| 1495 | let crop_bounds = if self.workspace_switch.is_some() || self.overview_progress.is_some() { |
| 1496 | Rectangle::new( |
| 1497 | Point::from((-i32::MAX / 2, 0)), |
| 1498 | Size::from((i32::MAX, height)), |
| 1499 | ) |
| 1500 | } else { |
| 1501 | Rectangle::new( |
| 1502 | Point::from((-i32::MAX / 2, -i32::MAX / 2)), |
| 1503 | Size::from((i32::MAX, i32::MAX)), |
| 1504 | ) |
| 1505 | }; |
| 1506 | |
| 1507 | let zoom = self.overview_zoom(); |
| 1508 | |
| 1509 | // Draw the insert hint. |
| 1510 | let mut insert_hint = None; |
| 1511 | if !self.options.insert_hint.off { |
| 1512 | if let Some(render_loc) = self.insert_hint_render_loc { |
| 1513 | if let InsertWorkspace::Existing(workspace_id) = render_loc.workspace { |
| 1514 | insert_hint = Some(( |
| 1515 | workspace_id, |
| 1516 | self.insert_hint_element |
| 1517 | .render(renderer, render_loc.location), |
| 1518 | )); |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | self.workspaces_with_render_geo().map(move |(ws, geo)| { |
| 1524 | let map_ws_contents = move |elem: WorkspaceRenderElement<R>| { |
| 1525 | let elem = CropRenderElement::from_element(elem, scale, crop_bounds)?; |
| 1526 | let elem = MonitorInnerRenderElement::Workspace(elem); |
| 1527 | Some(elem) |
| 1528 | }; |
| 1529 | |
| 1530 | let (floating, scrolling) = ws.render_elements(renderer, target, focus_ring); |
| 1531 | let floating = floating.filter_map(map_ws_contents); |
| 1532 | let scrolling = scrolling.filter_map(map_ws_contents); |
| 1533 | |
| 1534 | let hint = if matches!(insert_hint, Some((hint_ws_id, _)) if hint_ws_id == ws.id()) { |
| 1535 | let iter = insert_hint.take().unwrap().1; |
| 1536 | let iter = iter.filter_map(move |elem| { |
| 1537 | let elem = CropRenderElement::from_element(elem, scale, crop_bounds)?; |
| 1538 | let elem = MonitorInnerRenderElement::InsertHint(elem); |
| 1539 | Some(elem) |
| 1540 | }); |
| 1541 | Some(iter) |
| 1542 | } else { |
| 1543 | None |
| 1544 | }; |
| 1545 | let hint = hint.into_iter().flatten(); |
| 1546 | |
| 1547 | let iter = floating.chain(hint).chain(scrolling); |
| 1548 | |
| 1549 | let iter = iter.map(move |elem| { |
| 1550 | let elem = RescaleRenderElement::from_element(elem, Point::from((0, 0)), zoom); |
| 1551 | RelocateRenderElement::from_element( |
| 1552 | elem, |
| 1553 | // The offset we get from workspaces_with_render_positions() is already |
| 1554 | // rounded to physical pixels, but it's in the logical coordinate |
| 1555 | // space, so we need to convert it to physical. |
| 1556 | geo.loc.to_physical_precise_round(scale), |
| 1557 | Relocate::Relative, |
| 1558 | ) |
| 1559 | }); |
| 1560 | |
| 1561 | (geo, iter) |
| 1562 | }) |
| 1563 | } |
| 1564 | |
| 1565 | pub fn render_workspace_shadows<'a, R: NiriRenderer>( |
| 1566 | &'a self, |
| 1567 | renderer: &'a mut R, |
| 1568 | ) -> impl Iterator<Item = MonitorRenderElement<R>> + 'a { |
| 1569 | let _span = tracy_client::span!("Monitor::render_workspace_shadows"); |
| 1570 | |
| 1571 | let scale = self.scale.fractional_scale(); |
| 1572 | let zoom = self.overview_zoom(); |
| 1573 | let overview_clamped_progress = self.overview_progress.as_ref().map(|p| p.clamped_value()); |
| 1574 | |
| 1575 | self.workspaces_with_render_geo() |
| 1576 | .flat_map(move |(ws, geo)| { |
| 1577 | let shadow = overview_clamped_progress.map(|value| { |
| 1578 | ws.render_shadow(renderer) |
| 1579 | .map(move |elem| elem.with_alpha(value.clamp(0., 1.) as f32)) |
| 1580 | .map(MonitorInnerRenderElement::Shadow) |
| 1581 | }); |
| 1582 | let iter = shadow.into_iter().flatten(); |
| 1583 | |
| 1584 | iter.map(move |elem| { |
| 1585 | let elem = RescaleRenderElement::from_element(elem, Point::from((0, 0)), zoom); |
| 1586 | RelocateRenderElement::from_element( |
| 1587 | elem, |
| 1588 | geo.loc.to_physical_precise_round(scale), |
| 1589 | Relocate::Relative, |
| 1590 | ) |
| 1591 | }) |
| 1592 | }) |
| 1593 | } |
| 1594 | |
| 1595 | pub fn workspace_switch_gesture_begin(&mut self, is_touchpad: bool) { |
| 1596 | let center_idx = self.active_workspace_idx; |
| 1597 | let current_idx = self.workspace_render_idx(); |
| 1598 | |
| 1599 | let gesture = WorkspaceSwitchGesture { |
| 1600 | center_idx, |
| 1601 | start_idx: current_idx, |
| 1602 | current_idx, |
| 1603 | animation: None, |
| 1604 | tracker: SwipeTracker::new(), |
| 1605 | is_touchpad, |
| 1606 | is_clamped: !self.overview_open, |
| 1607 | dnd_last_event_time: None, |
| 1608 | dnd_nonzero_start_time: None, |
| 1609 | }; |
| 1610 | self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture)); |
| 1611 | } |
| 1612 | |
| 1613 | pub fn dnd_scroll_gesture_begin(&mut self) { |
| 1614 | if let Some(WorkspaceSwitch::Gesture(WorkspaceSwitchGesture { |
| 1615 | dnd_last_event_time: Some(_), |
| 1616 | .. |
| 1617 | })) = &self.workspace_switch |
| 1618 | { |
| 1619 | // Already active. |
| 1620 | return; |
| 1621 | } |
| 1622 | |
| 1623 | if !self.overview_open { |
| 1624 | // This gesture is only for the overview. |
| 1625 | return; |
| 1626 | } |
| 1627 | |
| 1628 | let center_idx = self.active_workspace_idx; |
| 1629 | let current_idx = self.workspace_render_idx(); |
| 1630 | |
| 1631 | let gesture = WorkspaceSwitchGesture { |
| 1632 | center_idx, |
| 1633 | start_idx: current_idx, |
| 1634 | current_idx, |
| 1635 | animation: None, |
| 1636 | tracker: SwipeTracker::new(), |
| 1637 | is_touchpad: false, |
| 1638 | is_clamped: false, |
| 1639 | dnd_last_event_time: Some(self.clock.now_unadjusted()), |
| 1640 | dnd_nonzero_start_time: None, |
| 1641 | }; |
| 1642 | self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture)); |
| 1643 | } |
| 1644 | |
| 1645 | pub fn workspace_switch_gesture_update( |
| 1646 | &mut self, |
| 1647 | delta_y: f64, |
| 1648 | timestamp: Duration, |
| 1649 | is_touchpad: bool, |
| 1650 | ) -> Option<bool> { |
| 1651 | let Some(WorkspaceSwitch::Gesture(gesture)) = &self.workspace_switch else { |
| 1652 | return None; |
| 1653 | }; |
| 1654 | |
| 1655 | if gesture.is_touchpad != is_touchpad || gesture.dnd_last_event_time.is_some() { |
| 1656 | return None; |
| 1657 | } |
| 1658 | |
| 1659 | let zoom = self.overview_zoom(); |
| 1660 | let total_height = if gesture.is_touchpad { |
| 1661 | WORKSPACE_GESTURE_MOVEMENT |
| 1662 | } else { |
| 1663 | self.workspace_size_with_gap(1.).h |
| 1664 | }; |
| 1665 | |
| 1666 | let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else { |
| 1667 | return None; |
| 1668 | }; |
| 1669 | |
| 1670 | // Reduce the effect of zoom on the touchpad somewhat. |
| 1671 | let delta_scale = if gesture.is_touchpad { |
| 1672 | (zoom - 1.) / 2.5 + 1. |
| 1673 | } else { |
| 1674 | zoom |
| 1675 | }; |
| 1676 | |
| 1677 | let delta_y = delta_y / delta_scale; |
| 1678 | let mut rubber_band = WORKSPACE_GESTURE_RUBBER_BAND; |
| 1679 | rubber_band.limit /= zoom; |
| 1680 | |
| 1681 | gesture.tracker.push(delta_y, timestamp); |
| 1682 | |
| 1683 | let pos = gesture.tracker.pos() / total_height; |
| 1684 | |
| 1685 | let (min, max) = gesture.min_max(self.workspaces.len()); |
| 1686 | let new_idx = gesture.start_idx + pos; |
| 1687 | let new_idx = rubber_band.clamp(min, max, new_idx); |
| 1688 | |
| 1689 | if gesture.current_idx == new_idx { |
| 1690 | return Some(false); |
| 1691 | } |
| 1692 | |
| 1693 | gesture.current_idx = new_idx; |
| 1694 | Some(true) |
| 1695 | } |
| 1696 | |
| 1697 | pub fn dnd_scroll_gesture_scroll(&mut self, pos: Point<f64, Logical>, speed: f64) -> bool { |
| 1698 | let zoom = self.overview_zoom(); |
| 1699 | |
| 1700 | let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else { |
| 1701 | return false; |
| 1702 | }; |
| 1703 | |
| 1704 | let Some(last_time) = gesture.dnd_last_event_time else { |
| 1705 | // Not a DnD scroll. |
| 1706 | return false; |
| 1707 | }; |
| 1708 | |
| 1709 | let config = &self.options.gestures.dnd_edge_workspace_switch; |
| 1710 | let trigger_height = config.trigger_height.0; |
| 1711 | |
| 1712 | // Restrict the scrolling horizontally to the strip of workspaces to avoid unwanted trigger |
| 1713 | // after using the hot corner or during horizontal scroll. |
| 1714 | let width = self.view_size.w * zoom; |
| 1715 | let x = pos.x - (self.view_size.w - width) / 2.; |
| 1716 | |
| 1717 | // Consider the working area so layer-shell docks and such don't prevent scrolling. |
| 1718 | let y = pos.y - self.working_area.loc.y; |
| 1719 | let height = self.working_area.size.h; |
| 1720 | |
| 1721 | let y = y.clamp(0., height); |
| 1722 | let trigger_height = trigger_height.clamp(0., height / 2.); |
| 1723 | |
| 1724 | let delta = if x < 0. || width <= x { |
| 1725 | // Outside the bounds horizontally. |
| 1726 | 0. |
| 1727 | } else if y < trigger_height { |
| 1728 | -(trigger_height - y) |
| 1729 | } else if height - y < trigger_height { |
| 1730 | trigger_height - (height - y) |
| 1731 | } else { |
| 1732 | 0. |
| 1733 | }; |
| 1734 | |
| 1735 | let delta = if trigger_height < 0.01 { |
| 1736 | // Sanity check for trigger-height 0 or small window sizes. |
| 1737 | 0. |
| 1738 | } else { |
| 1739 | // Normalize to [0, 1]. |
| 1740 | delta / trigger_height |
| 1741 | }; |
| 1742 | let delta = delta * speed; |
| 1743 | |
| 1744 | let now = self.clock.now_unadjusted(); |
| 1745 | gesture.dnd_last_event_time = Some(now); |
| 1746 | |
| 1747 | if delta == 0. { |
| 1748 | // We're outside the scrolling zone. |
| 1749 | gesture.dnd_nonzero_start_time = None; |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | let nonzero_start = *gesture.dnd_nonzero_start_time.get_or_insert(now); |
| 1754 | |
| 1755 | // Delay starting the gesture a bit to avoid unwanted movement when dragging across |
| 1756 | // monitors. |
| 1757 | let delay = Duration::from_millis(u64::from(config.delay_ms)); |
| 1758 | if now.saturating_sub(nonzero_start) < delay { |
| 1759 | return true; |
| 1760 | } |
| 1761 | |
| 1762 | let time_delta = now.saturating_sub(last_time).as_secs_f64(); |
| 1763 | |
| 1764 | let delta = delta * time_delta * config.max_speed.0; |
| 1765 | |
| 1766 | gesture.tracker.push(delta, now); |
| 1767 | |
| 1768 | let total_height = WORKSPACE_DND_EDGE_SCROLL_MOVEMENT; |
| 1769 | let pos = gesture.tracker.pos() / total_height; |
| 1770 | let unclamped = gesture.start_idx + pos; |
| 1771 | |
| 1772 | let (min, max) = gesture.min_max(self.workspaces.len()); |
| 1773 | let clamped = unclamped.clamp(min, max); |
| 1774 | |
| 1775 | // Make sure that DnD scrolling too much outside the min/max does not "build up". |
| 1776 | gesture.start_idx += clamped - unclamped; |
| 1777 | gesture.current_idx = clamped; |
| 1778 | |
| 1779 | true |
| 1780 | } |
| 1781 | |
| 1782 | pub fn workspace_switch_gesture_end(&mut self, is_touchpad: Option<bool>) -> bool { |
| 1783 | let Some(WorkspaceSwitch::Gesture(gesture)) = &self.workspace_switch else { |
| 1784 | return false; |
| 1785 | }; |
| 1786 | |
| 1787 | if is_touchpad.is_some_and(|x| gesture.is_touchpad != x) { |
| 1788 | return false; |
| 1789 | } |
| 1790 | |
| 1791 | let zoom = self.overview_zoom(); |
| 1792 | let total_height = if gesture.dnd_last_event_time.is_some() { |
| 1793 | WORKSPACE_DND_EDGE_SCROLL_MOVEMENT |
| 1794 | } else if gesture.is_touchpad { |
| 1795 | WORKSPACE_GESTURE_MOVEMENT |
| 1796 | } else { |
| 1797 | self.workspace_size_with_gap(1.).h |
| 1798 | }; |
| 1799 | |
| 1800 | let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else { |
| 1801 | return false; |
| 1802 | }; |
| 1803 | |
| 1804 | // Take into account any idle time between the last event and now. |
| 1805 | let now = self.clock.now_unadjusted(); |
| 1806 | gesture.tracker.push(0., now); |
| 1807 | |
| 1808 | let mut rubber_band = WORKSPACE_GESTURE_RUBBER_BAND; |
| 1809 | rubber_band.limit /= zoom; |
| 1810 | |
| 1811 | let mut velocity = gesture.tracker.velocity() / total_height; |
| 1812 | let current_pos = gesture.tracker.pos() / total_height; |
| 1813 | let pos = gesture.tracker.projected_end_pos() / total_height; |
| 1814 | |
| 1815 | let (min, max) = gesture.min_max(self.workspaces.len()); |
| 1816 | let new_idx = gesture.start_idx + pos; |
| 1817 | |
| 1818 | let new_idx = new_idx.clamp(min, max); |
| 1819 | let new_idx = new_idx.round() as usize; |
| 1820 | |
| 1821 | velocity *= rubber_band.clamp_derivative(min, max, gesture.start_idx + current_pos); |
| 1822 | |
| 1823 | self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id()); |
| 1824 | |
| 1825 | self.active_workspace_idx = new_idx; |
| 1826 | self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new( |
| 1827 | self.clock.clone(), |
| 1828 | gesture.current_idx, |
| 1829 | new_idx as f64, |
| 1830 | velocity, |
| 1831 | self.options.animations.workspace_switch.0, |
| 1832 | ))); |
| 1833 | |
| 1834 | true |
| 1835 | } |
| 1836 | |
| 1837 | pub fn dnd_scroll_gesture_end(&mut self) { |
| 1838 | if !matches!( |
| 1839 | self.workspace_switch, |
| 1840 | Some(WorkspaceSwitch::Gesture(WorkspaceSwitchGesture { |
| 1841 | dnd_last_event_time: Some(_), |
| 1842 | .. |
| 1843 | })) |
| 1844 | ) { |
| 1845 | // Not a DnD scroll. |
| 1846 | return; |
| 1847 | }; |
| 1848 | |
| 1849 | self.workspace_switch_gesture_end(None); |
| 1850 | } |
| 1851 | |
| 1852 | pub fn scale(&self) -> smithay::output::Scale { |
| 1853 | self.scale |
| 1854 | } |
| 1855 | |
| 1856 | pub fn view_size(&self) -> Size<f64, Logical> { |
| 1857 | self.view_size |
| 1858 | } |
| 1859 | |
| 1860 | pub fn working_area(&self) -> Rectangle<f64, Logical> { |
| 1861 | self.working_area |
| 1862 | } |
| 1863 | } |