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