// SPDX-License-Identifier: GPL-3.0-only use crate::{ backend::render::{ cursor::{CursorShape, CursorState}, element::AsGlowRenderer, IndicatorShader, Key, Usage, }, shell::{ element::{ stack_hover::{stack_hover, StackHover}, CosmicMappedRenderElement, }, focus::target::{KeyboardFocusTarget, PointerFocusTarget}, layout::floating::TiledCorners, CosmicMapped, CosmicSurface, Direction, ManagedLayer, }, utils::prelude::*, }; use calloop::LoopHandle; use cosmic::theme::CosmicTheme; use smithay::{ backend::{ input::ButtonState, renderer::{ element::{utils::RescaleRenderElement, AsRenderElements, RenderElement}, ImportAll, ImportMem, Renderer, }, }, desktop::{layer_map_for_output, space::SpaceElement}, input::{ pointer::{ AxisFrame, ButtonEvent, GestureHoldBeginEvent, GestureHoldEndEvent, GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, GestureSwipeEndEvent, GestureSwipeUpdateEvent, GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab, PointerInnerHandle, RelativeMotionEvent, }, Seat, }, output::Output, utils::{IsAlive, Logical, Point, Rectangle, Scale, SERIAL_COUNTER}, }; use std::{cell::RefCell, collections::HashSet, sync::atomic::Ordering, time::Instant}; use super::ReleaseMode; pub type SeatMoveGrabState = RefCell>; const RESCALE_ANIMATION_DURATION: f64 = 150.0; pub struct MoveGrabState { window: CosmicMapped, window_offset: Point, indicator_thickness: u8, start: Instant, previous: ManagedLayer, snapping_zone: Option, stacking_indicator: Option<(StackHover, Point)>, } impl MoveGrabState { #[profiling::function] pub fn render( &self, renderer: &mut R, seat: &Seat, output: &Output, theme: &CosmicTheme, ) -> Vec where R: Renderer + ImportAll + ImportMem + AsGlowRenderer, ::TextureId: 'static, CosmicMappedRenderElement: RenderElement, I: From>, { let scale = if self.previous == ManagedLayer::Tiling { 0.6 + ((1.0 - (Instant::now().duration_since(self.start).as_millis() as f64 / RESCALE_ANIMATION_DURATION) .min(1.0)) * 0.4) } else { 1.0 }; let alpha = if &seat.active_output() == output { 1.0 } else { 0.4 }; let cursor_at = seat.get_pointer().unwrap().current_location(); let mut window_geo = self.window.geometry(); window_geo.loc += cursor_at.to_i32_round() + self.window_offset; if !output .geometry() .as_logical() .intersection(window_geo) .is_some() { return Vec::new(); } let output_scale: Scale = output.current_scale().fractional_scale().into(); let scaling_offset = self.window_offset - self.window_offset.to_f64().upscale(scale).to_i32_round(); let render_location = cursor_at.to_i32_round() - output.geometry().loc.as_logical() + self.window_offset - scaling_offset; let active_window_hint = crate::theme::active_window_hint(theme); let focus_element = if self.indicator_thickness > 0 { Some( CosmicMappedRenderElement::from(IndicatorShader::focus_element( renderer, Key::Window(Usage::MoveGrabIndicator, self.window.clone()), Rectangle::from_loc_and_size( render_location, self.window .geometry() .size .to_f64() .upscale(scale) .to_i32_round(), ) .as_local(), self.indicator_thickness, output_scale.x, alpha, [ active_window_hint.red, active_window_hint.green, active_window_hint.blue, ], )) .into(), ) } else { None }; let non_exclusive_geometry = { let layers = layer_map_for_output(&output); layers.non_exclusive_zone() }; let snapping_indicator = match &self.snapping_zone { Some(t) => vec![IndicatorShader::element( renderer, Key::Window(Usage::SnappingIndicator, self.window.clone()), t.overlay_geometry(non_exclusive_geometry), 4, 8, 0.5, output_scale.x, [ active_window_hint.red, active_window_hint.green, active_window_hint.blue, ], ) .into()], None => vec![], }; let (window_elements, popup_elements) = self .window .split_render_elements::>( renderer, (render_location - self.window.geometry().loc) .to_physical_precise_round(output_scale), output_scale, alpha, ); self.stacking_indicator .iter() .flat_map(|(indicator, location)| { indicator.render_elements( renderer, location.to_physical_precise_round(output_scale), output_scale, 1.0, ) }) .chain(popup_elements) .chain(focus_element) .chain(window_elements.into_iter().map(|elem| match elem { CosmicMappedRenderElement::Stack(stack) => { CosmicMappedRenderElement::GrabbedStack( RescaleRenderElement::from_element( stack, render_location.to_physical_precise_round( output.current_scale().fractional_scale(), ), scale, ), ) } CosmicMappedRenderElement::Window(window) => { CosmicMappedRenderElement::GrabbedWindow( RescaleRenderElement::from_element( window, render_location.to_physical_precise_round( output.current_scale().fractional_scale(), ), scale, ), ) } x => x, })) .chain(snapping_indicator) .map(I::from) .collect() } pub fn window(&self) -> CosmicSurface { self.window.active_window() } } struct NotSend(pub T); unsafe impl Send for NotSend {} #[derive(Debug, Clone, PartialEq, Eq)] pub enum SnappingZone { TopMaxim, TopSnap, Left, Right, Bottom, } const SNAPPING_RANGE: i32 = 12; impl SnappingZone { pub fn contains( &self, point: Point, non_exclusive_geometry: Rectangle, output_geometry: Rectangle, ) -> bool { if !output_geometry.contains(point) { return false; } match self { SnappingZone::TopMaxim => point.y < non_exclusive_geometry.loc.y + SNAPPING_RANGE, SnappingZone::TopSnap => { point.y < non_exclusive_geometry.loc.y + SNAPPING_RANGE * 2 && point.y >= non_exclusive_geometry.loc.y + SNAPPING_RANGE } SnappingZone::Bottom => { point.y > non_exclusive_geometry.loc.y + non_exclusive_geometry.size.h - SNAPPING_RANGE } SnappingZone::Left => point.x < non_exclusive_geometry.loc.x + SNAPPING_RANGE, SnappingZone::Right => { point.x > non_exclusive_geometry.loc.x + non_exclusive_geometry.size.w - SNAPPING_RANGE } } } pub fn overlay_geometry( &self, non_exclusive_geometry: Rectangle, ) -> Rectangle { match self { SnappingZone::TopMaxim => non_exclusive_geometry.as_local(), SnappingZone::TopSnap => TiledCorners::Top.relative_geometry(non_exclusive_geometry), SnappingZone::Left => TiledCorners::Left.relative_geometry(non_exclusive_geometry), SnappingZone::Right => TiledCorners::Right.relative_geometry(non_exclusive_geometry), SnappingZone::Bottom => TiledCorners::Bottom.relative_geometry(non_exclusive_geometry), } } } pub struct MoveGrab { window: CosmicMapped, start_data: PointerGrabStartData, seat: Seat, cursor_output: Output, window_outputs: HashSet, previous: ManagedLayer, release: ReleaseMode, // SAFETY: This is only used on drop which will always be on the main thread evlh: NotSend>, } impl PointerGrab for MoveGrab { fn motion( &mut self, state: &mut State, handle: &mut PointerInnerHandle<'_, State>, _focus: Option<(PointerFocusTarget, Point)>, event: &MotionEvent, ) { let Some(current_output) = state .common .shell .outputs() .find(|output| { output .geometry() .as_logical() .overlaps_or_touches(Rectangle::from_loc_and_size( handle.current_location().to_i32_floor(), (0, 0), )) }) .cloned() else { return; }; if self.cursor_output != current_output { state .common .shell .workspaces .active_mut(&self.cursor_output) .tiling_layer .cleanup_drag(); self.cursor_output = current_output.clone(); } let mut borrow = self .seat .user_data() .get::() .map(|s| s.borrow_mut()); if let Some(grab_state) = borrow.as_mut().and_then(|s| s.as_mut()) { 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().as_logical().intersection(window_geo) { if self.window_outputs.insert(output.clone()) { self.window.output_enter(output, overlap); if let Some(indicator) = grab_state.stacking_indicator.as_ref().map(|x| &x.0) { indicator.output_enter(output, overlap); } } } else if self.window_outputs.remove(&output) { self.window.output_leave(output); if let Some(indicator) = grab_state.stacking_indicator.as_ref().map(|x| &x.0) { indicator.output_leave(output); } } } let indicator_location = state .common .shell .stacking_indicator(¤t_output, self.previous); if indicator_location.is_some() != grab_state.stacking_indicator.is_some() { grab_state.stacking_indicator = indicator_location.map(|geo| { let element = stack_hover( state.common.event_loop_handle.clone(), geo.size.as_logical(), state.common.theme.clone(), ); for output in &self.window_outputs { element.output_enter( output, Rectangle::from_loc_and_size( (0, 0), output.geometry().size.as_logical(), ), ); } (element, geo.loc.as_logical()) }); } // Check for overlapping with zones if grab_state.previous == ManagedLayer::Floating { let non_exclusive_geometry = { let layers = layer_map_for_output(¤t_output); layers.non_exclusive_zone() }; let total_geometry = current_output.geometry().as_logical(); grab_state.snapping_zone = vec![ SnappingZone::TopMaxim, SnappingZone::TopSnap, SnappingZone::Left, SnappingZone::Right, SnappingZone::Bottom, ] .iter() .find(|&x| { x.contains( handle.current_location().to_i32_floor(), non_exclusive_geometry, total_geometry, ) }) .cloned(); } } drop(borrow); // While the grab is active, no client has pointer focus handle.motion(state, None, event); if !self.window.alive() { handle.unset_grab(state, event.serial, event.time, true); } } fn relative_motion( &mut self, state: &mut State, handle: &mut PointerInnerHandle<'_, State>, _focus: Option<(PointerFocusTarget, Point)>, event: &RelativeMotionEvent, ) { // While the grab is active, no client has pointer focus handle.relative_motion(state, None, event); } fn button( &mut self, state: &mut State, handle: &mut PointerInnerHandle<'_, State>, event: &ButtonEvent, ) { handle.button(state, event); match self.release { ReleaseMode::NoMouseButtons => { if handle.current_pressed().is_empty() { handle.unset_grab(state, event.serial, event.time, true); } } ReleaseMode::Click => { if event.state == ButtonState::Pressed { handle.unset_grab(state, event.serial, event.time, true); } } } } fn axis( &mut self, state: &mut State, handle: &mut PointerInnerHandle<'_, State>, details: AxisFrame, ) { handle.axis(state, 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 { &self.start_data } } impl MoveGrab { pub fn new( start_data: PointerGrabStartData, window: CosmicMapped, seat: &Seat, initial_cursor_location: Point, initial_window_location: Point, indicator_thickness: u8, previous_layer: ManagedLayer, release: ReleaseMode, evlh: LoopHandle<'static, State>, ) -> MoveGrab { let output = seat.active_output(); let mut outputs = HashSet::new(); outputs.insert(output.clone()); window.output_enter(&output, window.geometry()); // not accurate but... window.moved_since_mapped.store(true, Ordering::SeqCst); let grab_state = MoveGrabState { window: window.clone(), window_offset: (initial_window_location - initial_cursor_location.to_i32_round()) .as_logical(), indicator_thickness, start: Instant::now(), stacking_indicator: None, snapping_zone: None, previous: previous_layer, }; *seat .user_data() .get::() .unwrap() .borrow_mut() = Some(grab_state); { let cursor_state = seat.user_data().get::().unwrap(); cursor_state.set_shape(CursorShape::Grab); } MoveGrab { window, start_data, seat: seat.clone(), window_outputs: outputs, cursor_output: output, previous: previous_layer, release, evlh: NotSend(evlh), } } pub fn is_tiling_grab(&self) -> bool { self.previous == ManagedLayer::Tiling } } impl Drop for MoveGrab { fn drop(&mut self) { // No more buttons are pressed, release the grab. let output = self.seat.active_output(); let seat = self.seat.clone(); let window_outputs = self.window_outputs.drain().collect::>(); let previous = self.previous; let window = self.window.clone(); let _ = self.evlh.0.insert_idle(move |state| { let pointer = seat.get_pointer().unwrap(); let position: Option<(CosmicMapped, Point)> = if let Some(grab_state) = seat.user_data() .get::() .and_then(|s| s.borrow_mut().take()) { if grab_state.window.alive() { let window_location = (pointer.current_location().to_i32_round() + grab_state.window_offset) .as_global(); let workspace_handle = state.common.shell.active_space(&output).handle; for old_output in window_outputs.iter().filter(|o| *o != &output) { grab_state.window.output_leave(old_output); } for (window, _) in grab_state.window.windows() { state .common .shell .toplevel_info_state .toplevel_enter_output(&window, &output); if previous != ManagedLayer::Sticky { state .common .shell .toplevel_info_state .toplevel_enter_workspace(&window, &workspace_handle); } } match previous { ManagedLayer::Sticky => { grab_state.window.set_geometry(Rectangle::from_loc_and_size( window_location, grab_state.window.geometry().size.as_global(), )); let set = state.common.shell.workspaces.sets.get_mut(&output).unwrap(); let (window, location) = set .sticky_layer .drop_window(grab_state.window, window_location.to_local(&output)); Some((window, location.to_global(&output))) } ManagedLayer::Tiling if state.common.shell.active_space(&output).tiling_enabled => { let (window, location) = state .common .shell .active_space_mut(&output) .tiling_layer .drop_window(grab_state.window); Some((window, location.to_global(&output))) } _ => { grab_state.window.set_geometry(Rectangle::from_loc_and_size( window_location, grab_state.window.geometry().size.as_global(), )); let theme = state.common.shell.theme.clone(); let workspace = state.common.shell.active_space_mut(&output); let (window, location) = workspace.floating_layer.drop_window( grab_state.window, window_location.to_local(&workspace.output), ); if previous == ManagedLayer::Floating { if let Some(sz) = grab_state.snapping_zone { if sz == SnappingZone::TopMaxim { state.common.shell.maximize_toggle(&window, &seat); } else { let direction = match sz { SnappingZone::TopSnap => Direction::Up, SnappingZone::Bottom => Direction::Down, SnappingZone::Left => Direction::Left, SnappingZone::Right => Direction::Right, _ => Direction::Up, }; workspace.floating_layer.move_element( direction, &seat, ManagedLayer::Floating, theme, &window, ); } } } Some((window, location.to_global(&output))) } } } else { None } } else { None }; { let cursor_state = seat.user_data().get::().unwrap(); cursor_state.set_shape(CursorShape::Default); } if let Some((mapped, position)) = position { let serial = SERIAL_COUNTER.next_serial(); pointer.motion( state, Some(( PointerFocusTarget::from(mapped.clone()), position.as_logical() - window.geometry().loc, )), &MotionEvent { location: pointer.current_location(), serial, time: 0, }, ); Common::set_focus( state, Some(&KeyboardFocusTarget::from(mapped)), &seat, Some(serial), ) } }); } }