diff --git a/cosmic-comp-config/src/lib.rs b/cosmic-comp-config/src/lib.rs index 046380763..81602ff74 100644 --- a/cosmic-comp-config/src/lib.rs +++ b/cosmic-comp-config/src/lib.rs @@ -52,6 +52,8 @@ pub struct CosmicCompConfig { pub xwayland_eavesdropping: XwaylandEavesdropping, /// The threshold before windows snap themselves to output edges pub edge_snap_threshold: u32, + /// How far the pointer can travel before it's considered as moved + pub pointer_moved_epsilon: u32, pub accessibility_zoom: ZoomConfig, } @@ -86,6 +88,7 @@ impl Default for CosmicCompConfig { descale_xwayland: XwaylandDescaling::Fractional, xwayland_eavesdropping: XwaylandEavesdropping::default(), edge_snap_threshold: 0, + pointer_moved_epsilon: 10, accessibility_zoom: ZoomConfig::default(), } } diff --git a/src/input/mod.rs b/src/input/mod.rs index 9299a4981..f0c04cd34 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -16,7 +16,7 @@ use crate::{ target::{KeyboardFocusTarget, PointerFocusTarget}, Stage, }, - grabs::{ReleaseMode, ResizeEdge}, + grabs::{MenuGrab, ReleaseMode, ResizeEdge, UngrabOnPointerUp}, layout::{ floating::ResizeGrabMarker, tiling::{NodeDesc, SwapWindowGrab, TilingLayout}, @@ -59,7 +59,8 @@ use smithay::{ }, output::Output, reexports::{ - input::Device as InputDevice, wayland_server::protocol::wl_shm::Format as ShmFormat, + input::Device as InputDevice, + wayland_server::protocol::{wl_shm::Format as ShmFormat, wl_surface::WlSurface}, }, utils::{Point, Rectangle, Serial, SERIAL_COUNTER}, wayland::{ @@ -743,6 +744,58 @@ impl State { } } + fn window_menu_grab( + state: &mut State, + seat: &Seat, + surface: WlSurface, + serial: Serial, + ) -> Option<(MenuGrab, smithay::input::pointer::Focus)> + { + let shell = state.common.shell.write(); + if let Some(mapped) = + shell.element_for_surface(&surface).cloned() + { + let position = if let Some((output, set)) = + shell.workspaces.sets.iter().find(|(_, set)| { + set.sticky_layer.mapped().any(|m| m == &mapped) + }) { + set.sticky_layer + .element_geometry(&mapped) + .unwrap() + .loc + .to_global(output) + } else if let Some(workspace) = shell.space_for(&mapped) + { + let Some(elem_geo) = + workspace.element_geometry(&mapped) + else { + return None; + }; + elem_geo.loc.to_global(&workspace.output) + } else { + return None; + }; + let cursor = seat + .get_pointer() + .unwrap() + .current_location() + .to_i32_round(); + + shell.menu_request( + &surface, + &seat, + serial, + cursor - position.as_logical(), + false, + &state.common.config, + &state.common.event_loop_handle, + false, + ) + } else { + None + } + } + if let Some(mouse_button) = mouse_button { match mouse_button { smithay::backend::input::MouseButton::Left => { @@ -760,7 +813,13 @@ impl State { &state.common.event_loop_handle, false, ); + drop(shell); + + seat_clone + .user_data() + .get_or_insert(UngrabOnPointerUp::new) + .set(true); dispatch_grab( res, seat_clone, serial, state, ); @@ -769,57 +828,132 @@ impl State { } smithay::backend::input::MouseButton::Right => { supress_button(); - self.common.event_loop_handle.insert_idle( - move |state| { - let mut shell = state.common.shell.write(); - let Some(target_elem) = - shell.element_for_surface(&surface) - else { - return; - }; - let Some(geom) = - shell.space_for(target_elem).and_then( - |f| f.element_geometry(target_elem), - ) - else { - return; - }; - let geom = geom.to_f64(); - let center = - geom.loc + geom.size.downscale(2.0); - let offset = center.to_global(&output) - - global_position; - let edge = match ( - offset.x > 0.0, - offset.y > 0.0, - ) { - (true, true) => ResizeEdge::TOP_LEFT, - (false, true) => ResizeEdge::TOP_RIGHT, - (true, false) => { - ResizeEdge::BOTTOM_LEFT - } - (false, false) => { - ResizeEdge::BOTTOM_RIGHT - } - }; - let res = shell.resize_request( - &surface, + let time = event.time_msec(); + self.common.event_loop_handle.insert_idle(move |state| { + let surface_clone = surface.clone(); + let surface_clone2 = surface.clone(); + let seat_clone2 = seat_clone.clone(); + let seat_clone3 = seat_clone.clone(); + + let mut shell = state.common.shell.write(); + let res = shell + .distance_switch_request( + &surface_clone, &seat_clone, serial, - edge, - state - .common - .config - .cosmic_conf - .edge_snap_threshold, + &state.common.config, + move |state| { + state.common.event_loop_handle.insert_idle(move |state| { + let mut shell = state + .common + .shell + .write(); + + let Some(target_elem) = + shell.element_for_surface(&surface) + else { + return; + }; + let Some(geom) = shell + .space_for(target_elem) + .and_then(|f| { + f.element_geometry( + target_elem, + ) + }) + else { + return; + }; + let geom = geom.to_f64(); + let center = geom.loc + + geom + .size + .downscale(2.0); + let offset = center + .to_global(&output) + - global_position; + let edge = match ( + offset.x > 0.0, + offset.y > 0.0, + ) { + (true, true) => { + ResizeEdge::TOP_LEFT + } + (false, true) => { + ResizeEdge::TOP_RIGHT + } + (true, false) => { + ResizeEdge::BOTTOM_LEFT + } + (false, false) => { + ResizeEdge::BOTTOM_RIGHT + } + }; + let res = shell + .resize_request( + &surface, + &seat_clone2, + serial, + edge, + state + .common + .config + .cosmic_conf + .edge_snap_threshold, + false, + ); + drop(shell); + + seat_clone2 + .user_data() + .get_or_insert( + UngrabOnPointerUp::new, + ) + .set(true); + + dispatch_grab(res, seat_clone2, serial, state); + }); + }, + move |state| { + state.common.event_loop_handle.insert_idle(move |state| { + let grab = window_menu_grab( + state, + &seat_clone3, + surface_clone2, + serial, + ); + dispatch_grab(grab, seat_clone3.clone(), serial, state); + + let ptr = seat_clone3.get_pointer().unwrap(); + ptr.motion( + state, + ptr.current_focus().map(|f| + (f, ptr.current_location()) + ), + &MotionEvent { + location: ptr.current_location(), + serial, + time + } + ); + }); + }, false, ); - drop(shell); - dispatch_grab( - res, seat_clone, serial, state, - ); - }, - ); + + drop(shell); + + seat_clone + .user_data() + .get_or_insert(UngrabOnPointerUp::new) + .set(true); + dispatch_grab( + res, + seat_clone, + serial, + state, + ); + }); } _ => {} } @@ -867,7 +1001,12 @@ impl State { ); ptr.frame(self); } else if event.state() == ButtonState::Released { - ptr.unset_grab(self, serial, event.time_msec()) + if let Some(ungrab) = seat.user_data().get::() { + if ungrab.get() { + ungrab.set(false); + ptr.unset_grab(self, serial, event.time_msec()) + } + } } } InputEvent::PointerAxis { event, .. } => { diff --git a/src/shell/element/stack.rs b/src/shell/element/stack.rs index bc3d51654..e2ce1cabd 100644 --- a/src/shell/element/stack.rs +++ b/src/shell/element/stack.rs @@ -929,6 +929,7 @@ impl Program for CosmicStackInternal { true, &state.common.config, &state.common.event_loop_handle, + true, ); std::mem::drop(shell); @@ -971,6 +972,7 @@ impl Program for CosmicStackInternal { false, &state.common.config, &state.common.event_loop_handle, + true, ); std::mem::drop(shell); diff --git a/src/shell/element/window.rs b/src/shell/element/window.rs index ab195e47c..d6e881320 100644 --- a/src/shell/element/window.rs +++ b/src/shell/element/window.rs @@ -520,6 +520,7 @@ impl Program for CosmicWindowInternal { false, &state.common.config, &state.common.event_loop_handle, + true, ); std::mem::drop(shell); diff --git a/src/shell/grabs/menu/mod.rs b/src/shell/grabs/menu/mod.rs index 35b12ec99..830f47081 100644 --- a/src/shell/grabs/menu/mod.rs +++ b/src/shell/grabs/menu/mod.rs @@ -202,8 +202,9 @@ impl Item { } } -/// Menu that comes up when right-clicking an application header bar -#[derive(Debug)] +/// Menu that comes up when: +/// - Right-clicking an application header bar, or +/// - Super + Right-clicking a window pub struct ContextMenu { items: Vec, selected: AtomicBool, diff --git a/src/shell/grabs/mod.rs b/src/shell/grabs/mod.rs index def21cfea..fb409ab09 100644 --- a/src/shell/grabs/mod.rs +++ b/src/shell/grabs/mod.rs @@ -1,3 +1,5 @@ +use std::cell::Cell; + use cosmic_settings_config::shortcuts; use smithay::{ input::{ @@ -67,6 +69,176 @@ pub enum ReleaseMode { NoMouseButtons, } +#[derive(Debug, Clone)] +pub struct DistanceSwitchGrab { + pub moved: Option, + pub unmoved: Option, + pub start: GrabStartData, + pub pointer_moved_epsilon: u32, +} +impl + PointerGrab for DistanceSwitchGrab +{ + fn motion( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + focus: Option<(PointerFocusTarget, Point)>, + event: &MotionEvent, + ) { + let start = self.start.location(); + let current = event.location; + + let x = start.x - current.x; + let y = start.y - current.y; + + let distance = ((x * x) + (y * y)).sqrt(); + if distance > self.pointer_moved_epsilon as f64 { + drop(data.common.shell.try_write().unwrap()); + self.moved.take().unwrap()(data); + } else { + handle.motion(data, focus, event) + } + } + + fn relative_motion( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + focus: Option<( + ::PointerFocus, + Point, + )>, + event: &RelativeMotionEvent, + ) { + handle.relative_motion(data, focus, event) + } + + fn button( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + event: &ButtonEvent, + ) { + handle.button(data, event) + } + + fn axis( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + details: AxisFrame, + ) { + handle.axis(data, details) + } + + fn frame(&mut self, data: &mut State, handle: &mut PointerInnerHandle<'_, State>) { + handle.frame(data) + } + + fn gesture_swipe_begin( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + event: &GestureSwipeBeginEvent, + ) { + handle.gesture_swipe_begin(data, event) + } + + fn gesture_swipe_update( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + event: &GestureSwipeUpdateEvent, + ) { + handle.gesture_swipe_update(data, event) + } + + fn gesture_swipe_end( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + event: &GestureSwipeEndEvent, + ) { + handle.gesture_swipe_end(data, event) + } + + fn gesture_pinch_begin( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + event: &GesturePinchBeginEvent, + ) { + handle.gesture_pinch_begin(data, event) + } + + fn gesture_pinch_update( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + event: &GesturePinchUpdateEvent, + ) { + handle.gesture_pinch_update(data, event) + } + + fn gesture_pinch_end( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + event: &GesturePinchEndEvent, + ) { + handle.gesture_pinch_end(data, event) + } + + fn gesture_hold_begin( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + event: &GestureHoldBeginEvent, + ) { + handle.gesture_hold_begin(data, event) + } + + fn gesture_hold_end( + &mut self, + data: &mut State, + handle: &mut PointerInnerHandle<'_, State>, + event: &GestureHoldEndEvent, + ) { + handle.gesture_hold_end(data, event) + } + + fn start_data(&self) -> &PointerGrabStartData { + match &self.start { + GrabStartData::Touch(_) => unreachable!(), + GrabStartData::Pointer(p) => p, + } + } + + fn unset(&mut self, data: &mut State) { + if self.moved.is_some() { + self.unmoved.take().unwrap()(data); + } + _ = data; + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct UngrabOnPointerUp(Cell); +impl UngrabOnPointerUp { + pub fn new() -> Self { + Self(Cell::new(false)) + } + + pub fn get(&self) -> bool { + self.0.get() + } + + pub fn set(&self, ungrab: bool) { + self.0.set(ungrab); + } +} + mod menu; pub use self::menu::*; mod moving; diff --git a/src/shell/mod.rs b/src/shell/mod.rs index 6ddd844fb..fec463a66 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -1,6 +1,6 @@ use calloop::LoopHandle; use focus::target::WindowGroup; -use grabs::{MenuAlignment, SeatMoveGrabState}; +use grabs::{DistanceSwitchGrab, MenuAlignment, SeatMoveGrabState}; use indexmap::IndexMap; use layout::TilingExceptions; use std::{ @@ -3279,10 +3279,11 @@ impl Shell { target_stack: bool, config: &Config, evlh: &LoopHandle<'static, State>, + client_initiated: bool, ) -> Option<(MenuGrab, Focus)> { let serial = serial.into(); let Some(GrabStartData::Pointer(start_data)) = - check_grab_preconditions(&seat, serial, Some(surface)) + check_grab_preconditions(&seat, serial, client_initiated.then_some(surface)) else { return None; // TODO: an application can send a menu request for a touch event }; @@ -3391,6 +3392,32 @@ impl Shell { Some((grab, Focus::Keep)) } + pub fn distance_switch_request( + &mut self, + surface: &WlSurface, + seat: &Seat, + serial: impl Into>, + config: &Config, + on_moved: Moved, + on_unmoved: Unmoved, + client_initiated: bool, + ) -> Option<(DistanceSwitchGrab, Focus)> { + let serial = serial.into(); + + let start_data = + check_grab_preconditions(&seat, serial, client_initiated.then_some(surface))?; + + Some(( + DistanceSwitchGrab { + moved: Some(on_moved), + unmoved: Some(on_unmoved), + start: start_data, + pointer_moved_epsilon: config.cosmic_conf.pointer_moved_epsilon, + }, + Focus::Keep, + )) + } + pub fn move_request( &mut self, surface: &WlSurface, diff --git a/src/wayland/handlers/xdg_shell/mod.rs b/src/wayland/handlers/xdg_shell/mod.rs index 3b5791107..e99bd4ccd 100644 --- a/src/wayland/handlers/xdg_shell/mod.rs +++ b/src/wayland/handlers/xdg_shell/mod.rs @@ -334,6 +334,7 @@ impl XdgShellHandler for State { false, &self.common.config, &self.common.event_loop_handle, + true, ); if let Some((grab, focus)) = res { std::mem::drop(shell);