forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBb0eb8adIvan Molodetskikh3 months ago2857 commits

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