2021-12-28 16:23:12 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
2022-10-25 14:43:50 +02:00
|
|
|
use crate::{
|
2022-10-27 20:40:55 +02:00
|
|
|
shell::{element::CosmicMapped, focus::target::PointerFocusTarget, grabs::ResizeEdge},
|
2022-10-25 14:43:50 +02:00
|
|
|
utils::prelude::*,
|
|
|
|
|
};
|
2021-12-28 16:23:12 +01:00
|
|
|
use smithay::{
|
2022-10-25 14:43:50 +02:00
|
|
|
desktop::space::SpaceElement,
|
2022-08-31 13:01:23 +02:00
|
|
|
input::pointer::{
|
|
|
|
|
AxisFrame, ButtonEvent, GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab,
|
|
|
|
|
PointerInnerHandle,
|
|
|
|
|
},
|
2022-10-25 14:43:50 +02:00
|
|
|
utils::{IsAlive, Logical, Point, Size},
|
2021-12-28 16:23:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Information about the resize operation.
|
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
2022-10-25 14:43:50 +02:00
|
|
|
pub struct ResizeData {
|
2021-12-28 16:23:12 +01:00
|
|
|
/// The edges the surface is being resized with.
|
|
|
|
|
edges: ResizeEdge,
|
|
|
|
|
/// The initial window location.
|
|
|
|
|
initial_window_location: Point<i32, Logical>,
|
|
|
|
|
/// The initial window size (geometry width and height).
|
|
|
|
|
initial_window_size: Size<i32, Logical>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// State of the resize operation.
|
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
2022-10-25 14:43:50 +02:00
|
|
|
pub enum ResizeState {
|
2021-12-28 16:23:12 +01:00
|
|
|
/// The surface is currently being resized.
|
|
|
|
|
Resizing(ResizeData),
|
|
|
|
|
/// The resize has finished, and the surface needs to commit its final state.
|
|
|
|
|
WaitingForCommit(ResizeData),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ResizeSurfaceGrab {
|
2022-08-31 13:01:23 +02:00
|
|
|
start_data: PointerGrabStartData<State>,
|
2022-10-25 14:43:50 +02:00
|
|
|
window: CosmicMapped,
|
2021-12-28 16:23:12 +01:00
|
|
|
edges: ResizeEdge,
|
|
|
|
|
initial_window_size: Size<i32, Logical>,
|
|
|
|
|
last_window_size: Size<i32, Logical>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
impl PointerGrab<State> for ResizeSurfaceGrab {
|
2021-12-28 16:23:12 +01:00
|
|
|
fn motion(
|
|
|
|
|
&mut self,
|
2022-08-31 13:01:23 +02:00
|
|
|
data: &mut State,
|
2022-07-04 15:28:03 +02:00
|
|
|
handle: &mut PointerInnerHandle<'_, State>,
|
2022-09-28 12:01:29 +02:00
|
|
|
_focus: Option<(PointerFocusTarget, Point<i32, Logical>)>,
|
2022-07-04 15:28:03 +02:00
|
|
|
event: &MotionEvent,
|
2021-12-28 16:23:12 +01:00
|
|
|
) {
|
2022-02-04 20:53:18 +01:00
|
|
|
// While the grab is active, no client has pointer focus
|
2022-08-31 13:01:23 +02:00
|
|
|
handle.motion(data, None, event);
|
2022-02-04 20:53:18 +01:00
|
|
|
|
2021-12-28 16:23:12 +01:00
|
|
|
// It is impossible to get `min_size` and `max_size` of dead toplevel, so we return early.
|
2022-07-04 15:28:03 +02:00
|
|
|
if !self.window.alive() {
|
2022-08-31 13:01:23 +02:00
|
|
|
handle.unset_grab(data, event.serial, event.time);
|
2021-12-28 16:23:12 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
let (mut dx, mut dy) = (event.location - self.start_data.location).into();
|
2021-12-28 16:23:12 +01:00
|
|
|
|
|
|
|
|
let mut new_window_width = self.initial_window_size.w;
|
|
|
|
|
let mut new_window_height = self.initial_window_size.h;
|
|
|
|
|
|
|
|
|
|
let left_right = ResizeEdge::LEFT | ResizeEdge::RIGHT;
|
|
|
|
|
let top_bottom = ResizeEdge::TOP | ResizeEdge::BOTTOM;
|
|
|
|
|
|
|
|
|
|
if self.edges.intersects(left_right) {
|
|
|
|
|
if self.edges.intersects(ResizeEdge::LEFT) {
|
|
|
|
|
dx = -dx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_window_width = (self.initial_window_size.w as f64 + dx) as i32;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.edges.intersects(top_bottom) {
|
|
|
|
|
if self.edges.intersects(ResizeEdge::TOP) {
|
|
|
|
|
dy = -dy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_window_height = (self.initial_window_size.h as f64 + dy) as i32;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 14:43:50 +02:00
|
|
|
let (min_size, max_size) = (self.window.min_size(), self.window.max_size());
|
2021-12-28 16:23:12 +01:00
|
|
|
|
|
|
|
|
let min_width = min_size.w.max(1);
|
|
|
|
|
let min_height = min_size.h.max(1);
|
|
|
|
|
let max_width = if max_size.w == 0 {
|
|
|
|
|
i32::max_value()
|
|
|
|
|
} else {
|
|
|
|
|
max_size.w
|
|
|
|
|
};
|
|
|
|
|
let max_height = if max_size.h == 0 {
|
|
|
|
|
i32::max_value()
|
|
|
|
|
} else {
|
|
|
|
|
max_size.h
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
new_window_width = new_window_width.max(min_width).min(max_width);
|
|
|
|
|
new_window_height = new_window_height.max(min_height).min(max_height);
|
|
|
|
|
|
|
|
|
|
self.last_window_size = (new_window_width, new_window_height).into();
|
|
|
|
|
|
2022-10-25 14:43:50 +02:00
|
|
|
self.window.set_resizing(true);
|
|
|
|
|
self.window.set_size(self.last_window_size);
|
|
|
|
|
self.window.configure();
|
2021-12-28 16:23:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn button(
|
|
|
|
|
&mut self,
|
2022-08-31 13:01:23 +02:00
|
|
|
data: &mut State,
|
2022-07-04 15:28:03 +02:00
|
|
|
handle: &mut PointerInnerHandle<'_, State>,
|
|
|
|
|
event: &ButtonEvent,
|
2021-12-28 16:23:12 +01:00
|
|
|
) {
|
2022-08-31 13:01:23 +02:00
|
|
|
handle.button(data, event);
|
2021-12-28 16:23:12 +01:00
|
|
|
if handle.current_pressed().is_empty() {
|
|
|
|
|
// No more buttons are pressed, release the grab.
|
2022-08-31 13:01:23 +02:00
|
|
|
handle.unset_grab(data, event.serial, event.time);
|
2021-12-28 16:23:12 +01:00
|
|
|
|
|
|
|
|
// If toplevel is dead, we can't resize it, so we return early.
|
2022-07-04 15:28:03 +02:00
|
|
|
if !self.window.alive() {
|
2021-12-28 16:23:12 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 14:43:50 +02:00
|
|
|
self.window.set_resizing(false);
|
|
|
|
|
self.window.set_size(self.last_window_size);
|
|
|
|
|
self.window.configure();
|
2021-12-28 16:23:12 +01:00
|
|
|
|
2022-10-25 14:43:50 +02:00
|
|
|
let mut resize_state = self.window.resize_state.lock().unwrap();
|
|
|
|
|
if let Some(ResizeState::Resizing(resize_data)) = *resize_state {
|
|
|
|
|
*resize_state = Some(ResizeState::WaitingForCommit(resize_data));
|
2021-12-28 16:23:12 +01:00
|
|
|
} else {
|
|
|
|
|
panic!("invalid resize state: {:?}", resize_state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:28:03 +02:00
|
|
|
fn axis(
|
|
|
|
|
&mut self,
|
2022-08-31 13:01:23 +02:00
|
|
|
data: &mut State,
|
2022-07-04 15:28:03 +02:00
|
|
|
handle: &mut PointerInnerHandle<'_, State>,
|
|
|
|
|
details: AxisFrame,
|
|
|
|
|
) {
|
2022-08-31 13:01:23 +02:00
|
|
|
handle.axis(data, details)
|
2021-12-28 16:23:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-31 13:01:23 +02:00
|
|
|
fn start_data(&self) -> &PointerGrabStartData<State> {
|
2021-12-28 16:23:12 +01:00
|
|
|
&self.start_data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ResizeSurfaceGrab {
|
|
|
|
|
pub fn new(
|
2022-08-31 13:01:23 +02:00
|
|
|
start_data: PointerGrabStartData<State>,
|
2022-10-25 14:43:50 +02:00
|
|
|
mapped: CosmicMapped,
|
2022-10-27 20:40:55 +02:00
|
|
|
edges: ResizeEdge,
|
2022-03-16 20:05:24 +01:00
|
|
|
initial_window_location: Point<i32, Logical>,
|
|
|
|
|
initial_window_size: Size<i32, Logical>,
|
2021-12-28 16:23:12 +01:00
|
|
|
) -> ResizeSurfaceGrab {
|
|
|
|
|
let resize_state = ResizeState::Resizing(ResizeData {
|
2022-10-27 20:40:55 +02:00
|
|
|
edges,
|
2021-12-28 16:23:12 +01:00
|
|
|
initial_window_location,
|
|
|
|
|
initial_window_size,
|
|
|
|
|
});
|
|
|
|
|
|
2022-10-25 14:43:50 +02:00
|
|
|
*mapped.resize_state.lock().unwrap() = Some(resize_state);
|
2021-12-28 16:23:12 +01:00
|
|
|
|
|
|
|
|
ResizeSurfaceGrab {
|
|
|
|
|
start_data,
|
2022-10-25 14:43:50 +02:00
|
|
|
window: mapped,
|
2022-10-27 20:40:55 +02:00
|
|
|
edges,
|
2021-12-28 16:23:12 +01:00
|
|
|
initial_window_size,
|
|
|
|
|
last_window_size: initial_window_size,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 14:43:50 +02:00
|
|
|
pub fn apply_resize_to_location(window: CosmicMapped, space: &mut Workspace) {
|
|
|
|
|
if let Some(mut location) = space.floating_layer.space.element_location(&window) {
|
|
|
|
|
let mut new_location = None;
|
2021-12-28 16:23:12 +01:00
|
|
|
|
2022-10-25 14:43:50 +02:00
|
|
|
let mut resize_state = window.resize_state.lock().unwrap();
|
2021-12-28 16:23:12 +01:00
|
|
|
// If the window is being resized by top or left, its location must be adjusted
|
|
|
|
|
// accordingly.
|
|
|
|
|
match *resize_state {
|
2022-10-25 14:43:50 +02:00
|
|
|
Some(ResizeState::Resizing(resize_data))
|
|
|
|
|
| Some(ResizeState::WaitingForCommit(resize_data)) => {
|
2021-12-28 16:23:12 +01:00
|
|
|
let ResizeData {
|
|
|
|
|
edges,
|
|
|
|
|
initial_window_location,
|
|
|
|
|
initial_window_size,
|
|
|
|
|
} = resize_data;
|
|
|
|
|
|
|
|
|
|
if edges.intersects(ResizeEdge::TOP_LEFT) {
|
2022-10-25 14:43:50 +02:00
|
|
|
let size = window.geometry().size;
|
2021-12-28 16:23:12 +01:00
|
|
|
if edges.intersects(ResizeEdge::LEFT) {
|
2022-03-16 20:05:24 +01:00
|
|
|
location.x =
|
|
|
|
|
initial_window_location.x + (initial_window_size.w - size.w);
|
2021-12-28 16:23:12 +01:00
|
|
|
}
|
|
|
|
|
if edges.intersects(ResizeEdge::TOP) {
|
2022-03-16 20:05:24 +01:00
|
|
|
location.y =
|
|
|
|
|
initial_window_location.y + (initial_window_size.h - size.h);
|
2021-12-28 16:23:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_location = Some(location);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-25 14:43:50 +02:00
|
|
|
_ => {}
|
|
|
|
|
};
|
2021-12-28 16:23:12 +01:00
|
|
|
|
|
|
|
|
// Finish resizing.
|
2022-10-25 14:43:50 +02:00
|
|
|
if let Some(ResizeState::WaitingForCommit(_)) = *resize_state {
|
|
|
|
|
if !window.is_resizing() {
|
|
|
|
|
*resize_state = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::mem::drop(resize_state);
|
|
|
|
|
|
|
|
|
|
if let Some(new_location) = new_location {
|
|
|
|
|
for (window, offset) in window.windows() {
|
|
|
|
|
update_reactive_popups(
|
|
|
|
|
&window,
|
|
|
|
|
new_location + offset,
|
|
|
|
|
space.floating_layer.space.outputs(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
space
|
|
|
|
|
.floating_layer
|
|
|
|
|
.space
|
|
|
|
|
.map_element(window, new_location, false);
|
2021-12-28 16:23:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|