Captures: the tap reports a press while the list is open, so a click away closes it

suppression's event_callback hands CapsuleKey::Press to the capsule on any
mouse-down while the list holds the keys, and still delivers the click to
the app and the listener. The flow closes the list when the pointer isn't on
the capsule. Replaces the 50 ms button-state poll, which could miss a quick
tap-to-click and woke the capsule 20 times a second.
This commit is contained in:
Kit Langton
2026-10-26 11:41:19 -07:00
parent 630cdb5688
commit f228c915bb
5 changed files with 28 additions and 50 deletions
+3 -2
View File
@@ -155,8 +155,9 @@ The capsule // capture.capsule (rune_ui/capture_cap
│ │ └── List open -> the session tap takes every key press without ⌘ or ⌃
│ │ that isn't a Hex shortcut: text in the active layout, ⌫, ↑↓, ⏎,
│ │ ⇧⏎ (always), 1-9, Esc; their releases too. A send, Esc, or a
│ │ press off the capsule (button state polled while the list is open)
│ │ closes the list and gives the keys back; hiding drops the grab
│ │ press off the capsule (the tap reports mouse-downs while the list
│ │ is open, and still delivers them) closes the list and gives the
│ │ keys back; hiding drops the grab
│ ├── `offer` (a rule offer) -> not drawn; Scrawl's capsule doesn't draw it
│ ├── Nothing fits, or no answer -> "Send to…" opens the list
│ ├── Every written capture -> Paste (the one accent action) and Inspect, as
+6
View File
@@ -2104,6 +2104,9 @@ pub enum CapsuleKey {
},
/// 1 to 9 typed in the list (otherwise text).
Digit(u8),
/// A mouse button went down anywhere, while the list is open. The click
/// still reaches the app under it.
Press,
}
type OnCapsuleKey = Arc<dyn Fn(CapsuleKey) + Send + Sync>;
@@ -3964,11 +3967,13 @@ mod tests {
assert!(!capsule_key(CapsuleKey::Shortcut));
assert!(!capsule_key(CapsuleKey::Escape));
assert!(capsule_key(CapsuleKey::OpenList));
assert!(!capsule_key(CapsuleKey::Press), "a press matters only to the list");
second.set(CapsuleKeys::Route { escape: true });
assert!(wants_escape());
second.set(CapsuleKeys::List);
assert!(list_open() && !wants_escape());
assert!(capsule_key(CapsuleKey::Text("ho".into())));
assert!(capsule_key(CapsuleKey::Press));
drop(second);
assert!(!picking() && !list_open());
assert!(!capsule_key(CapsuleKey::Escape));
@@ -3979,6 +3984,7 @@ mod tests {
(2, CapsuleKey::Escape),
(2, CapsuleKey::OpenList),
(2, CapsuleKey::Text("ho".into())),
(2, CapsuleKey::Press),
]
);
}
+8 -7
View File
@@ -183,8 +183,8 @@ pub(crate) enum Input {
/// The recording it shows left the snapshot with no outcome: a chip's
/// discard (the picker is already up) or `hex capture discard`.
Gone,
/// A click off the capsule: an open list closes, so typing goes back
/// to the app in front.
/// A mouse button went down while the list was open: off the capsule,
/// the list closes, so typing goes back to the app in front.
ClickAway,
}
@@ -417,10 +417,6 @@ impl Flow {
}
/// The pointer came onto the capsule or left it.
pub(crate) fn hovered(&self) -> bool {
self.hovered
}
pub(crate) fn hover(&mut self, on: bool, now: f64) {
self.hovered = on;
if let (
@@ -531,7 +527,7 @@ impl Flow {
(Phase::Routed(routed), Input::SendRow(row)) => {
send(routed, Some(row), false, now, &mut actions)
}
(Phase::Routed(mut routed), Input::ClickAway) => {
(Phase::Routed(mut routed), Input::ClickAway) if !self.hovered => {
routed.open = None;
Phase::Routed(routed)
}
@@ -1287,6 +1283,11 @@ mod tests {
flow.input(Input::Routed(guess()), 0.0);
flow.input(Input::OpenList, 0.5);
assert_eq!(flow.keys(0.6), Some(CapsuleKeys::List));
// A press on the capsule is the capsule's own click.
flow.hover(true, 0.6);
flow.input(Input::ClickAway, 0.6);
assert_eq!(flow.keys(0.6), Some(CapsuleKeys::List));
flow.hover(false, 0.65);
assert!(flow.input(Input::ClickAway, 0.7).is_empty());
assert_eq!(flow.keys(0.8), Some(CapsuleKeys::Route { escape: false }));
assert!(matches!(flow.pres(0.8), Pres::Done(_)));
+1 -41
View File
@@ -318,8 +318,6 @@ pub(crate) struct CapsuleState {
/// A capture started: the capsule moves to the display under the
/// pointer, even while still up on another one.
replace: bool,
/// A mouse button was down at the latest poll while the list was open.
pressed: bool,
/// The keys Hex's tap hands the capsule ([`Flow::keys`]): the overlay
/// never takes focus.
keys: Option<(
@@ -354,7 +352,6 @@ impl CapsuleState {
shown: false,
follow: false,
replace: false,
pressed: false,
keys: None,
picker: None,
picker_mode: None,
@@ -509,7 +506,6 @@ fn poll(s: &mut CapsuleState, now: f64, tx: &mut Tx) -> bool {
overlay.set_follow_pointer(follow);
}
}
changed |= click_away(s, now, tx);
take_keys(s, now);
// The on-screen picker shows what the capsule picks.
let picking = match s.surface.pres {
@@ -559,43 +555,6 @@ fn take_keys(s: &mut CapsuleState, now: f64) {
}
}
/// While the list holds the keyboard, a press off the capsule closes it, so
/// typing goes back to the app in front. The capsule lets those clicks
/// through and never sees them, so it watches the buttons until the list
/// closes.
fn click_away(s: &mut CapsuleState, now: f64, tx: &mut Tx) -> bool {
let listing =
s.overlay.is_some() && s.flow.keys(now) == Some(crate::capture_host::CapsuleKeys::List);
if !listing {
s.pressed = false;
return false;
}
let pressed = mouse_down();
let away = pressed && !s.pressed && !s.flow.hovered();
s.pressed = pressed;
tx.wake_after(CLICK_AWAY_EVERY);
if away {
s.apply(Input::ClickAway, now);
s.present(now);
s.live.sync(&s.surface);
}
away
}
const CLICK_AWAY_EVERY: Duration = Duration::from_millis(50);
/// Whether any of the first three mouse buttons is down, session-wide.
fn mouse_down() -> bool {
#[link(name = "CoreGraphics", kind = "framework")]
unsafe extern "C" {
fn CGEventSourceButtonState(state: i32, button: u32) -> bool;
}
// kCGEventSourceStateCombinedSessionState: what the session sees.
const COMBINED: i32 = 0;
// SAFETY: a stateless query of the session's button state.
(0..3).any(|button| unsafe { CGEventSourceButtonState(COMBINED, button) })
}
fn key_input(key: crate::capture_host::CapsuleKey) -> Input {
use crate::capture_host::CapsuleKey;
match key {
@@ -608,6 +567,7 @@ fn key_input(key: crate::capture_host::CapsuleKey) -> Input {
CapsuleKey::Down => Input::Move(1),
CapsuleKey::Return { shift } => Input::Send { always: shift },
CapsuleKey::Digit(digit) => Input::SendRow(usize::from(digit.saturating_sub(1))),
CapsuleKey::Press => Input::ClickAway,
}
}
+10
View File
@@ -679,6 +679,16 @@ unsafe extern "C" fn event_callback(
} else {
None
};
// A press anywhere while the list is open may close it; the click still
// goes to the app and to the listener as chord evidence.
#[cfg(hex_capture)]
if matches!(
event_type,
EVENT_LEFT_MOUSE_DOWN | EVENT_RIGHT_MOUSE_DOWN | EVENT_OTHER_MOUSE_DOWN
) && crate::capture_host::list_open()
{
crate::capture_host::capsule_key(crate::capture_host::CapsuleKey::Press);
}
let input = match event_type {
EVENT_FLAGS_CHANGED => {
// SAFETY: CoreGraphics supplied a valid event to this callback.