Lines
100 %
Functions
84.62 %
Branches
//! What the mouse or the size/move mode holds: a drag, or a press waiting for its release.
use ratatui::layout::{Position, Rect};
use super::App;
use super::button_press::KeyPress;
use super::drag::Drag;
use crate::window::WindowId;
/// What holds: a desktop window, or the dialog on top.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Owner {
Window(WindowId),
Layer,
}
/// The face of a press: a dialog button by its index, or the `[■]` icon of a frame.
pub(crate) enum Face {
Button(usize),
Close,
/// A press that acts if the pointer is still on its `area` when released.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Press {
face: Face,
area: Rect,
inside: bool,
pub(crate) enum Grip {
Drag(Drag),
Press(Press),
Key(KeyPress),
pub(crate) struct Hold {
pub(super) owner: Owner,
pub(super) grip: Grip,
/// The overlay stack the hold began on.
epoch: u64,
impl Hold {
/// A press outlives no push or pop of the overlays, nor does a drag of a layer; a window
/// dragged from the keyboard stays held under whatever opens above it.
fn survives(self, epoch: u64) -> bool {
let window_drag = matches!((self.owner, self.grip), (Owner::Window(_), Grip::Drag(_)));
window_drag || self.epoch == epoch
impl App {
/// The hold, if what it began on is still there.
pub(super) fn live_hold(&self) -> Option<Hold> {
self.hold
.filter(|hold| hold.survives(self.overlays.epoch()))
pub(super) fn begin_hold(&mut self, owner: Owner, grip: Grip) {
let epoch = self.overlays.epoch();
self.hold = Some(Hold { owner, grip, epoch });
pub(crate) fn hold_press(&mut self, owner: Owner, face: Face, area: Rect) {
let press = Press {
face,
area,
inside: true,
};
self.begin_hold(owner, Grip::Press(press));
/// Follows the pointer, now at `position`, with a press held.
pub(crate) fn track_press(&mut self, position: Position) {
if let Some(Hold {
grip: Grip::Press(press),
..
}) = &mut self.hold
{
press.inside = press.area.contains(position);
/// Ends a press; the face it pressed when the release is on it.
pub(crate) fn release_press(&mut self, position: Position) -> Option<Face> {
let live = self.live_hold();
let held = self
.hold
.take_if(|hold| matches!(hold.grip, Grip::Press(_)))?;
match (live, held.grip) {
(Some(_), Grip::Press(press)) if press.area.contains(position) => Some(press.face),
_ => None,
/// Drops a press held: a key was typed.
pub(crate) fn cancel_press(&mut self) {
.take_if(|hold| matches!(hold.grip, Grip::Press(_)));
/// The button drawn down: the one a mouse press holds under the pointer, or a key presses.
pub(crate) fn pressed_button(&self) -> Option<usize> {
let held = self.live_hold().and_then(|hold| match hold.grip {
Grip::Press(Press {
face: Face::Button(index),
}) => Some(index),
});
held.or_else(|| self.key_pressed_button())