hex/src/rune_ui/meeting_capsules/throw.rs
Dax 9781e5b57e
meetings: labs toggle to keep the recording indicator on any display (#2727)
* meetings: a lab keeps the recording indicator on the display it was dragged to

Labs › Indicator on any display (labs.meeting_indicator_display, off by
default, macOS and Windows) lets a release of the recording capsule or a panel
capsule land in the nearest corner of the display under the pointer, measured
by that display's own size, and pins the window there with Rune's
DisplayTarget::Native. The landed display is saved beside the corner as
meetings.recording_indicator_display, the OS's own name for it (the monitor's
device name on Windows, the CGDirectDisplayID on macOS), and every capsule
shows there from then on; a display that is gone falls back to the primary
display and keeps the saved name. Landing on another display jumps instead of
gliding, since a glide is measured within one display.

Off, every release and every show takes the primary-display path exactly as
before, and the saved display is ignored. A native Wayland capsule has no
display to measure, so Linux does not list the card.

The inherited pill now takes the offer's corner and display explicitly, so a
later drag measures from where its window really is.

Not verified on real multi-monitor hardware on either OS, and the macOS and
Windows halves were not compiled for Hex itself (no macOS SDK here; the Windows
C dependencies need ml64.exe), though scrawl-capture's monitor helper was
checked for x86_64-pc-windows-msvc.

Also refreshes the Labs and What's new goldens, which were already stale on dev
from #2650 (clipboard phrase).

* meetings: put the indicator back on the primary display with the lab off, and land where the pointer is

Review fixes for the display lab:

- `placement::place_on` always sets the display target now: the saved display
  while the lab is on, and `DisplayTarget::Primary` while it is off, so
  turning the lab off mid-session brings a capsule pinned to another display
  back instead of leaving the window there while every release measures the
  primary one. The pure choice is `screen::placed_target`, tested.
- `screen::displays` reports whether the pointer is actually on one of the
  displays it lists (`Displays { all, pointer }`). A release with no pointer
  display (a failed cursor read, or a gap between two displays) now measures
  the display it was dragged on instead of treating the first entry as the
  landing display. New test: `a_pointer_on_no_display_lands_where_it_was_dragged`.
- A saved display that is gone resolves through `screen::target_in_displays`,
  a pure function now, with `a_saved_display_that_is_gone_is_the_primary_target`.
- An accepted offer hands the pill the capsule's own corner and display, not
  the saved anchor, so a drag that has not reached settings yet still lands
  the recording where its window is.
- `hex settings` help for `labs.meeting_indicator_display` says "(macOS and
  Windows)", and the labs and meetings maps name the real test list and say
  the lab off explicitly returns a capsule to the primary display.
2026-11-24 12:08:02 -05:00

348 lines
12 KiB
Rust

//! Throwing a meeting capsule into a corner: the drag's release velocity,
//! the corner that throw carries it toward, and the spring that carries the
//! window from where it was let go into that corner.
use std::collections::VecDeque;
use rune::Spring;
use super::placement::corner_placement;
use crate::app_settings::MeetingIndicatorCorner;
/// The kit-motion `GLIDE` token: travel to a new place, no overshoot.
pub(super) const GLIDE: Spring = Spring::new(0.32, 0.0);
/// How far back the release velocity reads the pointer, in seconds.
const VELOCITY_WINDOW: f64 = 0.08;
/// The fastest throw counted, in points per second.
const MAX_SPEED: f32 = 6000.0;
/// UIScrollView's normal deceleration per millisecond: a throw picks the
/// corner nearest where the capsule would coast to rest.
const DECELERATION: f32 = 0.998;
/// Within this many points of the corner (and ten times it in points per
/// second) the glide is done.
const SETTLED: f32 = 0.5;
/// One drag's pointer history and the glide after it.
#[derive(Debug, Default)]
pub(super) struct Throw {
/// The window's offset from its corner when the drag began: a press
/// mid-glide catches the capsule where it is.
grab: (f32, f32),
/// `(clock, x, y)` offsets from the corner over the last
/// [`VELOCITY_WINDOW`].
samples: VecDeque<(f64, f32, f32)>,
glide: Option<Glide>,
}
impl Throw {
/// A press: stops a glide in flight where it is.
pub(super) fn press(&mut self, now: f64) {
self.grab = self
.glide
.take()
.and_then(|glide| glide.at(now))
.unwrap_or_default();
self.samples.clear();
self.samples.push_back((now, self.grab.0, self.grab.1));
}
/// The window's offset from its corner after pointer travel (dx, dy),
/// recorded for the release velocity.
pub(super) fn track(&mut self, now: f64, dx: f32, dy: f32) -> (f32, f32) {
let offset = (self.grab.0 + dx, self.grab.1 + dy);
self.samples.push_back((now, offset.0, offset.1));
while self.samples.len() > 2
&& self
.samples
.front()
.is_some_and(|(at, ..)| now - at > VELOCITY_WINDOW)
{
self.samples.pop_front();
}
offset
}
/// The pointer's velocity over the last [`VELOCITY_WINDOW`], in points
/// per second. A pointer held still before release throws nothing.
pub(super) fn velocity(&self, now: f64) -> (f32, f32) {
let recent = || {
self.samples
.iter()
.filter(move |(at, ..)| now - at <= VELOCITY_WINDOW)
};
let (Some(first), Some(last)) = (recent().next(), recent().next_back()) else {
return (0.0, 0.0);
};
let dt = (last.0 - first.0) as f32;
if dt < 0.008 {
return (0.0, 0.0);
}
let velocity = ((last.1 - first.1) / dt, (last.2 - first.2) / dt);
let speed = velocity.0.hypot(velocity.1);
if speed > MAX_SPEED {
let scale = MAX_SPEED / speed;
(velocity.0 * scale, velocity.1 * scale)
} else {
velocity
}
}
/// Ends the drag with no glide.
pub(super) fn stop(&mut self) {
self.grab = (0.0, 0.0);
self.samples.clear();
self.glide = None;
}
/// Glides from `landing` into its corner, starting `now`. Returns the
/// first offset, or `None` when there is nothing to travel.
pub(super) fn glide(&mut self, landing: Landing, now: f64) -> Option<(f32, f32)> {
self.stop();
let glide = Glide::new(landing.offset, landing.velocity, now);
let first = glide.at(now)?;
self.glide = Some(glide);
Some(first)
}
/// Where the glide holds the window `now`, or `None` once it is in its
/// corner (or there is no glide).
pub(super) fn step(&mut self, now: f64) -> Option<(f32, f32)> {
let offset = self.glide.as_ref()?.at(now);
if offset.is_none() {
self.glide = None;
}
offset
}
pub(super) fn gliding(&self) -> bool {
self.glide.is_some()
}
}
/// Where a released capsule goes: its corner, and its offset and velocity
/// from that corner as it is let go.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct Landing {
pub(super) corner: MeetingIndicatorCorner,
pub(super) offset: (f32, f32),
pub(super) velocity: (f32, f32),
}
/// The corner nearest where a capsule window of `size` let go at `offset`
/// from `from` with `velocity` would coast to rest, on a `screen`.
pub(super) fn land(
from: MeetingIndicatorCorner,
offset: (f32, f32),
velocity: (f32, f32),
screen: (f32, f32),
size: (f32, f32),
) -> Landing {
let (coast_x, coast_y) = coast(velocity);
let corner = nearest_corner(from, offset.0 + coast_x, offset.1 + coast_y, screen, size);
let (fx, fy) = rest_origin(from, screen, size);
let (tx, ty) = rest_origin(corner, screen, size);
Landing {
corner,
offset: (offset.0 + fx - tx, offset.1 + fy - ty),
velocity,
}
}
/// Where a throw of `velocity` points a second comes to rest, in points: the
/// coast UIScrollView's deceleration covers, shared by the corner a release
/// picks within one display (`nearest_corner`) and across displays
/// (`screen::release`).
pub(super) fn coast(velocity: (f32, f32)) -> (f32, f32) {
let coast = DECELERATION / (1.0 - DECELERATION) / 1000.0;
(velocity.0 * coast, velocity.1 * coast)
}
/// The top-left of a capsule window of `size` points resting in `corner`
/// on a screen of `screen` points. A drag's (dx, dy) adds to it in every
/// corner.
pub(super) fn rest_origin(
corner: MeetingIndicatorCorner,
screen: (f32, f32),
size: (f32, f32),
) -> (f32, f32) {
let (_, margin, _, inset) = corner_placement(corner, 0.0, 0.0);
let x = if corner.is_right() {
screen.0 - inset - size.0
} else {
inset
};
let y = if corner.is_top() {
margin
} else {
screen.1 - margin - size.1
};
(x, y)
}
/// The corner nearest the centre of a capsule window of `size` points after
/// a drag of (dx, dy) points from `from`, on a screen of `screen` points.
pub(super) fn nearest_corner(
from: MeetingIndicatorCorner,
dx: f32,
dy: f32,
screen: (f32, f32),
size: (f32, f32),
) -> MeetingIndicatorCorner {
let (left, top) = rest_origin(from, screen, size);
let x = left + dx + size.0 / 2.0;
let y = top + dy + size.1 / 2.0;
match (y < screen.1 / 2.0, x >= screen.0 / 2.0) {
(true, true) => MeetingIndicatorCorner::TopRight,
(true, false) => MeetingIndicatorCorner::TopLeft,
(false, true) => MeetingIndicatorCorner::BottomRight,
(false, false) => MeetingIndicatorCorner::BottomLeft,
}
}
/// The window springing from an offset to rest in its corner.
#[derive(Clone, Copy, Debug, PartialEq)]
struct Glide {
from: (f32, f32),
velocity: (f32, f32),
start: f64,
duration: f32,
}
impl Glide {
fn new(from: (f32, f32), velocity: (f32, f32), start: f64) -> Self {
let duration = GLIDE
.settling_duration_from(from.0, velocity.0, 0.0, SETTLED)
.max(GLIDE.settling_duration_from(from.1, velocity.1, 0.0, SETTLED));
Self {
from,
velocity,
start,
duration,
}
}
/// The offset `now`, or `None` once settled.
fn at(&self, now: f64) -> Option<(f32, f32)> {
let t = ((now - self.start) as f32).max(0.0);
if t >= self.duration {
return None;
}
Some((
GLIDE.sample(self.from.0, self.velocity.0, 0.0, t).0,
GLIDE.sample(self.from.1, self.velocity.1, 0.0, t).0,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use MeetingIndicatorCorner::*;
const SCREEN: (f32, f32) = (1512.0, 982.0);
const SIZE: (f32, f32) = (260.0, 180.0);
#[test]
fn velocity_reads_the_last_moments_of_the_drag() {
let mut throw = Throw::default();
throw.press(0.0);
for i in 1..=10 {
throw.track(i as f64 * 0.016, -(i as f32) * 16.0, 0.0);
}
let (vx, vy) = throw.velocity(0.16);
assert!((vx + 1000.0).abs() < 1.0, "{vx}");
assert_eq!(vy, 0.0);
// Held still before release: no throw.
assert_eq!(throw.velocity(0.4), (0.0, 0.0));
// A flick is capped.
let mut flick = Throw::default();
flick.press(0.0);
flick.track(0.01, -500.0, 0.0);
let (vx, _) = flick.velocity(0.01);
assert!((vx + MAX_SPEED).abs() < 0.1, "{vx}");
}
#[test]
fn a_throw_picks_the_corner_it_heads_for() {
// Let go near where it started, but thrown left and down.
let landing = land(TopRight, (-100.0, 40.0), (-2000.0, 0.0), SCREEN, SIZE);
assert_eq!(landing.corner, TopLeft);
let landing = land(TopRight, (-100.0, 40.0), (0.0, 1500.0), SCREEN, SIZE);
assert_eq!(landing.corner, BottomRight);
// Dropped without a throw, the nearest corner.
let landing = land(TopRight, (-100.0, 40.0), (0.0, 0.0), SCREEN, SIZE);
assert_eq!(
landing,
Landing {
corner: TopRight,
offset: (-100.0, 40.0),
velocity: (0.0, 0.0),
}
);
// Dragged past the middle but thrown back, it returns.
let landing = land(TopRight, (-700.0, 0.0), (2500.0, 0.0), SCREEN, SIZE);
assert_eq!(landing.corner, TopRight);
}
#[test]
fn the_window_lands_in_the_new_corner_where_it_was_let_go() {
let landing = land(TopRight, (-1100.0, 0.0), (-800.0, 0.0), SCREEN, SIZE);
assert_eq!(landing.corner, TopLeft);
let before = rest_origin(TopRight, SCREEN, SIZE);
let after = rest_origin(TopLeft, SCREEN, SIZE);
assert!((before.0 - 1100.0 - (after.0 + landing.offset.0)).abs() < 0.01);
assert!((before.1 - (after.1 + landing.offset.1)).abs() < 0.01);
}
#[test]
fn the_glide_springs_into_the_corner_and_stops() {
let mut throw = Throw::default();
let landing = Landing {
corner: TopLeft,
offset: (120.0, 30.0),
velocity: (-900.0, 0.0),
};
let first = throw.glide(landing, 1.0).unwrap();
assert_eq!(first, (120.0, 30.0));
let mid = throw.step(1.1).unwrap();
assert!(mid.0 < 120.0 && mid.0 > -1.0, "{mid:?}");
assert!(mid.1 < 30.0 && mid.1 > 0.0, "{mid:?}");
// No overshoot past the corner on the way in.
let mut t = 1.0;
while let Some((x, y)) = throw.step(t) {
assert!(x >= -SETTLED && y >= -SETTLED, "{x} {y} at {t}");
t += 1.0 / 120.0;
}
assert!(t - 1.0 < 1.0, "settles within a second, took {t}");
assert!(!throw.gliding());
}
#[test]
fn a_press_catches_a_gliding_capsule_where_it_is() {
let mut throw = Throw::default();
throw.glide(
Landing {
corner: TopLeft,
offset: (200.0, 0.0),
velocity: (0.0, 0.0),
},
0.0,
);
let held = throw.step(0.05).unwrap();
throw.press(0.05);
assert!(!throw.gliding());
assert_eq!(throw.track(0.06, 10.0, 0.0), (held.0 + 10.0, held.1));
}
#[test]
fn nothing_to_travel_is_no_glide() {
let mut throw = Throw::default();
let landing = Landing {
corner: TopRight,
offset: (0.0, 0.0),
velocity: (0.0, 0.0),
};
assert_eq!(throw.glide(landing, 0.0), None);
assert!(!throw.gliding());
}
}