forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

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