From d32036cb8c5d10155835219613245f68ef08ed1e Mon Sep 17 00:00:00 2001 From: haranishi <223786623+haranishi@users.noreply.github.com> Date: Fri, 20 Nov 2026 03:07:58 +0900 Subject: [PATCH] Add document chrome to canvas scrollbars --- crates/behavior/src/scroll.rs | 3 +- crates/ui-canvas/src/canvas/scrollbars/mod.rs | 204 ++++++++++++++++-- crates/ui-canvas/src/canvas/words.rs | 4 + .../ui-canvas/tests/it/canvas/scrollbars.rs | 92 +++++++- 4 files changed, 277 insertions(+), 26 deletions(-) diff --git a/crates/behavior/src/scroll.rs b/crates/behavior/src/scroll.rs index 1103a0016..723ae7042 100644 --- a/crates/behavior/src/scroll.rs +++ b/crates/behavior/src/scroll.rs @@ -81,7 +81,8 @@ pub fn jump(span: &ScrollSpan, mv: NavMove) -> Option> { }) } -fn shifted(span: &ScrollSpan, shift: f64) -> Range { +/// Moves the visible range by `shift` content units, keeping it within the scrollable range. +pub fn shifted(span: &ScrollSpan, shift: f64) -> Range { let visible = span.visible.end - span.visible.start; let last = (span.content.end - visible).max(span.content.start); let start = (span.visible.start + shift).clamp(span.content.start, last); diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index 021b78a5b..b219010ba 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -1,6 +1,7 @@ //! `CanvasScrollbars`: the scroll bars of a canvas (20.6): a horizontal bar along the bottom of the view and a vertical //! one along the right, always shown, in the docked look (`ScrollBarKind::Docked`): the thumb has the track's whole -//! thickness at rest and under the pointer. The thumb is the visible fraction of +//! thickness at rest and under the pointer. Document chrome uses inset thumbs, step buttons and a corner. +//! The thumb is the visible fraction of //! the scrollable extent (the document, plus wherever the view has been scrolled past it); drag it to scroll, or press //! the track to page by one screenful. With overscroll on, a document smaller than the window can also be scrolled from //! one edge of the window to the other. With overscroll off the app clamps the view before it draws @@ -15,20 +16,29 @@ mod stories; use super::words::CanvasWords; use crate::contract::{ Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, Name, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, - Role, THUMB, WordSetCx as _, + Remember as _, Role, THUMB, WordSetCx as _, }; +use crate::primitives::{Draw, PaintShape, ScrollBar, StrokeWidth}; use crate::runtime::access::Numeric; +use crate::runtime::commands::FireSource; use crate::runtime::env::Label; use crate::runtime::focus::KeyUse; -use crate::style::{ComponentStyle, ScrollBarKind, ScrollBarLook, StyleCx}; +use crate::runtime::frame; +use crate::style::{ComponentStyle, ScrollBarKind, ScrollBarLook, Stateful, StyleColor, StyleCx}; use craft_behavior::kurbo::{Point, Rect as DocRect}; use craft_behavior::scroll::ScrollSpan; -use craft_behavior::{Axis, CanvasView, Metrics, PartKey, PartRef, PressPhase}; -use egui::{Pos2, Rect, Response, Vec2}; +use craft_behavior::{Axis, CanvasView, Metrics, PartKey, PartRef, PressPhase, Sense}; +use egui::{CornerRadius, Rect, Response, Vec2, pos2, vec2}; use std::ops::{Deref, Range}; const HORIZONTAL: PartKey = PartKey::new("horizontal"); const VERTICAL: PartKey = PartKey::new("vertical"); +const START_ARROW: u64 = 1; +const END_ARROW: u64 = 2; +/// Distance of a line step in device pixels, like the canvas's scroll wheel. +const ARROW_STEP_PIXELS: f64 = 16.0; +const ARROW_HOLD_DELAY: f64 = 0.4; +const ARROW_HOLD_INTERVAL: f64 = 0.06; /// The thumb part that `PartsCx::scroll` declares for the bar `bar`. fn thumb_of(bar: PartKey) -> PartRef { @@ -42,6 +52,13 @@ pub struct ScrollbarsStyle { #[style(metrics)] metrics: Metrics, look: ScrollBarLook, + document: Option, +} + +#[derive(Clone, Debug, PartialEq)] +struct DocumentLook { + corner: StyleColor, + arrow: Stateful, } /// One axis of the view, in document units counted from the document's start on that axis. @@ -82,13 +99,96 @@ impl Span { /// One bar: where it lies and what it scrolls. struct Bar { rect: Rect, + track: Rect, span: ScrollSpan, } +impl Bar { + fn new(rect: Rect, axis: Axis, span: ScrollSpan, document_chrome: bool) -> Self { + let mut track = rect; + if document_chrome { + let cap = if axis == Axis::X { rect.height() } else { rect.width() }; + let length = if axis == Axis::X { rect.width() } else { rect.height() }; + if length > 2.0 * cap && cap > 0.0 { + let inset = if axis == Axis::X { vec2(cap, 0.0) } else { vec2(0.0, cap) }; + track = rect.shrink2(inset); + } + } + Self { rect, track, span } + } + + fn arrows(&self, key: PartKey, axis: Axis) -> impl Iterator { + (self.track != self.rect) + .then(|| { + let (before, after) = if axis == Axis::X { + (pos2(self.track.left(), self.rect.bottom()), pos2(self.track.right(), self.rect.top())) + } else { + (pos2(self.rect.right(), self.track.top()), pos2(self.rect.left(), self.track.bottom())) + }; + [ + (key.at(START_ARROW), Rect::from_min_max(self.rect.min, before), false), + (key.at(END_ARROW), Rect::from_min_max(after, self.rect.max), true), + ] + }) + .into_iter() + .flatten() + } +} + /// The bars of a view over a document, in the order horizontal, vertical. struct Bars { horizontal: Option, vertical: Option, + corner: Option, +} + +#[derive(Clone, Default)] +struct ArrowRepeat { + part: Option, + next: f64, +} + +fn arrow_pressed(cx: &mut Cx<'_, CanvasScrollbars<'_>>, part: PartRef, rect: Rect) -> bool { + if matches!(cx.part_activated(part), Some(FireSource::Access | FireSource::Key)) { + return true; + } + let phase = cx.part(part).press.phase; + if !matches!(phase, PressPhase::Down { .. } | PressPhase::Held { .. }) { + return false; + } + let ctx = cx.response.ctx.clone(); + let now = frame::clock(&ctx); + match phase { + PressPhase::Down { .. } => { + *cx.memory::() = ArrowRepeat { part: Some(part), next: now + ARROW_HOLD_DELAY }; + frame::request_repaint_after(&ctx, ARROW_HOLD_DELAY); + true + } + PressPhase::Held { pos, .. } if rect.contains(pos) => { + let repeat = cx.memory::(); + if repeat.part != Some(part) { + return false; + } + let due = now >= repeat.next; + if due { + repeat.next = now + ARROW_HOLD_INTERVAL; + } + frame::request_repaint_after(&ctx, repeat.next - now); + due + } + _ => false, + } +} + +fn paint_arrow(draw: &Draw<'_>, rect: Rect, axis: Axis, after: bool, color: StyleColor) { + let size = rect.width().min(rect.height()) / 4.0; + let sign = if after { 1.0 } else { -1.0 }; + let point = |along, across| { + let along = along * sign; + rect.center() + if axis == Axis::X { vec2(along, across) } else { vec2(across, along) } + }; + let path = PaintShape::Path(vec![point(-size * 0.5, -size), point(size * 0.5, 0.0), point(-size * 0.5, size)]); + draw.stroke(&path, StrokeWidth::Thin, color); } /// How the document's x axis runs on screen: with the view flipped, backwards. @@ -102,16 +202,23 @@ fn aspect(view: &CanvasView) -> f64 { impl Bars { /// The bars inside `rect` (bottom and right edges), `width` thick, for `view` over the document `doc`. - fn of(view: &CanvasView, doc: DocRect, overscroll: bool, horizontal: bool, rect: Rect, width: f32) -> Bars { + fn of(view: &CanvasView, doc: DocRect, overscroll: bool, horizontal: bool, rect: Rect, style: &ScrollbarsStyle) -> Bars { + let document_chrome = style.document.is_some(); + let mut width = style.look.thickness; + if document_chrome { + width = width.min(rect.width().max(0.0)).min(rect.height().max(0.0)); + } let x = Span::of(doc.width(), along_x(view, doc.width(), view.center.x - doc.x0), f64::from(view.rect.width()) / aspect(view), view.zoom) .filter(|_| horizontal); let y = Span::of(doc.height(), view.center.y - doc.y0, f64::from(view.rect.height()), view.zoom); let right = if y.is_some() { rect.right() - width } else { rect.right() }; let bottom = if x.is_some() { rect.bottom() - width } else { rect.bottom() }; - let bar = |span: Option, rect: Rect| span.map(|span| Bar { rect, span: span.scroll(overscroll) }); + let corner = (document_chrome && x.is_some() && y.is_some()).then(|| Rect::from_min_max(pos2(right, bottom), rect.max)); + let bar = |span: Option, axis, rect| span.map(|span| Bar::new(rect, axis, span.scroll(overscroll), document_chrome)); Bars { - horizontal: bar(x, Rect::from_min_max(Pos2::new(rect.left(), bottom), Pos2::new(right, rect.bottom()))), - vertical: bar(y, Rect::from_min_max(Pos2::new(right, rect.top()), Pos2::new(rect.right(), bottom))), + horizontal: bar(x, Axis::X, Rect::from_min_max(pos2(rect.left(), bottom), pos2(right, rect.bottom()))), + vertical: bar(y, Axis::Y, Rect::from_min_max(pos2(right, rect.top()), pos2(rect.right(), bottom))), + corner, } } @@ -176,12 +283,16 @@ pub struct CanvasScrollbars<'a> { /// horizontal bar or corner is shown. #[prop(flag)] vertical_only: bool, + /// Wider bars with inset thumbs, step arrows and a corner for a document window. + #[prop(flag)] + #[style_key] + document_chrome: bool, } impl<'a> CanvasScrollbars<'a> { /// Scroll bars named `label` that edit `view`, a view over the document `document` (document units). pub fn new(label: impl Into