forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB53500dcTahinli1 month ago2998 commits

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