2022-10-26 15:26:07 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
|
|
|
|
use crate::{
|
2023-05-30 13:20:46 +02:00
|
|
|
backend::render::{element::AsGlowRenderer, IndicatorShader},
|
2022-10-26 15:26:07 +02:00
|
|
|
shell::{
|
2023-03-07 22:20:44 +01:00
|
|
|
element::{window::CosmicWindowRenderElement, CosmicMapped, CosmicMappedRenderElement},
|
2022-10-26 15:26:07 +02:00
|
|
|
focus::target::{KeyboardFocusTarget, PointerFocusTarget},
|
2023-03-31 13:57:37 +02:00
|
|
|
CosmicSurface,
|
2022-10-26 15:26:07 +02:00
|
|
|
},
|
|
|
|
|
utils::prelude::*,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use smithay::{
|
2022-11-22 10:28:30 +01:00
|
|
|
backend::renderer::{
|
2023-03-23 14:58:29 +01:00
|
|
|
element::{AsRenderElements, RenderElement},
|
2023-01-16 15:12:25 +01:00
|
|
|
ImportAll, ImportMem, Renderer,
|
2022-11-22 10:28:30 +01:00
|
|
|
},
|
2022-10-26 15:26:07 +02:00
|
|
|
desktop::space::SpaceElement,
|
|
|
|
|
input::{
|
|
|
|
|
pointer::{
|
2022-11-03 18:51:27 +01:00
|
|
|
AxisFrame, ButtonEvent, GrabStartData as PointerGrabStartData, MotionEvent,
|
2023-01-30 23:19:36 +01:00
|
|
|
PointerGrab, PointerInnerHandle, RelativeMotionEvent,
|
2022-10-26 15:26:07 +02:00
|
|
|
},
|
|
|
|
|
Seat,
|
|
|
|
|
},
|
|
|
|
|
output::Output,
|
2023-02-13 17:56:13 +01:00
|
|
|
reexports::wayland_server::protocol::wl_surface::WlSurface,
|
2023-01-23 18:25:01 +01:00
|
|
|
utils::{IsAlive, Logical, Point, Rectangle, Serial},
|
2023-02-13 17:56:13 +01:00
|
|
|
wayland::compositor::SurfaceData,
|
2022-10-26 15:26:07 +02:00
|
|
|
};
|
2023-03-06 18:50:59 +01:00
|
|
|
use std::{cell::RefCell, collections::HashSet, time::Duration};
|
2022-10-26 15:26:07 +02:00
|
|
|
|
|
|
|
|
pub type SeatMoveGrabState = RefCell<Option<MoveGrabState>>;
|
|
|
|
|
|
|
|
|
|
pub struct MoveGrabState {
|
|
|
|
|
window: CosmicMapped,
|
2023-03-06 18:50:59 +01:00
|
|
|
window_offset: Point<i32, Logical>,
|
2023-03-09 18:27:11 +01:00
|
|
|
indicator_thickness: u8,
|
2022-10-26 15:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MoveGrabState {
|
2022-11-28 17:48:50 +01:00
|
|
|
pub fn render<I, R>(&self, renderer: &mut R, seat: &Seat<State>, output: &Output) -> Vec<I>
|
2022-10-26 15:26:07 +02:00
|
|
|
where
|
2023-01-16 15:12:25 +01:00
|
|
|
R: Renderer + ImportAll + ImportMem + AsGlowRenderer,
|
2022-10-26 15:26:07 +02:00
|
|
|
<R as Renderer>::TextureId: 'static,
|
2022-11-22 10:28:30 +01:00
|
|
|
CosmicMappedRenderElement<R>: RenderElement<R>,
|
2023-03-07 22:20:44 +01:00
|
|
|
CosmicWindowRenderElement<R>: RenderElement<R>,
|
2022-10-26 15:26:07 +02:00
|
|
|
I: From<CosmicMappedRenderElement<R>>,
|
|
|
|
|
{
|
2023-03-06 18:50:59 +01:00
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
|
puffin::profile_function!();
|
|
|
|
|
|
2022-10-26 15:26:07 +02:00
|
|
|
let cursor_at = seat.get_pointer().unwrap().current_location();
|
|
|
|
|
|
|
|
|
|
let mut window_geo = self.window.geometry();
|
2023-03-06 18:50:59 +01:00
|
|
|
window_geo.loc += cursor_at.to_i32_round() + self.window_offset;
|
2022-10-26 15:26:07 +02:00
|
|
|
if !output.geometry().intersection(window_geo).is_some() {
|
|
|
|
|
return Vec::new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let scale = output.current_scale().fractional_scale().into();
|
2023-03-06 18:50:59 +01:00
|
|
|
let render_location = cursor_at.to_i32_round() - output.geometry().loc + self.window_offset;
|
|
|
|
|
|
2023-03-09 18:27:11 +01:00
|
|
|
let mut elements: Vec<I> = Vec::new();
|
|
|
|
|
if self.indicator_thickness > 0 {
|
|
|
|
|
elements.push(
|
2023-05-30 13:20:46 +02:00
|
|
|
CosmicMappedRenderElement::from(IndicatorShader::focus_element(
|
2023-03-09 18:27:11 +01:00
|
|
|
renderer,
|
2023-05-17 19:46:21 +02:00
|
|
|
self.window.clone(),
|
2023-03-09 18:27:11 +01:00
|
|
|
Rectangle::from_loc_and_size(render_location, self.window.geometry().size),
|
|
|
|
|
self.indicator_thickness,
|
2023-05-12 20:01:37 +02:00
|
|
|
1.0,
|
2023-03-09 18:27:11 +01:00
|
|
|
))
|
|
|
|
|
.into(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-02-25 00:17:54 +01:00
|
|
|
elements.extend(AsRenderElements::<R>::render_elements::<I>(
|
2022-11-17 20:32:54 +01:00
|
|
|
&self.window,
|
2022-11-28 17:48:50 +01:00
|
|
|
renderer,
|
2023-03-08 15:36:26 -08:00
|
|
|
(render_location - self.window.geometry().loc).to_physical_precise_round(scale),
|
2022-10-26 15:26:07 +02:00
|
|
|
scale,
|
2023-05-12 20:01:37 +02:00
|
|
|
1.0,
|
2023-02-25 00:17:54 +01:00
|
|
|
));
|
|
|
|
|
elements
|
2022-10-26 15:26:07 +02:00
|
|
|
}
|
2023-02-13 17:56:13 +01:00
|
|
|
|
|
|
|
|
pub fn send_frames(
|
|
|
|
|
&self,
|
|
|
|
|
output: &Output,
|
|
|
|
|
time: impl Into<Duration>,
|
|
|
|
|
throttle: Option<Duration>,
|
|
|
|
|
primary_scan_out_output: impl FnMut(&WlSurface, &SurfaceData) -> Option<Output> + Copy,
|
|
|
|
|
) {
|
|
|
|
|
self.window
|
|
|
|
|
.active_window()
|
|
|
|
|
.send_frame(output, time, throttle, primary_scan_out_output)
|
|
|
|
|
}
|
2023-03-31 13:57:37 +02:00
|
|
|
|
|
|
|
|
pub fn window(&self) -> CosmicSurface {
|
|
|
|
|
self.window.active_window()
|
|
|
|
|
}
|
2022-10-26 15:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct MoveSurfaceGrab {
|
|
|
|
|
window: CosmicMapped,
|
|
|
|
|
start_data: PointerGrabStartData<State>,
|
|
|
|
|
seat: Seat<State>,
|
2023-03-06 18:50:59 +01:00
|
|
|
outputs: HashSet<Output>,
|
2022-10-26 15:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PointerGrab<State> for MoveSurfaceGrab {
|
|
|
|
|
fn motion(
|
|
|
|
|
&mut self,
|
|
|
|
|
state: &mut State,
|
|
|
|
|
handle: &mut PointerInnerHandle<'_, State>,
|
|
|
|
|
_focus: Option<(PointerFocusTarget, Point<i32, Logical>)>,
|
|
|
|
|
event: &MotionEvent,
|
|
|
|
|
) {
|
2023-03-06 18:50:59 +01:00
|
|
|
let borrow = self
|
|
|
|
|
.seat
|
|
|
|
|
.user_data()
|
|
|
|
|
.get::<SeatMoveGrabState>()
|
|
|
|
|
.map(|s| s.borrow());
|
|
|
|
|
if let Some(grab_state) = borrow.as_ref().and_then(|s| s.as_ref()) {
|
|
|
|
|
let mut window_geo = self.window.geometry();
|
|
|
|
|
window_geo.loc += event.location.to_i32_round() + grab_state.window_offset;
|
|
|
|
|
for output in state.common.shell.outputs() {
|
|
|
|
|
if let Some(overlap) = output.geometry().intersection(window_geo) {
|
|
|
|
|
if self.outputs.insert(output.clone()) {
|
|
|
|
|
self.window.output_enter(output, overlap);
|
|
|
|
|
}
|
|
|
|
|
} else if self.outputs.remove(&output) {
|
|
|
|
|
self.window.output_leave(output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
drop(borrow);
|
|
|
|
|
|
2022-10-26 15:26:07 +02:00
|
|
|
// While the grab is active, no client has pointer focus
|
|
|
|
|
handle.motion(state, None, event);
|
|
|
|
|
if !self.window.alive() {
|
|
|
|
|
self.ungrab(state, handle, event.serial, event.time);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 23:19:36 +01:00
|
|
|
fn relative_motion(
|
|
|
|
|
&mut self,
|
|
|
|
|
state: &mut State,
|
|
|
|
|
handle: &mut PointerInnerHandle<'_, State>,
|
|
|
|
|
_focus: Option<(PointerFocusTarget, Point<i32, Logical>)>,
|
|
|
|
|
event: &RelativeMotionEvent,
|
|
|
|
|
) {
|
|
|
|
|
// While the grab is active, no client has pointer focus
|
|
|
|
|
handle.relative_motion(state, None, event);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 15:26:07 +02:00
|
|
|
fn button(
|
|
|
|
|
&mut self,
|
|
|
|
|
state: &mut State,
|
|
|
|
|
handle: &mut PointerInnerHandle<'_, State>,
|
|
|
|
|
event: &ButtonEvent,
|
|
|
|
|
) {
|
|
|
|
|
handle.button(state, event);
|
|
|
|
|
if handle.current_pressed().is_empty() {
|
|
|
|
|
self.ungrab(state, handle, event.serial, event.time);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn axis(
|
|
|
|
|
&mut self,
|
|
|
|
|
state: &mut State,
|
|
|
|
|
handle: &mut PointerInnerHandle<'_, State>,
|
|
|
|
|
details: AxisFrame,
|
|
|
|
|
) {
|
|
|
|
|
handle.axis(state, details);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn start_data(&self) -> &PointerGrabStartData<State> {
|
|
|
|
|
&self.start_data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MoveSurfaceGrab {
|
|
|
|
|
pub fn new(
|
|
|
|
|
start_data: PointerGrabStartData<State>,
|
|
|
|
|
window: CosmicMapped,
|
|
|
|
|
seat: &Seat<State>,
|
|
|
|
|
initial_cursor_location: Point<f64, Logical>,
|
|
|
|
|
initial_window_location: Point<i32, Logical>,
|
2023-03-09 18:27:11 +01:00
|
|
|
indicator_thickness: u8,
|
2022-10-26 15:26:07 +02:00
|
|
|
) -> MoveSurfaceGrab {
|
2023-03-06 18:50:59 +01:00
|
|
|
let output = seat.active_output();
|
|
|
|
|
let mut outputs = HashSet::new();
|
|
|
|
|
outputs.insert(output.clone());
|
|
|
|
|
window.output_enter(&output, window.geometry()); // not accurate but...
|
|
|
|
|
|
2022-10-26 15:26:07 +02:00
|
|
|
let grab_state = MoveGrabState {
|
|
|
|
|
window: window.clone(),
|
2023-03-08 15:36:26 -08:00
|
|
|
window_offset: dbg!(initial_window_location)
|
2023-03-06 18:50:59 +01:00
|
|
|
- dbg!(initial_cursor_location.to_i32_round()),
|
2023-03-09 18:27:11 +01:00
|
|
|
indicator_thickness,
|
2022-10-26 15:26:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
*seat
|
|
|
|
|
.user_data()
|
|
|
|
|
.get::<SeatMoveGrabState>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.borrow_mut() = Some(grab_state);
|
|
|
|
|
|
|
|
|
|
MoveSurfaceGrab {
|
|
|
|
|
window,
|
|
|
|
|
start_data,
|
|
|
|
|
seat: seat.clone(),
|
2023-03-06 18:50:59 +01:00
|
|
|
outputs,
|
2022-10-26 15:26:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ungrab(
|
|
|
|
|
&mut self,
|
|
|
|
|
state: &mut State,
|
|
|
|
|
handle: &mut PointerInnerHandle<'_, State>,
|
|
|
|
|
serial: Serial,
|
|
|
|
|
time: u32,
|
|
|
|
|
) {
|
|
|
|
|
// No more buttons are pressed, release the grab.
|
|
|
|
|
let output = self.seat.active_output();
|
|
|
|
|
|
2023-04-20 16:19:52 +02:00
|
|
|
let position = if let Some(grab_state) = self
|
2022-10-26 15:26:07 +02:00
|
|
|
.seat
|
|
|
|
|
.user_data()
|
|
|
|
|
.get::<SeatMoveGrabState>()
|
|
|
|
|
.and_then(|s| s.borrow_mut().take())
|
|
|
|
|
{
|
|
|
|
|
if grab_state.window.alive() {
|
2023-03-06 18:50:59 +01:00
|
|
|
let window_location = handle.current_location().to_i32_round()
|
|
|
|
|
- output.geometry().loc
|
|
|
|
|
+ grab_state.window_offset;
|
2022-10-26 15:26:07 +02:00
|
|
|
|
|
|
|
|
let workspace_handle = state.common.shell.active_space(&output).handle;
|
|
|
|
|
for (window, _) in grab_state.window.windows() {
|
|
|
|
|
state
|
|
|
|
|
.common
|
|
|
|
|
.shell
|
|
|
|
|
.toplevel_info_state
|
|
|
|
|
.toplevel_enter_workspace(&window, &workspace_handle);
|
|
|
|
|
state
|
|
|
|
|
.common
|
|
|
|
|
.shell
|
|
|
|
|
.toplevel_info_state
|
|
|
|
|
.toplevel_enter_output(&window, &output);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 15:48:36 +01:00
|
|
|
let offset = state
|
|
|
|
|
.common
|
|
|
|
|
.shell
|
|
|
|
|
.active_space(&output)
|
|
|
|
|
.floating_layer
|
|
|
|
|
.space
|
|
|
|
|
.output_geometry(&output)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.loc;
|
2023-01-23 18:25:01 +01:00
|
|
|
grab_state.window.set_geometry(Rectangle::from_loc_and_size(
|
|
|
|
|
window_location + offset,
|
|
|
|
|
grab_state.window.geometry().size,
|
|
|
|
|
));
|
2022-10-26 15:26:07 +02:00
|
|
|
state
|
|
|
|
|
.common
|
|
|
|
|
.shell
|
|
|
|
|
.active_space_mut(&output)
|
|
|
|
|
.floating_layer
|
2022-11-22 15:48:36 +01:00
|
|
|
.map_internal(grab_state.window, &output, Some(window_location + offset));
|
2023-05-12 20:01:37 +02:00
|
|
|
|
2023-04-20 16:19:52 +02:00
|
|
|
let pointer_pos = handle.current_location();
|
|
|
|
|
let relative_pos = state.common.shell.map_global_to_space(pointer_pos, &output);
|
|
|
|
|
Some(window_location + offset + (pointer_pos - relative_pos).to_i32_round())
|
2023-04-19 11:42:46 +02:00
|
|
|
} else {
|
|
|
|
|
None
|
2022-10-26 15:26:07 +02:00
|
|
|
}
|
2023-04-19 11:42:46 +02:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2022-10-26 15:26:07 +02:00
|
|
|
|
|
|
|
|
handle.unset_grab(state, serial, time);
|
|
|
|
|
if self.window.alive() {
|
2023-04-20 16:19:52 +02:00
|
|
|
if let Some(position) = position {
|
2023-04-19 11:42:46 +02:00
|
|
|
handle.motion(
|
|
|
|
|
state,
|
2023-05-12 20:01:37 +02:00
|
|
|
Some((
|
|
|
|
|
PointerFocusTarget::from(self.window.clone()),
|
|
|
|
|
position - self.window.geometry().loc,
|
|
|
|
|
)),
|
2023-04-19 11:42:46 +02:00
|
|
|
&MotionEvent {
|
|
|
|
|
location: handle.current_location(),
|
|
|
|
|
serial: serial,
|
|
|
|
|
time: time,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-10-26 15:26:07 +02:00
|
|
|
Common::set_focus(
|
|
|
|
|
state,
|
|
|
|
|
Some(&KeyboardFocusTarget::from(self.window.clone())),
|
|
|
|
|
&self.seat,
|
|
|
|
|
Some(serial),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|