forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

workspace.rs2126 lines, 68.6 KiBHistoryRawDownload
1use std::cmp::max;
2use std::rc::Rc;
3use std::time::Duration;
4
5use niri_config::utils::MergeWith as _;
6use niri_config::{
7 CenterFocusedColumn, CornerRadius, OutputName, PresetSize, Workspace as WorkspaceConfig,
8};
9use niri_ipc::{ColumnDisplay, PositionChange, SizeChange, WindowLayout};
10use smithay::backend::renderer::element::Kind;
11use smithay::backend::renderer::gles::GlesRenderer;
12use smithay::desktop::{layer_map_for_output, Window};
13use smithay::output::Output;
14use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
15use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
16use smithay::utils::{Logical, Point, Rectangle, Serial, Size, Transform};
17use smithay::wayland::compositor::with_states;
18use smithay::wayland::shell::xdg::SurfaceCachedState;
19
20use super::floating::{FloatingSpace, FloatingSpaceRenderElement};
21use super::scrolling::{
22 Column, ColumnWidth, ScrollDirection, ScrollingSpace, ScrollingSpaceRenderElement,
23};
24use super::shadow::Shadow;
25use super::tile::{Tile, TileRenderSnapshot};
26use super::{
27 ActivateWindow, HitType, InsertPosition, InteractiveResizeData, LayoutElement, Options,
28 RemovedTile, SizeFrac,
29};
30use crate::animation::Clock;
31use crate::layout::RenderLayer;
32use crate::niri_render_elements;
33use crate::render_helpers::renderer::NiriRenderer;
34use crate::render_helpers::shadow::ShadowRenderElement;
35use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement};
36use crate::render_helpers::xray::{Xray, XrayPos};
37use crate::render_helpers::RenderCtx;
38use crate::utils::id::IdCounter;
39use crate::utils::transaction::{Transaction, TransactionBlocker};
40use crate::utils::{
41 ensure_min_max_size, ensure_min_max_size_maybe_zero, output_size, send_scale_transform,
42 ResizeEdge,
43};
44use crate::window::ResolvedWindowRules;
45
46#[derive(Debug)]
47pub struct Workspace<W: LayoutElement> {
48 /// The scrollable-tiling layout.
49 scrolling: ScrollingSpace<W>,
50
51 /// The floating layout.
52 floating: FloatingSpace<W>,
53
54 /// Whether the floating layout is active instead of the scrolling layout.
55 floating_is_active: FloatingActive,
56
57 /// The original outputs of this workspace, in preference order.
58 ///
59 /// Most of the time this will be the workspace's current output, however, after an output
60 /// disconnection, they may remain pointing to disconnected outputs.
61 pub(super) original_outputs: Vec<OutputId>,
62
63 /// Current output of this workspace.
64 output: Option<Output>,
65
66 /// Latest known output scale for this workspace.
67 ///
68 /// This should be set from the current workspace output, or, if all outputs have been
69 /// disconnected, preserved until a new output is connected.
70 scale: smithay::output::Scale,
71
72 /// Latest known output transform for this workspace.
73 ///
74 /// This should be set from the current workspace output, or, if all outputs have been
75 /// disconnected, preserved until a new output is connected.
76 transform: Transform,
77
78 /// Latest known view size for this workspace.
79 ///
80 /// This should be computed from the current workspace output size, or, if all outputs have
81 /// been disconnected, preserved until a new output is connected.
82 view_size: Size<f64, Logical>,
83
84 /// Latest known working area for this workspace.
85 ///
86 /// Not rounded to physical pixels.
87 ///
88 /// This is similar to view size, but takes into account things like layer shell exclusive
89 /// zones.
90 working_area: Rectangle<f64, Logical>,
91
92 /// This workspace's shadow in the overview.
93 shadow: Shadow,
94
95 /// This workspace's background.
96 background_buffer: SolidColorBuffer,
97
98 /// Clock for driving animations.
99 pub(super) clock: Clock,
100
101 /// Configurable properties of the layout as received from the parent monitor.
102 pub(super) base_options: Rc<Options>,
103
104 /// Configurable properties of the layout with logical sizes adjusted for the current `scale`.
105 pub(super) options: Rc<Options>,
106
107 /// Optional name of this workspace.
108 pub(super) name: Option<String>,
109
110 /// Layout config overrides for this workspace.
111 layout_config: Option<niri_config::LayoutPart>,
112
113 /// Unique ID of this workspace.
114 id: WorkspaceId,
115}
116
117#[derive(Debug, Clone, PartialEq, Eq)]
118pub struct OutputId(String);
119
120impl OutputId {
121 pub fn matches(&self, output: &Output) -> bool {
122 let output_name = output.user_data().get::<OutputName>().unwrap();
123 output_name.matches(&self.0)
124 }
125}
126
127static WORKSPACE_ID_COUNTER: IdCounter = IdCounter::new();
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
130pub struct WorkspaceId(u64);
131
132impl WorkspaceId {
133 fn next() -> WorkspaceId {
134 WorkspaceId(WORKSPACE_ID_COUNTER.next())
135 }
136
137 pub fn get(self) -> u64 {
138 self.0
139 }
140
141 pub fn specific(id: u64) -> Self {
142 Self(id)
143 }
144}
145
146niri_render_elements! {
147 WorkspaceRenderElement<R> => {
148 Scrolling = ScrollingSpaceRenderElement<R>,
149 Floating = FloatingSpaceRenderElement<R>,
150 }
151}
152
153#[derive(Debug)]
154pub(super) struct InteractiveResize<W: LayoutElement> {
155 pub window: W::Id,
156 pub original_window_size: Size<f64, Logical>,
157 pub data: InteractiveResizeData,
158}
159
160/// Resolved width or height in logical pixels.
161#[derive(Debug, Clone, Copy)]
162pub enum ResolvedSize {
163 /// Size of the tile including borders.
164 Tile(f64),
165 /// Size of the window excluding borders.
166 Window(f64),
167}
168
169/// Whether the floating space is active.
170#[derive(Debug, Clone, Copy, PartialEq, Eq)]
171enum FloatingActive {
172 /// The scrolling space is active.
173 No,
174 /// The scrolling space is active, but the floating space should render on top, even if the
175 /// active scrolling window is fullscreen.
176 ///
177 /// This is necessary for focus-follows-mouse that activates but doesn't raise the window to
178 /// avoid being annoying.
179 NoButRaised,
180 /// The floating space is active.
181 Yes,
182}
183
184/// Where to put a newly added window.
185#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
186pub enum WorkspaceAddWindowTarget<'a, W: LayoutElement> {
187 /// No particular preference.
188 #[default]
189 Auto,
190 /// As a new column at this index.
191 NewColumnAt(usize),
192 /// Next to this existing window.
193 NextTo(&'a W::Id),
194}
195
196impl OutputId {
197 pub fn new(output: &Output) -> Self {
198 let output_name = output.user_data().get::<OutputName>().unwrap();
199 Self(output_name.format_make_model_serial_or_connector())
200 }
201}
202
203impl FloatingActive {
204 fn get(self) -> bool {
205 self == Self::Yes
206 }
207}
208
209impl<W: LayoutElement> Workspace<W> {
210 pub fn new(output: Output, clock: Clock, options: Rc<Options>) -> Self {
211 Self::new_with_config(output, None, clock, options)
212 }
213
214 pub fn new_with_config(
215 output: Output,
216 mut config: Option<WorkspaceConfig>,
217 clock: Clock,
218 base_options: Rc<Options>,
219 ) -> Self {
220 let mut original_outputs: Vec<OutputId> = config
221 .as_ref()
222 .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect())
223 .unwrap_or_default();
224 if original_outputs.is_empty() {
225 original_outputs.push(OutputId::new(&output));
226 } else {
227 Self::normalize_original_outputs(&mut original_outputs, &output);
228 }
229
230 let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0));
231
232 let scale = output.current_scale();
233 let options = Rc::new(
234 Options::clone(&base_options)
235 .with_merged_layout(layout_config.as_ref())
236 .adjusted_for_scale(scale.fractional_scale()),
237 );
238
239 let view_size = output_size(&output);
240 let working_area = compute_working_area(&output);
241
242 let scrolling = ScrollingSpace::new(
243 view_size,
244 working_area,
245 scale.fractional_scale(),
246 clock.clone(),
247 options.clone(),
248 );
249
250 let floating = FloatingSpace::new(
251 view_size,
252 working_area,
253 scale.fractional_scale(),
254 clock.clone(),
255 options.clone(),
256 );
257
258 let shadow_config =
259 compute_workspace_shadow_config(options.overview.workspace_shadow, view_size);
260
261 Self {
262 scrolling,
263 floating,
264 floating_is_active: FloatingActive::No,
265 original_outputs,
266 scale,
267 transform: output.current_transform(),
268 view_size,
269 working_area,
270 shadow: Shadow::new(shadow_config),
271 background_buffer: SolidColorBuffer::new(view_size, options.layout.background_color),
272 output: Some(output),
273 clock,
274 base_options,
275 options,
276 name: config.map(|c| c.name.0),
277 layout_config,
278 id: WorkspaceId::next(),
279 }
280 }
281
282 pub fn new_with_config_no_outputs(
283 mut config: Option<WorkspaceConfig>,
284 clock: Clock,
285 base_options: Rc<Options>,
286 ) -> Self {
287 let original_outputs = config
288 .as_ref()
289 .map(|c| c.open_on_output.iter().cloned().map(OutputId).collect())
290 .unwrap_or_default();
291
292 let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0));
293
294 let scale = smithay::output::Scale::Integer(1);
295 let options = Rc::new(
296 Options::clone(&base_options)
297 .with_merged_layout(layout_config.as_ref())
298 .adjusted_for_scale(scale.fractional_scale()),
299 );
300
301 let view_size = Size::from((1280., 720.));
302 let working_area = Rectangle::from_size(Size::from((1280., 720.)));
303
304 let scrolling = ScrollingSpace::new(
305 view_size,
306 working_area,
307 scale.fractional_scale(),
308 clock.clone(),
309 options.clone(),
310 );
311
312 let floating = FloatingSpace::new(
313 view_size,
314 working_area,
315 scale.fractional_scale(),
316 clock.clone(),
317 options.clone(),
318 );
319
320 let shadow_config =
321 compute_workspace_shadow_config(options.overview.workspace_shadow, view_size);
322
323 Self {
324 scrolling,
325 floating,
326 floating_is_active: FloatingActive::No,
327 output: None,
328 scale,
329 transform: Transform::Normal,
330 original_outputs,
331 view_size,
332 working_area,
333 shadow: Shadow::new(shadow_config),
334 background_buffer: SolidColorBuffer::new(view_size, options.layout.background_color),
335 clock,
336 base_options,
337 options,
338 name: config.map(|c| c.name.0),
339 layout_config,
340 id: WorkspaceId::next(),
341 }
342 }
343
344 pub fn new_no_outputs(clock: Clock, options: Rc<Options>) -> Self {
345 Self::new_with_config_no_outputs(None, clock, options)
346 }
347
348 pub fn id(&self) -> WorkspaceId {
349 self.id
350 }
351
352 pub fn name(&self) -> Option<&String> {
353 self.name.as_ref()
354 }
355
356 pub fn unname(&mut self) {
357 self.name = None;
358 }
359
360 pub fn has_windows_or_name(&self) -> bool {
361 self.has_windows() || self.name.is_some()
362 }
363
364 pub fn scale(&self) -> smithay::output::Scale {
365 self.scale
366 }
367
368 pub fn advance_animations(&mut self) {
369 self.scrolling.advance_animations();
370 self.floating.advance_animations();
371 }
372
373 pub fn are_animations_ongoing(&self) -> bool {
374 self.scrolling.are_animations_ongoing() || self.floating.are_animations_ongoing()
375 }
376
377 pub fn are_transitions_ongoing(&self) -> bool {
378 self.scrolling.are_transitions_ongoing() || self.floating.are_transitions_ongoing()
379 }
380
381 pub fn update_render_elements(&mut self, is_active: bool, layer: RenderLayer) {
382 self.scrolling
383 .update_render_elements(is_active && !self.floating_is_active.get(), layer);
384
385 let view_rect = Rectangle::from_size(self.view_size);
386 self.floating.update_render_elements(
387 is_active && self.floating_is_active.get(),
388 view_rect,
389 layer,
390 );
391
392 if layer.is_normal() {
393 self.shadow.update_render_elements(
394 self.view_size,
395 true,
396 CornerRadius::default(),
397 self.scale.fractional_scale(),
398 1.,
399 );
400 }
401 }
402
403 pub fn update_config(&mut self, base_options: Rc<Options>) {
404 let scale = self.scale.fractional_scale();
405 let options = Rc::new(
406 Options::clone(&base_options)
407 .with_merged_layout(self.layout_config.as_ref())
408 .adjusted_for_scale(scale),
409 );
410
411 self.scrolling.update_config(
412 self.view_size,
413 self.working_area,
414 self.scale.fractional_scale(),
415 options.clone(),
416 );
417
418 self.floating.update_config(
419 self.view_size,
420 self.working_area,
421 self.scale.fractional_scale(),
422 options.clone(),
423 );
424
425 let shadow_config =
426 compute_workspace_shadow_config(options.overview.workspace_shadow, self.view_size);
427 self.shadow.update_config(shadow_config);
428
429 self.background_buffer
430 .set_color(options.layout.background_color);
431
432 self.base_options = base_options;
433 self.options = options;
434 }
435
436 pub fn update_layout_config(&mut self, layout_config: Option<niri_config::LayoutPart>) {
437 if self.layout_config == layout_config {
438 return;
439 }
440
441 self.layout_config = layout_config;
442 self.update_config(self.base_options.clone());
443 }
444
445 pub fn update_shaders(&mut self) {
446 self.scrolling.update_shaders();
447 self.floating.update_shaders();
448 self.shadow.update_shaders();
449 }
450
451 pub fn windows(&self) -> impl Iterator<Item = &W> + '_ {
452 self.tiles().map(Tile::window)
453 }
454
455 pub fn windows_mut(&mut self) -> impl Iterator<Item = &mut W> + '_ {
456 self.tiles_mut().map(Tile::window_mut)
457 }
458
459 pub fn tiles(&self) -> impl Iterator<Item = &Tile<W>> + '_ {
460 let scrolling = self.scrolling.tiles();
461 let floating = self.floating.tiles();
462 scrolling.chain(floating)
463 }
464
465 pub fn tiles_mut(&mut self) -> impl Iterator<Item = &mut Tile<W>> + '_ {
466 let scrolling = self.scrolling.tiles_mut();
467 let floating = self.floating.tiles_mut();
468 scrolling.chain(floating)
469 }
470
471 pub fn is_floating(&self, id: &W::Id) -> bool {
472 self.floating.has_window(id)
473 }
474
475 pub fn current_output(&self) -> Option<&Output> {
476 self.output.as_ref()
477 }
478
479 pub(super) fn find_preferred_output<'a>(
480 &self,
481 outputs: impl Iterator<Item = &'a Output> + Clone,
482 ) -> Option<usize> {
483 self.original_outputs
484 .iter()
485 .find_map(|id| outputs.clone().position(|output| id.matches(output)))
486 }
487
488 pub fn active_window(&self) -> Option<&W> {
489 if self.floating_is_active.get() {
490 self.floating.active_window()
491 } else {
492 self.scrolling.active_window()
493 }
494 }
495
496 pub fn active_window_mut(&mut self) -> Option<&mut W> {
497 if self.floating_is_active.get() {
498 self.floating.active_window_mut()
499 } else {
500 self.scrolling.active_window_mut()
501 }
502 }
503
504 pub fn is_active_pending_fullscreen(&self) -> bool {
505 self.scrolling.is_active_pending_fullscreen()
506 }
507
508 pub fn set_output(&mut self, output: Option<Output>) {
509 if self.output == output {
510 return;
511 }
512
513 if let Some(output) = self.output.take() {
514 for win in self.windows() {
515 win.output_leave(&output);
516 }
517 }
518
519 self.output = output;
520
521 if let Some(output) = &self.output {
522 if self.original_outputs.is_empty() {
523 self.original_outputs.push(OutputId::new(output));
524 } else {
525 // Normalize original outputs: possibly replace connectors with make/model/serial.
526 Self::normalize_original_outputs(&mut self.original_outputs, output);
527 }
528
529 self.update_output_size();
530
531 for win in self.windows() {
532 self.enter_output_for_window(win);
533 }
534 }
535 }
536
537 fn normalize_original_outputs(outputs: &mut Vec<OutputId>, output: &Output) {
538 for id in &mut *outputs {
539 if id.matches(output) {
540 *id = OutputId::new(output);
541 }
542 }
543
544 // Connector and make/model/serial candidates may resolve to the same output.
545 for idx in (0..outputs.len()).rev() {
546 if outputs[..idx].contains(&outputs[idx]) {
547 outputs.remove(idx);
548 }
549 }
550 }
551
552 fn enter_output_for_window(&self, window: &W) {
553 if let Some(output) = &self.output {
554 window.set_preferred_scale_transform(self.scale, self.transform);
555 window.output_enter(output);
556 }
557 }
558
559 pub fn update_output_size(&mut self) {
560 let output = self.output.as_ref().unwrap();
561 let scale = output.current_scale();
562 let transform = output.current_transform();
563 let view_size = output_size(output);
564 let working_area = compute_working_area(output);
565 self.set_view_size(scale, transform, view_size, working_area);
566 }
567
568 fn set_view_size(
569 &mut self,
570 scale: smithay::output::Scale,
571 transform: Transform,
572 size: Size<f64, Logical>,
573 working_area: Rectangle<f64, Logical>,
574 ) {
575 let scale_transform_changed = self.transform != transform
576 || self.scale.integer_scale() != scale.integer_scale()
577 || self.scale.fractional_scale() != scale.fractional_scale();
578 if !scale_transform_changed && self.view_size == size && self.working_area == working_area {
579 return;
580 }
581
582 let fractional_scale_changed = self.scale.fractional_scale() != scale.fractional_scale();
583
584 self.scale = scale;
585 self.transform = transform;
586 self.view_size = size;
587 self.working_area = working_area;
588
589 if fractional_scale_changed {
590 // Options need to be recomputed for the new scale.
591 self.update_config(self.base_options.clone());
592 } else {
593 // Pass our existing options as is.
594 self.scrolling.update_config(
595 size,
596 working_area,
597 scale.fractional_scale(),
598 self.options.clone(),
599 );
600 self.floating.update_config(
601 size,
602 working_area,
603 scale.fractional_scale(),
604 self.options.clone(),
605 );
606
607 let shadow_config =
608 compute_workspace_shadow_config(self.options.overview.workspace_shadow, size);
609 self.shadow.update_config(shadow_config);
610 }
611
612 self.background_buffer.resize(size);
613
614 if scale_transform_changed {
615 for window in self.windows() {
616 window.set_preferred_scale_transform(self.scale, self.transform);
617 }
618 }
619 }
620
621 pub fn view_size(&self) -> Size<f64, Logical> {
622 self.view_size
623 }
624
625 pub fn make_tile(&self, window: W) -> Tile<W> {
626 Tile::new(
627 window,
628 self.view_size,
629 self.scale.fractional_scale(),
630 self.clock.clone(),
631 self.options.clone(),
632 )
633 }
634
635 #[allow(clippy::too_many_arguments)]
636 pub fn add_tile(
637 &mut self,
638 mut tile: Tile<W>,
639 target: WorkspaceAddWindowTarget<W>,
640 activate: ActivateWindow,
641 width: ColumnWidth,
642 is_full_width: bool,
643 is_floating: bool,
644 anim: Option<niri_config::Animation>,
645 ) {
646 self.enter_output_for_window(tile.window());
647 tile.restore_to_floating = is_floating;
648
649 match target {
650 WorkspaceAddWindowTarget::Auto => {
651 // Don't steal focus from an active fullscreen window.
652 let activate = activate.map_smart(|| !self.is_active_pending_fullscreen());
653
654 // If the tile is pending maximized or fullscreen, open it in the scrolling layout
655 // where it can do that.
656 if is_floating && tile.window().pending_sizing_mode().is_normal() {
657 self.floating.add_tile(tile, activate);
658
659 if activate || self.scrolling.is_empty() {
660 self.floating_is_active = FloatingActive::Yes;
661 }
662 } else {
663 self.scrolling
664 .add_tile(None, tile, activate, width, is_full_width, anim);
665
666 if activate {
667 self.floating_is_active = FloatingActive::No;
668 }
669 }
670 }
671 WorkspaceAddWindowTarget::NewColumnAt(col_idx) => {
672 let activate = activate.map_smart(|| false);
673 self.scrolling
674 .add_tile(Some(col_idx), tile, activate, width, is_full_width, anim);
675
676 if activate {
677 self.floating_is_active = FloatingActive::No;
678 }
679 }
680 WorkspaceAddWindowTarget::NextTo(next_to) => {
681 let activate = activate.map_smart(|| self.active_window().unwrap().id() == next_to);
682
683 let floating_has_window = self.floating.has_window(next_to);
684
685 if is_floating && tile.window().pending_sizing_mode().is_normal() {
686 if floating_has_window {
687 self.floating.add_tile_above(next_to, tile, activate);
688 } else {
689 // FIXME: use static pos
690 let (next_to_tile, render_pos, _visible) = self
691 .scrolling
692 .tiles_with_render_positions()
693 .find(|(tile, _, _)| tile.window().id() == next_to)
694 .unwrap();
695
696 // Position the new tile in the center above the next_to tile. Think a
697 // dialog opening on top of a window.
698 let tile_size = tile.tile_size();
699 let pos = render_pos
700 + (next_to_tile.tile_size().to_point() - tile_size.to_point())
701 .downscale(2.);
702 let pos = self.floating.clamp_within_working_area(pos, tile_size);
703 let pos = self.floating.logical_to_size_frac(pos);
704 tile.floating_pos = Some(pos);
705
706 self.floating.add_tile(tile, activate);
707 }
708
709 if activate || self.scrolling.is_empty() {
710 self.floating_is_active = FloatingActive::Yes;
711 }
712 } else if floating_has_window {
713 self.scrolling
714 .add_tile(None, tile, activate, width, is_full_width, anim);
715
716 if activate {
717 self.floating_is_active = FloatingActive::No;
718 }
719 } else {
720 self.scrolling.add_tile_right_of(
721 next_to,
722 tile,
723 activate,
724 width,
725 is_full_width,
726 anim,
727 );
728
729 if activate {
730 self.floating_is_active = FloatingActive::No;
731 }
732 }
733 }
734 }
735 }
736
737 pub fn add_tile_to_column(
738 &mut self,
739 col_idx: usize,
740 tile_idx: Option<usize>,
741 tile: Tile<W>,
742 activate: bool,
743 ) {
744 self.enter_output_for_window(tile.window());
745 self.scrolling
746 .add_tile_to_column(col_idx, tile_idx, tile, activate);
747
748 if activate {
749 self.floating_is_active = FloatingActive::No;
750 }
751 }
752
753 pub fn add_column(
754 &mut self,
755 column: Column<W>,
756 activate: bool,
757 anim: Option<niri_config::Animation>,
758 ) {
759 for (tile, _) in column.tiles() {
760 self.enter_output_for_window(tile.window());
761 }
762
763 self.scrolling.add_column(None, column, activate, anim);
764
765 if activate {
766 self.floating_is_active = FloatingActive::No;
767 }
768 }
769
770 fn update_focus_floating_tiling_after_removing(&mut self, removed_from_floating: bool) {
771 if removed_from_floating {
772 if self.floating.is_empty() {
773 self.floating_is_active = FloatingActive::No;
774 }
775 } else {
776 // Scrolling should remain focused if both are empty.
777 if self.scrolling.is_empty() && !self.floating.is_empty() {
778 self.floating_is_active = FloatingActive::Yes;
779 }
780 }
781 }
782
783 pub fn remove_tile(&mut self, id: &W::Id, transaction: Transaction) -> RemovedTile<W> {
784 let mut from_floating = false;
785 let removed = if self.floating.has_window(id) {
786 from_floating = true;
787 self.floating.remove_tile(id)
788 } else {
789 self.scrolling.remove_tile(id, transaction)
790 };
791
792 if let Some(output) = &self.output {
793 removed.tile.window().output_leave(output);
794 }
795
796 self.update_focus_floating_tiling_after_removing(from_floating);
797
798 removed
799 }
800
801 pub fn remove_active_column(&mut self) -> Option<Column<W>> {
802 let from_floating = self.floating_is_active.get();
803 if from_floating {
804 return None;
805 }
806
807 let column = self.scrolling.remove_active_column()?;
808
809 if let Some(output) = &self.output {
810 for (tile, _) in column.tiles() {
811 tile.window().output_leave(output);
812 }
813 }
814
815 self.update_focus_floating_tiling_after_removing(from_floating);
816
817 Some(column)
818 }
819
820 pub fn resolve_default_width(
821 &self,
822 default_width: Option<Option<PresetSize>>,
823 is_floating: bool,
824 ) -> Option<PresetSize> {
825 match default_width {
826 Some(Some(width)) => Some(width),
827 Some(None) => None,
828 None if is_floating => None,
829 None => self.options.layout.default_column_width,
830 }
831 }
832
833 pub fn resolve_default_height(
834 &self,
835 default_height: Option<Option<PresetSize>>,
836 is_floating: bool,
837 ) -> Option<PresetSize> {
838 match default_height {
839 Some(Some(height)) => Some(height),
840 Some(None) => None,
841 None if is_floating => None,
842 // We don't have a global default at the moment.
843 None => None,
844 }
845 }
846
847 pub fn new_window_size(
848 &self,
849 width: Option<PresetSize>,
850 height: Option<PresetSize>,
851 is_floating: bool,
852 rules: &ResolvedWindowRules,
853 (min_size, max_size): (Size<i32, Logical>, Size<i32, Logical>),
854 ) -> Size<i32, Logical> {
855 let mut size = if is_floating {
856 self.floating.new_window_size(width, height, rules)
857 } else {
858 self.scrolling.new_window_size(width, height, rules)
859 };
860
861 // If the window has a fixed size, or we're picking some fixed size, apply min and max
862 // size. This is to ensure that a fixed-size window rule works on open, while still
863 // allowing the window freedom to pick its default size otherwise.
864 let (min_size, max_size) = rules.apply_min_max_size(min_size, max_size);
865 size.w = ensure_min_max_size_maybe_zero(size.w, min_size.w, max_size.w);
866 // For scrolling (where height is > 0) only ensure fixed height, since at runtime scrolling
867 // will only honor fixed height currently.
868 if min_size.h == max_size.h {
869 size.h = ensure_min_max_size(size.h, min_size.h, max_size.h);
870 } else if size.h > 0 {
871 // Also always honor min height, scrolling always does.
872 size.h = max(size.h, min_size.h);
873 }
874
875 size
876 }
877
878 pub fn configure_new_window(
879 &self,
880 window: &Window,
881 width: Option<PresetSize>,
882 height: Option<PresetSize>,
883 is_floating: bool,
884 rules: &ResolvedWindowRules,
885 ) {
886 window.with_surfaces(|surface, data| {
887 send_scale_transform(surface, data, self.scale, self.transform);
888 });
889
890 let toplevel = window.toplevel().expect("no x11 support");
891 let (min_size, max_size) = with_states(toplevel.wl_surface(), |state| {
892 let mut guard = state.cached_state.get::<SurfaceCachedState>();
893 let current = guard.current();
894 (current.min_size, current.max_size)
895 });
896 toplevel.with_pending_state(|state| {
897 if state.states.contains(xdg_toplevel::State::Fullscreen) {
898 state.size = Some(self.view_size.to_i32_round());
899 } else if state.states.contains(xdg_toplevel::State::Maximized) {
900 state.size = Some(self.working_area.size.to_i32_round());
901 } else {
902 let size =
903 self.new_window_size(width, height, is_floating, rules, (min_size, max_size));
904 state.size = Some(size);
905 }
906
907 if is_floating {
908 state.bounds = Some(self.floating.new_window_toplevel_bounds(rules));
909 } else {
910 state.bounds = Some(self.scrolling.new_window_toplevel_bounds(rules));
911 }
912 });
913 }
914
915 pub(super) fn resolve_scrolling_width(
916 &self,
917 window: &W,
918 width: Option<PresetSize>,
919 ) -> ColumnWidth {
920 let width = width.unwrap_or_else(|| PresetSize::Fixed(window.size().w));
921 match width {
922 PresetSize::Fixed(fixed) => {
923 let mut fixed = f64::from(fixed);
924
925 // Add border width since ColumnWidth includes borders.
926 let rules = window.rules();
927 let border = self.options.layout.border.merged_with(&rules.border);
928 if !border.off {
929 fixed += border.width * 2.;
930 }
931
932 ColumnWidth::Fixed(fixed)
933 }
934 PresetSize::Proportion(prop) => ColumnWidth::Proportion(prop),
935 }
936 }
937
938 pub fn focus_left(&mut self) -> bool {
939 if self.floating_is_active.get() {
940 self.floating.focus_left()
941 } else {
942 self.scrolling.focus_left()
943 }
944 }
945
946 pub fn focus_right(&mut self) -> bool {
947 if self.floating_is_active.get() {
948 self.floating.focus_right()
949 } else {
950 self.scrolling.focus_right()
951 }
952 }
953
954 pub fn focus_column_first(&mut self) {
955 if self.floating_is_active.get() {
956 self.floating.focus_leftmost();
957 } else {
958 self.scrolling.focus_column_first();
959 }
960 }
961
962 pub fn focus_column_last(&mut self) {
963 if self.floating_is_active.get() {
964 self.floating.focus_rightmost();
965 } else {
966 self.scrolling.focus_column_last();
967 }
968 }
969
970 pub fn focus_column_right_or_first(&mut self) {
971 if !self.focus_right() {
972 self.focus_column_first();
973 }
974 }
975
976 pub fn focus_column_left_or_last(&mut self) {
977 if !self.focus_left() {
978 self.focus_column_last();
979 }
980 }
981
982 pub fn focus_column(&mut self, index: usize) {
983 if self.floating_is_active.get() {
984 self.focus_tiling();
985 }
986 self.scrolling.focus_column(index);
987 }
988
989 pub fn focus_window_in_column(&mut self, index: u8) {
990 if self.floating_is_active.get() {
991 return;
992 }
993 self.scrolling.focus_window_in_column(index);
994 }
995
996 pub fn focus_down(&mut self) -> bool {
997 if self.floating_is_active.get() {
998 self.floating.focus_down()
999 } else {
1000 self.scrolling.focus_down()
1001 }
1002 }
1003
1004 pub fn focus_up(&mut self) -> bool {
1005 if self.floating_is_active.get() {
1006 self.floating.focus_up()
1007 } else {
1008 self.scrolling.focus_up()
1009 }
1010 }
1011
1012 pub fn focus_down_or_left(&mut self) {
1013 if self.floating_is_active.get() {
1014 self.floating.focus_down();
1015 } else {
1016 self.scrolling.focus_down_or_left();
1017 }
1018 }
1019
1020 pub fn focus_down_or_right(&mut self) {
1021 if self.floating_is_active.get() {
1022 self.floating.focus_down();
1023 } else {
1024 self.scrolling.focus_down_or_right();
1025 }
1026 }
1027
1028 pub fn focus_up_or_left(&mut self) {
1029 if self.floating_is_active.get() {
1030 self.floating.focus_up();
1031 } else {
1032 self.scrolling.focus_up_or_left();
1033 }
1034 }
1035
1036 pub fn focus_up_or_right(&mut self) {
1037 if self.floating_is_active.get() {
1038 self.floating.focus_up();
1039 } else {
1040 self.scrolling.focus_up_or_right();
1041 }
1042 }
1043
1044 pub fn focus_window_top(&mut self) {
1045 if self.floating_is_active.get() {
1046 self.floating.focus_topmost();
1047 } else {
1048 self.scrolling.focus_top();
1049 }
1050 }
1051
1052 pub fn focus_window_bottom(&mut self) {
1053 if self.floating_is_active.get() {
1054 self.floating.focus_bottommost();
1055 } else {
1056 self.scrolling.focus_bottom();
1057 }
1058 }
1059
1060 pub fn focus_window_down_or_top(&mut self) {
1061 if !self.focus_down() {
1062 self.focus_window_top();
1063 }
1064 }
1065
1066 pub fn focus_window_up_or_bottom(&mut self) {
1067 if !self.focus_up() {
1068 self.focus_window_bottom();
1069 }
1070 }
1071
1072 pub fn move_left(&mut self) -> bool {
1073 if self.floating_is_active.get() {
1074 self.floating.move_left();
1075 true
1076 } else {
1077 self.scrolling.move_left()
1078 }
1079 }
1080
1081 pub fn move_right(&mut self) -> bool {
1082 if self.floating_is_active.get() {
1083 self.floating.move_right();
1084 true
1085 } else {
1086 self.scrolling.move_right()
1087 }
1088 }
1089
1090 pub fn move_column_to_first(&mut self) {
1091 if self.floating_is_active.get() {
1092 return;
1093 }
1094 self.scrolling.move_column_to_first();
1095 }
1096
1097 pub fn move_column_to_last(&mut self) {
1098 if self.floating_is_active.get() {
1099 return;
1100 }
1101 self.scrolling.move_column_to_last();
1102 }
1103
1104 pub fn move_column_to_index(&mut self, index: usize) {
1105 if self.floating_is_active.get() {
1106 return;
1107 }
1108 self.scrolling.move_column_to_index(index);
1109 }
1110
1111 pub fn move_down(&mut self) -> bool {
1112 if self.floating_is_active.get() {
1113 self.floating.move_down();
1114 true
1115 } else {
1116 self.scrolling.move_down()
1117 }
1118 }
1119
1120 pub fn move_up(&mut self) -> bool {
1121 if self.floating_is_active.get() {
1122 self.floating.move_up();
1123 true
1124 } else {
1125 self.scrolling.move_up()
1126 }
1127 }
1128
1129 pub fn consume_or_expel_window_left(&mut self, window: Option<&W::Id>) {
1130 if window.map_or(self.floating_is_active.get(), |id| {
1131 self.floating.has_window(id)
1132 }) {
1133 return;
1134 }
1135 self.scrolling.consume_or_expel_window_left(window);
1136 }
1137
1138 pub fn consume_or_expel_window_right(&mut self, window: Option<&W::Id>) {
1139 if window.map_or(self.floating_is_active.get(), |id| {
1140 self.floating.has_window(id)
1141 }) {
1142 return;
1143 }
1144 self.scrolling.consume_or_expel_window_right(window);
1145 }
1146
1147 pub fn consume_into_column(&mut self) {
1148 if self.floating_is_active.get() {
1149 return;
1150 }
1151 self.scrolling.consume_into_column();
1152 }
1153
1154 pub fn expel_from_column(&mut self) {
1155 if self.floating_is_active.get() {
1156 return;
1157 }
1158 self.scrolling.expel_from_column();
1159 }
1160
1161 pub fn swap_window_in_direction(&mut self, direction: ScrollDirection) {
1162 if self.floating_is_active.get() {
1163 return;
1164 }
1165 self.scrolling.swap_window_in_direction(direction);
1166 }
1167
1168 pub fn toggle_column_tabbed_display(&mut self) {
1169 if self.floating_is_active.get() {
1170 return;
1171 }
1172 self.scrolling.toggle_column_tabbed_display();
1173 }
1174
1175 pub fn set_column_display(&mut self, display: ColumnDisplay) {
1176 if self.floating_is_active.get() {
1177 return;
1178 }
1179 self.scrolling.set_column_display(display);
1180 }
1181
1182 pub fn center_column(&mut self) {
1183 if self.floating_is_active.get() {
1184 self.floating.center_window(None);
1185 } else {
1186 self.scrolling.center_column();
1187 }
1188 }
1189
1190 pub fn center_window(&mut self, id: Option<&W::Id>) {
1191 if id.map_or(self.floating_is_active.get(), |id| {
1192 self.floating.has_window(id)
1193 }) {
1194 self.floating.center_window(id);
1195 } else {
1196 self.scrolling.center_window(id);
1197 }
1198 }
1199
1200 pub fn center_visible_columns(&mut self) {
1201 if self.floating_is_active.get() {
1202 return;
1203 }
1204 self.scrolling.center_visible_columns();
1205 }
1206
1207 pub fn toggle_width(&mut self, forwards: bool) {
1208 if self.floating_is_active.get() {
1209 self.floating.toggle_window_width(None, forwards);
1210 } else {
1211 self.scrolling.toggle_width(forwards);
1212 }
1213 }
1214
1215 pub fn toggle_full_width(&mut self) {
1216 if self.floating_is_active.get() {
1217 // Leave this unimplemented for now. For good UX, this probably needs moving the tile
1218 // to be against the left edge of the working area while it is full-width.
1219 return;
1220 }
1221 self.scrolling.toggle_full_width();
1222 }
1223
1224 pub fn set_column_width(&mut self, change: SizeChange) {
1225 if self.floating_is_active.get() {
1226 self.floating.set_window_width(None, change, true);
1227 } else {
1228 self.scrolling.set_window_width(None, change);
1229 }
1230 }
1231
1232 pub fn set_window_width(&mut self, window: Option<&W::Id>, change: SizeChange) {
1233 if window.map_or(self.floating_is_active.get(), |id| {
1234 self.floating.has_window(id)
1235 }) {
1236 self.floating.set_window_width(window, change, true);
1237 } else {
1238 self.scrolling.set_window_width(window, change);
1239 }
1240 }
1241
1242 pub fn set_window_height(&mut self, window: Option<&W::Id>, change: SizeChange) {
1243 if window.map_or(self.floating_is_active.get(), |id| {
1244 self.floating.has_window(id)
1245 }) {
1246 self.floating.set_window_height(window, change, true);
1247 } else {
1248 self.scrolling.set_window_height(window, change);
1249 }
1250 }
1251
1252 pub fn reset_window_height(&mut self, window: Option<&W::Id>) {
1253 if window.map_or(self.floating_is_active.get(), |id| {
1254 self.floating.has_window(id)
1255 }) {
1256 return;
1257 }
1258 self.scrolling.reset_window_height(window);
1259 }
1260
1261 pub fn toggle_window_width(&mut self, window: Option<&W::Id>, forwards: bool) {
1262 if window.map_or(self.floating_is_active.get(), |id| {
1263 self.floating.has_window(id)
1264 }) {
1265 self.floating.toggle_window_width(window, forwards);
1266 } else {
1267 self.scrolling.toggle_window_width(window, forwards);
1268 }
1269 }
1270
1271 pub fn toggle_window_height(&mut self, window: Option<&W::Id>, forwards: bool) {
1272 if window.map_or(self.floating_is_active.get(), |id| {
1273 self.floating.has_window(id)
1274 }) {
1275 self.floating.toggle_window_height(window, forwards);
1276 } else {
1277 self.scrolling.toggle_window_height(window, forwards);
1278 }
1279 }
1280
1281 pub fn expand_column_to_available_width(&mut self) {
1282 if self.floating_is_active.get() {
1283 return;
1284 }
1285 self.scrolling.expand_column_to_available_width();
1286 }
1287
1288 pub fn set_fullscreen(&mut self, window: &W::Id, is_fullscreen: bool) {
1289 let mut restore_to_floating = false;
1290 if self.floating.has_window(window) {
1291 if is_fullscreen {
1292 restore_to_floating = true;
1293 self.toggle_window_floating(Some(window));
1294 } else {
1295 // Floating windows are never fullscreen, so this is an unfullscreen request for an
1296 // already unfullscreen window.
1297 return;
1298 }
1299 } else if !is_fullscreen {
1300 // The window is in the scrolling layout and we're requesting an unfullscreen. If it is
1301 // indeed fullscreen (i.e. this isn't a duplicate unfullscreen request), then we may
1302 // need to unfullscreen into floating.
1303 let col = self
1304 .scrolling
1305 .columns()
1306 .find(|col| col.contains(window))
1307 .unwrap();
1308
1309 // When going from fullscreen to maximized, don't consider restore_to_floating yet.
1310 if col.is_pending_fullscreen() && !col.is_pending_maximized() {
1311 let (tile, _) = col
1312 .tiles()
1313 .find(|(tile, _)| tile.window().id() == window)
1314 .unwrap();
1315 if tile.restore_to_floating {
1316 // Unfullscreen and float in one call so it has a chance to notice and request a
1317 // (0, 0) size, rather than the scrolling column size.
1318 self.toggle_window_floating(Some(window));
1319 return;
1320 }
1321 }
1322 }
1323
1324 let tile = self
1325 .scrolling
1326 .tiles()
1327 .find(|tile| tile.window().id() == window)
1328 .unwrap();
1329 let was_normal = tile.window().pending_sizing_mode().is_normal();
1330
1331 self.scrolling.set_fullscreen(window, is_fullscreen);
1332
1333 // When going from normal to fullscreen, remember if we should unfullscreen to floating.
1334 let tile = self
1335 .scrolling
1336 .tiles_mut()
1337 .find(|tile| tile.window().id() == window)
1338 .unwrap();
1339 if was_normal && !tile.window().pending_sizing_mode().is_normal() {
1340 tile.restore_to_floating = restore_to_floating;
1341 }
1342 }
1343
1344 pub fn toggle_fullscreen(&mut self, window: &W::Id) {
1345 let tile = self
1346 .tiles()
1347 .find(|tile| tile.window().id() == window)
1348 .unwrap();
1349 let current = tile.window().pending_sizing_mode().is_fullscreen();
1350 self.set_fullscreen(window, !current);
1351 }
1352
1353 pub fn set_maximized(&mut self, window: &W::Id, maximize: bool) {
1354 let mut restore_to_floating = false;
1355 if self.floating.has_window(window) {
1356 if maximize {
1357 restore_to_floating = true;
1358 self.toggle_window_floating(Some(window));
1359 } else {
1360 // Floating windows are never maximized, so this is an unmaximize request for an
1361 // already unmaximized window.
1362 return;
1363 }
1364 } else if !maximize {
1365 // The window is in the scrolling layout and we're requesting to unmaximize. If it is
1366 // indeed maximized (i.e. this isn't a duplicate unmaximize request), then we may
1367 // need to unmaximize into floating.
1368 let tile = self
1369 .scrolling
1370 .tiles()
1371 .find(|tile| tile.window().id() == window)
1372 .unwrap();
1373 // The tile cannot unmaximize into fullscreen (pending_sizing_mode() will be fullscreen
1374 // in that case and not maximized), so this check works.
1375 if tile.window().pending_sizing_mode().is_maximized() && tile.restore_to_floating {
1376 // Unmaximize and float in one call so it has a chance to notice and request a
1377 // (0, 0) size, rather than the scrolling column size.
1378 self.toggle_window_floating(Some(window));
1379 return;
1380 }
1381 }
1382
1383 let tile = self
1384 .scrolling
1385 .tiles()
1386 .find(|tile| tile.window().id() == window)
1387 .unwrap();
1388 let was_normal = tile.window().pending_sizing_mode().is_normal();
1389
1390 self.scrolling.set_maximized(window, maximize);
1391
1392 // When going from normal to maximized, remember if we should unmaximize to floating.
1393 let tile = self
1394 .scrolling
1395 .tiles_mut()
1396 .find(|tile| tile.window().id() == window)
1397 .unwrap();
1398 if was_normal && !tile.window().pending_sizing_mode().is_normal() {
1399 tile.restore_to_floating = restore_to_floating;
1400 }
1401 }
1402
1403 pub fn toggle_maximized(&mut self, window: &W::Id) {
1404 let mut current = false;
1405
1406 // We have to check the column property in case the window is in the scrolling layout and
1407 // both maximized and fullscreen. In this case, only the column knows whether it's
1408 // maximized.
1409 //
1410 // In the floating layout, windows cannot be maximized.
1411 if let Some(col) = self.scrolling.columns().find(|col| col.contains(window)) {
1412 current = col.is_pending_maximized();
1413 }
1414
1415 self.set_maximized(window, !current);
1416 }
1417
1418 pub fn toggle_window_floating(&mut self, id: Option<&W::Id>) {
1419 let active_id = self.active_window().map(|win| win.id().clone());
1420 let target_is_active = id.is_none_or(|id| Some(id) == active_id.as_ref());
1421 let Some(id) = id.cloned().or(active_id) else {
1422 return;
1423 };
1424
1425 let (_, render_pos, _) = self
1426 .tiles_with_render_positions()
1427 .find(|(tile, _, _)| *tile.window().id() == id)
1428 .unwrap();
1429
1430 if self.floating.has_window(&id) {
1431 let removed = self.floating.remove_tile(&id);
1432 // FIXME: compute closest pos?
1433 self.scrolling.add_tile(
1434 None,
1435 removed.tile,
1436 target_is_active,
1437 removed.width,
1438 removed.is_full_width,
1439 None,
1440 );
1441 if target_is_active {
1442 self.floating_is_active = FloatingActive::No;
1443 }
1444 } else {
1445 let mut removed = self.scrolling.remove_tile(&id, Transaction::new());
1446 removed.tile.stop_move_animations();
1447
1448 // Come up with a default floating position close to the tile position.
1449 let stored_or_default = self.floating.stored_or_default_tile_pos(&removed.tile);
1450 if stored_or_default.is_none() {
1451 let offset =
1452 if self.options.layout.center_focused_column == CenterFocusedColumn::Always {
1453 Point::from((0., 0.))
1454 } else {
1455 Point::from((50., 50.))
1456 };
1457 let pos = render_pos + offset;
1458 let size = removed.tile.tile_size();
1459 let pos = self.floating.clamp_within_working_area(pos, size);
1460 let pos = self.floating.logical_to_size_frac(pos);
1461 removed.tile.floating_pos = Some(pos);
1462 }
1463
1464 self.floating.add_tile(removed.tile, target_is_active);
1465 if target_is_active {
1466 self.floating_is_active = FloatingActive::Yes;
1467 }
1468 }
1469
1470 let (tile, new_render_pos) = self
1471 .tiles_with_render_positions_mut(false)
1472 .find(|(tile, _)| *tile.window().id() == id)
1473 .unwrap();
1474
1475 tile.animate_move_from(render_pos - new_render_pos);
1476 }
1477
1478 pub fn set_window_floating(&mut self, id: Option<&W::Id>, floating: bool) {
1479 if id.map_or(self.floating_is_active.get(), |id| {
1480 self.floating.has_window(id)
1481 }) == floating
1482 {
1483 return;
1484 }
1485
1486 self.toggle_window_floating(id);
1487 }
1488
1489 pub fn focus_floating(&mut self) {
1490 if !self.floating_is_active.get() {
1491 self.switch_focus_floating_tiling();
1492 }
1493 }
1494
1495 pub fn focus_tiling(&mut self) {
1496 if self.floating_is_active.get() {
1497 self.switch_focus_floating_tiling();
1498 }
1499 }
1500
1501 pub fn switch_focus_floating_tiling(&mut self) {
1502 if self.floating.is_empty() {
1503 // If floating is empty, keep focus on scrolling.
1504 return;
1505 } else if self.scrolling.is_empty() {
1506 // If floating isn't empty but scrolling is, keep focus on floating.
1507 return;
1508 }
1509
1510 self.floating_is_active = if self.floating_is_active.get() {
1511 FloatingActive::No
1512 } else {
1513 FloatingActive::Yes
1514 };
1515 }
1516
1517 pub fn move_floating_window(
1518 &mut self,
1519 id: Option<&W::Id>,
1520 x: PositionChange,
1521 y: PositionChange,
1522 animate: bool,
1523 ) {
1524 if id.map_or(self.floating_is_active.get(), |id| {
1525 self.floating.has_window(id)
1526 }) {
1527 self.floating.move_window(id, x, y, animate);
1528 } else {
1529 // If the target tile isn't floating, set its stored floating position.
1530 let tile = if let Some(id) = id {
1531 self.scrolling
1532 .tiles_mut()
1533 .find(|tile| tile.window().id() == id)
1534 .unwrap()
1535 } else if let Some(tile) = self.scrolling.active_tile_mut() {
1536 tile
1537 } else {
1538 return;
1539 };
1540
1541 let pos = self.floating.stored_or_default_tile_pos(tile);
1542
1543 // If there's no stored floating position, we can only set both components at once, not
1544 // adjust.
1545 let pos = pos.or_else(|| {
1546 (matches!(
1547 x,
1548 PositionChange::SetFixed(_) | PositionChange::SetProportion(_)
1549 ) && matches!(
1550 y,
1551 PositionChange::SetFixed(_) | PositionChange::SetProportion(_)
1552 ))
1553 .then_some(Point::default())
1554 });
1555
1556 let Some(mut pos) = pos else {
1557 return;
1558 };
1559
1560 let working_area = self.floating.working_area();
1561 let available_width = working_area.size.w;
1562 let available_height = working_area.size.h;
1563 let working_area_loc = working_area.loc;
1564
1565 const MAX_F: f64 = 10000.;
1566
1567 match x {
1568 PositionChange::SetFixed(x) => pos.x = x + working_area_loc.x,
1569 PositionChange::SetProportion(prop) => {
1570 let prop = (prop / 100.).clamp(0., MAX_F);
1571 pos.x = available_width * prop + working_area_loc.x;
1572 }
1573 PositionChange::AdjustFixed(x) => pos.x += x,
1574 PositionChange::AdjustProportion(prop) => {
1575 let current_prop = (pos.x - working_area_loc.x) / available_width.max(1.);
1576 let prop = (current_prop + prop / 100.).clamp(0., MAX_F);
1577 pos.x = available_width * prop + working_area_loc.x;
1578 }
1579 }
1580 match y {
1581 PositionChange::SetFixed(y) => pos.y = y + working_area_loc.y,
1582 PositionChange::SetProportion(prop) => {
1583 let prop = (prop / 100.).clamp(0., MAX_F);
1584 pos.y = available_height * prop + working_area_loc.y;
1585 }
1586 PositionChange::AdjustFixed(y) => pos.y += y,
1587 PositionChange::AdjustProportion(prop) => {
1588 let current_prop = (pos.y - working_area_loc.y) / available_height.max(1.);
1589 let prop = (current_prop + prop / 100.).clamp(0., MAX_F);
1590 pos.y = available_height * prop + working_area_loc.y;
1591 }
1592 }
1593
1594 let pos = self.floating.logical_to_size_frac(pos);
1595 tile.floating_pos = Some(pos);
1596 }
1597 }
1598
1599 pub fn has_windows(&self) -> bool {
1600 self.windows().next().is_some()
1601 }
1602
1603 pub fn has_window(&self, window: &W::Id) -> bool {
1604 self.windows().any(|win| win.id() == window)
1605 }
1606
1607 pub fn find_wl_surface(&self, wl_surface: &WlSurface) -> Option<&W> {
1608 self.windows().find(|win| win.is_wl_surface(wl_surface))
1609 }
1610
1611 pub fn find_wl_surface_mut(&mut self, wl_surface: &WlSurface) -> Option<&mut W> {
1612 self.windows_mut().find(|win| win.is_wl_surface(wl_surface))
1613 }
1614
1615 pub fn tiles_with_render_positions(
1616 &self,
1617 ) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>, bool)> {
1618 let scrolling = self.scrolling.tiles_with_render_positions();
1619
1620 let floating = self.floating.tiles_with_render_positions();
1621 let visible = self.is_floating_visible();
1622 let floating = floating.map(move |(tile, pos)| (tile, pos, visible));
1623
1624 floating.chain(scrolling)
1625 }
1626
1627 pub fn tiles_with_render_positions_mut(
1628 &mut self,
1629 round: bool,
1630 ) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> {
1631 let scrolling = self.scrolling.tiles_with_render_positions_mut(round);
1632 let floating = self.floating.tiles_with_render_positions_mut(round);
1633 floating.chain(scrolling)
1634 }
1635
1636 pub fn tiles_with_ipc_layouts(&self) -> impl Iterator<Item = (&Tile<W>, WindowLayout)> {
1637 let scrolling = self.scrolling.tiles_with_ipc_layouts();
1638 let floating = self.floating.tiles_with_ipc_layouts();
1639 floating.chain(scrolling)
1640 }
1641
1642 pub fn active_window_visual_rectangle(&self) -> Option<Rectangle<f64, Logical>> {
1643 if self.floating_is_active.get() {
1644 self.floating.active_window_visual_rectangle()
1645 } else {
1646 self.scrolling.active_window_visual_rectangle()
1647 }
1648 }
1649
1650 pub fn popup_target_rect(
1651 &self,
1652 window: &W::Id,
1653 overflow: bool,
1654 ) -> Option<Rectangle<f64, Logical>> {
1655 if self.floating.has_window(window) {
1656 self.floating.popup_target_rect(window)
1657 } else {
1658 self.scrolling.popup_target_rect(window, overflow)
1659 }
1660 }
1661
1662 pub fn render_scrolling<R: NiriRenderer>(
1663 &self,
1664 ctx: RenderCtx<R>,
1665 xray_pos: XrayPos,
1666 focus_ring: bool,
1667 layer: RenderLayer,
1668 push: &mut dyn FnMut(WorkspaceRenderElement<R>),
1669 ) {
1670 let scrolling_focus_ring = focus_ring && !self.floating_is_active();
1671 self.scrolling
1672 .render(ctx, xray_pos, scrolling_focus_ring, layer, &mut |elem| {
1673 push(elem.into())
1674 });
1675 }
1676
1677 pub fn render_floating<R: NiriRenderer>(
1678 &self,
1679 ctx: RenderCtx<R>,
1680 xray_pos: XrayPos,
1681 focus_ring: bool,
1682 layer: RenderLayer,
1683 push: &mut dyn FnMut(WorkspaceRenderElement<R>),
1684 ) {
1685 if !self.is_floating_visible() && layer.is_normal() {
1686 return;
1687 }
1688
1689 let view_rect = Rectangle::from_size(self.view_size);
1690 let floating_focus_ring = focus_ring && self.floating_is_active();
1691 self.floating.render(
1692 ctx,
1693 xray_pos,
1694 view_rect,
1695 floating_focus_ring,
1696 layer,
1697 &mut |elem| push(elem.into()),
1698 );
1699 }
1700
1701 pub fn render_shadow<R: NiriRenderer>(
1702 &self,
1703 renderer: &mut R,
1704 push: &mut dyn FnMut(ShadowRenderElement),
1705 ) {
1706 self.shadow.render(renderer, Point::from((0., 0.)), push);
1707 }
1708
1709 pub fn render_background(&self) -> SolidColorRenderElement {
1710 SolidColorRenderElement::from_buffer(
1711 &self.background_buffer,
1712 Point::new(0., 0.),
1713 1.,
1714 Kind::Unspecified,
1715 )
1716 }
1717
1718 pub fn fullscreen_window_above_top_layer(&self) -> Option<&W> {
1719 self.scrolling.fullscreen_window_above_top_layer()
1720 }
1721
1722 pub fn is_floating_visible(&self) -> bool {
1723 // If the focus is on a fullscreen scrolling window, hide the floating windows.
1724 matches!(
1725 self.floating_is_active,
1726 FloatingActive::Yes | FloatingActive::NoButRaised
1727 ) || self.fullscreen_window_above_top_layer().is_none()
1728 }
1729
1730 pub fn store_unmap_snapshot_if_empty(
1731 &mut self,
1732 renderer: &mut GlesRenderer,
1733 xray: Option<&mut Xray>,
1734 xray_has_blocked_out_layers: bool,
1735 xray_pos: XrayPos,
1736 window: &W::Id,
1737 ) {
1738 let view_size = self.view_size();
1739 for (tile, tile_pos) in self.tiles_with_render_positions_mut(false) {
1740 if tile.window().id() == window {
1741 let view_pos = Point::from((-tile_pos.x, -tile_pos.y));
1742 let view_rect = Rectangle::new(view_pos, view_size);
1743 tile.update_render_elements(false, view_rect);
1744 let xray_pos = xray_pos.offset(tile_pos);
1745 tile.store_unmap_snapshot_if_empty(
1746 renderer,
1747 xray,
1748 xray_has_blocked_out_layers,
1749 xray_pos,
1750 );
1751 return;
1752 }
1753 }
1754 }
1755
1756 pub fn clear_unmap_snapshot(&mut self, window: &W::Id) {
1757 for tile in self.tiles_mut() {
1758 if tile.window().id() == window {
1759 let _ = tile.take_unmap_snapshot();
1760 return;
1761 }
1762 }
1763 }
1764
1765 pub fn start_close_animation_for_window(
1766 &mut self,
1767 renderer: &mut GlesRenderer,
1768 window: &W::Id,
1769 blocker: TransactionBlocker,
1770 ) {
1771 if self.floating.has_window(window) {
1772 self.floating
1773 .start_close_animation_for_window(renderer, window, blocker);
1774 } else {
1775 self.scrolling
1776 .start_close_animation_for_window(renderer, window, blocker);
1777 }
1778 }
1779
1780 pub fn start_close_animation_for_tile(
1781 &mut self,
1782 renderer: &mut GlesRenderer,
1783 snapshot: TileRenderSnapshot,
1784 tile_size: Size<f64, Logical>,
1785 tile_pos: Point<f64, Logical>,
1786 blocker: TransactionBlocker,
1787 ) {
1788 self.floating
1789 .start_close_animation_for_tile(renderer, snapshot, tile_size, tile_pos, blocker);
1790 }
1791
1792 pub fn start_open_animation(&mut self, id: &W::Id) -> bool {
1793 self.scrolling.start_open_animation(id) || self.floating.start_open_animation(id)
1794 }
1795
1796 pub fn window_under(&self, pos: Point<f64, Logical>) -> Option<(&W, HitType)> {
1797 // This logic is consistent with tiles_with_render_positions().
1798 if self.is_floating_visible() {
1799 if let Some(rv) = self
1800 .floating
1801 .tiles_with_render_positions()
1802 .find_map(|(tile, tile_pos)| HitType::hit_tile(tile, tile_pos, pos))
1803 {
1804 return Some(rv);
1805 }
1806 }
1807
1808 self.scrolling.window_under(pos)
1809 }
1810
1811 pub fn resize_edges_under(&self, pos: Point<f64, Logical>) -> Option<ResizeEdge> {
1812 self.tiles_with_render_positions()
1813 .find_map(|(tile, tile_pos, visible)| {
1814 // This logic should be consistent with window_under() in when it returns Some vs.
1815 // None.
1816 if !visible {
1817 return None;
1818 }
1819
1820 let pos_within_tile = pos - tile_pos;
1821
1822 if tile.hit(pos_within_tile).is_some() {
1823 let size = tile.tile_size().to_f64();
1824
1825 let mut edges = ResizeEdge::empty();
1826 if pos_within_tile.x < size.w / 3. {
1827 edges |= ResizeEdge::LEFT;
1828 } else if 2. * size.w / 3. < pos_within_tile.x {
1829 edges |= ResizeEdge::RIGHT;
1830 }
1831 if pos_within_tile.y < size.h / 3. {
1832 edges |= ResizeEdge::TOP;
1833 } else if 2. * size.h / 3. < pos_within_tile.y {
1834 edges |= ResizeEdge::BOTTOM;
1835 }
1836 return Some(edges);
1837 }
1838
1839 None
1840 })
1841 }
1842
1843 pub fn descendants_added(&mut self, id: &W::Id) -> bool {
1844 self.floating.descendants_added(id)
1845 }
1846
1847 pub fn update_window(&mut self, window: &W::Id, serial: Option<Serial>) {
1848 if !self.floating.update_window(window, serial) {
1849 self.scrolling.update_window(window, serial);
1850 }
1851 }
1852
1853 pub fn refresh(&mut self, is_active: bool, is_focused: bool) {
1854 self.scrolling
1855 .refresh(is_active && !self.floating_is_active.get(), is_focused);
1856 self.floating
1857 .refresh(is_active && self.floating_is_active.get(), is_focused);
1858 }
1859
1860 pub fn scroll_amount_to_activate(&self, window: &W::Id) -> f64 {
1861 if self.floating.has_window(window) {
1862 return 0.;
1863 }
1864
1865 self.scrolling.scroll_amount_to_activate(window)
1866 }
1867
1868 pub fn is_urgent(&self) -> bool {
1869 self.windows().any(|win| win.is_urgent())
1870 }
1871
1872 pub fn activate_window(&mut self, window: &W::Id) -> bool {
1873 if self.floating.activate_window(window) {
1874 self.floating_is_active = FloatingActive::Yes;
1875 true
1876 } else if self.scrolling.activate_window(window) {
1877 self.floating_is_active = FloatingActive::No;
1878 true
1879 } else {
1880 false
1881 }
1882 }
1883
1884 pub fn activate_window_without_raising(&mut self, window: &W::Id) -> bool {
1885 if self.floating.activate_window_without_raising(window) {
1886 self.floating_is_active = FloatingActive::Yes;
1887 true
1888 } else if self.scrolling.activate_window(window) {
1889 self.floating_is_active = match self.floating_is_active {
1890 FloatingActive::No => FloatingActive::No,
1891 FloatingActive::NoButRaised => FloatingActive::NoButRaised,
1892 FloatingActive::Yes => FloatingActive::NoButRaised,
1893 };
1894 true
1895 } else {
1896 false
1897 }
1898 }
1899
1900 pub(super) fn scrolling_insert_position(&self, pos: Point<f64, Logical>) -> InsertPosition {
1901 self.scrolling.insert_position(pos)
1902 }
1903
1904 pub(super) fn insert_hint_area(
1905 &self,
1906 position: InsertPosition,
1907 ) -> Option<Rectangle<f64, Logical>> {
1908 self.scrolling.insert_hint_area(position)
1909 }
1910
1911 pub fn view_offset_gesture_begin(&mut self, is_touchpad: bool) {
1912 self.scrolling.view_offset_gesture_begin(is_touchpad);
1913 }
1914
1915 pub fn view_offset_gesture_update(
1916 &mut self,
1917 delta_x: f64,
1918 timestamp: Duration,
1919 is_touchpad: bool,
1920 ) -> Option<bool> {
1921 self.scrolling
1922 .view_offset_gesture_update(delta_x, timestamp, is_touchpad)
1923 }
1924
1925 pub fn view_offset_gesture_end(&mut self, is_touchpad: Option<bool>) -> bool {
1926 self.scrolling.view_offset_gesture_end(is_touchpad)
1927 }
1928
1929 pub fn dnd_scroll_gesture_begin(&mut self) {
1930 self.scrolling.dnd_scroll_gesture_begin();
1931 }
1932
1933 pub fn dnd_scroll_gesture_scroll(&mut self, pos: Point<f64, Logical>, speed: f64) -> bool {
1934 let config = &self.options.gestures.dnd_edge_view_scroll;
1935 let trigger_width = config.trigger_width;
1936
1937 // This working area intentionally does not include extra struts from Options.
1938 let x = pos.x - self.working_area.loc.x;
1939 let width = self.working_area.size.w;
1940
1941 let x = x.clamp(0., width);
1942 let trigger_width = trigger_width.clamp(0., width / 2.);
1943
1944 let delta = if x < trigger_width {
1945 -(trigger_width - x)
1946 } else if width - x < trigger_width {
1947 trigger_width - (width - x)
1948 } else {
1949 0.
1950 };
1951
1952 let delta = if trigger_width < 0.01 {
1953 // Sanity check for trigger-width 0 or small window sizes.
1954 0.
1955 } else {
1956 // Normalize to [0, 1].
1957 delta / trigger_width
1958 };
1959 let delta = delta * speed;
1960
1961 self.scrolling.dnd_scroll_gesture_scroll(delta)
1962 }
1963
1964 pub fn dnd_scroll_gesture_end(&mut self) {
1965 self.scrolling.dnd_scroll_gesture_end();
1966 }
1967
1968 pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool {
1969 if self.floating.has_window(&window) {
1970 self.floating.interactive_resize_begin(window, edges)
1971 } else {
1972 self.scrolling.interactive_resize_begin(window, edges)
1973 }
1974 }
1975
1976 pub fn interactive_resize_update(
1977 &mut self,
1978 window: &W::Id,
1979 delta: Point<f64, Logical>,
1980 ) -> bool {
1981 if self.floating.has_window(window) {
1982 self.floating.interactive_resize_update(window, delta)
1983 } else {
1984 self.scrolling.interactive_resize_update(window, delta)
1985 }
1986 }
1987
1988 pub fn interactive_resize_end(&mut self, window: Option<&W::Id>) {
1989 if let Some(window) = window {
1990 if self.floating.has_window(window) {
1991 self.floating.interactive_resize_end(Some(window));
1992 } else {
1993 self.scrolling.interactive_resize_end(Some(window));
1994 }
1995 } else {
1996 self.floating.interactive_resize_end(None);
1997 self.scrolling.interactive_resize_end(None);
1998 }
1999 }
2000
2001 pub fn floating_is_active(&self) -> bool {
2002 self.floating_is_active.get()
2003 }
2004
2005 pub fn floating_logical_to_size_frac(
2006 &self,
2007 logical_pos: Point<f64, Logical>,
2008 ) -> Point<f64, SizeFrac> {
2009 self.floating.logical_to_size_frac(logical_pos)
2010 }
2011
2012 pub fn working_area(&self) -> Rectangle<f64, Logical> {
2013 self.working_area
2014 }
2015
2016 pub fn layout_config(&self) -> Option<&niri_config::LayoutPart> {
2017 self.layout_config.as_ref()
2018 }
2019
2020 pub fn scrolling(&self) -> &ScrollingSpace<W> {
2021 &self.scrolling
2022 }
2023
2024 pub fn scrolling_mut(&mut self) -> &mut ScrollingSpace<W> {
2025 &mut self.scrolling
2026 }
2027
2028 pub fn floating(&self) -> &FloatingSpace<W> {
2029 &self.floating
2030 }
2031
2032 #[cfg(test)]
2033 pub fn verify_invariants(&self, move_win_id: Option<&W::Id>) {
2034 use approx::assert_abs_diff_eq;
2035
2036 let scale = self.scale.fractional_scale();
2037 assert!(scale > 0.);
2038 assert!(scale.is_finite());
2039
2040 let options = Options::clone(&self.base_options)
2041 .with_merged_layout(self.layout_config.as_ref())
2042 .adjusted_for_scale(scale);
2043 assert_eq!(
2044 &*self.options, &options,
2045 "options must be base options adjusted for scale"
2046 );
2047
2048 assert!(self.view_size.w > 0.);
2049 assert!(self.view_size.h > 0.);
2050
2051 assert_eq!(self.background_buffer.size(), self.view_size);
2052 assert_eq!(
2053 self.background_buffer.color().components(),
2054 options.layout.background_color.to_array_unpremul(),
2055 );
2056
2057 assert_eq!(self.view_size, self.scrolling.view_size());
2058 assert_eq!(self.working_area, self.scrolling.parent_area());
2059 assert_eq!(&self.clock, self.scrolling.clock());
2060 assert!(Rc::ptr_eq(&self.options, self.scrolling.options()));
2061 self.scrolling.verify_invariants();
2062
2063 assert_eq!(self.view_size, self.floating.view_size());
2064 assert_eq!(self.working_area, self.floating.working_area());
2065 assert_eq!(&self.clock, self.floating.clock());
2066 assert!(Rc::ptr_eq(&self.options, self.floating.options()));
2067 self.floating.verify_invariants();
2068
2069 if self.floating.is_empty() {
2070 assert!(
2071 !self.floating_is_active.get(),
2072 "when floating is empty it must never be active"
2073 );
2074 } else if self.scrolling.is_empty() {
2075 assert!(
2076 self.floating_is_active.get(),
2077 "when scrolling is empty but floating isn't, floating should be active"
2078 );
2079 }
2080
2081 for (tile, tile_pos, visible) in self.tiles_with_render_positions() {
2082 if Some(tile.window().id()) != move_win_id {
2083 assert_eq!(tile.interactive_move_offset, Point::from((0., 0.)));
2084 }
2085
2086 let rounded_pos = tile_pos.to_physical_precise_round(scale).to_logical(scale);
2087
2088 // Tile positions must be rounded to physical pixels.
2089 assert_abs_diff_eq!(tile_pos.x, rounded_pos.x, epsilon = 1e-5);
2090 assert_abs_diff_eq!(tile_pos.y, rounded_pos.y, epsilon = 1e-5);
2091
2092 if let Some(alpha) = &tile.alpha_animation {
2093 let anim = &alpha.anim;
2094 if visible {
2095 assert_eq!(anim.to(), 1., "visible tiles can animate alpha only to 1");
2096 }
2097
2098 assert!(
2099 !alpha.hold_after_done,
2100 "tiles in the layout cannot have held alpha animation"
2101 );
2102 }
2103 }
2104 }
2105}
2106
2107pub(super) fn compute_working_area(output: &Output) -> Rectangle<f64, Logical> {
2108 layer_map_for_output(output).non_exclusive_zone().to_f64()
2109}
2110
2111fn compute_workspace_shadow_config(
2112 config: niri_config::WorkspaceShadow,
2113 view_size: Size<f64, Logical>,
2114) -> niri_config::Shadow {
2115 // Gaps between workspaces are a multiple of the view height, so shadow settings should also be
2116 // normalized to the view height to prevent them from overlapping on lower resolutions.
2117 let norm = view_size.h / 1080.;
2118
2119 let mut config = niri_config::Shadow::from(config);
2120 config.softness *= norm;
2121 config.spread *= norm;
2122 config.offset.x.0 *= norm;
2123 config.offset.y.0 *= norm;
2124
2125 config
2126}