niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·fa2bace·Maximilian Huber·1 year ago·1550 commits
| 1 | use std::cmp::min; |
| 2 | use std::rc::Rc; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | use niri_ipc::SizeChange; |
| 6 | use smithay::backend::renderer::element::utils::{ |
| 7 | CropRenderElement, Relocate, RelocateRenderElement, |
| 8 | }; |
| 9 | use smithay::output::Output; |
| 10 | use smithay::utils::{Logical, Point, Rectangle}; |
| 11 | |
| 12 | use super::scrolling::{Column, ColumnWidth}; |
| 13 | use super::tile::Tile; |
| 14 | use super::workspace::{OutputId, Workspace, WorkspaceId, WorkspaceRenderElement}; |
| 15 | use super::{LayoutElement, Options}; |
| 16 | use crate::animation::{Animation, Clock}; |
| 17 | use crate::input::swipe_tracker::SwipeTracker; |
| 18 | use crate::render_helpers::renderer::NiriRenderer; |
| 19 | use crate::render_helpers::RenderTarget; |
| 20 | use crate::rubber_band::RubberBand; |
| 21 | use crate::utils::transaction::Transaction; |
| 22 | use crate::utils::{output_size, round_logical_in_physical, ResizeEdge}; |
| 23 | |
| 24 | /// Amount of touchpad movement to scroll the height of one workspace. |
| 25 | const WORKSPACE_GESTURE_MOVEMENT: f64 = 300.; |
| 26 | |
| 27 | const WORKSPACE_GESTURE_RUBBER_BAND: RubberBand = RubberBand { |
| 28 | stiffness: 0.5, |
| 29 | limit: 0.05, |
| 30 | }; |
| 31 | |
| 32 | #[derive(Debug)] |
| 33 | pub struct Monitor<W: LayoutElement> { |
| 34 | /// Output for this monitor. |
| 35 | pub(super) output: Output, |
| 36 | /// Cached name of the output. |
| 37 | output_name: String, |
| 38 | // Must always contain at least one. |
| 39 | pub(super) workspaces: Vec<Workspace<W>>, |
| 40 | /// Index of the currently active workspace. |
| 41 | pub(super) active_workspace_idx: usize, |
| 42 | /// ID of the previously active workspace. |
| 43 | pub(super) previous_workspace_id: Option<WorkspaceId>, |
| 44 | /// In-progress switch between workspaces. |
| 45 | pub(super) workspace_switch: Option<WorkspaceSwitch>, |
| 46 | /// Clock for driving animations. |
| 47 | pub(super) clock: Clock, |
| 48 | /// Configurable properties of the layout. |
| 49 | pub(super) options: Rc<Options>, |
| 50 | } |
| 51 | |
| 52 | #[derive(Debug)] |
| 53 | pub enum WorkspaceSwitch { |
| 54 | Animation(Animation), |
| 55 | Gesture(WorkspaceSwitchGesture), |
| 56 | } |
| 57 | |
| 58 | #[derive(Debug)] |
| 59 | pub struct WorkspaceSwitchGesture { |
| 60 | /// Index of the workspace where the gesture was started. |
| 61 | center_idx: usize, |
| 62 | /// Current, fractional workspace index. |
| 63 | pub(super) current_idx: f64, |
| 64 | tracker: SwipeTracker, |
| 65 | /// Whether the gesture is controlled by the touchpad. |
| 66 | is_touchpad: bool, |
| 67 | } |
| 68 | |
| 69 | pub type MonitorRenderElement<R> = |
| 70 | RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>; |
| 71 | |
| 72 | impl WorkspaceSwitch { |
| 73 | pub fn current_idx(&self) -> f64 { |
| 74 | match self { |
| 75 | WorkspaceSwitch::Animation(anim) => anim.value(), |
| 76 | WorkspaceSwitch::Gesture(gesture) => gesture.current_idx, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | pub fn target_idx(&self) -> f64 { |
| 81 | match self { |
| 82 | WorkspaceSwitch::Animation(anim) => anim.to(), |
| 83 | WorkspaceSwitch::Gesture(gesture) => gesture.current_idx, |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub fn offset(&mut self, delta: isize) { |
| 88 | match self { |
| 89 | WorkspaceSwitch::Animation(anim) => anim.offset(delta as f64), |
| 90 | WorkspaceSwitch::Gesture(gesture) => { |
| 91 | if delta >= 0 { |
| 92 | gesture.center_idx += delta as usize; |
| 93 | } else { |
| 94 | gesture.center_idx -= (-delta) as usize; |
| 95 | } |
| 96 | gesture.current_idx += delta as f64; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /// Returns `true` if the workspace switch is [`Animation`]. |
| 102 | /// |
| 103 | /// [`Animation`]: WorkspaceSwitch::Animation |
| 104 | #[must_use] |
| 105 | fn is_animation(&self) -> bool { |
| 106 | matches!(self, Self::Animation(..)) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl<W: LayoutElement> Monitor<W> { |
| 111 | pub fn new( |
| 112 | output: Output, |
| 113 | workspaces: Vec<Workspace<W>>, |
| 114 | clock: Clock, |
| 115 | options: Rc<Options>, |
| 116 | ) -> Self { |
| 117 | Self { |
| 118 | output_name: output.name(), |
| 119 | output, |
| 120 | workspaces, |
| 121 | active_workspace_idx: 0, |
| 122 | previous_workspace_id: None, |
| 123 | workspace_switch: None, |
| 124 | clock, |
| 125 | options, |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | pub fn output(&self) -> &Output { |
| 130 | &self.output |
| 131 | } |
| 132 | |
| 133 | pub fn output_name(&self) -> &String { |
| 134 | &self.output_name |
| 135 | } |
| 136 | |
| 137 | pub fn active_workspace_idx(&self) -> usize { |
| 138 | self.active_workspace_idx |
| 139 | } |
| 140 | |
| 141 | pub fn active_workspace_ref(&self) -> &Workspace<W> { |
| 142 | &self.workspaces[self.active_workspace_idx] |
| 143 | } |
| 144 | |
| 145 | pub fn find_named_workspace(&self, workspace_name: &str) -> Option<&Workspace<W>> { |
| 146 | self.workspaces.iter().find(|ws| { |
| 147 | ws.name |
| 148 | .as_ref() |
| 149 | .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name)) |
| 150 | }) |
| 151 | } |
| 152 | |
| 153 | pub fn find_named_workspace_index(&self, workspace_name: &str) -> Option<usize> { |
| 154 | self.workspaces.iter().position(|ws| { |
| 155 | ws.name |
| 156 | .as_ref() |
| 157 | .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name)) |
| 158 | }) |
| 159 | } |
| 160 | |
| 161 | pub fn active_workspace(&mut self) -> &mut Workspace<W> { |
| 162 | &mut self.workspaces[self.active_workspace_idx] |
| 163 | } |
| 164 | |
| 165 | pub fn windows(&self) -> impl Iterator<Item = &W> { |
| 166 | self.workspaces.iter().flat_map(|ws| ws.windows()) |
| 167 | } |
| 168 | |
| 169 | pub fn has_window(&self, window: &W::Id) -> bool { |
| 170 | self.windows().any(|win| win.id() == window) |
| 171 | } |
| 172 | |
| 173 | pub fn add_workspace_top(&mut self) { |
| 174 | let ws = Workspace::new( |
| 175 | self.output.clone(), |
| 176 | self.clock.clone(), |
| 177 | self.options.clone(), |
| 178 | ); |
| 179 | self.workspaces.insert(0, ws); |
| 180 | self.active_workspace_idx += 1; |
| 181 | |
| 182 | if let Some(switch) = &mut self.workspace_switch { |
| 183 | switch.offset(1); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | pub fn add_workspace_bottom(&mut self) { |
| 188 | let ws = Workspace::new( |
| 189 | self.output.clone(), |
| 190 | self.clock.clone(), |
| 191 | self.options.clone(), |
| 192 | ); |
| 193 | self.workspaces.push(ws); |
| 194 | } |
| 195 | |
| 196 | fn activate_workspace(&mut self, idx: usize) { |
| 197 | if self.active_workspace_idx == idx { |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | // FIXME: also compute and use current velocity. |
| 202 | let current_idx = self |
| 203 | .workspace_switch |
| 204 | .as_ref() |
| 205 | .map(|s| s.current_idx()) |
| 206 | .unwrap_or(self.active_workspace_idx as f64); |
| 207 | |
| 208 | self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id()); |
| 209 | |
| 210 | self.active_workspace_idx = idx; |
| 211 | |
| 212 | self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new( |
| 213 | self.clock.clone(), |
| 214 | current_idx, |
| 215 | idx as f64, |
| 216 | 0., |
| 217 | self.options.animations.workspace_switch.0, |
| 218 | ))); |
| 219 | } |
| 220 | |
| 221 | pub fn add_window( |
| 222 | &mut self, |
| 223 | mut workspace_idx: usize, |
| 224 | window: W, |
| 225 | activate: bool, |
| 226 | width: ColumnWidth, |
| 227 | is_full_width: bool, |
| 228 | ) { |
| 229 | let workspace = &mut self.workspaces[workspace_idx]; |
| 230 | |
| 231 | workspace.add_window(window, activate, width, is_full_width); |
| 232 | |
| 233 | // After adding a new window, workspace becomes this output's own. |
| 234 | workspace.original_output = OutputId::new(&self.output); |
| 235 | |
| 236 | if workspace_idx == self.workspaces.len() - 1 { |
| 237 | self.add_workspace_bottom(); |
| 238 | } |
| 239 | |
| 240 | if self.options.empty_workspace_above_first && workspace_idx == 0 { |
| 241 | self.add_workspace_top(); |
| 242 | workspace_idx += 1; |
| 243 | } |
| 244 | |
| 245 | if activate { |
| 246 | self.activate_workspace(workspace_idx); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | pub fn add_window_right_of( |
| 251 | &mut self, |
| 252 | right_of: &W::Id, |
| 253 | window: W, |
| 254 | width: ColumnWidth, |
| 255 | is_full_width: bool, |
| 256 | ) { |
| 257 | let workspace_idx = self |
| 258 | .workspaces |
| 259 | .iter_mut() |
| 260 | .position(|ws| ws.has_window(right_of)) |
| 261 | .unwrap(); |
| 262 | let workspace = &mut self.workspaces[workspace_idx]; |
| 263 | |
| 264 | workspace.add_window_right_of(right_of, window, width, is_full_width); |
| 265 | |
| 266 | // After adding a new window, workspace becomes this output's own. |
| 267 | workspace.original_output = OutputId::new(&self.output); |
| 268 | |
| 269 | // Since we're adding window right of something, the workspace isn't empty, and therefore |
| 270 | // cannot be the last one, so we never need to insert a new empty workspace. |
| 271 | } |
| 272 | |
| 273 | pub fn add_column(&mut self, mut workspace_idx: usize, column: Column<W>, activate: bool) { |
| 274 | let workspace = &mut self.workspaces[workspace_idx]; |
| 275 | |
| 276 | workspace.add_column(column, activate); |
| 277 | |
| 278 | // After adding a new window, workspace becomes this output's own. |
| 279 | workspace.original_output = OutputId::new(&self.output); |
| 280 | |
| 281 | if workspace_idx == self.workspaces.len() - 1 { |
| 282 | self.add_workspace_bottom(); |
| 283 | } |
| 284 | if self.options.empty_workspace_above_first && workspace_idx == 0 { |
| 285 | self.add_workspace_top(); |
| 286 | workspace_idx += 1; |
| 287 | } |
| 288 | |
| 289 | if activate { |
| 290 | self.activate_workspace(workspace_idx); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | pub fn add_tile( |
| 295 | &mut self, |
| 296 | mut workspace_idx: usize, |
| 297 | column_idx: Option<usize>, |
| 298 | tile: Tile<W>, |
| 299 | activate: bool, |
| 300 | width: ColumnWidth, |
| 301 | is_full_width: bool, |
| 302 | ) { |
| 303 | let workspace = &mut self.workspaces[workspace_idx]; |
| 304 | |
| 305 | workspace.add_tile(column_idx, tile, activate, width, is_full_width); |
| 306 | |
| 307 | // After adding a new window, workspace becomes this output's own. |
| 308 | workspace.original_output = OutputId::new(&self.output); |
| 309 | |
| 310 | if workspace_idx == self.workspaces.len() - 1 { |
| 311 | // Insert a new empty workspace. |
| 312 | self.add_workspace_bottom(); |
| 313 | } |
| 314 | |
| 315 | if self.options.empty_workspace_above_first && workspace_idx == 0 { |
| 316 | self.add_workspace_top(); |
| 317 | workspace_idx += 1; |
| 318 | } |
| 319 | |
| 320 | if activate { |
| 321 | self.activate_workspace(workspace_idx); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | pub fn add_tile_to_column( |
| 326 | &mut self, |
| 327 | workspace_idx: usize, |
| 328 | column_idx: usize, |
| 329 | tile_idx: Option<usize>, |
| 330 | tile: Tile<W>, |
| 331 | activate: bool, |
| 332 | ) { |
| 333 | let workspace = &mut self.workspaces[workspace_idx]; |
| 334 | |
| 335 | workspace.add_tile_to_column(column_idx, tile_idx, tile, activate); |
| 336 | |
| 337 | // After adding a new window, workspace becomes this output's own. |
| 338 | workspace.original_output = OutputId::new(&self.output); |
| 339 | |
| 340 | // Since we're adding window to an existing column, the workspace isn't empty, and |
| 341 | // therefore cannot be the last one, so we never need to insert a new empty workspace. |
| 342 | |
| 343 | if activate { |
| 344 | self.activate_workspace(workspace_idx); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | pub fn clean_up_workspaces(&mut self) { |
| 349 | assert!(self.workspace_switch.is_none()); |
| 350 | |
| 351 | let range_start = if self.options.empty_workspace_above_first { |
| 352 | 1 |
| 353 | } else { |
| 354 | 0 |
| 355 | }; |
| 356 | for idx in (range_start..self.workspaces.len() - 1).rev() { |
| 357 | if self.active_workspace_idx == idx { |
| 358 | continue; |
| 359 | } |
| 360 | |
| 361 | if !self.workspaces[idx].has_windows_or_name() { |
| 362 | self.workspaces.remove(idx); |
| 363 | if self.active_workspace_idx > idx { |
| 364 | self.active_workspace_idx -= 1; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // Special case handling when empty_workspace_above_first is set and all workspaces |
| 370 | // are empty. |
| 371 | if self.options.empty_workspace_above_first && self.workspaces.len() == 2 { |
| 372 | assert!(!self.workspaces[0].has_windows_or_name()); |
| 373 | assert!(!self.workspaces[1].has_windows_or_name()); |
| 374 | self.workspaces.remove(1); |
| 375 | self.active_workspace_idx = 0; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | pub fn unname_workspace(&mut self, workspace_name: &str) -> bool { |
| 380 | for ws in &mut self.workspaces { |
| 381 | if ws |
| 382 | .name |
| 383 | .as_ref() |
| 384 | .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name)) |
| 385 | { |
| 386 | ws.unname(); |
| 387 | return true; |
| 388 | } |
| 389 | } |
| 390 | false |
| 391 | } |
| 392 | |
| 393 | pub fn move_left(&mut self) -> bool { |
| 394 | self.active_workspace().move_left() |
| 395 | } |
| 396 | |
| 397 | pub fn move_right(&mut self) -> bool { |
| 398 | self.active_workspace().move_right() |
| 399 | } |
| 400 | |
| 401 | pub fn move_column_to_first(&mut self) { |
| 402 | self.active_workspace().move_column_to_first(); |
| 403 | } |
| 404 | |
| 405 | pub fn move_column_to_last(&mut self) { |
| 406 | self.active_workspace().move_column_to_last(); |
| 407 | } |
| 408 | |
| 409 | pub fn move_down(&mut self) { |
| 410 | self.active_workspace().move_down(); |
| 411 | } |
| 412 | |
| 413 | pub fn move_up(&mut self) { |
| 414 | self.active_workspace().move_up(); |
| 415 | } |
| 416 | |
| 417 | pub fn move_down_or_to_workspace_down(&mut self) { |
| 418 | if !self.active_workspace().move_down() { |
| 419 | self.move_to_workspace_down(); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | pub fn move_up_or_to_workspace_up(&mut self) { |
| 424 | if !self.active_workspace().move_up() { |
| 425 | self.move_to_workspace_up(); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | pub fn focus_left(&mut self) -> bool { |
| 430 | self.active_workspace().focus_left() |
| 431 | } |
| 432 | |
| 433 | pub fn focus_right(&mut self) -> bool { |
| 434 | self.active_workspace().focus_right() |
| 435 | } |
| 436 | |
| 437 | pub fn focus_column_first(&mut self) { |
| 438 | self.active_workspace().focus_column_first(); |
| 439 | } |
| 440 | |
| 441 | pub fn focus_column_last(&mut self) { |
| 442 | self.active_workspace().focus_column_last(); |
| 443 | } |
| 444 | |
| 445 | pub fn focus_column_right_or_first(&mut self) { |
| 446 | self.active_workspace().focus_column_right_or_first(); |
| 447 | } |
| 448 | |
| 449 | pub fn focus_column_left_or_last(&mut self) { |
| 450 | self.active_workspace().focus_column_left_or_last(); |
| 451 | } |
| 452 | |
| 453 | pub fn focus_down(&mut self) -> bool { |
| 454 | self.active_workspace().focus_down() |
| 455 | } |
| 456 | |
| 457 | pub fn focus_up(&mut self) -> bool { |
| 458 | self.active_workspace().focus_up() |
| 459 | } |
| 460 | |
| 461 | pub fn focus_down_or_left(&mut self) { |
| 462 | self.active_workspace().focus_down_or_left(); |
| 463 | } |
| 464 | |
| 465 | pub fn focus_down_or_right(&mut self) { |
| 466 | self.active_workspace().focus_down_or_right(); |
| 467 | } |
| 468 | |
| 469 | pub fn focus_up_or_left(&mut self) { |
| 470 | self.active_workspace().focus_up_or_left(); |
| 471 | } |
| 472 | |
| 473 | pub fn focus_up_or_right(&mut self) { |
| 474 | self.active_workspace().focus_up_or_right(); |
| 475 | } |
| 476 | |
| 477 | pub fn focus_window_or_workspace_down(&mut self) { |
| 478 | if !self.active_workspace().focus_down() { |
| 479 | self.switch_workspace_down(); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | pub fn focus_window_or_workspace_up(&mut self) { |
| 484 | if !self.active_workspace().focus_up() { |
| 485 | self.switch_workspace_up(); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | pub fn move_to_workspace_up(&mut self) { |
| 490 | let source_workspace_idx = self.active_workspace_idx; |
| 491 | |
| 492 | let new_idx = source_workspace_idx.saturating_sub(1); |
| 493 | if new_idx == source_workspace_idx { |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 498 | let Some(removed) = workspace.remove_active_tile(Transaction::new()) else { |
| 499 | return; |
| 500 | }; |
| 501 | |
| 502 | self.add_window( |
| 503 | new_idx, |
| 504 | removed.tile.into_window(), |
| 505 | true, |
| 506 | removed.width, |
| 507 | removed.is_full_width, |
| 508 | ); |
| 509 | } |
| 510 | |
| 511 | pub fn move_to_workspace_down(&mut self) { |
| 512 | let source_workspace_idx = self.active_workspace_idx; |
| 513 | |
| 514 | let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1); |
| 515 | if new_idx == source_workspace_idx { |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 520 | let Some(removed) = workspace.remove_active_tile(Transaction::new()) else { |
| 521 | return; |
| 522 | }; |
| 523 | |
| 524 | self.add_window( |
| 525 | new_idx, |
| 526 | removed.tile.into_window(), |
| 527 | true, |
| 528 | removed.width, |
| 529 | removed.is_full_width, |
| 530 | ); |
| 531 | } |
| 532 | |
| 533 | pub fn move_to_workspace(&mut self, window: Option<&W::Id>, idx: usize) { |
| 534 | let source_workspace_idx = if let Some(window) = window { |
| 535 | self.workspaces |
| 536 | .iter() |
| 537 | .position(|ws| ws.has_window(window)) |
| 538 | .unwrap() |
| 539 | } else { |
| 540 | self.active_workspace_idx |
| 541 | }; |
| 542 | |
| 543 | let new_idx = min(idx, self.workspaces.len() - 1); |
| 544 | if new_idx == source_workspace_idx { |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | let activate = window.map_or(true, |win| { |
| 549 | self.active_window().map(|win| win.id()) == Some(win) |
| 550 | }); |
| 551 | |
| 552 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 553 | let transaction = Transaction::new(); |
| 554 | let removed = if let Some(window) = window { |
| 555 | workspace.remove_tile(window, transaction) |
| 556 | } else if let Some(removed) = workspace.remove_active_tile(transaction) { |
| 557 | removed |
| 558 | } else { |
| 559 | return; |
| 560 | }; |
| 561 | |
| 562 | self.add_window( |
| 563 | new_idx, |
| 564 | removed.tile.into_window(), |
| 565 | activate, |
| 566 | removed.width, |
| 567 | removed.is_full_width, |
| 568 | ); |
| 569 | |
| 570 | if self.workspace_switch.is_none() { |
| 571 | self.clean_up_workspaces(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | pub fn move_column_to_workspace_up(&mut self) { |
| 576 | let source_workspace_idx = self.active_workspace_idx; |
| 577 | |
| 578 | let new_idx = source_workspace_idx.saturating_sub(1); |
| 579 | if new_idx == source_workspace_idx { |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 584 | let Some(column) = workspace.remove_active_column() else { |
| 585 | return; |
| 586 | }; |
| 587 | |
| 588 | self.add_column(new_idx, column, true); |
| 589 | } |
| 590 | |
| 591 | pub fn move_column_to_workspace_down(&mut self) { |
| 592 | let source_workspace_idx = self.active_workspace_idx; |
| 593 | |
| 594 | let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1); |
| 595 | if new_idx == source_workspace_idx { |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 600 | let Some(column) = workspace.remove_active_column() else { |
| 601 | return; |
| 602 | }; |
| 603 | |
| 604 | self.add_column(new_idx, column, true); |
| 605 | } |
| 606 | |
| 607 | pub fn move_column_to_workspace(&mut self, idx: usize) { |
| 608 | let source_workspace_idx = self.active_workspace_idx; |
| 609 | |
| 610 | let new_idx = min(idx, self.workspaces.len() - 1); |
| 611 | if new_idx == source_workspace_idx { |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 616 | let Some(column) = workspace.remove_active_column() else { |
| 617 | return; |
| 618 | }; |
| 619 | |
| 620 | self.add_column(new_idx, column, true); |
| 621 | } |
| 622 | |
| 623 | pub fn switch_workspace_up(&mut self) { |
| 624 | self.activate_workspace(self.active_workspace_idx.saturating_sub(1)); |
| 625 | } |
| 626 | |
| 627 | pub fn switch_workspace_down(&mut self) { |
| 628 | self.activate_workspace(min( |
| 629 | self.active_workspace_idx + 1, |
| 630 | self.workspaces.len() - 1, |
| 631 | )); |
| 632 | } |
| 633 | |
| 634 | fn previous_workspace_idx(&self) -> Option<usize> { |
| 635 | let id = self.previous_workspace_id?; |
| 636 | self.workspaces.iter().position(|w| w.id() == id) |
| 637 | } |
| 638 | |
| 639 | pub fn switch_workspace(&mut self, idx: usize) { |
| 640 | self.activate_workspace(min(idx, self.workspaces.len() - 1)); |
| 641 | } |
| 642 | |
| 643 | pub fn switch_workspace_auto_back_and_forth(&mut self, idx: usize) { |
| 644 | let idx = min(idx, self.workspaces.len() - 1); |
| 645 | |
| 646 | if idx == self.active_workspace_idx { |
| 647 | if let Some(prev_idx) = self.previous_workspace_idx() { |
| 648 | self.switch_workspace(prev_idx); |
| 649 | } |
| 650 | } else { |
| 651 | self.switch_workspace(idx); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | pub fn switch_workspace_previous(&mut self) { |
| 656 | if let Some(idx) = self.previous_workspace_idx() { |
| 657 | self.switch_workspace(idx); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | pub fn consume_into_column(&mut self) { |
| 662 | self.active_workspace().consume_into_column(); |
| 663 | } |
| 664 | |
| 665 | pub fn expel_from_column(&mut self) { |
| 666 | self.active_workspace().expel_from_column(); |
| 667 | } |
| 668 | |
| 669 | pub fn center_column(&mut self) { |
| 670 | self.active_workspace().center_column(); |
| 671 | } |
| 672 | |
| 673 | pub fn active_window(&self) -> Option<&W> { |
| 674 | self.active_workspace_ref().active_window() |
| 675 | } |
| 676 | |
| 677 | pub fn is_active_fullscreen(&self) -> bool { |
| 678 | self.active_workspace_ref().is_active_fullscreen() |
| 679 | } |
| 680 | |
| 681 | pub fn advance_animations(&mut self) { |
| 682 | if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch { |
| 683 | if anim.is_done() { |
| 684 | self.workspace_switch = None; |
| 685 | self.clean_up_workspaces(); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | for ws in &mut self.workspaces { |
| 690 | ws.advance_animations(); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | pub(super) fn are_animations_ongoing(&self) -> bool { |
| 695 | self.workspace_switch |
| 696 | .as_ref() |
| 697 | .is_some_and(|s| s.is_animation()) |
| 698 | || self.workspaces.iter().any(|ws| ws.are_animations_ongoing()) |
| 699 | } |
| 700 | |
| 701 | pub fn are_transitions_ongoing(&self) -> bool { |
| 702 | self.workspace_switch.is_some() |
| 703 | || self |
| 704 | .workspaces |
| 705 | .iter() |
| 706 | .any(|ws| ws.are_transitions_ongoing()) |
| 707 | } |
| 708 | |
| 709 | pub fn update_render_elements(&mut self, is_active: bool) { |
| 710 | match &self.workspace_switch { |
| 711 | Some(switch) => { |
| 712 | let render_idx = switch.current_idx(); |
| 713 | let before_idx = render_idx.floor(); |
| 714 | let after_idx = render_idx.ceil(); |
| 715 | |
| 716 | if after_idx < 0. || before_idx as usize >= self.workspaces.len() { |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | let after_idx = after_idx as usize; |
| 721 | if after_idx < self.workspaces.len() { |
| 722 | self.workspaces[after_idx].update_render_elements(is_active); |
| 723 | |
| 724 | if before_idx < 0. { |
| 725 | return; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | let before_idx = before_idx as usize; |
| 730 | self.workspaces[before_idx].update_render_elements(is_active); |
| 731 | } |
| 732 | None => { |
| 733 | self.workspaces[self.active_workspace_idx].update_render_elements(is_active); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | pub fn update_config(&mut self, options: Rc<Options>) { |
| 739 | if self.options.empty_workspace_above_first != options.empty_workspace_above_first |
| 740 | && self.workspaces.len() > 1 |
| 741 | { |
| 742 | if options.empty_workspace_above_first { |
| 743 | self.add_workspace_top(); |
| 744 | } else if self.workspace_switch.is_none() && self.active_workspace_idx != 0 { |
| 745 | self.workspaces.remove(0); |
| 746 | self.active_workspace_idx = self.active_workspace_idx.saturating_sub(1); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | for ws in &mut self.workspaces { |
| 751 | ws.update_config(options.clone()); |
| 752 | } |
| 753 | |
| 754 | self.options = options; |
| 755 | } |
| 756 | |
| 757 | pub fn toggle_width(&mut self) { |
| 758 | self.active_workspace().toggle_width(); |
| 759 | } |
| 760 | |
| 761 | pub fn toggle_full_width(&mut self) { |
| 762 | self.active_workspace().toggle_full_width(); |
| 763 | } |
| 764 | |
| 765 | pub fn set_column_width(&mut self, change: SizeChange) { |
| 766 | self.active_workspace().set_column_width(change); |
| 767 | } |
| 768 | |
| 769 | pub fn move_workspace_down(&mut self) { |
| 770 | let mut new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1); |
| 771 | if new_idx == self.active_workspace_idx { |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | self.workspaces.swap(self.active_workspace_idx, new_idx); |
| 776 | |
| 777 | if new_idx == self.workspaces.len() - 1 { |
| 778 | // Insert a new empty workspace. |
| 779 | self.add_workspace_bottom(); |
| 780 | } |
| 781 | |
| 782 | if self.options.empty_workspace_above_first && self.active_workspace_idx == 0 { |
| 783 | self.add_workspace_top(); |
| 784 | new_idx += 1; |
| 785 | } |
| 786 | |
| 787 | let previous_workspace_id = self.previous_workspace_id; |
| 788 | self.activate_workspace(new_idx); |
| 789 | self.workspace_switch = None; |
| 790 | self.previous_workspace_id = previous_workspace_id; |
| 791 | |
| 792 | self.clean_up_workspaces(); |
| 793 | } |
| 794 | |
| 795 | pub fn move_workspace_up(&mut self) { |
| 796 | let mut new_idx = self.active_workspace_idx.saturating_sub(1); |
| 797 | if new_idx == self.active_workspace_idx { |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | self.workspaces.swap(self.active_workspace_idx, new_idx); |
| 802 | |
| 803 | if self.active_workspace_idx == self.workspaces.len() - 1 { |
| 804 | // Insert a new empty workspace. |
| 805 | self.add_workspace_bottom(); |
| 806 | } |
| 807 | |
| 808 | if self.options.empty_workspace_above_first && new_idx == 0 { |
| 809 | self.add_workspace_top(); |
| 810 | new_idx += 1; |
| 811 | } |
| 812 | |
| 813 | let previous_workspace_id = self.previous_workspace_id; |
| 814 | self.activate_workspace(new_idx); |
| 815 | self.workspace_switch = None; |
| 816 | self.previous_workspace_id = previous_workspace_id; |
| 817 | |
| 818 | self.clean_up_workspaces(); |
| 819 | } |
| 820 | |
| 821 | /// Returns the geometry of the active tile relative to and clamped to the output. |
| 822 | /// |
| 823 | /// During animations, assumes the final view position. |
| 824 | pub fn active_tile_visual_rectangle(&self) -> Option<Rectangle<f64, Logical>> { |
| 825 | let mut rect = self.active_workspace_ref().active_tile_visual_rectangle()?; |
| 826 | |
| 827 | if let Some(switch) = &self.workspace_switch { |
| 828 | let size = output_size(&self.output).to_f64(); |
| 829 | |
| 830 | let offset = switch.target_idx() - self.active_workspace_idx as f64; |
| 831 | let offset = offset * size.h; |
| 832 | |
| 833 | let clip_rect = Rectangle::from_loc_and_size((0., -offset), size); |
| 834 | rect = rect.intersection(clip_rect)?; |
| 835 | } |
| 836 | |
| 837 | Some(rect) |
| 838 | } |
| 839 | |
| 840 | pub fn workspaces_with_render_positions( |
| 841 | &self, |
| 842 | ) -> impl Iterator<Item = (&Workspace<W>, Point<f64, Logical>)> { |
| 843 | let mut first = None; |
| 844 | let mut second = None; |
| 845 | |
| 846 | match &self.workspace_switch { |
| 847 | Some(switch) => { |
| 848 | let render_idx = switch.current_idx(); |
| 849 | let before_idx = render_idx.floor(); |
| 850 | let after_idx = render_idx.ceil(); |
| 851 | |
| 852 | if after_idx >= 0. && before_idx < self.workspaces.len() as f64 { |
| 853 | let scale = self.output.current_scale().fractional_scale(); |
| 854 | let size = output_size(&self.output); |
| 855 | let offset = |
| 856 | round_logical_in_physical(scale, (render_idx - before_idx) * size.h); |
| 857 | |
| 858 | // Ceil the height in physical pixels. |
| 859 | let height = (size.h * scale).ceil() / scale; |
| 860 | |
| 861 | if before_idx >= 0. { |
| 862 | let before_idx = before_idx as usize; |
| 863 | let before_offset = Point::from((0., -offset)); |
| 864 | first = Some((&self.workspaces[before_idx], before_offset)); |
| 865 | } |
| 866 | |
| 867 | let after_idx = after_idx as usize; |
| 868 | if after_idx < self.workspaces.len() { |
| 869 | let after_offset = Point::from((0., -offset + height)); |
| 870 | second = Some((&self.workspaces[after_idx], after_offset)); |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | None => { |
| 875 | first = Some(( |
| 876 | &self.workspaces[self.active_workspace_idx], |
| 877 | Point::from((0., 0.)), |
| 878 | )); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | first.into_iter().chain(second) |
| 883 | } |
| 884 | |
| 885 | pub fn workspace_under( |
| 886 | &self, |
| 887 | pos_within_output: Point<f64, Logical>, |
| 888 | ) -> Option<(&Workspace<W>, Point<f64, Logical>)> { |
| 889 | let size = output_size(&self.output); |
| 890 | let (ws, bounds) = self |
| 891 | .workspaces_with_render_positions() |
| 892 | .map(|(ws, offset)| (ws, Rectangle::from_loc_and_size(offset, size))) |
| 893 | .find(|(_, bounds)| bounds.contains(pos_within_output))?; |
| 894 | Some((ws, bounds.loc)) |
| 895 | } |
| 896 | |
| 897 | pub fn window_under( |
| 898 | &self, |
| 899 | pos_within_output: Point<f64, Logical>, |
| 900 | ) -> Option<(&W, Option<Point<f64, Logical>>)> { |
| 901 | let (ws, offset) = self.workspace_under(pos_within_output)?; |
| 902 | let (win, win_pos) = ws.window_under(pos_within_output - offset)?; |
| 903 | Some((win, win_pos.map(|p| p + offset))) |
| 904 | } |
| 905 | |
| 906 | pub fn resize_edges_under(&self, pos_within_output: Point<f64, Logical>) -> Option<ResizeEdge> { |
| 907 | let (ws, offset) = self.workspace_under(pos_within_output)?; |
| 908 | ws.resize_edges_under(pos_within_output - offset) |
| 909 | } |
| 910 | |
| 911 | pub fn render_above_top_layer(&self) -> bool { |
| 912 | // Render above the top layer only if the view is stationary. |
| 913 | if self.workspace_switch.is_some() { |
| 914 | return false; |
| 915 | } |
| 916 | |
| 917 | let ws = &self.workspaces[self.active_workspace_idx]; |
| 918 | ws.render_above_top_layer() |
| 919 | } |
| 920 | |
| 921 | pub fn render_elements<'a, R: NiriRenderer>( |
| 922 | &'a self, |
| 923 | renderer: &'a mut R, |
| 924 | target: RenderTarget, |
| 925 | ) -> impl Iterator<Item = MonitorRenderElement<R>> + 'a { |
| 926 | let _span = tracy_client::span!("Monitor::render_elements"); |
| 927 | |
| 928 | let scale = self.output.current_scale().fractional_scale(); |
| 929 | let size = output_size(&self.output); |
| 930 | // Ceil the height in physical pixels. |
| 931 | let height = (size.h * scale).ceil() as i32; |
| 932 | |
| 933 | // Crop the elements to prevent them overflowing, currently visible during a workspace |
| 934 | // switch. |
| 935 | // |
| 936 | // HACK: crop to infinite bounds at least horizontally where we |
| 937 | // know there's no workspace joining or monitor bounds, otherwise |
| 938 | // it will cut pixel shaders and mess up the coordinate space. |
| 939 | // There's also a damage tracking bug which causes glitched |
| 940 | // rendering for maximized GTK windows. |
| 941 | // |
| 942 | // FIXME: use proper bounds after fixing the Crop element. |
| 943 | let crop_bounds = if self.workspace_switch.is_some() { |
| 944 | Rectangle::from_loc_and_size((-i32::MAX / 2, 0), (i32::MAX, height)) |
| 945 | } else { |
| 946 | Rectangle::from_loc_and_size((-i32::MAX / 2, -i32::MAX / 2), (i32::MAX, i32::MAX)) |
| 947 | }; |
| 948 | |
| 949 | self.workspaces_with_render_positions() |
| 950 | .flat_map(move |(ws, offset)| { |
| 951 | ws.render_elements(renderer, target) |
| 952 | .filter_map(move |elem| { |
| 953 | CropRenderElement::from_element(elem, scale, crop_bounds) |
| 954 | }) |
| 955 | .map(move |elem| { |
| 956 | RelocateRenderElement::from_element( |
| 957 | elem, |
| 958 | // The offset we get from workspaces_with_render_positions() is already |
| 959 | // rounded to physical pixels, but it's in the logical coordinate |
| 960 | // space, so we need to convert it to physical. |
| 961 | offset.to_physical_precise_round(scale), |
| 962 | Relocate::Relative, |
| 963 | ) |
| 964 | }) |
| 965 | }) |
| 966 | } |
| 967 | |
| 968 | pub fn workspace_switch_gesture_begin(&mut self, is_touchpad: bool) { |
| 969 | let center_idx = self.active_workspace_idx; |
| 970 | let current_idx = self |
| 971 | .workspace_switch |
| 972 | .as_ref() |
| 973 | .map(|s| s.current_idx()) |
| 974 | .unwrap_or(center_idx as f64); |
| 975 | |
| 976 | let gesture = WorkspaceSwitchGesture { |
| 977 | center_idx, |
| 978 | current_idx, |
| 979 | tracker: SwipeTracker::new(), |
| 980 | is_touchpad, |
| 981 | }; |
| 982 | self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture)); |
| 983 | } |
| 984 | |
| 985 | pub fn workspace_switch_gesture_update( |
| 986 | &mut self, |
| 987 | delta_y: f64, |
| 988 | timestamp: Duration, |
| 989 | is_touchpad: bool, |
| 990 | ) -> Option<bool> { |
| 991 | let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else { |
| 992 | return None; |
| 993 | }; |
| 994 | |
| 995 | if gesture.is_touchpad != is_touchpad { |
| 996 | return None; |
| 997 | } |
| 998 | |
| 999 | gesture.tracker.push(delta_y, timestamp); |
| 1000 | |
| 1001 | let total_height = if gesture.is_touchpad { |
| 1002 | WORKSPACE_GESTURE_MOVEMENT |
| 1003 | } else { |
| 1004 | self.workspaces[0].view_size().h |
| 1005 | }; |
| 1006 | let pos = gesture.tracker.pos() / total_height; |
| 1007 | |
| 1008 | let min = gesture.center_idx.saturating_sub(1) as f64; |
| 1009 | let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64; |
| 1010 | let new_idx = gesture.center_idx as f64 + pos; |
| 1011 | let new_idx = WORKSPACE_GESTURE_RUBBER_BAND.clamp(min, max, new_idx); |
| 1012 | |
| 1013 | if gesture.current_idx == new_idx { |
| 1014 | return Some(false); |
| 1015 | } |
| 1016 | |
| 1017 | gesture.current_idx = new_idx; |
| 1018 | Some(true) |
| 1019 | } |
| 1020 | |
| 1021 | pub fn workspace_switch_gesture_end( |
| 1022 | &mut self, |
| 1023 | cancelled: bool, |
| 1024 | is_touchpad: Option<bool>, |
| 1025 | ) -> bool { |
| 1026 | let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else { |
| 1027 | return false; |
| 1028 | }; |
| 1029 | |
| 1030 | if is_touchpad.map_or(false, |x| gesture.is_touchpad != x) { |
| 1031 | return false; |
| 1032 | } |
| 1033 | |
| 1034 | if cancelled { |
| 1035 | self.workspace_switch = None; |
| 1036 | self.clean_up_workspaces(); |
| 1037 | return true; |
| 1038 | } |
| 1039 | |
| 1040 | let total_height = if gesture.is_touchpad { |
| 1041 | WORKSPACE_GESTURE_MOVEMENT |
| 1042 | } else { |
| 1043 | self.workspaces[0].view_size().h |
| 1044 | }; |
| 1045 | |
| 1046 | let mut velocity = gesture.tracker.velocity() / total_height; |
| 1047 | let current_pos = gesture.tracker.pos() / total_height; |
| 1048 | let pos = gesture.tracker.projected_end_pos() / total_height; |
| 1049 | |
| 1050 | let min = gesture.center_idx.saturating_sub(1) as f64; |
| 1051 | let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64; |
| 1052 | let new_idx = gesture.center_idx as f64 + pos; |
| 1053 | |
| 1054 | let new_idx = WORKSPACE_GESTURE_RUBBER_BAND.clamp(min, max, new_idx); |
| 1055 | let new_idx = new_idx.round() as usize; |
| 1056 | |
| 1057 | velocity *= WORKSPACE_GESTURE_RUBBER_BAND.clamp_derivative( |
| 1058 | min, |
| 1059 | max, |
| 1060 | gesture.center_idx as f64 + current_pos, |
| 1061 | ); |
| 1062 | |
| 1063 | self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id()); |
| 1064 | |
| 1065 | self.active_workspace_idx = new_idx; |
| 1066 | self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new( |
| 1067 | self.clock.clone(), |
| 1068 | gesture.current_idx, |
| 1069 | new_idx as f64, |
| 1070 | velocity, |
| 1071 | self.options.animations.workspace_switch.0, |
| 1072 | ))); |
| 1073 | |
| 1074 | true |
| 1075 | } |
| 1076 | } |