forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB66583cdIvan Molodetskikh1 year ago2489 commits

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