niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·f51dd67·Ivan Molodetskikh·2 years ago·981 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, Scale}; |
| 11 | |
| 12 | use super::workspace::{ |
| 13 | compute_working_area, Column, ColumnWidth, OutputId, Workspace, WorkspaceId, |
| 14 | WorkspaceRenderElement, |
| 15 | }; |
| 16 | use super::{LayoutElement, Options}; |
| 17 | use crate::animation::Animation; |
| 18 | use crate::render_helpers::renderer::NiriRenderer; |
| 19 | use crate::render_helpers::RenderTarget; |
| 20 | use crate::rubber_band::RubberBand; |
| 21 | use crate::swipe_tracker::SwipeTracker; |
| 22 | use crate::utils::output_size; |
| 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 output: Output, |
| 36 | // Must always contain at least one. |
| 37 | pub workspaces: Vec<Workspace<W>>, |
| 38 | /// Index of the currently active workspace. |
| 39 | pub active_workspace_idx: usize, |
| 40 | /// ID of the previously active workspace. |
| 41 | pub previous_workspace_id: Option<WorkspaceId>, |
| 42 | /// In-progress switch between workspaces. |
| 43 | pub workspace_switch: Option<WorkspaceSwitch>, |
| 44 | /// Configurable properties of the layout. |
| 45 | pub options: Rc<Options>, |
| 46 | } |
| 47 | |
| 48 | #[derive(Debug)] |
| 49 | pub enum WorkspaceSwitch { |
| 50 | Animation(Animation), |
| 51 | Gesture(WorkspaceSwitchGesture), |
| 52 | } |
| 53 | |
| 54 | #[derive(Debug)] |
| 55 | pub struct WorkspaceSwitchGesture { |
| 56 | /// Index of the workspace where the gesture was started. |
| 57 | pub center_idx: usize, |
| 58 | /// Current, fractional workspace index. |
| 59 | pub current_idx: f64, |
| 60 | pub tracker: SwipeTracker, |
| 61 | } |
| 62 | |
| 63 | pub type MonitorRenderElement<R> = |
| 64 | RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>; |
| 65 | |
| 66 | impl WorkspaceSwitch { |
| 67 | pub fn current_idx(&self) -> f64 { |
| 68 | match self { |
| 69 | WorkspaceSwitch::Animation(anim) => anim.value(), |
| 70 | WorkspaceSwitch::Gesture(gesture) => gesture.current_idx, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | pub fn target_idx(&self) -> f64 { |
| 75 | match self { |
| 76 | WorkspaceSwitch::Animation(anim) => anim.to(), |
| 77 | WorkspaceSwitch::Gesture(gesture) => gesture.current_idx, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Returns `true` if the workspace switch is [`Animation`]. |
| 82 | /// |
| 83 | /// [`Animation`]: WorkspaceSwitch::Animation |
| 84 | #[must_use] |
| 85 | fn is_animation(&self) -> bool { |
| 86 | matches!(self, Self::Animation(..)) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl<W: LayoutElement> Monitor<W> { |
| 91 | pub fn new(output: Output, workspaces: Vec<Workspace<W>>, options: Rc<Options>) -> Self { |
| 92 | Self { |
| 93 | output, |
| 94 | workspaces, |
| 95 | active_workspace_idx: 0, |
| 96 | previous_workspace_id: None, |
| 97 | workspace_switch: None, |
| 98 | options, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn active_workspace_ref(&self) -> &Workspace<W> { |
| 103 | &self.workspaces[self.active_workspace_idx] |
| 104 | } |
| 105 | |
| 106 | pub fn active_workspace(&mut self) -> &mut Workspace<W> { |
| 107 | &mut self.workspaces[self.active_workspace_idx] |
| 108 | } |
| 109 | |
| 110 | fn activate_workspace(&mut self, idx: usize) { |
| 111 | if self.active_workspace_idx == idx { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // FIXME: also compute and use current velocity. |
| 116 | let current_idx = self |
| 117 | .workspace_switch |
| 118 | .as_ref() |
| 119 | .map(|s| s.current_idx()) |
| 120 | .unwrap_or(self.active_workspace_idx as f64); |
| 121 | |
| 122 | self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id()); |
| 123 | |
| 124 | self.active_workspace_idx = idx; |
| 125 | |
| 126 | self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new( |
| 127 | current_idx, |
| 128 | idx as f64, |
| 129 | 0., |
| 130 | self.options.animations.workspace_switch.0, |
| 131 | ))); |
| 132 | } |
| 133 | |
| 134 | pub fn add_window( |
| 135 | &mut self, |
| 136 | workspace_idx: usize, |
| 137 | window: W, |
| 138 | activate: bool, |
| 139 | width: ColumnWidth, |
| 140 | is_full_width: bool, |
| 141 | ) { |
| 142 | let workspace = &mut self.workspaces[workspace_idx]; |
| 143 | |
| 144 | workspace.add_window(window, activate, width, is_full_width); |
| 145 | |
| 146 | // After adding a new window, workspace becomes this output's own. |
| 147 | workspace.original_output = OutputId::new(&self.output); |
| 148 | |
| 149 | if workspace_idx == self.workspaces.len() - 1 { |
| 150 | // Insert a new empty workspace. |
| 151 | let ws = Workspace::new(self.output.clone(), self.options.clone()); |
| 152 | self.workspaces.push(ws); |
| 153 | } |
| 154 | |
| 155 | if activate { |
| 156 | self.activate_workspace(workspace_idx); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | pub fn add_window_right_of( |
| 161 | &mut self, |
| 162 | right_of: &W::Id, |
| 163 | window: W, |
| 164 | width: ColumnWidth, |
| 165 | is_full_width: bool, |
| 166 | ) { |
| 167 | let workspace_idx = self |
| 168 | .workspaces |
| 169 | .iter_mut() |
| 170 | .position(|ws| ws.has_window(right_of)) |
| 171 | .unwrap(); |
| 172 | let workspace = &mut self.workspaces[workspace_idx]; |
| 173 | |
| 174 | workspace.add_window_right_of(right_of, window, width, is_full_width); |
| 175 | |
| 176 | // After adding a new window, workspace becomes this output's own. |
| 177 | workspace.original_output = OutputId::new(&self.output); |
| 178 | } |
| 179 | |
| 180 | pub fn add_column(&mut self, workspace_idx: usize, column: Column<W>, activate: bool) { |
| 181 | let workspace = &mut self.workspaces[workspace_idx]; |
| 182 | |
| 183 | workspace.add_column(column, activate); |
| 184 | |
| 185 | // After adding a new window, workspace becomes this output's own. |
| 186 | workspace.original_output = OutputId::new(&self.output); |
| 187 | |
| 188 | if workspace_idx == self.workspaces.len() - 1 { |
| 189 | // Insert a new empty workspace. |
| 190 | let ws = Workspace::new(self.output.clone(), self.options.clone()); |
| 191 | self.workspaces.push(ws); |
| 192 | } |
| 193 | |
| 194 | if activate { |
| 195 | self.activate_workspace(workspace_idx); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | pub fn clean_up_workspaces(&mut self) { |
| 200 | assert!(self.workspace_switch.is_none()); |
| 201 | |
| 202 | for idx in (0..self.workspaces.len() - 1).rev() { |
| 203 | if self.active_workspace_idx == idx { |
| 204 | continue; |
| 205 | } |
| 206 | |
| 207 | if !self.workspaces[idx].has_windows() { |
| 208 | self.workspaces.remove(idx); |
| 209 | if self.active_workspace_idx > idx { |
| 210 | self.active_workspace_idx -= 1; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | pub fn move_left(&mut self) { |
| 217 | self.active_workspace().move_left(); |
| 218 | } |
| 219 | |
| 220 | pub fn move_right(&mut self) { |
| 221 | self.active_workspace().move_right(); |
| 222 | } |
| 223 | |
| 224 | pub fn move_column_to_first(&mut self) { |
| 225 | self.active_workspace().move_column_to_first(); |
| 226 | } |
| 227 | |
| 228 | pub fn move_column_to_last(&mut self) { |
| 229 | self.active_workspace().move_column_to_last(); |
| 230 | } |
| 231 | |
| 232 | pub fn move_down(&mut self) { |
| 233 | self.active_workspace().move_down(); |
| 234 | } |
| 235 | |
| 236 | pub fn move_up(&mut self) { |
| 237 | self.active_workspace().move_up(); |
| 238 | } |
| 239 | |
| 240 | pub fn move_down_or_to_workspace_down(&mut self) { |
| 241 | let workspace = self.active_workspace(); |
| 242 | if workspace.columns.is_empty() { |
| 243 | return; |
| 244 | } |
| 245 | let column = &mut workspace.columns[workspace.active_column_idx]; |
| 246 | let curr_idx = column.active_tile_idx; |
| 247 | let new_idx = min(column.active_tile_idx + 1, column.tiles.len() - 1); |
| 248 | if curr_idx == new_idx { |
| 249 | self.move_to_workspace_down(); |
| 250 | } else { |
| 251 | workspace.move_down(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | pub fn move_up_or_to_workspace_up(&mut self) { |
| 256 | let workspace = self.active_workspace(); |
| 257 | if workspace.columns.is_empty() { |
| 258 | return; |
| 259 | } |
| 260 | let curr_idx = workspace.columns[workspace.active_column_idx].active_tile_idx; |
| 261 | let new_idx = curr_idx.saturating_sub(1); |
| 262 | if curr_idx == new_idx { |
| 263 | self.move_to_workspace_up(); |
| 264 | } else { |
| 265 | workspace.move_up(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | pub fn consume_or_expel_window_left(&mut self) { |
| 270 | self.active_workspace().consume_or_expel_window_left(); |
| 271 | } |
| 272 | |
| 273 | pub fn consume_or_expel_window_right(&mut self) { |
| 274 | self.active_workspace().consume_or_expel_window_right(); |
| 275 | } |
| 276 | |
| 277 | pub fn focus_left(&mut self) { |
| 278 | self.active_workspace().focus_left(); |
| 279 | } |
| 280 | |
| 281 | pub fn focus_right(&mut self) { |
| 282 | self.active_workspace().focus_right(); |
| 283 | } |
| 284 | |
| 285 | pub fn focus_column_first(&mut self) { |
| 286 | self.active_workspace().focus_column_first(); |
| 287 | } |
| 288 | |
| 289 | pub fn focus_column_last(&mut self) { |
| 290 | self.active_workspace().focus_column_last(); |
| 291 | } |
| 292 | |
| 293 | pub fn focus_down(&mut self) { |
| 294 | self.active_workspace().focus_down(); |
| 295 | } |
| 296 | |
| 297 | pub fn focus_up(&mut self) { |
| 298 | self.active_workspace().focus_up(); |
| 299 | } |
| 300 | |
| 301 | pub fn focus_window_or_workspace_down(&mut self) { |
| 302 | let workspace = self.active_workspace(); |
| 303 | if workspace.columns.is_empty() { |
| 304 | self.switch_workspace_down(); |
| 305 | } else { |
| 306 | let column = &workspace.columns[workspace.active_column_idx]; |
| 307 | let curr_idx = column.active_tile_idx; |
| 308 | let new_idx = min(column.active_tile_idx + 1, column.tiles.len() - 1); |
| 309 | if curr_idx == new_idx { |
| 310 | self.switch_workspace_down(); |
| 311 | } else { |
| 312 | workspace.focus_down(); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | pub fn focus_window_or_workspace_up(&mut self) { |
| 318 | let workspace = self.active_workspace(); |
| 319 | if workspace.columns.is_empty() { |
| 320 | self.switch_workspace_up(); |
| 321 | } else { |
| 322 | let curr_idx = workspace.columns[workspace.active_column_idx].active_tile_idx; |
| 323 | let new_idx = curr_idx.saturating_sub(1); |
| 324 | if curr_idx == new_idx { |
| 325 | self.switch_workspace_up(); |
| 326 | } else { |
| 327 | workspace.focus_up(); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | pub fn move_to_workspace_up(&mut self) { |
| 333 | let source_workspace_idx = self.active_workspace_idx; |
| 334 | |
| 335 | let new_idx = source_workspace_idx.saturating_sub(1); |
| 336 | if new_idx == source_workspace_idx { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 341 | if workspace.columns.is_empty() { |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | let column = &workspace.columns[workspace.active_column_idx]; |
| 346 | let width = column.width; |
| 347 | let is_full_width = column.is_full_width; |
| 348 | let window = workspace |
| 349 | .remove_tile_by_idx(workspace.active_column_idx, column.active_tile_idx) |
| 350 | .into_window(); |
| 351 | |
| 352 | self.add_window(new_idx, window, true, width, is_full_width); |
| 353 | } |
| 354 | |
| 355 | pub fn move_to_workspace_down(&mut self) { |
| 356 | let source_workspace_idx = self.active_workspace_idx; |
| 357 | |
| 358 | let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1); |
| 359 | if new_idx == source_workspace_idx { |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 364 | if workspace.columns.is_empty() { |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | let column = &workspace.columns[workspace.active_column_idx]; |
| 369 | let width = column.width; |
| 370 | let is_full_width = column.is_full_width; |
| 371 | let window = workspace |
| 372 | .remove_tile_by_idx(workspace.active_column_idx, column.active_tile_idx) |
| 373 | .into_window(); |
| 374 | |
| 375 | self.add_window(new_idx, window, true, width, is_full_width); |
| 376 | } |
| 377 | |
| 378 | pub fn move_to_workspace(&mut self, idx: usize) { |
| 379 | let source_workspace_idx = self.active_workspace_idx; |
| 380 | |
| 381 | let new_idx = min(idx, self.workspaces.len() - 1); |
| 382 | if new_idx == source_workspace_idx { |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 387 | if workspace.columns.is_empty() { |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | let column = &workspace.columns[workspace.active_column_idx]; |
| 392 | let width = column.width; |
| 393 | let is_full_width = column.is_full_width; |
| 394 | let window = workspace |
| 395 | .remove_tile_by_idx(workspace.active_column_idx, column.active_tile_idx) |
| 396 | .into_window(); |
| 397 | |
| 398 | self.add_window(new_idx, window, true, width, is_full_width); |
| 399 | |
| 400 | // Don't animate this action. |
| 401 | self.workspace_switch = None; |
| 402 | |
| 403 | self.clean_up_workspaces(); |
| 404 | } |
| 405 | |
| 406 | pub fn move_column_to_workspace_up(&mut self) { |
| 407 | let source_workspace_idx = self.active_workspace_idx; |
| 408 | |
| 409 | let new_idx = source_workspace_idx.saturating_sub(1); |
| 410 | if new_idx == source_workspace_idx { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 415 | if workspace.columns.is_empty() { |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | let column = workspace.remove_column_by_idx(workspace.active_column_idx); |
| 420 | self.add_column(new_idx, column, true); |
| 421 | } |
| 422 | |
| 423 | pub fn move_column_to_workspace_down(&mut self) { |
| 424 | let source_workspace_idx = self.active_workspace_idx; |
| 425 | |
| 426 | let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1); |
| 427 | if new_idx == source_workspace_idx { |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 432 | if workspace.columns.is_empty() { |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | let column = workspace.remove_column_by_idx(workspace.active_column_idx); |
| 437 | self.add_column(new_idx, column, true); |
| 438 | } |
| 439 | |
| 440 | pub fn move_column_to_workspace(&mut self, idx: usize) { |
| 441 | let source_workspace_idx = self.active_workspace_idx; |
| 442 | |
| 443 | let new_idx = min(idx, self.workspaces.len() - 1); |
| 444 | if new_idx == source_workspace_idx { |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 449 | if workspace.columns.is_empty() { |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | let column = workspace.remove_column_by_idx(workspace.active_column_idx); |
| 454 | self.add_column(new_idx, column, true); |
| 455 | |
| 456 | // Don't animate this action. |
| 457 | self.workspace_switch = None; |
| 458 | |
| 459 | self.clean_up_workspaces(); |
| 460 | } |
| 461 | |
| 462 | pub fn switch_workspace_up(&mut self) { |
| 463 | self.activate_workspace(self.active_workspace_idx.saturating_sub(1)); |
| 464 | } |
| 465 | |
| 466 | pub fn switch_workspace_down(&mut self) { |
| 467 | self.activate_workspace(min( |
| 468 | self.active_workspace_idx + 1, |
| 469 | self.workspaces.len() - 1, |
| 470 | )); |
| 471 | } |
| 472 | |
| 473 | fn previous_workspace_idx(&self) -> Option<usize> { |
| 474 | let id = self.previous_workspace_id?; |
| 475 | self.workspaces.iter().position(|w| w.id() == id) |
| 476 | } |
| 477 | |
| 478 | pub fn switch_workspace(&mut self, idx: usize) { |
| 479 | self.activate_workspace(min(idx, self.workspaces.len() - 1)); |
| 480 | // Don't animate this action. |
| 481 | self.workspace_switch = None; |
| 482 | |
| 483 | self.clean_up_workspaces(); |
| 484 | } |
| 485 | |
| 486 | pub fn switch_workspace_auto_back_and_forth(&mut self, idx: usize) { |
| 487 | let idx = min(idx, self.workspaces.len() - 1); |
| 488 | |
| 489 | if idx == self.active_workspace_idx { |
| 490 | if let Some(prev_idx) = self.previous_workspace_idx() { |
| 491 | self.switch_workspace(prev_idx); |
| 492 | } |
| 493 | } else { |
| 494 | self.switch_workspace(idx); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | pub fn switch_workspace_previous(&mut self) { |
| 499 | if let Some(idx) = self.previous_workspace_idx() { |
| 500 | self.switch_workspace(idx); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | pub fn consume_into_column(&mut self) { |
| 505 | self.active_workspace().consume_into_column(); |
| 506 | } |
| 507 | |
| 508 | pub fn expel_from_column(&mut self) { |
| 509 | self.active_workspace().expel_from_column(); |
| 510 | } |
| 511 | |
| 512 | pub fn center_column(&mut self) { |
| 513 | self.active_workspace().center_column(); |
| 514 | } |
| 515 | |
| 516 | pub fn focus(&self) -> Option<&W> { |
| 517 | let workspace = &self.workspaces[self.active_workspace_idx]; |
| 518 | if !workspace.has_windows() { |
| 519 | return None; |
| 520 | } |
| 521 | |
| 522 | let column = &workspace.columns[workspace.active_column_idx]; |
| 523 | Some(column.tiles[column.active_tile_idx].window()) |
| 524 | } |
| 525 | |
| 526 | pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) { |
| 527 | if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch { |
| 528 | anim.set_current_time(current_time); |
| 529 | if anim.is_done() { |
| 530 | self.workspace_switch = None; |
| 531 | self.clean_up_workspaces(); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | for ws in &mut self.workspaces { |
| 536 | ws.advance_animations(current_time, is_active); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | pub fn are_animations_ongoing(&self) -> bool { |
| 541 | self.workspace_switch |
| 542 | .as_ref() |
| 543 | .is_some_and(|s| s.is_animation()) |
| 544 | || self.workspaces.iter().any(|ws| ws.are_animations_ongoing()) |
| 545 | } |
| 546 | |
| 547 | pub fn are_transitions_ongoing(&self) -> bool { |
| 548 | self.workspace_switch.is_some() |
| 549 | || self |
| 550 | .workspaces |
| 551 | .iter() |
| 552 | .any(|ws| ws.are_transitions_ongoing()) |
| 553 | } |
| 554 | |
| 555 | pub fn update_config(&mut self, options: Rc<Options>) { |
| 556 | for ws in &mut self.workspaces { |
| 557 | ws.update_config(options.clone()); |
| 558 | } |
| 559 | |
| 560 | if self.options.struts != options.struts { |
| 561 | let view_size = output_size(&self.output); |
| 562 | let working_area = compute_working_area(&self.output, options.struts); |
| 563 | |
| 564 | for ws in &mut self.workspaces { |
| 565 | ws.set_view_size(view_size, working_area); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | self.options = options; |
| 570 | } |
| 571 | |
| 572 | pub fn toggle_width(&mut self) { |
| 573 | self.active_workspace().toggle_width(); |
| 574 | } |
| 575 | |
| 576 | pub fn toggle_full_width(&mut self) { |
| 577 | self.active_workspace().toggle_full_width(); |
| 578 | } |
| 579 | |
| 580 | pub fn set_column_width(&mut self, change: SizeChange) { |
| 581 | self.active_workspace().set_column_width(change); |
| 582 | } |
| 583 | |
| 584 | pub fn set_window_height(&mut self, change: SizeChange) { |
| 585 | self.active_workspace().set_window_height(change); |
| 586 | } |
| 587 | |
| 588 | pub fn move_workspace_down(&mut self) { |
| 589 | let new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1); |
| 590 | if new_idx == self.active_workspace_idx { |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | self.workspaces.swap(self.active_workspace_idx, new_idx); |
| 595 | |
| 596 | if new_idx == self.workspaces.len() - 1 { |
| 597 | // Insert a new empty workspace. |
| 598 | let ws = Workspace::new(self.output.clone(), self.options.clone()); |
| 599 | self.workspaces.push(ws); |
| 600 | } |
| 601 | |
| 602 | let previous_workspace_id = self.previous_workspace_id; |
| 603 | self.activate_workspace(new_idx); |
| 604 | self.workspace_switch = None; |
| 605 | self.previous_workspace_id = previous_workspace_id; |
| 606 | |
| 607 | self.clean_up_workspaces(); |
| 608 | } |
| 609 | |
| 610 | pub fn move_workspace_up(&mut self) { |
| 611 | let new_idx = self.active_workspace_idx.saturating_sub(1); |
| 612 | if new_idx == self.active_workspace_idx { |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | self.workspaces.swap(self.active_workspace_idx, new_idx); |
| 617 | |
| 618 | if self.active_workspace_idx == self.workspaces.len() - 1 { |
| 619 | // Insert a new empty workspace. |
| 620 | let ws = Workspace::new(self.output.clone(), self.options.clone()); |
| 621 | self.workspaces.push(ws); |
| 622 | } |
| 623 | |
| 624 | let previous_workspace_id = self.previous_workspace_id; |
| 625 | self.activate_workspace(new_idx); |
| 626 | self.workspace_switch = None; |
| 627 | self.previous_workspace_id = previous_workspace_id; |
| 628 | |
| 629 | self.clean_up_workspaces(); |
| 630 | } |
| 631 | |
| 632 | /// Returns the geometry of the active tile relative to and clamped to the output. |
| 633 | /// |
| 634 | /// During animations, assumes the final view position. |
| 635 | pub fn active_tile_visual_rectangle(&self) -> Option<Rectangle<i32, Logical>> { |
| 636 | let mut rect = self.active_workspace_ref().active_tile_visual_rectangle()?; |
| 637 | |
| 638 | if let Some(switch) = &self.workspace_switch { |
| 639 | let size = output_size(&self.output); |
| 640 | |
| 641 | let offset = switch.target_idx() - self.active_workspace_idx as f64; |
| 642 | let offset = (offset * size.h as f64).round() as i32; |
| 643 | |
| 644 | let clip_rect = Rectangle::from_loc_and_size((0, -offset), size); |
| 645 | rect = rect.intersection(clip_rect)?; |
| 646 | } |
| 647 | |
| 648 | Some(rect) |
| 649 | } |
| 650 | |
| 651 | pub fn window_under( |
| 652 | &self, |
| 653 | pos_within_output: Point<f64, Logical>, |
| 654 | ) -> Option<(&W, Option<Point<i32, Logical>>)> { |
| 655 | match &self.workspace_switch { |
| 656 | Some(switch) => { |
| 657 | let size = output_size(&self.output); |
| 658 | |
| 659 | let render_idx = switch.current_idx(); |
| 660 | let before_idx = render_idx.floor(); |
| 661 | let after_idx = render_idx.ceil(); |
| 662 | |
| 663 | let offset = ((render_idx - before_idx) * size.h as f64).round() as i32; |
| 664 | |
| 665 | if after_idx < 0. || before_idx as usize >= self.workspaces.len() { |
| 666 | return None; |
| 667 | } |
| 668 | |
| 669 | let after_idx = after_idx as usize; |
| 670 | |
| 671 | let (idx, ws_offset) = if pos_within_output.y < (size.h - offset) as f64 { |
| 672 | if before_idx < 0. { |
| 673 | return None; |
| 674 | } |
| 675 | |
| 676 | (before_idx as usize, Point::from((0, offset))) |
| 677 | } else { |
| 678 | if after_idx >= self.workspaces.len() { |
| 679 | return None; |
| 680 | } |
| 681 | |
| 682 | (after_idx, Point::from((0, -size.h + offset))) |
| 683 | }; |
| 684 | |
| 685 | let ws = &self.workspaces[idx]; |
| 686 | let (win, win_pos) = ws.window_under(pos_within_output + ws_offset.to_f64())?; |
| 687 | Some((win, win_pos.map(|p| p - ws_offset))) |
| 688 | } |
| 689 | None => { |
| 690 | let ws = &self.workspaces[self.active_workspace_idx]; |
| 691 | ws.window_under(pos_within_output) |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | pub fn render_above_top_layer(&self) -> bool { |
| 697 | // Render above the top layer only if the view is stationary. |
| 698 | if self.workspace_switch.is_some() { |
| 699 | return false; |
| 700 | } |
| 701 | |
| 702 | let ws = &self.workspaces[self.active_workspace_idx]; |
| 703 | ws.render_above_top_layer() |
| 704 | } |
| 705 | |
| 706 | pub fn render_elements<R: NiriRenderer>( |
| 707 | &self, |
| 708 | renderer: &mut R, |
| 709 | target: RenderTarget, |
| 710 | ) -> Vec<MonitorRenderElement<R>> { |
| 711 | let _span = tracy_client::span!("Monitor::render_elements"); |
| 712 | |
| 713 | let output_scale = Scale::from(self.output.current_scale().fractional_scale()); |
| 714 | let output_transform = self.output.current_transform(); |
| 715 | let output_mode = self.output.current_mode().unwrap(); |
| 716 | let size = output_transform.transform_size(output_mode.size); |
| 717 | |
| 718 | match &self.workspace_switch { |
| 719 | Some(switch) => { |
| 720 | let render_idx = switch.current_idx(); |
| 721 | let before_idx = render_idx.floor(); |
| 722 | let after_idx = render_idx.ceil(); |
| 723 | |
| 724 | let offset = ((render_idx - before_idx) * size.h as f64).round() as i32; |
| 725 | |
| 726 | if after_idx < 0. || before_idx as usize >= self.workspaces.len() { |
| 727 | return vec![]; |
| 728 | } |
| 729 | |
| 730 | let after_idx = after_idx as usize; |
| 731 | let after = if after_idx < self.workspaces.len() { |
| 732 | let after = self.workspaces[after_idx].render_elements(renderer, target); |
| 733 | let after = after.into_iter().filter_map(|elem| { |
| 734 | Some(RelocateRenderElement::from_element( |
| 735 | CropRenderElement::from_element( |
| 736 | elem, |
| 737 | output_scale, |
| 738 | // HACK: crop to infinite bounds for all sides except the side |
| 739 | // where the workspaces join, |
| 740 | // otherwise it will cut pixel shaders and mess up |
| 741 | // the coordinate space. |
| 742 | Rectangle::from_extemities( |
| 743 | (-i32::MAX / 2, 0), |
| 744 | (i32::MAX / 2, i32::MAX / 2), |
| 745 | ), |
| 746 | )?, |
| 747 | (0, -offset + size.h), |
| 748 | Relocate::Relative, |
| 749 | )) |
| 750 | }); |
| 751 | |
| 752 | if before_idx < 0. { |
| 753 | return after.collect(); |
| 754 | } |
| 755 | |
| 756 | Some(after) |
| 757 | } else { |
| 758 | None |
| 759 | }; |
| 760 | |
| 761 | let before_idx = before_idx as usize; |
| 762 | let before = self.workspaces[before_idx].render_elements(renderer, target); |
| 763 | let before = before.into_iter().filter_map(|elem| { |
| 764 | Some(RelocateRenderElement::from_element( |
| 765 | CropRenderElement::from_element( |
| 766 | elem, |
| 767 | output_scale, |
| 768 | Rectangle::from_extemities( |
| 769 | (-i32::MAX / 2, -i32::MAX / 2), |
| 770 | (i32::MAX / 2, size.h), |
| 771 | ), |
| 772 | )?, |
| 773 | (0, -offset), |
| 774 | Relocate::Relative, |
| 775 | )) |
| 776 | }); |
| 777 | before.chain(after.into_iter().flatten()).collect() |
| 778 | } |
| 779 | None => { |
| 780 | let elements = |
| 781 | self.workspaces[self.active_workspace_idx].render_elements(renderer, target); |
| 782 | elements |
| 783 | .into_iter() |
| 784 | .filter_map(|elem| { |
| 785 | Some(RelocateRenderElement::from_element( |
| 786 | CropRenderElement::from_element( |
| 787 | elem, |
| 788 | output_scale, |
| 789 | // HACK: set infinite crop bounds due to a damage tracking bug |
| 790 | // which causes glitched rendering for maximized GTK windows. |
| 791 | // FIXME: use proper bounds after fixing the Crop element. |
| 792 | Rectangle::from_loc_and_size( |
| 793 | (-i32::MAX / 2, -i32::MAX / 2), |
| 794 | (i32::MAX, i32::MAX), |
| 795 | ), |
| 796 | // Rectangle::from_loc_and_size((0, 0), size), |
| 797 | )?, |
| 798 | (0, 0), |
| 799 | Relocate::Relative, |
| 800 | )) |
| 801 | }) |
| 802 | .collect() |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | pub fn workspace_switch_gesture_begin(&mut self) { |
| 808 | let center_idx = self.active_workspace_idx; |
| 809 | let current_idx = self |
| 810 | .workspace_switch |
| 811 | .as_ref() |
| 812 | .map(|s| s.current_idx()) |
| 813 | .unwrap_or(center_idx as f64); |
| 814 | |
| 815 | let gesture = WorkspaceSwitchGesture { |
| 816 | center_idx, |
| 817 | current_idx, |
| 818 | tracker: SwipeTracker::new(), |
| 819 | }; |
| 820 | self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture)); |
| 821 | } |
| 822 | |
| 823 | pub fn workspace_switch_gesture_update( |
| 824 | &mut self, |
| 825 | delta_y: f64, |
| 826 | timestamp: Duration, |
| 827 | ) -> Option<bool> { |
| 828 | let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else { |
| 829 | return None; |
| 830 | }; |
| 831 | |
| 832 | gesture.tracker.push(delta_y, timestamp); |
| 833 | |
| 834 | let pos = gesture.tracker.pos() / WORKSPACE_GESTURE_MOVEMENT; |
| 835 | |
| 836 | let min = gesture.center_idx.saturating_sub(1) as f64; |
| 837 | let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64; |
| 838 | let new_idx = gesture.center_idx as f64 + pos; |
| 839 | let new_idx = WORKSPACE_GESTURE_RUBBER_BAND.clamp(min, max, new_idx); |
| 840 | |
| 841 | if gesture.current_idx == new_idx { |
| 842 | return Some(false); |
| 843 | } |
| 844 | |
| 845 | gesture.current_idx = new_idx; |
| 846 | Some(true) |
| 847 | } |
| 848 | |
| 849 | pub fn workspace_switch_gesture_end(&mut self, cancelled: bool) -> bool { |
| 850 | let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else { |
| 851 | return false; |
| 852 | }; |
| 853 | |
| 854 | if cancelled { |
| 855 | self.workspace_switch = None; |
| 856 | self.clean_up_workspaces(); |
| 857 | return true; |
| 858 | } |
| 859 | |
| 860 | let mut velocity = gesture.tracker.velocity() / WORKSPACE_GESTURE_MOVEMENT; |
| 861 | let current_pos = gesture.tracker.pos() / WORKSPACE_GESTURE_MOVEMENT; |
| 862 | let pos = gesture.tracker.projected_end_pos() / WORKSPACE_GESTURE_MOVEMENT; |
| 863 | |
| 864 | let min = gesture.center_idx.saturating_sub(1) as f64; |
| 865 | let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64; |
| 866 | let new_idx = gesture.center_idx as f64 + pos; |
| 867 | |
| 868 | let new_idx = WORKSPACE_GESTURE_RUBBER_BAND.clamp(min, max, new_idx); |
| 869 | let new_idx = new_idx.round() as usize; |
| 870 | |
| 871 | velocity *= WORKSPACE_GESTURE_RUBBER_BAND.clamp_derivative( |
| 872 | min, |
| 873 | max, |
| 874 | gesture.center_idx as f64 + current_pos, |
| 875 | ); |
| 876 | |
| 877 | self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id()); |
| 878 | |
| 879 | self.active_workspace_idx = new_idx; |
| 880 | self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new( |
| 881 | gesture.current_idx, |
| 882 | new_idx as f64, |
| 883 | velocity, |
| 884 | self.options.animations.workspace_switch.0, |
| 885 | ))); |
| 886 | |
| 887 | true |
| 888 | } |
| 889 | } |