forge
Explore
Log in

Don't you dare go Hollow

Refactor everything again, make more things work

Browse files at this commit

8f71842e7cf0c2e77183e99b954c9b511dca3576Ivan MolodetskikhParent: 95c810c
src/backend.rsmodified+1 -5
@@ -1,4 +1,3 @@
1-use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
21 use smithay::backend::renderer::gles::GlesRenderer;
32 use smithay::output::Output;
43
@@ -12,9 +11,6 @@ pub trait Backend {
1211 &mut self,
1312 niri: &mut Niri,
1413 output: &Output,
15- elements: &[OutputRenderElements<
16- GlesRenderer,
17- WaylandSurfaceRenderElement<GlesRenderer>,
18- >],
14+ elements: &[OutputRenderElements<GlesRenderer>],
1915 );
2016 }
src/handlers/compositor.rsmodified+5 -10
@@ -68,10 +68,8 @@ impl CompositorHandler for Niri {
6868 }
6969
7070 // This is a commit of a previously-mapped root or a non-toplevel root.
71- if let Some((window, space)) = self.monitor_set.find_window_and_space(surface) {
71+ if let Some((window, output)) = self.monitor_set.find_window_and_output(surface) {
7272 // This is a commit of a previously-mapped toplevel.
73- let output = space.outputs().next().unwrap().clone();
74-
7573 window.on_commit();
7674
7775 // This is a commit of a previously-mapped toplevel.
@@ -80,7 +78,6 @@ impl CompositorHandler for Niri {
8078
8179 if !is_mapped {
8280 // The toplevel got unmapped.
83- let window = window.clone();
8481 self.monitor_set.remove_window(&window);
8582 self.unmapped_windows.insert(surface.clone(), window);
8683 self.update_focus();
@@ -101,9 +98,8 @@ impl CompositorHandler for Niri {
10198 }
10299
103100 // This is a commit of a non-root or a non-toplevel root.
104- let root_window_space = self.monitor_set.find_window_and_space(&root_surface);
105- if let Some((window, space)) = root_window_space {
106- let output = space.outputs().next().unwrap().clone();
101+ let root_window_output = self.monitor_set.find_window_and_output(&root_surface);
102+ if let Some((window, output)) = root_window_output {
107103 window.on_commit();
108104 self.monitor_set.update_window(&window);
109105 self.queue_redraw(output);
@@ -114,9 +110,8 @@ impl CompositorHandler for Niri {
114110 self.popups_handle_commit(surface);
115111 if let Some(popup) = self.popups.find_popup(surface) {
116112 if let Ok(root) = find_popup_root_surface(&popup) {
117- let root_window_space = self.monitor_set.find_window_and_space(&root);
118- if let Some((_window, space)) = root_window_space {
119- let output = space.outputs().next().unwrap().clone();
113+ let root_window_output = self.monitor_set.find_window_and_output(&root);
114+ if let Some((_window, output)) = root_window_output {
120115 self.queue_redraw(output);
121116 }
122117 }
src/handlers/xdg_shell.rsmodified+48 -41
@@ -1,5 +1,5 @@
11 use smithay::delegate_xdg_shell;
2-use smithay::desktop::{PopupKind, Window};
2+use smithay::desktop::{find_popup_root_surface, PopupKind, Window};
33 use smithay::input::pointer::{Focus, GrabStartData as PointerGrabStartData};
44 use smithay::input::Seat;
55 use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
@@ -14,7 +14,7 @@ use smithay::wayland::shell::xdg::{
1414 };
1515
1616 use crate::grabs::{MoveSurfaceGrab, ResizeSurfaceGrab};
17-use crate::layout::MonitorSet;
17+use crate::layout::{configure_new_window, output_size};
1818 use crate::Niri;
1919
2020 impl XdgShellHandler for Niri {
@@ -28,7 +28,7 @@ impl XdgShellHandler for Niri {
2828
2929 // Tell the surface the preferred size and bounds for its likely output.
3030 let output = self.monitor_set.active_output().unwrap();
31- MonitorSet::configure_new_window(output, &window);
31+ configure_new_window(output_size(output), &window);
3232
3333 // At the moment of creation, xdg toplevels must have no buffer.
3434 let existing = self.unmapped_windows.insert(wl_surface, window);
@@ -49,24 +49,26 @@ impl XdgShellHandler for Niri {
4949 }
5050
5151 fn move_request(&mut self, surface: ToplevelSurface, seat: wl_seat::WlSeat, serial: Serial) {
52- let seat = Seat::from_resource(&seat).unwrap();
52+ // FIXME
5353
54- let wl_surface = surface.wl_surface();
54+ // let seat = Seat::from_resource(&seat).unwrap();
5555
56- if let Some(start_data) = check_grab(&seat, wl_surface, serial) {
57- let pointer = seat.get_pointer().unwrap();
56+ // let wl_surface = surface.wl_surface();
5857
59- let (window, space) = self.monitor_set.find_window_and_space(wl_surface).unwrap();
60- let initial_window_location = space.element_location(&window).unwrap();
58+ // if let Some(start_data) = check_grab(&seat, wl_surface, serial) {
59+ // let pointer = seat.get_pointer().unwrap();
6160
62- let grab = MoveSurfaceGrab {
63- start_data,
64- window: window.clone(),
65- initial_window_location,
66- };
61+ // let (window, space) = self.monitor_set.find_window_and_space(wl_surface).unwrap();
62+ // let initial_window_location = space.element_location(&window).unwrap();
6763
68- pointer.set_grab(self, grab, serial, Focus::Clear);
69- }
64+ // let grab = MoveSurfaceGrab {
65+ // start_data,
66+ // window: window.clone(),
67+ // initial_window_location,
68+ // };
69+
70+ // pointer.set_grab(self, grab, serial, Focus::Clear);
71+ // }
7072 }
7173
7274 fn resize_request(
@@ -76,32 +78,34 @@ impl XdgShellHandler for Niri {
7678 serial: Serial,
7779 edges: xdg_toplevel::ResizeEdge,
7880 ) {
79- let seat = Seat::from_resource(&seat).unwrap();
81+ // FIXME
8082
81- let wl_surface = surface.wl_surface();
83+ // let seat = Seat::from_resource(&seat).unwrap();
8284
83- if let Some(start_data) = check_grab(&seat, wl_surface, serial) {
84- let pointer = seat.get_pointer().unwrap();
85+ // let wl_surface = surface.wl_surface();
8586
86- let (window, space) = self.monitor_set.find_window_and_space(wl_surface).unwrap();
87- let initial_window_location = space.element_location(&window).unwrap();
88- let initial_window_size = window.geometry().size;
87+ // if let Some(start_data) = check_grab(&seat, wl_surface, serial) {
88+ // let pointer = seat.get_pointer().unwrap();
8989
90- surface.with_pending_state(|state| {
91- state.states.set(xdg_toplevel::State::Resizing);
92- });
90+ // let (window, space) = self.monitor_set.find_window_and_space(wl_surface).unwrap();
91+ // let initial_window_location = space.element_location(&window).unwrap();
92+ // let initial_window_size = window.geometry().size;
9393
94- surface.send_pending_configure();
94+ // surface.with_pending_state(|state| {
95+ // state.states.set(xdg_toplevel::State::Resizing);
96+ // });
9597
96- let grab = ResizeSurfaceGrab::start(
97- start_data,
98- window.clone(),
99- edges.into(),
100- Rectangle::from_loc_and_size(initial_window_location, initial_window_size),
101- );
98+ // surface.send_pending_configure();
10299
103- pointer.set_grab(self, grab, serial, Focus::Clear);
104- }
100+ // let grab = ResizeSurfaceGrab::start(
101+ // start_data,
102+ // window.clone(),
103+ // edges.into(),
104+ // Rectangle::from_loc_and_size(initial_window_location, initial_window_size),
105+ // );
106+
107+ // pointer.set_grab(self, grab, serial, Focus::Clear);
108+ // }
105109 }
106110
107111 fn reposition_request(
@@ -232,19 +236,22 @@ impl XdgShellHandler for Niri {
232236 return;
233237 }
234238
235- let (window, space) = self
239+ let (window, output) = self
236240 .monitor_set
237- .find_window_and_space(surface.wl_surface())
241+ .find_window_and_output(surface.wl_surface())
238242 .unwrap();
239- let output = space.outputs().next().unwrap().clone();
240243 self.monitor_set.remove_window(&window);
241244 self.update_focus();
242245 self.queue_redraw(output);
243246 }
244247
245- fn popup_destroyed(&mut self, _surface: PopupSurface) {
246- // FIXME granular
247- self.queue_redraw_all();
248+ fn popup_destroyed(&mut self, surface: PopupSurface) {
249+ if let Ok(root) = find_popup_root_surface(&surface.into()) {
250+ let root_window_output = self.monitor_set.find_window_and_output(&root);
251+ if let Some((_window, output)) = root_window_output {
252+ self.queue_redraw(output);
253+ }
254+ }
248255 }
249256 }
250257
src/input.rsmodified+6 -51
@@ -1,4 +1,3 @@
1-use std::cell::Cell;
21 use std::process::Command;
32
43 use smithay::backend::input::{
@@ -7,9 +6,7 @@ use smithay::backend::input::{
76 };
87 use smithay::input::keyboard::{keysyms, FilterResult, KeysymHandle, ModifiersState};
98 use smithay::input::pointer::{AxisFrame, ButtonEvent, MotionEvent, RelativeMotionEvent};
10-use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
119 use smithay::utils::SERIAL_COUNTER;
12-use smithay::wayland::shell::xdg::XdgShellHandler;
1310
1411 use crate::niri::Niri;
1512
@@ -140,54 +137,13 @@ impl Niri {
140137 }
141138 }
142139 Action::CloseWindow => {
143- if let Some(focus) = self.seat.get_keyboard().unwrap().current_focus() {
144- // FIXME: is there a better way of doing this?
145- for window in self
146- .monitor_set
147- .workspaces()
148- .flat_map(|workspace| workspace.space.elements())
149- {
150- let found = Cell::new(false);
151- window.with_surfaces(|surface, _| {
152- if surface == &focus {
153- found.set(true);
154- }
155- });
156- if found.get() {
157- window.toplevel().send_close();
158- break;
159- }
160- }
140+ if let Some(window) = self.monitor_set.focus() {
141+ window.toplevel().send_close();
161142 }
162143 }
163144 Action::ToggleFullscreen => {
164- if let Some(focus) = self.seat.get_keyboard().unwrap().current_focus() {
165- // FIXME: is there a better way of doing this?
166- let window = self
167- .monitor_set
168- .workspaces()
169- .flat_map(|workspace| workspace.space.elements())
170- .find(|window| {
171- let found = Cell::new(false);
172- window.with_surfaces(|surface, _| {
173- if surface == &focus {
174- found.set(true);
175- }
176- });
177- found.get()
178- });
179- if let Some(window) = window {
180- let toplevel = window.toplevel().clone();
181- if toplevel
182- .current_state()
183- .states
184- .contains(xdg_toplevel::State::Fullscreen)
185- {
186- self.unfullscreen_request(toplevel);
187- } else {
188- self.fullscreen_request(toplevel, None);
189- }
190- }
145+ if let Some(window) = self.monitor_set.focus() {
146+ // FIXME
191147 }
192148 }
193149 Action::MoveLeft => {
@@ -353,9 +309,8 @@ impl Niri {
353309 let button_state = event.state();
354310
355311 if ButtonState::Pressed == button_state && !pointer.is_grabbed() {
356- if let Some((_space, window, _loc)) =
357- self.window_under(pointer.current_location())
358- {
312+ if let Some(window) = self.window_under_cursor() {
313+ let window = window.clone();
359314 self.monitor_set.activate_window(&window);
360315 } else {
361316 let output = self.output_under_cursor().unwrap();
src/layout.rsmodified+669 -357
@@ -31,69 +31,113 @@
3131
3232 use std::cmp::{max, min};
3333 use std::mem;
34+use std::time::Duration;
3435
35-use smithay::desktop::{Space, Window};
36+use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
37+use smithay::backend::renderer::element::AsRenderElements;
38+use smithay::backend::renderer::gles::GlesRenderer;
39+use smithay::desktop::space::SpaceElement;
40+use smithay::desktop::Window;
3641 use smithay::output::Output;
3742 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
38-use smithay::utils::{Logical, Size};
43+use smithay::utils::{Logical, Point, Rectangle, Scale, Size};
44+use smithay::wayland::compositor::{with_states, SurfaceData};
45+use smithay::wayland::shell::xdg::XdgToplevelSurfaceData;
3946
4047 const PADDING: i32 = 16;
4148
4249 #[derive(Debug, Clone, PartialEq, Eq)]
4350 pub struct OutputId(String);
4451
52+pub trait LayoutElement: SpaceElement + PartialEq + Clone {
53+ fn request_size(&self, size: Size<i32, Logical>);
54+ fn send_pending_configure(&self);
55+ fn min_size(&self) -> Size<i32, Logical>;
56+ fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool;
57+ fn send_frame<T, F>(
58+ &self,
59+ output: &Output,
60+ time: T,
61+ throttle: Option<Duration>,
62+ primary_scan_out_output: F,
63+ ) where
64+ T: Into<Duration>,
65+ F: FnMut(&WlSurface, &SurfaceData) -> Option<Output> + Copy;
66+}
67+
4568 #[derive(Debug)]
46-pub enum MonitorSet {
69+pub enum MonitorSet<W: LayoutElement> {
4770 /// At least one output is connected.
4871 Normal {
49- monitors: Vec<Monitor>,
72+ /// Connected monitors.
73+ monitors: Vec<Monitor<W>>,
5074 /// Index of the primary monitor.
5175 primary_idx: usize,
5276 /// Index of the active monitor.
5377 active_monitor_idx: usize,
5478 },
5579 /// No outputs are connected, and these are the workspaces.
56- // FIXME: preserve active output id?
57- NoOutputs(Vec<Workspace>),
80+ NoOutputs(Vec<Workspace<W>>),
5881 }
5982
6083 #[derive(Debug)]
61-pub struct Monitor {
84+pub struct Monitor<W: LayoutElement> {
85+ /// Output for this monitor.
6286 output: Output,
6387 // Must always contain at least one.
64- workspaces: Vec<Workspace>,
88+ workspaces: Vec<Workspace<W>>,
6589 /// Index of the currently active workspace.
6690 active_workspace_idx: usize,
6791 }
6892
6993 #[derive(Debug)]
70-pub struct Workspace {
94+pub struct Workspace<W: LayoutElement> {
7195 /// The original output of this workspace.
7296 ///
7397 /// Most of the time this will be the workspace's current output, however, after an output
7498 /// disconnection, it may remain pointing to the disconnected output.
7599 original_output: OutputId,
76100
77- layout: Layout,
101+ /// Current output of this workspace.
102+ output: Option<Output>,
78103
79- // The actual Space with windows in this workspace. Should be synchronized to the layout except
80- // for a brief period during surface commit handling.
81- pub space: Space<Window>,
82-}
104+ /// Latest known view size for this workspace.
105+ ///
106+ /// This should be computed from the current workspace output size, or, if all outputs have
107+ /// been disconnected, preserved until a new output is connected.
108+ view_size: Size<i32, Logical>,
109+
110+ /// Columns of windows on this workspace.
111+ columns: Vec<Column<W>>,
83112
84-#[derive(Debug)]
85-pub struct Layout {
86- columns: Vec<Column>,
87113 /// Index of the currently active column, if any.
88114 active_column_idx: usize,
115+
116+ /// Offset of the view computed from the active column.
117+ view_offset: i32,
118+}
119+
120+/// Width of a column.
121+#[derive(Debug, Clone, Copy)]
122+enum ColumnWidth {
123+ /// Proportion of the current view width.
124+ Proportion(f64),
125+ /// Fixed width in logical pixels.
126+ Fixed(i32),
89127 }
90128
91129 #[derive(Debug)]
92-pub struct Column {
93- // Must be non-empty.
94- windows: Vec<Window>,
130+struct Column<W: LayoutElement> {
131+ /// Windows in this column.
132+ ///
133+ /// Must be non-empty.
134+ windows: Vec<W>,
135+
95136 /// Index of the currently active window.
96137 active_window_idx: usize,
138+
139+ /// Desired width of this column.
140+ width: ColumnWidth,
97141 }
98142
99143 impl OutputId {
@@ -102,7 +146,65 @@ impl OutputId {
102146 }
103147 }
104148
105-impl MonitorSet {
149+impl LayoutElement for Window {
150+ fn request_size(&self, size: Size<i32, Logical>) {
151+ let toplevel = &self.toplevel();
152+ toplevel.with_pending_state(|state| {
153+ state.size = Some(size);
154+ });
155+ toplevel.send_pending_configure();
156+ }
157+
158+ fn send_pending_configure(&self) {
159+ self.toplevel().send_pending_configure();
160+ }
161+
162+ fn min_size(&self) -> Size<i32, Logical> {
163+ with_states(self.toplevel().wl_surface(), |state| {
164+ state
165+ .data_map
166+ .get::<XdgToplevelSurfaceData>()
167+ .unwrap()
168+ .lock()
169+ .unwrap()
170+ .min_size
171+ })
172+ }
173+
174+ fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool {
175+ self.toplevel().wl_surface() == wl_surface
176+ }
177+
178+ fn send_frame<T, F>(
179+ &self,
180+ output: &Output,
181+ time: T,
182+ throttle: Option<Duration>,
183+ primary_scan_out_output: F,
184+ ) where
185+ T: Into<Duration>,
186+ F: FnMut(&WlSurface, &SurfaceData) -> Option<Output> + Copy,
187+ {
188+ self.send_frame(output, time, throttle, primary_scan_out_output);
189+ }
190+}
191+
192+impl ColumnWidth {
193+ fn resolve(self, view_width: i32) -> i32 {
194+ match self {
195+ ColumnWidth::Proportion(proportion) => (view_width as f64 * proportion).floor() as i32,
196+ ColumnWidth::Fixed(width) => width,
197+ }
198+ }
199+}
200+
201+impl Default for ColumnWidth {
202+ fn default() -> Self {
203+ Self::Proportion(0.5)
204+ }
205+}
206+
207+impl<W: LayoutElement> MonitorSet<W> {
106208 pub fn new() -> Self {
107209 Self::NoOutputs(vec![])
108210 }
@@ -121,26 +223,18 @@ impl MonitorSet {
121223 let mut workspaces = vec![];
122224 for i in (0..primary.workspaces.len()).rev() {
123225 if primary.workspaces[i].original_output == id {
124- let mut ws = primary.workspaces.remove(i);
125- ws.space.unmap_output(&primary.output);
226+ let ws = primary.workspaces.remove(i);
126227 workspaces.push(ws);
127228 }
128229 }
129230 workspaces.reverse();
130- if workspaces
131- .iter()
132- .all(|ws| ws.space.elements().next().is_some())
133- {
231+ if workspaces.iter().all(|ws| ws.has_windows()) {
134232 // Make sure there's always an empty workspace.
135- workspaces.push(Workspace {
136- original_output: id,
137- layout: Layout::new(),
138- space: Space::default(),
139- });
233+ workspaces.push(Workspace::new(output.clone()));
140234 }
141235
142236 for ws in &mut workspaces {
143- ws.space.map_output(&output, (0, 0));
237+ ws.set_output(Some(output.clone()));
144238 }
145239
146240 monitors.push(Monitor {
@@ -155,19 +249,11 @@ impl MonitorSet {
155249 }
156250 }
157251 MonitorSet::NoOutputs(mut workspaces) => {
158- if workspaces.iter().all(|ws| ws.original_output != id) {
159- workspaces.insert(
160- 0,
161- Workspace {
162- original_output: id.clone(),
163- layout: Layout::new(),
164- space: Space::default(),
165- },
166- );
167- }
252+ // We know there are no empty workspaces there, so add one.
253+ workspaces.push(Workspace::new(output.clone()));
168254
169255 for workspace in &mut workspaces {
170- workspace.space.map_output(&output, (0, 0));
256+ workspace.set_output(Some(output.clone()));
171257 }
172258
173259 let monitor = Monitor {
@@ -199,11 +285,11 @@ impl MonitorSet {
199285 let mut workspaces = monitor.workspaces;
200286
201287 for ws in &mut workspaces {
202- ws.space.unmap_output(output);
288+ ws.set_output(None);
203289 }
204290
205291 // Get rid of empty workspaces.
206- workspaces.retain(|ws| ws.space.elements().next().is_some());
292+ workspaces.retain(|ws| ws.has_windows());
207293
208294 if monitors.is_empty() {
209295 // Removed the last monitor.
@@ -223,9 +309,12 @@ impl MonitorSet {
223309
224310 let primary = &mut monitors[primary_idx];
225311 for ws in &mut workspaces {
226- ws.space.map_output(&primary.output, (0, 0));
312+ ws.set_output(Some(primary.output.clone()));
227313 }
314+
315+ let empty = primary.workspaces.remove(primary.workspaces.len() - 1);
228316 primary.workspaces.extend(workspaces);
317+ primary.workspaces.push(empty);
229318
230319 MonitorSet::Normal {
231320 monitors,
@@ -240,25 +329,11 @@ impl MonitorSet {
240329 }
241330 }
242331
243- pub fn configure_new_window(output: &Output, window: &Window) {
244- let output_size = output_size(output);
245- let size = Size::from((
246- (output_size.w - PADDING * 3) / 2,
247- output_size.h - PADDING * 2,
248- ));
249- let bounds = Size::from((output_size.w - PADDING * 2, output_size.h - PADDING * 2));
250-
251- window.toplevel().with_pending_state(|state| {
252- state.size = Some(size);
253- state.bounds = Some(bounds);
254- });
255- }
256-
257332 pub fn add_window(
258333 &mut self,
259334 monitor_idx: usize,
260335 workspace_idx: usize,
261- window: Window,
336+ window: W,
262337 activate: bool,
263338 ) {
264339 let MonitorSet::Normal {
@@ -272,31 +347,24 @@ impl MonitorSet {
272347
273348 let monitor = &mut monitors[monitor_idx];
274349 let workspace = &mut monitor.workspaces[workspace_idx];
275- workspace.layout.add_window(window.clone(), activate);
276- workspace.space.map_element(window.clone(), (0, 0), false);
277- workspace.layout.sync_space(&mut workspace.space);
278-
279- MonitorSet::configure_new_window(&monitor.output, &window);
280- window.toplevel().send_pending_configure();
281350
282351 if activate {
283352 *active_monitor_idx = monitor_idx;
284353 monitor.active_workspace_idx = workspace_idx;
354+ // Configure will be sent in add_window().
355+ window.set_activate(true);
285356 }
286357
358+ workspace.add_window(window.clone(), activate);
359+
287360 if workspace_idx == monitor.workspaces.len() - 1 {
288361 // Insert a new empty workspace.
289- let mut ws = Workspace {
290- original_output: OutputId::new(&monitor.output),
291- layout: Layout::new(),
292- space: Space::default(),
293- };
294- ws.space.map_output(&monitor.output, (0, 0));
362+ let ws = Workspace::new(monitor.output.clone());
295363 monitor.workspaces.push(ws);
296364 }
297365 }
298366
299- pub fn add_window_to_output(&mut self, output: &Output, window: Window, activate: bool) {
367+ pub fn add_window_to_output(&mut self, output: &Output, window: W, activate: bool) {
300368 let MonitorSet::Normal { monitors, .. } = self else {
301369 panic!()
302370 };
@@ -308,38 +376,110 @@ impl MonitorSet {
308376 .unwrap();
309377 let workspace_idx = monitor.active_workspace_idx;
310378
311- self.add_window(monitor_idx, workspace_idx, window, activate)
379+ self.add_window(monitor_idx, workspace_idx, window, activate);
312380 }
313381
314- pub fn remove_window(&mut self, window: &Window) {
315- let MonitorSet::Normal { monitors, .. } = self else {
316- panic!()
317- };
382+ pub fn remove_window(&mut self, window: &W) {
383+ match self {
384+ MonitorSet::Normal { monitors, .. } => {
385+ for mon in monitors {
386+ for (idx, ws) in mon.workspaces.iter_mut().enumerate() {
387+ if ws.has_window(window) {
388+ ws.remove_window(window);
389+
390+ // Clean up empty workspaces that are not active and not last.
391+ if !ws.has_windows()
392+ && idx != mon.active_workspace_idx
393+ && idx != mon.workspaces.len() - 1
394+ {
395+ mon.workspaces.remove(idx);
396+ }
397+
398+ break;
399+ }
400+ }
401+ }
402+ }
403+ MonitorSet::NoOutputs(workspaces) => {
404+ for (idx, ws) in workspaces.iter_mut().enumerate() {
405+ if ws.has_window(window) {
406+ ws.remove_window(window);
318407
319- let (output, workspace) = monitors
320- .iter_mut()
321- .flat_map(|mon| mon.workspaces.iter_mut().map(|ws| (&mon.output, ws)))
322- .find(|(_, ws)| ws.space.elements().any(|win| win == window))
323- .unwrap();
408+ // Clean up empty workspaces.
409+ if !ws.has_windows() {
410+ workspaces.remove(idx);
411+ }
324412
325- workspace
326- .layout
327- .remove_window(window, output_size(output).h);
328- workspace.space.unmap_elem(window);
329- workspace.layout.sync_space(&mut workspace.space);
413+ break;
414+ }
415+ }
416+ }
417+ }
418+ }
330419
331- // FIXME: remove empty unfocused workspaces.
420+ pub fn update_window(&mut self, window: &W) {
421+ match self {
422+ MonitorSet::Normal { monitors, .. } => {
423+ for mon in monitors {
424+ for ws in &mut mon.workspaces {
425+ if ws.has_window(window) {
426+ ws.update_window(window);
427+ break;
428+ }
429+ }
430+ }
431+ }
432+ MonitorSet::NoOutputs(workspaces) => {
433+ for ws in workspaces {
434+ if ws.has_window(window) {
435+ ws.update_window(window);
436+ break;
437+ }
438+ }
439+ }
440+ }
332441 }
333442
334- pub fn update_window(&mut self, window: &Window) {
335- let workspace = self
336- .workspaces()
337- .find(|ws| ws.space.elements().any(|w| w == window))
338- .unwrap();
339- workspace.layout.sync_space(&mut workspace.space);
443+ pub fn send_frame(&self, output: &Output, time: Duration) {
444+ if let MonitorSet::Normal { monitors, .. } = self {
445+ for mon in monitors {
446+ if &mon.output == output {
447+ mon.workspaces[mon.active_workspace_idx].send_frame(time);
448+ }
449+ }
450+ }
340451 }
341452
342- pub fn activate_window(&mut self, window: &Window) {
453+ pub fn find_window_and_output(&mut self, wl_surface: &WlSurface) -> Option<(W, Output)> {
454+ if let MonitorSet::Normal { monitors, .. } = self {
455+ for mon in monitors {
456+ for ws in &mut mon.workspaces {
457+ if let Some(window) = ws.find_wl_surface(wl_surface) {
458+ return Some((window.clone(), mon.output.clone()));
459+ }
460+ }
461+ }
462+ }
463+
464+ None
465+ }
466+
467+ pub fn update_output(&mut self, output: &Output) {
468+ let MonitorSet::Normal { monitors, .. } = self else {
469+ panic!()
470+ };
471+
472+ for mon in monitors {
473+ if &mon.output == output {
474+ for ws in &mut mon.workspaces {
475+ ws.set_view_size(output_size(output));
476+ }
477+ break;
478+ }
479+ }
480+ }
481+
482+ pub fn activate_window(&mut self, window: &W) {
343483 let MonitorSet::Normal {
344484 monitors,
345485 active_monitor_idx,
@@ -351,15 +491,10 @@ impl MonitorSet {
351491
352492 for (monitor_idx, mon) in monitors.iter_mut().enumerate() {
353493 for (workspace_idx, ws) in mon.workspaces.iter_mut().enumerate() {
354- if ws.space.elements().any(|win| win == window) {
494+ if ws.has_window(window) {
355495 *active_monitor_idx = monitor_idx;
356496 mon.active_workspace_idx = workspace_idx;
357-
358- let changed = ws.layout.activate_window(window);
359- if changed {
360- ws.layout.sync_space(&mut ws.space);
361- }
362-
497+ ws.activate_window(window);
363498 break;
364499 }
365500 }
@@ -396,12 +531,12 @@ impl MonitorSet {
396531 Some(&monitors[*active_monitor_idx].output)
397532 }
398533
399- fn active_workspace(&mut self) -> Option<&mut Workspace> {
534+ fn active_workspace(&mut self) -> Option<&mut Workspace<W>> {
400535 let monitor = self.active_monitor()?;
401536 Some(&mut monitor.workspaces[monitor.active_workspace_idx])
402537 }
403538
404- fn active_monitor(&mut self) -> Option<&mut Monitor> {
539+ fn active_monitor(&mut self) -> Option<&mut Monitor<W>> {
405540 let MonitorSet::Normal {
406541 monitors,
407542 active_monitor_idx,
@@ -418,68 +553,56 @@ impl MonitorSet {
418553 let Some(workspace) = self.active_workspace() else {
419554 return;
420555 };
421- let changed = workspace.layout.move_left();
422- if changed {
423- workspace.layout.sync_space(&mut workspace.space);
424- }
556+ workspace.move_left();
425557 }
426558
427559 pub fn move_right(&mut self) {
428560 let Some(workspace) = self.active_workspace() else {
429561 return;
430562 };
431- let changed = workspace.layout.move_right();
432- if changed {
433- workspace.layout.sync_space(&mut workspace.space);
434- }
563+ workspace.move_right();
435564 }
436565
437566 pub fn move_down(&mut self) {
438567 let Some(workspace) = self.active_workspace() else {
439568 return;
440569 };
441- let changed = workspace.layout.move_down();
442- if changed {
443- workspace.layout.sync_space(&mut workspace.space);
444- }
570+ workspace.move_down();
445571 }
446572
447573 pub fn move_up(&mut self) {
448574 let Some(workspace) = self.active_workspace() else {
449575 return;
450576 };
451- let changed = workspace.layout.move_up();
452- if changed {
453- workspace.layout.sync_space(&mut workspace.space);
454- }
577+ workspace.move_up();
455578 }
456579
457580 pub fn focus_left(&mut self) {
458581 let Some(workspace) = self.active_workspace() else {
459582 return;
460583 };
461- workspace.layout.focus_left();
584+ workspace.focus_left();
462585 }
463586
464587 pub fn focus_right(&mut self) {
465588 let Some(workspace) = self.active_workspace() else {
466589 return;
467590 };
468- workspace.layout.focus_right();
591+ workspace.focus_right();
469592 }
470593
471594 pub fn focus_down(&mut self) {
472595 let Some(workspace) = self.active_workspace() else {
473596 return;
474597 };
475- workspace.layout.focus_down();
598+ workspace.focus_down();
476599 }
477600
478601 pub fn focus_up(&mut self) {
479602 let Some(workspace) = self.active_workspace() else {
480603 return;
481604 };
482- workspace.layout.focus_up();
605+ workspace.focus_up();
483606 }
484607
485608 pub fn move_to_workspace_up(&mut self) {
@@ -493,28 +616,27 @@ impl MonitorSet {
493616 };
494617
495618 let monitor = &mut monitors[*active_monitor_idx];
619+ let source_workspace_idx = monitor.active_workspace_idx;
496620
497- let new_idx = monitor.active_workspace_idx.saturating_sub(1);
498- if new_idx == monitor.active_workspace_idx {
621+ let new_idx = source_workspace_idx.saturating_sub(1);
622+ if new_idx == source_workspace_idx {
499623 return;
500624 }
501625
502- let workspace = &mut monitor.workspaces[monitor.active_workspace_idx];
503- if workspace.layout.columns.is_empty() {
626+ let workspace = &mut monitor.workspaces[source_workspace_idx];
627+ if workspace.columns.is_empty() {
504628 return;
505629 }
506630
507- let column = &mut workspace.layout.columns[workspace.layout.active_column_idx];
631+ let column = &mut workspace.columns[workspace.active_column_idx];
508632 let window = column.windows[column.active_window_idx].clone();
509- workspace
510- .layout
511- .remove_window(&window, output_size(&monitor.output).h);
512- workspace.space.unmap_elem(&window);
513- workspace.layout.sync_space(&mut workspace.space);
633+ workspace.remove_window(&window);
514634
515- self.add_window(*active_monitor_idx, new_idx, window, true);
635+ if !workspace.has_windows() && source_workspace_idx != monitor.workspaces.len() - 1 {
636+ monitor.workspaces.remove(source_workspace_idx);
637+ }
516638
517- // FIXME: remove empty unfocused workspaces.
639+ self.add_window(*active_monitor_idx, new_idx, window, true);
518640 }
519641
520642 pub fn move_to_workspace_down(&mut self) {
@@ -528,32 +650,28 @@ impl MonitorSet {
528650 };
529651
530652 let monitor = &mut monitors[*active_monitor_idx];
653+ let source_workspace_idx = monitor.active_workspace_idx;
531654
532- let new_idx = min(
533- monitor.active_workspace_idx + 1,
534- monitor.workspaces.len() - 1,
535- );
536-
537- if new_idx == monitor.active_workspace_idx {
655+ let mut new_idx = min(source_workspace_idx + 1, monitor.workspaces.len() - 1);
656+ if new_idx == source_workspace_idx {
538657 return;
539658 }
540659
541- let workspace = &mut monitor.workspaces[monitor.active_workspace_idx];
542- if workspace.layout.columns.is_empty() {
660+ let workspace = &mut monitor.workspaces[source_workspace_idx];
661+ if workspace.columns.is_empty() {
543662 return;
544663 }
545664
546- let column = &mut workspace.layout.columns[workspace.layout.active_column_idx];
665+ let column = &mut workspace.columns[workspace.active_column_idx];
547666 let window = column.windows[column.active_window_idx].clone();
548- workspace
549- .layout
550- .remove_window(&window, output_size(&monitor.output).h);
551- workspace.space.unmap_elem(&window);
552- workspace.layout.sync_space(&mut workspace.space);
667+ workspace.remove_window(&window);
553668
554- self.add_window(*active_monitor_idx, new_idx, window, true);
669+ if !workspace.has_windows() {
670+ monitor.workspaces.remove(source_workspace_idx);
671+ new_idx -= 1;
672+ }
555673
556- // FIXME: remove empty unfocused workspaces.
674+ self.add_window(*active_monitor_idx, new_idx, window, true);
557675 }
558676
559677 pub fn switch_workspace_up(&mut self) {
@@ -561,21 +679,40 @@ impl MonitorSet {
561679 return;
562680 };
563681
564- monitor.active_workspace_idx = monitor.active_workspace_idx.saturating_sub(1);
682+ let source_workspace_idx = monitor.active_workspace_idx;
683+
684+ let new_idx = source_workspace_idx.saturating_sub(1);
685+ if new_idx == source_workspace_idx {
686+ return;
687+ }
565688
566- // FIXME: remove empty unfocused workspaces.
689+ monitor.active_workspace_idx = new_idx;
690+
691+ if !monitor.workspaces[source_workspace_idx].has_windows()
692+ && source_workspace_idx != monitor.workspaces.len() - 1
693+ {
694+ monitor.workspaces.remove(source_workspace_idx);
695+ }
567696 }
568697
569698 pub fn switch_workspace_down(&mut self) {
570699 let Some(monitor) = self.active_monitor() else {
571700 return;
572701 };
573- monitor.active_workspace_idx = min(
574- monitor.active_workspace_idx + 1,
575- monitor.workspaces.len() - 1,
576- );
577702
578- // FIXME: remove empty unfocused workspaces.
703+ let source_workspace_idx = monitor.active_workspace_idx;
704+
705+ let mut new_idx = min(source_workspace_idx + 1, monitor.workspaces.len() - 1);
706+ if new_idx == source_workspace_idx {
707+ return;
708+ }
709+
710+ if !monitor.workspaces[source_workspace_idx].has_windows() {
711+ monitor.workspaces.remove(source_workspace_idx);
712+ new_idx -= 1;
713+ }
714+
715+ monitor.active_workspace_idx = new_idx;
579716 }
580717
581718 pub fn consume_into_column(&mut self) {
@@ -591,12 +728,7 @@ impl MonitorSet {
591728 let monitor = &mut monitors[*active_monitor_idx];
592729
593730 let workspace = &mut monitor.workspaces[monitor.active_workspace_idx];
594- let changed = workspace
595- .layout
596- .consume_into_column(output_size(&monitor.output).h);
597- if changed {
598- workspace.layout.sync_space(&mut workspace.space);
599- }
731+ workspace.consume_into_column();
600732 }
601733
602734 pub fn expel_from_column(&mut self) {
@@ -610,22 +742,11 @@ impl MonitorSet {
610742 };
611743
612744 let monitor = &mut monitors[*active_monitor_idx];
613-
614- let output_scale = monitor.output.current_scale().integer_scale();
615- let output_transform = monitor.output.current_transform();
616- let output_mode = monitor.output.current_mode().unwrap();
617- let output_size = output_transform
618- .transform_size(output_mode.size)
619- .to_logical(output_scale);
620-
621745 let workspace = &mut monitor.workspaces[monitor.active_workspace_idx];
622- let changed = workspace.layout.expel_from_column(output_size.h);
623- if changed {
624- workspace.layout.sync_space(&mut workspace.space);
625- }
746+ workspace.expel_from_column();
626747 }
627748
628- pub fn focus(&self) -> Option<&Window> {
749+ pub fn focus(&self) -> Option<&W> {
629750 let MonitorSet::Normal {
630751 monitors,
631752 active_monitor_idx,
@@ -637,65 +758,40 @@ impl MonitorSet {
637758
638759 let monitor = &monitors[*active_monitor_idx];
639760 let workspace = &monitor.workspaces[monitor.active_workspace_idx];
640- if workspace.layout.columns.is_empty() {
761+ if !workspace.has_windows() {
641762 return None;
642763 }
643764
644- let column = &workspace.layout.columns[workspace.layout.active_column_idx];
765+ let column = &workspace.columns[workspace.active_column_idx];
645766 Some(&column.windows[column.active_window_idx])
646767 }
647768
648- pub fn workspace_for_output(&mut self, output: &Output) -> Option<&mut Workspace> {
769+ pub fn workspace_for_output(&self, output: &Output) -> Option<&Workspace<W>> {
649770 let MonitorSet::Normal { monitors, .. } = self else {
650771 return None;
651772 };
652773
653- monitors.iter_mut().find_map(|monitor| {
774+ monitors.iter().find_map(|monitor| {
654775 if &monitor.output == output {
655- Some(&mut monitor.workspaces[monitor.active_workspace_idx])
776+ Some(&monitor.workspaces[monitor.active_workspace_idx])
656777 } else {
657778 None
658779 }
659780 })
660781 }
661782
662- pub fn workspaces(&mut self) -> impl Iterator<Item = &mut Workspace> + '_ {
663- match self {
664- MonitorSet::Normal { monitors, .. } => {
665- monitors.iter_mut().flat_map(|mon| &mut mon.workspaces)
666- }
667- MonitorSet::NoOutputs(_workspaces) => todo!(),
668- }
669- }
670-
671- pub fn spaces(&mut self) -> impl Iterator<Item = &Space<Window>> + '_ {
672- self.workspaces().map(|workspace| &workspace.space)
673- }
674-
675- pub fn find_window(&mut self, wl_surface: &WlSurface) -> Option<&Window> {
676- self.workspaces()
677- .flat_map(|workspace| workspace.space.elements())
678- .find(|window| window.toplevel().wl_surface() == wl_surface)
679- }
680-
681- pub fn find_window_and_space(
682- &mut self,
683- wl_surface: &WlSurface,
684- ) -> Option<(Window, &Space<Window>)> {
685- self.spaces().find_map(|space| {
686- let window = space
687- .elements()
688- .find(|window| window.toplevel().wl_surface() == wl_surface)
689- .cloned();
690- window.map(|window| (window, space))
691- })
783+ pub fn window_under(
784+ &self,
785+ output: &Output,
786+ pos_within_output: Point<f64, Logical>,
787+ ) -> Option<(&W, Point<i32, Logical>)> {
788+ let ws = self.workspace_for_output(output).unwrap();
789+ ws.window_under(pos_within_output)
692790 }
693791
694- /// Refreshes the `Space`s.
792+ /// Refreshes the `Workspace`s.
695793 pub fn refresh(&mut self) {
696- for workspace in self.workspaces() {
697- workspace.space.refresh();
698- }
794+ // TODO
699795 }
700796
701797 fn verify_invariants(&self) {
@@ -708,11 +804,11 @@ impl MonitorSet {
708804 MonitorSet::NoOutputs(workspaces) => {
709805 for workspace in workspaces {
710806 assert!(
711- !workspace.layout.has_windows(),
807+ !workspace.has_windows(),
712808 "with no outputs there cannot be empty workspaces"
713809 );
714810
715- workspace.layout.verify_invariants();
811+ workspace.verify_invariants();
716812 }
717813
718814 return;
@@ -731,13 +827,6 @@ impl MonitorSet {
731827 let monitor_id = OutputId::new(&monitor.output);
732828
733829 if idx == primary_idx {
734- assert!(
735- monitor
736- .workspaces
737- .iter()
738- .any(|workspace| workspace.original_output == monitor_id),
739- "primary monitor must have at least one own workspace"
740- );
741830 } else {
742831 assert!(
743832 monitor
@@ -752,76 +841,102 @@ impl MonitorSet {
752841 // exists.
753842
754843 for workspace in &monitor.workspaces {
755- workspace.layout.verify_invariants();
844+ workspace.verify_invariants();
756845 }
757846 }
758847 }
759848 }
760849
761-fn output_size(output: &Output) -> Size<i32, Logical> {
762- let output_scale = output.current_scale().integer_scale();
763- let output_transform = output.current_transform();
764- let output_mode = output.current_mode().unwrap();
765- let output_size = output_transform
766- .transform_size(output_mode.size)
767- .to_logical(output_scale);
768- output_size
850+impl MonitorSet<Window> {
851+ pub fn render_elements(
852+ &self,
853+ renderer: &mut GlesRenderer,
854+ output: &Output,
855+ ) -> Vec<WaylandSurfaceRenderElement<GlesRenderer>> {
856+ let ws = self.workspace_for_output(output).unwrap();
857+ ws.render_elements(renderer)
858+ }
769859 }
770860
771-impl Default for MonitorSet {
861+impl<W: LayoutElement> Default for MonitorSet<W> {
772862 fn default() -> Self {
773863 Self::new()
774864 }
775865 }
776866
777-impl Layout {
778- fn new() -> Self {
867+impl<W: LayoutElement> Workspace<W> {
868+ fn new(output: Output) -> Self {
779869 Self {
870+ original_output: OutputId::new(&output),
871+ view_size: output_size(&output),
872+ output: Some(output),
780873 columns: vec![],
781874 active_column_idx: 0,
875+ view_offset: 0,
782876 }
783877 }
784878
785- fn sync_space(&self, space: &mut Space<Window>) {
786- // FIXME: this is really inefficient
787- let mut active_window = None;
879+ fn refresh(&self) {
880+ // FIXME: proper overlap.
881+ }
788882
789- let mut x = PADDING;
790- for (column_idx, column) in self.columns.iter().enumerate() {
791- let mut y = PADDING;
792- for (window_idx, window) in column.windows.iter().enumerate() {
793- let active =
794- column_idx == self.active_column_idx && window_idx == column.active_window_idx;
795- if active {
796- active_window = Some(window.clone());
797- }
883+ fn windows(&self) -> impl Iterator<Item = &W> + '_ {
884+ self.columns.iter().flat_map(|col| col.windows.iter())
885+ }
798886
799- window.set_activated(active);
800- space.map_element(window.clone(), (x, y), false);
801- window.toplevel().send_pending_configure();
802- y += window.geometry().size.h + PADDING;
887+ fn set_output(&mut self, output: Option<Output>) {
888+ if self.output == output {
889+ return;
890+ }
891+
892+ if let Some(output) = self.output.take() {
893+ for win in self.windows() {
894+ win.output_leave(&output);
803895 }
804- x += column.size().w + PADDING;
805896 }
806897
807- if let Some(window) = active_window {
808- space.raise_element(&window, false);
898+ if let Some(output) = output {
899+ self.set_view_size(output_size(&output));
900+
901+ self.output = Some(output);
902+
903+ for win in self.windows() {
904+ self.enter_output_for_window(win);
905+ }
809906 }
810907 }
811908
812- fn has_windows(&self) -> bool {
813- self.columns.is_empty()
909+ fn enter_output_for_window(&self, window: &W) {
910+ if let Some(output) = &self.output {
911+ // FIXME: proper overlap.
912+ window.output_enter(
913+ output,
914+ Rectangle::from_loc_and_size((0, 0), (i32::MAX, i32::MAX)),
915+ );
916+ }
814917 }
815918
816- /// Computes the width of the layout including left and right padding, in Logical coordinates.
817- fn width(&self) -> i32 {
818- let mut total = PADDING;
919+ fn set_view_size(&mut self, size: Size<i32, Logical>) {
920+ if self.view_size == size {
921+ return;
922+ }
819923
820- for column in &self.columns {
821- total += column.size().w + PADDING;
924+ self.view_size = size;
925+ for col in &mut self.columns {
926+ col.update_window_sizes(self.view_size);
822927 }
928+ }
823929
824- total
930+ fn has_windows(&self) -> bool {
931+ self.windows().next().is_some()
932+ }
933+
934+ fn has_window(&self, window: &W) -> bool {
935+ self.windows().any(|win| win == window)
936+ }
937+
938+ fn find_wl_surface(&self, wl_surface: &WlSurface) -> Option<&W> {
939+ self.windows().find(|win| win.is_wl_surface(wl_surface))
825940 }
826941
827942 /// Computes the X position of the windows in the given column, in logical coordinates.
@@ -835,17 +950,25 @@ impl Layout {
835950 x
836951 }
837952
838- fn add_window(&mut self, window: Window, activate: bool) {
953+ fn add_window(&mut self, window: W, activate: bool) {
954+ self.enter_output_for_window(&window);
955+ // Configure will be sent in Column::new().
956+ window.set_activate(activate);
957+
958+ if activate {
959+ for win in self.windows() {
960+ win.set_activate(false);
961+ win.send_pending_configure();
962+ }
963+ }
964+
839965 let idx = if self.columns.is_empty() {
840966 0
841967 } else {
842968 self.active_column_idx + 1
843969 };
844970
845- let column = Column {
846- windows: vec![window],
847- active_window_idx: 0,
848- };
971+ let column = Column::new(window, self.view_size);
849972 self.columns.insert(idx, column);
850973
851974 if activate {
@@ -853,11 +976,15 @@ impl Layout {
853976 }
854977 }
855978
856- fn remove_window(&mut self, window: &Window, total_height: i32) {
979+ fn remove_window(&mut self, window: &W) {
980+ if let Some(output) = &self.output {
981+ window.output_leave(output);
982+ }
983+
857984 let column_idx = self
858985 .columns
859986 .iter()
860- .position(|col| col.windows.contains(window))
987+ .position(|col| col.contains(window))
861988 .unwrap();
862989 let column = &mut self.columns[column_idx];
863990
@@ -870,51 +997,79 @@ impl Layout {
870997 }
871998
872999 self.active_column_idx = min(self.active_column_idx, self.columns.len() - 1);
1000+ let column = &self.columns[self.active_column_idx];
1001+ let window = &column.windows[column.active_window_idx];
1002+ window.set_activate(true);
1003+ window.send_pending_configure();
8731004 return;
8741005 }
8751006
8761007 column.active_window_idx = min(column.active_window_idx, column.windows.len() - 1);
1008+ if self.active_column_idx == column_idx {
1009+ let window = &column.windows[column.active_window_idx];
1010+ window.set_activate(true);
1011+ }
1012+ column.update_window_sizes(self.view_size);
1013+ }
8771014
878- // Update window sizes.
879- let window_count = column.windows.len() as i32;
880- let height = (total_height - PADDING * (window_count + 1)) / window_count;
881- let width = column.size().w;
1015+ fn update_window(&mut self, window: &W) {
1016+ let column = self
1017+ .columns
1018+ .iter_mut()
1019+ .find(|col| col.contains(window))
1020+ .unwrap();
1021+ column.update_window_sizes(self.view_size);
1022+ }
8821023
883- for window in &mut column.windows {
884- window
885- .toplevel()
886- .with_pending_state(|state| state.size = Some(Size::from((width, height))));
887- window.toplevel().send_pending_configure();
1024+ fn activate_window(&mut self, window: &W) {
1025+ for win in self.windows() {
1026+ if win != window {
1027+ win.set_activate(false);
1028+ win.send_pending_configure();
1029+ }
8881030 }
889- }
1031+ window.set_activate(true);
1032+ window.send_pending_configure();
8901033
891- fn activate_window(&mut self, window: &Window) -> bool {
8921034 let column_idx = self
8931035 .columns
8941036 .iter()
895- .position(|col| col.windows.contains(window))
1037+ .position(|col| col.contains(window))
8961038 .unwrap();
8971039 let column = &mut self.columns[column_idx];
8981040
899- let window_idx = column.windows.iter().position(|win| win == window).unwrap();
900-
901- if column.active_window_idx != window_idx || self.active_column_idx != column_idx {
902- column.active_window_idx = window_idx;
903- self.active_column_idx = column_idx;
904- true
905- } else {
906- false
907- }
1041+ column.activate_window(window);
1042+ self.active_column_idx = column_idx;
9081043 }
9091044
9101045 fn verify_invariants(&self) {
1046+ assert!(self.view_size.w > 0);
1047+ assert!(self.view_size.h > 0);
1048+
1049+ assert!(self.columns.is_empty() || self.active_column_idx < self.columns.len());
1050+
9111051 for column in &self.columns {
9121052 column.verify_invariants();
9131053 }
9141054 }
9151055
9161056 fn focus_left(&mut self) {
917- self.active_column_idx = self.active_column_idx.saturating_sub(1);
1057+ let new_idx = self.active_column_idx.saturating_sub(1);
1058+ if self.active_column_idx == new_idx {
1059+ return;
1060+ }
1061+
1062+ let column = &self.columns[self.active_column_idx];
1063+ let window = &column.windows[column.active_window_idx];
1064+ window.set_activate(false);
1065+ window.send_pending_configure();
1066+
1067+ self.active_column_idx = new_idx;
1068+
1069+ let column = &self.columns[self.active_column_idx];
1070+ let window = &column.windows[column.active_window_idx];
1071+ window.set_activate(true);
1072+ window.send_pending_configure();
9181073 }
9191074
9201075 fn focus_right(&mut self) {
@@ -922,7 +1077,22 @@ impl Layout {
9221077 return;
9231078 }
9241079
925- self.active_column_idx = min(self.active_column_idx + 1, self.columns.len() - 1);
1080+ let new_idx = min(self.active_column_idx + 1, self.columns.len() - 1);
1081+ if self.active_column_idx == new_idx {
1082+ return;
1083+ }
1084+
1085+ let column = &self.columns[self.active_column_idx];
1086+ let window = &column.windows[column.active_window_idx];
1087+ window.set_activate(false);
1088+ window.send_pending_configure();
1089+
1090+ self.active_column_idx = new_idx;
1091+
1092+ let column = &self.columns[self.active_column_idx];
1093+ let window = &column.windows[column.active_window_idx];
1094+ window.set_activate(true);
1095+ window.send_pending_configure();
9261096 }
9271097
9281098 fn focus_down(&mut self) {
@@ -941,104 +1111,217 @@ impl Layout {
9411111 self.columns[self.active_column_idx].focus_up();
9421112 }
9431113
944- fn move_left(&mut self) -> bool {
1114+ fn move_left(&mut self) {
9451115 let new_idx = self.active_column_idx.saturating_sub(1);
9461116 if self.active_column_idx == new_idx {
947- return false;
1117+ return;
9481118 }
9491119
9501120 self.columns.swap(self.active_column_idx, new_idx);
9511121 self.active_column_idx = new_idx;
952- true
9531122 }
9541123
955- fn move_right(&mut self) -> bool {
1124+ fn move_right(&mut self) {
9561125 if self.columns.is_empty() {
957- return false;
1126+ return;
9581127 }
9591128
9601129 let new_idx = min(self.active_column_idx + 1, self.columns.len() - 1);
9611130 if self.active_column_idx == new_idx {
962- return false;
1131+ return;
9631132 }
9641133
9651134 self.columns.swap(self.active_column_idx, new_idx);
9661135 self.active_column_idx = new_idx;
967- true
9681136 }
9691137
970- fn move_down(&mut self) -> bool {
1138+ fn move_down(&mut self) {
9711139 if self.columns.is_empty() {
972- return false;
1140+ return;
9731141 }
9741142
975- self.columns[self.active_column_idx].move_down()
1143+ self.columns[self.active_column_idx].move_down();
9761144 }
9771145
978- fn move_up(&mut self) -> bool {
1146+ fn move_up(&mut self) {
9791147 if self.columns.is_empty() {
980- return false;
1148+ return;
9811149 }
9821150
983- self.columns[self.active_column_idx].move_up()
1151+ self.columns[self.active_column_idx].move_up();
9841152 }
9851153
986- fn consume_into_column(&mut self, total_height: i32) -> bool {
1154+ fn consume_into_column(&mut self) {
9871155 if self.columns.len() < 2 {
988- return false;
1156+ return;
9891157 }
9901158
9911159 if self.active_column_idx == self.columns.len() - 1 {
992- return false;
1160+ return;
9931161 }
9941162
9951163 let source_column_idx = self.active_column_idx + 1;
9961164
9971165 let source_column = &mut self.columns[source_column_idx];
9981166 let window = source_column.windows[0].clone();
999- self.remove_window(&window, total_height);
1167+ self.remove_window(&window);
10001168
10011169 let target_column = &mut self.columns[self.active_column_idx];
1002-
1003- let window_count = target_column.windows.len() as i32 + 1;
1004- let height = (total_height - PADDING * (window_count + 1)) / window_count;
1005- let width = target_column.size().w;
1006-
1007- target_column.windows.push(window);
1008-
1009- for window in &mut target_column.windows {
1010- window
1011- .toplevel()
1012- .with_pending_state(|state| state.size = Some(Size::from((width, height))));
1013- window.toplevel().send_pending_configure();
1014- }
1015- true
1170+ target_column.add_window(self.view_size, window);
10161171 }
10171172
1018- fn expel_from_column(&mut self, total_height: i32) -> bool {
1173+ fn expel_from_column(&mut self) {
10191174 if self.columns.is_empty() {
1020- return false;
1175+ return;
10211176 }
10221177
10231178 let source_column = &mut self.columns[self.active_column_idx];
10241179 if source_column.windows.len() == 1 {
1025- return false;
1180+ return;
10261181 }
10271182
10281183 let window = source_column.windows[source_column.active_window_idx].clone();
1029- self.remove_window(&window, total_height);
1184+ self.remove_window(&window);
10301185
1031- window.toplevel().with_pending_state(|state| {
1032- state.size = Some(Size::from((state.size.unwrap().w, total_height)))
1033- });
1034- window.toplevel().send_pending_configure();
10351186 self.add_window(window, true);
1187+ }
1188+
1189+ fn send_frame(&self, time: Duration) {
1190+ let output = self.output.as_ref().unwrap();
1191+ for win in self.windows() {
1192+ win.send_frame(output, time, None, |_, _| Some(output.clone()));
1193+ }
1194+ }
10361195
1037- true
1196+ fn view_pos(&self) -> i32 {
1197+ self.column_x(self.active_column_idx) + self.view_offset - PADDING
1198+ }
1199+
1200+ fn window_under(
1201+ &self,
1202+ pos_within_output: Point<f64, Logical>,
1203+ ) -> Option<(&W, Point<i32, Logical>)> {
1204+ let view_pos = self.view_pos();
1205+
1206+ let mut pos = pos_within_output;
1207+ pos.x += view_pos as f64;
1208+
1209+ let mut x = PADDING;
1210+ for col in &self.columns {
1211+ let mut y = PADDING;
1212+
1213+ for win in &col.windows {
1214+ let geom = win.geometry();
1215+
1216+ // x, y point at the top-left of the window geometry.
1217+ let win_pos = Point::from((x, y)) - geom.loc;
1218+ if win.is_in_input_region(&(pos - win_pos.to_f64())) {
1219+ let mut win_pos_within_output = win_pos;
1220+ win_pos_within_output.x -= view_pos;
1221+ return Some((win, win_pos_within_output));
1222+ }
1223+
1224+ y += geom.size.h + PADDING;
1225+ }
1226+
1227+ x += col.size().w + PADDING;
1228+ }
1229+
1230+ None
1231+ }
1232+}
1233+
1234+impl Workspace<Window> {
1235+ fn render_elements(
1236+ &self,
1237+ renderer: &mut GlesRenderer,
1238+ ) -> Vec<WaylandSurfaceRenderElement<GlesRenderer>> {
1239+ let mut rv = vec![];
1240+ let view_pos = self.view_pos();
1241+
1242+ let mut x = PADDING;
1243+ for col in &self.columns {
1244+ let mut y = PADDING;
1245+
1246+ for win in &col.windows {
1247+ let geom = win.geometry();
1248+
1249+ let win_pos = Point::from((x - view_pos, y)) - geom.loc;
1250+ rv.extend(win.render_elements(
1251+ renderer,
1252+ win_pos.to_physical(1),
1253+ Scale::from(1.),
1254+ 1.,
1255+ ));
1256+ y += win.geometry().size.h + PADDING;
1257+ }
1258+
1259+ x += col.size().w + PADDING;
1260+ }
1261+
1262+ rv
10381263 }
10391264 }
10401265
1041-impl Column {
1266+impl<W: LayoutElement> Column<W> {
1267+ fn new(window: W, view_size: Size<i32, Logical>) -> Self {
1268+ let mut rv = Self {
1269+ windows: vec![],
1270+ active_window_idx: 0,
1271+ width: ColumnWidth::default(),
1272+ };
1273+
1274+ rv.add_window(view_size, window);
1275+
1276+ rv
1277+ }
1278+
1279+ fn window_count(&self) -> usize {
1280+ self.windows.len()
1281+ }
1282+
1283+ fn set_width(&mut self, view_size: Size<i32, Logical>, width: ColumnWidth) {
1284+ self.width = width;
1285+ self.update_window_sizes(view_size);
1286+ }
1287+
1288+ fn contains(&self, window: &W) -> bool {
1289+ self.windows.iter().any(|win| win == window)
1290+ }
1291+
1292+ fn activate_window(&mut self, window: &W) {
1293+ let idx = self.windows.iter().position(|win| win == window).unwrap();
1294+ self.active_window_idx = idx;
1295+ }
1296+
1297+ fn add_window(&mut self, view_size: Size<i32, Logical>, window: W) {
1298+ self.windows.push(window);
1299+ self.update_window_sizes(view_size);
1300+ }
1301+
1302+ fn update_window_sizes(&mut self, view_size: Size<i32, Logical>) {
1303+ let min_width = self
1304+ .windows
1305+ .iter()
1306+ .filter_map(|win| {
1307+ let w = win.min_size().w;
1308+ if w == 0 {
1309+ None
1310+ } else {
1311+ Some(w)
1312+ }
1313+ })
1314+ .max()
1315+ .unwrap_or(1);
1316+ let width = self.width.resolve(view_size.w - PADDING) - PADDING;
1317+ let height = (view_size.h - PADDING) / self.window_count() as i32 - PADDING;
1318+ let size = Size::from((max(width, min_width), max(height, 1)));
1319+
1320+ for win in &self.windows {
1321+ win.request_size(size);
1322+ }
1323+ }
1324+
10421325 /// Computes the size of the column including top and bottom padding.
10431326 fn size(&self) -> Size<i32, Logical> {
10441327 let mut total = Size::from((0, PADDING));
@@ -1052,48 +1335,77 @@ impl Column {
10521335 total
10531336 }
10541337
1055- fn window_y(&self, window_idx: usize) -> i32 {
1056- let mut y = PADDING;
1057-
1058- for window in self.windows.iter().take(window_idx) {
1059- let size = window.geometry().size;
1060- y += size.h + PADDING;
1338+ fn focus_up(&mut self) {
1339+ let new_idx = self.active_window_idx.saturating_sub(1);
1340+ if self.active_window_idx == new_idx {
1341+ return;
10611342 }
10621343
1063- y
1064- }
1065-
1066- fn focus_up(&mut self) {
1067- self.active_window_idx = self.active_window_idx.saturating_sub(1);
1344+ self.windows[self.active_window_idx].set_activate(false);
1345+ self.windows[self.active_window_idx].send_pending_configure();
1346+ self.windows[new_idx].set_activate(true);
1347+ self.windows[new_idx].send_pending_configure();
1348+ self.active_window_idx = new_idx;
10681349 }
10691350
10701351 fn focus_down(&mut self) {
1071- self.active_window_idx = min(self.active_window_idx + 1, self.windows.len() - 1);
1352+ let new_idx = min(self.active_window_idx + 1, self.windows.len() - 1);
1353+ if self.active_window_idx == new_idx {
1354+ return;
1355+ }
1356+
1357+ self.windows[self.active_window_idx].set_activate(false);
1358+ self.windows[self.active_window_idx].send_pending_configure();
1359+ self.windows[new_idx].set_activate(true);
1360+ self.windows[new_idx].send_pending_configure();
1361+ self.active_window_idx = new_idx;
10721362 }
10731363
1074- fn move_up(&mut self) -> bool {
1364+ fn move_up(&mut self) {
10751365 let new_idx = self.active_window_idx.saturating_sub(1);
10761366 if self.active_window_idx == new_idx {
1077- return false;
1367+ return;
10781368 }
10791369
10801370 self.windows.swap(self.active_window_idx, new_idx);
10811371 self.active_window_idx = new_idx;
1082- true
10831372 }
10841373
1085- fn move_down(&mut self) -> bool {
1374+ fn move_down(&mut self) {
10861375 let new_idx = min(self.active_window_idx + 1, self.windows.len() - 1);
10871376 if self.active_window_idx == new_idx {
1088- return false;
1377+ return;
10891378 }
10901379
10911380 self.windows.swap(self.active_window_idx, new_idx);
10921381 self.active_window_idx = new_idx;
1093- true
10941382 }
10951383
10961384 fn verify_invariants(&self) {
10971385 assert!(!self.windows.is_empty(), "columns can't be empty");
1386+ assert!(self.active_window_idx < self.windows.len());
10981387 }
10991388 }
1389+
1390+pub fn output_size(output: &Output) -> Size<i32, Logical> {
1391+ let output_scale = output.current_scale().integer_scale();
1392+ let output_transform = output.current_transform();
1393+ let output_mode = output.current_mode().unwrap();
1394+
1395+ output_transform
1396+ .transform_size(output_mode.size)
1397+ .to_logical(output_scale)
1398+}
1399+
1400+pub fn configure_new_window(view_size: Size<i32, Logical>, window: &Window) {
1401+ let width = ColumnWidth::default().resolve(view_size.w - PADDING) - PADDING;
1402+ let height = view_size.h - PADDING * 2;
1403+ let size = Size::from((max(width, 1), max(height, 1)));
1404+
1405+ let bounds = Size::from((view_size.w - PADDING * 2, view_size.h - PADDING * 2));
1406+
1407+ window.toplevel().with_pending_state(|state| {
1408+ state.size = Some(size);
1409+ state.bounds = Some(bounds);
1410+ });
1411+}
src/niri.rsmodified+25 -85
@@ -1,12 +1,11 @@
11 use std::collections::HashMap;
22 use std::os::unix::io::AsRawFd;
33 use std::sync::Arc;
4-use std::time::Duration;
54
5+use smithay::backend::renderer::element::render_elements;
66 use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};
7-use smithay::backend::renderer::element::{render_elements, RenderElement};
7+use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
88 use smithay::backend::renderer::ImportAll;
9-use smithay::desktop::space::{space_render_elements, SpaceRenderElements};
109 use smithay::desktop::{PopupManager, Space, Window, WindowSurfaceType};
1110 use smithay::input::keyboard::XkbConfig;
1211 use smithay::input::{Seat, SeatState};
@@ -39,7 +38,7 @@ pub struct Niri {
3938
4039 // Each workspace corresponds to a Space. Each workspace generally has one Output mapped to it,
4140 // however it may have none (when there are no outputs connected) or mutiple (when mirroring).
42- pub monitor_set: MonitorSet,
41+ pub monitor_set: MonitorSet<Window>,
4342
4443 // This space does not actually contain any windows, but all outputs are mapped into it
4544 // according to their global position.
@@ -200,7 +199,7 @@ impl Niri {
200199 }
201200
202201 pub fn output_resized(&mut self, output: Output) {
203- // FIXME resize windows etc
202+ self.monitor_set.update_output(&output);
204203 self.queue_redraw(output);
205204 }
206205
@@ -217,40 +216,11 @@ impl Niri {
217216 Some((output, pos_within_output))
218217 }
219218
220- pub fn window_under(
221- &mut self,
222- pos: Point<f64, Logical>,
223- ) -> Option<(&mut Space<Window>, Window, Point<i32, Logical>)> {
219+ pub fn window_under_cursor(&self) -> Option<&Window> {
220+ let pos = self.seat.get_pointer().unwrap().current_location();
224221 let (output, pos_within_output) = self.output_under(pos)?;
225- let output = output.clone();
226-
227- let space = &mut self.monitor_set.workspace_for_output(&output)?.space;
228- let output_pos = space.output_geometry(&output).unwrap().loc.to_f64();
229-
230- let pos_within_space = pos_within_output + output_pos;
231- let (window, loc) = space.element_under(pos_within_space)?;
232- let window = window.clone();
233- Some((space, window, loc))
234- }
235-
236- pub fn surface_under(
237- &mut self,
238- pos: Point<f64, Logical>,
239- ) -> Option<(WlSurface, Point<i32, Logical>)> {
240- let (output, pos_within_output) = self.output_under(pos)?;
241- let output = output.clone();
242-
243- let space = &self.monitor_set.workspace_for_output(&output)?.space;
244- let output_pos = space.output_geometry(&output).unwrap().loc.to_f64();
245-
246- let pos_within_space = pos_within_output + output_pos;
247- space
248- .element_under(pos_within_space)
249- .and_then(|(window, location)| {
250- window
251- .surface_under(pos_within_space - location.to_f64(), WindowSurfaceType::ALL)
252- .map(|(s, p)| (s, p + location))
253- })
222+ let (window, _loc) = self.monitor_set.window_under(output, pos_within_output)?;
223+ Some(window)
254224 }
255225
256226 /// Returns the surface under cursor and its position in the global space.
@@ -263,23 +233,17 @@ impl Niri {
263233 pos: Point<f64, Logical>,
264234 ) -> Option<(WlSurface, Point<i32, Logical>)> {
265235 let (output, pos_within_output) = self.output_under(pos)?;
266- let output = output.clone();
267-
268- let workspace = &self.monitor_set.workspace_for_output(&output)?;
269- let space = &workspace.space;
270- let output_pos_in_local_space = space.output_geometry(&output).unwrap().loc;
271- let pos_within_space = pos_within_output + output_pos_in_local_space.to_f64();
272-
273- let (surface, surface_loc_in_local_space) = space
274- .element_under(pos_within_space)
275- .and_then(|(window, location)| {
276- window
277- .surface_under(pos_within_space - location.to_f64(), WindowSurfaceType::ALL)
278- .map(|(s, p)| (s, p + location))
279- })?;
280- let output_pos_in_global_space = self.global_space.output_geometry(&output).unwrap().loc;
281- let surface_loc_in_global_space =
282- surface_loc_in_local_space - output_pos_in_local_space + output_pos_in_global_space;
236+ let (window, win_pos_within_output) =
237+ self.monitor_set.window_under(output, pos_within_output)?;
238+
239+ let (surface, surface_pos_within_output) = window
240+ .surface_under(
241+ pos_within_output - win_pos_within_output.to_f64(),
242+ WindowSurfaceType::ALL,
243+ )
244+ .map(|(s, pos_within_window)| (s, pos_within_window + win_pos_within_output))?;
245+ let output_pos_in_global_space = self.global_space.output_geometry(output).unwrap().loc;
246+ let surface_loc_in_global_space = surface_pos_within_output + output_pos_in_global_space;
283247
284248 Some((surface, surface_loc_in_global_space))
285249 }
@@ -338,8 +302,7 @@ impl Niri {
338302 assert!(state.queued_redraw.take().is_some());
339303 assert!(!state.waiting_for_vblank);
340304
341- let space = &self.monitor_set.workspace_for_output(output).unwrap().space;
342- let elements = space_render_elements(backend.renderer(), [space], output, 1.).unwrap();
305+ let elements = self.monitor_set.render_elements(backend.renderer(), output);
343306
344307 let output_pos = self.global_space.output_geometry(output).unwrap().loc;
345308 let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64();
@@ -360,41 +323,18 @@ impl Niri {
360323
361324 backend.render(self, output, &elements);
362325
363- let space = &self.monitor_set.workspace_for_output(output).unwrap().space;
364- space.elements().for_each(|window| {
365- window.send_frame(
366- output,
367- self.start_time.elapsed(),
368- Some(Duration::ZERO),
369- |_, _| Some(output.clone()),
370- )
371- });
326+ self.monitor_set
327+ .send_frame(output, self.start_time.elapsed());
372328 }
373329 }
374330
375331 render_elements! {
376- pub OutputRenderElements<R, E> where R: ImportAll;
377- Space = SpaceRenderElements<R, E>,
332+ #[derive(Debug)]
333+ pub OutputRenderElements<R> where R: ImportAll;
334+ Wayland = WaylandSurfaceRenderElement<R>,
378335 Pointer = SolidColorRenderElement,
379336 }
380337
381-impl<R: ImportAll, E: RenderElement<R>> std::fmt::Debug for OutputRenderElements<R, E> {
382- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
383- match self {
384- OutputRenderElements::Space(_) => {
385- f.debug_tuple("OutputRenderElements::Space").finish()?
386- }
387- OutputRenderElements::Pointer(element) => f
388- .debug_tuple("OutputRenderElements::Pointer")
389- .field(element)
390- .finish()?,
391- _ => (),
392- }
393-
394- Ok(())
395- }
396-}
397-
398338 #[derive(Default)]
399339 pub struct ClientState {
400340 pub compositor_state: CompositorClientState,
src/tty.rsmodified+1 -5
@@ -10,7 +10,6 @@ use smithay::backend::drm::compositor::DrmCompositor;
1010 use smithay::backend::drm::{DrmDevice, DrmDeviceFd, DrmEvent};
1111 use smithay::backend::egl::{EGLContext, EGLDisplay};
1212 use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface};
13-use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
1413 use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
1514 use smithay::backend::renderer::{Bind, ImportEgl};
1615 use smithay::backend::session::libseat::LibSeatSession;
@@ -73,10 +72,7 @@ impl Backend for Tty {
7372 &mut self,
7473 niri: &mut Niri,
7574 output: &Output,
76- elements: &[OutputRenderElements<
77- GlesRenderer,
78- WaylandSurfaceRenderElement<GlesRenderer>,
79- >],
75+ elements: &[OutputRenderElements<GlesRenderer>],
8076 ) {
8177 let _span = tracy_client::span!("Tty::render");
8278
src/winit.rsmodified+1 -5
@@ -1,7 +1,6 @@
11 use std::time::Duration;
22
33 use smithay::backend::renderer::damage::OutputDamageTracker;
4-use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
54 use smithay::backend::renderer::gles::GlesRenderer;
65 use smithay::backend::renderer::ImportEgl;
76 use smithay::backend::winit::{self, WinitError, WinitEvent, WinitEventLoop, WinitGraphicsBackend};
@@ -37,10 +36,7 @@ impl Backend for Winit {
3736 &mut self,
3837 _niri: &mut Niri,
3938 _output: &Output,
40- elements: &[OutputRenderElements<
41- GlesRenderer,
42- WaylandSurfaceRenderElement<GlesRenderer>,
43- >],
39+ elements: &[OutputRenderElements<GlesRenderer>],
4440 ) {
4541 let _span = tracy_client::span!("Winit::render");
4642