Design-lint clean: the scroll thumb's width per state, Canvas::layer_bounds as a prop, centring relations in the tool rail trial

This commit is contained in:
aiongg
2026-11-16 19:55:05 +00:00
parent b1766bb698
commit 95b0a3e714
5 changed files with 32 additions and 28 deletions
+3 -9
View File
@@ -164,7 +164,9 @@ pub struct Canvas<'a> {
/// disabled for it, and so is each command whose fact the target lacks.
#[prop(into)]
document: Option<ViewTarget>,
/// The bounds Fit Layers on Screen shows ([`layer_bounds`](Self::layer_bounds)).
/// The bounds Fit Layers on Screen shows, asked only in the pass that runs the command. Without it Fit Layers is
/// disabled for this canvas.
#[prop]
layer_bounds: Option<LayerBounds<'a>>,
/// The user's navigation options: how the wheel and a pinch move the view ([`WheelNavigation`]), whether a zoom
/// eases (Animated Zoom) and whether the view may leave the document (Overscroll).
@@ -276,14 +278,6 @@ impl<'a> Canvas<'a> {
self
}
/// The bounds Fit Layers on Screen shows, asked only in the pass that runs the command. Without it Fit Layers is
/// disabled for this canvas.
#[must_use]
pub fn layer_bounds(mut self, bounds: LayerBounds<'a>) -> Self {
self.layer_bounds = Some(bounds);
self
}
/// Eases from `from`, the view the app drew last, to the view given this pass, as the canvas eases its own zoom
/// commands: an app's zoom step made outside the canvas (a menu row, a Zoom tool click before the app's tools run
/// on the tool host). `anchor` keeps a document point under a screen point along the way. With Animated Zoom off,
@@ -85,13 +85,11 @@ impl ScrollBar {
}
/// The shapes of the bar in `track` along `axis` for `span`, in `state` (the bar's: hovered while the pointer is
/// anywhere on it): the track and the fully round thumb. At rest the thumb is `look.rest` thick along the track's
/// outer edge (its bottom, or its end side in the reading direction) and the track is clear; hovered or pressed
/// the thumb fills the track's thickness.
/// anywhere on it): the track and the fully round thumb, `look.thumb_width` thick along the track's outer edge (its
/// bottom, or its end side in the reading direction).
pub fn shapes(look: &ScrollBarLook, track: Rect, axis: Axis, rtl: bool, span: &ScrollSpan, state: &StateSet) -> Vec<Shape> {
let full = Self::thumb(look.min_thumb, track, axis, rtl, span);
let lit = matches!(state.state, craft_theme::State::Hover | craft_theme::State::Pressed);
let thumb = if lit { full } else { Self::resting(full, look.rest, axis, rtl) };
let thumb = Self::along_edge(full, state.resolve(&look.thumb_width), axis, rtl);
let round = |rect: Rect| CornerRadius::from(rect.width().min(rect.height()) / 2.0);
vec![
Shape::rect_filled(track, round(full), state.resolve(&look.track).egui()),
@@ -99,14 +97,14 @@ impl ScrollBar {
]
}
/// The resting thumb: `rest` thick along the outer edge of the full thumb `full`.
fn resting(full: Rect, rest: f32, axis: Axis, rtl: bool) -> Rect {
/// The thumb `width` thick along the outer edge of the full thumb `full`, at most `full`.
fn along_edge(full: Rect, width: f32, axis: Axis, rtl: bool) -> Rect {
let side = if axis == Axis::X { RectSide::Bottom } else { RectSide::end(rtl) };
let rest = rest.min(if axis == Axis::X { full.height() } else { full.width() });
let width = width.min(if axis == Axis::X { full.height() } else { full.width() });
match side {
RectSide::Bottom => Rect::from_min_max(pos2(full.min.x, full.max.y - rest), full.max),
RectSide::Left => Rect::from_min_max(full.min, pos2(full.min.x + rest, full.max.y)),
_ => Rect::from_min_max(pos2(full.max.x - rest, full.min.y), full.max),
RectSide::Bottom => Rect::from_min_max(pos2(full.min.x, full.max.y - width), full.max),
RectSide::Left => Rect::from_min_max(full.min, pos2(full.min.x + width, full.max.y)),
_ => Rect::from_min_max(pos2(full.max.x - width, full.min.y), full.max),
}
}
}
+7 -5
View File
@@ -180,10 +180,11 @@ pub struct ScrollBarLook {
pub track: Stateful<StyleColor>,
/// The thumb fill per state.
pub thumb: Stateful<StyleColor>,
/// The bar's thickness: its track, and its thumb under the pointer or while pressed.
/// The bar's thickness: its track, and its thumb at its widest.
pub thickness: f32,
/// The thumb's thickness at rest.
pub rest: f32,
/// The thumb's thickness per state: thin at rest along the track's outer edge, the bar's thickness under the
/// pointer or while pressed.
pub thumb_width: Stateful<f32>,
/// The shortest the thumb gets along the bar.
pub min_thumb: f32,
/// The length of the fade at an edge past which content is hidden.
@@ -206,11 +207,12 @@ pub(crate) fn scroll_bar_look(tokens: &UiTokens) -> ScrollBarLook {
));
let hover = style_color(c.state.hover);
let track = Stateful::new(clear).off(craft_theme::States::new(clear, hover, hover, clear));
let (thickness, rest) = (tokens.size.handle.sm, tokens.stroke.thick);
ScrollBarLook {
track,
thumb,
thickness: tokens.size.handle.sm,
rest: tokens.stroke.thick,
thickness,
thumb_width: Stateful::new(rest).off(craft_theme::States::new(rest, thickness, thickness, rest)),
min_thumb: min_thumb(tokens),
fade: tokens.size.scroll_fade,
}
@@ -36,6 +36,16 @@ pub fn same_column(a: Rect, b: Rect) -> bool {
aligned_left(a, b) && (a.width() - b.width()).abs() <= EPS
}
/// `a` and `b` share a horizontal centre.
pub fn centred_across(a: Rect, b: Rect) -> bool {
(a.center().x - b.center().x).abs() <= EPS
}
/// `a` and `b` share a vertical centre: they stand on one line.
pub fn on_one_line(a: Rect, b: Rect) -> bool {
(a.center().y - b.center().y).abs() <= EPS
}
/// `inner` lies within `outer`.
pub fn inside(inner: Rect, outer: Rect) -> bool {
inner.min.x >= outer.min.x - EPS && inner.min.y >= outer.min.y - EPS && inner.max.x <= outer.max.x + EPS && inner.max.y <= outer.max.y + EPS
@@ -7,7 +7,7 @@ use crate::contract::Show as _;
use crate::rails::tool::stories::{StoryTools, tools};
use crate::runtime::commands::CommandState;
use crate::testing::pointer::{moved, wheel};
use crate::testing::relations::{above, grows_with};
use crate::testing::relations::{above, centred_across, grows_with, on_one_line};
use crate::testing::{Fixture, trial};
use egui::{Rect, Shape, Vec2};
use std::cell::Cell;
@@ -134,9 +134,9 @@ fn a_tool_rail_centres_its_buttons_in_its_columns_and_keeps_its_toggles_on_one_l
}
let (first, last) = (rect(&fixture, "Move"), rect(&fixture, if columns == 1 { "Move" } else { "Brush" }));
let grid = first.union(last);
assert!((grid.center().x - rail.center().x).abs() < 0.5, "{columns} columns: the buttons are centred: {grid:?} in {rail:?}");
assert!(centred_across(grid, rail), "{columns} columns: the buttons are centred: {grid:?} in {rail:?}");
let (toggle, side) = (rect(&fixture, "Toolbar"), rect(&fixture, "Move Toolbar"));
assert!((toggle.center().y - side.center().y).abs() < 0.5, "the toggles share a line: {toggle:?} and {side:?}");
assert!(on_one_line(toggle, side), "the toggles share a line: {toggle:?} and {side:?}");
assert!(above(toggle, first), "the toggles are above the buttons");
}
}