2022-03-24 20:32:31 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
|
|
|
|
use smithay::{
|
2022-07-04 16:00:29 +02:00
|
|
|
desktop::{layer_map_for_output, space::RenderZindex, Kind, Space, Window},
|
2022-07-04 15:28:03 +02:00
|
|
|
reexports::wayland_protocols::xdg::shell::server::xdg_toplevel::{
|
2022-03-24 20:32:31 +01:00
|
|
|
ResizeEdge, State as XdgState,
|
|
|
|
|
},
|
2022-07-07 22:41:02 +02:00
|
|
|
utils::{IsAlive, Rectangle, Logical},
|
2022-03-24 20:32:31 +01:00
|
|
|
wayland::{
|
2022-07-06 23:41:11 +02:00
|
|
|
compositor::with_states,
|
2022-03-24 20:32:31 +01:00
|
|
|
output::Output,
|
2022-07-07 19:45:04 +02:00
|
|
|
seat::{Focus, PointerGrabStartData, Seat},
|
2022-07-06 23:41:11 +02:00
|
|
|
shell::xdg::XdgToplevelSurfaceRoleAttributes,
|
2022-03-24 20:32:31 +01:00
|
|
|
Serial,
|
|
|
|
|
},
|
|
|
|
|
};
|
2022-07-06 23:41:11 +02:00
|
|
|
use std::{collections::HashSet, sync::Mutex};
|
2022-07-04 15:28:03 +02:00
|
|
|
|
|
|
|
|
use crate::state::State;
|
2022-03-24 20:32:31 +01:00
|
|
|
|
|
|
|
|
mod grabs;
|
|
|
|
|
pub use self::grabs::*;
|
|
|
|
|
|
2022-07-07 19:45:04 +02:00
|
|
|
pub const FLOATING_INDEX: u8 = RenderZindex::Shell as u8 + 1;
|
|
|
|
|
|
2022-04-05 16:35:58 +02:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
pub struct FloatingLayout {
|
|
|
|
|
pending_windows: Vec<Window>,
|
2022-07-04 15:28:03 +02:00
|
|
|
pub windows: HashSet<Window>,
|
2022-04-05 16:35:58 +02:00
|
|
|
}
|
2022-03-24 20:32:31 +01:00
|
|
|
|
2022-07-07 22:41:02 +02:00
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct WindowUserDataInner {
|
|
|
|
|
last_geometry: Rectangle<i32, Logical>,
|
|
|
|
|
}
|
|
|
|
|
pub type WindowUserData = Mutex<WindowUserDataInner>;
|
|
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
impl FloatingLayout {
|
|
|
|
|
pub fn new() -> FloatingLayout {
|
|
|
|
|
Default::default()
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 16:00:29 +02:00
|
|
|
pub fn map_window(&mut self, space: &mut Space, window: Window, seat: &Seat<State>) {
|
2022-04-05 16:35:58 +02:00
|
|
|
if let Some(output) = super::output_from_seat(Some(seat), space) {
|
2022-07-04 15:28:03 +02:00
|
|
|
self.map_window_internal(space, window, &output);
|
2022-04-05 16:35:58 +02:00
|
|
|
} else {
|
2022-07-04 15:28:03 +02:00
|
|
|
self.pending_windows.push(window);
|
2022-03-24 20:32:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
pub fn refresh(&mut self, space: &mut Space) {
|
2022-04-05 16:35:58 +02:00
|
|
|
self.pending_windows.retain(|w| w.toplevel().alive());
|
|
|
|
|
if let Some(output) = super::output_from_seat(None, space) {
|
2022-07-04 15:28:03 +02:00
|
|
|
for window in std::mem::take(&mut self.pending_windows).into_iter() {
|
|
|
|
|
self.map_window_internal(space, window, &output);
|
2022-04-05 16:35:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-24 20:32:31 +01:00
|
|
|
// TODO make sure all windows are still visible on any output or move them
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 16:00:29 +02:00
|
|
|
fn map_window_internal(&mut self, space: &mut Space, window: Window, output: &Output) {
|
2022-07-07 22:41:02 +02:00
|
|
|
let last_geometry = window.user_data().get::<WindowUserData>().map(|u| u.lock().unwrap().last_geometry);
|
2022-07-06 23:41:11 +02:00
|
|
|
let mut win_geo = window.geometry();
|
2022-07-07 22:41:02 +02:00
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
let layers = layer_map_for_output(&output);
|
|
|
|
|
let geometry = layers.non_exclusive_zone();
|
|
|
|
|
|
2022-07-06 23:41:11 +02:00
|
|
|
let mut geo_updated = false;
|
2022-07-07 22:41:02 +02:00
|
|
|
if let Some(size) = last_geometry.clone().map(|g| g.size) {
|
|
|
|
|
geo_updated = win_geo.size == size;
|
|
|
|
|
win_geo.size = size;
|
|
|
|
|
}
|
2022-07-06 23:41:11 +02:00
|
|
|
{
|
|
|
|
|
let (min_size, max_size) = with_states(window.toplevel().wl_surface(), |states| {
|
|
|
|
|
let attrs = states
|
|
|
|
|
.data_map
|
|
|
|
|
.get::<Mutex<XdgToplevelSurfaceRoleAttributes>>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap();
|
|
|
|
|
(attrs.min_size, attrs.max_size)
|
|
|
|
|
});
|
|
|
|
|
if win_geo.size.w > geometry.size.w / 3 * 2 {
|
|
|
|
|
// try a more reasonable size
|
|
|
|
|
let mut width = geometry.size.w / 3 * 2;
|
|
|
|
|
if max_size.w != 0 {
|
|
|
|
|
// don't go larger then the max_size ...
|
|
|
|
|
width = std::cmp::min(max_size.w, width);
|
|
|
|
|
}
|
|
|
|
|
if min_size.w != 0 {
|
|
|
|
|
// ... but also don't go smaller than the min_size
|
|
|
|
|
width = std::cmp::max(min_size.w, width);
|
|
|
|
|
}
|
|
|
|
|
// but no matter the supported sizes, don't be larger than our non-exclusive-zone
|
|
|
|
|
win_geo.size.w = std::cmp::min(width, geometry.size.w);
|
|
|
|
|
geo_updated = true;
|
|
|
|
|
}
|
|
|
|
|
if win_geo.size.h > geometry.size.h / 3 * 2 {
|
|
|
|
|
// try a more reasonable size
|
|
|
|
|
let mut height = geometry.size.h / 3 * 2;
|
|
|
|
|
if max_size.h != 0 {
|
|
|
|
|
// don't go larger then the max_size ...
|
|
|
|
|
height = std::cmp::min(max_size.h, height);
|
|
|
|
|
}
|
|
|
|
|
if min_size.h != 0 {
|
|
|
|
|
// ... but also don't go smaller than the min_size
|
|
|
|
|
height = std::cmp::max(min_size.h, height);
|
|
|
|
|
}
|
|
|
|
|
// but no matter the supported sizes, don't be larger than our non-exclusive-zone
|
|
|
|
|
win_geo.size.h = std::cmp::min(height, geometry.size.h);
|
|
|
|
|
geo_updated = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-07 22:41:02 +02:00
|
|
|
let position = last_geometry.map(|g| g.loc).unwrap_or_else(|| (
|
2022-07-06 23:41:11 +02:00
|
|
|
geometry.loc.x + (geometry.size.w / 2) - (win_geo.size.w / 2) + win_geo.loc.x,
|
|
|
|
|
geometry.loc.y + (geometry.size.h / 2) - (win_geo.size.h / 2) + win_geo.loc.y,
|
2022-07-07 22:41:02 +02:00
|
|
|
).into());
|
2022-07-06 23:41:11 +02:00
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
#[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);
|
2022-07-06 23:41:11 +02:00
|
|
|
if geo_updated {
|
|
|
|
|
state.size = Some(win_geo.size);
|
|
|
|
|
}
|
2022-07-04 15:28:03 +02:00
|
|
|
});
|
|
|
|
|
xdg.send_configure();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-07 19:45:04 +02:00
|
|
|
space.map_window(&window, position, FLOATING_INDEX, false);
|
2022-07-04 15:28:03 +02:00
|
|
|
self.windows.insert(window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn unmap_window(&mut self, space: &mut Space, window: &Window) {
|
2022-07-07 22:41:02 +02:00
|
|
|
if let Some(location) = space.window_location(window) {
|
|
|
|
|
let user_data = window.user_data();
|
|
|
|
|
user_data.insert_if_missing(|| WindowUserData::default());
|
|
|
|
|
user_data.get::<WindowUserData>().unwrap().lock().unwrap().last_geometry = Rectangle::from_loc_and_size(
|
|
|
|
|
location,
|
|
|
|
|
window.geometry().size,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 16:35:58 +02:00
|
|
|
space.unmap_window(window);
|
|
|
|
|
self.pending_windows.retain(|w| w != window);
|
2022-07-04 15:28:03 +02:00
|
|
|
self.windows.remove(window);
|
2022-03-30 22:11:29 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
pub fn maximize_request(&mut self, space: &mut Space, window: &Window, output: &Output) {
|
2022-03-24 20:32:31 +01:00
|
|
|
let layers = layer_map_for_output(&output);
|
|
|
|
|
let geometry = layers.non_exclusive_zone();
|
|
|
|
|
|
2022-07-07 19:45:04 +02:00
|
|
|
space.map_window(
|
|
|
|
|
&window,
|
|
|
|
|
(geometry.loc.x, geometry.loc.y),
|
|
|
|
|
FLOATING_INDEX,
|
|
|
|
|
true,
|
|
|
|
|
);
|
2022-03-24 20:32:31 +01:00
|
|
|
#[allow(irrefutable_let_patterns)]
|
|
|
|
|
if let Kind::Xdg(surface) = &window.toplevel() {
|
2022-07-04 15:28:03 +02:00
|
|
|
surface.with_pending_state(|state| {
|
2022-03-24 20:32:31 +01:00
|
|
|
state.states.set(XdgState::Maximized);
|
|
|
|
|
state.size = Some(geometry.size);
|
|
|
|
|
});
|
2022-07-04 15:28:03 +02:00
|
|
|
window.configure();
|
2022-03-24 20:32:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
pub fn move_request(
|
2022-03-24 20:32:31 +01:00
|
|
|
&mut self,
|
|
|
|
|
space: &mut Space,
|
|
|
|
|
window: &Window,
|
2022-07-04 15:28:03 +02:00
|
|
|
seat: &Seat<State>,
|
2022-03-24 20:32:31 +01:00
|
|
|
serial: Serial,
|
|
|
|
|
start_data: PointerGrabStartData,
|
|
|
|
|
) {
|
|
|
|
|
if let Some(pointer) = seat.get_pointer() {
|
|
|
|
|
let mut initial_window_location = space.window_location(&window).unwrap();
|
|
|
|
|
|
|
|
|
|
#[allow(irrefutable_let_patterns)]
|
|
|
|
|
if let Kind::Xdg(surface) = &window.toplevel() {
|
|
|
|
|
// If surface is maximized then unmaximize it
|
2022-07-04 15:28:03 +02:00
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
surface.send_configure();
|
|
|
|
|
|
|
|
|
|
// 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();
|
2022-03-24 20:32:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let grab = MoveSurfaceGrab::new(start_data, window.clone(), initial_window_location);
|
|
|
|
|
|
2022-07-07 19:45:04 +02:00
|
|
|
pointer.set_grab(grab, serial, Focus::Clear);
|
2022-03-24 20:32:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
pub fn resize_request(
|
2022-03-24 20:32:31 +01:00
|
|
|
&mut self,
|
|
|
|
|
space: &mut Space,
|
|
|
|
|
window: &Window,
|
2022-07-04 15:28:03 +02:00
|
|
|
seat: &Seat<State>,
|
2022-03-24 20:32:31 +01:00
|
|
|
serial: Serial,
|
|
|
|
|
start_data: PointerGrabStartData,
|
|
|
|
|
edges: ResizeEdge,
|
|
|
|
|
) {
|
|
|
|
|
if let Some(pointer) = seat.get_pointer() {
|
|
|
|
|
let location = space.window_location(&window).unwrap();
|
|
|
|
|
let size = window.geometry().size;
|
|
|
|
|
|
|
|
|
|
let grab =
|
|
|
|
|
grabs::ResizeSurfaceGrab::new(start_data, window.clone(), edges, location, size);
|
|
|
|
|
|
2022-07-07 19:45:04 +02:00
|
|
|
pointer.set_grab(grab, serial, Focus::Clear);
|
2022-03-24 20:32:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|