forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB2d82fddIvan Molodetskikh1 year ago2536 commits

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