Launcher: a thumb scrolls the app list
Deploy to the handset / build-and-deploy (push) Successful in 2m21s
CI / test (push) Failing after 2m49s

On a touchscreen a drag over the launcher's list scrolls it by pixels, a flick
keeps it going, and a tap still launches (or stops a moving list).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-29 22:40:32 -08:00
1 parent cb0ff0715e
commit 7029c92e6e
8 files changed
+313 -32

No files matched your search

+77 -8
View File
@@ -8,7 +8,7 @@
///
/// A catalog too long for the popup gets a category column on its left ("All", then the catalog's
/// own categories: "Chrome Apps", "Games", ...) and a scrollbar; the wheel arrives as the keys
/// "ScrollUp" and "ScrollDown", one row each.
/// "ScrollUp" and "ScrollDown", one row each, and a finger as "ScrollPx:N", N pixels (+: down).
///| Which surface an event is addressed to (mirrors the WIT `surface` enum).
pub(all) enum Target {
@@ -30,6 +30,8 @@ pub struct State {
mut category : String
/// How many listed apps are scrolled off above the first row.
mut scroll : Int
/// ... and how many pixels of the first row (a finger scrolls by pixels).
mut offset : Int
/// Category row under the pointer (-1: none).
mut hover_category : Int
}
@@ -45,6 +47,7 @@ pub fn State::new() -> State {
armed: -1,
category: "",
scroll: 0,
offset: 0,
hover_category: -1,
}
}
@@ -212,6 +215,11 @@ fn first_row(n : Int, st : State) -> Int {
if st.scroll > m { m } else { st.scroll }
}
///| How many pixels of the first row are scrolled off (none at the end of the list).
fn first_part(n : Int, st : State) -> Int {
if st.scroll < max_scroll(n) { st.offset } else { 0 }
}
///| Scroll the list by `rows` (+: down). True when it moved.
fn scroll_by(apps : Array[@types.AppEntry], st : State, rows : Int) -> Bool {
let n = listed(apps, st).length()
@@ -219,19 +227,35 @@ fn scroll_by(apps : Array[@types.AppEntry], st : State, rows : Int) -> Bool {
let to = from + rows
let m = max_scroll(n)
st.scroll = if to < 0 { 0 } else if to > m { m } else { to }
if st.scroll == from {
let part = st.offset
st.offset = 0
if st.scroll == from && part == 0 {
return false
}
st.hover = -1
true
}
///| The app rows the popup draws now, at most `max_rows`. The shell publishes icons for exactly
///| Scroll the list by `px` pixels (+: down), stopping at its ends. True when it moved.
fn scroll_px(apps : Array[@types.AppEntry], st : State, px : Int) -> Bool {
let n = listed(apps, st).length()
let from = first_row(n, st) * row_h + first_part(n, st)
let end = max_scroll(n) * row_h
let to = if from + px < 0 { 0 } else if from + px > end { end } else { from + px }
st.scroll = to / row_h
st.offset = to % row_h
st.hover = -1
to != from
}
///| The app rows the popup draws now: `max_rows` at most, and one more when the first is partly
/// scrolled off. The shell publishes icons for exactly
/// these while the menu is open.
pub fn shown_rows(apps : Array[@types.AppEntry], st : State) -> Array[@types.AppEntry] {
let all = listed(apps, st)
let first = first_row(all.length(), st)
all[first:first + visible_rows(all.length())].to_owned()
let last = first + visible_rows(all.length()) + (if first_part(all.length(), st) > 0 { 1 } else { 0 })
all[first:if last > all.length() { all.length() } else { last }].to_owned()
}
///| Category row under a popup-local point, or -1.
@@ -262,7 +286,7 @@ fn scroll_thumb(n : Int, st : State) -> (Int, Int)? {
}
let track = max_rows * row_h - 8
let th = if track * max_rows / n < 24 { 24 } else { track * max_rows / n }
Some((header_h + 4 + (track - th) * first_row(n, st) / max_scroll(n), th))
Some((header_h + 4 + (track - th) * (first_row(n, st) * row_h + first_part(n, st)) / (max_scroll(n) * row_h), th))
}
///| Side of an app's icon in its row.
@@ -314,6 +338,7 @@ pub fn close(st : State) -> Unit {
st.armed = -1
st.category = ""
st.scroll = 0
st.offset = 0
st.hover_category = -1
}
@@ -404,9 +429,13 @@ pub fn render_popup(
}
let thumb = scroll_thumb(total, st)
let bar = if thumb is Some(_) { scrollbar_w - 6 } else { 0 }
let part = first_part(total, st)
if part > 0 {
ops.push(@types.DrawOp::PushClip(@ui.rect(lx, header_h, w - lx, max_rows * row_h)))
}
for i = 0; i < shown.length(); i = i + 1 {
let a = shown[i]
let y = header_h + i * row_h
let y = header_h + i * row_h - part
let hot = first + i == st.hover
if hot {
ops.push(@ui.fill_rect(@ui.rect(lx + 6, y, w - lx - 12 - bar, row_h - 2), p.accent, radius=r))
@@ -427,6 +456,9 @@ pub fn render_popup(
let about = if a.comment.length() > 0 { a.comment } else { a.category }
ops.push(@ui.text(lx + 56, y + 31, about, fs - 3, sub))
}
if part > 0 {
ops.push(@types.DrawOp::PopClip)
}
match thumb {
Some((ty, th)) => {
ops.push(@ui.fill_rect(@ui.rect(w - 11, header_h + 4, 5, max_rows * row_h - 8), p.surface, radius=2))
@@ -470,8 +502,10 @@ pub fn row_at(apps : Array[@types.AppEntry], st : State, p : @types.Point) -> In
if p.x < lx + 6 || p.x >= right || p.y < header_h {
return -1
}
let i = (p.y - header_h) / row_h
if i < visible_rows(n) { first_row(n, st) + i } else { -1 }
if p.y >= header_h + visible_rows(n) * row_h {
return -1
}
first_row(n, st) + (p.y - header_h + first_part(n, st)) / row_h
}
///| A press at a popup-local point on the scrollbar pages the list. False when it isn't on it.
@@ -545,6 +579,7 @@ pub fn handle_event(
st.category = if c == 0 { "" } else { categories(apps)[c] }
st.query = ""
st.scroll = 0
st.offset = 0
st.hover = -1
return true
}
@@ -576,11 +611,15 @@ pub fn handle_event(
}
st.hover = -1
st.scroll = 0
st.offset = 0
true
} else if k == "ScrollUp" {
scroll_by(apps, st, -1)
} else if k == "ScrollDown" {
scroll_by(apps, st, 1)
} else if k.has_prefix("ScrollPx:") {
let px = @string.parse_int(k[9:]) catch { _ => 0 }
scroll_px(apps, st, px)
} else if k == "Enter" {
if listed(apps, st).length() > 0 {
activate(apps, st, 0, launch)
@@ -590,6 +629,7 @@ pub fn handle_event(
st.query = st.query + k
st.hover = -1
st.scroll = 0
st.offset = 0
true
} else {
false
@@ -924,6 +964,35 @@ test "the list scrolls with the wheel and the scrollbar" {
assert_eq(st.scroll, 0)
}
///|
test "a finger scrolls the list by pixels" {
let apps = long_catalog()
let st = State::new()
st.open = true
let send = fn(k : String) { handle_event(apps, st, Popup, KeyDown(k), _ => Ok(())) }
assert_false(send("ScrollPx:-5"))
assert_true(send("ScrollPx:" + (row_h + 10).to_string()))
assert_eq((st.scroll, st.offset), (1, 10))
assert_eq(shown_rows(apps, st).length(), max_rows + 1)
// Rows are hit where they are drawn, and not below the list.
let x = sidebar_w + 20
assert_eq(row_at(apps, st, { x, y: header_h }), 1)
assert_eq(row_at(apps, st, { x, y: header_h + row_h - 10 }), 2)
assert_eq(row_at(apps, st, { x, y: header_h + max_rows * row_h }), -1)
let ops = render_popup(apps, test_tokens(), st)
assert_true(ops.iter().any(o => o is PushClip(_)) && ops.iter().any(o => o is PopClip))
// To the end, and no further.
assert_true(send("ScrollPx:100000"))
assert_eq((shown_rows(apps, st).last().unwrap().id, st.offset), ("app19", 0))
assert_false(send("ScrollPx:1"))
// The wheel goes back to whole rows; nonsense does nothing.
ignore(send("ScrollPx:-7"))
assert_true(st.offset > 0)
ignore(send("ScrollDown"))
assert_eq(st.offset, 0)
assert_false(send("ScrollPx:x"))
}
///|
fn test_tokens() -> @theme.Tokens {
{
+1
View File
@@ -1,4 +1,5 @@
import {
"moonbitlang/core/string",
"chandra/desktop/interface/chandra/desktop/types" @types,
"chandra/desktop/interface/chandra/desktop/theme" @theme,
"chandra/desktop/lib/ui" @ui,
+1
View File
@@ -57,6 +57,7 @@ pub struct State {
mut armed : Int32
mut category : String
mut scroll : Int32
mut offset : Int32
mut hover_category : Int32
}
pub fn State::new() -> Self