tabs: a strip with wheel_selects adds the wheel scroll short of a notch to the next pass, so small trackpad deltas switch tabs

This commit is contained in:
Dylan M. Taylor
2026-11-24 09:56:03 -05:00
parent 7762aab37b
commit 36106c2b93
2 changed files with 56 additions and 10 deletions
+40 -10
View File
@@ -21,7 +21,8 @@ use crate::buttons::IconButton;
use crate::contract::{
Access, Animate as _, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, Common, Component, ContainerProps,
ContextMenuRequest, Cx, DropKind, DropMark, Entry, ListDrag, Meta, Name, NoValue, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx,
Pointer as _, PopupKind, Props as _, Role, RowState, SelectMode, ShowContainer as _, ShowShape, Sizing, WindowDropTarget, WordSetCx as _, spec,
Pointer as _, PopupKind, Props as _, Remember as _, Role, RowState, SelectMode, ShowContainer as _, ShowShape, Sizing, WindowDropTarget,
WordSetCx as _, spec,
};
use crate::list::{DropTest, RowTest};
use crate::menu::Menu;
@@ -186,6 +187,19 @@ enum Lane {
Ghost(usize),
}
/// The wheel scroll a strip has taken but not yet turned into a step, and the tab the strip left shown.
#[derive(Clone)]
struct WheelCarry<K> {
owed: f32,
left: Option<K>,
}
impl<K> Default for WheelCarry<K> {
fn default() -> Self {
Self { owed: 0.0, left: None }
}
}
/// What the fit reads besides the tabs' widths: the shown tab, the kind's rule, the tabs kept ahead of the others and
/// the end items.
#[derive(Clone, Debug)]
@@ -629,24 +643,40 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> TabStri
}
/// With `wheel_selects`, the wheel's whole notches step the shown tab through the tabs in the source's order:
/// down to later tabs, up to earlier ones.
/// down to later tabs, up to earlier ones. The scroll short of a notch is kept for the next pass, so a trackpad's
/// small deltas add up; it is dropped when the scroll reverses, when the strip stops at an end, and when the
/// shown tab is not the one the last step left.
fn wheel_step(&mut self, cx: &mut Cx<'_, Self>) {
if !self.wheel_selects {
return;
}
let Some(wheel) = cx.take_wheel().filter(|wheel| !wheel.mods.cmd && !wheel.mods.ctrl) else { return };
let notch = self.items.wheel_notch(cx.style.line).points(cx.rect.height());
let steps = if notch > 0.0 { (wheel.scroll.y / notch).trunc() } else { 0.0 };
if steps == 0.0 || !steps.is_finite() {
if !notch.is_finite() || notch <= 0.0 || !wheel.scroll.y.is_finite() {
return;
}
let texts = self.texts.borrow();
let Some(at) = self.shown.as_ref().and_then(|shown| texts.iter().position(|(key, _)| key == shown)) else { return };
let last = texts.len().saturating_sub(1) as i64;
let to = (at as i64 - steps as i64).clamp(0, last) as usize;
if to != at {
self.wheeled = Some(texts[to].0.clone());
let mut carry = cx.memory::<WheelCarry<K>>().clone();
if carry.left != self.shown || carry.owed * wheel.scroll.y < 0.0 {
carry.owed = 0.0;
}
carry.owed += wheel.scroll.y;
let steps = (carry.owed / notch).trunc();
carry.owed -= steps * notch;
let texts = self.texts.borrow();
let at = self.shown.as_ref().and_then(|shown| texts.iter().position(|(key, _)| key == shown));
if let Some(at) = at
&& steps != 0.0
{
let last = texts.len().saturating_sub(1) as i64;
let to = (at as i64 - steps as i64).clamp(0, last) as usize;
if to == at {
carry.owed = 0.0;
} else {
self.wheeled = Some(texts[to].0.clone());
}
}
carry.left = self.wheeled.clone().or_else(|| self.shown.clone());
*cx.memory::<WheelCarry<K>>() = carry;
}
fn report(&self) -> Report {
+16
View File
@@ -270,6 +270,22 @@ fn the_wheel_steps_the_shown_tab_only_where_the_strip_asks_for_it() {
}
}
/// A notch of wheel scroll that arrives in four quarter-notch passes shows one tab step, as a whole notch in one pass
/// does: a trackpad's small deltas add up instead of being dropped.
#[trial]
fn small_wheel_deltas_add_up_to_one_tab_step() {
use craft_ui::testing::pointer;
for parts in [4_u32, 1] {
let (mut fixture, shown) = wheel_rig(true);
let over = fixture.by_label("Two").expect("the tab's node").center();
fixture.frame(vec![pointer::moved(over)]).idle(1);
for _ in 0..parts {
fixture.frame(vec![pointer::wheel_lines(-1.0 / parts as f32)]).idle(2);
}
assert_eq!(shown.get(), 1, "a notch in {parts} passes shows exactly the next tab");
}
}
/// A double click on the empty room after the last document tab asks for a new document; a double click on a tab
/// activates it and asks for none.
#[trial]