R6.13 round 1 fix: notches per platform, the worker decides arrows

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-15 20:22:23 -04:00
1 parent c2897aa21a
commit d427d05b96
15 files changed
+196 -73

No files matched your search

+8
View File
@@ -237,6 +237,14 @@ impl Worker {
Ok(())
}
/// Up or Down: whether it turned, not reaching the kernel.
pub fn turn_key(&mut self, doc: &Doc, char_code: u32, key_code: i32) -> io::Result<bool> {
let doc_id = self.doc_id(doc)?;
let payload = Pack::new().u64(doc_id).i64(char_code as i64).i64(key_code as i64).bytes();
let reply = self.request(tag::TURN_KEY, &payload)?;
Ok(Unpack::new(&reply).u64()? == 1)
}
/// Drags a selected object's handle, start then end.
pub fn drag_graphic(&mut self, doc: &Doc, from: (i32, i32), to: (i32, i32)) -> io::Result<()> {
for (kind, (x, y)) in [(0, from), (1, to)] {
+9 -11
View File
@@ -362,11 +362,14 @@ pub fn stop_all(docs: &HashMap<u64, Document>) {
/// The current generation of `id`.
pub fn generation(id: u64) -> u64 {
slots()
.lock()
.expect("the events lock")
.get(&id)
.map_or(0, |slot| slot.batch.generation)
slots().lock().expect("the events lock").get(&id).map_or(0, |slot| slot.batch.generation)
}
/// Whether `id` shows a selection, object or caret.
pub fn picked(id: u64) -> bool {
let slots = slots().lock().expect("the events lock");
let Some(batch) = slots.get(&id).map(|slot| &slot.batch) else { return false };
batch.graphic.is_some() || !batch.selection.is_empty() || batch.cursor_visible && batch.cursor.is_some()
}
/// Blocks until documents have unpushed events.
@@ -399,12 +402,7 @@ pub fn clear_result(id: u64) {
/// The completion the last dispatch reported.
pub fn take_result(id: u64) -> Option<(String, bool)> {
slots()
.lock()
.expect("the events lock")
.get_mut(&id)?
.result
.take()
slots().lock().expect("the events lock").get_mut(&id)?.result.take()
}
/// The bridged a11y text now.
+2
View File
@@ -88,6 +88,8 @@ pub mod tag {
pub const REVISION: u8 = 34;
/// One layer's pixels from the last `SLIDE_LAYERS`.
pub const SLIDE_LAYER: u8 = 35;
/// Up or Down: turns the part when nothing is picked.
pub const TURN_KEY: u8 = 36;
pub const OK: u8 = 100;
pub const ERR: u8 = 101;
}
+1 -1
View File
@@ -20,7 +20,7 @@ pub use client::{
};
#[cfg(feature = "view")]
pub use view::{
CONTEXT, Copy, Cut, Geom, Layout, LokError, LokView, Paste, Reported, Turn, ZOOM_STEPS,
CONTEXT, Copy, Cut, Geom, Layout, LokError, LokView, Paste, Reported, Turn, ZOOM_STEPS, notch,
Zoomed, init, layout, paper, read_off, scheme,
};
#[cfg(feature = "view")]
+1 -1
View File
@@ -34,7 +34,7 @@ pub use input::{CONTEXT, Copy, Cut, Paste, init};
pub use off::read_off;
pub use render::{paper, scheme};
mod turn;
pub use turn::Turn;
pub use turn::{Turn, notch};
pub(crate) use crate::{TILE_TWIPS, TWIPS_PER_PIXEL};
+22 -6
View File
@@ -64,6 +64,18 @@ pub(super) enum Input {
Visible(Rect, (u32, i32)),
/// Shows sheet or slide `part`, from 0.
Part(usize),
/// Up or Down; turns `step` parts when nothing is picked.
Turn {
char_code: u32,
key_code: i32,
step: i32,
},
}
/// What an input gives back.
pub(super) enum Back {
Copied(String),
Turned(i32),
}
impl Input {
@@ -73,6 +85,7 @@ impl Input {
// Arrows, Home, End, Page keys, Escape.
Input::Key { key_code, .. } => !matches!(key_code & 0x0FFF, 1024..=1031 | 1281),
Input::Mouse(_)
| Input::Turn { .. }
| Input::Copy { cut: false }
| Input::Visible(..)
| Input::Part(_)
@@ -169,7 +182,7 @@ fn lok_button(button: MouseButton) -> Option<i32> {
}
/// Runs one input; gives a copy's mark and the revision.
pub(super) fn run(worker: &Mutex<Worker>, doc: &Doc, input: Input) -> (io::Result<Option<String>>, Option<u64>) {
pub(super) fn run(worker: &Mutex<Worker>, doc: &Doc, input: Input) -> (io::Result<Option<Back>>, Option<u64>) {
let mut worker = worker.lock().expect("the worker lock");
let counts = !matches!(input, Input::Visible(..));
let copied = apply(&mut worker, doc, input);
@@ -181,12 +194,16 @@ pub(super) fn run(worker: &Mutex<Worker>, doc: &Doc, input: Input) -> (io::Resul
(copied, now)
}
fn apply(worker: &mut Worker, doc: &Doc, input: Input) -> io::Result<Option<String>> {
fn apply(worker: &mut Worker, doc: &Doc, input: Input) -> io::Result<Option<Back>> {
let done = match input {
Input::Key {
char_code,
key_code,
} => worker.post_key(doc, char_code, key_code),
Input::Turn { char_code, key_code, step } => {
let turned = worker.turn_key(doc, char_code, key_code)?;
return Ok(turned.then_some(Back::Turned(step)));
}
Input::Mouse(event) => worker.post_mouse(doc, event),
Input::Drag { from, to } => worker.drag_graphic(doc, from, to),
Input::Visible(area, scale) => doc.visible_area(worker, area, scale),
@@ -195,9 +212,8 @@ fn apply(worker: &mut Worker, doc: &Doc, input: Input) -> io::Result<Option<Stri
Input::Theme(scheme) => worker.run(doc, "view.theme", &[scheme]),
Input::Text { commit, text } => worker.post_text(doc, commit, &text),
Input::Copy { cut } => {
return clip::copy_out(worker, doc, cut, |clip| {
clipboard::write(clip).map_err(io::Error::other)
});
let copied = clip::copy_out(worker, doc, cut, |clip| clipboard::write(clip).map_err(io::Error::other));
return copied.map(|copied| copied.map(Back::Copied));
}
Input::Run { commands, at } => {
let values: Vec<Vec<&str>> =
@@ -299,7 +315,7 @@ impl LokView {
cx: &mut Context<Self>,
) {
if self.walk(&event.keystroke, cx)
|| self.turn_key(&event.keystroke, cx)
|| self.turn_key(&event.keystroke)
|| self.key_down(&event.keystroke)
{
cx.stop_propagation();
+5 -3
View File
@@ -9,7 +9,8 @@ use std::{
use futures::{StreamExt, channel::mpsc, select_biased};
use gpui::Context;
use super::{Geom, LokError, LokView, Reported, TILE_TWIPS, input::Input};
use super::input::{Back, Input};
use super::{Geom, LokError, LokView, Reported, TILE_TWIPS, Turn};
use crate::{Doc, Tile, Worker};
/// An input slower than this is logged as a warning.
@@ -24,7 +25,7 @@ pub(super) struct Job {
/// What came back from the lane.
enum Landed {
/// Sequence, whether it edits, whether it switched part, outcome.
Input(u64, bool, bool, (io::Result<Option<String>>, Option<u64>)),
Input(u64, bool, bool, (io::Result<Option<Back>>, Option<u64>)),
Tile(Job, io::Result<Tile>),
}
@@ -111,7 +112,8 @@ impl LokView {
}
self.seen_revision(now.unwrap_or(0));
match outcome {
Ok(Some(copied)) => self.copied = Some(copied),
Ok(Some(Back::Copied(copied))) => self.copied = Some(copied),
Ok(Some(Back::Turned(step))) => cx.emit(Turn(step)),
Ok(None) => {}
Err(error) => {
log::error!("lok: input failed ({:?})", error.kind());
+113 -45
View File
@@ -5,6 +5,7 @@ use std::time::{Duration, Instant};
use gpui::{Context, EventEmitter, Keystroke, Modifiers, ScrollDelta, ScrollWheelEvent, TouchPhase};
use super::LokView;
use super::input::{Input, vcl_key};
/// Asks for the part `step` away from the shown one.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -14,53 +15,90 @@ impl EventEmitter<Turn> for LokView {}
/// Trackpad pixels one turn takes.
const SWIPE: f32 = 50.0;
/// A pause this long ends a trackpad gesture.
/// A pause this long ends a gesture without phases.
const PAUSE: Duration = Duration::from_millis(250);
/// Lines one wheel notch scrolls on this platform.
pub fn notch() -> f32 {
#[cfg(windows)]
{
use windows_sys::Win32::UI::WindowsAndMessaging::{SPI_GETWHEELSCROLLLINES, SystemParametersInfoW};
let mut lines = 3u32;
// Same setting gpui scales the wheel by.
unsafe { SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, (&raw mut lines).cast(), 0) };
lines as f32
}
#[cfg(target_os = "macos")]
{
1.0
}
// gpui's X11 and Wayland scroll three lines a notch.
#[cfg(not(any(windows, target_os = "macos")))]
{
3.0
}
}
/// The wheel's progress towards a turn.
#[derive(Default)]
pub(super) struct Turning {
lines: f32,
pixels: f32,
/// This gesture's travel, in turns.
travel: f32,
/// This gesture already turned.
spent: bool,
/// The platform began a gesture it has not ended.
held: bool,
/// Fractional lines: a smooth touchpad, not a wheel.
smooth: bool,
last: Option<Instant>,
}
impl Turning {
/// The turn a wheel event makes; 0 for none.
pub(super) fn wheel(&mut self, delta: ScrollDelta, phase: TouchPhase, now: Instant) -> i32 {
pub(super) fn wheel(&mut self, delta: ScrollDelta, phase: TouchPhase, now: Instant, notch: f32) -> i32 {
let paused = self.last.is_none_or(|last| now.duration_since(last) >= PAUSE);
self.last = Some(now);
let ahead = match delta {
// Each notch turns once.
let travel = match delta {
ScrollDelta::Lines(_) if notch <= 0.0 => return 0,
ScrollDelta::Lines(lines) => {
if paused {
self.lines = 0.0;
let notches = lines.y / notch;
let whole = notches.round();
// Whole notches outside a smooth stream: each turns once.
if whole != 0.0 && (notches - whole).abs() < 1e-3 && (paused || !self.smooth) {
(self.travel, self.spent, self.smooth) = (0.0, false, false);
return -(whole as i32);
}
self.lines += lines.y;
let whole = self.lines.trunc();
self.lines -= whole;
whole
if paused || !self.smooth {
(self.travel, self.spent) = (0.0, false);
}
self.smooth = true;
notches
}
// One trackpad gesture turns once, momentum included.
ScrollDelta::Pixels(pixels) => {
if paused || phase == TouchPhase::Started {
(self.pixels, self.spent) = (0.0, false);
self.smooth = false;
// Without phases, only a pause ends a gesture.
if phase == TouchPhase::Started || paused && !self.held {
(self.travel, self.spent) = (0.0, false);
}
if self.spent {
return 0;
}
self.pixels += f32::from(pixels.y);
if self.pixels.abs() < SWIPE {
return 0;
}
self.spent = true;
self.pixels.signum()
self.held = match phase {
TouchPhase::Started => true,
TouchPhase::Ended | TouchPhase::Cancelled => false,
TouchPhase::Moved => self.held,
};
f32::from(pixels.y) / SWIPE
}
};
// One gesture turns once, momentum included.
if self.spent {
return 0;
}
self.travel += travel;
if self.travel.abs() < 1.0 {
return 0;
}
self.spent = true;
// Content moving down shows the part before.
-(ahead as i32)
-(self.travel.signum() as i32)
}
}
@@ -75,10 +113,9 @@ impl LokView {
self.part
}
/// Up and Down turn while nothing is picked or edited.
pub(super) fn turn_key(&mut self, keystroke: &Keystroke, cx: &mut Context<Self>) -> bool {
let idle = !self.caret() && self.graphic.is_none() && self.selection.is_empty();
if self.turning.is_none() || !idle || keystroke.modifiers != Modifiers::none() {
/// Queues Up, Down; the worker turns if nothing is picked.
pub(super) fn turn_key(&mut self, keystroke: &Keystroke) -> bool {
if self.turning.is_none() || keystroke.modifiers != Modifiers::none() {
return false;
}
let step = match keystroke.key.as_str() {
@@ -86,7 +123,10 @@ impl LokView {
"up" => -1,
_ => return false,
};
cx.emit(Turn(step));
let Some((char_code, key_code)) = vcl_key(keystroke) else {
return false;
};
self.send(Input::Turn { char_code, key_code, step });
true
}
@@ -99,7 +139,7 @@ impl LokView {
let Some(turning) = self.turning.as_mut().filter(|_| whole) else {
return false;
};
let step = turning.wheel(event.delta, event.touch_phase, Instant::now());
let step = turning.wheel(event.delta, event.touch_phase, Instant::now(), notch());
if step != 0 {
cx.emit(Turn(step));
}
@@ -113,30 +153,58 @@ mod tests {
use super::{PAUSE, Turning};
const NOTCH: f32 = 3.0;
fn lines(y: f32) -> ScrollDelta {
ScrollDelta::Lines(point(0.0, y))
}
fn pixels(y: f32) -> ScrollDelta {
ScrollDelta::Pixels(point(px(0.0), px(y)))
}
#[test]
fn each_notch_turns_once() {
let (mut turning, now) = (Turning::default(), std::time::Instant::now());
let notch = |y| ScrollDelta::Lines(point(0.0, y));
assert_eq!(turning.wheel(notch(-1.0), TouchPhase::Moved, now), 1);
assert_eq!(turning.wheel(notch(-1.0), TouchPhase::Moved, now), 1);
assert_eq!(turning.wheel(notch(1.0), TouchPhase::Moved, now), -1);
assert_eq!(turning.wheel(notch(0.5), TouchPhase::Moved, now), 0);
assert_eq!(turning.wheel(notch(0.5), TouchPhase::Moved, now), -1);
let mut wheel = |y| turning.wheel(lines(y), TouchPhase::Moved, now, NOTCH);
assert_eq!(wheel(-3.0), 1, "Windows and Linux scroll three lines a notch");
assert_eq!(wheel(-3.0), 1);
assert_eq!(wheel(3.0), -1);
assert_eq!(wheel(-6.0), 2, "two notches in one event");
let mut mac = Turning::default();
assert_eq!(mac.wheel(lines(-1.0), TouchPhase::Moved, now, 1.0), 1);
}
#[test]
fn smooth_lines_turn_once_a_gesture() {
let (mut turning, now) = (Turning::default(), std::time::Instant::now());
// An X11 or Windows touchpad: fractions, no phases.
let turned: i32 = (0..30).map(|_| turning.wheel(lines(-0.4), TouchPhase::Moved, now, NOTCH)).sum();
assert_eq!(turned, 1, "one swipe turns once");
assert_eq!(turning.wheel(lines(-3.0), TouchPhase::Moved, now, NOTCH), 0, "a whole step mid-swipe");
let later = now + PAUSE;
assert_eq!(turning.wheel(lines(-3.0), TouchPhase::Moved, later, NOTCH), 1, "a pause ends the swipe");
}
#[test]
fn a_swipe_turns_once_until_the_next() {
let (mut turning, now) = (Turning::default(), std::time::Instant::now());
let swipe = |y| ScrollDelta::Pixels(point(px(0.0), px(y)));
assert_eq!(turning.wheel(swipe(-30.0), TouchPhase::Started, now), 0);
assert_eq!(turning.wheel(swipe(-30.0), TouchPhase::Moved, now), 1);
let mut swipe = |y, phase, at| turning.wheel(pixels(y), phase, at, NOTCH);
assert_eq!(swipe(-30.0, TouchPhase::Started, now), 0);
assert_eq!(swipe(-30.0, TouchPhase::Moved, now), 1);
// A pause with fingers down is the same gesture.
assert_eq!(swipe(-60.0, TouchPhase::Moved, now + PAUSE * 2), 0);
assert_eq!(swipe(0.0, TouchPhase::Ended, now + PAUSE * 2), 0);
// Momentum after the turn turns nothing more.
for _ in 0..20 {
assert_eq!(turning.wheel(swipe(-80.0), TouchPhase::Moved, now), 0);
assert_eq!(swipe(-80.0, TouchPhase::Moved, now + PAUSE * 2), 0);
}
assert_eq!(turning.wheel(swipe(60.0), TouchPhase::Started, now), -1);
// A pause ends a gesture without phases.
let later = now + PAUSE;
assert_eq!(turning.wheel(swipe(60.0), TouchPhase::Moved, later), -1);
assert_eq!(swipe(60.0, TouchPhase::Started, now + PAUSE * 2), -1);
assert_eq!(swipe(0.0, TouchPhase::Ended, now + PAUSE * 2), 0);
// Without phases a pause ends a gesture (Wayland).
let later = now + PAUSE * 4;
assert_eq!(swipe(60.0, TouchPhase::Moved, later), -1);
assert_eq!(swipe(60.0, TouchPhase::Moved, later), 0);
assert_eq!(swipe(60.0, TouchPhase::Moved, later + PAUSE), -1);
}
}
+1
View File
@@ -218,6 +218,7 @@ fn answer(lok: &Lok, tag: u8, id: u64, payload: &[u8]) {
}
}),
tag::POST_KEY => input::key(unpack),
tag::TURN_KEY => input::turn_key(unpack),
tag::POST_MOUSE => input::mouse(unpack),
tag::GRAPHIC => input::graphic(unpack),
tag::POST_TEXT => input::text(unpack),
+14
View File
@@ -99,6 +99,20 @@ pub(super) fn key(unpack: &mut Unpack) -> io::Result<Vec<u8>> {
Ok(Vec::new())
}
/// Up or Down after earlier input: 1 if it turns.
pub(super) fn turn_key(unpack: &mut Unpack) -> io::Result<Vec<u8>> {
let (id, char_code, key_code) = (unpack.u64()?, unpack.i64()?, unpack.i64()?);
// Earlier input's callbacks already landed here.
if !crate::events::picked(id) {
return Ok(Pack::new().u64(1).bytes());
}
with_doc(id, |doc| {
doc.post_key(0, char_code as i32, key_code as i32);
doc.post_key(1, char_code as i32, key_code as i32);
})?;
Ok(Pack::new().u64(0).bytes())
}
/// One mouse event: kind, x, y, count, buttons, modifiers.
pub(super) fn mouse(unpack: &mut Unpack) -> io::Result<Vec<u8>> {
let id = unpack.u64()?;
+1 -1
View File
@@ -51,7 +51,7 @@ pub fn expect(unpack: &mut Unpack) -> io::Result<Vec<u8>> {
pub fn bump(request: u8, payload: &[u8]) {
let changes = matches!(
request,
tag::POST_KEY | tag::POST_MOUSE | tag::GRAPHIC | tag::POST_TEXT | tag::POST_UNO | tag::PASTE | tag::CUT | tag::COMPLETE_FUNCTION
tag::POST_KEY | tag::TURN_KEY | tag::POST_MOUSE | tag::GRAPHIC | tag::POST_TEXT | tag::POST_UNO | tag::PASTE | tag::CUT | tag::COMPLETE_FUNCTION
);
let mut unpack = Unpack::new(payload);
let Ok(id) = unpack.u64() else {