shell: Cleanup and don't abstract over layouts anymore

This commit is contained in:
Victoria Brekenfeld 2022-07-04 15:28:03 +02:00
parent a088f7fd6e
commit b126dfaf77
12 changed files with 1165 additions and 1974 deletions

View file

@ -1,151 +0,0 @@
use super::{FocusDirection, Layout, Orientation};
use smithay::{
desktop::{Space, Window},
reexports::wayland_protocols::xdg_shell::server::xdg_toplevel::ResizeEdge,
wayland::{
seat::{PointerGrabStartData, Seat},
Serial,
},
};
struct Filtered;
pub struct Combined<A: Layout, B: Layout> {
first: A,
second: B,
windows_a: Vec<Window>,
windows_b: Vec<Window>,
filter: Box<dyn Fn(&Window) -> bool>,
}
impl<A: Layout, B: Layout> Combined<A, B> {
pub fn new(first: A, second: B, filter: impl Fn(&Window) -> bool + 'static) -> Combined<A, B> {
Combined {
first,
second,
windows_a: Vec::new(),
windows_b: Vec::new(),
filter: Box::new(filter),
}
}
}
impl<A: Layout, B: Layout> Layout for Combined<A, B> {
fn map_window<'a>(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
) {
if (self.filter)(window) {
self.windows_b.push(window.clone());
self.second.map_window(
space,
window,
seat,
Box::new(focus_stack.filter(|w| w.user_data().get::<Filtered>().is_some())),
)
} else {
self.windows_a.push(window.clone());
self.first.map_window(
space,
window,
seat,
Box::new(focus_stack.filter(|w| w.user_data().get::<Filtered>().is_none())),
)
}
}
fn refresh(&mut self, space: &mut Space) {
self.first.refresh(space);
self.second.refresh(space);
self.windows_a.retain(|w| w.toplevel().alive());
self.windows_b.retain(|w| w.toplevel().alive());
}
fn unmap_window(&mut self, space: &mut Space, window: &Window) {
if self.windows_a.contains(window) {
self.windows_a.retain(|w| w != window);
self.first.unmap_window(space, window)
} else if self.windows_b.contains(window) {
self.windows_b.retain(|w| w != window);
self.second.unmap_window(space, window)
}
}
fn move_request(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
serial: Serial,
start_data: PointerGrabStartData,
) {
if self.windows_a.contains(window) {
self.first
.move_request(space, window, seat, serial, start_data)
} else if self.windows_b.contains(window) {
self.second
.move_request(space, window, seat, serial, start_data)
}
}
fn resize_request(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
serial: Serial,
start_data: PointerGrabStartData,
edges: ResizeEdge,
) {
if self.windows_a.contains(window) {
self.first
.resize_request(space, window, seat, serial, start_data, edges)
} else if self.windows_b.contains(window) {
self.second
.resize_request(space, window, seat, serial, start_data, edges)
}
}
fn move_focus<'a>(
&mut self,
direction: FocusDirection,
seat: &Seat,
space: &mut Space,
focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
) -> Option<Window> {
let focus_stack = focus_stack.collect::<Vec<_>>();
match self.first.move_focus(
direction,
seat,
space,
Box::new(focus_stack.clone().into_iter()),
) {
Some(window) => Some(window),
None => {
self.second
.move_focus(direction, seat, space, Box::new(focus_stack.into_iter()))
}
}
}
fn update_orientation<'a>(
&mut self,
orientation: Orientation,
seat: &Seat,
space: &mut Space,
focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
) {
let focus_stack = focus_stack.collect::<Vec<_>>();
self.first.update_orientation(
orientation,
seat,
space,
Box::new(focus_stack.clone().into_iter()),
);
self.second
.update_orientation(orientation, seat, space, Box::new(focus_stack.into_iter()));
}
}

View file

@ -3,73 +3,70 @@
use smithay::{
desktop::{Kind, Window},
reexports::{
wayland_protocols::xdg_shell::server::xdg_toplevel,
wayland_server::protocol::{wl_pointer::ButtonState, wl_surface},
wayland_protocols::xdg::shell::server::xdg_toplevel,
wayland_server::DisplayHandle,
},
utils::{Logical, Point, Size},
utils::{IsAlive, Logical, Point, Size},
wayland::{
compositor::with_states,
seat::{AxisFrame, PointerGrab, PointerGrabStartData, PointerInnerHandle},
seat::{AxisFrame, PointerGrab, PointerGrabStartData, PointerInnerHandle, MotionEvent, ButtonEvent},
shell::xdg::{SurfaceCachedState, ToplevelConfigure, XdgToplevelSurfaceRoleAttributes},
Serial,
},
};
use std::{cell::RefCell, sync::Mutex};
#[derive(Debug, Default)]
struct MoveData {
new_location: Point<i32, Logical>,
}
use crate::utils::prelude::*;
use std::{
cell::RefCell,
convert::TryFrom,
sync::Mutex,
};
pub struct MoveSurfaceGrab {
start_data: PointerGrabStartData,
window: Window,
initial_window_location: Point<i32, Logical>,
delta: Point<f64, Logical>,
}
impl PointerGrab for MoveSurfaceGrab {
impl PointerGrab<State> for MoveSurfaceGrab {
fn motion(
&mut self,
handle: &mut PointerInnerHandle<'_>,
location: Point<f64, Logical>,
_focus: Option<(wl_surface::WlSurface, Point<i32, Logical>)>,
serial: Serial,
time: u32,
data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
event: &MotionEvent
) {
// While the grab is active, no client has pointer focus
handle.motion(location, None, serial, time);
let delta = location - self.start_data.location;
let new_location = self.initial_window_location.to_f64() + delta;
self.window
.user_data()
.insert_if_missing(|| RefCell::<Option<MoveData>>::new(None));
let data = self
.window
.user_data()
.get::<RefCell<Option<MoveData>>>()
.unwrap();
*data.borrow_mut() = Some(MoveData {
new_location: new_location.to_i32_round(),
});
handle.motion(event.location, None, event.serial, event.time);
self.delta = event.location - self.start_data.location;
if let Some(workspace) = data.common.shell.space_for_surface_mut(self.window.toplevel().wl_surface()) {
let new_location = (self.initial_window_location.to_f64() + self.delta).to_i32_round();
workspace.space.map_window(&self.window, new_location, true);
}
}
fn button(
&mut self,
handle: &mut PointerInnerHandle<'_>,
button: u32,
state: ButtonState,
serial: Serial,
time: u32,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
event: &ButtonEvent,
) {
handle.button(button, state, serial, time);
handle.button(event.button, event.state, event.serial, event.time);
if handle.current_pressed().is_empty() {
// No more buttons are pressed, release the grab.
handle.unset_grab(serial, time);
handle.unset_grab(event.serial, event.time);
}
}
fn axis(&mut self, handle: &mut PointerInnerHandle<'_>, details: AxisFrame) {
fn axis(
&mut self,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
details: AxisFrame,
) {
handle.axis(details)
}
@ -88,19 +85,9 @@ impl MoveSurfaceGrab {
start_data,
window,
initial_window_location,
delta: (0.0, 0.0).into(),
}
}
pub fn apply_move_state(window: &Window) -> Option<Point<i32, Logical>> {
window
.user_data()
.get::<RefCell<Option<MoveData>>>()
.and_then(|opt| {
opt.borrow_mut()
.take()
.map(|move_data| move_data.new_location)
})
}
}
bitflags::bitflags! {
@ -120,14 +107,14 @@ bitflags::bitflags! {
impl From<xdg_toplevel::ResizeEdge> for ResizeEdge {
#[inline]
fn from(x: xdg_toplevel::ResizeEdge) -> Self {
Self::from_bits(x.to_raw()).unwrap()
Self::from_bits(x.into()).unwrap()
}
}
impl From<ResizeEdge> for xdg_toplevel::ResizeEdge {
#[inline]
fn from(x: ResizeEdge) -> Self {
Self::from_raw(x.bits()).unwrap()
Self::try_from(x.bits()).unwrap()
}
}
@ -169,25 +156,24 @@ pub struct ResizeSurfaceGrab {
last_window_size: Size<i32, Logical>,
}
impl PointerGrab for ResizeSurfaceGrab {
impl PointerGrab<State> for ResizeSurfaceGrab {
fn motion(
&mut self,
handle: &mut PointerInnerHandle<'_>,
location: Point<f64, Logical>,
_focus: Option<(wl_surface::WlSurface, Point<i32, Logical>)>,
serial: Serial,
time: u32,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
event: &MotionEvent,
) {
// While the grab is active, no client has pointer focus
handle.motion(location, None, serial, time);
handle.motion(event.location, None, event.serial, event.time);
// It is impossible to get `min_size` and `max_size` of dead toplevel, so we return early.
if !self.window.toplevel().alive() | self.window.toplevel().get_surface().is_none() {
handle.unset_grab(serial, time);
if !self.window.alive() {
handle.unset_grab(event.serial, event.time);
return;
}
let (mut dx, mut dy) = (location - self.start_data.location).into();
let (mut dx, mut dy) = (event.location - self.start_data.location).into();
let mut new_window_width = self.initial_window_size.w;
let mut new_window_height = self.initial_window_size.h;
@ -212,11 +198,10 @@ impl PointerGrab for ResizeSurfaceGrab {
}
let (min_size, max_size) =
with_states(self.window.toplevel().get_surface().unwrap(), |states| {
with_states(self.window.toplevel().wl_surface(), |states| {
let data = states.cached_state.current::<SurfaceCachedState>();
(data.min_size, data.max_size)
})
.unwrap();
});
let min_width = min_size.w.max(1);
let min_height = min_size.h.max(1);
@ -238,44 +223,39 @@ impl PointerGrab for ResizeSurfaceGrab {
match &self.window.toplevel() {
Kind::Xdg(xdg) => {
let ret = xdg.with_pending_state(|state| {
xdg.with_pending_state(|state| {
state.states.set(xdg_toplevel::State::Resizing);
state.size = Some(self.last_window_size);
});
if ret.is_ok() {
xdg.send_configure();
}
xdg.send_configure();
}
}
}
fn button(
&mut self,
handle: &mut PointerInnerHandle<'_>,
button: u32,
state: ButtonState,
serial: Serial,
time: u32,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
event: &ButtonEvent,
) {
handle.button(button, state, serial, time);
handle.button(event.button, event.state, event.serial, event.time);
if handle.current_pressed().is_empty() {
// No more buttons are pressed, release the grab.
handle.unset_grab(serial, time);
handle.unset_grab(event.serial, event.time);
// If toplevel is dead, we can't resize it, so we return early.
if !self.window.toplevel().alive() | self.window.toplevel().get_surface().is_none() {
if !self.window.alive() {
return;
}
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(xdg) = &self.window.toplevel() {
let ret = xdg.with_pending_state(|state| {
xdg.with_pending_state(|state| {
state.states.unset(xdg_toplevel::State::Resizing);
state.size = Some(self.last_window_size);
});
if ret.is_ok() {
xdg.send_configure();
}
xdg.send_configure();
}
let mut resize_state = self
@ -285,14 +265,20 @@ impl PointerGrab for ResizeSurfaceGrab {
.unwrap()
.borrow_mut();
if let ResizeState::Resizing(resize_data) = *resize_state {
*resize_state = ResizeState::WaitingForFinalAck(resize_data, serial);
*resize_state = ResizeState::WaitingForFinalAck(resize_data, event.serial);
} else {
panic!("invalid resize state: {:?}", resize_state);
}
}
}
fn axis(&mut self, handle: &mut PointerInnerHandle<'_>, details: AxisFrame) {
fn axis(
&mut self,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
details: AxisFrame,
) {
handle.axis(details)
}
@ -334,11 +320,7 @@ impl ResizeSurfaceGrab {
}
pub fn ack_configure(window: &Window, configure: ToplevelConfigure) {
let surface = if let Some(surface) = window.toplevel().get_surface() {
surface
} else {
return;
};
let surface = window.toplevel().wl_surface();
let waiting_for_serial =
if let Some(data) = window.user_data().get::<RefCell<ResizeState>>() {
@ -372,8 +354,7 @@ impl ResizeSurfaceGrab {
.current
.states
.contains(xdg_toplevel::State::Resizing)
})
.unwrap();
});
if configure.serial >= serial && is_resizing {
let mut resize_state = window

View file

@ -1,9 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::shell::layout::Layout;
use smithay::{
desktop::{layer_map_for_output, Kind, Space, Window},
reexports::wayland_protocols::xdg_shell::server::xdg_toplevel::{
desktop::{layer_map_for_output, Kind, Space, Window, space::RenderZindex},
reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::{
ResizeEdge, State as XdgState,
},
wayland::{
@ -11,7 +10,11 @@ use smithay::{
seat::{PointerGrabStartData, Seat},
Serial,
},
utils::IsAlive,
};
use std::collections::HashSet;
use crate::state::State;
mod grabs;
pub use self::grabs::*;
@ -19,61 +22,94 @@ pub use self::grabs::*;
#[derive(Debug, Default)]
pub struct FloatingLayout {
pending_windows: Vec<Window>,
pub windows: HashSet<Window>,
}
impl Layout for FloatingLayout {
fn map_window<'a>(
impl FloatingLayout {
pub fn new() -> FloatingLayout {
Default::default()
}
pub fn map_window(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
_focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
window: Window,
seat: &Seat<State>,
) {
if let Some(output) = super::output_from_seat(Some(seat), space) {
Self::map_window(space, window, &output)
self.map_window_internal(space, window, &output);
} else {
self.pending_windows.push(window.clone());
self.pending_windows.push(window);
}
}
fn refresh(&mut self, space: &mut Space) {
pub fn refresh(&mut self, space: &mut Space) {
self.pending_windows.retain(|w| w.toplevel().alive());
if let Some(output) = super::output_from_seat(None, space) {
for window in self.pending_windows.drain(..) {
Self::map_window(space, &window, &output);
for window in std::mem::take(&mut self.pending_windows).into_iter() {
self.map_window_internal(space, window, &output);
}
}
// TODO make sure all windows are still visible on any output or move them
}
fn unmap_window(&mut self, space: &mut Space, window: &Window) {
space.unmap_window(window);
self.pending_windows.retain(|w| w != window);
}
fn maximize_request(&mut self, space: &mut Space, window: &Window, output: &Output) {
fn map_window_internal(
&mut self,
space: &mut Space,
window: Window,
output: &Output
) {
let win_geo = window.bbox();
let layers = layer_map_for_output(&output);
let geometry = layers.non_exclusive_zone();
space.map_window(&window, (-geometry.loc.x, -geometry.loc.y), true);
let position = (
-geometry.loc.x + (geometry.size.w / 2) - (win_geo.size.w / 2),
-geometry.loc.y + (geometry.size.h / 2) - (win_geo.size.h / 2),
);
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(xdg) = &window.toplevel() {
xdg.with_pending_state(|state| {
state.states.unset(XdgState::TiledLeft);
state.states.unset(XdgState::TiledRight);
state.states.unset(XdgState::TiledTop);
state.states.unset(XdgState::TiledBottom);
});
xdg.send_configure();
}
window.override_z_index(RenderZindex::Shell as u8 + 1);
space.map_window(&window, position, false);
self.windows.insert(window);
}
pub fn unmap_window(&mut self, space: &mut Space, window: &Window) {
window.clear_z_index();
space.unmap_window(window);
self.pending_windows.retain(|w| w != window);
self.windows.remove(window);
}
pub fn maximize_request(&mut self, space: &mut Space, window: &Window, output: &Output) {
let layers = layer_map_for_output(&output);
let geometry = layers.non_exclusive_zone();
space.map_window(&window, (geometry.loc.x, geometry.loc.y), true);
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(surface) = &window.toplevel() {
let ret = surface.with_pending_state(|state| {
surface.with_pending_state(|state| {
state.states.set(XdgState::Maximized);
state.size = Some(geometry.size);
});
if ret.is_ok() {
window.configure();
}
window.configure();
}
}
fn move_request(
pub fn move_request(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
seat: &Seat<State>,
serial: Serial,
start_data: PointerGrabStartData,
) {
@ -83,31 +119,26 @@ impl Layout for FloatingLayout {
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(surface) = &window.toplevel() {
// If surface is maximized then unmaximize it
if let Some(current_state) = surface.current_state() {
if current_state.states.contains(XdgState::Maximized) {
let fs_changed = surface.with_pending_state(|state| {
state.states.unset(XdgState::Maximized);
state.size = None;
});
let current_state = surface.current_state();
if current_state.states.contains(XdgState::Maximized) {
surface.with_pending_state(|state| {
state.states.unset(XdgState::Maximized);
state.size = None;
});
if fs_changed.is_ok() {
surface.send_configure();
surface.send_configure();
// NOTE: In real compositor mouse location should be mapped to a new window size
// For example, you could:
// 1) transform mouse pointer position from compositor space to window space (location relative)
// 2) divide the x coordinate by width of the window to get the percentage
// - 0.0 would be on the far left of the window
// - 0.5 would be in middle of the window
// - 1.0 would be on the far right of the window
// 3) multiply the percentage by new window width
// 4) by doing that, drag will look a lot more natural
//
// but for anvil needs setting location to pointer location is fine
let pos = pointer.current_location();
initial_window_location = (pos.x as i32, pos.y as i32).into();
}
}
// TODO: The mouse location should be mapped to a new window size
// For example, you could:
// 1) transform mouse pointer position from compositor space to window space (location relative)
// 2) divide the x coordinate by width of the window to get the percentage
// - 0.0 would be on the far left of the window
// - 0.5 would be in middle of the window
// - 1.0 would be on the far right of the window
// 3) multiply the percentage by new window width
// 4) by doing that, drag will look a lot more natural
let pos = pointer.current_location();
initial_window_location = (pos.x as i32, pos.y as i32).into();
}
}
@ -117,11 +148,11 @@ impl Layout for FloatingLayout {
}
}
fn resize_request(
pub fn resize_request(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
seat: &Seat<State>,
serial: Serial,
start_data: PointerGrabStartData,
edges: ResizeEdge,
@ -137,33 +168,3 @@ impl Layout for FloatingLayout {
}
}
}
impl FloatingLayout {
pub fn new() -> FloatingLayout {
Default::default()
}
fn map_window(space: &mut Space, window: &Window, output: &Output) {
let win_geo = window.bbox();
let layers = layer_map_for_output(&output);
let geometry = layers.non_exclusive_zone();
let position = (
-geometry.loc.x + (geometry.size.w / 2) - (win_geo.size.w / 2),
-geometry.loc.y + (geometry.size.h / 2) - (win_geo.size.h / 2),
);
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(xdg) = &window.toplevel() {
let ret = xdg.with_pending_state(|state| {
state.states.unset(XdgState::TiledLeft);
state.states.unset(XdgState::TiledRight);
state.states.unset(XdgState::TiledTop);
state.states.unset(XdgState::TiledBottom);
});
if ret.is_ok() {
xdg.send_configure();
}
}
space.map_window(&window, position, false);
}
}

View file

@ -1,19 +1,20 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::input::ActiveOutput;
use crate::{
input::ActiveOutput,
state::State,
};
use smithay::{
desktop::{Space, Window},
reexports::wayland_protocols::xdg_shell::server::xdg_toplevel::ResizeEdge,
wayland::{
compositor::with_states,
output::Output,
seat::{PointerGrabStartData, Seat},
seat::Seat,
shell::xdg::XdgToplevelSurfaceRoleAttributes,
Serial,
},
};
use std::sync::Mutex;
pub mod combined;
pub mod floating;
pub mod tiling;
@ -23,112 +24,38 @@ pub enum Orientation {
Vertical,
}
#[derive(Debug, serde::Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum FocusDirection {
Left,
Right,
Up,
Down,
pub fn should_be_floating(window: &Window) -> bool {
let surface = window.toplevel().wl_surface();
with_states(surface, |states| {
let attrs = states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap();
// simple heuristic taken from
// sway/desktop/xdg_shell.c:188 @ 0ee54a52
if attrs.parent.is_some()
|| (attrs.min_size.w != 0
&& attrs.min_size.h != 0
&& attrs.min_size == attrs.max_size)
{
return true;
}
// else take a look at our exceptions
match (
attrs.app_id.as_deref().unwrap_or(""),
attrs.title.as_deref().unwrap_or(""),
) {
("gcr-prompter", _) => true,
_ => false,
}
})
}
pub trait Layout {
fn map_window<'a>(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
); //working around object safety..
fn refresh(&mut self, space: &mut Space);
fn unmap_window(&mut self, space: &mut Space, window: &Window);
fn move_focus<'a>(
&mut self,
direction: FocusDirection,
seat: &Seat,
space: &mut Space,
focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
) -> Option<Window> {
let _ = (direction, seat, space, focus_stack);
None
}
fn update_orientation<'a>(
&mut self,
orientation: Orientation,
seat: &Seat,
space: &mut Space,
focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
) {
let _ = (orientation, seat, space, focus_stack);
}
fn maximize_request(&mut self, space: &mut Space, window: &Window, output: &Output) {
let _ = (space, window, output);
}
fn move_request(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
serial: Serial,
start_data: PointerGrabStartData,
) {
let _ = (space, window, seat, serial, start_data);
}
fn resize_request(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
serial: Serial,
start_data: PointerGrabStartData,
edges: ResizeEdge,
) {
let _ = (space, window, seat, serial, start_data, edges);
}
}
pub fn new_default_layout() -> Box<dyn Layout> {
Box::new(combined::Combined::new(
tiling::TilingLayout::new(),
floating::FloatingLayout::new(),
|window| {
if let Some(surface) = window.toplevel().get_surface() {
with_states(surface, |states| {
let attrs = states
.data_map
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
.unwrap()
.lock()
.unwrap();
// simple heuristic taken from
// sway/desktop/xdg_shell.c:188 @ 0ee54a52
if attrs.parent.is_some()
|| (attrs.min_size.w != 0
&& attrs.min_size.h != 0
&& attrs.min_size == attrs.max_size)
{
return true;
}
// else take a look at our exceptions
match (
attrs.app_id.as_deref().unwrap_or(""),
attrs.title.as_deref().unwrap_or(""),
) {
("gcr-prompter", _) => true,
_ => false,
}
})
.unwrap_or(false)
} else {
false
}
},
))
}
fn output_from_seat(seat: Option<&Seat>, space: &Space) -> Option<Output> {
fn output_from_seat(seat: Option<&Seat<State>>, space: &Space) -> Option<Output> {
seat.and_then(|seat| {
seat.user_data()
.get::<ActiveOutput>()

View file

@ -1,13 +1,16 @@
// SPDX-License-Identifier: GPL-3.0-only
use super::Orientation;
use crate::{
utils::prelude::*,
shell::layout::Orientation,
};
use atomic_float::AtomicF64;
use smithay::{
reexports::wayland_server::protocol::{wl_pointer::ButtonState, wl_surface},
utils::{Logical, Point, Size},
wayland::{
seat::{AxisFrame, PointerGrab, PointerGrabStartData, PointerInnerHandle},
Serial,
reexports::wayland_server::{
DisplayHandle,
},
utils::{Logical, Size},
wayland::seat::{AxisFrame, PointerGrab, PointerGrabStartData, PointerInnerHandle, MotionEvent, ButtonEvent},
};
use std::sync::{atomic::Ordering, Arc};
@ -19,19 +22,18 @@ pub struct ResizeForkGrab {
pub ratio: Arc<AtomicF64>,
}
impl PointerGrab for ResizeForkGrab {
impl PointerGrab<State> for ResizeForkGrab {
fn motion(
&mut self,
handle: &mut PointerInnerHandle<'_>,
location: Point<f64, Logical>,
_focus: Option<(wl_surface::WlSurface, Point<i32, Logical>)>,
serial: Serial,
time: u32,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
event: &MotionEvent
) {
// While the grab is active, no client has pointer focus
handle.motion(location, None, serial, time);
handle.motion(event.location, None, event.serial, event.time);
let delta = location - self.start_data.location;
let delta = event.location - self.start_data.location;
let delta = match self.orientation {
Orientation::Vertical => delta.x / self.initial_size.w as f64,
Orientation::Horizontal => delta.y / self.initial_size.h as f64,
@ -44,20 +46,24 @@ impl PointerGrab for ResizeForkGrab {
fn button(
&mut self,
handle: &mut PointerInnerHandle<'_>,
button: u32,
state: ButtonState,
serial: Serial,
time: u32,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
event: &ButtonEvent,
) {
handle.button(button, state, serial, time);
handle.button(event.button, event.state, event.serial, event.time);
if handle.current_pressed().is_empty() {
// No more buttons are pressed, release the grab.
handle.unset_grab(serial, time);
handle.unset_grab(event.serial, event.time);
}
}
fn axis(&mut self, handle: &mut PointerInnerHandle<'_>, details: AxisFrame) {
fn axis(&mut self,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
details: AxisFrame,
) {
handle.axis(details)
}

View file

@ -1,14 +1,21 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::shell::layout::{FocusDirection, Layout, Orientation};
use crate::{
shell::{
layout::Orientation,
focus::FocusDirection,
},
utils::prelude::*,
};
use atomic_float::AtomicF64;
use id_tree::{InsertBehavior, MoveBehavior, Node, NodeId, NodeIdError, RemoveBehavior, Tree};
use smithay::{
desktop::{layer_map_for_output, Kind, Space, Window},
reexports::wayland_protocols::xdg_shell::server::xdg_toplevel::{
reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::{
ResizeEdge, State as XdgState,
},
utils::Rectangle,
utils::{IsAlive, Rectangle},
wayland::{
seat::{PointerGrabStartData, Seat},
Serial,
@ -16,6 +23,7 @@ use smithay::{
};
use std::{
cell::RefCell,
collections::HashSet,
sync::{atomic::Ordering, Arc},
};
@ -26,6 +34,7 @@ pub use self::grabs::*;
pub struct TilingLayout {
gaps: (i32, i32),
trees: Vec<Tree<Data>>,
pub windows: HashSet<Window>,
}
#[derive(Debug)]
@ -61,28 +70,30 @@ impl TilingLayout {
TilingLayout {
gaps: (0, 4),
trees: Vec::new(),
windows: HashSet::new(),
}
}
}
impl Layout for TilingLayout {
fn map_window<'a>(
impl TilingLayout {
pub fn map_window<'a>(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
window: Window,
seat: &Seat<State>,
focus_stack: impl Iterator<Item = &'a Window> + 'a,
) {
self.map_window(space, window, Some(seat), Some(focus_stack));
self.map_window_internal(space, &window, Some(seat), Some(focus_stack));
self.windows.insert(window);
self.refresh(space);
}
fn move_focus<'a>(
pub fn move_focus<'a>(
&mut self,
direction: FocusDirection,
seat: &Seat,
seat: &Seat<State>,
space: &mut Space,
focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
focus_stack: impl Iterator<Item = &'a Window> + 'a,
) -> Option<Window> {
let output = super::output_from_seat(Some(seat), space);
let idx = space
@ -136,12 +147,13 @@ impl Layout for TilingLayout {
None
}
fn update_orientation<'a>(
pub fn update_orientation<'a>(
&mut self,
new_orientation: Orientation,
seat: &Seat,
seat: &Seat<State>,
space: &mut Space,
focus_stack: Box<dyn Iterator<Item = &'a Window> + 'a>,
focus_stack: impl Iterator<Item = &'a Window> + 'a,
) {
let output = super::output_from_seat(Some(seat), space);
let idx = space
@ -163,7 +175,7 @@ impl Layout for TilingLayout {
self.refresh(space);
}
fn refresh<'a>(&mut self, space: &mut Space) {
pub fn refresh<'a>(&mut self, space: &mut Space) {
let active_outputs = space.outputs().count();
if self.trees.len() > active_outputs {
for tree in self
@ -175,7 +187,7 @@ impl Layout for TilingLayout {
if let Some(root_id) = tree.root_node_id() {
for node in tree.traverse_pre_order(root_id).unwrap() {
if let Data::Window(window) = node.data() {
self.map_window(space, window, None, None);
self.map_window_internal(space, window, None, Option::<std::iter::Empty<&Window>>::None);
}
}
}
@ -189,22 +201,32 @@ impl Layout for TilingLayout {
.filter(|v| !v.is_empty())
{
for window in dead_windows {
self.unmap_window(&window);
self.unmap_window_internal(&window);
}
}
}
fn unmap_window(&mut self, space: &mut Space, window: &Window) {
self.unmap_window(&window);
space.unmap_window(&window);
pub fn unmap_window(&mut self, space: &mut Space, window: &Window) {
self.unmap_window_internal(window);
space.unmap_window(window);
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(xdg) = &window.toplevel() {
xdg.with_pending_state(|state| {
state.states.unset(XdgState::TiledLeft);
state.states.unset(XdgState::TiledRight);
state.states.unset(XdgState::TiledTop);
state.states.unset(XdgState::TiledBottom);
});
}
self.windows.remove(window);
self.refresh(space);
}
fn resize_request(
pub fn resize_request(
&mut self,
space: &mut Space,
window: &Window,
seat: &Seat,
seat: &Seat<State>,
serial: Serial,
start_data: PointerGrabStartData,
edges: ResizeEdge,
@ -252,9 +274,7 @@ impl Layout for TilingLayout {
}
}
}
}
impl TilingLayout {
fn active_tree<'a>(trees: &'a mut Vec<Tree<Data>>, output: usize) -> &'a mut Tree<Data> {
while trees.len() <= output {
trees.push(Tree::new())
@ -286,12 +306,12 @@ impl TilingLayout {
None
}
fn map_window<'a>(
fn map_window_internal<'a>(
&mut self,
space: &mut Space,
window: &Window,
seat: Option<&Seat>,
focus_stack: Option<Box<dyn Iterator<Item = &'a Window> + 'a>>,
seat: Option<&Seat<State>>,
focus_stack: Option<impl Iterator<Item = &'a Window> + 'a>,
) {
let output = super::output_from_seat(seat, space);
let idx = space
@ -353,7 +373,7 @@ impl TilingLayout {
}
}
fn unmap_window(&mut self, window: &Window) {
fn unmap_window_internal(&mut self, window: &Window) {
if let Some(info) = window.user_data().get::<RefCell<WindowInfo>>() {
let output = info.borrow().output;
let tree = TilingLayout::active_tree(&mut self.trees, output);
@ -534,25 +554,21 @@ impl TilingLayout {
}
}
Data::Window(window) => {
if window.toplevel().alive() {
if window.alive() {
if let Some(geo) = geo {
#[allow(irrefutable_let_patterns)]
if let Kind::Xdg(xdg) = &window.toplevel() {
if xdg
.current_state()
.map(|state| {
state.states.contains(XdgState::Fullscreen)
.states.contains(XdgState::Fullscreen)
|| xdg
.with_pending_state(|pending| {
pending.states.contains(XdgState::Fullscreen)
})
.unwrap_or(false)
|| xdg
.with_pending_state(|pending| {
pending.states.contains(XdgState::Fullscreen)
})
.unwrap_or(false)
{
continue;
}
let ret = xdg.with_pending_state(|state| {
xdg.with_pending_state(|state| {
state.size = Some(
(geo.size.w - inner * 2, geo.size.h - inner * 2)
.into(),
@ -562,9 +578,7 @@ impl TilingLayout {
state.states.set(XdgState::TiledTop);
state.states.set(XdgState::TiledBottom);
});
if ret.is_ok() {
xdg.send_configure();
}
xdg.send_configure();
}
space.map_window(
&window,