mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:27:49 +00:00
Add document chrome to canvas scrollbars
This commit is contained in:
@@ -81,7 +81,8 @@ pub fn jump(span: &ScrollSpan, mv: NavMove) -> Option<Range<f64>> {
|
||||
})
|
||||
}
|
||||
|
||||
fn shifted(span: &ScrollSpan, shift: f64) -> Range<f64> {
|
||||
/// Moves the visible range by `shift` content units, keeping it within the scrollable range.
|
||||
pub fn shifted(span: &ScrollSpan, shift: f64) -> Range<f64> {
|
||||
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);
|
||||
|
||||
@@ -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<DocumentLook>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct DocumentLook {
|
||||
corner: StyleColor,
|
||||
arrow: Stateful<StyleColor>,
|
||||
}
|
||||
|
||||
/// 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<Item = (PartRef, Rect, bool)> {
|
||||
(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<Bar>,
|
||||
vertical: Option<Bar>,
|
||||
corner: Option<Rect>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct ArrowRepeat {
|
||||
part: Option<PartRef>,
|
||||
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>() = 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::<ArrowRepeat>();
|
||||
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<Span>, 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<Span>, 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<Label>, view: Bind<'a, CanvasView>, document: DocRect) -> Self {
|
||||
CanvasScrollbars { common: Common::new(), view, document, overscroll: false, vertical_only: false }.label(label)
|
||||
CanvasScrollbars { common: Common::new(), view, document, overscroll: false, vertical_only: false, document_chrome: false }.label(label)
|
||||
}
|
||||
|
||||
/// Applies overscroll off to `view` over the document `doc`: the view can't leave the document's edges, and a
|
||||
@@ -230,8 +341,8 @@ impl<'a> CanvasScrollbars<'a> {
|
||||
changed
|
||||
}
|
||||
|
||||
fn bars(&self, view: &CanvasView, rect: Rect, width: f32) -> Bars {
|
||||
Bars::of(view, self.document, self.overscroll, !self.vertical_only, rect, width)
|
||||
fn bars(&self, view: &CanvasView, rect: Rect, style: &ScrollbarsStyle) -> Bars {
|
||||
Bars::of(view, self.document, self.overscroll, !self.vertical_only, rect, style)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,8 +352,15 @@ impl Component for CanvasScrollbars<'_> {
|
||||
type Style = ScrollbarsStyle;
|
||||
type Output = CanvasScrollbarsOutput;
|
||||
|
||||
fn style(cx: &StyleCx<'_>, _: CanvasScrollbarsStyleKey) -> ScrollbarsStyle {
|
||||
ScrollbarsStyle { metrics: Metrics::new(), look: cx.scroll_bar_of(ScrollBarKind::Docked) }
|
||||
fn style(cx: &StyleCx<'_>, key: CanvasScrollbarsStyleKey) -> ScrollbarsStyle {
|
||||
let mut look = cx.scroll_bar_of(ScrollBarKind::Docked);
|
||||
let document = key.document_chrome.then(|| {
|
||||
let color = &cx.tokens.color;
|
||||
look.thickness = cx.tokens.size.icon.md;
|
||||
look.track = Stateful::new(cx.color(color.surface.sunken));
|
||||
DocumentLook { corner: cx.color(color.surface.panel), arrow: cx.quiet_ink() }
|
||||
});
|
||||
ScrollbarsStyle { metrics: Metrics::new(), look, document }
|
||||
}
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
@@ -256,16 +374,32 @@ impl Component for CanvasScrollbars<'_> {
|
||||
/// The bars: `PartsCx::scroll` runs the thumb drag and the page press in the pipeline.
|
||||
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
||||
let Some(view) = cx.value().copied() else { return };
|
||||
let bars = self.bars(&view, rect, cx.style.look.thickness);
|
||||
let bars = self.bars(&view, rect, cx.style);
|
||||
for (key, axis, bar) in bars.each() {
|
||||
cx.scroll(key, bar.rect, axis, bar.span.clone());
|
||||
cx.scroll(key, bar.track, axis, bar.span.clone());
|
||||
for (part, rect, _) in bar.arrows(key, axis) {
|
||||
cx.part(part, rect).sense(Sense::CLICK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
let Some(view) = cx.value().copied() else { return };
|
||||
let bars = self.bars(&view, cx.rect, cx.style.look.thickness);
|
||||
for (key, axis, _) in bars.each() {
|
||||
let bars = self.bars(&view, cx.rect, cx.style);
|
||||
for (key, axis, bar) in bars.each() {
|
||||
for (part, rect, after) in bar.arrows(key, axis) {
|
||||
if arrow_pressed(cx, part, rect) {
|
||||
let scale = if axis == Axis::X { aspect(&view) } else { 1.0 };
|
||||
let pixel_zoom = view.zoom * scale * f64::from(cx.response.ctx.pixels_per_point());
|
||||
let toward_end = after != (axis == Axis::X && cx.flow.is_rtl());
|
||||
let sign = if toward_end { 1.0 } else { -1.0 };
|
||||
let range = craft_behavior::scroll::shifted(&bar.span, sign * ARROW_STEP_PIXELS / pixel_zoom);
|
||||
let next = moved(&view, self.document, axis, &range);
|
||||
if next != view {
|
||||
cx.set(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
let phase = cx.part(thumb_of(key)).press.phase;
|
||||
match phase {
|
||||
PressPhase::DragStart { .. } => cx.begin(),
|
||||
@@ -285,17 +419,34 @@ impl Component for CanvasScrollbars<'_> {
|
||||
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
let Some(view) = cx.value().copied() else { return };
|
||||
let style = cx.style;
|
||||
let bars = self.bars(&view, cx.rect, style.look.thickness);
|
||||
let bars = self.bars(&view, cx.rect, style);
|
||||
let draw = ui.draw();
|
||||
let rtl = cx.flow.is_rtl();
|
||||
let fill = |rect, color| draw.fill(&PaintShape::Rect(rect, CornerRadius::ZERO), color);
|
||||
for (key, axis, bar) in bars.each() {
|
||||
draw.scroll_bar(&style.look, bar.rect, axis, cx.flow.is_rtl(), &bar.span, &cx.scroll_bar_state(key));
|
||||
let state = cx.scroll_bar_state(key);
|
||||
if let Some(look) = &style.document {
|
||||
fill(bar.rect, state.resolve(&style.look.track));
|
||||
let inset = if axis == Axis::X { vec2(0.0, bar.rect.height() / 8.0) } else { vec2(bar.rect.width() / 8.0, 0.0) };
|
||||
let thumb = ScrollBar::thumb(style.look.min_thumb, bar.track, axis, rtl, &bar.span).shrink2(inset);
|
||||
fill(thumb, state.resolve(&style.look.thumb));
|
||||
for (part, rect, after) in bar.arrows(key, axis) {
|
||||
let color = cx.part_state(part).resolve(&look.arrow);
|
||||
paint_arrow(&draw, rect, axis, after, color);
|
||||
}
|
||||
} else {
|
||||
draw.scroll_bar(&style.look, bar.track, axis, rtl, &bar.span, &state);
|
||||
}
|
||||
}
|
||||
if let (Some(corner), Some(look)) = (bars.corner, &style.document) {
|
||||
fill(corner, look.corner);
|
||||
}
|
||||
}
|
||||
|
||||
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
|
||||
let Some(view) = cx.value().copied() else { return };
|
||||
let words = cx.words::<CanvasWords>();
|
||||
let bars = self.bars(&view, cx.rect, cx.style.look.thickness);
|
||||
let bars = self.bars(&view, cx.rect, cx.style);
|
||||
for (key, axis, bar) in bars.each() {
|
||||
let name = if axis == Axis::X { &words.scroll_horizontal } else { &words.scroll_vertical };
|
||||
let room = (bar.span.content.end - bar.span.content.start - (bar.span.visible.end - bar.span.visible.start)).max(0.0);
|
||||
@@ -303,13 +454,24 @@ impl Component for CanvasScrollbars<'_> {
|
||||
let node = access.node(key, Role::SCROLL_BAR);
|
||||
node.name = Name::Text(name.as_str().to_owned());
|
||||
node.numeric = Some(Numeric::new(position, 0.0..=room));
|
||||
for (part, _, after) in bar.arrows(key, axis) {
|
||||
let name = match (axis, after) {
|
||||
(Axis::X, false) => &words.scroll_left,
|
||||
(Axis::X, true) => &words.scroll_right,
|
||||
(_, false) => &words.scroll_up,
|
||||
(_, true) => &words.scroll_down,
|
||||
};
|
||||
access.node(part, Role::BUTTON).name = Name::Text(name.as_str().to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> CanvasScrollbarsOutput {
|
||||
let over = [HORIZONTAL, VERTICAL].into_iter().any(|key| {
|
||||
let arrows = self.document_chrome.then_some([key.at(START_ARROW), key.at(END_ARROW)]);
|
||||
[PartRef::from(key), thumb_of(key)]
|
||||
.into_iter()
|
||||
.chain(arrows.into_iter().flatten())
|
||||
.any(|part| cx.part_if_declared(part).is_some_and(|result| result.hovered || result.press.phase.is_press()))
|
||||
});
|
||||
CanvasScrollbarsOutput { view: cx.edit_response(), over }
|
||||
|
||||
@@ -27,6 +27,10 @@ craft_ui::words! {
|
||||
|
||||
scroll_horizontal: "canvasScrollbars.horizontal" = "Horizontal scroll bar";
|
||||
scroll_vertical: "canvasScrollbars.vertical" = "Vertical scroll bar";
|
||||
scroll_left: "canvasScrollbars.left" = "Scroll left";
|
||||
scroll_right: "canvasScrollbars.right" = "Scroll right";
|
||||
scroll_up: "canvasScrollbars.up" = "Scroll up";
|
||||
scroll_down: "canvasScrollbars.down" = "Scroll down";
|
||||
|
||||
navigate_zoom: "navigateBar.zoom" = "Zoom";
|
||||
navigate_direction: "navigateBar.direction" = "Zoom direction";
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
//! `CanvasScrollbars`: the clamp of the view and the bars' drag and page press.
|
||||
|
||||
use std::{cell::Cell, rc::Rc};
|
||||
|
||||
use craft_behavior::CanvasView;
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect};
|
||||
use craft_ui::canvas::CanvasScrollbars;
|
||||
use craft_ui::testing::trial;
|
||||
use egui::{Pos2, Rect};
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect, Size};
|
||||
use craft_ui::canvas::{Canvas, CanvasScrollbars, NavigationOptions, canvas_space};
|
||||
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use craft_ui::testing::{Fixture, pointer, trial};
|
||||
use egui::{Pos2, Rect, Shape, Vec2, pos2, vec2};
|
||||
|
||||
/// The view's rect in the fixture, in points.
|
||||
const VIEW: Rect = Rect::from_min_max(Pos2::new(0.0, 0.0), Pos2::new(400.0, 300.0));
|
||||
@@ -48,3 +51,84 @@ fn overscroll_off_below_fit_keeps_the_image_centred() {
|
||||
assert!((image.left() - VIEW.left()).abs() < TOLERANCE, "the wide axis stops at its edge: {image:?}");
|
||||
assert!((image.center().y - VIEW.center().y).abs() < TOLERANCE, "the short axis is centred: {image:?}");
|
||||
}
|
||||
|
||||
const FITTING_DOCUMENT: Size = Size::new(400.0, 300.0);
|
||||
const DOCUMENT_WINDOW: Rect = Rect::from_min_max(Pos2::new(12.0, 12.0), Pos2::new(732.0, 512.0));
|
||||
|
||||
fn document_scrollbars() -> (Fixture, Rc<Cell<CanvasView>>) {
|
||||
let view = Rc::new(Cell::new(CanvasView::new(DOCUMENT_WINDOW, Point::new(200.0, 150.0), 1.0)));
|
||||
let observed = Rc::clone(&view);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut navigation = NavigationOptions::default();
|
||||
navigation.overscroll = true;
|
||||
Canvas::new("Document").document(FITTING_DOCUMENT).view(observed.get()).navigation(navigation).show_rulers(true).show_at(
|
||||
ui,
|
||||
DOCUMENT_WINDOW,
|
||||
|ui, body| {
|
||||
let Some(space) = canvas_space(&body.env) else { return };
|
||||
let mut next = space.view;
|
||||
let doc = DocRect::from_origin_size(Point::ZERO, FITTING_DOCUMENT);
|
||||
CanvasScrollbars::new("Scroll bars", Bind::bound(&mut next), doc).overscroll(true).document_chrome().show_at(ui, body.rect);
|
||||
observed.set(next);
|
||||
},
|
||||
);
|
||||
});
|
||||
fixture.room(vec2(760.0, 560.0)).settle();
|
||||
(fixture, view)
|
||||
}
|
||||
|
||||
/// #15529: the document bars leave room for arrows and a corner, and put a thinner thumb inside each track.
|
||||
#[trial]
|
||||
fn document_bars_show_arrows_and_an_inset_thumb_for_a_fitting_image() {
|
||||
let (mut fixture, view) = document_scrollbars();
|
||||
let horizontal = fixture.by_label("Horizontal scroll bar").expect("the horizontal bar").rect;
|
||||
let vertical = fixture.by_label("Vertical scroll bar").expect("the vertical bar").rect;
|
||||
assert!((horizontal.height() - 16.0).abs() < TOLERANCE, "horizontal track: {horizontal:?}");
|
||||
assert!((vertical.width() - 16.0).abs() < TOLERANCE, "vertical track: {vertical:?}");
|
||||
|
||||
let left = fixture.by_label("Scroll left").expect("the left arrow").rect;
|
||||
let right = fixture.by_label("Scroll right").expect("the right arrow").rect;
|
||||
let up = fixture.by_label("Scroll up").expect("the up arrow").rect;
|
||||
let down = fixture.by_label("Scroll down").expect("the down arrow").rect;
|
||||
assert!((left.right() - horizontal.left()).abs() < TOLERANCE && (right.left() - horizontal.right()).abs() < TOLERANCE);
|
||||
assert!((up.bottom() - vertical.top()).abs() < TOLERANCE && (down.top() - vertical.bottom()).abs() < TOLERANCE);
|
||||
assert!((up.top() - DOCUMENT_WINDOW.top()).abs() < TOLERANCE, "the bar starts beside the ruler row");
|
||||
assert!(up.top() < view.get().rect.top(), "the visible document starts below the ruler row");
|
||||
|
||||
let corner = Rect::from_min_size(pos2(right.right(), down.bottom()), Vec2::splat(16.0));
|
||||
let shapes = fixture.frame_shapes();
|
||||
assert!(shapes.iter().any(|shape| matches!(shape, Shape::Rect(paint) if paint.rect == corner && paint.fill.a() > 0)), "the corner is painted");
|
||||
assert!(
|
||||
shapes.iter().any(|shape| matches!(shape, Shape::Rect(paint)
|
||||
if horizontal.contains_rect(paint.rect)
|
||||
&& (paint.rect.top() - horizontal.top() - 2.0).abs() < TOLERANCE
|
||||
&& (paint.rect.height() - 12.0).abs() < TOLERANCE
|
||||
&& paint.rect.width() < horizontal.width()
|
||||
&& paint.fill.a() > 0)),
|
||||
"the fitting document's thumb is shorter than the track and inset on both sides",
|
||||
);
|
||||
}
|
||||
|
||||
/// #15529: a press is one small step, a held press repeats, and the rest of the track still pages.
|
||||
#[trial]
|
||||
fn a_document_scroll_arrow_steps_and_repeats_while_its_track_pages() {
|
||||
let (mut fixture, view) = document_scrollbars();
|
||||
let start = view.get().center;
|
||||
let arrow = fixture.by_label("Scroll right").expect("the right arrow").center();
|
||||
fixture.frame(vec![pointer::moved(arrow)]).frame(vec![pointer::press(arrow)]);
|
||||
let stepped = view.get().center;
|
||||
assert!((stepped.x - start.x - 16.0).abs() < f64::from(TOLERANCE), "the arrow moves 16 screen pixels at 100%: {start:?} -> {stepped:?}");
|
||||
assert_eq!(stepped.y, start.y);
|
||||
fixture.advance(0.05).frame(vec![]);
|
||||
assert_eq!(view.get().center, stepped, "the first step precedes the hold delay");
|
||||
fixture.advance(0.5).frame(vec![]);
|
||||
let repeated = view.get().center;
|
||||
assert!(repeated.x > stepped.x && repeated.y == start.y, "the held arrow repeats on its own axis");
|
||||
fixture.frame(vec![pointer::release(arrow)]).advance(1.0).frame(vec![]);
|
||||
assert_eq!(view.get().center, repeated, "releasing the arrow stops the repeat");
|
||||
|
||||
let track = fixture.by_label("Horizontal scroll bar").expect("the horizontal track").rect;
|
||||
fixture.click_at(pos2(track.right() - 8.0, track.center().y));
|
||||
fixture.settle();
|
||||
assert!(view.get().center.x > repeated.x + 16.0, "the track still pages instead of stepping");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user