R6.13 round 2 fix: the worker turns in input order; scrolls by gesture

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:35:43 -04:00
1 parent d427d05b96
commit 407ce00ab9
17 files changed
+91 -109

No files matched your search

+6 -5
View File
@@ -237,12 +237,13 @@ 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> {
/// Turns `step` parts, or posts `key` if something is picked.
pub fn turn(&mut self, doc: &Doc, step: i32, key: Option<(u32, i32)>) -> io::Result<Option<usize>> {
let (char_code, key_code) = key.unwrap_or_default();
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)
let payload = Pack::new().u64(doc_id).i64(step.into()).i64(char_code.into()).i64(key_code.into()).bytes();
let shown = Unpack::new(&self.request(tag::TURN, &payload)?).i64()?;
Ok(usize::try_from(shown).ok())
}
/// Drags a selected object's handle, start then end.
+2 -2
View File
@@ -88,8 +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;
/// Turns parts; an arrow with something picked goes in.
pub const TURN: 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, notch,
CONTEXT, Copy, Cut, Geom, Layout, LokError, LokView, Paste, Reported, 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, notch};
pub use turn::notch;
pub(crate) use crate::{TILE_TWIPS, TWIPS_PER_PIXEL};
+4 -11
View File
@@ -64,18 +64,14 @@ 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,
},
/// Turns `step` parts; a picked object takes the arrow `key`.
Turn { step: i32, key: Option<(u32, i32)> },
}
/// What an input gives back.
pub(super) enum Back {
Copied(String),
Turned(i32),
Shown(usize),
}
impl Input {
@@ -200,10 +196,7 @@ fn apply(worker: &mut Worker, doc: &Doc, input: Input) -> io::Result<Option<Back
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::Turn { step, key } => return Ok(worker.turn(doc, step, key)?.map(Back::Shown)),
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),
+7 -2
View File
@@ -10,7 +10,7 @@ use futures::{StreamExt, channel::mpsc, select_biased};
use gpui::Context;
use super::input::{Back, Input};
use super::{Geom, LokError, LokView, Reported, TILE_TWIPS, Turn};
use super::{Geom, LokError, LokView, Reported, TILE_TWIPS};
use crate::{Doc, Tile, Worker};
/// An input slower than this is logged as a warning.
@@ -113,7 +113,12 @@ impl LokView {
self.seen_revision(now.unwrap_or(0));
match outcome {
Ok(Some(Back::Copied(copied))) => self.copied = Some(copied),
Ok(Some(Back::Turned(step))) => cx.emit(Turn(step)),
Ok(Some(Back::Shown(part))) if part != self.part => {
// The worker turned in order.
self.part = part;
self.invalidate(cx);
}
Ok(Some(Back::Shown(_))) => {}
Ok(None) => {}
Err(error) => {
log::error!("lok: input failed ({:?})", error.kind());
+37 -56
View File
@@ -2,21 +2,15 @@
use std::time::{Duration, Instant};
use gpui::{Context, EventEmitter, Keystroke, Modifiers, ScrollDelta, ScrollWheelEvent, TouchPhase};
use gpui::{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)]
pub struct Turn(pub i32);
impl EventEmitter<Turn> for LokView {}
/// Trackpad pixels one turn takes.
const SWIPE: f32 = 50.0;
/// A pause this long ends a gesture without phases.
const PAUSE: Duration = Duration::from_millis(250);
const PAUSE: Duration = Duration::from_millis(200);
/// Lines one wheel notch scrolls on this platform.
pub fn notch() -> f32 {
@@ -48,8 +42,6 @@ pub(super) struct Turning {
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>,
}
@@ -58,42 +50,27 @@ impl Turning {
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 travel = match delta {
// X11 lines mix wheels and touchpads; they carry no phase.
let (travel, phase) = match delta {
ScrollDelta::Lines(_) if notch <= 0.0 => return 0,
ScrollDelta::Lines(lines) => {
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);
}
if paused || !self.smooth {
(self.travel, self.spent) = (0.0, false);
}
self.smooth = true;
notches
}
ScrollDelta::Pixels(pixels) => {
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);
}
self.held = match phase {
TouchPhase::Started => true,
TouchPhase::Ended | TouchPhase::Cancelled => false,
TouchPhase::Moved => self.held,
};
f32::from(pixels.y) / SWIPE
}
ScrollDelta::Lines(lines) => (lines.y / notch, None),
ScrollDelta::Pixels(pixels) => (f32::from(pixels.y) / SWIPE, Some(phase)),
};
// Phases bound a gesture; without them, a pause does.
if phase == Some(TouchPhase::Started) || paused && !self.held {
(self.travel, self.spent) = (0.0, false);
}
self.held = match phase {
Some(TouchPhase::Started) => true,
Some(TouchPhase::Moved) => self.held,
Some(TouchPhase::Ended | TouchPhase::Cancelled) | None => false,
};
// One gesture turns once, momentum included.
if self.spent {
return 0;
}
self.travel += travel;
if self.travel.abs() < 1.0 {
if self.travel.abs() < 1.0 - 1e-3 {
return 0;
}
self.spent = true;
@@ -123,15 +100,15 @@ impl LokView {
"up" => -1,
_ => return false,
};
let Some((char_code, key_code)) = vcl_key(keystroke) else {
let Some(key) = vcl_key(keystroke) else {
return false;
};
self.send(Input::Turn { char_code, key_code, step });
self.send(Input::Turn { step, key: Some(key) });
true
}
/// The wheel turns while the whole page shows.
pub(super) fn turn_wheel(&mut self, event: &ScrollWheelEvent, cx: &mut Context<Self>) -> bool {
pub(super) fn turn_wheel(&mut self, event: &ScrollWheelEvent) -> bool {
let (width, height) = self.extent();
// A pixel of slack for rounding.
let whole = self.geom.px_of(width) <= self.viewport.0 + 1.0
@@ -141,7 +118,8 @@ impl LokView {
};
let step = turning.wheel(event.delta, event.touch_phase, Instant::now(), notch());
if step != 0 {
cx.emit(Turn(step));
// In order with the input around it.
self.send(Input::Turn { step, key: None });
}
true
}
@@ -164,26 +142,29 @@ mod tests {
}
#[test]
fn each_notch_turns_once() {
fn each_notch_with_a_pause_turns_once() {
let (mut turning, now) = (Turning::default(), std::time::Instant::now());
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 wheel = |y, at| turning.wheel(lines(y), TouchPhase::Moved, at, NOTCH);
assert_eq!(wheel(-3.0, now), 1, "Windows and Linux scroll three lines a notch");
assert_eq!(wheel(-3.0, now + PAUSE), 1);
assert_eq!(wheel(3.0, now + PAUSE * 2), -1);
assert_eq!(wheel(-6.0, now + PAUSE * 3), 1, "two notches at once turn once");
assert_eq!(wheel(-1.0, now + PAUSE * 4), 0, "a third of a notch");
let mut mac = Turning::default();
assert_eq!(mac.wheel(lines(-1.0), TouchPhase::Moved, now, 1.0), 1);
assert_eq!(mac.wheel(lines(-1.0), TouchPhase::Moved, now, 1.0), 1, "macOS scrolls a line a notch");
}
#[test]
fn smooth_lines_turn_once_a_gesture() {
fn a_stream_of_lines_turns_once() {
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();
// X11 smooth scrolling: whole steps, then fractions, no pause.
let stream = [-3.0, -3.0, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4];
let turned: i32 = stream.iter().map(|&y| turning.wheel(lines(y), 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");
let mut turning = Turning::default();
let turned: i32 = (0..30).map(|_| turning.wheel(lines(-0.4), TouchPhase::Moved, now, NOTCH)).sum();
assert_eq!(turned, 1, "fractions add up once");
assert_eq!(turning.wheel(lines(-3.0), TouchPhase::Moved, now + PAUSE, NOTCH), 1, "a pause ends it");
}
#[test]
+1 -1
View File
@@ -67,7 +67,7 @@ impl LokView {
/// Ctrl (macOS: Cmd) wheel zooms; else it turns or scrolls.
pub(super) fn on_wheel(&mut self, event: &ScrollWheelEvent, window: &mut Window, cx: &mut Context<Self>) {
if !self.wheel_zoom(event, cx) && !self.turn_wheel(event, cx) {
if !self.wheel_zoom(event, cx) && !self.turn_wheel(event) {
self.on_scroll(event, window, cx);
}
}
+1 -1
View File
@@ -218,7 +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::TURN => input::turn(unpack),
tag::POST_MOUSE => input::mouse(unpack),
tag::GRAPHIC => input::graphic(unpack),
tag::POST_TEXT => input::text(unpack),
+17 -9
View File
@@ -99,18 +99,26 @@ 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()?);
/// Turns `step` parts in order, or posts the arrow (-1).
pub(super) fn turn(unpack: &mut Unpack) -> io::Result<Vec<u8>> {
let (id, step, char_code, key_code) = (unpack.u64()?, unpack.i64()?, unpack.i64()?, unpack.i64()?);
// Earlier input's callbacks already landed here.
if !crate::events::picked(id) {
return Ok(Pack::new().u64(1).bytes());
if key_code != 0 && crate::events::picked(id) {
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);
})?;
return Ok(Pack::new().i64(-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);
let shown = with_doc(id, |doc| {
let (part, last) = (i64::from(doc.part()), i64::from(doc.parts()) - 1);
let next = (part + step).clamp(0, last.max(0));
if next != part {
doc.set_part(next as i32);
}
next
})?;
Ok(Pack::new().u64(0).bytes())
Ok(Pack::new().i64(shown).bytes())
}
/// One mouse event: kind, x, y, count, buttons, modifiers.
+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::TURN_KEY | tag::POST_MOUSE | tag::GRAPHIC | tag::POST_TEXT | tag::POST_UNO | tag::PASTE | tag::CUT | tag::COMPLETE_FUNCTION
tag::POST_KEY | tag::TURN | 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 {