forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBad3847fIvan Molodetskikh1 year ago2457 commits

workspace.rs1958 lines, 62.4 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.unfullscreen_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 fullscreen, open it in the scrolling layout where it can
622 // go fullscreen.
623 if is_floating && !tile.window().is_pending_fullscreen() {
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().is_pending_fullscreen() {
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 {
873 let size =
874 self.new_window_size(width, height, is_floating, rules, (min_size, max_size));
875 state.size = Some(size);
876 }
877
878 if is_floating {
879 state.bounds = Some(self.floating.new_window_toplevel_bounds(rules));
880 } else {
881 state.bounds = Some(self.scrolling.new_window_toplevel_bounds(rules));
882 }
883 });
884 }
885
886 pub(super) fn resolve_scrolling_width(
887 &self,
888 window: &W,
889 width: Option<PresetSize>,
890 ) -> ColumnWidth {
891 let width = width.unwrap_or_else(|| PresetSize::Fixed(window.size().w));
892 match width {
893 PresetSize::Fixed(fixed) => {
894 let mut fixed = f64::from(fixed);
895
896 // Add border width since ColumnWidth includes borders.
897 let rules = window.rules();
898 let border = self.options.layout.border.merged_with(&rules.border);
899 if !border.off {
900 fixed += border.width * 2.;
901 }
902
903 ColumnWidth::Fixed(fixed)
904 }
905 PresetSize::Proportion(prop) => ColumnWidth::Proportion(prop),
906 }
907 }
908
909 pub fn focus_left(&mut self) -> bool {
910 if self.floating_is_active.get() {
911 self.floating.focus_left()
912 } else {
913 self.scrolling.focus_left()
914 }
915 }
916
917 pub fn focus_right(&mut self) -> bool {
918 if self.floating_is_active.get() {
919 self.floating.focus_right()
920 } else {
921 self.scrolling.focus_right()
922 }
923 }
924
925 pub fn focus_column_first(&mut self) {
926 if self.floating_is_active.get() {
927 self.floating.focus_leftmost();
928 } else {
929 self.scrolling.focus_column_first();
930 }
931 }
932
933 pub fn focus_column_last(&mut self) {
934 if self.floating_is_active.get() {
935 self.floating.focus_rightmost();
936 } else {
937 self.scrolling.focus_column_last();
938 }
939 }
940
941 pub fn focus_column_right_or_first(&mut self) {
942 if !self.focus_right() {
943 self.focus_column_first();
944 }
945 }
946
947 pub fn focus_column_left_or_last(&mut self) {
948 if !self.focus_left() {
949 self.focus_column_last();
950 }
951 }
952
953 pub fn focus_column(&mut self, index: usize) {
954 if self.floating_is_active.get() {
955 self.focus_tiling();
956 }
957 self.scrolling.focus_column(index);
958 }
959
960 pub fn focus_window_in_column(&mut self, index: u8) {
961 if self.floating_is_active.get() {
962 return;
963 }
964 self.scrolling.focus_window_in_column(index);
965 }
966
967 pub fn focus_down(&mut self) -> bool {
968 if self.floating_is_active.get() {
969 self.floating.focus_down()
970 } else {
971 self.scrolling.focus_down()
972 }
973 }
974
975 pub fn focus_up(&mut self) -> bool {
976 if self.floating_is_active.get() {
977 self.floating.focus_up()
978 } else {
979 self.scrolling.focus_up()
980 }
981 }
982
983 pub fn focus_down_or_left(&mut self) {
984 if self.floating_is_active.get() {
985 self.floating.focus_down();
986 } else {
987 self.scrolling.focus_down_or_left();
988 }
989 }
990
991 pub fn focus_down_or_right(&mut self) {
992 if self.floating_is_active.get() {
993 self.floating.focus_down();
994 } else {
995 self.scrolling.focus_down_or_right();
996 }
997 }
998
999 pub fn focus_up_or_left(&mut self) {
1000 if self.floating_is_active.get() {
1001 self.floating.focus_up();
1002 } else {
1003 self.scrolling.focus_up_or_left();
1004 }
1005 }
1006
1007 pub fn focus_up_or_right(&mut self) {
1008 if self.floating_is_active.get() {
1009 self.floating.focus_up();
1010 } else {
1011 self.scrolling.focus_up_or_right();
1012 }
1013 }
1014
1015 pub fn focus_window_top(&mut self) {
1016 if self.floating_is_active.get() {
1017 self.floating.focus_topmost();
1018 } else {
1019 self.scrolling.focus_top();
1020 }
1021 }
1022
1023 pub fn focus_window_bottom(&mut self) {
1024 if self.floating_is_active.get() {
1025 self.floating.focus_bottommost();
1026 } else {
1027 self.scrolling.focus_bottom();
1028 }
1029 }
1030
1031 pub fn focus_window_down_or_top(&mut self) {
1032 if !self.focus_down() {
1033 self.focus_window_top();
1034 }
1035 }
1036
1037 pub fn focus_window_up_or_bottom(&mut self) {
1038 if !self.focus_up() {
1039 self.focus_window_bottom();
1040 }
1041 }
1042
1043 pub fn move_left(&mut self) -> bool {
1044 if self.floating_is_active.get() {
1045 self.floating.move_left();
1046 true
1047 } else {
1048 self.scrolling.move_left()
1049 }
1050 }
1051
1052 pub fn move_right(&mut self) -> bool {
1053 if self.floating_is_active.get() {
1054 self.floating.move_right();
1055 true
1056 } else {
1057 self.scrolling.move_right()
1058 }
1059 }
1060
1061 pub fn move_column_to_first(&mut self) {
1062 if self.floating_is_active.get() {
1063 return;
1064 }
1065 self.scrolling.move_column_to_first();
1066 }
1067
1068 pub fn move_column_to_last(&mut self) {
1069 if self.floating_is_active.get() {
1070 return;
1071 }
1072 self.scrolling.move_column_to_last();
1073 }
1074
1075 pub fn move_column_to_index(&mut self, index: usize) {
1076 if self.floating_is_active.get() {
1077 return;
1078 }
1079 self.scrolling.move_column_to_index(index);
1080 }
1081
1082 pub fn move_down(&mut self) -> bool {
1083 if self.floating_is_active.get() {
1084 self.floating.move_down();
1085 true
1086 } else {
1087 self.scrolling.move_down()
1088 }
1089 }
1090
1091 pub fn move_up(&mut self) -> bool {
1092 if self.floating_is_active.get() {
1093 self.floating.move_up();
1094 true
1095 } else {
1096 self.scrolling.move_up()
1097 }
1098 }
1099
1100 pub fn consume_or_expel_window_left(&mut self, window: Option<&W::Id>) {
1101 if window.map_or(self.floating_is_active.get(), |id| {
1102 self.floating.has_window(id)
1103 }) {
1104 return;
1105 }
1106 self.scrolling.consume_or_expel_window_left(window);
1107 }
1108
1109 pub fn consume_or_expel_window_right(&mut self, window: Option<&W::Id>) {
1110 if window.map_or(self.floating_is_active.get(), |id| {
1111 self.floating.has_window(id)
1112 }) {
1113 return;
1114 }
1115 self.scrolling.consume_or_expel_window_right(window);
1116 }
1117
1118 pub fn consume_into_column(&mut self) {
1119 if self.floating_is_active.get() {
1120 return;
1121 }
1122 self.scrolling.consume_into_column();
1123 }
1124
1125 pub fn expel_from_column(&mut self) {
1126 if self.floating_is_active.get() {
1127 return;
1128 }
1129 self.scrolling.expel_from_column();
1130 }
1131
1132 pub fn swap_window_in_direction(&mut self, direction: ScrollDirection) {
1133 if self.floating_is_active.get() {
1134 return;
1135 }
1136 self.scrolling.swap_window_in_direction(direction);
1137 }
1138
1139 pub fn toggle_column_tabbed_display(&mut self) {
1140 if self.floating_is_active.get() {
1141 return;
1142 }
1143 self.scrolling.toggle_column_tabbed_display();
1144 }
1145
1146 pub fn set_column_display(&mut self, display: ColumnDisplay) {
1147 if self.floating_is_active.get() {
1148 return;
1149 }
1150 self.scrolling.set_column_display(display);
1151 }
1152
1153 pub fn center_column(&mut self) {
1154 if self.floating_is_active.get() {
1155 self.floating.center_window(None);
1156 } else {
1157 self.scrolling.center_column();
1158 }
1159 }
1160
1161 pub fn center_window(&mut self, id: Option<&W::Id>) {
1162 if id.map_or(self.floating_is_active.get(), |id| {
1163 self.floating.has_window(id)
1164 }) {
1165 self.floating.center_window(id);
1166 } else {
1167 self.scrolling.center_window(id);
1168 }
1169 }
1170
1171 pub fn center_visible_columns(&mut self) {
1172 if self.floating_is_active.get() {
1173 return;
1174 }
1175 self.scrolling.center_visible_columns();
1176 }
1177
1178 pub fn toggle_width(&mut self, forwards: bool) {
1179 if self.floating_is_active.get() {
1180 self.floating.toggle_window_width(None, forwards);
1181 } else {
1182 self.scrolling.toggle_width(forwards);
1183 }
1184 }
1185
1186 pub fn toggle_full_width(&mut self) {
1187 if self.floating_is_active.get() {
1188 // Leave this unimplemented for now. For good UX, this probably needs moving the tile
1189 // to be against the left edge of the working area while it is full-width.
1190 return;
1191 }
1192 self.scrolling.toggle_full_width();
1193 }
1194
1195 pub fn set_column_width(&mut self, change: SizeChange) {
1196 if self.floating_is_active.get() {
1197 self.floating.set_window_width(None, change, true);
1198 } else {
1199 self.scrolling.set_window_width(None, change);
1200 }
1201 }
1202
1203 pub fn set_window_width(&mut self, window: Option<&W::Id>, change: SizeChange) {
1204 if window.map_or(self.floating_is_active.get(), |id| {
1205 self.floating.has_window(id)
1206 }) {
1207 self.floating.set_window_width(window, change, true);
1208 } else {
1209 self.scrolling.set_window_width(window, change);
1210 }
1211 }
1212
1213 pub fn set_window_height(&mut self, window: Option<&W::Id>, change: SizeChange) {
1214 if window.map_or(self.floating_is_active.get(), |id| {
1215 self.floating.has_window(id)
1216 }) {
1217 self.floating.set_window_height(window, change, true);
1218 } else {
1219 self.scrolling.set_window_height(window, change);
1220 }
1221 }
1222
1223 pub fn reset_window_height(&mut self, window: Option<&W::Id>) {
1224 if window.map_or(self.floating_is_active.get(), |id| {
1225 self.floating.has_window(id)
1226 }) {
1227 return;
1228 }
1229 self.scrolling.reset_window_height(window);
1230 }
1231
1232 pub fn toggle_window_width(&mut self, window: Option<&W::Id>, forwards: bool) {
1233 if window.map_or(self.floating_is_active.get(), |id| {
1234 self.floating.has_window(id)
1235 }) {
1236 self.floating.toggle_window_width(window, forwards);
1237 } else {
1238 self.scrolling.toggle_window_width(window, forwards);
1239 }
1240 }
1241
1242 pub fn toggle_window_height(&mut self, window: Option<&W::Id>, forwards: bool) {
1243 if window.map_or(self.floating_is_active.get(), |id| {
1244 self.floating.has_window(id)
1245 }) {
1246 self.floating.toggle_window_height(window, forwards);
1247 } else {
1248 self.scrolling.toggle_window_height(window, forwards);
1249 }
1250 }
1251
1252 pub fn expand_column_to_available_width(&mut self) {
1253 if self.floating_is_active.get() {
1254 return;
1255 }
1256 self.scrolling.expand_column_to_available_width();
1257 }
1258
1259 pub fn set_fullscreen(&mut self, window: &W::Id, is_fullscreen: bool) {
1260 let mut unfullscreen_to_floating = false;
1261 if self.floating.has_window(window) {
1262 if is_fullscreen {
1263 unfullscreen_to_floating = true;
1264 self.toggle_window_floating(Some(window));
1265 } else {
1266 // Floating windows are never fullscreen, so this is an unfullscreen request for an
1267 // already unfullscreen window.
1268 return;
1269 }
1270 } else if !is_fullscreen {
1271 // The window is in the scrolling layout and we're requesting an unfullscreen. If it is
1272 // indeed fullscreen (i.e. this isn't a duplicate unfullscreen request), then we may
1273 // need to unfullscreen into floating.
1274 let tile = self
1275 .scrolling
1276 .tiles()
1277 .find(|tile| tile.window().id() == window)
1278 .unwrap();
1279 if tile.window().is_pending_fullscreen() && tile.unfullscreen_to_floating {
1280 // Unfullscreen and float in one call so it has a chance to notice and request a
1281 // (0, 0) size, rather than the scrolling column size.
1282 self.toggle_window_floating(Some(window));
1283 return;
1284 }
1285 }
1286
1287 let changed = self.scrolling.set_fullscreen(window, is_fullscreen);
1288
1289 // When going to fullscreen, remember if we should unfullscreen to floating.
1290 if changed && is_fullscreen {
1291 let tile = self
1292 .scrolling
1293 .tiles_mut()
1294 .find(|tile| tile.window().id() == window)
1295 .unwrap();
1296
1297 tile.unfullscreen_to_floating = unfullscreen_to_floating;
1298 }
1299 }
1300
1301 pub fn toggle_fullscreen(&mut self, window: &W::Id) {
1302 let tile = self
1303 .tiles()
1304 .find(|tile| tile.window().id() == window)
1305 .unwrap();
1306 let current = tile.window().is_pending_fullscreen();
1307 self.set_fullscreen(window, !current);
1308 }
1309
1310 pub fn toggle_window_floating(&mut self, id: Option<&W::Id>) {
1311 let active_id = self.active_window().map(|win| win.id().clone());
1312 let target_is_active = id.map_or(true, |id| Some(id) == active_id.as_ref());
1313 let Some(id) = id.cloned().or(active_id) else {
1314 return;
1315 };
1316
1317 let (_, render_pos, _) = self
1318 .tiles_with_render_positions()
1319 .find(|(tile, _, _)| *tile.window().id() == id)
1320 .unwrap();
1321
1322 if self.floating.has_window(&id) {
1323 let removed = self.floating.remove_tile(&id);
1324 // FIXME: compute closest pos?
1325 self.scrolling.add_tile(
1326 None,
1327 removed.tile,
1328 target_is_active,
1329 removed.width,
1330 removed.is_full_width,
1331 None,
1332 );
1333 if target_is_active {
1334 self.floating_is_active = FloatingActive::No;
1335 }
1336 } else {
1337 let mut removed = self.scrolling.remove_tile(&id, Transaction::new());
1338 removed.tile.stop_move_animations();
1339
1340 // Come up with a default floating position close to the tile position.
1341 let stored_or_default = self.floating.stored_or_default_tile_pos(&removed.tile);
1342 if stored_or_default.is_none() {
1343 let offset =
1344 if self.options.layout.center_focused_column == CenterFocusedColumn::Always {
1345 Point::from((0., 0.))
1346 } else {
1347 Point::from((50., 50.))
1348 };
1349 let pos = render_pos + offset;
1350 let size = removed.tile.tile_size();
1351 let pos = self.floating.clamp_within_working_area(pos, size);
1352 let pos = self.floating.logical_to_size_frac(pos);
1353 removed.tile.floating_pos = Some(pos);
1354 }
1355
1356 self.floating.add_tile(removed.tile, target_is_active);
1357 if target_is_active {
1358 self.floating_is_active = FloatingActive::Yes;
1359 }
1360 }
1361
1362 let (tile, new_render_pos) = self
1363 .tiles_with_render_positions_mut(false)
1364 .find(|(tile, _)| *tile.window().id() == id)
1365 .unwrap();
1366
1367 tile.animate_move_from(render_pos - new_render_pos);
1368 }
1369
1370 pub fn set_window_floating(&mut self, id: Option<&W::Id>, floating: bool) {
1371 if id.map_or(self.floating_is_active.get(), |id| {
1372 self.floating.has_window(id)
1373 }) == floating
1374 {
1375 return;
1376 }
1377
1378 self.toggle_window_floating(id);
1379 }
1380
1381 pub fn focus_floating(&mut self) {
1382 if !self.floating_is_active.get() {
1383 self.switch_focus_floating_tiling();
1384 }
1385 }
1386
1387 pub fn focus_tiling(&mut self) {
1388 if self.floating_is_active.get() {
1389 self.switch_focus_floating_tiling();
1390 }
1391 }
1392
1393 pub fn switch_focus_floating_tiling(&mut self) {
1394 if self.floating.is_empty() {
1395 // If floating is empty, keep focus on scrolling.
1396 return;
1397 } else if self.scrolling.is_empty() {
1398 // If floating isn't empty but scrolling is, keep focus on floating.
1399 return;
1400 }
1401
1402 self.floating_is_active = if self.floating_is_active.get() {
1403 FloatingActive::No
1404 } else {
1405 FloatingActive::Yes
1406 };
1407 }
1408
1409 pub fn move_floating_window(
1410 &mut self,
1411 id: Option<&W::Id>,
1412 x: PositionChange,
1413 y: PositionChange,
1414 animate: bool,
1415 ) {
1416 if id.map_or(self.floating_is_active.get(), |id| {
1417 self.floating.has_window(id)
1418 }) {
1419 self.floating.move_window(id, x, y, animate);
1420 } else {
1421 // If the target tile isn't floating, set its stored floating position.
1422 let tile = if let Some(id) = id {
1423 self.scrolling
1424 .tiles_mut()
1425 .find(|tile| tile.window().id() == id)
1426 .unwrap()
1427 } else if let Some(tile) = self.scrolling.active_tile_mut() {
1428 tile
1429 } else {
1430 return;
1431 };
1432
1433 let working_area_loc = self.floating.working_area().loc;
1434 let pos = self.floating.stored_or_default_tile_pos(tile);
1435
1436 // If there's no stored floating position, we can only set both components at once, not
1437 // adjust.
1438 let pos = pos.or_else(|| {
1439 (matches!(x, PositionChange::SetFixed(_))
1440 && matches!(y, PositionChange::SetFixed(_)))
1441 .then_some(Point::default())
1442 });
1443
1444 let Some(mut pos) = pos else {
1445 return;
1446 };
1447
1448 match x {
1449 PositionChange::SetFixed(x) => pos.x = x + working_area_loc.x,
1450 PositionChange::AdjustFixed(x) => pos.x += x,
1451 }
1452 match y {
1453 PositionChange::SetFixed(y) => pos.y = y + working_area_loc.y,
1454 PositionChange::AdjustFixed(y) => pos.y += y,
1455 }
1456
1457 let pos = self.floating.logical_to_size_frac(pos);
1458 tile.floating_pos = Some(pos);
1459 }
1460 }
1461
1462 pub fn has_windows(&self) -> bool {
1463 self.windows().next().is_some()
1464 }
1465
1466 pub fn has_window(&self, window: &W::Id) -> bool {
1467 self.windows().any(|win| win.id() == window)
1468 }
1469
1470 pub fn find_wl_surface(&self, wl_surface: &WlSurface) -> Option<&W> {
1471 self.windows().find(|win| win.is_wl_surface(wl_surface))
1472 }
1473
1474 pub fn find_wl_surface_mut(&mut self, wl_surface: &WlSurface) -> Option<&mut W> {
1475 self.windows_mut().find(|win| win.is_wl_surface(wl_surface))
1476 }
1477
1478 pub fn tiles_with_render_positions(
1479 &self,
1480 ) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>, bool)> {
1481 let scrolling = self.scrolling.tiles_with_render_positions();
1482
1483 let floating = self.floating.tiles_with_render_positions();
1484 let visible = self.is_floating_visible();
1485 let floating = floating.map(move |(tile, pos)| (tile, pos, visible));
1486
1487 floating.chain(scrolling)
1488 }
1489
1490 pub fn tiles_with_render_positions_mut(
1491 &mut self,
1492 round: bool,
1493 ) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> {
1494 let scrolling = self.scrolling.tiles_with_render_positions_mut(round);
1495 let floating = self.floating.tiles_with_render_positions_mut(round);
1496 floating.chain(scrolling)
1497 }
1498
1499 pub fn tiles_with_ipc_layouts(&self) -> impl Iterator<Item = (&Tile<W>, WindowLayout)> {
1500 let scrolling = self.scrolling.tiles_with_ipc_layouts();
1501 let floating = self.floating.tiles_with_ipc_layouts();
1502 floating.chain(scrolling)
1503 }
1504
1505 pub fn active_tile_visual_rectangle(&self) -> Option<Rectangle<f64, Logical>> {
1506 if self.floating_is_active.get() {
1507 self.floating.active_tile_visual_rectangle()
1508 } else {
1509 self.scrolling.active_tile_visual_rectangle()
1510 }
1511 }
1512
1513 pub fn popup_target_rect(&self, window: &W::Id) -> Option<Rectangle<f64, Logical>> {
1514 if self.floating.has_window(window) {
1515 self.floating.popup_target_rect(window)
1516 } else {
1517 self.scrolling.popup_target_rect(window)
1518 }
1519 }
1520
1521 pub fn render_elements<R: NiriRenderer>(
1522 &self,
1523 renderer: &mut R,
1524 target: RenderTarget,
1525 focus_ring: bool,
1526 ) -> (
1527 impl Iterator<Item = WorkspaceRenderElement<R>>,
1528 impl Iterator<Item = WorkspaceRenderElement<R>>,
1529 ) {
1530 let scrolling_focus_ring = focus_ring && !self.floating_is_active();
1531 let scrolling = self
1532 .scrolling
1533 .render_elements(renderer, target, scrolling_focus_ring);
1534 let scrolling = scrolling.into_iter().map(WorkspaceRenderElement::from);
1535
1536 let floating_focus_ring = focus_ring && self.floating_is_active();
1537 let floating = self.is_floating_visible().then(|| {
1538 let view_rect = Rectangle::from_size(self.view_size);
1539 let floating =
1540 self.floating
1541 .render_elements(renderer, view_rect, target, floating_focus_ring);
1542 floating.into_iter().map(WorkspaceRenderElement::from)
1543 });
1544 let floating = floating.into_iter().flatten();
1545
1546 (floating, scrolling)
1547 }
1548
1549 pub fn render_shadow<R: NiriRenderer>(
1550 &self,
1551 renderer: &mut R,
1552 ) -> impl Iterator<Item = ShadowRenderElement> + '_ {
1553 self.shadow.render(renderer, Point::from((0., 0.)))
1554 }
1555
1556 pub fn render_background(&self) -> SolidColorRenderElement {
1557 SolidColorRenderElement::from_buffer(
1558 &self.background_buffer,
1559 Point::new(0., 0.),
1560 1.,
1561 Kind::Unspecified,
1562 )
1563 }
1564
1565 pub fn render_above_top_layer(&self) -> bool {
1566 self.scrolling.render_above_top_layer()
1567 }
1568
1569 pub fn is_floating_visible(&self) -> bool {
1570 // If the focus is on a fullscreen scrolling window, hide the floating windows.
1571 matches!(
1572 self.floating_is_active,
1573 FloatingActive::Yes | FloatingActive::NoButRaised
1574 ) || !self.render_above_top_layer()
1575 }
1576
1577 pub fn store_unmap_snapshot_if_empty(&mut self, renderer: &mut GlesRenderer, window: &W::Id) {
1578 let view_size = self.view_size();
1579 for (tile, tile_pos) in self.tiles_with_render_positions_mut(false) {
1580 if tile.window().id() == window {
1581 let view_pos = Point::from((-tile_pos.x, -tile_pos.y));
1582 let view_rect = Rectangle::new(view_pos, view_size);
1583 tile.update_render_elements(false, view_rect);
1584 tile.store_unmap_snapshot_if_empty(renderer);
1585 return;
1586 }
1587 }
1588 }
1589
1590 pub fn clear_unmap_snapshot(&mut self, window: &W::Id) {
1591 for tile in self.tiles_mut() {
1592 if tile.window().id() == window {
1593 let _ = tile.take_unmap_snapshot();
1594 return;
1595 }
1596 }
1597 }
1598
1599 pub fn start_close_animation_for_window(
1600 &mut self,
1601 renderer: &mut GlesRenderer,
1602 window: &W::Id,
1603 blocker: TransactionBlocker,
1604 ) {
1605 if self.floating.has_window(window) {
1606 self.floating
1607 .start_close_animation_for_window(renderer, window, blocker);
1608 } else {
1609 self.scrolling
1610 .start_close_animation_for_window(renderer, window, blocker);
1611 }
1612 }
1613
1614 pub fn start_close_animation_for_tile(
1615 &mut self,
1616 renderer: &mut GlesRenderer,
1617 snapshot: TileRenderSnapshot,
1618 tile_size: Size<f64, Logical>,
1619 tile_pos: Point<f64, Logical>,
1620 blocker: TransactionBlocker,
1621 ) {
1622 self.floating
1623 .start_close_animation_for_tile(renderer, snapshot, tile_size, tile_pos, blocker);
1624 }
1625
1626 pub fn start_open_animation(&mut self, id: &W::Id) -> bool {
1627 self.scrolling.start_open_animation(id) || self.floating.start_open_animation(id)
1628 }
1629
1630 pub fn window_under(&self, pos: Point<f64, Logical>) -> Option<(&W, HitType)> {
1631 // This logic is consistent with tiles_with_render_positions().
1632 if self.is_floating_visible() {
1633 if let Some(rv) = self
1634 .floating
1635 .tiles_with_render_positions()
1636 .find_map(|(tile, tile_pos)| HitType::hit_tile(tile, tile_pos, pos))
1637 {
1638 return Some(rv);
1639 }
1640 }
1641
1642 self.scrolling.window_under(pos)
1643 }
1644
1645 pub fn resize_edges_under(&self, pos: Point<f64, Logical>) -> Option<ResizeEdge> {
1646 self.tiles_with_render_positions()
1647 .find_map(|(tile, tile_pos, visible)| {
1648 // This logic should be consistent with window_under() in when it returns Some vs.
1649 // None.
1650 if !visible {
1651 return None;
1652 }
1653
1654 let pos_within_tile = pos - tile_pos;
1655
1656 if tile.hit(pos_within_tile).is_some() {
1657 let size = tile.tile_size().to_f64();
1658
1659 let mut edges = ResizeEdge::empty();
1660 if pos_within_tile.x < size.w / 3. {
1661 edges |= ResizeEdge::LEFT;
1662 } else if 2. * size.w / 3. < pos_within_tile.x {
1663 edges |= ResizeEdge::RIGHT;
1664 }
1665 if pos_within_tile.y < size.h / 3. {
1666 edges |= ResizeEdge::TOP;
1667 } else if 2. * size.h / 3. < pos_within_tile.y {
1668 edges |= ResizeEdge::BOTTOM;
1669 }
1670 return Some(edges);
1671 }
1672
1673 None
1674 })
1675 }
1676
1677 pub fn descendants_added(&mut self, id: &W::Id) -> bool {
1678 self.floating.descendants_added(id)
1679 }
1680
1681 pub fn update_window(&mut self, window: &W::Id, serial: Option<Serial>) {
1682 if !self.floating.update_window(window, serial) {
1683 self.scrolling.update_window(window, serial);
1684 }
1685 }
1686
1687 pub fn refresh(&mut self, is_active: bool, is_focused: bool) {
1688 self.scrolling
1689 .refresh(is_active && !self.floating_is_active.get(), is_focused);
1690 self.floating
1691 .refresh(is_active && self.floating_is_active.get(), is_focused);
1692 }
1693
1694 pub fn scroll_amount_to_activate(&self, window: &W::Id) -> f64 {
1695 if self.floating.has_window(window) {
1696 return 0.;
1697 }
1698
1699 self.scrolling.scroll_amount_to_activate(window)
1700 }
1701
1702 pub fn is_urgent(&self) -> bool {
1703 self.windows().any(|win| win.is_urgent())
1704 }
1705
1706 pub fn activate_window(&mut self, window: &W::Id) -> bool {
1707 if self.floating.activate_window(window) {
1708 self.floating_is_active = FloatingActive::Yes;
1709 true
1710 } else if self.scrolling.activate_window(window) {
1711 self.floating_is_active = FloatingActive::No;
1712 true
1713 } else {
1714 false
1715 }
1716 }
1717
1718 pub fn activate_window_without_raising(&mut self, window: &W::Id) -> bool {
1719 if self.floating.activate_window_without_raising(window) {
1720 self.floating_is_active = FloatingActive::Yes;
1721 true
1722 } else if self.scrolling.activate_window(window) {
1723 self.floating_is_active = match self.floating_is_active {
1724 FloatingActive::No => FloatingActive::No,
1725 FloatingActive::NoButRaised => FloatingActive::NoButRaised,
1726 FloatingActive::Yes => FloatingActive::NoButRaised,
1727 };
1728 true
1729 } else {
1730 false
1731 }
1732 }
1733
1734 pub(super) fn scrolling_insert_position(&self, pos: Point<f64, Logical>) -> InsertPosition {
1735 self.scrolling.insert_position(pos)
1736 }
1737
1738 pub(super) fn insert_hint_area(
1739 &self,
1740 position: InsertPosition,
1741 ) -> Option<Rectangle<f64, Logical>> {
1742 self.scrolling.insert_hint_area(position)
1743 }
1744
1745 pub fn view_offset_gesture_begin(&mut self, is_touchpad: bool) {
1746 self.scrolling.view_offset_gesture_begin(is_touchpad);
1747 }
1748
1749 pub fn view_offset_gesture_update(
1750 &mut self,
1751 delta_x: f64,
1752 timestamp: Duration,
1753 is_touchpad: bool,
1754 ) -> Option<bool> {
1755 self.scrolling
1756 .view_offset_gesture_update(delta_x, timestamp, is_touchpad)
1757 }
1758
1759 pub fn view_offset_gesture_end(&mut self, is_touchpad: Option<bool>) -> bool {
1760 self.scrolling.view_offset_gesture_end(is_touchpad)
1761 }
1762
1763 pub fn dnd_scroll_gesture_begin(&mut self) {
1764 self.scrolling.dnd_scroll_gesture_begin();
1765 }
1766
1767 pub fn dnd_scroll_gesture_scroll(&mut self, pos: Point<f64, Logical>, speed: f64) -> bool {
1768 let config = &self.options.gestures.dnd_edge_view_scroll;
1769 let trigger_width = config.trigger_width;
1770
1771 // This working area intentionally does not include extra struts from Options.
1772 let x = pos.x - self.working_area.loc.x;
1773 let width = self.working_area.size.w;
1774
1775 let x = x.clamp(0., width);
1776 let trigger_width = trigger_width.clamp(0., width / 2.);
1777
1778 let delta = if x < trigger_width {
1779 -(trigger_width - x)
1780 } else if width - x < trigger_width {
1781 trigger_width - (width - x)
1782 } else {
1783 0.
1784 };
1785
1786 let delta = if trigger_width < 0.01 {
1787 // Sanity check for trigger-width 0 or small window sizes.
1788 0.
1789 } else {
1790 // Normalize to [0, 1].
1791 delta / trigger_width
1792 };
1793 let delta = delta * speed;
1794
1795 self.scrolling.dnd_scroll_gesture_scroll(delta)
1796 }
1797
1798 pub fn dnd_scroll_gesture_end(&mut self) {
1799 self.scrolling.dnd_scroll_gesture_end();
1800 }
1801
1802 pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool {
1803 if self.floating.has_window(&window) {
1804 self.floating.interactive_resize_begin(window, edges)
1805 } else {
1806 self.scrolling.interactive_resize_begin(window, edges)
1807 }
1808 }
1809
1810 pub fn interactive_resize_update(
1811 &mut self,
1812 window: &W::Id,
1813 delta: Point<f64, Logical>,
1814 ) -> bool {
1815 if self.floating.has_window(window) {
1816 self.floating.interactive_resize_update(window, delta)
1817 } else {
1818 self.scrolling.interactive_resize_update(window, delta)
1819 }
1820 }
1821
1822 pub fn interactive_resize_end(&mut self, window: Option<&W::Id>) {
1823 if let Some(window) = window {
1824 if self.floating.has_window(window) {
1825 self.floating.interactive_resize_end(Some(window));
1826 } else {
1827 self.scrolling.interactive_resize_end(Some(window));
1828 }
1829 } else {
1830 self.floating.interactive_resize_end(None);
1831 self.scrolling.interactive_resize_end(None);
1832 }
1833 }
1834
1835 pub fn floating_is_active(&self) -> bool {
1836 self.floating_is_active.get()
1837 }
1838
1839 pub fn floating_logical_to_size_frac(
1840 &self,
1841 logical_pos: Point<f64, Logical>,
1842 ) -> Point<f64, SizeFrac> {
1843 self.floating.logical_to_size_frac(logical_pos)
1844 }
1845
1846 pub fn working_area(&self) -> Rectangle<f64, Logical> {
1847 self.working_area
1848 }
1849
1850 pub fn layout_config(&self) -> Option<&niri_config::LayoutPart> {
1851 self.layout_config.as_ref()
1852 }
1853
1854 #[cfg(test)]
1855 pub fn scrolling(&self) -> &ScrollingSpace<W> {
1856 &self.scrolling
1857 }
1858
1859 #[cfg(test)]
1860 pub fn floating(&self) -> &FloatingSpace<W> {
1861 &self.floating
1862 }
1863
1864 #[cfg(test)]
1865 pub fn verify_invariants(&self, move_win_id: Option<&W::Id>) {
1866 use approx::assert_abs_diff_eq;
1867
1868 let scale = self.scale.fractional_scale();
1869 assert!(scale > 0.);
1870 assert!(scale.is_finite());
1871
1872 let options = Options::clone(&self.base_options)
1873 .with_merged_layout(self.layout_config.as_ref())
1874 .adjusted_for_scale(scale);
1875 assert_eq!(
1876 &*self.options, &options,
1877 "options must be base options adjusted for scale"
1878 );
1879
1880 assert!(self.view_size.w > 0.);
1881 assert!(self.view_size.h > 0.);
1882
1883 assert_eq!(self.background_buffer.size(), self.view_size);
1884 assert_eq!(
1885 self.background_buffer.color().components(),
1886 options.layout.background_color.to_array_unpremul(),
1887 );
1888
1889 assert_eq!(self.view_size, self.scrolling.view_size());
1890 assert_eq!(self.working_area, self.scrolling.parent_area());
1891 assert_eq!(&self.clock, self.scrolling.clock());
1892 assert!(Rc::ptr_eq(&self.options, self.scrolling.options()));
1893 self.scrolling.verify_invariants();
1894
1895 assert_eq!(self.view_size, self.floating.view_size());
1896 assert_eq!(self.working_area, self.floating.working_area());
1897 assert_eq!(&self.clock, self.floating.clock());
1898 assert!(Rc::ptr_eq(&self.options, self.floating.options()));
1899 self.floating.verify_invariants();
1900
1901 if self.floating.is_empty() {
1902 assert!(
1903 !self.floating_is_active.get(),
1904 "when floating is empty it must never be active"
1905 );
1906 } else if self.scrolling.is_empty() {
1907 assert!(
1908 self.floating_is_active.get(),
1909 "when scrolling is empty but floating isn't, floating should be active"
1910 );
1911 }
1912
1913 for (tile, tile_pos, visible) in self.tiles_with_render_positions() {
1914 if Some(tile.window().id()) != move_win_id {
1915 assert_eq!(tile.interactive_move_offset, Point::from((0., 0.)));
1916 }
1917
1918 let rounded_pos = tile_pos.to_physical_precise_round(scale).to_logical(scale);
1919
1920 // Tile positions must be rounded to physical pixels.
1921 assert_abs_diff_eq!(tile_pos.x, rounded_pos.x, epsilon = 1e-5);
1922 assert_abs_diff_eq!(tile_pos.y, rounded_pos.y, epsilon = 1e-5);
1923
1924 if let Some(alpha) = &tile.alpha_animation {
1925 let anim = &alpha.anim;
1926 if visible {
1927 assert_eq!(anim.to(), 1., "visible tiles can animate alpha only to 1");
1928 }
1929
1930 assert!(
1931 !alpha.hold_after_done,
1932 "tiles in the layout cannot have held alpha animation"
1933 );
1934 }
1935 }
1936 }
1937}
1938
1939pub(super) fn compute_working_area(output: &Output) -> Rectangle<f64, Logical> {
1940 layer_map_for_output(output).non_exclusive_zone().to_f64()
1941}
1942
1943fn compute_workspace_shadow_config(
1944 config: niri_config::WorkspaceShadow,
1945 view_size: Size<f64, Logical>,
1946) -> niri_config::Shadow {
1947 // Gaps between workspaces are a multiple of the view height, so shadow settings should also be
1948 // normalized to the view height to prevent them from overlapping on lower resolutions.
1949 let norm = view_size.h / 1080.;
1950
1951 let mut config = niri_config::Shadow::from(config);
1952 config.softness *= norm;
1953 config.spread *= norm;
1954 config.offset.x.0 *= norm;
1955 config.offset.y.0 *= norm;
1956
1957 config
1958}