Refactor everything again, make more things work
Browse files at this commit8f71842e7cf0c2e77183e99b954c9b511dca3576Parent: 95c810c8 changed files +756 -559
- src/backend.rs +1 -5
- src/handlers/compositor.rs +5 -10
- src/handlers/xdg_shell.rs +48 -41
- src/input.rs +6 -51
- src/layout.rs +669 -357
- src/niri.rs +25 -85
- src/tty.rs +1 -5
- src/winit.rs +1 -5
src/backend.rsmodified+1 -5
| @@ -1,4 +1,3 @@ | |||
| 1 | -use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; | ||
| 2 | use smithay::backend::renderer::gles::GlesRenderer; | 1 | use smithay::backend::renderer::gles::GlesRenderer; |
| 3 | use smithay::output::Output; | 2 | use smithay::output::Output; |
| 4 | 3 | ||
| @@ -12,9 +11,6 @@ pub trait Backend { | |||
| 12 | &mut self, | 11 | &mut self, |
| 13 | niri: &mut Niri, | 12 | niri: &mut Niri, |
| 14 | output: &Output, | 13 | output: &Output, |
| 15 | - elements: &[OutputRenderElements< | 14 | + elements: &[OutputRenderElements<GlesRenderer>], |
| 16 | - GlesRenderer, | ||
| 17 | - WaylandSurfaceRenderElement<GlesRenderer>, | ||
| 18 | - >], | ||
| 19 | ); | 15 | ); |
| 20 | } | 16 | } |
src/handlers/compositor.rsmodified+5 -10
| @@ -68,10 +68,8 @@ impl CompositorHandler for Niri { | |||
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | // This is a commit of a previously-mapped root or a non-toplevel root. | 70 | // 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) { |
| 72 | // This is a commit of a previously-mapped toplevel. | 72 | // This is a commit of a previously-mapped toplevel. |
| 73 | - let output = space.outputs().next().unwrap().clone(); | ||
| 74 | - | ||
| 75 | window.on_commit(); | 73 | window.on_commit(); |
| 76 | 74 | ||
| 77 | // This is a commit of a previously-mapped toplevel. | 75 | // This is a commit of a previously-mapped toplevel. |
| @@ -80,7 +78,6 @@ impl CompositorHandler for Niri { | |||
| 80 | 78 | ||
| 81 | if !is_mapped { | 79 | if !is_mapped { |
| 82 | // The toplevel got unmapped. | 80 | // The toplevel got unmapped. |
| 83 | - let window = window.clone(); | ||
| 84 | self.monitor_set.remove_window(&window); | 81 | self.monitor_set.remove_window(&window); |
| 85 | self.unmapped_windows.insert(surface.clone(), window); | 82 | self.unmapped_windows.insert(surface.clone(), window); |
| 86 | self.update_focus(); | 83 | self.update_focus(); |
| @@ -101,9 +98,8 @@ impl CompositorHandler for Niri { | |||
| 101 | } | 98 | } |
| 102 | 99 | ||
| 103 | // This is a commit of a non-root or a non-toplevel root. | 100 | // 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); | 101 | + let root_window_output = self.monitor_set.find_window_and_output(&root_surface); |
| 105 | - if let Some((window, space)) = root_window_space { | 102 | + if let Some((window, output)) = root_window_output { |
| 106 | - let output = space.outputs().next().unwrap().clone(); | ||
| 107 | window.on_commit(); | 103 | window.on_commit(); |
| 108 | self.monitor_set.update_window(&window); | 104 | self.monitor_set.update_window(&window); |
| 109 | self.queue_redraw(output); | 105 | self.queue_redraw(output); |
| @@ -114,9 +110,8 @@ impl CompositorHandler for Niri { | |||
| 114 | self.popups_handle_commit(surface); | 110 | self.popups_handle_commit(surface); |
| 115 | if let Some(popup) = self.popups.find_popup(surface) { | 111 | if let Some(popup) = self.popups.find_popup(surface) { |
| 116 | if let Ok(root) = find_popup_root_surface(&popup) { | 112 | if let Ok(root) = find_popup_root_surface(&popup) { |
| 117 | - let root_window_space = self.monitor_set.find_window_and_space(&root); | 113 | + let root_window_output = self.monitor_set.find_window_and_output(&root); |
| 118 | - if let Some((_window, space)) = root_window_space { | 114 | + if let Some((_window, output)) = root_window_output { |
| 119 | - let output = space.outputs().next().unwrap().clone(); | ||
| 120 | self.queue_redraw(output); | 115 | self.queue_redraw(output); |
| 121 | } | 116 | } |
| 122 | } | 117 | } |
src/handlers/xdg_shell.rsmodified+48 -41
| @@ -1,5 +1,5 @@ | |||
| 1 | use smithay::delegate_xdg_shell; | 1 | use smithay::delegate_xdg_shell; |
| 2 | -use smithay::desktop::{PopupKind, Window}; | 2 | +use smithay::desktop::{find_popup_root_surface, PopupKind, Window}; |
| 3 | use smithay::input::pointer::{Focus, GrabStartData as PointerGrabStartData}; | 3 | use smithay::input::pointer::{Focus, GrabStartData as PointerGrabStartData}; |
| 4 | use smithay::input::Seat; | 4 | use smithay::input::Seat; |
| 5 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | 5 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| @@ -14,7 +14,7 @@ use smithay::wayland::shell::xdg::{ | |||
| 14 | }; | 14 | }; |
| 15 | 15 | ||
| 16 | use crate::grabs::{MoveSurfaceGrab, ResizeSurfaceGrab}; | 16 | use crate::grabs::{MoveSurfaceGrab, ResizeSurfaceGrab}; |
| 17 | -use crate::layout::MonitorSet; | 17 | +use crate::layout::{configure_new_window, output_size}; |
| 18 | use crate::Niri; | 18 | use crate::Niri; |
| 19 | 19 | ||
| 20 | impl XdgShellHandler for Niri { | 20 | impl XdgShellHandler for Niri { |
| @@ -28,7 +28,7 @@ impl XdgShellHandler for Niri { | |||
| 28 | 28 | ||
| 29 | // Tell the surface the preferred size and bounds for its likely output. | 29 | // Tell the surface the preferred size and bounds for its likely output. |
| 30 | let output = self.monitor_set.active_output().unwrap(); | 30 | let output = self.monitor_set.active_output().unwrap(); |
| 31 | - MonitorSet::configure_new_window(output, &window); | 31 | + configure_new_window(output_size(output), &window); |
| 32 | 32 | ||
| 33 | // At the moment of creation, xdg toplevels must have no buffer. | 33 | // At the moment of creation, xdg toplevels must have no buffer. |
| 34 | let existing = self.unmapped_windows.insert(wl_surface, window); | 34 | let existing = self.unmapped_windows.insert(wl_surface, window); |
| @@ -49,24 +49,26 @@ impl XdgShellHandler for Niri { | |||
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | fn move_request(&mut self, surface: ToplevelSurface, seat: wl_seat::WlSeat, serial: Serial) { | 51 | fn move_request(&mut self, surface: ToplevelSurface, seat: wl_seat::WlSeat, serial: Serial) { |
| 52 | - let seat = Seat::from_resource(&seat).unwrap(); | 52 | + // FIXME |
| 53 | 53 | ||
| 54 | - let wl_surface = surface.wl_surface(); | 54 | + // let seat = Seat::from_resource(&seat).unwrap(); |
| 55 | 55 | ||
| 56 | - if let Some(start_data) = check_grab(&seat, wl_surface, serial) { | 56 | + // let wl_surface = surface.wl_surface(); |
| 57 | - let pointer = seat.get_pointer().unwrap(); | ||
| 58 | 57 | ||
| 59 | - let (window, space) = self.monitor_set.find_window_and_space(wl_surface).unwrap(); | 58 | + // if let Some(start_data) = check_grab(&seat, wl_surface, serial) { |
| 60 | - let initial_window_location = space.element_location(&window).unwrap(); | 59 | + // let pointer = seat.get_pointer().unwrap(); |
| 61 | 60 | ||
| 62 | - let grab = MoveSurfaceGrab { | 61 | + // let (window, space) = self.monitor_set.find_window_and_space(wl_surface).unwrap(); |
| 63 | - start_data, | 62 | + // let initial_window_location = space.element_location(&window).unwrap(); |
| 64 | - window: window.clone(), | ||
| 65 | - initial_window_location, | ||
| 66 | - }; | ||
| 67 | 63 | ||
| 68 | - pointer.set_grab(self, grab, serial, Focus::Clear); | 64 | + // let grab = MoveSurfaceGrab { |
| 69 | - } | 65 | + // start_data, |
| 66 | + // window: window.clone(), | ||
| 67 | + // initial_window_location, | ||
| 68 | + // }; | ||
| 69 | + | ||
| 70 | + // pointer.set_grab(self, grab, serial, Focus::Clear); | ||
| 71 | + // } | ||
| 70 | } | 72 | } |
| 71 | 73 | ||
| 72 | fn resize_request( | 74 | fn resize_request( |
| @@ -76,32 +78,34 @@ impl XdgShellHandler for Niri { | |||
| 76 | serial: Serial, | 78 | serial: Serial, |
| 77 | edges: xdg_toplevel::ResizeEdge, | 79 | edges: xdg_toplevel::ResizeEdge, |
| 78 | ) { | 80 | ) { |
| 79 | - let seat = Seat::from_resource(&seat).unwrap(); | 81 | + // FIXME |
| 80 | 82 | ||
| 81 | - let wl_surface = surface.wl_surface(); | 83 | + // let seat = Seat::from_resource(&seat).unwrap(); |
| 82 | 84 | ||
| 83 | - if let Some(start_data) = check_grab(&seat, wl_surface, serial) { | 85 | + // let wl_surface = surface.wl_surface(); |
| 84 | - let pointer = seat.get_pointer().unwrap(); | ||
| 85 | 86 | ||
| 86 | - let (window, space) = self.monitor_set.find_window_and_space(wl_surface).unwrap(); | 87 | + // if let Some(start_data) = check_grab(&seat, wl_surface, serial) { |
| 87 | - let initial_window_location = space.element_location(&window).unwrap(); | 88 | + // let pointer = seat.get_pointer().unwrap(); |
| 88 | - let initial_window_size = window.geometry().size; | ||
| 89 | 89 | ||
| 90 | - surface.with_pending_state(|state| { | 90 | + // let (window, space) = self.monitor_set.find_window_and_space(wl_surface).unwrap(); |
| 91 | - state.states.set(xdg_toplevel::State::Resizing); | 91 | + // let initial_window_location = space.element_location(&window).unwrap(); |
| 92 | - }); | 92 | + // let initial_window_size = window.geometry().size; |
| 93 | 93 | ||
| 94 | - surface.send_pending_configure(); | 94 | + // surface.with_pending_state(|state| { |
| 95 | + // state.states.set(xdg_toplevel::State::Resizing); | ||
| 96 | + // }); | ||
| 95 | 97 | ||
| 96 | - let grab = ResizeSurfaceGrab::start( | 98 | + // surface.send_pending_configure(); |
| 97 | - start_data, | ||
| 98 | - window.clone(), | ||
| 99 | - edges.into(), | ||
| 100 | - Rectangle::from_loc_and_size(initial_window_location, initial_window_size), | ||
| 101 | - ); | ||
| 102 | 99 | ||
| 103 | - pointer.set_grab(self, grab, serial, Focus::Clear); | 100 | + // let grab = ResizeSurfaceGrab::start( |
| 104 | - } | 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 | + // } | ||
| 105 | } | 109 | } |
| 106 | 110 | ||
| 107 | fn reposition_request( | 111 | fn reposition_request( |
| @@ -232,19 +236,22 @@ impl XdgShellHandler for Niri { | |||
| 232 | return; | 236 | return; |
| 233 | } | 237 | } |
| 234 | 238 | ||
| 235 | - let (window, space) = self | 239 | + let (window, output) = self |
| 236 | .monitor_set | 240 | .monitor_set |
| 237 | - .find_window_and_space(surface.wl_surface()) | 241 | + .find_window_and_output(surface.wl_surface()) |
| 238 | .unwrap(); | 242 | .unwrap(); |
| 239 | - let output = space.outputs().next().unwrap().clone(); | ||
| 240 | self.monitor_set.remove_window(&window); | 243 | self.monitor_set.remove_window(&window); |
| 241 | self.update_focus(); | 244 | self.update_focus(); |
| 242 | self.queue_redraw(output); | 245 | self.queue_redraw(output); |
| 243 | } | 246 | } |
| 244 | 247 | ||
| 245 | - fn popup_destroyed(&mut self, _surface: PopupSurface) { | 248 | + fn popup_destroyed(&mut self, surface: PopupSurface) { |
| 246 | - // FIXME granular | 249 | + if let Ok(root) = find_popup_root_surface(&surface.into()) { |
| 247 | - self.queue_redraw_all(); | 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 | + } | ||
| 248 | } | 255 | } |
| 249 | } | 256 | } |
| 250 | 257 | ||
src/input.rsmodified+6 -51
| @@ -1,4 +1,3 @@ | |||
| 1 | -use std::cell::Cell; | ||
| 2 | use std::process::Command; | 1 | use std::process::Command; |
| 3 | 2 | ||
| 4 | use smithay::backend::input::{ | 3 | use smithay::backend::input::{ |
| @@ -7,9 +6,7 @@ use smithay::backend::input::{ | |||
| 7 | }; | 6 | }; |
| 8 | use smithay::input::keyboard::{keysyms, FilterResult, KeysymHandle, ModifiersState}; | 7 | use smithay::input::keyboard::{keysyms, FilterResult, KeysymHandle, ModifiersState}; |
| 9 | use smithay::input::pointer::{AxisFrame, ButtonEvent, MotionEvent, RelativeMotionEvent}; | 8 | use smithay::input::pointer::{AxisFrame, ButtonEvent, MotionEvent, RelativeMotionEvent}; |
| 10 | -use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | ||
| 11 | use smithay::utils::SERIAL_COUNTER; | 9 | use smithay::utils::SERIAL_COUNTER; |
| 12 | -use smithay::wayland::shell::xdg::XdgShellHandler; | ||
| 13 | 10 | ||
| 14 | use crate::niri::Niri; | 11 | use crate::niri::Niri; |
| 15 | 12 | ||
| @@ -140,54 +137,13 @@ impl Niri { | |||
| 140 | } | 137 | } |
| 141 | } | 138 | } |
| 142 | Action::CloseWindow => { | 139 | Action::CloseWindow => { |
| 143 | - if let Some(focus) = self.seat.get_keyboard().unwrap().current_focus() { | 140 | + if let Some(window) = self.monitor_set.focus() { |
| 144 | - // FIXME: is there a better way of doing this? | 141 | + window.toplevel().send_close(); |
| 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 | - } | ||
| 161 | } | 142 | } |
| 162 | } | 143 | } |
| 163 | Action::ToggleFullscreen => { | 144 | Action::ToggleFullscreen => { |
| 164 | - if let Some(focus) = self.seat.get_keyboard().unwrap().current_focus() { | 145 | + if let Some(window) = self.monitor_set.focus() { |
| 165 | - // FIXME: is there a better way of doing this? | 146 | + // FIXME |
| 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 | - } | ||
| 191 | } | 147 | } |
| 192 | } | 148 | } |
| 193 | Action::MoveLeft => { | 149 | Action::MoveLeft => { |
| @@ -353,9 +309,8 @@ impl Niri { | |||
| 353 | let button_state = event.state(); | 309 | let button_state = event.state(); |
| 354 | 310 | ||
| 355 | if ButtonState::Pressed == button_state && !pointer.is_grabbed() { | 311 | if ButtonState::Pressed == button_state && !pointer.is_grabbed() { |
| 356 | - if let Some((_space, window, _loc)) = | 312 | + if let Some(window) = self.window_under_cursor() { |
| 357 | - self.window_under(pointer.current_location()) | 313 | + let window = window.clone(); |
| 358 | - { | ||
| 359 | self.monitor_set.activate_window(&window); | 314 | self.monitor_set.activate_window(&window); |
| 360 | } else { | 315 | } else { |
| 361 | let output = self.output_under_cursor().unwrap(); | 316 | let output = self.output_under_cursor().unwrap(); |
src/layout.rsmodified+669 -357
| @@ -31,69 +31,113 @@ | |||
| 31 | 31 | ||
| 32 | use std::cmp::{max, min}; | 32 | use std::cmp::{max, min}; |
| 33 | use std::mem; | 33 | use std::mem; |
| 34 | +use std::time::Duration; | ||
| 34 | 35 | ||
| 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; | ||
| 36 | use smithay::output::Output; | 41 | use smithay::output::Output; |
| 37 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 42 | 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; | ||
| 39 | 46 | ||
| 40 | const PADDING: i32 = 16; | 47 | const PADDING: i32 = 16; |
| 41 | 48 | ||
| 42 | #[derive(Debug, Clone, PartialEq, Eq)] | 49 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 43 | pub struct OutputId(String); | 50 | pub struct OutputId(String); |
| 44 | 51 | ||
| 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 | + | ||
| 45 | #[derive(Debug)] | 68 | #[derive(Debug)] |
| 46 | -pub enum MonitorSet { | 69 | +pub enum MonitorSet<W: LayoutElement> { |
| 47 | /// At least one output is connected. | 70 | /// At least one output is connected. |
| 48 | Normal { | 71 | Normal { |
| 49 | - monitors: Vec<Monitor>, | 72 | + /// Connected monitors. |
| 73 | + monitors: Vec<Monitor<W>>, | ||
| 50 | /// Index of the primary monitor. | 74 | /// Index of the primary monitor. |
| 51 | primary_idx: usize, | 75 | primary_idx: usize, |
| 52 | /// Index of the active monitor. | 76 | /// Index of the active monitor. |
| 53 | active_monitor_idx: usize, | 77 | active_monitor_idx: usize, |
| 54 | }, | 78 | }, |
| 55 | /// No outputs are connected, and these are the workspaces. | 79 | /// No outputs are connected, and these are the workspaces. |
| 56 | - // FIXME: preserve active output id? | 80 | + NoOutputs(Vec<Workspace<W>>), |
| 57 | - NoOutputs(Vec<Workspace>), | ||
| 58 | } | 81 | } |
| 59 | 82 | ||
| 60 | #[derive(Debug)] | 83 | #[derive(Debug)] |
| 61 | -pub struct Monitor { | 84 | +pub struct Monitor<W: LayoutElement> { |
| 85 | + /// Output for this monitor. | ||
| 62 | output: Output, | 86 | output: Output, |
| 63 | // Must always contain at least one. | 87 | // Must always contain at least one. |
| 64 | - workspaces: Vec<Workspace>, | 88 | + workspaces: Vec<Workspace<W>>, |
| 65 | /// Index of the currently active workspace. | 89 | /// Index of the currently active workspace. |
| 66 | active_workspace_idx: usize, | 90 | active_workspace_idx: usize, |
| 67 | } | 91 | } |
| 68 | 92 | ||
| 69 | #[derive(Debug)] | 93 | #[derive(Debug)] |
| 70 | -pub struct Workspace { | 94 | +pub struct Workspace<W: LayoutElement> { |
| 71 | /// The original output of this workspace. | 95 | /// The original output of this workspace. |
| 72 | /// | 96 | /// |
| 73 | /// Most of the time this will be the workspace's current output, however, after an output | 97 | /// Most of the time this will be the workspace's current output, however, after an output |
| 74 | /// disconnection, it may remain pointing to the disconnected output. | 98 | /// disconnection, it may remain pointing to the disconnected output. |
| 75 | original_output: OutputId, | 99 | original_output: OutputId, |
| 76 | 100 | ||
| 77 | - layout: Layout, | 101 | + /// Current output of this workspace. |
| 102 | + output: Option<Output>, | ||
| 78 | 103 | ||
| 79 | - // The actual Space with windows in this workspace. Should be synchronized to the layout except | 104 | + /// Latest known view size for this workspace. |
| 80 | - // for a brief period during surface commit handling. | 105 | + /// |
| 81 | - pub space: Space<Window>, | 106 | + /// This should be computed from the current workspace output size, or, if all outputs have |
| 82 | -} | 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>>, | ||
| 83 | 112 | ||
| 84 | -#[derive(Debug)] | ||
| 85 | -pub struct Layout { | ||
| 86 | - columns: Vec<Column>, | ||
| 87 | /// Index of the currently active column, if any. | 113 | /// Index of the currently active column, if any. |
| 88 | active_column_idx: usize, | 114 | 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), | ||
| 89 | } | 127 | } |
| 90 | 128 | ||
| 91 | #[derive(Debug)] | 129 | #[derive(Debug)] |
| 92 | -pub struct Column { | 130 | +struct Column<W: LayoutElement> { |
| 93 | - // Must be non-empty. | 131 | + /// Windows in this column. |
| 94 | - windows: Vec<Window>, | 132 | + /// |
| 133 | + /// Must be non-empty. | ||
| 134 | + windows: Vec<W>, | ||
| 135 | + | ||
| 95 | /// Index of the currently active window. | 136 | /// Index of the currently active window. |
| 96 | active_window_idx: usize, | 137 | active_window_idx: usize, |
| 138 | + | ||
| 139 | + /// Desired width of this column. | ||
| 140 | + width: ColumnWidth, | ||
| 97 | } | 141 | } |
| 98 | 142 | ||
| 99 | impl OutputId { | 143 | impl OutputId { |
| @@ -102,7 +146,65 @@ impl OutputId { | |||
| 102 | } | 146 | } |
| 103 | } | 147 | } |
| 104 | 148 | ||
| 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> { | ||
| 106 | pub fn new() -> Self { | 208 | pub fn new() -> Self { |
| 107 | Self::NoOutputs(vec![]) | 209 | Self::NoOutputs(vec![]) |
| 108 | } | 210 | } |
| @@ -121,26 +223,18 @@ impl MonitorSet { | |||
| 121 | let mut workspaces = vec![]; | 223 | let mut workspaces = vec![]; |
| 122 | for i in (0..primary.workspaces.len()).rev() { | 224 | for i in (0..primary.workspaces.len()).rev() { |
| 123 | if primary.workspaces[i].original_output == id { | 225 | if primary.workspaces[i].original_output == id { |
| 124 | - let mut ws = primary.workspaces.remove(i); | 226 | + let ws = primary.workspaces.remove(i); |
| 125 | - ws.space.unmap_output(&primary.output); | ||
| 126 | workspaces.push(ws); | 227 | workspaces.push(ws); |
| 127 | } | 228 | } |
| 128 | } | 229 | } |
| 129 | workspaces.reverse(); | 230 | workspaces.reverse(); |
| 130 | - if workspaces | 231 | + if workspaces.iter().all(|ws| ws.has_windows()) { |
| 131 | - .iter() | ||
| 132 | - .all(|ws| ws.space.elements().next().is_some()) | ||
| 133 | - { | ||
| 134 | // Make sure there's always an empty workspace. | 232 | // Make sure there's always an empty workspace. |
| 135 | - workspaces.push(Workspace { | 233 | + workspaces.push(Workspace::new(output.clone())); |
| 136 | - original_output: id, | ||
| 137 | - layout: Layout::new(), | ||
| 138 | - space: Space::default(), | ||
| 139 | - }); | ||
| 140 | } | 234 | } |
| 141 | 235 | ||
| 142 | for ws in &mut workspaces { | 236 | for ws in &mut workspaces { |
| 143 | - ws.space.map_output(&output, (0, 0)); | 237 | + ws.set_output(Some(output.clone())); |
| 144 | } | 238 | } |
| 145 | 239 | ||
| 146 | monitors.push(Monitor { | 240 | monitors.push(Monitor { |
| @@ -155,19 +249,11 @@ impl MonitorSet { | |||
| 155 | } | 249 | } |
| 156 | } | 250 | } |
| 157 | MonitorSet::NoOutputs(mut workspaces) => { | 251 | MonitorSet::NoOutputs(mut workspaces) => { |
| 158 | - if workspaces.iter().all(|ws| ws.original_output != id) { | 252 | + // We know there are no empty workspaces there, so add one. |
| 159 | - workspaces.insert( | 253 | + workspaces.push(Workspace::new(output.clone())); |
| 160 | - 0, | ||
| 161 | - Workspace { | ||
| 162 | - original_output: id.clone(), | ||
| 163 | - layout: Layout::new(), | ||
| 164 | - space: Space::default(), | ||
| 165 | - }, | ||
| 166 | - ); | ||
| 167 | - } | ||
| 168 | 254 | ||
| 169 | for workspace in &mut workspaces { | 255 | for workspace in &mut workspaces { |
| 170 | - workspace.space.map_output(&output, (0, 0)); | 256 | + workspace.set_output(Some(output.clone())); |
| 171 | } | 257 | } |
| 172 | 258 | ||
| 173 | let monitor = Monitor { | 259 | let monitor = Monitor { |
| @@ -199,11 +285,11 @@ impl MonitorSet { | |||
| 199 | let mut workspaces = monitor.workspaces; | 285 | let mut workspaces = monitor.workspaces; |
| 200 | 286 | ||
| 201 | for ws in &mut workspaces { | 287 | for ws in &mut workspaces { |
| 202 | - ws.space.unmap_output(output); | 288 | + ws.set_output(None); |
| 203 | } | 289 | } |
| 204 | 290 | ||
| 205 | // Get rid of empty workspaces. | 291 | // Get rid of empty workspaces. |
| 206 | - workspaces.retain(|ws| ws.space.elements().next().is_some()); | 292 | + workspaces.retain(|ws| ws.has_windows()); |
| 207 | 293 | ||
| 208 | if monitors.is_empty() { | 294 | if monitors.is_empty() { |
| 209 | // Removed the last monitor. | 295 | // Removed the last monitor. |
| @@ -223,9 +309,12 @@ impl MonitorSet { | |||
| 223 | 309 | ||
| 224 | let primary = &mut monitors[primary_idx]; | 310 | let primary = &mut monitors[primary_idx]; |
| 225 | for ws in &mut workspaces { | 311 | for ws in &mut workspaces { |
| 226 | - ws.space.map_output(&primary.output, (0, 0)); | 312 | + ws.set_output(Some(primary.output.clone())); |
| 227 | } | 313 | } |
| 314 | + | ||
| 315 | + let empty = primary.workspaces.remove(primary.workspaces.len() - 1); | ||
| 228 | primary.workspaces.extend(workspaces); | 316 | primary.workspaces.extend(workspaces); |
| 317 | + primary.workspaces.push(empty); | ||
| 229 | 318 | ||
| 230 | MonitorSet::Normal { | 319 | MonitorSet::Normal { |
| 231 | monitors, | 320 | monitors, |
| @@ -240,25 +329,11 @@ impl MonitorSet { | |||
| 240 | } | 329 | } |
| 241 | } | 330 | } |
| 242 | 331 | ||
| 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 | - | ||
| 257 | pub fn add_window( | 332 | pub fn add_window( |
| 258 | &mut self, | 333 | &mut self, |
| 259 | monitor_idx: usize, | 334 | monitor_idx: usize, |
| 260 | workspace_idx: usize, | 335 | workspace_idx: usize, |
| 261 | - window: Window, | 336 | + window: W, |
| 262 | activate: bool, | 337 | activate: bool, |
| 263 | ) { | 338 | ) { |
| 264 | let MonitorSet::Normal { | 339 | let MonitorSet::Normal { |
| @@ -272,31 +347,24 @@ impl MonitorSet { | |||
| 272 | 347 | ||
| 273 | let monitor = &mut monitors[monitor_idx]; | 348 | let monitor = &mut monitors[monitor_idx]; |
| 274 | let workspace = &mut monitor.workspaces[workspace_idx]; | 349 | 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(); | ||
| 281 | 350 | ||
| 282 | if activate { | 351 | if activate { |
| 283 | *active_monitor_idx = monitor_idx; | 352 | *active_monitor_idx = monitor_idx; |
| 284 | monitor.active_workspace_idx = workspace_idx; | 353 | monitor.active_workspace_idx = workspace_idx; |
| 354 | + // Configure will be sent in add_window(). | ||
| 355 | + window.set_activate(true); | ||
| 285 | } | 356 | } |
| 286 | 357 | ||
| 358 | + workspace.add_window(window.clone(), activate); | ||
| 359 | + | ||
| 287 | if workspace_idx == monitor.workspaces.len() - 1 { | 360 | if workspace_idx == monitor.workspaces.len() - 1 { |
| 288 | // Insert a new empty workspace. | 361 | // Insert a new empty workspace. |
| 289 | - let mut ws = Workspace { | 362 | + let ws = Workspace::new(monitor.output.clone()); |
| 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)); | ||
| 295 | monitor.workspaces.push(ws); | 363 | monitor.workspaces.push(ws); |
| 296 | } | 364 | } |
| 297 | } | 365 | } |
| 298 | 366 | ||
| 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) { |
| 300 | let MonitorSet::Normal { monitors, .. } = self else { | 368 | let MonitorSet::Normal { monitors, .. } = self else { |
| 301 | panic!() | 369 | panic!() |
| 302 | }; | 370 | }; |
| @@ -308,38 +376,110 @@ impl MonitorSet { | |||
| 308 | .unwrap(); | 376 | .unwrap(); |
| 309 | let workspace_idx = monitor.active_workspace_idx; | 377 | let workspace_idx = monitor.active_workspace_idx; |
| 310 | 378 | ||
| 311 | - self.add_window(monitor_idx, workspace_idx, window, activate) | 379 | + self.add_window(monitor_idx, workspace_idx, window, activate); |
| 312 | } | 380 | } |
| 313 | 381 | ||
| 314 | - pub fn remove_window(&mut self, window: &Window) { | 382 | + pub fn remove_window(&mut self, window: &W) { |
| 315 | - let MonitorSet::Normal { monitors, .. } = self else { | 383 | + match self { |
| 316 | - panic!() | 384 | + MonitorSet::Normal { monitors, .. } => { |
| 317 | - }; | 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); | ||
| 318 | 407 | ||
| 319 | - let (output, workspace) = monitors | 408 | + // Clean up empty workspaces. |
| 320 | - .iter_mut() | 409 | + if !ws.has_windows() { |
| 321 | - .flat_map(|mon| mon.workspaces.iter_mut().map(|ws| (&mon.output, ws))) | 410 | + workspaces.remove(idx); |
| 322 | - .find(|(_, ws)| ws.space.elements().any(|win| win == window)) | 411 | + } |
| 323 | - .unwrap(); | ||
| 324 | 412 | ||
| 325 | - workspace | 413 | + break; |
| 326 | - .layout | 414 | + } |
| 327 | - .remove_window(window, output_size(output).h); | 415 | + } |
| 328 | - workspace.space.unmap_elem(window); | 416 | + } |
| 329 | - workspace.layout.sync_space(&mut workspace.space); | 417 | + } |
| 418 | + } | ||
| 330 | 419 | ||
| 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 | + } | ||
| 332 | } | 441 | } |
| 333 | 442 | ||
| 334 | - pub fn update_window(&mut self, window: &Window) { | 443 | + pub fn send_frame(&self, output: &Output, time: Duration) { |
| 335 | - let workspace = self | 444 | + if let MonitorSet::Normal { monitors, .. } = self { |
| 336 | - .workspaces() | 445 | + for mon in monitors { |
| 337 | - .find(|ws| ws.space.elements().any(|w| w == window)) | 446 | + if &mon.output == output { |
| 338 | - .unwrap(); | 447 | + mon.workspaces[mon.active_workspace_idx].send_frame(time); |
| 339 | - workspace.layout.sync_space(&mut workspace.space); | 448 | + } |
| 449 | + } | ||
| 450 | + } | ||
| 340 | } | 451 | } |
| 341 | 452 | ||
| 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) { | ||
| 343 | let MonitorSet::Normal { | 483 | let MonitorSet::Normal { |
| 344 | monitors, | 484 | monitors, |
| 345 | active_monitor_idx, | 485 | active_monitor_idx, |
| @@ -351,15 +491,10 @@ impl MonitorSet { | |||
| 351 | 491 | ||
| 352 | for (monitor_idx, mon) in monitors.iter_mut().enumerate() { | 492 | for (monitor_idx, mon) in monitors.iter_mut().enumerate() { |
| 353 | for (workspace_idx, ws) in mon.workspaces.iter_mut().enumerate() { | 493 | 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) { |
| 355 | *active_monitor_idx = monitor_idx; | 495 | *active_monitor_idx = monitor_idx; |
| 356 | mon.active_workspace_idx = workspace_idx; | 496 | mon.active_workspace_idx = workspace_idx; |
| 357 | - | 497 | + ws.activate_window(window); |
| 358 | - let changed = ws.layout.activate_window(window); | ||
| 359 | - if changed { | ||
| 360 | - ws.layout.sync_space(&mut ws.space); | ||
| 361 | - } | ||
| 362 | - | ||
| 363 | break; | 498 | break; |
| 364 | } | 499 | } |
| 365 | } | 500 | } |
| @@ -396,12 +531,12 @@ impl MonitorSet { | |||
| 396 | Some(&monitors[*active_monitor_idx].output) | 531 | Some(&monitors[*active_monitor_idx].output) |
| 397 | } | 532 | } |
| 398 | 533 | ||
| 399 | - fn active_workspace(&mut self) -> Option<&mut Workspace> { | 534 | + fn active_workspace(&mut self) -> Option<&mut Workspace<W>> { |
| 400 | let monitor = self.active_monitor()?; | 535 | let monitor = self.active_monitor()?; |
| 401 | Some(&mut monitor.workspaces[monitor.active_workspace_idx]) | 536 | Some(&mut monitor.workspaces[monitor.active_workspace_idx]) |
| 402 | } | 537 | } |
| 403 | 538 | ||
| 404 | - fn active_monitor(&mut self) -> Option<&mut Monitor> { | 539 | + fn active_monitor(&mut self) -> Option<&mut Monitor<W>> { |
| 405 | let MonitorSet::Normal { | 540 | let MonitorSet::Normal { |
| 406 | monitors, | 541 | monitors, |
| 407 | active_monitor_idx, | 542 | active_monitor_idx, |
| @@ -418,68 +553,56 @@ impl MonitorSet { | |||
| 418 | let Some(workspace) = self.active_workspace() else { | 553 | let Some(workspace) = self.active_workspace() else { |
| 419 | return; | 554 | return; |
| 420 | }; | 555 | }; |
| 421 | - let changed = workspace.layout.move_left(); | 556 | + workspace.move_left(); |
| 422 | - if changed { | ||
| 423 | - workspace.layout.sync_space(&mut workspace.space); | ||
| 424 | - } | ||
| 425 | } | 557 | } |
| 426 | 558 | ||
| 427 | pub fn move_right(&mut self) { | 559 | pub fn move_right(&mut self) { |
| 428 | let Some(workspace) = self.active_workspace() else { | 560 | let Some(workspace) = self.active_workspace() else { |
| 429 | return; | 561 | return; |
| 430 | }; | 562 | }; |
| 431 | - let changed = workspace.layout.move_right(); | 563 | + workspace.move_right(); |
| 432 | - if changed { | ||
| 433 | - workspace.layout.sync_space(&mut workspace.space); | ||
| 434 | - } | ||
| 435 | } | 564 | } |
| 436 | 565 | ||
| 437 | pub fn move_down(&mut self) { | 566 | pub fn move_down(&mut self) { |
| 438 | let Some(workspace) = self.active_workspace() else { | 567 | let Some(workspace) = self.active_workspace() else { |
| 439 | return; | 568 | return; |
| 440 | }; | 569 | }; |
| 441 | - let changed = workspace.layout.move_down(); | 570 | + workspace.move_down(); |
| 442 | - if changed { | ||
| 443 | - workspace.layout.sync_space(&mut workspace.space); | ||
| 444 | - } | ||
| 445 | } | 571 | } |
| 446 | 572 | ||
| 447 | pub fn move_up(&mut self) { | 573 | pub fn move_up(&mut self) { |
| 448 | let Some(workspace) = self.active_workspace() else { | 574 | let Some(workspace) = self.active_workspace() else { |
| 449 | return; | 575 | return; |
| 450 | }; | 576 | }; |
| 451 | - let changed = workspace.layout.move_up(); | 577 | + workspace.move_up(); |
| 452 | - if changed { | ||
| 453 | - workspace.layout.sync_space(&mut workspace.space); | ||
| 454 | - } | ||
| 455 | } | 578 | } |
| 456 | 579 | ||
| 457 | pub fn focus_left(&mut self) { | 580 | pub fn focus_left(&mut self) { |
| 458 | let Some(workspace) = self.active_workspace() else { | 581 | let Some(workspace) = self.active_workspace() else { |
| 459 | return; | 582 | return; |
| 460 | }; | 583 | }; |
| 461 | - workspace.layout.focus_left(); | 584 | + workspace.focus_left(); |
| 462 | } | 585 | } |
| 463 | 586 | ||
| 464 | pub fn focus_right(&mut self) { | 587 | pub fn focus_right(&mut self) { |
| 465 | let Some(workspace) = self.active_workspace() else { | 588 | let Some(workspace) = self.active_workspace() else { |
| 466 | return; | 589 | return; |
| 467 | }; | 590 | }; |
| 468 | - workspace.layout.focus_right(); | 591 | + workspace.focus_right(); |
| 469 | } | 592 | } |
| 470 | 593 | ||
| 471 | pub fn focus_down(&mut self) { | 594 | pub fn focus_down(&mut self) { |
| 472 | let Some(workspace) = self.active_workspace() else { | 595 | let Some(workspace) = self.active_workspace() else { |
| 473 | return; | 596 | return; |
| 474 | }; | 597 | }; |
| 475 | - workspace.layout.focus_down(); | 598 | + workspace.focus_down(); |
| 476 | } | 599 | } |
| 477 | 600 | ||
| 478 | pub fn focus_up(&mut self) { | 601 | pub fn focus_up(&mut self) { |
| 479 | let Some(workspace) = self.active_workspace() else { | 602 | let Some(workspace) = self.active_workspace() else { |
| 480 | return; | 603 | return; |
| 481 | }; | 604 | }; |
| 482 | - workspace.layout.focus_up(); | 605 | + workspace.focus_up(); |
| 483 | } | 606 | } |
| 484 | 607 | ||
| 485 | pub fn move_to_workspace_up(&mut self) { | 608 | pub fn move_to_workspace_up(&mut self) { |
| @@ -493,28 +616,27 @@ impl MonitorSet { | |||
| 493 | }; | 616 | }; |
| 494 | 617 | ||
| 495 | let monitor = &mut monitors[*active_monitor_idx]; | 618 | let monitor = &mut monitors[*active_monitor_idx]; |
| 619 | + let source_workspace_idx = monitor.active_workspace_idx; | ||
| 496 | 620 | ||
| 497 | - let new_idx = monitor.active_workspace_idx.saturating_sub(1); | 621 | + let new_idx = source_workspace_idx.saturating_sub(1); |
| 498 | - if new_idx == monitor.active_workspace_idx { | 622 | + if new_idx == source_workspace_idx { |
| 499 | return; | 623 | return; |
| 500 | } | 624 | } |
| 501 | 625 | ||
| 502 | - let workspace = &mut monitor.workspaces[monitor.active_workspace_idx]; | 626 | + let workspace = &mut monitor.workspaces[source_workspace_idx]; |
| 503 | - if workspace.layout.columns.is_empty() { | 627 | + if workspace.columns.is_empty() { |
| 504 | return; | 628 | return; |
| 505 | } | 629 | } |
| 506 | 630 | ||
| 507 | - let column = &mut workspace.layout.columns[workspace.layout.active_column_idx]; | 631 | + let column = &mut workspace.columns[workspace.active_column_idx]; |
| 508 | let window = column.windows[column.active_window_idx].clone(); | 632 | let window = column.windows[column.active_window_idx].clone(); |
| 509 | - workspace | 633 | + workspace.remove_window(&window); |
| 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); | ||
| 514 | 634 | ||
| 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 | + } | ||
| 516 | 638 | ||
| 517 | - // FIXME: remove empty unfocused workspaces. | 639 | + self.add_window(*active_monitor_idx, new_idx, window, true); |
| 518 | } | 640 | } |
| 519 | 641 | ||
| 520 | pub fn move_to_workspace_down(&mut self) { | 642 | pub fn move_to_workspace_down(&mut self) { |
| @@ -528,32 +650,28 @@ impl MonitorSet { | |||
| 528 | }; | 650 | }; |
| 529 | 651 | ||
| 530 | let monitor = &mut monitors[*active_monitor_idx]; | 652 | let monitor = &mut monitors[*active_monitor_idx]; |
| 653 | + let source_workspace_idx = monitor.active_workspace_idx; | ||
| 531 | 654 | ||
| 532 | - let new_idx = min( | 655 | + let mut new_idx = min(source_workspace_idx + 1, monitor.workspaces.len() - 1); |
| 533 | - monitor.active_workspace_idx + 1, | 656 | + if new_idx == source_workspace_idx { |
| 534 | - monitor.workspaces.len() - 1, | ||
| 535 | - ); | ||
| 536 | - | ||
| 537 | - if new_idx == monitor.active_workspace_idx { | ||
| 538 | return; | 657 | return; |
| 539 | } | 658 | } |
| 540 | 659 | ||
| 541 | - let workspace = &mut monitor.workspaces[monitor.active_workspace_idx]; | 660 | + let workspace = &mut monitor.workspaces[source_workspace_idx]; |
| 542 | - if workspace.layout.columns.is_empty() { | 661 | + if workspace.columns.is_empty() { |
| 543 | return; | 662 | return; |
| 544 | } | 663 | } |
| 545 | 664 | ||
| 546 | - let column = &mut workspace.layout.columns[workspace.layout.active_column_idx]; | 665 | + let column = &mut workspace.columns[workspace.active_column_idx]; |
| 547 | let window = column.windows[column.active_window_idx].clone(); | 666 | let window = column.windows[column.active_window_idx].clone(); |
| 548 | - workspace | 667 | + workspace.remove_window(&window); |
| 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); | ||
| 553 | 668 | ||
| 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 | + } | ||
| 555 | 673 | ||
| 556 | - // FIXME: remove empty unfocused workspaces. | 674 | + self.add_window(*active_monitor_idx, new_idx, window, true); |
| 557 | } | 675 | } |
| 558 | 676 | ||
| 559 | pub fn switch_workspace_up(&mut self) { | 677 | pub fn switch_workspace_up(&mut self) { |
| @@ -561,21 +679,40 @@ impl MonitorSet { | |||
| 561 | return; | 679 | return; |
| 562 | }; | 680 | }; |
| 563 | 681 | ||
| 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 | + } | ||
| 565 | 688 | ||
| 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 | + } | ||
| 567 | } | 696 | } |
| 568 | 697 | ||
| 569 | pub fn switch_workspace_down(&mut self) { | 698 | pub fn switch_workspace_down(&mut self) { |
| 570 | let Some(monitor) = self.active_monitor() else { | 699 | let Some(monitor) = self.active_monitor() else { |
| 571 | return; | 700 | return; |
| 572 | }; | 701 | }; |
| 573 | - monitor.active_workspace_idx = min( | ||
| 574 | - monitor.active_workspace_idx + 1, | ||
| 575 | - monitor.workspaces.len() - 1, | ||
| 576 | - ); | ||
| 577 | 702 | ||
| 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; | ||
| 579 | } | 716 | } |
| 580 | 717 | ||
| 581 | pub fn consume_into_column(&mut self) { | 718 | pub fn consume_into_column(&mut self) { |
| @@ -591,12 +728,7 @@ impl MonitorSet { | |||
| 591 | let monitor = &mut monitors[*active_monitor_idx]; | 728 | let monitor = &mut monitors[*active_monitor_idx]; |
| 592 | 729 | ||
| 593 | let workspace = &mut monitor.workspaces[monitor.active_workspace_idx]; | 730 | let workspace = &mut monitor.workspaces[monitor.active_workspace_idx]; |
| 594 | - let changed = workspace | 731 | + workspace.consume_into_column(); |
| 595 | - .layout | ||
| 596 | - .consume_into_column(output_size(&monitor.output).h); | ||
| 597 | - if changed { | ||
| 598 | - workspace.layout.sync_space(&mut workspace.space); | ||
| 599 | - } | ||
| 600 | } | 732 | } |
| 601 | 733 | ||
| 602 | pub fn expel_from_column(&mut self) { | 734 | pub fn expel_from_column(&mut self) { |
| @@ -610,22 +742,11 @@ impl MonitorSet { | |||
| 610 | }; | 742 | }; |
| 611 | 743 | ||
| 612 | let monitor = &mut monitors[*active_monitor_idx]; | 744 | 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 | - | ||
| 621 | let workspace = &mut monitor.workspaces[monitor.active_workspace_idx]; | 745 | let workspace = &mut monitor.workspaces[monitor.active_workspace_idx]; |
| 622 | - let changed = workspace.layout.expel_from_column(output_size.h); | 746 | + workspace.expel_from_column(); |
| 623 | - if changed { | ||
| 624 | - workspace.layout.sync_space(&mut workspace.space); | ||
| 625 | - } | ||
| 626 | } | 747 | } |
| 627 | 748 | ||
| 628 | - pub fn focus(&self) -> Option<&Window> { | 749 | + pub fn focus(&self) -> Option<&W> { |
| 629 | let MonitorSet::Normal { | 750 | let MonitorSet::Normal { |
| 630 | monitors, | 751 | monitors, |
| 631 | active_monitor_idx, | 752 | active_monitor_idx, |
| @@ -637,65 +758,40 @@ impl MonitorSet { | |||
| 637 | 758 | ||
| 638 | let monitor = &monitors[*active_monitor_idx]; | 759 | let monitor = &monitors[*active_monitor_idx]; |
| 639 | let workspace = &monitor.workspaces[monitor.active_workspace_idx]; | 760 | let workspace = &monitor.workspaces[monitor.active_workspace_idx]; |
| 640 | - if workspace.layout.columns.is_empty() { | 761 | + if !workspace.has_windows() { |
| 641 | return None; | 762 | return None; |
| 642 | } | 763 | } |
| 643 | 764 | ||
| 644 | - let column = &workspace.layout.columns[workspace.layout.active_column_idx]; | 765 | + let column = &workspace.columns[workspace.active_column_idx]; |
| 645 | Some(&column.windows[column.active_window_idx]) | 766 | Some(&column.windows[column.active_window_idx]) |
| 646 | } | 767 | } |
| 647 | 768 | ||
| 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>> { |
| 649 | let MonitorSet::Normal { monitors, .. } = self else { | 770 | let MonitorSet::Normal { monitors, .. } = self else { |
| 650 | return None; | 771 | return None; |
| 651 | }; | 772 | }; |
| 652 | 773 | ||
| 653 | - monitors.iter_mut().find_map(|monitor| { | 774 | + monitors.iter().find_map(|monitor| { |
| 654 | if &monitor.output == output { | 775 | if &monitor.output == output { |
| 655 | - Some(&mut monitor.workspaces[monitor.active_workspace_idx]) | 776 | + Some(&monitor.workspaces[monitor.active_workspace_idx]) |
| 656 | } else { | 777 | } else { |
| 657 | None | 778 | None |
| 658 | } | 779 | } |
| 659 | }) | 780 | }) |
| 660 | } | 781 | } |
| 661 | 782 | ||
| 662 | - pub fn workspaces(&mut self) -> impl Iterator<Item = &mut Workspace> + '_ { | 783 | + pub fn window_under( |
| 663 | - match self { | 784 | + &self, |
| 664 | - MonitorSet::Normal { monitors, .. } => { | 785 | + output: &Output, |
| 665 | - monitors.iter_mut().flat_map(|mon| &mut mon.workspaces) | 786 | + pos_within_output: Point<f64, Logical>, |
| 666 | - } | 787 | + ) -> Option<(&W, Point<i32, Logical>)> { |
| 667 | - MonitorSet::NoOutputs(_workspaces) => todo!(), | 788 | + let ws = self.workspace_for_output(output).unwrap(); |
| 668 | - } | 789 | + ws.window_under(pos_within_output) |
| 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 | - }) | ||
| 692 | } | 790 | } |
| 693 | 791 | ||
| 694 | - /// Refreshes the `Space`s. | 792 | + /// Refreshes the `Workspace`s. |
| 695 | pub fn refresh(&mut self) { | 793 | pub fn refresh(&mut self) { |
| 696 | - for workspace in self.workspaces() { | 794 | + // TODO |
| 697 | - workspace.space.refresh(); | ||
| 698 | - } | ||
| 699 | } | 795 | } |
| 700 | 796 | ||
| 701 | fn verify_invariants(&self) { | 797 | fn verify_invariants(&self) { |
| @@ -708,11 +804,11 @@ impl MonitorSet { | |||
| 708 | MonitorSet::NoOutputs(workspaces) => { | 804 | MonitorSet::NoOutputs(workspaces) => { |
| 709 | for workspace in workspaces { | 805 | for workspace in workspaces { |
| 710 | assert!( | 806 | assert!( |
| 711 | - !workspace.layout.has_windows(), | 807 | + !workspace.has_windows(), |
| 712 | "with no outputs there cannot be empty workspaces" | 808 | "with no outputs there cannot be empty workspaces" |
| 713 | ); | 809 | ); |
| 714 | 810 | ||
| 715 | - workspace.layout.verify_invariants(); | 811 | + workspace.verify_invariants(); |
| 716 | } | 812 | } |
| 717 | 813 | ||
| 718 | return; | 814 | return; |
| @@ -731,13 +827,6 @@ impl MonitorSet { | |||
| 731 | let monitor_id = OutputId::new(&monitor.output); | 827 | let monitor_id = OutputId::new(&monitor.output); |
| 732 | 828 | ||
| 733 | if idx == primary_idx { | 829 | 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 | - ); | ||
| 741 | } else { | 830 | } else { |
| 742 | assert!( | 831 | assert!( |
| 743 | monitor | 832 | monitor |
| @@ -752,76 +841,102 @@ impl MonitorSet { | |||
| 752 | // exists. | 841 | // exists. |
| 753 | 842 | ||
| 754 | for workspace in &monitor.workspaces { | 843 | for workspace in &monitor.workspaces { |
| 755 | - workspace.layout.verify_invariants(); | 844 | + workspace.verify_invariants(); |
| 756 | } | 845 | } |
| 757 | } | 846 | } |
| 758 | } | 847 | } |
| 759 | } | 848 | } |
| 760 | 849 | ||
| 761 | -fn output_size(output: &Output) -> Size<i32, Logical> { | 850 | +impl MonitorSet<Window> { |
| 762 | - let output_scale = output.current_scale().integer_scale(); | 851 | + pub fn render_elements( |
| 763 | - let output_transform = output.current_transform(); | 852 | + &self, |
| 764 | - let output_mode = output.current_mode().unwrap(); | 853 | + renderer: &mut GlesRenderer, |
| 765 | - let output_size = output_transform | 854 | + output: &Output, |
| 766 | - .transform_size(output_mode.size) | 855 | + ) -> Vec<WaylandSurfaceRenderElement<GlesRenderer>> { |
| 767 | - .to_logical(output_scale); | 856 | + let ws = self.workspace_for_output(output).unwrap(); |
| 768 | - output_size | 857 | + ws.render_elements(renderer) |
| 858 | + } | ||
| 769 | } | 859 | } |
| 770 | 860 | ||
| 771 | -impl Default for MonitorSet { | 861 | +impl<W: LayoutElement> Default for MonitorSet<W> { |
| 772 | fn default() -> Self { | 862 | fn default() -> Self { |
| 773 | Self::new() | 863 | Self::new() |
| 774 | } | 864 | } |
| 775 | } | 865 | } |
| 776 | 866 | ||
| 777 | -impl Layout { | 867 | +impl<W: LayoutElement> Workspace<W> { |
| 778 | - fn new() -> Self { | 868 | + fn new(output: Output) -> Self { |
| 779 | Self { | 869 | Self { |
| 870 | + original_output: OutputId::new(&output), | ||
| 871 | + view_size: output_size(&output), | ||
| 872 | + output: Some(output), | ||
| 780 | columns: vec![], | 873 | columns: vec![], |
| 781 | active_column_idx: 0, | 874 | active_column_idx: 0, |
| 875 | + view_offset: 0, | ||
| 782 | } | 876 | } |
| 783 | } | 877 | } |
| 784 | 878 | ||
| 785 | - fn sync_space(&self, space: &mut Space<Window>) { | 879 | + fn refresh(&self) { |
| 786 | - // FIXME: this is really inefficient | 880 | + // FIXME: proper overlap. |
| 787 | - let mut active_window = None; | 881 | + } |
| 788 | 882 | ||
| 789 | - let mut x = PADDING; | 883 | + fn windows(&self) -> impl Iterator<Item = &W> + '_ { |
| 790 | - for (column_idx, column) in self.columns.iter().enumerate() { | 884 | + self.columns.iter().flat_map(|col| col.windows.iter()) |
| 791 | - let mut y = PADDING; | 885 | + } |
| 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 | - } | ||
| 798 | 886 | ||
| 799 | - window.set_activated(active); | 887 | + fn set_output(&mut self, output: Option<Output>) { |
| 800 | - space.map_element(window.clone(), (x, y), false); | 888 | + if self.output == output { |
| 801 | - window.toplevel().send_pending_configure(); | 889 | + return; |
| 802 | - y += window.geometry().size.h + PADDING; | 890 | + } |
| 891 | + | ||
| 892 | + if let Some(output) = self.output.take() { | ||
| 893 | + for win in self.windows() { | ||
| 894 | + win.output_leave(&output); | ||
| 803 | } | 895 | } |
| 804 | - x += column.size().w + PADDING; | ||
| 805 | } | 896 | } |
| 806 | 897 | ||
| 807 | - if let Some(window) = active_window { | 898 | + if let Some(output) = output { |
| 808 | - space.raise_element(&window, false); | 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 | + } | ||
| 809 | } | 906 | } |
| 810 | } | 907 | } |
| 811 | 908 | ||
| 812 | - fn has_windows(&self) -> bool { | 909 | + fn enter_output_for_window(&self, window: &W) { |
| 813 | - self.columns.is_empty() | 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 | + } | ||
| 814 | } | 917 | } |
| 815 | 918 | ||
| 816 | - /// Computes the width of the layout including left and right padding, in Logical coordinates. | 919 | + fn set_view_size(&mut self, size: Size<i32, Logical>) { |
| 817 | - fn width(&self) -> i32 { | 920 | + if self.view_size == size { |
| 818 | - let mut total = PADDING; | 921 | + return; |
| 922 | + } | ||
| 819 | 923 | ||
| 820 | - for column in &self.columns { | 924 | + self.view_size = size; |
| 821 | - total += column.size().w + PADDING; | 925 | + for col in &mut self.columns { |
| 926 | + col.update_window_sizes(self.view_size); | ||
| 822 | } | 927 | } |
| 928 | + } | ||
| 823 | 929 | ||
| 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)) | ||
| 825 | } | 940 | } |
| 826 | 941 | ||
| 827 | /// Computes the X position of the windows in the given column, in logical coordinates. | 942 | /// Computes the X position of the windows in the given column, in logical coordinates. |
| @@ -835,17 +950,25 @@ impl Layout { | |||
| 835 | x | 950 | x |
| 836 | } | 951 | } |
| 837 | 952 | ||
| 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 | + | ||
| 839 | let idx = if self.columns.is_empty() { | 965 | let idx = if self.columns.is_empty() { |
| 840 | 0 | 966 | 0 |
| 841 | } else { | 967 | } else { |
| 842 | self.active_column_idx + 1 | 968 | self.active_column_idx + 1 |
| 843 | }; | 969 | }; |
| 844 | 970 | ||
| 845 | - let column = Column { | 971 | + let column = Column::new(window, self.view_size); |
| 846 | - windows: vec![window], | ||
| 847 | - active_window_idx: 0, | ||
| 848 | - }; | ||
| 849 | self.columns.insert(idx, column); | 972 | self.columns.insert(idx, column); |
| 850 | 973 | ||
| 851 | if activate { | 974 | if activate { |
| @@ -853,11 +976,15 @@ impl Layout { | |||
| 853 | } | 976 | } |
| 854 | } | 977 | } |
| 855 | 978 | ||
| 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 | + | ||
| 857 | let column_idx = self | 984 | let column_idx = self |
| 858 | .columns | 985 | .columns |
| 859 | .iter() | 986 | .iter() |
| 860 | - .position(|col| col.windows.contains(window)) | 987 | + .position(|col| col.contains(window)) |
| 861 | .unwrap(); | 988 | .unwrap(); |
| 862 | let column = &mut self.columns[column_idx]; | 989 | let column = &mut self.columns[column_idx]; |
| 863 | 990 | ||
| @@ -870,51 +997,79 @@ impl Layout { | |||
| 870 | } | 997 | } |
| 871 | 998 | ||
| 872 | self.active_column_idx = min(self.active_column_idx, self.columns.len() - 1); | 999 | 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(); | ||
| 873 | return; | 1004 | return; |
| 874 | } | 1005 | } |
| 875 | 1006 | ||
| 876 | column.active_window_idx = min(column.active_window_idx, column.windows.len() - 1); | 1007 | 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 | + } | ||
| 877 | 1014 | ||
| 878 | - // Update window sizes. | 1015 | + fn update_window(&mut self, window: &W) { |
| 879 | - let window_count = column.windows.len() as i32; | 1016 | + let column = self |
| 880 | - let height = (total_height - PADDING * (window_count + 1)) / window_count; | 1017 | + .columns |
| 881 | - let width = column.size().w; | 1018 | + .iter_mut() |
| 1019 | + .find(|col| col.contains(window)) | ||
| 1020 | + .unwrap(); | ||
| 1021 | + column.update_window_sizes(self.view_size); | ||
| 1022 | + } | ||
| 882 | 1023 | ||
| 883 | - for window in &mut column.windows { | 1024 | + fn activate_window(&mut self, window: &W) { |
| 884 | - window | 1025 | + for win in self.windows() { |
| 885 | - .toplevel() | 1026 | + if win != window { |
| 886 | - .with_pending_state(|state| state.size = Some(Size::from((width, height)))); | 1027 | + win.set_activate(false); |
| 887 | - window.toplevel().send_pending_configure(); | 1028 | + win.send_pending_configure(); |
| 1029 | + } | ||
| 888 | } | 1030 | } |
| 889 | - } | 1031 | + window.set_activate(true); |
| 1032 | + window.send_pending_configure(); | ||
| 890 | 1033 | ||
| 891 | - fn activate_window(&mut self, window: &Window) -> bool { | ||
| 892 | let column_idx = self | 1034 | let column_idx = self |
| 893 | .columns | 1035 | .columns |
| 894 | .iter() | 1036 | .iter() |
| 895 | - .position(|col| col.windows.contains(window)) | 1037 | + .position(|col| col.contains(window)) |
| 896 | .unwrap(); | 1038 | .unwrap(); |
| 897 | let column = &mut self.columns[column_idx]; | 1039 | let column = &mut self.columns[column_idx]; |
| 898 | 1040 | ||
| 899 | - let window_idx = column.windows.iter().position(|win| win == window).unwrap(); | 1041 | + column.activate_window(window); |
| 900 | - | 1042 | + self.active_column_idx = column_idx; |
| 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 | - } | ||
| 908 | } | 1043 | } |
| 909 | 1044 | ||
| 910 | fn verify_invariants(&self) { | 1045 | 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 | + | ||
| 911 | for column in &self.columns { | 1051 | for column in &self.columns { |
| 912 | column.verify_invariants(); | 1052 | column.verify_invariants(); |
| 913 | } | 1053 | } |
| 914 | } | 1054 | } |
| 915 | 1055 | ||
| 916 | fn focus_left(&mut self) { | 1056 | 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(); | ||
| 918 | } | 1073 | } |
| 919 | 1074 | ||
| 920 | fn focus_right(&mut self) { | 1075 | fn focus_right(&mut self) { |
| @@ -922,7 +1077,22 @@ impl Layout { | |||
| 922 | return; | 1077 | return; |
| 923 | } | 1078 | } |
| 924 | 1079 | ||
| 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(); | ||
| 926 | } | 1096 | } |
| 927 | 1097 | ||
| 928 | fn focus_down(&mut self) { | 1098 | fn focus_down(&mut self) { |
| @@ -941,104 +1111,217 @@ impl Layout { | |||
| 941 | self.columns[self.active_column_idx].focus_up(); | 1111 | self.columns[self.active_column_idx].focus_up(); |
| 942 | } | 1112 | } |
| 943 | 1113 | ||
| 944 | - fn move_left(&mut self) -> bool { | 1114 | + fn move_left(&mut self) { |
| 945 | let new_idx = self.active_column_idx.saturating_sub(1); | 1115 | let new_idx = self.active_column_idx.saturating_sub(1); |
| 946 | if self.active_column_idx == new_idx { | 1116 | if self.active_column_idx == new_idx { |
| 947 | - return false; | 1117 | + return; |
| 948 | } | 1118 | } |
| 949 | 1119 | ||
| 950 | self.columns.swap(self.active_column_idx, new_idx); | 1120 | self.columns.swap(self.active_column_idx, new_idx); |
| 951 | self.active_column_idx = new_idx; | 1121 | self.active_column_idx = new_idx; |
| 952 | - true | ||
| 953 | } | 1122 | } |
| 954 | 1123 | ||
| 955 | - fn move_right(&mut self) -> bool { | 1124 | + fn move_right(&mut self) { |
| 956 | if self.columns.is_empty() { | 1125 | if self.columns.is_empty() { |
| 957 | - return false; | 1126 | + return; |
| 958 | } | 1127 | } |
| 959 | 1128 | ||
| 960 | let new_idx = min(self.active_column_idx + 1, self.columns.len() - 1); | 1129 | let new_idx = min(self.active_column_idx + 1, self.columns.len() - 1); |
| 961 | if self.active_column_idx == new_idx { | 1130 | if self.active_column_idx == new_idx { |
| 962 | - return false; | 1131 | + return; |
| 963 | } | 1132 | } |
| 964 | 1133 | ||
| 965 | self.columns.swap(self.active_column_idx, new_idx); | 1134 | self.columns.swap(self.active_column_idx, new_idx); |
| 966 | self.active_column_idx = new_idx; | 1135 | self.active_column_idx = new_idx; |
| 967 | - true | ||
| 968 | } | 1136 | } |
| 969 | 1137 | ||
| 970 | - fn move_down(&mut self) -> bool { | 1138 | + fn move_down(&mut self) { |
| 971 | if self.columns.is_empty() { | 1139 | if self.columns.is_empty() { |
| 972 | - return false; | 1140 | + return; |
| 973 | } | 1141 | } |
| 974 | 1142 | ||
| 975 | - self.columns[self.active_column_idx].move_down() | 1143 | + self.columns[self.active_column_idx].move_down(); |
| 976 | } | 1144 | } |
| 977 | 1145 | ||
| 978 | - fn move_up(&mut self) -> bool { | 1146 | + fn move_up(&mut self) { |
| 979 | if self.columns.is_empty() { | 1147 | if self.columns.is_empty() { |
| 980 | - return false; | 1148 | + return; |
| 981 | } | 1149 | } |
| 982 | 1150 | ||
| 983 | - self.columns[self.active_column_idx].move_up() | 1151 | + self.columns[self.active_column_idx].move_up(); |
| 984 | } | 1152 | } |
| 985 | 1153 | ||
| 986 | - fn consume_into_column(&mut self, total_height: i32) -> bool { | 1154 | + fn consume_into_column(&mut self) { |
| 987 | if self.columns.len() < 2 { | 1155 | if self.columns.len() < 2 { |
| 988 | - return false; | 1156 | + return; |
| 989 | } | 1157 | } |
| 990 | 1158 | ||
| 991 | if self.active_column_idx == self.columns.len() - 1 { | 1159 | if self.active_column_idx == self.columns.len() - 1 { |
| 992 | - return false; | 1160 | + return; |
| 993 | } | 1161 | } |
| 994 | 1162 | ||
| 995 | let source_column_idx = self.active_column_idx + 1; | 1163 | let source_column_idx = self.active_column_idx + 1; |
| 996 | 1164 | ||
| 997 | let source_column = &mut self.columns[source_column_idx]; | 1165 | let source_column = &mut self.columns[source_column_idx]; |
| 998 | let window = source_column.windows[0].clone(); | 1166 | let window = source_column.windows[0].clone(); |
| 999 | - self.remove_window(&window, total_height); | 1167 | + self.remove_window(&window); |
| 1000 | 1168 | ||
| 1001 | let target_column = &mut self.columns[self.active_column_idx]; | 1169 | let target_column = &mut self.columns[self.active_column_idx]; |
| 1002 | - | 1170 | + target_column.add_window(self.view_size, window); |
| 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 | ||
| 1016 | } | 1171 | } |
| 1017 | 1172 | ||
| 1018 | - fn expel_from_column(&mut self, total_height: i32) -> bool { | 1173 | + fn expel_from_column(&mut self) { |
| 1019 | if self.columns.is_empty() { | 1174 | if self.columns.is_empty() { |
| 1020 | - return false; | 1175 | + return; |
| 1021 | } | 1176 | } |
| 1022 | 1177 | ||
| 1023 | let source_column = &mut self.columns[self.active_column_idx]; | 1178 | let source_column = &mut self.columns[self.active_column_idx]; |
| 1024 | if source_column.windows.len() == 1 { | 1179 | if source_column.windows.len() == 1 { |
| 1025 | - return false; | 1180 | + return; |
| 1026 | } | 1181 | } |
| 1027 | 1182 | ||
| 1028 | let window = source_column.windows[source_column.active_window_idx].clone(); | 1183 | let window = source_column.windows[source_column.active_window_idx].clone(); |
| 1029 | - self.remove_window(&window, total_height); | 1184 | + self.remove_window(&window); |
| 1030 | 1185 | ||
| 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(); | ||
| 1035 | self.add_window(window, true); | 1186 | 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 | + } | ||
| 1036 | 1195 | ||
| 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 | ||
| 1038 | } | 1263 | } |
| 1039 | } | 1264 | } |
| 1040 | 1265 | ||
| 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 | + | ||
| 1042 | /// Computes the size of the column including top and bottom padding. | 1325 | /// Computes the size of the column including top and bottom padding. |
| 1043 | fn size(&self) -> Size<i32, Logical> { | 1326 | fn size(&self) -> Size<i32, Logical> { |
| 1044 | let mut total = Size::from((0, PADDING)); | 1327 | let mut total = Size::from((0, PADDING)); |
| @@ -1052,48 +1335,77 @@ impl Column { | |||
| 1052 | total | 1335 | total |
| 1053 | } | 1336 | } |
| 1054 | 1337 | ||
| 1055 | - fn window_y(&self, window_idx: usize) -> i32 { | 1338 | + fn focus_up(&mut self) { |
| 1056 | - let mut y = PADDING; | 1339 | + let new_idx = self.active_window_idx.saturating_sub(1); |
| 1057 | - | 1340 | + if self.active_window_idx == new_idx { |
| 1058 | - for window in self.windows.iter().take(window_idx) { | 1341 | + return; |
| 1059 | - let size = window.geometry().size; | ||
| 1060 | - y += size.h + PADDING; | ||
| 1061 | } | 1342 | } |
| 1062 | 1343 | ||
| 1063 | - y | 1344 | + self.windows[self.active_window_idx].set_activate(false); |
| 1064 | - } | 1345 | + self.windows[self.active_window_idx].send_pending_configure(); |
| 1065 | - | 1346 | + self.windows[new_idx].set_activate(true); |
| 1066 | - fn focus_up(&mut self) { | 1347 | + self.windows[new_idx].send_pending_configure(); |
| 1067 | - self.active_window_idx = self.active_window_idx.saturating_sub(1); | 1348 | + self.active_window_idx = new_idx; |
| 1068 | } | 1349 | } |
| 1069 | 1350 | ||
| 1070 | fn focus_down(&mut self) { | 1351 | 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; | ||
| 1072 | } | 1362 | } |
| 1073 | 1363 | ||
| 1074 | - fn move_up(&mut self) -> bool { | 1364 | + fn move_up(&mut self) { |
| 1075 | let new_idx = self.active_window_idx.saturating_sub(1); | 1365 | let new_idx = self.active_window_idx.saturating_sub(1); |
| 1076 | if self.active_window_idx == new_idx { | 1366 | if self.active_window_idx == new_idx { |
| 1077 | - return false; | 1367 | + return; |
| 1078 | } | 1368 | } |
| 1079 | 1369 | ||
| 1080 | self.windows.swap(self.active_window_idx, new_idx); | 1370 | self.windows.swap(self.active_window_idx, new_idx); |
| 1081 | self.active_window_idx = new_idx; | 1371 | self.active_window_idx = new_idx; |
| 1082 | - true | ||
| 1083 | } | 1372 | } |
| 1084 | 1373 | ||
| 1085 | - fn move_down(&mut self) -> bool { | 1374 | + fn move_down(&mut self) { |
| 1086 | let new_idx = min(self.active_window_idx + 1, self.windows.len() - 1); | 1375 | let new_idx = min(self.active_window_idx + 1, self.windows.len() - 1); |
| 1087 | if self.active_window_idx == new_idx { | 1376 | if self.active_window_idx == new_idx { |
| 1088 | - return false; | 1377 | + return; |
| 1089 | } | 1378 | } |
| 1090 | 1379 | ||
| 1091 | self.windows.swap(self.active_window_idx, new_idx); | 1380 | self.windows.swap(self.active_window_idx, new_idx); |
| 1092 | self.active_window_idx = new_idx; | 1381 | self.active_window_idx = new_idx; |
| 1093 | - true | ||
| 1094 | } | 1382 | } |
| 1095 | 1383 | ||
| 1096 | fn verify_invariants(&self) { | 1384 | fn verify_invariants(&self) { |
| 1097 | assert!(!self.windows.is_empty(), "columns can't be empty"); | 1385 | assert!(!self.windows.is_empty(), "columns can't be empty"); |
| 1386 | + assert!(self.active_window_idx < self.windows.len()); | ||
| 1098 | } | 1387 | } |
| 1099 | } | 1388 | } |
| 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 @@ | |||
| 1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
| 2 | use std::os::unix::io::AsRawFd; | 2 | use std::os::unix::io::AsRawFd; |
| 3 | use std::sync::Arc; | 3 | use std::sync::Arc; |
| 4 | -use std::time::Duration; | ||
| 5 | 4 | ||
| 5 | +use smithay::backend::renderer::element::render_elements; | ||
| 6 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; | 6 | 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; |
| 8 | use smithay::backend::renderer::ImportAll; | 8 | use smithay::backend::renderer::ImportAll; |
| 9 | -use smithay::desktop::space::{space_render_elements, SpaceRenderElements}; | ||
| 10 | use smithay::desktop::{PopupManager, Space, Window, WindowSurfaceType}; | 9 | use smithay::desktop::{PopupManager, Space, Window, WindowSurfaceType}; |
| 11 | use smithay::input::keyboard::XkbConfig; | 10 | use smithay::input::keyboard::XkbConfig; |
| 12 | use smithay::input::{Seat, SeatState}; | 11 | use smithay::input::{Seat, SeatState}; |
| @@ -39,7 +38,7 @@ pub struct Niri { | |||
| 39 | 38 | ||
| 40 | // Each workspace corresponds to a Space. Each workspace generally has one Output mapped to it, | 39 | // Each workspace corresponds to a Space. Each workspace generally has one Output mapped to it, |
| 41 | // however it may have none (when there are no outputs connected) or mutiple (when mirroring). | 40 | // 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>, |
| 43 | 42 | ||
| 44 | // This space does not actually contain any windows, but all outputs are mapped into it | 43 | // This space does not actually contain any windows, but all outputs are mapped into it |
| 45 | // according to their global position. | 44 | // according to their global position. |
| @@ -200,7 +199,7 @@ impl Niri { | |||
| 200 | } | 199 | } |
| 201 | 200 | ||
| 202 | pub fn output_resized(&mut self, output: Output) { | 201 | pub fn output_resized(&mut self, output: Output) { |
| 203 | - // FIXME resize windows etc | 202 | + self.monitor_set.update_output(&output); |
| 204 | self.queue_redraw(output); | 203 | self.queue_redraw(output); |
| 205 | } | 204 | } |
| 206 | 205 | ||
| @@ -217,40 +216,11 @@ impl Niri { | |||
| 217 | Some((output, pos_within_output)) | 216 | Some((output, pos_within_output)) |
| 218 | } | 217 | } |
| 219 | 218 | ||
| 220 | - pub fn window_under( | 219 | + pub fn window_under_cursor(&self) -> Option<&Window> { |
| 221 | - &mut self, | 220 | + let pos = self.seat.get_pointer().unwrap().current_location(); |
| 222 | - pos: Point<f64, Logical>, | ||
| 223 | - ) -> Option<(&mut Space<Window>, Window, Point<i32, Logical>)> { | ||
| 224 | let (output, pos_within_output) = self.output_under(pos)?; | 221 | let (output, pos_within_output) = self.output_under(pos)?; |
| 225 | - let output = output.clone(); | 222 | + let (window, _loc) = self.monitor_set.window_under(output, pos_within_output)?; |
| 226 | - | 223 | + Some(window) |
| 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 | - }) | ||
| 254 | } | 224 | } |
| 255 | 225 | ||
| 256 | /// Returns the surface under cursor and its position in the global space. | 226 | /// Returns the surface under cursor and its position in the global space. |
| @@ -263,23 +233,17 @@ impl Niri { | |||
| 263 | pos: Point<f64, Logical>, | 233 | pos: Point<f64, Logical>, |
| 264 | ) -> Option<(WlSurface, Point<i32, Logical>)> { | 234 | ) -> Option<(WlSurface, Point<i32, Logical>)> { |
| 265 | let (output, pos_within_output) = self.output_under(pos)?; | 235 | let (output, pos_within_output) = self.output_under(pos)?; |
| 266 | - let output = output.clone(); | 236 | + let (window, win_pos_within_output) = |
| 267 | - | 237 | + self.monitor_set.window_under(output, pos_within_output)?; |
| 268 | - let workspace = &self.monitor_set.workspace_for_output(&output)?; | 238 | + |
| 269 | - let space = &workspace.space; | 239 | + let (surface, surface_pos_within_output) = window |
| 270 | - let output_pos_in_local_space = space.output_geometry(&output).unwrap().loc; | 240 | + .surface_under( |
| 271 | - let pos_within_space = pos_within_output + output_pos_in_local_space.to_f64(); | 241 | + pos_within_output - win_pos_within_output.to_f64(), |
| 272 | - | 242 | + WindowSurfaceType::ALL, |
| 273 | - let (surface, surface_loc_in_local_space) = space | 243 | + ) |
| 274 | - .element_under(pos_within_space) | 244 | + .map(|(s, pos_within_window)| (s, pos_within_window + win_pos_within_output))?; |
| 275 | - .and_then(|(window, location)| { | 245 | + let output_pos_in_global_space = self.global_space.output_geometry(output).unwrap().loc; |
| 276 | - window | 246 | + let surface_loc_in_global_space = surface_pos_within_output + output_pos_in_global_space; |
| 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; | ||
| 283 | 247 | ||
| 284 | Some((surface, surface_loc_in_global_space)) | 248 | Some((surface, surface_loc_in_global_space)) |
| 285 | } | 249 | } |
| @@ -338,8 +302,7 @@ impl Niri { | |||
| 338 | assert!(state.queued_redraw.take().is_some()); | 302 | assert!(state.queued_redraw.take().is_some()); |
| 339 | assert!(!state.waiting_for_vblank); | 303 | assert!(!state.waiting_for_vblank); |
| 340 | 304 | ||
| 341 | - let space = &self.monitor_set.workspace_for_output(output).unwrap().space; | 305 | + let elements = self.monitor_set.render_elements(backend.renderer(), output); |
| 342 | - let elements = space_render_elements(backend.renderer(), [space], output, 1.).unwrap(); | ||
| 343 | 306 | ||
| 344 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; | 307 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; |
| 345 | let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); | 308 | let pointer_pos = self.seat.get_pointer().unwrap().current_location() - output_pos.to_f64(); |
| @@ -360,41 +323,18 @@ impl Niri { | |||
| 360 | 323 | ||
| 361 | backend.render(self, output, &elements); | 324 | backend.render(self, output, &elements); |
| 362 | 325 | ||
| 363 | - let space = &self.monitor_set.workspace_for_output(output).unwrap().space; | 326 | + self.monitor_set |
| 364 | - space.elements().for_each(|window| { | 327 | + .send_frame(output, self.start_time.elapsed()); |
| 365 | - window.send_frame( | ||
| 366 | - output, | ||
| 367 | - self.start_time.elapsed(), | ||
| 368 | - Some(Duration::ZERO), | ||
| 369 | - |_, _| Some(output.clone()), | ||
| 370 | - ) | ||
| 371 | - }); | ||
| 372 | } | 328 | } |
| 373 | } | 329 | } |
| 374 | 330 | ||
| 375 | render_elements! { | 331 | render_elements! { |
| 376 | - pub OutputRenderElements<R, E> where R: ImportAll; | 332 | + #[derive(Debug)] |
| 377 | - Space = SpaceRenderElements<R, E>, | 333 | + pub OutputRenderElements<R> where R: ImportAll; |
| 334 | + Wayland = WaylandSurfaceRenderElement<R>, | ||
| 378 | Pointer = SolidColorRenderElement, | 335 | Pointer = SolidColorRenderElement, |
| 379 | } | 336 | } |
| 380 | 337 | ||
| 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 | - | ||
| 398 | #[derive(Default)] | 338 | #[derive(Default)] |
| 399 | pub struct ClientState { | 339 | pub struct ClientState { |
| 400 | pub compositor_state: CompositorClientState, | 340 | pub compositor_state: CompositorClientState, |
src/tty.rsmodified+1 -5
| @@ -10,7 +10,6 @@ use smithay::backend::drm::compositor::DrmCompositor; | |||
| 10 | use smithay::backend::drm::{DrmDevice, DrmDeviceFd, DrmEvent}; | 10 | use smithay::backend::drm::{DrmDevice, DrmDeviceFd, DrmEvent}; |
| 11 | use smithay::backend::egl::{EGLContext, EGLDisplay}; | 11 | use smithay::backend::egl::{EGLContext, EGLDisplay}; |
| 12 | use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface}; | 12 | use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface}; |
| 13 | -use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; | ||
| 14 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; | 13 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 15 | use smithay::backend::renderer::{Bind, ImportEgl}; | 14 | use smithay::backend::renderer::{Bind, ImportEgl}; |
| 16 | use smithay::backend::session::libseat::LibSeatSession; | 15 | use smithay::backend::session::libseat::LibSeatSession; |
| @@ -73,10 +72,7 @@ impl Backend for Tty { | |||
| 73 | &mut self, | 72 | &mut self, |
| 74 | niri: &mut Niri, | 73 | niri: &mut Niri, |
| 75 | output: &Output, | 74 | output: &Output, |
| 76 | - elements: &[OutputRenderElements< | 75 | + elements: &[OutputRenderElements<GlesRenderer>], |
| 77 | - GlesRenderer, | ||
| 78 | - WaylandSurfaceRenderElement<GlesRenderer>, | ||
| 79 | - >], | ||
| 80 | ) { | 76 | ) { |
| 81 | let _span = tracy_client::span!("Tty::render"); | 77 | let _span = tracy_client::span!("Tty::render"); |
| 82 | 78 | ||
src/winit.rsmodified+1 -5
| @@ -1,7 +1,6 @@ | |||
| 1 | use std::time::Duration; | 1 | use std::time::Duration; |
| 2 | 2 | ||
| 3 | use smithay::backend::renderer::damage::OutputDamageTracker; | 3 | use smithay::backend::renderer::damage::OutputDamageTracker; |
| 4 | -use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; | ||
| 5 | use smithay::backend::renderer::gles::GlesRenderer; | 4 | use smithay::backend::renderer::gles::GlesRenderer; |
| 6 | use smithay::backend::renderer::ImportEgl; | 5 | use smithay::backend::renderer::ImportEgl; |
| 7 | use smithay::backend::winit::{self, WinitError, WinitEvent, WinitEventLoop, WinitGraphicsBackend}; | 6 | use smithay::backend::winit::{self, WinitError, WinitEvent, WinitEventLoop, WinitGraphicsBackend}; |
| @@ -37,10 +36,7 @@ impl Backend for Winit { | |||
| 37 | &mut self, | 36 | &mut self, |
| 38 | _niri: &mut Niri, | 37 | _niri: &mut Niri, |
| 39 | _output: &Output, | 38 | _output: &Output, |
| 40 | - elements: &[OutputRenderElements< | 39 | + elements: &[OutputRenderElements<GlesRenderer>], |
| 41 | - GlesRenderer, | ||
| 42 | - WaylandSurfaceRenderElement<GlesRenderer>, | ||
| 43 | - >], | ||
| 44 | ) { | 40 | ) { |
| 45 | let _span = tracy_client::span!("Winit::render"); | 41 | let _span = tracy_client::span!("Winit::render"); |
| 46 | 42 | ||