forge
Explore
Log in

Don't you dare go Hollow

Split layout mod into files

Browse files at this commit

ed3080d908001bf468789b8f47f893e00306135dIvan MolodetskikhParent: 461ce5f
No functional change intended.

4 changed files +2125 -2068

src/layout/focus_ring.rsadded+108 -0
@@ -0,0 +1,108 @@
1+use std::iter::zip;
2+
3+use arrayvec::ArrayVec;
4+use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};
5+use smithay::backend::renderer::element::Kind;
6+use smithay::utils::{Logical, Point, Scale, Size};
7+
8+use crate::config::{self, Color};
9+
10+#[derive(Debug)]
11+pub struct FocusRing {
12+ buffers: [SolidColorBuffer; 4],
13+ locations: [Point<i32, Logical>; 4],
14+ is_off: bool,
15+ is_border: bool,
16+ width: i32,
17+ active_color: Color,
18+ inactive_color: Color,
19+}
20+
21+pub type FocusRingRenderElement = SolidColorRenderElement;
22+
23+impl FocusRing {
24+ pub fn new(config: config::FocusRing) -> Self {
25+ Self {
26+ buffers: Default::default(),
27+ locations: Default::default(),
28+ is_off: config.off,
29+ is_border: false,
30+ width: config.width.into(),
31+ active_color: config.active_color,
32+ inactive_color: config.inactive_color,
33+ }
34+ }
35+
36+ pub fn update_config(&mut self, config: config::FocusRing) {
37+ self.is_off = config.off;
38+ self.width = config.width.into();
39+ self.active_color = config.active_color;
40+ self.inactive_color = config.inactive_color;
41+ }
42+
43+ pub fn update(
44+ &mut self,
45+ win_pos: Point<i32, Logical>,
46+ win_size: Size<i32, Logical>,
47+ is_border: bool,
48+ ) {
49+ if is_border {
50+ self.buffers[0].resize((win_size.w + self.width * 2, self.width));
51+ self.buffers[1].resize((win_size.w + self.width * 2, self.width));
52+ self.buffers[2].resize((self.width, win_size.h));
53+ self.buffers[3].resize((self.width, win_size.h));
54+
55+ self.locations[0] = win_pos + Point::from((-self.width, -self.width));
56+ self.locations[1] = win_pos + Point::from((-self.width, win_size.h));
57+ self.locations[2] = win_pos + Point::from((-self.width, 0));
58+ self.locations[3] = win_pos + Point::from((win_size.w, 0));
59+ } else {
60+ let size = win_size + Size::from((self.width * 2, self.width * 2));
61+ self.buffers[0].resize(size);
62+ self.locations[0] = win_pos - Point::from((self.width, self.width));
63+ }
64+
65+ self.is_border = is_border;
66+ }
67+
68+ pub fn set_active(&mut self, is_active: bool) {
69+ let color = if is_active {
70+ self.active_color.into()
71+ } else {
72+ self.inactive_color.into()
73+ };
74+
75+ for buf in &mut self.buffers {
76+ buf.set_color(color);
77+ }
78+ }
79+
80+ pub fn render(&self, scale: Scale<f64>) -> impl Iterator<Item = SolidColorRenderElement> {
81+ let mut rv = ArrayVec::<_, 4>::new();
82+
83+ if self.is_off {
84+ return rv.into_iter();
85+ }
86+
87+ let mut push = |buffer, location: Point<i32, Logical>| {
88+ let elem = SolidColorRenderElement::from_buffer(
89+ buffer,
90+ location.to_physical_precise_round(scale),
91+ scale,
92+ 1.,
93+ Kind::Unspecified,
94+ );
95+ rv.push(elem);
96+ };
97+
98+ if self.is_border {
99+ for (buf, loc) in zip(&self.buffers, self.locations) {
100+ push(buf, loc);
101+ }
102+ } else {
103+ push(&self.buffers[0], self.locations[0]);
104+ }
105+
106+ rv.into_iter()
107+ }
108+}
src/layout/mod.rsmodified+11 -2068

Diff too large; view file

@@ -29,47 +29,30 @@
29 //! compromise we only keep the first workspace there, and move the rest to the primary output,29 //! compromise we only keep the first workspace there, and move the rest to the primary output,
30 //! making the primary output their original output.30 //! making the primary output their original output.
31 31
32-use std::cmp::{max, min};
33-use std::iter::zip;
34 use std::mem;32 use std::mem;
35 use std::rc::Rc;33 use std::rc::Rc;
36 use std::time::Duration;34 use std::time::Duration;
37 35
38-use arrayvec::ArrayVec;
39-use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};
40-use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
41-use smithay::backend::renderer::element::utils::{
42- CropRenderElement, Relocate, RelocateRenderElement,
43-};
44-use smithay::backend::renderer::element::{AsRenderElements, Kind};
45-use smithay::backend::renderer::gles::GlesRenderer;
46-use smithay::backend::renderer::ImportAll;
47 use smithay::desktop::space::SpaceElement;36 use smithay::desktop::space::SpaceElement;
48-use smithay::desktop::{layer_map_for_output, Window};37+use smithay::desktop::Window;
49 use smithay::output::Output;38 use smithay::output::Output;
50 use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1;39 use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1;
51 use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;40 use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
52 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;41 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
53-use smithay::render_elements;42+use smithay::utils::{Logical, Point, Size, Transform};
54-use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform};
55 use smithay::wayland::compositor::{send_surface_state, with_states};43 use smithay::wayland::compositor::{send_surface_state, with_states};
56 use smithay::wayland::shell::xdg::SurfaceCachedState;44 use smithay::wayland::shell::xdg::SurfaceCachedState;
57 45
46+pub use self::monitor::MonitorRenderElement;
47+use self::monitor::{Monitor, WorkspaceSwitch, WorkspaceSwitchGesture};
48+use self::workspace::{compute_working_area, ColumnWidth, OutputId, Workspace};
58 use crate::animation::Animation;49 use crate::animation::Animation;
59-use crate::config::{self, Color, Config, PresetWidth, SizeChange, Struts};50+use crate::config::{self, Config, SizeChange, Struts};
60 use crate::utils::output_size;51 use crate::utils::output_size;
61 52
62-#[derive(Debug, Clone, PartialEq, Eq)]53+mod focus_ring;
63-pub struct OutputId(String);54+mod monitor;
64-55+mod workspace;
65-render_elements! {
66- #[derive(Debug)]
67- pub WorkspaceRenderElement<R> where R: ImportAll;
68- Wayland = WaylandSurfaceRenderElement<R>,
69- FocusRing = SolidColorRenderElement,
70-}
71-pub type MonitorRenderElement<R> =
72- RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>;
73 56
74 pub trait LayoutElement: SpaceElement + PartialEq + Clone {57 pub trait LayoutElement: SpaceElement + PartialEq + Clone {
75 fn request_size(&self, size: Size<i32, Logical>);58 fn request_size(&self, size: Size<i32, Logical>);
@@ -107,103 +90,8 @@ enum MonitorSet<W: LayoutElement> {
107 },90 },
108 }91 }
109 92
110-#[derive(Debug)]
111-pub struct Monitor<W: LayoutElement> {
112- /// Output for this monitor.
113- output: Output,
114- // Must always contain at least one.
115- workspaces: Vec<Workspace<W>>,
116- /// Index of the currently active workspace.
117- active_workspace_idx: usize,
118- /// In-progress switch between workspaces.
119- workspace_switch: Option<WorkspaceSwitch>,
120- /// Configurable properties of the layout.
121- options: Rc<Options>,
122-}
123-
124-#[derive(Debug)]
125-enum WorkspaceSwitch {
126- Animation(Animation),
127- Gesture(WorkspaceSwitchGesture),
128-}
129-
130-#[derive(Debug)]
131-struct WorkspaceSwitchGesture {
132- /// Index of the workspace where the gesture was started.
133- center_idx: usize,
134- /// Current, fractional workspace index.
135- current_idx: f64,
136-}
137-
138-#[derive(Debug)]
139-pub struct Workspace<W: LayoutElement> {
140- /// The original output of this workspace.
141- ///
142- /// Most of the time this will be the workspace's current output, however, after an output
143- /// disconnection, it may remain pointing to the disconnected output.
144- original_output: OutputId,
145-
146- /// Current output of this workspace.
147- output: Option<Output>,
148-
149- /// Latest known view size for this workspace.
150- ///
151- /// This should be computed from the current workspace output size, or, if all outputs have
152- /// been disconnected, preserved until a new output is connected.
153- view_size: Size<i32, Logical>,
154-
155- /// Latest known working area for this workspace.
156- ///
157- /// This is similar to view size, but takes into account things like layer shell exclusive
158- /// zones.
159- working_area: Rectangle<i32, Logical>,
160-
161- /// Columns of windows on this workspace.
162- columns: Vec<Column<W>>,
163-
164- /// Index of the currently active column, if any.
165- active_column_idx: usize,
166-
167- /// Focus ring buffer and parameters.
168- focus_ring: FocusRing,
169-
170- /// Offset of the view computed from the active column.
171- ///
172- /// Any gaps, including left padding from work area left exclusive zone, is handled
173- /// with this view offset (rather than added as a constant elsewhere in the code). This allows
174- /// for natural handling of fullscreen windows, which must ignore work area padding.
175- view_offset: i32,
176-
177- /// Animation of the view offset, if one is currently ongoing.
178- view_offset_anim: Option<Animation>,
179-
180- /// Whether to activate the previous, rather than the next, column upon column removal.
181- ///
182- /// When a new column is created and removed with no focus changes in-between, it is more
183- /// natural to activate the previously-focused column. This variable tracks that.
184- ///
185- /// Since we only create-and-activate columns immediately to the right of the active column (in
186- /// contrast to tabs in Firefox, for example), we can track this as a bool, rather than an
187- /// index of the previous column to activate.
188- activate_prev_column_on_removal: bool,
189-
190- /// Configurable properties of the layout.
191- options: Rc<Options>,
192-}
193-
194-#[derive(Debug)]
195-struct FocusRing {
196- buffers: [SolidColorBuffer; 4],
197- locations: [Point<i32, Logical>; 4],
198- is_off: bool,
199- is_border: bool,
200- width: i32,
201- active_color: Color,
202- inactive_color: Color,
203-}
204-
205 #[derive(Debug, PartialEq)]93 #[derive(Debug, PartialEq)]
206-struct Options {94+pub struct Options {
207 /// Padding around windows in logical pixels.95 /// Padding around windows in logical pixels.
208 gaps: i32,96 gaps: i32,
209 /// Extra padding around the working area in logical pixels.97 /// Extra padding around the working area in logical pixels.
@@ -263,92 +151,6 @@ impl Options {
263 }151 }
264 }152 }
265 153
266-/// Width of a column.
267-#[derive(Debug, Clone, Copy, PartialEq)]
268-pub enum ColumnWidth {
269- /// Proportion of the current view width.
270- Proportion(f64),
271- /// One of the proportion presets.
272- ///
273- /// This is separate from Proportion in order to be able to reliably cycle between preset
274- /// proportions.
275- Preset(usize),
276- /// Fixed width in logical pixels.
277- Fixed(i32),
278-}
279-
280-impl From<PresetWidth> for ColumnWidth {
281- fn from(value: PresetWidth) -> Self {
282- match value {
283- PresetWidth::Proportion(p) => Self::Proportion(p.clamp(0., 10000.)),
284- PresetWidth::Fixed(f) => Self::Fixed(f.clamp(1, 100000)),
285- }
286- }
287-}
288-
289-/// Height of a window in a column.
290-///
291-/// Proportional height is intentionally omitted. With column widths you frequently want e.g. two
292-/// columns side-by-side with 50% width each, and you want them to remain this way when moving to a
293-/// differently sized monitor. Windows in a column, however, already auto-size to fill the available
294-/// height, giving you this behavior. The only reason to set a different window height, then, is
295-/// when you want something in the window to fit exactly, e.g. to fit 30 lines in a terminal, which
296-/// corresponds to the `Fixed` variant.
297-///
298-/// This does not preclude the usual set of binds to set or resize a window proportionally. Just,
299-/// they are converted to, and stored as fixed height right away, so that once you resize a window
300-/// to fit the desired content, it can never become smaller than that when moving between monitors.
301-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
302-pub enum WindowHeight {
303- /// Automatically computed height, evenly distributed across the column.
304- Auto,
305- /// Fixed height in logical pixels.
306- Fixed(i32),
307-}
308-
309-#[derive(Debug)]
310-struct Column<W: LayoutElement> {
311- /// Windows in this column.
312- ///
313- /// Must be non-empty.
314- windows: Vec<W>,
315-
316- /// Heights of the windows.
317- ///
318- /// Must have the same number of elements as `windows`.
319- heights: Vec<WindowHeight>,
320-
321- /// Index of the currently active window.
322- active_window_idx: usize,
323-
324- /// Desired width of this column.
325- ///
326- /// If the column is full-width or full-screened, this is the width that should be restored
327- /// upon unfullscreening and untoggling full-width.
328- width: ColumnWidth,
329-
330- /// Whether this column is full-width.
331- is_full_width: bool,
332-
333- /// Whether this column contains a single full-screened window.
334- is_fullscreen: bool,
335-
336- /// Latest known view size for this column's workspace.
337- view_size: Size<i32, Logical>,
338-
339- /// Latest known working area for this column's workspace.
340- working_area: Rectangle<i32, Logical>,
341-
342- /// Configurable properties of the layout.
343- options: Rc<Options>,
344-}
345-
346-impl OutputId {
347- pub fn new(output: &Output) -> Self {
348- Self(output.name())
349- }
350-}
351-
352 impl LayoutElement for Window {154 impl LayoutElement for Window {
353 fn request_size(&self, size: Size<i32, Logical>) {155 fn request_size(&self, size: Size<i32, Logical>) {
354 self.toplevel().with_pending_state(|state| {156 self.toplevel().with_pending_state(|state| {
@@ -394,117 +196,6 @@ impl LayoutElement for Window {
394 }196 }
395 }197 }
396 198
397-impl FocusRing {
398- fn update(
399- &mut self,
400- win_pos: Point<i32, Logical>,
401- win_size: Size<i32, Logical>,
402- is_border: bool,
403- ) {
404- if is_border {
405- self.buffers[0].resize((win_size.w + self.width * 2, self.width));
406- self.buffers[1].resize((win_size.w + self.width * 2, self.width));
407- self.buffers[2].resize((self.width, win_size.h));
408- self.buffers[3].resize((self.width, win_size.h));
409-
410- self.locations[0] = win_pos + Point::from((-self.width, -self.width));
411- self.locations[1] = win_pos + Point::from((-self.width, win_size.h));
412- self.locations[2] = win_pos + Point::from((-self.width, 0));
413- self.locations[3] = win_pos + Point::from((win_size.w, 0));
414- } else {
415- let size = win_size + Size::from((self.width * 2, self.width * 2));
416- self.buffers[0].resize(size);
417- self.locations[0] = win_pos - Point::from((self.width, self.width));
418- }
419-
420- self.is_border = is_border;
421- }
422-
423- fn set_active(&mut self, is_active: bool) {
424- let color = if is_active {
425- self.active_color.into()
426- } else {
427- self.inactive_color.into()
428- };
429-
430- for buf in &mut self.buffers {
431- buf.set_color(color);
432- }
433- }
434-
435- fn render(&self, scale: Scale<f64>) -> impl Iterator<Item = SolidColorRenderElement> {
436- let mut rv = ArrayVec::<_, 4>::new();
437-
438- if self.is_off {
439- return rv.into_iter();
440- }
441-
442- let mut push = |buffer, location: Point<i32, Logical>| {
443- let elem = SolidColorRenderElement::from_buffer(
444- buffer,
445- location.to_physical_precise_round(scale),
446- scale,
447- 1.,
448- Kind::Unspecified,
449- );
450- rv.push(elem);
451- };
452-
453- if self.is_border {
454- for (buf, loc) in zip(&self.buffers, self.locations) {
455- push(buf, loc);
456- }
457- } else {
458- push(&self.buffers[0], self.locations[0]);
459- }
460-
461- rv.into_iter()
462- }
463-}
464-
465-impl FocusRing {
466- fn new(config: config::FocusRing) -> Self {
467- Self {
468- buffers: Default::default(),
469- locations: Default::default(),
470- is_off: config.off,
471- is_border: false,
472- width: config.width.into(),
473- active_color: config.active_color,
474- inactive_color: config.inactive_color,
475- }
476- }
477-}
478-
479-impl WorkspaceSwitch {
480- fn current_idx(&self) -> f64 {
481- match self {
482- WorkspaceSwitch::Animation(anim) => anim.value(),
483- WorkspaceSwitch::Gesture(gesture) => gesture.current_idx,
484- }
485- }
486-
487- /// Returns `true` if the workspace switch is [`Animation`].
488- ///
489- /// [`Animation`]: WorkspaceSwitch::Animation
490- #[must_use]
491- fn is_animation(&self) -> bool {
492- matches!(self, Self::Animation(..))
493- }
494-}
495-
496-impl ColumnWidth {
497- fn resolve(self, options: &Options, view_width: i32) -> i32 {
498- match self {
499- ColumnWidth::Proportion(proportion) => {
500- ((view_width - options.gaps) as f64 * proportion).floor() as i32 - options.gaps
501- }
502- ColumnWidth::Preset(idx) => options.preset_widths[idx].resolve(options, view_width),
503- ColumnWidth::Fixed(width) => width,
504- }
505- }
506-}
507-
508 impl<W: LayoutElement> Layout<W> {199 impl<W: LayoutElement> Layout<W> {
509 pub fn new(config: &Config) -> Self {200 pub fn new(config: &Config) -> Self {
510 Self {201 Self {
@@ -1578,1754 +1269,6 @@ impl<W: LayoutElement> Default for MonitorSet<W> {
1578 }1269 }
1579 }1270 }
1580 1271
1581-impl<W: LayoutElement> Monitor<W> {
1582- fn new(output: Output, workspaces: Vec<Workspace<W>>, options: Rc<Options>) -> Self {
1583- Self {
1584- output,
1585- workspaces,
1586- active_workspace_idx: 0,
1587- workspace_switch: None,
1588- options,
1589- }
1590- }
1591-
1592- fn active_workspace(&mut self) -> &mut Workspace<W> {
1593- &mut self.workspaces[self.active_workspace_idx]
1594- }
1595-
1596- fn activate_workspace(&mut self, idx: usize) {
1597- if self.active_workspace_idx == idx {
1598- return;
1599- }
1600-
1601- let current_idx = self
1602- .workspace_switch
1603- .as_ref()
1604- .map(|s| s.current_idx())
1605- .unwrap_or(self.active_workspace_idx as f64);
1606-
1607- self.active_workspace_idx = idx;
1608-
1609- self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
1610- current_idx,
1611- idx as f64,
1612- Duration::from_millis(250),
1613- )));
1614- }
1615-
1616- pub fn add_window(
1617- &mut self,
1618- workspace_idx: usize,
1619- window: W,
1620- activate: bool,
1621- width: ColumnWidth,
1622- is_full_width: bool,
1623- ) {
1624- let workspace = &mut self.workspaces[workspace_idx];
1625-
1626- workspace.add_window(window.clone(), activate, width, is_full_width);
1627-
1628- // After adding a new window, workspace becomes this output's own.
1629- workspace.original_output = OutputId::new(&self.output);
1630-
1631- if workspace_idx == self.workspaces.len() - 1 {
1632- // Insert a new empty workspace.
1633- let ws = Workspace::new(self.output.clone(), self.options.clone());
1634- self.workspaces.push(ws);
1635- }
1636-
1637- if activate {
1638- self.activate_workspace(workspace_idx);
1639- }
1640- }
1641-
1642- fn clean_up_workspaces(&mut self) {
1643- assert!(self.workspace_switch.is_none());
1644-
1645- for idx in (0..self.workspaces.len() - 1).rev() {
1646- if self.active_workspace_idx == idx {
1647- continue;
1648- }
1649-
1650- if !self.workspaces[idx].has_windows() {
1651- self.workspaces.remove(idx);
1652- if self.active_workspace_idx > idx {
1653- self.active_workspace_idx -= 1;
1654- }
1655- }
1656- }
1657- }
1658-
1659- pub fn move_left(&mut self) {
1660- self.active_workspace().move_left();
1661- }
1662-
1663- pub fn move_right(&mut self) {
1664- self.active_workspace().move_right();
1665- }
1666-
1667- pub fn move_down(&mut self) {
1668- self.active_workspace().move_down();
1669- }
1670-
1671- pub fn move_up(&mut self) {
1672- self.active_workspace().move_up();
1673- }
1674-
1675- pub fn move_down_or_to_workspace_down(&mut self) {
1676- let workspace = self.active_workspace();
1677- if workspace.columns.is_empty() {
1678- return;
1679- }
1680- let column = &mut workspace.columns[workspace.active_column_idx];
1681- let curr_idx = column.active_window_idx;
1682- let new_idx = min(column.active_window_idx + 1, column.windows.len() - 1);
1683- if curr_idx == new_idx {
1684- self.move_to_workspace_down();
1685- } else {
1686- workspace.move_down();
1687- }
1688- }
1689-
1690- pub fn move_up_or_to_workspace_up(&mut self) {
1691- let workspace = self.active_workspace();
1692- if workspace.columns.is_empty() {
1693- return;
1694- }
1695- let curr_idx = workspace.columns[workspace.active_column_idx].active_window_idx;
1696- let new_idx = curr_idx.saturating_sub(1);
1697- if curr_idx == new_idx {
1698- self.move_to_workspace_up();
1699- } else {
1700- workspace.move_up();
1701- }
1702- }
1703-
1704- pub fn focus_left(&mut self) {
1705- self.active_workspace().focus_left();
1706- }
1707-
1708- pub fn focus_right(&mut self) {
1709- self.active_workspace().focus_right();
1710- }
1711-
1712- pub fn focus_down(&mut self) {
1713- self.active_workspace().focus_down();
1714- }
1715-
1716- pub fn focus_up(&mut self) {
1717- self.active_workspace().focus_up();
1718- }
1719-
1720- pub fn focus_window_or_workspace_down(&mut self) {
1721- let workspace = self.active_workspace();
1722- if workspace.columns.is_empty() {
1723- self.switch_workspace_down();
1724- } else {
1725- let column = &workspace.columns[workspace.active_column_idx];
1726- let curr_idx = column.active_window_idx;
1727- let new_idx = min(column.active_window_idx + 1, column.windows.len() - 1);
1728- if curr_idx == new_idx {
1729- self.switch_workspace_down();
1730- } else {
1731- workspace.focus_down();
1732- }
1733- }
1734- }
1735-
1736- pub fn focus_window_or_workspace_up(&mut self) {
1737- let workspace = self.active_workspace();
1738- if workspace.columns.is_empty() {
1739- self.switch_workspace_up();
1740- } else {
1741- let curr_idx = workspace.columns[workspace.active_column_idx].active_window_idx;
1742- let new_idx = curr_idx.saturating_sub(1);
1743- if curr_idx == new_idx {
1744- self.switch_workspace_up();
1745- } else {
1746- workspace.focus_up();
1747- }
1748- }
1749- }
1750-
1751- pub fn move_to_workspace_up(&mut self) {
1752- let source_workspace_idx = self.active_workspace_idx;
1753-
1754- let new_idx = source_workspace_idx.saturating_sub(1);
1755- if new_idx == source_workspace_idx {
1756- return;
1757- }
1758-
1759- let workspace = &mut self.workspaces[source_workspace_idx];
1760- if workspace.columns.is_empty() {
1761- return;
1762- }
1763-
1764- let column = &mut workspace.columns[workspace.active_column_idx];
1765- let width = column.width;
1766- let is_full_width = column.is_full_width;
1767- let window = column.windows[column.active_window_idx].clone();
1768- workspace.remove_window(&window);
1769-
1770- self.add_window(new_idx, window, true, width, is_full_width);
1771- }
1772-
1773- pub fn move_to_workspace_down(&mut self) {
1774- let source_workspace_idx = self.active_workspace_idx;
1775-
1776- let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1);
1777- if new_idx == source_workspace_idx {
1778- return;
1779- }
1780-
1781- let workspace = &mut self.workspaces[source_workspace_idx];
1782- if workspace.columns.is_empty() {
1783- return;
1784- }
1785-
1786- let column = &mut workspace.columns[workspace.active_column_idx];
1787- let width = column.width;
1788- let is_full_width = column.is_full_width;
1789- let window = column.windows[column.active_window_idx].clone();
1790- workspace.remove_window(&window);
1791-
1792- self.add_window(new_idx, window, true, width, is_full_width);
1793- }
1794-
1795- pub fn move_to_workspace(&mut self, idx: usize) {
1796- let source_workspace_idx = self.active_workspace_idx;
1797-
1798- let new_idx = min(idx, self.workspaces.len() - 1);
1799- if new_idx == source_workspace_idx {
1800- return;
1801- }
1802-
1803- let workspace = &mut self.workspaces[source_workspace_idx];
1804- if workspace.columns.is_empty() {
1805- return;
1806- }
1807-
1808- let column = &mut workspace.columns[workspace.active_column_idx];
1809- let width = column.width;
1810- let is_full_width = column.is_full_width;
1811- let window = column.windows[column.active_window_idx].clone();
1812- workspace.remove_window(&window);
1813-
1814- self.add_window(new_idx, window, true, width, is_full_width);
1815-
1816- // Don't animate this action.
1817- self.workspace_switch = None;
1818-
1819- self.clean_up_workspaces();
1820- }
1821-
1822- pub fn switch_workspace_up(&mut self) {
1823- self.activate_workspace(self.active_workspace_idx.saturating_sub(1));
1824- }
1825-
1826- pub fn switch_workspace_down(&mut self) {
1827- self.activate_workspace(min(
1828- self.active_workspace_idx + 1,
1829- self.workspaces.len() - 1,
1830- ));
1831- }
1832-
1833- pub fn switch_workspace(&mut self, idx: usize) {
1834- self.activate_workspace(min(idx, self.workspaces.len() - 1));
1835- // Don't animate this action.
1836- self.workspace_switch = None;
1837-
1838- self.clean_up_workspaces();
1839- }
1840-
1841- pub fn consume_into_column(&mut self) {
1842- self.active_workspace().consume_into_column();
1843- }
1844-
1845- pub fn expel_from_column(&mut self) {
1846- self.active_workspace().expel_from_column();
1847- }
1848-
1849- pub fn center_column(&mut self) {
1850- self.active_workspace().center_column();
1851- }
1852-
1853- pub fn focus(&self) -> Option<&W> {
1854- let workspace = &self.workspaces[self.active_workspace_idx];
1855- if !workspace.has_windows() {
1856- return None;
1857- }
1858-
1859- let column = &workspace.columns[workspace.active_column_idx];
1860- Some(&column.windows[column.active_window_idx])
1861- }
1862-
1863- pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
1864- if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch {
1865- anim.set_current_time(current_time);
1866- if anim.is_done() {
1867- self.workspace_switch = None;
1868- self.clean_up_workspaces();
1869- }
1870- }
1871-
1872- for ws in &mut self.workspaces {
1873- ws.advance_animations(current_time, is_active);
1874- }
1875- }
1876-
1877- pub fn are_animations_ongoing(&self) -> bool {
1878- self.workspace_switch
1879- .as_ref()
1880- .is_some_and(|s| s.is_animation())
1881- || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
1882- }
1883-
1884- pub fn are_transitions_ongoing(&self) -> bool {
1885- self.workspace_switch.is_some()
1886- || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
1887- }
1888-
1889- fn update_config(&mut self, options: Rc<Options>) {
1890- for ws in &mut self.workspaces {
1891- ws.update_config(options.clone());
1892- }
1893-
1894- if self.options.struts != options.struts {
1895- let view_size = output_size(&self.output);
1896- let working_area = compute_working_area(&self.output, options.struts);
1897-
1898- for ws in &mut self.workspaces {
1899- ws.set_view_size(view_size, working_area);
1900- }
1901- }
1902-
1903- self.options = options;
1904- }
1905-
1906- fn toggle_width(&mut self) {
1907- self.active_workspace().toggle_width();
1908- }
1909-
1910- fn toggle_full_width(&mut self) {
1911- self.active_workspace().toggle_full_width();
1912- }
1913-
1914- fn set_column_width(&mut self, change: SizeChange) {
1915- self.active_workspace().set_column_width(change);
1916- }
1917-
1918- fn set_window_height(&mut self, change: SizeChange) {
1919- self.active_workspace().set_window_height(change);
1920- }
1921-
1922- fn move_workspace_down(&mut self) {
1923- let new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1);
1924- if new_idx == self.active_workspace_idx {
1925- return;
1926- }
1927-
1928- self.workspaces.swap(self.active_workspace_idx, new_idx);
1929-
1930- if new_idx == self.workspaces.len() - 1 {
1931- // Insert a new empty workspace.
1932- let ws = Workspace::new(self.output.clone(), self.options.clone());
1933- self.workspaces.push(ws);
1934- }
1935-
1936- self.activate_workspace(new_idx);
1937- self.workspace_switch = None;
1938-
1939- self.clean_up_workspaces();
1940- }
1941-
1942- fn move_workspace_up(&mut self) {
1943- let new_idx = self.active_workspace_idx.saturating_sub(1);
1944- if new_idx == self.active_workspace_idx {
1945- return;
1946- }
1947-
1948- self.workspaces.swap(self.active_workspace_idx, new_idx);
1949-
1950- if self.active_workspace_idx == self.workspaces.len() - 1 {
1951- // Insert a new empty workspace.
1952- let ws = Workspace::new(self.output.clone(), self.options.clone());
1953- self.workspaces.push(ws);
1954- }
1955-
1956- self.activate_workspace(new_idx);
1957- self.workspace_switch = None;
1958-
1959- self.clean_up_workspaces();
1960- }
1961-
1962- pub fn window_under(
1963- &self,
1964- pos_within_output: Point<f64, Logical>,
1965- ) -> Option<(&W, Point<i32, Logical>)> {
1966- match &self.workspace_switch {
1967- Some(switch) => {
1968- let size = output_size(&self.output);
1969-
1970- let render_idx = switch.current_idx();
1971- let before_idx = render_idx.floor() as usize;
1972- let after_idx = render_idx.ceil() as usize;
1973-
1974- let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32;
1975-
1976- let (idx, ws_offset) = if pos_within_output.y < (size.h - offset) as f64 {
1977- (before_idx, Point::from((0, offset)))
1978- } else {
1979- (after_idx, Point::from((0, -size.h + offset)))
1980- };
1981-
1982- let ws = &self.workspaces[idx];
1983- let (win, win_pos) = ws.window_under(pos_within_output + ws_offset.to_f64())?;
1984- Some((win, win_pos - ws_offset))
1985- }
1986- None => {
1987- let ws = &self.workspaces[self.active_workspace_idx];
1988- ws.window_under(pos_within_output)
1989- }
1990- }
1991- }
1992-
1993- pub fn render_above_top_layer(&self) -> bool {
1994- // Render above the top layer only if the view is stationary.
1995- if self.workspace_switch.is_some() {
1996- return false;
1997- }
1998-
1999- let ws = &self.workspaces[self.active_workspace_idx];
2000- ws.render_above_top_layer()
2001- }
2002-}
2003-
2004-impl Monitor<Window> {
2005- pub fn render_elements(
2006- &self,
2007- renderer: &mut GlesRenderer,
2008- ) -> Vec<MonitorRenderElement<GlesRenderer>> {
2009- let _span = tracy_client::span!("Monitor::render_elements");
2010-
2011- let output_scale = Scale::from(self.output.current_scale().fractional_scale());
2012- let output_transform = self.output.current_transform();
2013- let output_mode = self.output.current_mode().unwrap();
2014- let size = output_transform.transform_size(output_mode.size);
2015-
2016- match &self.workspace_switch {
2017- Some(switch) => {
2018- let render_idx = switch.current_idx();
2019- let before_idx = render_idx.floor() as usize;
2020- let after_idx = render_idx.ceil() as usize;
2021-
2022- let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32;
2023-
2024- let before = self.workspaces[before_idx].render_elements(renderer);
2025- let after = self.workspaces[after_idx].render_elements(renderer);
2026-
2027- let before = before.into_iter().filter_map(|elem| {
2028- Some(RelocateRenderElement::from_element(
2029- CropRenderElement::from_element(
2030- elem,
2031- output_scale,
2032- Rectangle::from_extemities((0, offset), (size.w, size.h)),
2033- )?,
2034- (0, -offset),
2035- Relocate::Relative,
2036- ))
2037- });
2038- let after = after.into_iter().filter_map(|elem| {
2039- Some(RelocateRenderElement::from_element(
2040- CropRenderElement::from_element(
2041- elem,
2042- output_scale,
2043- Rectangle::from_extemities((0, 0), (size.w, offset)),
2044- )?,
2045- (0, -offset + size.h),
2046- Relocate::Relative,
2047- ))
2048- });
2049- before.chain(after).collect()
2050- }
2051- None => {
2052- let elements = self.workspaces[self.active_workspace_idx].render_elements(renderer);
2053- elements
2054- .into_iter()
2055- .filter_map(|elem| {
2056- Some(RelocateRenderElement::from_element(
2057- CropRenderElement::from_element(
2058- elem,
2059- output_scale,
2060- // HACK: set infinite crop bounds due to a damage tracking bug
2061- // which causes glitched rendering for maximized GTK windows.
2062- // FIXME: use proper bounds after fixing the Crop element.
2063- Rectangle::from_loc_and_size(
2064- (-i32::MAX / 2, -i32::MAX / 2),
2065- (i32::MAX, i32::MAX),
2066- ),
2067- // Rectangle::from_loc_and_size((0, 0), size),
2068- )?,
2069- (0, 0),
2070- Relocate::Relative,
2071- ))
2072- })
2073- .collect()
2074- }
2075- }
2076- }
2077-}
2078-
2079-impl<W: LayoutElement> Workspace<W> {
2080- fn new(output: Output, options: Rc<Options>) -> Self {
2081- let working_area = compute_working_area(&output, options.struts);
2082- Self {
2083- original_output: OutputId::new(&output),
2084- view_size: output_size(&output),
2085- working_area,
2086- output: Some(output),
2087- columns: vec![],
2088- active_column_idx: 0,
2089- focus_ring: FocusRing::new(options.focus_ring),
2090- view_offset: 0,
2091- view_offset_anim: None,
2092- activate_prev_column_on_removal: false,
2093- options,
2094- }
2095- }
2096-
2097- fn new_no_outputs(options: Rc<Options>) -> Self {
2098- Self {
2099- output: None,
2100- original_output: OutputId(String::new()),
2101- view_size: Size::from((1280, 720)),
2102- working_area: Rectangle::from_loc_and_size((0, 0), (1280, 720)),
2103- columns: vec![],
2104- active_column_idx: 0,
2105- focus_ring: FocusRing::new(options.focus_ring),
2106- view_offset: 0,
2107- view_offset_anim: None,
2108- activate_prev_column_on_removal: false,
2109- options,
2110- }
2111- }
2112-
2113- pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
2114- match &mut self.view_offset_anim {
2115- Some(anim) => {
2116- anim.set_current_time(current_time);
2117- self.view_offset = anim.value().round() as i32;
2118- if anim.is_done() {
2119- self.view_offset_anim = None;
2120- }
2121- }
2122- None => (),
2123- }
2124-
2125- let view_pos = self.view_pos();
2126-
2127- // This shall one day become a proper animation.
2128- if !self.columns.is_empty() {
2129- let col = &self.columns[self.active_column_idx];
2130- let active_win = &col.windows[col.active_window_idx];
2131- let geom = active_win.geometry();
2132- let has_ssd = active_win.has_ssd();
2133-
2134- let win_pos = Point::from((
2135- self.column_x(self.active_column_idx) - view_pos,
2136- col.window_y(col.active_window_idx),
2137- ));
2138-
2139- self.focus_ring.update(win_pos, geom.size, has_ssd);
2140- self.focus_ring.set_active(is_active);
2141- }
2142- }
2143-
2144- pub fn are_animations_ongoing(&self) -> bool {
2145- self.view_offset_anim.is_some()
2146- }
2147-
2148- fn update_config(&mut self, options: Rc<Options>) {
2149- let c = &options.focus_ring;
2150- self.focus_ring.is_off = c.off;
2151- self.focus_ring.width = c.width.into();
2152- self.focus_ring.active_color = c.active_color;
2153- self.focus_ring.inactive_color = c.inactive_color;
2154- // The focus ring buffer will be updated in a subsequent update_animations call.
2155-
2156- for column in &mut self.columns {
2157- column.update_config(options.clone());
2158- }
2159-
2160- self.options = options;
2161- }
2162-
2163- fn windows(&self) -> impl Iterator<Item = &W> + '_ {
2164- self.columns.iter().flat_map(|col| col.windows.iter())
2165- }
2166-
2167- fn set_output(&mut self, output: Option<Output>) {
2168- if self.output == output {
2169- return;
2170- }
2171-
2172- if let Some(output) = self.output.take() {
2173- for win in self.windows() {
2174- win.output_leave(&output);
2175- }
2176- }
2177-
2178- self.output = output;
2179-
2180- if let Some(output) = &self.output {
2181- let working_area = compute_working_area(output, self.options.struts);
2182- self.set_view_size(output_size(output), working_area);
2183-
2184- for win in self.windows() {
2185- self.enter_output_for_window(win);
2186- }
2187- }
2188- }
2189-
2190- fn enter_output_for_window(&self, window: &W) {
2191- if let Some(output) = &self.output {
2192- prepare_for_output(window, output);
2193-
2194- // FIXME: proper overlap.
2195- window.output_enter(
2196- output,
2197- Rectangle::from_loc_and_size((0, 0), (i32::MAX, i32::MAX)),
2198- );
2199- }
2200- }
2201-
2202- fn set_view_size(&mut self, size: Size<i32, Logical>, working_area: Rectangle<i32, Logical>) {
2203- if self.view_size == size && self.working_area == working_area {
2204- return;
2205- }
2206-
2207- self.view_size = size;
2208- self.working_area = working_area;
2209-
2210- for col in &mut self.columns {
2211- col.set_view_size(self.view_size, self.working_area);
2212- }
2213- }
2214-
2215- fn toplevel_bounds(&self) -> Size<i32, Logical> {
2216- Size::from((
2217- max(self.working_area.size.w - self.options.gaps * 2, 1),
2218- max(self.working_area.size.h - self.options.gaps * 2, 1),
2219- ))
2220- }
2221-
2222- pub fn configure_new_window(&self, window: &Window) {
2223- let width = if let Some(width) = self.options.default_width {
2224- max(1, width.resolve(&self.options, self.working_area.size.w))
2225- } else {
2226- 0
2227- };
2228-
2229- let height = self.working_area.size.h - self.options.gaps * 2;
2230- let size = Size::from((width, max(height, 1)));
2231-
2232- let bounds = self.toplevel_bounds();
2233-
2234- if let Some(output) = self.output.as_ref() {
2235- prepare_for_output(window, output);
2236- }
2237-
2238- window.toplevel().with_pending_state(|state| {
2239- state.size = Some(size);
2240- state.bounds = Some(bounds);
2241- });
2242- }
2243-
2244- fn compute_new_view_offset_for_column(&self, current_x: i32, idx: usize) -> i32 {
2245- if self.columns[idx].is_fullscreen {
2246- return 0;
2247- }
2248-
2249- let new_col_x = self.column_x(idx);
2250-
2251- let final_x = if let Some(anim) = &self.view_offset_anim {
2252- current_x - self.view_offset + anim.to().round() as i32
2253- } else {
2254- current_x
2255- };
2256-
2257- let new_offset = compute_new_view_offset(
2258- final_x + self.working_area.loc.x,
2259- self.working_area.size.w,
2260- new_col_x,
2261- self.columns[idx].width(),
2262- self.options.gaps,
2263- );
2264-
2265- // Non-fullscreen windows are always offset at least by the working area position.
2266- new_offset - self.working_area.loc.x
2267- }
2268-
2269- fn animate_view_offset_to_column(&mut self, current_x: i32, idx: usize) {
2270- let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx);
2271-
2272- let new_col_x = self.column_x(idx);
2273- let from_view_offset = current_x - new_col_x;
2274- self.view_offset = from_view_offset;
2275-
2276- // If we're already animating towards that, don't restart it.
2277- if let Some(anim) = &self.view_offset_anim {
2278- if anim.value().round() as i32 == self.view_offset
2279- && anim.to().round() as i32 == new_view_offset
2280- {
2281- return;
2282- }
2283- }
2284-
2285- // If our view offset is already this, we don't need to do anything.
2286- if self.view_offset == new_view_offset {
2287- self.view_offset_anim = None;
2288- return;
2289- }
2290-
2291- self.view_offset_anim = Some(Animation::new(
2292- self.view_offset as f64,
2293- new_view_offset as f64,
2294- Duration::from_millis(250),
2295- ));
2296- }
2297-
2298- fn activate_column(&mut self, idx: usize) {
2299- if self.active_column_idx == idx {
2300- return;
2301- }
2302-
2303- let current_x = self.view_pos();
2304- self.animate_view_offset_to_column(current_x, idx);
2305-
2306- self.active_column_idx = idx;
2307-
2308- // A different column was activated; reset the flag.
2309- self.activate_prev_column_on_removal = false;
2310- }
2311-
2312- fn has_windows(&self) -> bool {
2313- self.windows().next().is_some()
2314- }
2315-
2316- fn has_window(&self, window: &W) -> bool {
2317- self.windows().any(|win| win == window)
2318- }
2319-
2320- fn find_wl_surface(&self, wl_surface: &WlSurface) -> Option<&W> {
2321- self.windows().find(|win| win.is_wl_surface(wl_surface))
2322- }
2323-
2324- /// Computes the X position of the windows in the given column, in logical coordinates.
2325- fn column_x(&self, column_idx: usize) -> i32 {
2326- let mut x = 0;
2327-
2328- for column in self.columns.iter().take(column_idx) {
2329- x += column.width() + self.options.gaps;
2330- }
2331-
2332- x
2333- }
2334-
2335- fn add_window(&mut self, window: W, activate: bool, width: ColumnWidth, is_full_width: bool) {
2336- self.enter_output_for_window(&window);
2337-
2338- let was_empty = self.columns.is_empty();
2339-
2340- let idx = if self.columns.is_empty() {
2341- 0
2342- } else {
2343- self.active_column_idx + 1
2344- };
2345-
2346- let column = Column::new(
2347- window,
2348- self.view_size,
2349- self.working_area,
2350- self.options.clone(),
2351- width,
2352- is_full_width,
2353- );
2354- self.columns.insert(idx, column);
2355-
2356- if activate {
2357- // If this is the first window on an empty workspace, skip the animation from whatever
2358- // view_offset was left over.
2359- if was_empty {
2360- // Try to make the code produce a left-aligned offset, even in presence of left
2361- // exclusive zones.
2362- self.view_offset = self.compute_new_view_offset_for_column(self.column_x(0), 0);
2363- self.view_offset_anim = None;
2364- }
2365-
2366- self.activate_column(idx);
2367- self.activate_prev_column_on_removal = true;
2368- }
2369- }
2370-
2371- fn remove_window(&mut self, window: &W) {
2372- if let Some(output) = &self.output {
2373- window.output_leave(output);
2374- }
2375-
2376- let column_idx = self
2377- .columns
2378- .iter()
2379- .position(|col| col.contains(window))
2380- .unwrap();
2381- let column = &mut self.columns[column_idx];
2382-
2383- let window_idx = column.windows.iter().position(|win| win == window).unwrap();
2384- column.windows.remove(window_idx);
2385- column.heights.remove(window_idx);
2386- if column.windows.is_empty() {
2387- if column_idx + 1 == self.active_column_idx {
2388- // The previous column, that we were going to activate upon removal of the active
2389- // column, has just been itself removed.
2390- self.activate_prev_column_on_removal = false;
2391- }
2392-
2393- // FIXME: activate_column below computes current view position to compute the new view
2394- // position, which can include the column we're removing here. This leads to unwanted
2395- // view jumps.
2396- self.columns.remove(column_idx);
2397- if self.columns.is_empty() {
2398- return;
2399- }
2400-
2401- if self.active_column_idx > column_idx
2402- || (self.active_column_idx == column_idx && self.activate_prev_column_on_removal)
2403- {
2404- // A column to the left was removed; preserve the current position.
2405- // FIXME: preserve activate_prev_column_on_removal.
2406- // Or, the active column was removed, and we needed to activate the previous column.
2407- self.activate_column(self.active_column_idx.saturating_sub(1));
2408- } else {
2409- self.activate_column(min(self.active_column_idx, self.columns.len() - 1));
2410- }
2411-
2412- return;
2413- }
2414-
2415- column.active_window_idx = min(column.active_window_idx, column.windows.len() - 1);
2416- column.update_window_sizes();
2417- }
2418-
2419- fn update_window(&mut self, window: &W) {
2420- let (idx, column) = self
2421- .columns
2422- .iter_mut()
2423- .enumerate()
2424- .find(|(_, col)| col.contains(window))
2425- .unwrap();
2426- column.update_window_sizes();
2427-
2428- if idx == self.active_column_idx {
2429- // We might need to move the view to ensure the resized window is still visible.
2430- let current_x = self.view_pos();
2431- self.animate_view_offset_to_column(current_x, idx);
2432- }
2433- }
2434-
2435- fn activate_window(&mut self, window: &W) {
2436- let column_idx = self
2437- .columns
2438- .iter()
2439- .position(|col| col.contains(window))
2440- .unwrap();
2441- let column = &mut self.columns[column_idx];
2442-
2443- column.activate_window(window);
2444- self.activate_column(column_idx);
2445- }
2446-
2447- #[cfg(test)]
2448- fn verify_invariants(&self) {
2449- assert!(self.view_size.w > 0);
2450- assert!(self.view_size.h > 0);
2451-
2452- if !self.columns.is_empty() {
2453- assert!(self.active_column_idx < self.columns.len());
2454-
2455- for column in &self.columns {
2456- column.verify_invariants();
2457- }
2458- }
2459- }
2460-
2461- fn focus_left(&mut self) {
2462- self.activate_column(self.active_column_idx.saturating_sub(1));
2463- }
2464-
2465- fn focus_right(&mut self) {
2466- if self.columns.is_empty() {
2467- return;
2468- }
2469-
2470- self.activate_column(min(self.active_column_idx + 1, self.columns.len() - 1));
2471- }
2472-
2473- fn focus_down(&mut self) {
2474- if self.columns.is_empty() {
2475- return;
2476- }
2477-
2478- self.columns[self.active_column_idx].focus_down();
2479- }
2480-
2481- fn focus_up(&mut self) {
2482- if self.columns.is_empty() {
2483- return;
2484- }
2485-
2486- self.columns[self.active_column_idx].focus_up();
2487- }
2488-
2489- fn move_left(&mut self) {
2490- let new_idx = self.active_column_idx.saturating_sub(1);
2491- if self.active_column_idx == new_idx {
2492- return;
2493- }
2494-
2495- let current_x = self.view_pos();
2496-
2497- self.columns.swap(self.active_column_idx, new_idx);
2498-
2499- self.view_offset =
2500- self.compute_new_view_offset_for_column(current_x, self.active_column_idx);
2501-
2502- self.activate_column(new_idx);
2503- }
2504-
2505- fn move_right(&mut self) {
2506- if self.columns.is_empty() {
2507- return;
2508- }
2509-
2510- let new_idx = min(self.active_column_idx + 1, self.columns.len() - 1);
2511- if self.active_column_idx == new_idx {
2512- return;
2513- }
2514-
2515- let current_x = self.view_pos();
2516-
2517- self.columns.swap(self.active_column_idx, new_idx);
2518-
2519- self.view_offset =
2520- self.compute_new_view_offset_for_column(current_x, self.active_column_idx);
2521-
2522- self.activate_column(new_idx);
2523- }
2524-
2525- fn move_down(&mut self) {
2526- if self.columns.is_empty() {
2527- return;
2528- }
2529-
2530- self.columns[self.active_column_idx].move_down();
2531- }
2532-
2533- fn move_up(&mut self) {
2534- if self.columns.is_empty() {
2535- return;
2536- }
2537-
2538- self.columns[self.active_column_idx].move_up();
2539- }
2540-
2541- fn consume_into_column(&mut self) {
2542- if self.columns.len() < 2 {
2543- return;
2544- }
2545-
2546- if self.active_column_idx == self.columns.len() - 1 {
2547- return;
2548- }
2549-
2550- let source_column_idx = self.active_column_idx + 1;
2551-
2552- let source_column = &mut self.columns[source_column_idx];
2553- let window = source_column.windows[0].clone();
2554- self.remove_window(&window);
2555-
2556- let target_column = &mut self.columns[self.active_column_idx];
2557- target_column.add_window(window);
2558- }
2559-
2560- fn expel_from_column(&mut self) {
2561- if self.columns.is_empty() {
2562- return;
2563- }
2564-
2565- let source_column = &mut self.columns[self.active_column_idx];
2566- if source_column.windows.len() == 1 {
2567- return;
2568- }
2569-
2570- let width = source_column.width;
2571- let is_full_width = source_column.is_full_width;
2572- let window = source_column.windows[source_column.active_window_idx].clone();
2573- self.remove_window(&window);
2574-
2575- self.add_window(window, true, width, is_full_width);
2576- }
2577-
2578- fn center_column(&mut self) {
2579- if self.columns.is_empty() {
2580- return;
2581- }
2582-
2583- let col = &self.columns[self.active_column_idx];
2584- if col.is_fullscreen {
2585- return;
2586- }
2587-
2588- let width = col.width();
2589-
2590- // If the column is wider than the working area, then on commit it will be shifted to left
2591- // edge alignment by the usual positioning code, so there's no use in doing anything here.
2592- if self.working_area.size.w <= width {
2593- return;
2594- }
2595-
2596- let new_view_offset = -(self.working_area.size.w - width) / 2 - self.working_area.loc.x;
2597-
2598- // If we're already animating towards that, don't restart it.
2599- if let Some(anim) = &self.view_offset_anim {
2600- if anim.to().round() as i32 == new_view_offset {
2601- return;
2602- }
2603- }
2604-
2605- // If our view offset is already this, we don't need to do anything.
2606- if self.view_offset == new_view_offset {
2607- return;
2608- }
2609-
2610- self.view_offset_anim = Some(Animation::new(
2611- self.view_offset as f64,
2612- new_view_offset as f64,
2613- Duration::from_millis(250),
2614- ));
2615- }
2616-
2617- fn view_pos(&self) -> i32 {
2618- self.column_x(self.active_column_idx) + self.view_offset
2619- }
2620-
2621- fn window_under(&self, pos: Point<f64, Logical>) -> Option<(&W, Point<i32, Logical>)> {
2622- if self.columns.is_empty() {
2623- return None;
2624- }
2625-
2626- let view_pos = self.view_pos();
2627-
2628- // Prefer the active window since it's drawn on top.
2629- let col = &self.columns[self.active_column_idx];
2630- let active_win = &col.windows[col.active_window_idx];
2631- let geom = active_win.geometry();
2632- let buf_pos = Point::from((
2633- self.column_x(self.active_column_idx) - view_pos,
2634- col.window_y(col.active_window_idx),
2635- )) - geom.loc;
2636- if active_win.is_in_input_region(&(pos - buf_pos.to_f64())) {
2637- return Some((active_win, buf_pos));
2638- }
2639-
2640- let mut x = -view_pos;
2641- for col in &self.columns {
2642- for (win, y) in zip(&col.windows, col.window_ys()) {
2643- if win == active_win {
2644- // Already handled it above.
2645- continue;
2646- }
2647-
2648- let geom = win.geometry();
2649- let buf_pos = Point::from((x, y)) - geom.loc;
2650- if win.is_in_input_region(&(pos - buf_pos.to_f64())) {
2651- return Some((win, buf_pos));
2652- }
2653- }
2654-
2655- x += col.width() + self.options.gaps;
2656- }
2657-
2658- None
2659- }
2660-
2661- fn toggle_width(&mut self) {
2662- if self.columns.is_empty() {
2663- return;
2664- }
2665-
2666- self.columns[self.active_column_idx].toggle_width();
2667- }
2668-
2669- fn toggle_full_width(&mut self) {
2670- if self.columns.is_empty() {
2671- return;
2672- }
2673-
2674- self.columns[self.active_column_idx].toggle_full_width();
2675- }
2676-
2677- fn set_column_width(&mut self, change: SizeChange) {
2678- if self.columns.is_empty() {
2679- return;
2680- }
2681-
2682- self.columns[self.active_column_idx].set_column_width(change);
2683- }
2684-
2685- fn set_window_height(&mut self, change: SizeChange) {
2686- if self.columns.is_empty() {
2687- return;
2688- }
2689-
2690- self.columns[self.active_column_idx].set_window_height(change);
2691- }
2692-
2693- pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) {
2694- let (mut col_idx, win_idx) = self
2695- .columns
2696- .iter()
2697- .enumerate()
2698- .find_map(|(col_idx, col)| {
2699- col.windows
2700- .iter()
2701- .position(|w| w == window)
2702- .map(|win_idx| (col_idx, win_idx))
2703- })
2704- .unwrap();
2705-
2706- let mut col = &mut self.columns[col_idx];
2707-
2708- if is_fullscreen && col.windows.len() > 1 {
2709- // This wasn't the only window in its column; extract it into a separate column.
2710- let target_window_was_focused =
2711- self.active_column_idx == col_idx && col.active_window_idx == win_idx;
2712- let window = col.windows.remove(win_idx);
2713- col.heights.remove(win_idx);
2714- col.active_window_idx = min(col.active_window_idx, col.windows.len() - 1);
2715- col.update_window_sizes();
2716- let width = col.width;
2717- let is_full_width = col.is_full_width;
2718-
2719- col_idx += 1;
2720- self.columns.insert(
2721- col_idx,
2722- Column::new(
2723- window,
2724- self.view_size,
2725- self.working_area,
2726- self.options.clone(),
2727- width,
2728- is_full_width,
2729- ),
2730- );
2731- if self.active_column_idx >= col_idx || target_window_was_focused {
2732- self.active_column_idx += 1;
2733- }
2734- col = &mut self.columns[col_idx];
2735- }
2736-
2737- col.set_fullscreen(is_fullscreen);
2738- }
2739-
2740- pub fn toggle_fullscreen(&mut self, window: &W) {
2741- let col = self
2742- .columns
2743- .iter_mut()
2744- .find(|col| col.windows.contains(window))
2745- .unwrap();
2746- let value = !col.is_fullscreen;
2747- self.set_fullscreen(window, value);
2748- }
2749-
2750- pub fn render_above_top_layer(&self) -> bool {
2751- // Render above the top layer if we're on a fullscreen window and the view is stationary.
2752- if self.columns.is_empty() {
2753- return false;
2754- }
2755-
2756- if self.view_offset_anim.is_some() {
2757- return false;
2758- }
2759-
2760- self.columns[self.active_column_idx].is_fullscreen
2761- }
2762-}
2763-
2764-impl Workspace<Window> {
2765- fn refresh(&self) {
2766- let bounds = self.toplevel_bounds();
2767-
2768- for (col_idx, col) in self.columns.iter().enumerate() {
2769- for (win_idx, win) in col.windows.iter().enumerate() {
2770- let active = self.active_column_idx == col_idx && col.active_window_idx == win_idx;
2771- win.set_activated(active);
2772-
2773- win.toplevel().with_pending_state(|state| {
2774- state.bounds = Some(bounds);
2775- });
2776-
2777- win.toplevel().send_pending_configure();
2778- win.refresh();
2779- }
2780- }
2781- }
2782-
2783- pub fn render_elements(
2784- &self,
2785- renderer: &mut GlesRenderer,
2786- ) -> Vec<WorkspaceRenderElement<GlesRenderer>> {
2787- if self.columns.is_empty() {
2788- return vec![];
2789- }
2790-
2791- // FIXME: workspaces should probably cache their last used scale so they can be correctly
2792- // rendered even with no outputs connected.
2793- let output_scale = self
2794- .output
2795- .as_ref()
2796- .map(|o| Scale::from(o.current_scale().fractional_scale()))
2797- .unwrap_or(Scale::from(1.));
2798-
2799- let mut rv = vec![];
2800- let view_pos = self.view_pos();
2801-
2802- // Draw the active window on top.
2803- let col = &self.columns[self.active_column_idx];
2804- let active_win = &col.windows[col.active_window_idx];
2805- let win_pos = Point::from((
2806- self.column_x(self.active_column_idx) - view_pos,
2807- col.window_y(col.active_window_idx),
2808- ));
2809-
2810- // Draw the window itself.
2811- let geom = active_win.geometry();
2812- let buf_pos = win_pos - geom.loc;
2813- rv.extend(active_win.render_elements(
2814- renderer,
2815- buf_pos.to_physical_precise_round(output_scale),
2816- output_scale,
2817- 1.,
2818- ));
2819-
2820- // Draw the focus ring.
2821- rv.extend(self.focus_ring.render(output_scale).map(Into::into));
2822-
2823- let mut x = -view_pos;
2824- for col in &self.columns {
2825- for (win, y) in zip(&col.windows, col.window_ys()) {
2826- if win == active_win {
2827- // Already handled it above.
2828- continue;
2829- }
2830-
2831- let geom = win.geometry();
2832- let buf_pos = Point::from((x, y)) - geom.loc;
2833- rv.extend(win.render_elements(
2834- renderer,
2835- buf_pos.to_physical_precise_round(output_scale),
2836- output_scale,
2837- 1.,
2838- ));
2839- }
2840-
2841- x += col.width() + self.options.gaps;
2842- }
2843-
2844- rv
2845- }
2846-}
2847-
2848-impl<W: LayoutElement> Column<W> {
2849- fn new(
2850- window: W,
2851- view_size: Size<i32, Logical>,
2852- working_area: Rectangle<i32, Logical>,
2853- options: Rc<Options>,
2854- width: ColumnWidth,
2855- is_full_width: bool,
2856- ) -> Self {
2857- let mut rv = Self {
2858- windows: vec![],
2859- heights: vec![],
2860- active_window_idx: 0,
2861- width,
2862- is_full_width,
2863- is_fullscreen: false,
2864- view_size,
2865- working_area,
2866- options,
2867- };
2868-
2869- rv.add_window(window);
2870-
2871- rv
2872- }
2873-
2874- fn set_view_size(&mut self, size: Size<i32, Logical>, working_area: Rectangle<i32, Logical>) {
2875- if self.view_size == size && self.working_area == working_area {
2876- return;
2877- }
2878-
2879- self.view_size = size;
2880- self.working_area = working_area;
2881-
2882- self.update_window_sizes();
2883- }
2884-
2885- fn update_config(&mut self, options: Rc<Options>) {
2886- let mut update_sizes = false;
2887-
2888- // If preset widths changed, make our width non-preset.
2889- if self.options.preset_widths != options.preset_widths {
2890- if let ColumnWidth::Preset(idx) = self.width {
2891- self.width = self.options.preset_widths[idx];
2892- }
2893- }
2894-
2895- if self.options.gaps != options.gaps {
2896- update_sizes = true;
2897- }
2898-
2899- self.options = options;
2900-
2901- if update_sizes {
2902- self.update_window_sizes();
2903- }
2904- }
2905-
2906- fn set_width(&mut self, width: ColumnWidth) {
2907- self.width = width;
2908- self.is_full_width = false;
2909- self.update_window_sizes();
2910- }
2911-
2912- fn contains(&self, window: &W) -> bool {
2913- self.windows.iter().any(|win| win == window)
2914- }
2915-
2916- fn activate_window(&mut self, window: &W) {
2917- let idx = self.windows.iter().position(|win| win == window).unwrap();
2918- self.active_window_idx = idx;
2919- }
2920-
2921- fn add_window(&mut self, window: W) {
2922- self.is_fullscreen = false;
2923- self.windows.push(window);
2924- self.heights.push(WindowHeight::Auto);
2925- self.update_window_sizes();
2926- }
2927-
2928- fn update_window_sizes(&mut self) {
2929- if self.is_fullscreen {
2930- self.windows[0].request_fullscreen(self.view_size);
2931- return;
2932- }
2933-
2934- let min_size: Vec<_> = self.windows.iter().map(LayoutElement::min_size).collect();
2935- let max_size: Vec<_> = self.windows.iter().map(LayoutElement::max_size).collect();
2936-
2937- // Compute the column width.
2938- let min_width = min_size
2939- .iter()
2940- .filter_map(|size| {
2941- let w = size.w;
2942- if w == 0 {
2943- None
2944- } else {
2945- Some(w)
2946- }
2947- })
2948- .max()
2949- .unwrap_or(1);
2950- let max_width = max_size
2951- .iter()
2952- .filter_map(|size| {
2953- let w = size.w;
2954- if w == 0 {
2955- None
2956- } else {
2957- Some(w)
2958- }
2959- })
2960- .min()
2961- .unwrap_or(i32::MAX);
2962- let max_width = max(max_width, min_width);
2963-
2964- let width = if self.is_full_width {
2965- ColumnWidth::Proportion(1.)
2966- } else {
2967- self.width
2968- };
2969-
2970- let width = width.resolve(&self.options, self.working_area.size.w);
2971- let width = max(min(width, max_width), min_width);
2972-
2973- // Compute the window heights.
2974- let mut heights = self.heights.clone();
2975- let mut height_left = self.working_area.size.h - self.options.gaps;
2976- let mut auto_windows_left = self.windows.len();
2977-
2978- // Subtract all fixed-height windows.
2979- for (h, (min_size, max_size)) in zip(&mut heights, zip(&min_size, &max_size)) {
2980- // Check if the window has an exact height constraint.
2981- if min_size.h > 0 && min_size.h == max_size.h {
2982- *h = WindowHeight::Fixed(min_size.h);
2983- }
2984-
2985- if let WindowHeight::Fixed(h) = h {
2986- if max_size.h > 0 {
2987- *h = min(*h, max_size.h);
2988- }
2989- if min_size.h > 0 {
2990- *h = max(*h, min_size.h);
2991- }
2992- *h = max(*h, 1);
2993-
2994- height_left -= *h + self.options.gaps;
2995- auto_windows_left -= 1;
2996- }
2997- }
2998-
2999- // Iteratively try to distribute the remaining height, checking against window min heights.
3000- // Pick an auto height according to the current sizes, then check if it satisfies all
3001- // remaining min heights. If not, allocate fixed height to those windows and repeat the
3002- // loop. On each iteration the auto height will get smaller.
3003- //
3004- // NOTE: we do not respect max height here. Doing so would complicate things: if the current
3005- // auto height is above some window's max height, then the auto height can become larger.
3006- // Combining this with the min height loop is where the complexity appears.
3007- //
3008- // However, most max height uses are for fixed-size dialogs, where min height == max_height.
3009- // This case is separately handled above.
3010- while auto_windows_left > 0 {
3011- // Compute the current auto height.
3012- let auto_height = height_left / auto_windows_left as i32 - self.options.gaps;
3013- let auto_height = max(auto_height, 1);
3014-
3015- // Integer division above can result in imperfect height distribution. We will make some
3016- // windows 1 px taller to account for this.
3017- let mut ones_left = height_left
3018- .saturating_sub((auto_height + self.options.gaps) * auto_windows_left as i32);
3019-
3020- let mut unsatisfied_min = false;
3021- let mut ones_left_2 = ones_left;
3022- for (h, min_size) in zip(&mut heights, &min_size) {
3023- if matches!(h, WindowHeight::Fixed(_)) {
3024- continue;
3025- }
3026-
3027- let mut auto = auto_height;
3028- if ones_left_2 > 0 {
3029- auto += 1;
3030- ones_left_2 -= 1;
3031- }
3032-
3033- // Check if the auto height satisfies the min height.
3034- if min_size.h > 0 && min_size.h > auto {
3035- *h = WindowHeight::Fixed(min_size.h);
3036- height_left -= min_size.h + self.options.gaps;
3037- auto_windows_left -= 1;
3038- unsatisfied_min = true;
3039- }
3040- }
3041-
3042- // If some min height was unsatisfied, then we allocated the window more than the auto
3043- // height, which means that the remaining auto windows now have less height to work
3044- // with, and the loop must run again.
3045- if unsatisfied_min {
3046- continue;
3047- }
3048-
3049- // All min heights were satisfied, fill them in.
3050- for h in &mut heights {
3051- if matches!(h, WindowHeight::Fixed(_)) {
3052- continue;
3053- }
3054-
3055- let mut auto = auto_height;
3056- if ones_left > 0 {
3057- auto += 1;
3058- ones_left -= 1;
3059- }
3060-
3061- *h = WindowHeight::Fixed(auto);
3062- auto_windows_left -= 1;
3063- }
3064-
3065- assert_eq!(auto_windows_left, 0);
3066- }
3067-
3068- for (win, h) in zip(&self.windows, heights) {
3069- let WindowHeight::Fixed(height) = h else {
3070- unreachable!()
3071- };
3072-
3073- let size = Size::from((width, height));
3074- win.request_size(size);
3075- }
3076- }
3077-
3078- fn width(&self) -> i32 {
3079- self.windows
3080- .iter()
3081- .map(|win| win.geometry().size.w)
3082- .max()
3083- .unwrap()
3084- }
3085-
3086- fn focus_up(&mut self) {
3087- self.active_window_idx = self.active_window_idx.saturating_sub(1);
3088- }
3089-
3090- fn focus_down(&mut self) {
3091- self.active_window_idx = min(self.active_window_idx + 1, self.windows.len() - 1);
3092- }
3093-
3094- fn move_up(&mut self) {
3095- let new_idx = self.active_window_idx.saturating_sub(1);
3096- if self.active_window_idx == new_idx {
3097- return;
3098- }
3099-
3100- self.windows.swap(self.active_window_idx, new_idx);
3101- self.heights.swap(self.active_window_idx, new_idx);
3102- self.active_window_idx = new_idx;
3103- }
3104-
3105- fn move_down(&mut self) {
3106- let new_idx = min(self.active_window_idx + 1, self.windows.len() - 1);
3107- if self.active_window_idx == new_idx {
3108- return;
3109- }
3110-
3111- self.windows.swap(self.active_window_idx, new_idx);
3112- self.heights.swap(self.active_window_idx, new_idx);
3113- self.active_window_idx = new_idx;
3114- }
3115-
3116- #[cfg(test)]
3117- fn verify_invariants(&self) {
3118- assert!(!self.windows.is_empty(), "columns can't be empty");
3119- assert!(self.active_window_idx < self.windows.len());
3120- assert_eq!(self.windows.len(), self.heights.len());
3121-
3122- if self.is_fullscreen {
3123- assert_eq!(self.windows.len(), 1);
3124- }
3125- }
3126-
3127- fn toggle_width(&mut self) {
3128- let width = if self.is_full_width {
3129- ColumnWidth::Proportion(1.)
3130- } else {
3131- self.width
3132- };
3133-
3134- let idx = match width {
3135- ColumnWidth::Preset(idx) => (idx + 1) % self.options.preset_widths.len(),
3136- _ => {
3137- let current = self.width();
3138- self.options
3139- .preset_widths
3140- .iter()
3141- .position(|prop| {
3142- prop.resolve(&self.options, self.working_area.size.w) > current
3143- })
3144- .unwrap_or(0)
3145- }
3146- };
3147- let width = ColumnWidth::Preset(idx);
3148- self.set_width(width);
3149- }
3150-
3151- fn toggle_full_width(&mut self) {
3152- self.is_full_width = !self.is_full_width;
3153- self.update_window_sizes();
3154- }
3155-
3156- fn set_column_width(&mut self, change: SizeChange) {
3157- let width = if self.is_full_width {
3158- ColumnWidth::Proportion(1.)
3159- } else {
3160- self.width
3161- };
3162-
3163- let current_px = width.resolve(&self.options, self.working_area.size.w);
3164-
3165- let current = match width {
3166- ColumnWidth::Preset(idx) => self.options.preset_widths[idx],
3167- current => current,
3168- };
3169-
3170- // FIXME: fix overflows then remove limits.
3171- const MAX_PX: i32 = 100000;
3172- const MAX_F: f64 = 10000.;
3173-
3174- let width = match (current, change) {
3175- (_, SizeChange::SetFixed(fixed)) => ColumnWidth::Fixed(fixed.clamp(1, MAX_PX)),
3176- (_, SizeChange::SetProportion(proportion)) => {
3177- ColumnWidth::Proportion((proportion / 100.).clamp(0., MAX_F))
3178- }
3179- (_, SizeChange::AdjustFixed(delta)) => {
3180- let width = current_px.saturating_add(delta).clamp(1, MAX_PX);
3181- ColumnWidth::Fixed(width)
3182- }
3183- (ColumnWidth::Proportion(current), SizeChange::AdjustProportion(delta)) => {
3184- let proportion = (current + delta / 100.).clamp(0., MAX_F);
3185- ColumnWidth::Proportion(proportion)
3186- }
3187- (ColumnWidth::Fixed(_), SizeChange::AdjustProportion(delta)) => {
3188- let current = (current_px + self.options.gaps) as f64
3189- / (self.working_area.size.w - self.options.gaps) as f64;
3190- let proportion = (current + delta / 100.).clamp(0., MAX_F);
3191- ColumnWidth::Proportion(proportion)
3192- }
3193- (ColumnWidth::Preset(_), _) => unreachable!(),
3194- };
3195-
3196- self.set_width(width);
3197- }
3198-
3199- fn set_window_height(&mut self, change: SizeChange) {
3200- let current = self.heights[self.active_window_idx];
3201- let current_px = match current {
3202- WindowHeight::Auto => self.windows[self.active_window_idx].geometry().size.h,
3203- WindowHeight::Fixed(height) => height,
3204- };
3205- let current_prop = (current_px + self.options.gaps) as f64
3206- / (self.working_area.size.h - self.options.gaps) as f64;
3207-
3208- // FIXME: fix overflows then remove limits.
src/layout/monitor.rsadded+565 -0
@@ -0,0 +1,565 @@
1+use std::cmp::min;
2+use std::rc::Rc;
3+use std::time::Duration;
4+
5+use smithay::backend::renderer::element::utils::{
6+ CropRenderElement, Relocate, RelocateRenderElement,
7+};
8+use smithay::backend::renderer::gles::GlesRenderer;
9+use smithay::desktop::Window;
10+use smithay::output::Output;
11+use smithay::utils::{Logical, Point, Rectangle, Scale};
12+
13+use super::workspace::{
14+ compute_working_area, ColumnWidth, OutputId, Workspace, WorkspaceRenderElement,
15+};
16+use super::{LayoutElement, Options};
17+use crate::animation::Animation;
18+use crate::config::SizeChange;
19+use crate::utils::output_size;
20+
21+#[derive(Debug)]
22+pub struct Monitor<W: LayoutElement> {
23+ /// Output for this monitor.
24+ pub output: Output,
25+ // Must always contain at least one.
26+ pub workspaces: Vec<Workspace<W>>,
27+ /// Index of the currently active workspace.
28+ pub active_workspace_idx: usize,
29+ /// In-progress switch between workspaces.
30+ pub workspace_switch: Option<WorkspaceSwitch>,
31+ /// Configurable properties of the layout.
32+ pub options: Rc<Options>,
33+}
34+
35+#[derive(Debug)]
36+pub enum WorkspaceSwitch {
37+ Animation(Animation),
38+ Gesture(WorkspaceSwitchGesture),
39+}
40+
41+#[derive(Debug)]
42+pub struct WorkspaceSwitchGesture {
43+ /// Index of the workspace where the gesture was started.
44+ pub center_idx: usize,
45+ /// Current, fractional workspace index.
46+ pub current_idx: f64,
47+}
48+
49+pub type MonitorRenderElement<R> =
50+ RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>;
51+
52+impl WorkspaceSwitch {
53+ pub fn current_idx(&self) -> f64 {
54+ match self {
55+ WorkspaceSwitch::Animation(anim) => anim.value(),
56+ WorkspaceSwitch::Gesture(gesture) => gesture.current_idx,
57+ }
58+ }
59+
60+ /// Returns `true` if the workspace switch is [`Animation`].
61+ ///
62+ /// [`Animation`]: WorkspaceSwitch::Animation
63+ #[must_use]
64+ fn is_animation(&self) -> bool {
65+ matches!(self, Self::Animation(..))
66+ }
67+}
68+
69+impl<W: LayoutElement> Monitor<W> {
70+ pub fn new(output: Output, workspaces: Vec<Workspace<W>>, options: Rc<Options>) -> Self {
71+ Self {
72+ output,
73+ workspaces,
74+ active_workspace_idx: 0,
75+ workspace_switch: None,
76+ options,
77+ }
78+ }
79+
80+ pub fn active_workspace(&mut self) -> &mut Workspace<W> {
81+ &mut self.workspaces[self.active_workspace_idx]
82+ }
83+
84+ fn activate_workspace(&mut self, idx: usize) {
85+ if self.active_workspace_idx == idx {
86+ return;
87+ }
88+
89+ let current_idx = self
90+ .workspace_switch
91+ .as_ref()
92+ .map(|s| s.current_idx())
93+ .unwrap_or(self.active_workspace_idx as f64);
94+
95+ self.active_workspace_idx = idx;
96+
97+ self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
98+ current_idx,
99+ idx as f64,
100+ Duration::from_millis(250),
101+ )));
102+ }
103+
104+ pub fn add_window(
105+ &mut self,
106+ workspace_idx: usize,
107+ window: W,
108+ activate: bool,
109+ width: ColumnWidth,
110+ is_full_width: bool,
111+ ) {
112+ let workspace = &mut self.workspaces[workspace_idx];
113+
114+ workspace.add_window(window.clone(), activate, width, is_full_width);
115+
116+ // After adding a new window, workspace becomes this output's own.
117+ workspace.original_output = OutputId::new(&self.output);
118+
119+ if workspace_idx == self.workspaces.len() - 1 {
120+ // Insert a new empty workspace.
121+ let ws = Workspace::new(self.output.clone(), self.options.clone());
122+ self.workspaces.push(ws);
123+ }
124+
125+ if activate {
126+ self.activate_workspace(workspace_idx);
127+ }
128+ }
129+
130+ fn clean_up_workspaces(&mut self) {
131+ assert!(self.workspace_switch.is_none());
132+
133+ for idx in (0..self.workspaces.len() - 1).rev() {
134+ if self.active_workspace_idx == idx {
135+ continue;
136+ }
137+
138+ if !self.workspaces[idx].has_windows() {
139+ self.workspaces.remove(idx);
140+ if self.active_workspace_idx > idx {
141+ self.active_workspace_idx -= 1;
142+ }
143+ }
144+ }
145+ }
146+
147+ pub fn move_left(&mut self) {
148+ self.active_workspace().move_left();
149+ }
150+
151+ pub fn move_right(&mut self) {
152+ self.active_workspace().move_right();
153+ }
154+
155+ pub fn move_down(&mut self) {
156+ self.active_workspace().move_down();
157+ }
158+
159+ pub fn move_up(&mut self) {
160+ self.active_workspace().move_up();
161+ }
162+
163+ pub fn move_down_or_to_workspace_down(&mut self) {
164+ let workspace = self.active_workspace();
165+ if workspace.columns.is_empty() {
166+ return;
167+ }
168+ let column = &mut workspace.columns[workspace.active_column_idx];
169+ let curr_idx = column.active_window_idx;
170+ let new_idx = min(column.active_window_idx + 1, column.windows.len() - 1);
171+ if curr_idx == new_idx {
172+ self.move_to_workspace_down();
173+ } else {
174+ workspace.move_down();
175+ }
176+ }
177+
178+ pub fn move_up_or_to_workspace_up(&mut self) {
179+ let workspace = self.active_workspace();
180+ if workspace.columns.is_empty() {
181+ return;
182+ }
183+ let curr_idx = workspace.columns[workspace.active_column_idx].active_window_idx;
184+ let new_idx = curr_idx.saturating_sub(1);
185+ if curr_idx == new_idx {
186+ self.move_to_workspace_up();
187+ } else {
188+ workspace.move_up();
189+ }
190+ }
191+
192+ pub fn focus_left(&mut self) {
193+ self.active_workspace().focus_left();
194+ }
195+
196+ pub fn focus_right(&mut self) {
197+ self.active_workspace().focus_right();
198+ }
199+
200+ pub fn focus_down(&mut self) {
201+ self.active_workspace().focus_down();
202+ }
203+
204+ pub fn focus_up(&mut self) {
205+ self.active_workspace().focus_up();
206+ }
207+
208+ pub fn focus_window_or_workspace_down(&mut self) {
209+ let workspace = self.active_workspace();
210+ if workspace.columns.is_empty() {
211+ self.switch_workspace_down();
212+ } else {
213+ let column = &workspace.columns[workspace.active_column_idx];
214+ let curr_idx = column.active_window_idx;
215+ let new_idx = min(column.active_window_idx + 1, column.windows.len() - 1);
216+ if curr_idx == new_idx {
217+ self.switch_workspace_down();
218+ } else {
219+ workspace.focus_down();
220+ }
221+ }
222+ }
223+
224+ pub fn focus_window_or_workspace_up(&mut self) {
225+ let workspace = self.active_workspace();
226+ if workspace.columns.is_empty() {
227+ self.switch_workspace_up();
228+ } else {
229+ let curr_idx = workspace.columns[workspace.active_column_idx].active_window_idx;
230+ let new_idx = curr_idx.saturating_sub(1);
231+ if curr_idx == new_idx {
232+ self.switch_workspace_up();
233+ } else {
234+ workspace.focus_up();
235+ }
236+ }
237+ }
238+
239+ pub fn move_to_workspace_up(&mut self) {
240+ let source_workspace_idx = self.active_workspace_idx;
241+
242+ let new_idx = source_workspace_idx.saturating_sub(1);
243+ if new_idx == source_workspace_idx {
244+ return;
245+ }
246+
247+ let workspace = &mut self.workspaces[source_workspace_idx];
248+ if workspace.columns.is_empty() {
249+ return;
250+ }
251+
252+ let column = &mut workspace.columns[workspace.active_column_idx];
253+ let width = column.width;
254+ let is_full_width = column.is_full_width;
255+ let window = column.windows[column.active_window_idx].clone();
256+ workspace.remove_window(&window);
257+
258+ self.add_window(new_idx, window, true, width, is_full_width);
259+ }
260+
261+ pub fn move_to_workspace_down(&mut self) {
262+ let source_workspace_idx = self.active_workspace_idx;
263+
264+ let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1);
265+ if new_idx == source_workspace_idx {
266+ return;
267+ }
268+
269+ let workspace = &mut self.workspaces[source_workspace_idx];
270+ if workspace.columns.is_empty() {
271+ return;
272+ }
273+
274+ let column = &mut workspace.columns[workspace.active_column_idx];
275+ let width = column.width;
276+ let is_full_width = column.is_full_width;
277+ let window = column.windows[column.active_window_idx].clone();
278+ workspace.remove_window(&window);
279+
280+ self.add_window(new_idx, window, true, width, is_full_width);
281+ }
282+
283+ pub fn move_to_workspace(&mut self, idx: usize) {
284+ let source_workspace_idx = self.active_workspace_idx;
285+
286+ let new_idx = min(idx, self.workspaces.len() - 1);
287+ if new_idx == source_workspace_idx {
288+ return;
289+ }
290+
291+ let workspace = &mut self.workspaces[source_workspace_idx];
292+ if workspace.columns.is_empty() {
293+ return;
294+ }
295+
296+ let column = &mut workspace.columns[workspace.active_column_idx];
297+ let width = column.width;
298+ let is_full_width = column.is_full_width;
299+ let window = column.windows[column.active_window_idx].clone();
300+ workspace.remove_window(&window);
301+
302+ self.add_window(new_idx, window, true, width, is_full_width);
303+
304+ // Don't animate this action.
305+ self.workspace_switch = None;
306+
307+ self.clean_up_workspaces();
308+ }
309+
310+ pub fn switch_workspace_up(&mut self) {
311+ self.activate_workspace(self.active_workspace_idx.saturating_sub(1));
312+ }
313+
314+ pub fn switch_workspace_down(&mut self) {
315+ self.activate_workspace(min(
316+ self.active_workspace_idx + 1,
317+ self.workspaces.len() - 1,
318+ ));
319+ }
320+
321+ pub fn switch_workspace(&mut self, idx: usize) {
322+ self.activate_workspace(min(idx, self.workspaces.len() - 1));
323+ // Don't animate this action.
324+ self.workspace_switch = None;
325+
326+ self.clean_up_workspaces();
327+ }
328+
329+ pub fn consume_into_column(&mut self) {
330+ self.active_workspace().consume_into_column();
331+ }
332+
333+ pub fn expel_from_column(&mut self) {
334+ self.active_workspace().expel_from_column();
335+ }
336+
337+ pub fn center_column(&mut self) {
338+ self.active_workspace().center_column();
339+ }
340+
341+ pub fn focus(&self) -> Option<&W> {
342+ let workspace = &self.workspaces[self.active_workspace_idx];
343+ if !workspace.has_windows() {
344+ return None;
345+ }
346+
347+ let column = &workspace.columns[workspace.active_column_idx];
348+ Some(&column.windows[column.active_window_idx])
349+ }
350+
351+ pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
352+ if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch {
353+ anim.set_current_time(current_time);
354+ if anim.is_done() {
355+ self.workspace_switch = None;
356+ self.clean_up_workspaces();
357+ }
358+ }
359+
360+ for ws in &mut self.workspaces {
361+ ws.advance_animations(current_time, is_active);
362+ }
363+ }
364+
365+ pub fn are_animations_ongoing(&self) -> bool {
366+ self.workspace_switch
367+ .as_ref()
368+ .is_some_and(|s| s.is_animation())
369+ || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
370+ }
371+
372+ pub fn are_transitions_ongoing(&self) -> bool {
373+ self.workspace_switch.is_some()
374+ || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
375+ }
376+
377+ pub fn update_config(&mut self, options: Rc<Options>) {
378+ for ws in &mut self.workspaces {
379+ ws.update_config(options.clone());
380+ }
381+
382+ if self.options.struts != options.struts {
383+ let view_size = output_size(&self.output);
384+ let working_area = compute_working_area(&self.output, options.struts);
385+
386+ for ws in &mut self.workspaces {
387+ ws.set_view_size(view_size, working_area);
388+ }
389+ }
390+
391+ self.options = options;
392+ }
393+
394+ pub fn toggle_width(&mut self) {
395+ self.active_workspace().toggle_width();
396+ }
397+
398+ pub fn toggle_full_width(&mut self) {
399+ self.active_workspace().toggle_full_width();
400+ }
401+
402+ pub fn set_column_width(&mut self, change: SizeChange) {
403+ self.active_workspace().set_column_width(change);
404+ }
405+
406+ pub fn set_window_height(&mut self, change: SizeChange) {
407+ self.active_workspace().set_window_height(change);
408+ }
409+
410+ pub fn move_workspace_down(&mut self) {
411+ let new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1);
412+ if new_idx == self.active_workspace_idx {
413+ return;
414+ }
415+
416+ self.workspaces.swap(self.active_workspace_idx, new_idx);
417+
418+ if new_idx == self.workspaces.len() - 1 {
419+ // Insert a new empty workspace.
420+ let ws = Workspace::new(self.output.clone(), self.options.clone());
421+ self.workspaces.push(ws);
422+ }
423+
424+ self.activate_workspace(new_idx);
425+ self.workspace_switch = None;
426+
427+ self.clean_up_workspaces();
428+ }
429+
430+ pub fn move_workspace_up(&mut self) {
431+ let new_idx = self.active_workspace_idx.saturating_sub(1);
432+ if new_idx == self.active_workspace_idx {
433+ return;
434+ }
435+
436+ self.workspaces.swap(self.active_workspace_idx, new_idx);
437+
438+ if self.active_workspace_idx == self.workspaces.len() - 1 {
439+ // Insert a new empty workspace.
440+ let ws = Workspace::new(self.output.clone(), self.options.clone());
441+ self.workspaces.push(ws);
442+ }
443+
444+ self.activate_workspace(new_idx);
445+ self.workspace_switch = None;
446+
447+ self.clean_up_workspaces();
448+ }
449+
450+ pub fn window_under(
451+ &self,
452+ pos_within_output: Point<f64, Logical>,
453+ ) -> Option<(&W, Point<i32, Logical>)> {
454+ match &self.workspace_switch {
455+ Some(switch) => {
456+ let size = output_size(&self.output);
457+
458+ let render_idx = switch.current_idx();
459+ let before_idx = render_idx.floor() as usize;
460+ let after_idx = render_idx.ceil() as usize;
461+
462+ let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32;
463+
464+ let (idx, ws_offset) = if pos_within_output.y < (size.h - offset) as f64 {
465+ (before_idx, Point::from((0, offset)))
466+ } else {
467+ (after_idx, Point::from((0, -size.h + offset)))
468+ };
469+
470+ let ws = &self.workspaces[idx];
471+ let (win, win_pos) = ws.window_under(pos_within_output + ws_offset.to_f64())?;
472+ Some((win, win_pos - ws_offset))
473+ }
474+ None => {
475+ let ws = &self.workspaces[self.active_workspace_idx];
476+ ws.window_under(pos_within_output)
477+ }
478+ }
479+ }
480+
481+ pub fn render_above_top_layer(&self) -> bool {
482+ // Render above the top layer only if the view is stationary.
483+ if self.workspace_switch.is_some() {
484+ return false;
485+ }
486+
487+ let ws = &self.workspaces[self.active_workspace_idx];
488+ ws.render_above_top_layer()
489+ }
490+}
491+
492+impl Monitor<Window> {
493+ pub fn render_elements(
494+ &self,
495+ renderer: &mut GlesRenderer,
496+ ) -> Vec<MonitorRenderElement<GlesRenderer>> {
497+ let _span = tracy_client::span!("Monitor::render_elements");
498+
499+ let output_scale = Scale::from(self.output.current_scale().fractional_scale());
500+ let output_transform = self.output.current_transform();
501+ let output_mode = self.output.current_mode().unwrap();
502+ let size = output_transform.transform_size(output_mode.size);
503+
504+ match &self.workspace_switch {
505+ Some(switch) => {
506+ let render_idx = switch.current_idx();
507+ let before_idx = render_idx.floor() as usize;
508+ let after_idx = render_idx.ceil() as usize;
509+
510+ let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32;
511+
512+ let before = self.workspaces[before_idx].render_elements(renderer);
513+ let after = self.workspaces[after_idx].render_elements(renderer);
514+
515+ let before = before.into_iter().filter_map(|elem| {
516+ Some(RelocateRenderElement::from_element(
517+ CropRenderElement::from_element(
518+ elem,
519+ output_scale,
520+ Rectangle::from_extemities((0, offset), (size.w, size.h)),
521+ )?,
522+ (0, -offset),
523+ Relocate::Relative,
524+ ))
525+ });
526+ let after = after.into_iter().filter_map(|elem| {
527+ Some(RelocateRenderElement::from_element(
528+ CropRenderElement::from_element(
529+ elem,
530+ output_scale,
531+ Rectangle::from_extemities((0, 0), (size.w, offset)),
532+ )?,
533+ (0, -offset + size.h),
534+ Relocate::Relative,
535+ ))
536+ });
537+ before.chain(after).collect()
538+ }
539+ None => {
540+ let elements = self.workspaces[self.active_workspace_idx].render_elements(renderer);
541+ elements
542+ .into_iter()
543+ .filter_map(|elem| {
544+ Some(RelocateRenderElement::from_element(
545+ CropRenderElement::from_element(
546+ elem,
547+ output_scale,
548+ // HACK: set infinite crop bounds due to a damage tracking bug
549+ // which causes glitched rendering for maximized GTK windows.
550+ // FIXME: use proper bounds after fixing the Crop element.
551+ Rectangle::from_loc_and_size(
552+ (-i32::MAX / 2, -i32::MAX / 2),
553+ (i32::MAX, i32::MAX),
554+ ),
555+ // Rectangle::from_loc_and_size((0, 0), size),
556+ )?,
557+ (0, 0),
558+ Relocate::Relative,
559+ ))
560+ })
561+ .collect()
562+ }
563+ }
564+ }
565+}
src/layout/workspace.rsadded+1441 -0
@@ -0,0 +1,1441 @@
1+use std::cmp::{max, min};
2+use std::iter::zip;
3+use std::rc::Rc;
4+use std::time::Duration;
5+
6+use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
7+use smithay::backend::renderer::element::AsRenderElements;
8+use smithay::backend::renderer::gles::GlesRenderer;
9+use smithay::backend::renderer::ImportAll;
10+use smithay::desktop::space::SpaceElement;
11+use smithay::desktop::{layer_map_for_output, Window};
12+use smithay::output::Output;
13+use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
14+use smithay::render_elements;
15+use smithay::utils::{Logical, Point, Rectangle, Scale, Size};
16+
17+use super::focus_ring::{FocusRing, FocusRingRenderElement};
18+use super::{LayoutElement, Options};
19+use crate::animation::Animation;
20+use crate::config::{PresetWidth, SizeChange, Struts};
21+use crate::utils::output_size;
22+
23+#[derive(Debug)]
24+pub struct Workspace<W: LayoutElement> {
25+ /// The original output of this workspace.
26+ ///
27+ /// Most of the time this will be the workspace's current output, however, after an output
28+ /// disconnection, it may remain pointing to the disconnected output.
29+ pub original_output: OutputId,
30+
31+ /// Current output of this workspace.
32+ output: Option<Output>,
33+
34+ /// Latest known view size for this workspace.
35+ ///
36+ /// This should be computed from the current workspace output size, or, if all outputs have
37+ /// been disconnected, preserved until a new output is connected.
38+ view_size: Size<i32, Logical>,
39+
40+ /// Latest known working area for this workspace.
41+ ///
42+ /// This is similar to view size, but takes into account things like layer shell exclusive
43+ /// zones.
44+ working_area: Rectangle<i32, Logical>,
45+
46+ /// Columns of windows on this workspace.
47+ pub columns: Vec<Column<W>>,
48+
49+ /// Index of the currently active column, if any.
50+ pub active_column_idx: usize,
51+
52+ /// Focus ring buffer and parameters.
53+ focus_ring: FocusRing,
54+
55+ /// Offset of the view computed from the active column.
56+ ///
57+ /// Any gaps, including left padding from work area left exclusive zone, is handled
58+ /// with this view offset (rather than added as a constant elsewhere in the code). This allows
59+ /// for natural handling of fullscreen windows, which must ignore work area padding.
60+ view_offset: i32,
61+
62+ /// Animation of the view offset, if one is currently ongoing.
63+ view_offset_anim: Option<Animation>,
64+
65+ /// Whether to activate the previous, rather than the next, column upon column removal.
66+ ///
67+ /// When a new column is created and removed with no focus changes in-between, it is more
68+ /// natural to activate the previously-focused column. This variable tracks that.
69+ ///
70+ /// Since we only create-and-activate columns immediately to the right of the active column (in
71+ /// contrast to tabs in Firefox, for example), we can track this as a bool, rather than an
72+ /// index of the previous column to activate.
73+ activate_prev_column_on_removal: bool,
74+
75+ /// Configurable properties of the layout.
76+ pub options: Rc<Options>,
77+}
78+
79+#[derive(Debug, Clone, PartialEq, Eq)]
80+pub struct OutputId(String);
81+
82+render_elements! {
83+ #[derive(Debug)]
84+ pub WorkspaceRenderElement<R> where R: ImportAll;
85+ Wayland = WaylandSurfaceRenderElement<R>,
86+ FocusRing = FocusRingRenderElement,
87+}
88+
89+/// Width of a column.
90+#[derive(Debug, Clone, Copy, PartialEq)]
91+pub enum ColumnWidth {
92+ /// Proportion of the current view width.
93+ Proportion(f64),
94+ /// One of the proportion presets.
95+ ///
96+ /// This is separate from Proportion in order to be able to reliably cycle between preset
97+ /// proportions.
98+ Preset(usize),
99+ /// Fixed width in logical pixels.
100+ Fixed(i32),
101+}
102+
103+/// Height of a window in a column.
104+///
105+/// Proportional height is intentionally omitted. With column widths you frequently want e.g. two
106+/// columns side-by-side with 50% width each, and you want them to remain this way when moving to a
107+/// differently sized monitor. Windows in a column, however, already auto-size to fill the available
108+/// height, giving you this behavior. The only reason to set a different window height, then, is
109+/// when you want something in the window to fit exactly, e.g. to fit 30 lines in a terminal, which
110+/// corresponds to the `Fixed` variant.
111+///
112+/// This does not preclude the usual set of binds to set or resize a window proportionally. Just,
113+/// they are converted to, and stored as fixed height right away, so that once you resize a window
114+/// to fit the desired content, it can never become smaller than that when moving between monitors.
115+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116+pub enum WindowHeight {
117+ /// Automatically computed height, evenly distributed across the column.
118+ Auto,
119+ /// Fixed height in logical pixels.
120+ Fixed(i32),
121+}
122+
123+#[derive(Debug)]
124+pub struct Column<W: LayoutElement> {
125+ /// Windows in this column.
126+ ///
127+ /// Must be non-empty.
128+ pub windows: Vec<W>,
129+
130+ /// Heights of the windows.
131+ ///
132+ /// Must have the same number of elements as `windows`.
133+ heights: Vec<WindowHeight>,
134+
135+ /// Index of the currently active window.
136+ pub active_window_idx: usize,
137+
138+ /// Desired width of this column.
139+ ///
140+ /// If the column is full-width or full-screened, this is the width that should be restored
141+ /// upon unfullscreening and untoggling full-width.
142+ pub width: ColumnWidth,
143+
144+ /// Whether this column is full-width.
145+ pub is_full_width: bool,
146+
147+ /// Whether this column contains a single full-screened window.
148+ pub is_fullscreen: bool,
149+
150+ /// Latest known view size for this column's workspace.
151+ view_size: Size<i32, Logical>,
152+
153+ /// Latest known working area for this column's workspace.
154+ working_area: Rectangle<i32, Logical>,
155+
156+ /// Configurable properties of the layout.
157+ options: Rc<Options>,
158+}
159+
160+impl OutputId {
161+ pub fn new(output: &Output) -> Self {
162+ Self(output.name())
163+ }
164+}
165+
166+impl ColumnWidth {
167+ fn resolve(self, options: &Options, view_width: i32) -> i32 {
168+ match self {
169+ ColumnWidth::Proportion(proportion) => {
170+ ((view_width - options.gaps) as f64 * proportion).floor() as i32 - options.gaps
171+ }
172+ ColumnWidth::Preset(idx) => options.preset_widths[idx].resolve(options, view_width),
173+ ColumnWidth::Fixed(width) => width,
174+ }
175+ }
176+}
177+
178+impl From<PresetWidth> for ColumnWidth {
179+ fn from(value: PresetWidth) -> Self {
180+ match value {
181+ PresetWidth::Proportion(p) => Self::Proportion(p.clamp(0., 10000.)),
182+ PresetWidth::Fixed(f) => Self::Fixed(f.clamp(1, 100000)),
183+ }
184+ }
185+}
186+
187+impl<W: LayoutElement> Workspace<W> {
188+ pub fn new(output: Output, options: Rc<Options>) -> Self {
189+ let working_area = compute_working_area(&output, options.struts);
190+ Self {
191+ original_output: OutputId::new(&output),
192+ view_size: output_size(&output),
193+ working_area,
194+ output: Some(output),
195+ columns: vec![],
196+ active_column_idx: 0,
197+ focus_ring: FocusRing::new(options.focus_ring),
198+ view_offset: 0,
199+ view_offset_anim: None,
200+ activate_prev_column_on_removal: false,
201+ options,
202+ }
203+ }
204+
205+ pub fn new_no_outputs(options: Rc<Options>) -> Self {
206+ Self {
207+ output: None,
208+ original_output: OutputId(String::new()),
209+ view_size: Size::from((1280, 720)),
210+ working_area: Rectangle::from_loc_and_size((0, 0), (1280, 720)),
211+ columns: vec![],
212+ active_column_idx: 0,
213+ focus_ring: FocusRing::new(options.focus_ring),
214+ view_offset: 0,
215+ view_offset_anim: None,
216+ activate_prev_column_on_removal: false,
217+ options,
218+ }
219+ }
220+
221+ pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
222+ match &mut self.view_offset_anim {
223+ Some(anim) => {
224+ anim.set_current_time(current_time);
225+ self.view_offset = anim.value().round() as i32;
226+ if anim.is_done() {
227+ self.view_offset_anim = None;
228+ }
229+ }
230+ None => (),
231+ }
232+
233+ let view_pos = self.view_pos();
234+
235+ // This shall one day become a proper animation.
236+ if !self.columns.is_empty() {
237+ let col = &self.columns[self.active_column_idx];
238+ let active_win = &col.windows[col.active_window_idx];
239+ let geom = active_win.geometry();
240+ let has_ssd = active_win.has_ssd();
241+
242+ let win_pos = Point::from((
243+ self.column_x(self.active_column_idx) - view_pos,
244+ col.window_y(col.active_window_idx),
245+ ));
246+
247+ self.focus_ring.update(win_pos, geom.size, has_ssd);
248+ self.focus_ring.set_active(is_active);
249+ }
250+ }
251+
252+ pub fn are_animations_ongoing(&self) -> bool {
253+ self.view_offset_anim.is_some()
254+ }
255+
256+ pub fn update_config(&mut self, options: Rc<Options>) {
257+ self.focus_ring.update_config(options.focus_ring);
258+ // The focus ring buffer will be updated in a subsequent update_animations call.
259+
260+ for column in &mut self.columns {
261+ column.update_config(options.clone());
262+ }
263+
264+ self.options = options;
265+ }
266+
267+ pub fn windows(&self) -> impl Iterator<Item = &W> + '_ {
268+ self.columns.iter().flat_map(|col| col.windows.iter())
269+ }
270+
271+ pub fn set_output(&mut self, output: Option<Output>) {
272+ if self.output == output {
273+ return;
274+ }
275+
276+ if let Some(output) = self.output.take() {
277+ for win in self.windows() {
278+ win.output_leave(&output);
279+ }
280+ }
281+
282+ self.output = output;
283+
284+ if let Some(output) = &self.output {
285+ let working_area = compute_working_area(output, self.options.struts);
286+ self.set_view_size(output_size(output), working_area);
287+
288+ for win in self.windows() {
289+ self.enter_output_for_window(win);
290+ }
291+ }
292+ }
293+
294+ fn enter_output_for_window(&self, window: &W) {
295+ if let Some(output) = &self.output {
296+ prepare_for_output(window, output);
297+
298+ // FIXME: proper overlap.
299+ window.output_enter(
300+ output,
301+ Rectangle::from_loc_and_size((0, 0), (i32::MAX, i32::MAX)),
302+ );
303+ }
304+ }
305+
306+ pub fn set_view_size(
307+ &mut self,
308+ size: Size<i32, Logical>,
309+ working_area: Rectangle<i32, Logical>,
310+ ) {
311+ if self.view_size == size && self.working_area == working_area {
312+ return;
313+ }
314+
315+ self.view_size = size;
316+ self.working_area = working_area;
317+
318+ for col in &mut self.columns {
319+ col.set_view_size(self.view_size, self.working_area);
320+ }
321+ }
322+
323+ fn toplevel_bounds(&self) -> Size<i32, Logical> {
324+ Size::from((
325+ max(self.working_area.size.w - self.options.gaps * 2, 1),
326+ max(self.working_area.size.h - self.options.gaps * 2, 1),
327+ ))
328+ }
329+
330+ pub fn configure_new_window(&self, window: &Window) {
331+ let width = if let Some(width) = self.options.default_width {
332+ max(1, width.resolve(&self.options, self.working_area.size.w))
333+ } else {
334+ 0
335+ };
336+
337+ let height = self.working_area.size.h - self.options.gaps * 2;
338+ let size = Size::from((width, max(height, 1)));
339+
340+ let bounds = self.toplevel_bounds();
341+
342+ if let Some(output) = self.output.as_ref() {
343+ prepare_for_output(window, output);
344+ }
345+
346+ window.toplevel().with_pending_state(|state| {
347+ state.size = Some(size);
348+ state.bounds = Some(bounds);
349+ });
350+ }
351+
352+ fn compute_new_view_offset_for_column(&self, current_x: i32, idx: usize) -> i32 {
353+ if self.columns[idx].is_fullscreen {
354+ return 0;
355+ }
356+
357+ let new_col_x = self.column_x(idx);
358+
359+ let final_x = if let Some(anim) = &self.view_offset_anim {
360+ current_x - self.view_offset + anim.to().round() as i32
361+ } else {
362+ current_x
363+ };
364+
365+ let new_offset = compute_new_view_offset(
366+ final_x + self.working_area.loc.x,
367+ self.working_area.size.w,
368+ new_col_x,
369+ self.columns[idx].width(),
370+ self.options.gaps,
371+ );
372+
373+ // Non-fullscreen windows are always offset at least by the working area position.
374+ new_offset - self.working_area.loc.x
375+ }
376+
377+ fn animate_view_offset_to_column(&mut self, current_x: i32, idx: usize) {
378+ let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx);
379+
380+ let new_col_x = self.column_x(idx);
381+ let from_view_offset = current_x - new_col_x;
382+ self.view_offset = from_view_offset;
383+
384+ // If we're already animating towards that, don't restart it.
385+ if let Some(anim) = &self.view_offset_anim {
386+ if anim.value().round() as i32 == self.view_offset
387+ && anim.to().round() as i32 == new_view_offset
388+ {
389+ return;
390+ }
391+ }
392+
393+ // If our view offset is already this, we don't need to do anything.
394+ if self.view_offset == new_view_offset {
395+ self.view_offset_anim = None;
396+ return;
397+ }
398+
399+ self.view_offset_anim = Some(Animation::new(
400+ self.view_offset as f64,
401+ new_view_offset as f64,
402+ Duration::from_millis(250),
403+ ));
404+ }
405+
406+ fn activate_column(&mut self, idx: usize) {
407+ if self.active_column_idx == idx {
408+ return;
409+ }
410+
411+ let current_x = self.view_pos();
412+ self.animate_view_offset_to_column(current_x, idx);
413+
414+ self.active_column_idx = idx;
415+
416+ // A different column was activated; reset the flag.
417+ self.activate_prev_column_on_removal = false;
418+ }
419+
420+ pub fn has_windows(&self) -> bool {
421+ self.windows().next().is_some()
422+ }
423+
424+ pub fn has_window(&self, window: &W) -> bool {
425+ self.windows().any(|win| win == window)
426+ }
427+
428+ pub fn find_wl_surface(&self, wl_surface: &WlSurface) -> Option<&W> {
429+ self.windows().find(|win| win.is_wl_surface(wl_surface))
430+ }
431+
432+ /// Computes the X position of the windows in the given column, in logical coordinates.
433+ fn column_x(&self, column_idx: usize) -> i32 {
434+ let mut x = 0;
435+
436+ for column in self.columns.iter().take(column_idx) {
437+ x += column.width() + self.options.gaps;
438+ }
439+
440+ x
441+ }
442+
443+ pub fn add_window(
444+ &mut self,
445+ window: W,
446+ activate: bool,
447+ width: ColumnWidth,
448+ is_full_width: bool,
449+ ) {
450+ self.enter_output_for_window(&window);
451+
452+ let was_empty = self.columns.is_empty();
453+
454+ let idx = if self.columns.is_empty() {
455+ 0
456+ } else {
457+ self.active_column_idx + 1
458+ };
459+
460+ let column = Column::new(
461+ window,
462+ self.view_size,
463+ self.working_area,
464+ self.options.clone(),
465+ width,
466+ is_full_width,
467+ );
468+ self.columns.insert(idx, column);
469+
470+ if activate {
471+ // If this is the first window on an empty workspace, skip the animation from whatever
472+ // view_offset was left over.
473+ if was_empty {
474+ // Try to make the code produce a left-aligned offset, even in presence of left
475+ // exclusive zones.
476+ self.view_offset = self.compute_new_view_offset_for_column(self.column_x(0), 0);
477+ self.view_offset_anim = None;
478+ }
479+
480+ self.activate_column(idx);
481+ self.activate_prev_column_on_removal = true;
482+ }
483+ }
484+
485+ pub fn remove_window(&mut self, window: &W) {
486+ if let Some(output) = &self.output {
487+ window.output_leave(output);
488+ }
489+
490+ let column_idx = self
491+ .columns
492+ .iter()
493+ .position(|col| col.contains(window))
494+ .unwrap();
495+ let column = &mut self.columns[column_idx];
496+
497+ let window_idx = column.windows.iter().position(|win| win == window).unwrap();
498+ column.windows.remove(window_idx);
499+ column.heights.remove(window_idx);
500+ if column.windows.is_empty() {
501+ if column_idx + 1 == self.active_column_idx {
502+ // The previous column, that we were going to activate upon removal of the active
503+ // column, has just been itself removed.
504+ self.activate_prev_column_on_removal = false;
505+ }
506+
507+ // FIXME: activate_column below computes current view position to compute the new view
508+ // position, which can include the column we're removing here. This leads to unwanted
509+ // view jumps.
510+ self.columns.remove(column_idx);
511+ if self.columns.is_empty() {
512+ return;
513+ }
514+
515+ if self.active_column_idx > column_idx
516+ || (self.active_column_idx == column_idx && self.activate_prev_column_on_removal)
517+ {
518+ // A column to the left was removed; preserve the current position.
519+ // FIXME: preserve activate_prev_column_on_removal.
520+ // Or, the active column was removed, and we needed to activate the previous column.
521+ self.activate_column(self.active_column_idx.saturating_sub(1));
522+ } else {
523+ self.activate_column(min(self.active_column_idx, self.columns.len() - 1));
524+ }
525+
526+ return;
527+ }
528+
529+ column.active_window_idx = min(column.active_window_idx, column.windows.len() - 1);
530+ column.update_window_sizes();
531+ }
532+
533+ pub fn update_window(&mut self, window: &W) {
534+ let (idx, column) = self
535+ .columns
536+ .iter_mut()
537+ .enumerate()
538+ .find(|(_, col)| col.contains(window))
539+ .unwrap();
540+ column.update_window_sizes();
541+
542+ if idx == self.active_column_idx {
543+ // We might need to move the view to ensure the resized window is still visible.
544+ let current_x = self.view_pos();
545+ self.animate_view_offset_to_column(current_x, idx);
546+ }
547+ }
548+
549+ pub fn activate_window(&mut self, window: &W) {
550+ let column_idx = self
551+ .columns
552+ .iter()
553+ .position(|col| col.contains(window))
554+ .unwrap();
555+ let column = &mut self.columns[column_idx];
556+
557+ column.activate_window(window);
558+ self.activate_column(column_idx);
559+ }
560+
561+ #[cfg(test)]
562+ pub fn verify_invariants(&self) {
563+ assert!(self.view_size.w > 0);
564+ assert!(self.view_size.h > 0);
565+
566+ if !self.columns.is_empty() {
567+ assert!(self.active_column_idx < self.columns.len());
568+
569+ for column in &self.columns {
570+ column.verify_invariants();
571+ }
572+ }
573+ }
574+
575+ pub fn focus_left(&mut self) {
576+ self.activate_column(self.active_column_idx.saturating_sub(1));
577+ }
578+
579+ pub fn focus_right(&mut self) {
580+ if self.columns.is_empty() {
581+ return;
582+ }
583+
584+ self.activate_column(min(self.active_column_idx + 1, self.columns.len() - 1));
585+ }
586+
587+ pub fn focus_down(&mut self) {
588+ if self.columns.is_empty() {
589+ return;
590+ }
591+
592+ self.columns[self.active_column_idx].focus_down();
593+ }
594+
595+ pub fn focus_up(&mut self) {
596+ if self.columns.is_empty() {
597+ return;
598+ }
599+
600+ self.columns[self.active_column_idx].focus_up();
601+ }
602+
603+ pub fn move_left(&mut self) {
604+ let new_idx = self.active_column_idx.saturating_sub(1);
605+ if self.active_column_idx == new_idx {
606+ return;
607+ }
608+
609+ let current_x = self.view_pos();
610+
611+ self.columns.swap(self.active_column_idx, new_idx);
612+
613+ self.view_offset =
614+ self.compute_new_view_offset_for_column(current_x, self.active_column_idx);
615+
616+ self.activate_column(new_idx);
617+ }
618+
619+ pub fn move_right(&mut self) {
620+ if self.columns.is_empty() {
621+ return;
622+ }
623+
624+ let new_idx = min(self.active_column_idx + 1, self.columns.len() - 1);
625+ if self.active_column_idx == new_idx {
626+ return;
627+ }
628+
629+ let current_x = self.view_pos();
630+
631+ self.columns.swap(self.active_column_idx, new_idx);
632+
633+ self.view_offset =
634+ self.compute_new_view_offset_for_column(current_x, self.active_column_idx);
635+
636+ self.activate_column(new_idx);
637+ }
638+
639+ pub fn move_down(&mut self) {
640+ if self.columns.is_empty() {
641+ return;
642+ }
643+
644+ self.columns[self.active_column_idx].move_down();
645+ }
646+
647+ pub fn move_up(&mut self) {
648+ if self.columns.is_empty() {
649+ return;
650+ }
651+
652+ self.columns[self.active_column_idx].move_up();
653+ }
654+
655+ pub fn consume_into_column(&mut self) {
656+ if self.columns.len() < 2 {
657+ return;
658+ }
659+
660+ if self.active_column_idx == self.columns.len() - 1 {
661+ return;
662+ }
663+
664+ let source_column_idx = self.active_column_idx + 1;
665+
666+ let source_column = &mut self.columns[source_column_idx];
667+ let window = source_column.windows[0].clone();
668+ self.remove_window(&window);
669+
670+ let target_column = &mut self.columns[self.active_column_idx];
671+ target_column.add_window(window);
672+ }
673+
674+ pub fn expel_from_column(&mut self) {
675+ if self.columns.is_empty() {
676+ return;
677+ }
678+
679+ let source_column = &mut self.columns[self.active_column_idx];
680+ if source_column.windows.len() == 1 {
681+ return;
682+ }
683+
684+ let width = source_column.width;
685+ let is_full_width = source_column.is_full_width;
686+ let window = source_column.windows[source_column.active_window_idx].clone();
687+ self.remove_window(&window);
688+
689+ self.add_window(window, true, width, is_full_width);
690+ }
691+
692+ pub fn center_column(&mut self) {
693+ if self.columns.is_empty() {
694+ return;
695+ }
696+
697+ let col = &self.columns[self.active_column_idx];
698+ if col.is_fullscreen {
699+ return;
700+ }
701+
702+ let width = col.width();
703+
704+ // If the column is wider than the working area, then on commit it will be shifted to left
705+ // edge alignment by the usual positioning code, so there's no use in doing anything here.
706+ if self.working_area.size.w <= width {
707+ return;
708+ }
709+
710+ let new_view_offset = -(self.working_area.size.w - width) / 2 - self.working_area.loc.x;
711+
712+ // If we're already animating towards that, don't restart it.
713+ if let Some(anim) = &self.view_offset_anim {
714+ if anim.to().round() as i32 == new_view_offset {
715+ return;
716+ }
717+ }
718+
719+ // If our view offset is already this, we don't need to do anything.
720+ if self.view_offset == new_view_offset {
721+ return;
722+ }
723+
724+ self.view_offset_anim = Some(Animation::new(
725+ self.view_offset as f64,
726+ new_view_offset as f64,
727+ Duration::from_millis(250),
728+ ));
729+ }
730+
731+ fn view_pos(&self) -> i32 {
732+ self.column_x(self.active_column_idx) + self.view_offset
733+ }
734+
735+ pub fn window_under(&self, pos: Point<f64, Logical>) -> Option<(&W, Point<i32, Logical>)> {
736+ if self.columns.is_empty() {
737+ return None;
738+ }
739+
740+ let view_pos = self.view_pos();
741+
742+ // Prefer the active window since it's drawn on top.
743+ let col = &self.columns[self.active_column_idx];
744+ let active_win = &col.windows[col.active_window_idx];
745+ let geom = active_win.geometry();
746+ let buf_pos = Point::from((
747+ self.column_x(self.active_column_idx) - view_pos,
748+ col.window_y(col.active_window_idx),
749+ )) - geom.loc;
750+ if active_win.is_in_input_region(&(pos - buf_pos.to_f64())) {
751+ return Some((active_win, buf_pos));
752+ }
753+
754+ let mut x = -view_pos;
755+ for col in &self.columns {
756+ for (win, y) in zip(&col.windows, col.window_ys()) {
757+ if win == active_win {
758+ // Already handled it above.
759+ continue;
760+ }
761+
762+ let geom = win.geometry();
763+ let buf_pos = Point::from((x, y)) - geom.loc;
764+ if win.is_in_input_region(&(pos - buf_pos.to_f64())) {
765+ return Some((win, buf_pos));
766+ }
767+ }
768+
769+ x += col.width() + self.options.gaps;
770+ }
771+
772+ None
773+ }
774+
775+ pub fn toggle_width(&mut self) {
776+ if self.columns.is_empty() {
777+ return;
778+ }
779+
780+ self.columns[self.active_column_idx].toggle_width();
781+ }
782+
783+ pub fn toggle_full_width(&mut self) {
784+ if self.columns.is_empty() {
785+ return;
786+ }
787+
788+ self.columns[self.active_column_idx].toggle_full_width();
789+ }
790+
791+ pub fn set_column_width(&mut self, change: SizeChange) {
792+ if self.columns.is_empty() {
793+ return;
794+ }
795+
796+ self.columns[self.active_column_idx].set_column_width(change);
797+ }
798+
799+ pub fn set_window_height(&mut self, change: SizeChange) {
800+ if self.columns.is_empty() {
801+ return;
802+ }
803+
804+ self.columns[self.active_column_idx].set_window_height(change);
805+ }
806+
807+ pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) {
808+ let (mut col_idx, win_idx) = self
809+ .columns
810+ .iter()
811+ .enumerate()
812+ .find_map(|(col_idx, col)| {
813+ col.windows
814+ .iter()
815+ .position(|w| w == window)
816+ .map(|win_idx| (col_idx, win_idx))
817+ })
818+ .unwrap();
819+
820+ let mut col = &mut self.columns[col_idx];
821+
822+ if is_fullscreen && col.windows.len() > 1 {
823+ // This wasn't the only window in its column; extract it into a separate column.
824+ let target_window_was_focused =
825+ self.active_column_idx == col_idx && col.active_window_idx == win_idx;
826+ let window = col.windows.remove(win_idx);
827+ col.heights.remove(win_idx);
828+ col.active_window_idx = min(col.active_window_idx, col.windows.len() - 1);
829+ col.update_window_sizes();
830+ let width = col.width;
831+ let is_full_width = col.is_full_width;
832+
833+ col_idx += 1;
834+ self.columns.insert(
835+ col_idx,
836+ Column::new(
837+ window,
838+ self.view_size,
839+ self.working_area,
840+ self.options.clone(),
841+ width,
842+ is_full_width,
843+ ),
844+ );
845+ if self.active_column_idx >= col_idx || target_window_was_focused {
846+ self.active_column_idx += 1;
847+ }
848+ col = &mut self.columns[col_idx];
849+ }
850+
851+ col.set_fullscreen(is_fullscreen);
852+ }
853+
854+ pub fn toggle_fullscreen(&mut self, window: &W) {
855+ let col = self
856+ .columns
857+ .iter_mut()
858+ .find(|col| col.windows.contains(window))
859+ .unwrap();
860+ let value = !col.is_fullscreen;
861+ self.set_fullscreen(window, value);
862+ }
863+
864+ pub fn render_above_top_layer(&self) -> bool {
865+ // Render above the top layer if we're on a fullscreen window and the view is stationary.
866+ if self.columns.is_empty() {
867+ return false;
868+ }
869+
870+ if self.view_offset_anim.is_some() {
871+ return false;
872+ }
873+
874+ self.columns[self.active_column_idx].is_fullscreen
875+ }
876+}
877+
878+impl Workspace<Window> {
879+ pub fn refresh(&self) {
880+ let bounds = self.toplevel_bounds();
881+
882+ for (col_idx, col) in self.columns.iter().enumerate() {
883+ for (win_idx, win) in col.windows.iter().enumerate() {
884+ let active = self.active_column_idx == col_idx && col.active_window_idx == win_idx;
885+ win.set_activated(active);
886+
887+ win.toplevel().with_pending_state(|state| {
888+ state.bounds = Some(bounds);
889+ });
890+
891+ win.toplevel().send_pending_configure();
892+ win.refresh();
893+ }
894+ }
895+ }
896+
897+ pub fn render_elements(
898+ &self,
899+ renderer: &mut GlesRenderer,
900+ ) -> Vec<WorkspaceRenderElement<GlesRenderer>> {
901+ if self.columns.is_empty() {
902+ return vec![];
903+ }
904+
905+ // FIXME: workspaces should probably cache their last used scale so they can be correctly
906+ // rendered even with no outputs connected.
907+ let output_scale = self
908+ .output
909+ .as_ref()
910+ .map(|o| Scale::from(o.current_scale().fractional_scale()))
911+ .unwrap_or(Scale::from(1.));
912+
913+ let mut rv = vec![];
914+ let view_pos = self.view_pos();
915+
916+ // Draw the active window on top.
917+ let col = &self.columns[self.active_column_idx];
918+ let active_win = &col.windows[col.active_window_idx];
919+ let win_pos = Point::from((
920+ self.column_x(self.active_column_idx) - view_pos,
921+ col.window_y(col.active_window_idx),
922+ ));
923+
924+ // Draw the window itself.
925+ let geom = active_win.geometry();
926+ let buf_pos = win_pos - geom.loc;
927+ rv.extend(active_win.render_elements(
928+ renderer,
929+ buf_pos.to_physical_precise_round(output_scale),
930+ output_scale,
931+ 1.,
932+ ));
933+
934+ // Draw the focus ring.
935+ rv.extend(self.focus_ring.render(output_scale).map(Into::into));
936+
937+ let mut x = -view_pos;
938+ for col in &self.columns {
939+ for (win, y) in zip(&col.windows, col.window_ys()) {
940+ if win == active_win {
941+ // Already handled it above.
942+ continue;
943+ }
944+
945+ let geom = win.geometry();
946+ let buf_pos = Point::from((x, y)) - geom.loc;
947+ rv.extend(win.render_elements(
948+ renderer,
949+ buf_pos.to_physical_precise_round(output_scale),
950+ output_scale,
951+ 1.,
952+ ));
953+ }
954+
955+ x += col.width() + self.options.gaps;
956+ }
957+
958+ rv
959+ }
960+}
961+
962+impl<W: LayoutElement> Column<W> {
963+ fn new(
964+ window: W,
965+ view_size: Size<i32, Logical>,
966+ working_area: Rectangle<i32, Logical>,
967+ options: Rc<Options>,
968+ width: ColumnWidth,
969+ is_full_width: bool,
970+ ) -> Self {
971+ let mut rv = Self {
972+ windows: vec![],
973+ heights: vec![],
974+ active_window_idx: 0,
975+ width,
976+ is_full_width,
977+ is_fullscreen: false,
978+ view_size,
979+ working_area,
980+ options,
981+ };
982+
983+ rv.add_window(window);
984+
985+ rv
986+ }
987+
988+ fn set_view_size(&mut self, size: Size<i32, Logical>, working_area: Rectangle<i32, Logical>) {
989+ if self.view_size == size && self.working_area == working_area {
990+ return;
991+ }
992+
993+ self.view_size = size;
994+ self.working_area = working_area;
995+
996+ self.update_window_sizes();
997+ }
998+
999+ fn update_config(&mut self, options: Rc<Options>) {
1000+ let mut update_sizes = false;
1001+
1002+ // If preset widths changed, make our width non-preset.
1003+ if self.options.preset_widths != options.preset_widths {
1004+ if let ColumnWidth::Preset(idx) = self.width {
1005+ self.width = self.options.preset_widths[idx];
1006+ }
1007+ }
1008+
1009+ if self.options.gaps != options.gaps {
1010+ update_sizes = true;
1011+ }
1012+
1013+ self.options = options;
1014+
1015+ if update_sizes {
1016+ self.update_window_sizes();
1017+ }
1018+ }
1019+
1020+ fn set_width(&mut self, width: ColumnWidth) {
1021+ self.width = width;
1022+ self.is_full_width = false;
1023+ self.update_window_sizes();
1024+ }
1025+
1026+ fn contains(&self, window: &W) -> bool {
1027+ self.windows.iter().any(|win| win == window)
1028+ }
1029+
1030+ fn activate_window(&mut self, window: &W) {
1031+ let idx = self.windows.iter().position(|win| win == window).unwrap();
1032+ self.active_window_idx = idx;
1033+ }
1034+
1035+ fn add_window(&mut self, window: W) {
1036+ self.is_fullscreen = false;
1037+ self.windows.push(window);
1038+ self.heights.push(WindowHeight::Auto);
1039+ self.update_window_sizes();
1040+ }
1041+
1042+ fn update_window_sizes(&mut self) {
1043+ if self.is_fullscreen {
1044+ self.windows[0].request_fullscreen(self.view_size);
1045+ return;
1046+ }
1047+
1048+ let min_size: Vec<_> = self.windows.iter().map(LayoutElement::min_size).collect();
1049+ let max_size: Vec<_> = self.windows.iter().map(LayoutElement::max_size).collect();
1050+
1051+ // Compute the column width.
1052+ let min_width = min_size
1053+ .iter()
1054+ .filter_map(|size| {
1055+ let w = size.w;
1056+ if w == 0 {
1057+ None
1058+ } else {
1059+ Some(w)
1060+ }
1061+ })
1062+ .max()
1063+ .unwrap_or(1);
1064+ let max_width = max_size
1065+ .iter()
1066+ .filter_map(|size| {
1067+ let w = size.w;
1068+ if w == 0 {
1069+ None
1070+ } else {
1071+ Some(w)
1072+ }
1073+ })
1074+ .min()
1075+ .unwrap_or(i32::MAX);
1076+ let max_width = max(max_width, min_width);
1077+
1078+ let width = if self.is_full_width {
1079+ ColumnWidth::Proportion(1.)
1080+ } else {
1081+ self.width
1082+ };
1083+
1084+ let width = width.resolve(&self.options, self.working_area.size.w);
1085+ let width = max(min(width, max_width), min_width);
1086+
1087+ // Compute the window heights.
1088+ let mut heights = self.heights.clone();
1089+ let mut height_left = self.working_area.size.h - self.options.gaps;
1090+ let mut auto_windows_left = self.windows.len();
1091+
1092+ // Subtract all fixed-height windows.
1093+ for (h, (min_size, max_size)) in zip(&mut heights, zip(&min_size, &max_size)) {
1094+ // Check if the window has an exact height constraint.
1095+ if min_size.h > 0 && min_size.h == max_size.h {
1096+ *h = WindowHeight::Fixed(min_size.h);
1097+ }
1098+
1099+ if let WindowHeight::Fixed(h) = h {
1100+ if max_size.h > 0 {
1101+ *h = min(*h, max_size.h);
1102+ }
1103+ if min_size.h > 0 {
1104+ *h = max(*h, min_size.h);
1105+ }
1106+ *h = max(*h, 1);
1107+
1108+ height_left -= *h + self.options.gaps;
1109+ auto_windows_left -= 1;
1110+ }
1111+ }
1112+
1113+ // Iteratively try to distribute the remaining height, checking against window min heights.
1114+ // Pick an auto height according to the current sizes, then check if it satisfies all
1115+ // remaining min heights. If not, allocate fixed height to those windows and repeat the
1116+ // loop. On each iteration the auto height will get smaller.
1117+ //
1118+ // NOTE: we do not respect max height here. Doing so would complicate things: if the current
1119+ // auto height is above some window's max height, then the auto height can become larger.
1120+ // Combining this with the min height loop is where the complexity appears.
1121+ //
1122+ // However, most max height uses are for fixed-size dialogs, where min height == max_height.
1123+ // This case is separately handled above.
1124+ while auto_windows_left > 0 {
1125+ // Compute the current auto height.
1126+ let auto_height = height_left / auto_windows_left as i32 - self.options.gaps;
1127+ let auto_height = max(auto_height, 1);
1128+
1129+ // Integer division above can result in imperfect height distribution. We will make some
1130+ // windows 1 px taller to account for this.
1131+ let mut ones_left = height_left
1132+ .saturating_sub((auto_height + self.options.gaps) * auto_windows_left as i32);
1133+
1134+ let mut unsatisfied_min = false;
1135+ let mut ones_left_2 = ones_left;
1136+ for (h, min_size) in zip(&mut heights, &min_size) {
1137+ if matches!(h, WindowHeight::Fixed(_)) {
1138+ continue;
1139+ }
1140+
1141+ let mut auto = auto_height;
1142+ if ones_left_2 > 0 {
1143+ auto += 1;
1144+ ones_left_2 -= 1;
1145+ }
1146+
1147+ // Check if the auto height satisfies the min height.
1148+ if min_size.h > 0 && min_size.h > auto {
1149+ *h = WindowHeight::Fixed(min_size.h);
1150+ height_left -= min_size.h + self.options.gaps;
1151+ auto_windows_left -= 1;
1152+ unsatisfied_min = true;
1153+ }
1154+ }
1155+
1156+ // If some min height was unsatisfied, then we allocated the window more than the auto
1157+ // height, which means that the remaining auto windows now have less height to work
1158+ // with, and the loop must run again.
1159+ if unsatisfied_min {
1160+ continue;
1161+ }
1162+
1163+ // All min heights were satisfied, fill them in.
1164+ for h in &mut heights {
1165+ if matches!(h, WindowHeight::Fixed(_)) {
1166+ continue;
1167+ }
1168+
1169+ let mut auto = auto_height;
1170+ if ones_left > 0 {
1171+ auto += 1;
1172+ ones_left -= 1;
1173+ }
1174+
1175+ *h = WindowHeight::Fixed(auto);
1176+ auto_windows_left -= 1;
1177+ }
1178+
1179+ assert_eq!(auto_windows_left, 0);
1180+ }
1181+
1182+ for (win, h) in zip(&self.windows, heights) {
1183+ let WindowHeight::Fixed(height) = h else {
1184+ unreachable!()
1185+ };
1186+
1187+ let size = Size::from((width, height));
1188+ win.request_size(size);
1189+ }
1190+ }
1191+
1192+ fn width(&self) -> i32 {
1193+ self.windows
1194+ .iter()
1195+ .map(|win| win.geometry().size.w)
1196+ .max()
1197+ .unwrap()
1198+ }
1199+
1200+ fn focus_up(&mut self) {
1201+ self.active_window_idx = self.active_window_idx.saturating_sub(1);
1202+ }
1203+
1204+ fn focus_down(&mut self) {
1205+ self.active_window_idx = min(self.active_window_idx + 1, self.windows.len() - 1);
1206+ }
1207+
1208+ fn move_up(&mut self) {
1209+ let new_idx = self.active_window_idx.saturating_sub(1);
1210+ if self.active_window_idx == new_idx {
1211+ return;
1212+ }
1213+
1214+ self.windows.swap(self.active_window_idx, new_idx);
1215+ self.heights.swap(self.active_window_idx, new_idx);
1216+ self.active_window_idx = new_idx;
1217+ }
1218+
1219+ fn move_down(&mut self) {
1220+ let new_idx = min(self.active_window_idx + 1, self.windows.len() - 1);
1221+ if self.active_window_idx == new_idx {
1222+ return;
1223+ }
1224+
1225+ self.windows.swap(self.active_window_idx, new_idx);
1226+ self.heights.swap(self.active_window_idx, new_idx);
1227+ self.active_window_idx = new_idx;
1228+ }
1229+
1230+ #[cfg(test)]
1231+ fn verify_invariants(&self) {
1232+ assert!(!self.windows.is_empty(), "columns can't be empty");
1233+ assert!(self.active_window_idx < self.windows.len());
1234+ assert_eq!(self.windows.len(), self.heights.len());
1235+
1236+ if self.is_fullscreen {
1237+ assert_eq!(self.windows.len(), 1);
1238+ }
1239+ }
1240+
1241+ fn toggle_width(&mut self) {
1242+ let width = if self.is_full_width {
1243+ ColumnWidth::Proportion(1.)
1244+ } else {
1245+ self.width
1246+ };
1247+
1248+ let idx = match width {
1249+ ColumnWidth::Preset(idx) => (idx + 1) % self.options.preset_widths.len(),
1250+ _ => {
1251+ let current = self.width();
1252+ self.options
1253+ .preset_widths
1254+ .iter()
1255+ .position(|prop| {
1256+ prop.resolve(&self.options, self.working_area.size.w) > current
1257+ })
1258+ .unwrap_or(0)
1259+ }
1260+ };
1261+ let width = ColumnWidth::Preset(idx);
1262+ self.set_width(width);
1263+ }
1264+
1265+ fn toggle_full_width(&mut self) {
1266+ self.is_full_width = !self.is_full_width;
1267+ self.update_window_sizes();
1268+ }
1269+
1270+ fn set_column_width(&mut self, change: SizeChange) {
1271+ let width = if self.is_full_width {
1272+ ColumnWidth::Proportion(1.)
1273+ } else {
1274+ self.width
1275+ };
1276+
1277+ let current_px = width.resolve(&self.options, self.working_area.size.w);
1278+
1279+ let current = match width {
1280+ ColumnWidth::Preset(idx) => self.options.preset_widths[idx],
1281+ current => current,
1282+ };
1283+
1284+ // FIXME: fix overflows then remove limits.
1285+ const MAX_PX: i32 = 100000;
1286+ const MAX_F: f64 = 10000.;
1287+
1288+ let width = match (current, change) {
1289+ (_, SizeChange::SetFixed(fixed)) => ColumnWidth::Fixed(fixed.clamp(1, MAX_PX)),
1290+ (_, SizeChange::SetProportion(proportion)) => {
1291+ ColumnWidth::Proportion((proportion / 100.).clamp(0., MAX_F))
1292+ }
1293+ (_, SizeChange::AdjustFixed(delta)) => {
1294+ let width = current_px.saturating_add(delta).clamp(1, MAX_PX);
1295+ ColumnWidth::Fixed(width)
1296+ }
1297+ (ColumnWidth::Proportion(current), SizeChange::AdjustProportion(delta)) => {
1298+ let proportion = (current + delta / 100.).clamp(0., MAX_F);
1299+ ColumnWidth::Proportion(proportion)
1300+ }
1301+ (ColumnWidth::Fixed(_), SizeChange::AdjustProportion(delta)) => {
1302+ let current = (current_px + self.options.gaps) as f64
1303+ / (self.working_area.size.w - self.options.gaps) as f64;
1304+ let proportion = (current + delta / 100.).clamp(0., MAX_F);
1305+ ColumnWidth::Proportion(proportion)
1306+ }
1307+ (ColumnWidth::Preset(_), _) => unreachable!(),
1308+ };
1309+
1310+ self.set_width(width);
1311+ }
1312+
1313+ fn set_window_height(&mut self, change: SizeChange) {
1314+ let current = self.heights[self.active_window_idx];
1315+ let current_px = match current {
1316+ WindowHeight::Auto => self.windows[self.active_window_idx].geometry().size.h,
1317+ WindowHeight::Fixed(height) => height,
1318+ };
1319+ let current_prop = (current_px + self.options.gaps) as f64
1320+ / (self.working_area.size.h - self.options.gaps) as f64;
1321+
1322+ // FIXME: fix overflows then remove limits.
1323+ const MAX_PX: i32 = 100000;
1324+
1325+ let mut height = match change {
1326+ SizeChange::SetFixed(fixed) => fixed,
1327+ SizeChange::SetProportion(proportion) => {
1328+ ((self.working_area.size.h - self.options.gaps) as f64 * proportion
1329+ - self.options.gaps as f64)
1330+ .round() as i32
1331+ }
1332+ SizeChange::AdjustFixed(delta) => current_px.saturating_add(delta),
1333+ SizeChange::AdjustProportion(delta) => {
1334+ let proportion = current_prop + delta / 100.;
1335+ ((self.working_area.size.h - self.options.gaps) as f64 * proportion
1336+ - self.options.gaps as f64)
1337+ .round() as i32
1338+ }
1339+ };
1340+
1341+ // Clamp it against the window height constraints.
1342+ let win = &self.windows[self.active_window_idx];
1343+ let min_h = win.min_size().h;
1344+ let max_h = win.max_size().h;
1345+
1346+ if max_h > 0 {
1347+ height = height.min(max_h);
1348+ }
1349+ if min_h > 0 {
1350+ height = height.max(min_h);
1351+ }
1352+
1353+ self.heights[self.active_window_idx] = WindowHeight::Fixed(height.clamp(1, MAX_PX));
1354+ self.update_window_sizes();
1355+ }
1356+
1357+ fn set_fullscreen(&mut self, is_fullscreen: bool) {
1358+ assert_eq!(self.windows.len(), 1);
1359+ self.is_fullscreen = is_fullscreen;
1360+ self.update_window_sizes();
1361+ }
1362+
1363+ pub fn window_y(&self, window_idx: usize) -> i32 {
1364+ self.window_ys().nth(window_idx).unwrap()
1365+ }
1366+
1367+ fn window_ys(&self) -> impl Iterator<Item = i32> + '_ {
1368+ let mut y = 0;
1369+
1370+ if !self.is_fullscreen {
1371+ y = self.working_area.loc.y + self.options.gaps;
1372+ }
1373+
1374+ self.windows.iter().map(move |win| {
1375+ let pos = y;
1376+ y += win.geometry().size.h + self.options.gaps;
1377+ pos
1378+ })
1379+ }
1380+}
1381+
1382+fn compute_new_view_offset(
1383+ cur_x: i32,
1384+ view_width: i32,
1385+ new_col_x: i32,
1386+ new_col_width: i32,
1387+ gaps: i32,
1388+) -> i32 {
1389+ // If the column is wider than the view, always left-align it.
1390+ if view_width <= new_col_width {
1391+ return 0;
1392+ }
1393+
1394+ // Compute the padding in case it needs to be smaller due to large window width.
1395+ let padding = ((view_width - new_col_width) / 2).clamp(0, gaps);
1396+
1397+ // Compute the desired new X with padding.
1398+ let new_x = new_col_x - padding;
1399+ let new_right_x = new_col_x + new_col_width + padding;
1400+
1401+ // If the column is already fully visible, leave the view as is.
1402+ if cur_x <= new_x && new_right_x <= cur_x + view_width {
1403+ return -(new_col_x - cur_x);
1404+ }
1405+
1406+ // Otherwise, prefer the aligment that results in less motion from the current position.
1407+ let dist_to_left = cur_x.abs_diff(new_x);
1408+ let dist_to_right = (cur_x + view_width).abs_diff(new_right_x);
1409+ if dist_to_left <= dist_to_right {
1410+ -padding
1411+ } else {
1412+ -(view_width - padding - new_col_width)
1413+ }
1414+}
1415+
1416+fn prepare_for_output(window: &impl LayoutElement, output: &Output) {
1417+ let scale = output.current_scale().integer_scale();
1418+ let transform = output.current_transform();
1419+ window.set_preferred_scale_transform(scale, transform);
1420+}
1421+
1422+pub fn compute_working_area(output: &Output, struts: Struts) -> Rectangle<i32, Logical> {
1423+ // Start with the layer-shell non-exclusive zone.
1424+ let mut working_area = layer_map_for_output(output).non_exclusive_zone();
1425+
1426+ // Add struts.
1427+ let w = working_area.size.w;
1428+ let h = working_area.size.h;
1429+
1430+ working_area.size.w = w
1431+ .saturating_sub(struts.left.into())
1432+ .saturating_sub(struts.right.into());
1433+ working_area.loc.x += struts.left as i32;
1434+
1435+ working_area.size.h = h
1436+ .saturating_sub(struts.top.into())
1437+ .saturating_sub(struts.bottom.into());
1438+ working_area.loc.y += struts.top as i32;
1439+
1440+ working_area
1441+}