cosmic-comp/src/shell/layout/tiling/grabs.rs

72 lines
2.1 KiB
Rust
Raw Normal View History

2022-03-29 18:03:21 +02:00
// SPDX-License-Identifier: GPL-3.0-only
2022-07-04 16:00:29 +02:00
use crate::{shell::layout::Orientation, utils::prelude::*};
use atomic_float::AtomicF64;
2022-03-29 18:03:21 +02:00
use smithay::{
2022-07-04 16:00:29 +02:00
reexports::wayland_server::DisplayHandle,
utils::{Logical, Size},
2022-07-04 16:00:29 +02:00
wayland::seat::{
AxisFrame, ButtonEvent, MotionEvent, PointerGrab, PointerGrabStartData, PointerInnerHandle,
},
2022-03-29 18:03:21 +02:00
};
use std::sync::{atomic::Ordering, Arc};
2022-03-29 18:03:21 +02:00
pub struct ResizeForkGrab {
pub start_data: PointerGrabStartData,
pub orientation: Orientation,
pub initial_size: Size<i32, Logical>,
2022-03-29 18:03:21 +02:00
pub initial_ratio: f64,
pub ratio: Arc<AtomicF64>,
2022-03-29 18:03:21 +02:00
}
impl PointerGrab<State> for ResizeForkGrab {
2022-03-29 18:03:21 +02:00
fn motion(
&mut self,
_data: &mut State,
2022-07-04 16:00:29 +02:00
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
event: &MotionEvent,
2022-03-29 18:03:21 +02:00
) {
// While the grab is active, no client has pointer focus
handle.motion(event.location, None, event.serial, event.time);
2022-03-29 18:03:21 +02:00
let delta = event.location - self.start_data.location;
let delta = match self.orientation {
Orientation::Vertical => delta.x / self.initial_size.w as f64,
Orientation::Horizontal => delta.y / self.initial_size.h as f64,
};
self.ratio.store(
0.9f64.min(0.1f64.max(self.initial_ratio + delta)),
Ordering::SeqCst,
);
2022-03-29 18:03:21 +02:00
}
fn button(
&mut self,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
event: &ButtonEvent,
2022-03-29 18:03:21 +02:00
) {
handle.button(event.button, event.state, event.serial, event.time);
2022-03-29 18:03:21 +02:00
if handle.current_pressed().is_empty() {
// No more buttons are pressed, release the grab.
handle.unset_grab(event.serial, event.time);
2022-03-29 18:03:21 +02:00
}
}
2022-07-04 16:00:29 +02:00
fn axis(
&mut self,
_data: &mut State,
_dh: &DisplayHandle,
handle: &mut PointerInnerHandle<'_, State>,
details: AxisFrame,
) {
2022-03-29 18:03:21 +02:00
handle.axis(details)
}
fn start_data(&self) -> &PointerGrabStartData {
&self.start_data
}
}