mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 23:02:31 +00:00
Canvas scroll bars: docked look keeps the thumb's width at rest, on a chrome track
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
//! `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 floating look every scroll area has (`ScrollBarLook`): a thin line at rest
|
||||
//! over the view's edge, the whole bar under the pointer. The thumb is the visible fraction of
|
||||
//! 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
|
||||
//! 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
|
||||
@@ -20,7 +20,7 @@ use crate::contract::{
|
||||
use crate::runtime::access::Numeric;
|
||||
use crate::runtime::env::Label;
|
||||
use crate::runtime::focus::KeyUse;
|
||||
use crate::style::{ComponentStyle, ScrollBarLook, StyleCx};
|
||||
use crate::style::{ComponentStyle, ScrollBarKind, ScrollBarLook, StyleCx};
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect};
|
||||
use craft_behavior::scroll::ScrollSpan;
|
||||
use craft_behavior::{Axis, CanvasView, Metrics, PartKey, PartRef, PressPhase};
|
||||
@@ -242,7 +242,7 @@ impl Component for CanvasScrollbars<'_> {
|
||||
type Output = CanvasScrollbarsOutput;
|
||||
|
||||
fn style(cx: &StyleCx<'_>, _: CanvasScrollbarsStyleKey) -> ScrollbarsStyle {
|
||||
ScrollbarsStyle { metrics: Metrics::new(), look: cx.scroll_bar() }
|
||||
ScrollbarsStyle { metrics: Metrics::new(), look: cx.scroll_bar_of(ScrollBarKind::Docked) }
|
||||
}
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
use super::key::{EnvKey, Ink};
|
||||
use super::look::{DataColor, FocusLook, StyleColor, SurfaceKind, SurfaceLook, TrackLook};
|
||||
use super::paint::{
|
||||
HandleLook, MarkerLook, ResetMarkLook, RuleKind, RuleLook, ScrollBarLook, handle_look, marker_look, reset_mark_look, rule_look, scroll_bar_look,
|
||||
HandleLook, MarkerLook, ResetMarkLook, RuleKind, RuleLook, ScrollBarKind, ScrollBarLook, handle_look, marker_look, reset_mark_look, rule_look,
|
||||
scroll_bar_look_of,
|
||||
};
|
||||
use super::surface::{SurfaceStyle, surface_style};
|
||||
use super::tokens::UiTokens;
|
||||
@@ -430,7 +431,12 @@ impl<'a> StyleCx<'a> {
|
||||
|
||||
/// The look of a scroll bar.
|
||||
pub fn scroll_bar(&self) -> ScrollBarLook {
|
||||
scroll_bar_look(self.tokens)
|
||||
self.scroll_bar_of(ScrollBarKind::Floating)
|
||||
}
|
||||
|
||||
/// The look of a scroll bar of `kind`.
|
||||
pub fn scroll_bar_of(&self, kind: ScrollBarKind) -> ScrollBarLook {
|
||||
scroll_bar_look_of(self.tokens, kind)
|
||||
}
|
||||
|
||||
/// The state layer: the theme's hover and press fills, clear at rest and while disabled. A part declared
|
||||
|
||||
@@ -25,7 +25,7 @@ pub use cx::{GlyphRole, StyleCx, VariantParts};
|
||||
pub use key::{EnvKey, Ink, StyleKey};
|
||||
pub(crate) use look::StyleView;
|
||||
pub use look::{ComponentStyle, Corners, DataColor, FocusLook, IconLook, StyleColor, SurfaceKind, SurfaceLook, TextLook, TrackLook};
|
||||
pub use paint::{HandleLook, MarkerLook, ResetMarkLook, ResizeLineLook, RuleKind, RuleLook, RuleWidth, ScrollBarLook, resize_line_look};
|
||||
pub use paint::{HandleLook, MarkerLook, ResetMarkLook, ResizeLineLook, RuleKind, RuleLook, RuleWidth, ScrollBarKind, ScrollBarLook, resize_line_look};
|
||||
pub(crate) use paint::{min_thumb, scroll_bar_look};
|
||||
pub use replacements::{DuplicateStyle, StyleReplacements};
|
||||
pub use span::Span;
|
||||
|
||||
@@ -170,9 +170,21 @@ pub(crate) fn marker_look(tokens: &UiTokens) -> MarkerLook {
|
||||
}
|
||||
}
|
||||
|
||||
/// A scroll bar's track and thumb: one floating indicator for every scroll area, over the content at its edge. At rest
|
||||
/// the thumb is a thin dim line along the track's outer edge and the track is clear; under the pointer or while pressed
|
||||
/// the thumb takes the track's whole thickness over a tinted track.
|
||||
/// Which of the two scroll bar looks a bar has.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum ScrollBarKind {
|
||||
/// The indicator of a scroll area, over the content at its edge. At rest the thumb is a thin dim line along the
|
||||
/// track's outer edge and the track is clear; under the pointer or while pressed the thumb takes the track's whole
|
||||
/// thickness over a tinted track.
|
||||
#[default]
|
||||
Floating,
|
||||
/// The bar beside a canvas view, always shown: the thumb has the track's whole thickness in every state, on a
|
||||
/// `surface.chrome` track.
|
||||
Docked,
|
||||
}
|
||||
|
||||
/// A scroll bar's track and thumb, of a [`ScrollBarKind`].
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub struct ScrollBarLook {
|
||||
@@ -197,6 +209,10 @@ pub(crate) fn min_thumb(tokens: &UiTokens) -> f32 {
|
||||
}
|
||||
|
||||
pub(crate) fn scroll_bar_look(tokens: &UiTokens) -> ScrollBarLook {
|
||||
scroll_bar_look_of(tokens, ScrollBarKind::Floating)
|
||||
}
|
||||
|
||||
pub(crate) fn scroll_bar_look_of(tokens: &UiTokens, kind: ScrollBarKind) -> ScrollBarLook {
|
||||
let c = &tokens.color;
|
||||
let clear = style_color(Rgba::TRANSPARENT);
|
||||
let thumb = Stateful::new(style_color(c.text.faint)).off(craft_theme::States::new(
|
||||
@@ -206,8 +222,14 @@ pub(crate) fn scroll_bar_look(tokens: &UiTokens) -> ScrollBarLook {
|
||||
style_color(c.state.disabled_fill),
|
||||
));
|
||||
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);
|
||||
let (thickness, thin) = (tokens.size.handle.sm, tokens.stroke.thick);
|
||||
let (track, rest) = match kind {
|
||||
ScrollBarKind::Floating => (Stateful::new(clear).off(craft_theme::States::new(clear, hover, hover, clear)), thin),
|
||||
ScrollBarKind::Docked => {
|
||||
let chrome = style_color(c.surface.chrome);
|
||||
(Stateful::new(chrome).off(craft_theme::States::new(chrome, hover, hover, chrome)), thickness)
|
||||
}
|
||||
};
|
||||
ScrollBarLook {
|
||||
track,
|
||||
thumb,
|
||||
|
||||
@@ -185,6 +185,40 @@ fn ant_edges_are_whole_device_pixels_at_a_fractional_scale() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A docked scroll bar (the canvas's) has the same thumb across the track at rest and under the pointer, on a
|
||||
/// `surface.chrome` track.
|
||||
#[trial]
|
||||
fn a_docked_scroll_bar_keeps_its_width_at_rest_and_under_the_pointer() {
|
||||
use craft_behavior::{Axis, ScrollSpan};
|
||||
use craft_theme::{Contrast, ControlSize, Mode, Variant};
|
||||
use craft_ui::primitives::ScrollBar;
|
||||
use craft_ui::style::{Flags, ScrollBarKind, State, StateMix, StateSet};
|
||||
use egui::{Rect, Shape};
|
||||
|
||||
let tokens = craft_ui::testing::style::tokens(Mode::Dark, Contrast::Standard);
|
||||
let look = craft_ui::testing::style::with_cx(&egui::Context::default(), &tokens, Variant::SECONDARY, ControlSize::Md, |cx| {
|
||||
cx.scroll_bar_of(ScrollBarKind::Docked)
|
||||
});
|
||||
let span = ScrollSpan::new(0.0..400.0, 100.0..300.0);
|
||||
for (axis, rtl) in [(Axis::Y, false), (Axis::Y, true), (Axis::X, false)] {
|
||||
let track = match axis {
|
||||
Axis::X => Rect::from_min_max(pos2(0.0, 100.0), pos2(200.0, 100.0 + look.thickness)),
|
||||
_ => Rect::from_min_max(pos2(100.0, 0.0), pos2(100.0 + look.thickness, 200.0)),
|
||||
};
|
||||
let thumb = |state: State, mix: StateMix| {
|
||||
let shapes = ScrollBar::shapes(&look, track, axis, rtl, &span, &StateSet::new(state, Flags::empty(), mix));
|
||||
match shapes.last() {
|
||||
Some(Shape::Rect(shape)) => shape.rect,
|
||||
other => panic!("the thumb is a rect: {other:?}"),
|
||||
}
|
||||
};
|
||||
let (rest, hover) = (thumb(State::Rest, StateMix::REST), thumb(State::Hover, StateMix::new(1.0, 0.0, 0.0)));
|
||||
assert_eq!(rest, hover, "{axis:?} rtl {rtl}: the thumb is the same at rest and under the pointer");
|
||||
let across = |rect: Rect| if axis == Axis::X { rect.height() } else { rect.width() };
|
||||
assert!((across(rest) - across(track)).abs() < EPS, "{axis:?}: the thumb fills the track");
|
||||
}
|
||||
}
|
||||
|
||||
/// Every scroll bar rests as a thin line along its track's outer edge and shows its thumb across the whole track under
|
||||
/// the pointer, at the same place along the bar.
|
||||
#[trial]
|
||||
|
||||
Reference in New Issue
Block a user