From 7cd391de7e58d8978136c1b28679e10331db5115 Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 16:12:12 +0000 Subject: [PATCH] One floating scroll indicator for every scroll area ScrollBarLook is one look: a clear track and a thin dim thumb line along the track's outer edge at rest (stroke.thick), the whole track thickness (handle.sm) over a tinted track under the pointer or while pressed. The docked kind (chrome track, edge rule, inset thumb) goes: canvas and timeline bars take the same look. CxCore::scroll_bar_state gives a bar's state, hovered while the pointer is anywhere on the bar, for every painter of a bar. --- crates/timeline/src/ui/view.rs | 8 ++-- crates/ui-canvas/src/canvas/scrollbars/mod.rs | 20 +++----- crates/ui-contract/src/contract/body/hooks.rs | 4 +- .../ui-contract/src/contract/body/scroll.rs | 15 ++---- crates/ui-contract/src/contract/cx/styled.rs | 9 +++- .../ui-contract/src/contract/items/header.rs | 4 +- .../ui-contract/src/contract/items/hooks.rs | 13 +++-- crates/ui-contract/src/primitives/draw.rs | 5 +- .../ui-contract/src/primitives/scroll_bar.rs | 48 +++++++++---------- crates/ui-contract/src/style/cx.rs | 8 ++-- crates/ui-contract/src/style/mod.rs | 2 +- crates/ui-contract/src/style/paint.rs | 45 +++++++---------- crates/ui-contract/tests/it/paint/painters.rs | 40 ++++++++++++++++ 13 files changed, 123 insertions(+), 98 deletions(-) diff --git a/crates/timeline/src/ui/view.rs b/crates/timeline/src/ui/view.rs index 6bfebbe70..d3420d63d 100644 --- a/crates/timeline/src/ui/view.rs +++ b/crates/timeline/src/ui/view.rs @@ -11,7 +11,7 @@ use craft_behavior::scroll::ScrollSpan; use craft_behavior::{Axis, EditEvent, Granularity, Intent, Metrics, Mixed, NavMove, PartKey, Sense, Wheel}; use craft_ui::contract::{ Access, Bind, BindMut, CommandSet, Commands as _, Common, Component, Container, ContainerProps, Cx, Edit as _, EditResponse, Focus as _, Name, - Outcome, PaintUi, PartsCx, Pointer as _, Props, Remember as _, Role, Sizing, THUMB, WordSetCx as _, + Outcome, PaintUi, PartsCx, Pointer as _, Props, Remember as _, Role, Sizing, WordSetCx as _, }; use craft_ui::primitives::{PaintShape, StrokeWidth}; use craft_ui::runtime::access::Numeric; @@ -19,7 +19,7 @@ use craft_ui::runtime::commands::CommandState; use craft_ui::runtime::env::{AppValues, EnvValue, EnvView, Label}; use craft_ui::runtime::focus::KeyUse; use craft_ui::runtime::motion::{Animate as _, spec}; -use craft_ui::style::{ComponentStyle, ScrollBarKind, ScrollBarLook, StyleColor, StyleCx}; +use craft_ui::style::{ComponentStyle, ScrollBarLook, StyleColor, StyleCx}; use egui::{CornerRadius, Pos2, Rect, Response}; use std::cell::Cell; use std::ops::Deref; @@ -447,7 +447,7 @@ impl Component for TimelineView<'_> { fn style(cx: &StyleCx<'_>, _: TimelineViewStyleKey) -> TimelineViewStyle { TimelineViewStyle { metrics: Metrics::new(), - look: cx.scroll_bar(ScrollBarKind::Docked), + look: cx.scroll_bar(), playhead: cx.color(cx.tokens.color.accent.solid), snap: cx.color(cx.tokens.color.overlay.line), marquee_fill: cx.color(cx.tokens.color.accent.soft), @@ -545,7 +545,7 @@ impl Component for TimelineView<'_> { let style = cx.style; let track = self.bar(cx.rect, cx.flow, style); let span = self.span(&view, width); - ui.draw().scroll_bar(&style.look, track, Axis::X, cx.flow.is_rtl(), &span, &cx.part_state(BAR.at(THUMB))); + ui.draw().scroll_bar(&style.look, track, Axis::X, cx.flow.is_rtl(), &span, &cx.scroll_bar_state(BAR)); } /// The lines and the marquee over the panes, after the body has reported them. diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index 10b38a124..4c8da55aa 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -1,5 +1,6 @@ -//! `CanvasScrollbars`: the scroll bars of a canvas, as Photoshop's document window has them (20.6): a horizontal bar -//! along the bottom of the view and a vertical one along the right, always shown. The thumb is the visible fraction of +//! `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 //! 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 @@ -16,15 +17,14 @@ use crate::contract::{ Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, Name, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Role, Styled as _, THUMB, WordSetCx as _, }; -use crate::primitives::PaintShape; use crate::runtime::access::Numeric; use crate::runtime::env::Label; use crate::runtime::focus::KeyUse; -use crate::style::{ComponentStyle, ScrollBarKind, ScrollBarLook, StyleCx}; +use crate::style::{ComponentStyle, ScrollBarLook, 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::{CornerRadius, Pos2, Rect, Response, Vec2}; +use egui::{Pos2, Rect, Response, Vec2}; use std::ops::{Deref, Range}; const HORIZONTAL: PartKey = PartKey::new("horizontal"); @@ -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(ScrollBarKind::Docked) } + ScrollbarsStyle { metrics: Metrics::new(), look: cx.scroll_bar() } } fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { @@ -287,14 +287,8 @@ impl Component for CanvasScrollbars<'_> { let style = cx.style; let bars = self.bars(&view, cx.rect, style.look.thickness); let draw = ui.draw(); - if let (Some(h), Some(v)) = (&bars.horizontal, &bars.vertical) { - draw.fill( - &PaintShape::Rect(Rect::from_min_max(Pos2::new(v.rect.left(), h.rect.top()), cx.rect.max), CornerRadius::ZERO), - cx.resolve(&style.look.track), - ); - } for (key, axis, bar) in bars.each() { - draw.scroll_bar(&style.look, bar.rect, axis, cx.flow.is_rtl(), &bar.span, &cx.part_state(thumb_of(key))); + draw.scroll_bar(&style.look, bar.rect, axis, cx.flow.is_rtl(), &bar.span, &cx.scroll_bar_state(key)); } } diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 19b2b1994..d5b0aa8b1 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -33,7 +33,7 @@ use crate::runtime::overlay::CloseReason; use crate::runtime::pass_state; use crate::runtime::{keys, layout_memory, pointer, wheel, windows}; use crate::style::ComponentStyle as _; -use crate::style::{ScrollBarKind, StyleCx, SurfaceKind, band_color, scroll_bar_look, surface_style}; +use crate::style::{StyleCx, SurfaceKind, band_color, scroll_bar_look, surface_style}; use craft_behavior::layout_memory::measure_tolerance; use craft_behavior::{CancelRule, CursorIcon, EditEvent, KeyRole, Mixed, Roving, ScrollPos, ScrollSpan, WheelRoom, WheelTaker}; use egui::{Context, CornerRadius, Id, Pos2, Rect, Shape, Ui, UiBuilder, Vec2}; @@ -875,7 +875,7 @@ impl ShapeHooks for BodyHooks<'_> { Some(scroll) => { let off = self.offset; let big = crate::contract::MAX_EXTENT; - let bar = scroll_bar_look(&body_env.tokens, ScrollBarKind::Overlay).thickness; + let bar = scroll_bar_look(&body_env.tokens).thickness; let last = Vec2::new(self.last_extent.0, self.last_extent.1); let inset = overflow_inset(scroll, last, at.size(), bar); let shows = view_within(scroll, last, at, self.strip); diff --git a/crates/ui-contract/src/contract/body/scroll.rs b/crates/ui-contract/src/contract/body/scroll.rs index 6f5925185..3c9a94dd7 100644 --- a/crates/ui-contract/src/contract/body/scroll.rs +++ b/crates/ui-contract/src/contract/body/scroll.rs @@ -11,7 +11,7 @@ use crate::runtime::egui::{repaint, sizing}; use crate::runtime::env::Env; use crate::runtime::lazy; use crate::runtime::pass_state; -use crate::style::{ScrollBarKind, SurfaceKind, UiTokens, scroll_bar_look, surface_style}; +use crate::style::{SurfaceKind, UiTokens, scroll_bar_look, surface_style}; use craft_behavior::{Axis, PartKey, ScrollPos, ScrollSpan}; use egui::{Context, Id, Pos2, Rect, Ui, Vec2}; @@ -164,7 +164,7 @@ impl BodyHooks<'_> { /// The edge fades and the scroll bars over the body. The fades blend the content into the surface it sits on. pub(super) fn paint_scroll_bars(&self, ui: &mut Ui, cx: &Cx<'_, C>, props: &ContainerProps, rtl: bool, env: &Env) { let tokens = &*env.tokens; - let look = scroll_bar_look(tokens, ScrollBarKind::Overlay); + let look = scroll_bar_look(tokens); if let Some(at) = self.view && let Some(scroll) = scroll_of(props) && let Some(strips) = self.strips(scroll, at) @@ -191,13 +191,8 @@ impl BodyHooks<'_> { if !overflow { continue; } - let enabled = cx.core.looks_enabled(); - let state = crate::contract::steps::part_state( - cx.core.results.get(&(bar.key, Some(crate::contract::cx::THUMB))).unwrap_or(&crate::primitives::PartResult::IDLE), - enabled, - None, - ); - ui.painter().extend(ScrollBar::shapes(&look, bar.track, bar.axis, rtl, &span, &state, ui.ctx().pixels_per_point())); + let state = cx.core.scroll_bar_state(bar.key); + ui.painter().extend(ScrollBar::shapes(&look, bar.track, bar.axis, rtl, &span, &state)); } } } @@ -280,7 +275,7 @@ impl<'b> BodyHooks<'b> { pub(super) fn scroll_geometry(&self, props: &ContainerProps, view: Rect, rtl: bool, tokens: &UiTokens) -> Vec<(Bar, ScrollSpan)> { let Some(scroll) = scroll_of(props) else { return Vec::new() }; - let thickness = scroll_bar_look(tokens, ScrollBarKind::Overlay).thickness; + let thickness = scroll_bar_look(tokens).thickness; let (v_on, h_on) = (scroll.vertical_on(), scroll.horizontal_on()); let (ex, ey) = self.last_extent; let mut bars = Vec::new(); diff --git a/crates/ui-contract/src/contract/cx/styled.rs b/crates/ui-contract/src/contract/cx/styled.rs index ebf840b28..7189a504f 100644 --- a/crates/ui-contract/src/contract/cx/styled.rs +++ b/crates/ui-contract/src/contract/cx/styled.rs @@ -6,7 +6,7 @@ use crate::contract::Component; use crate::primitives::PartResult; use crate::runtime::focus::{self}; use crate::style::{Ink, Mix, StateSet, Stateful}; -use craft_behavior::PartRef; +use craft_behavior::{PartKey, PartRef}; use craft_theme::Flags; /// The resolved look (chapter 4); every method reads step 12's output. @@ -59,6 +59,13 @@ impl CxCore<'_> { self.part_state_lit(part, false) } + /// The state set of the scroll bar `bar` (`PartsCx::scroll`): its thumb's, hovered while the pointer is anywhere + /// on the bar or the bar's track is pressed, so the bar shows at its full thickness then. + pub fn scroll_bar_state(&self, bar: PartKey) -> StateSet { + let on_bar = self.part_if_declared(bar).is_some_and(|track| track.hovered || track.pressed()); + self.part_state_lit(bar.at(super::THUMB), on_bar) + } + /// The state set of `part`, hovered as well when `lit`: a part the pointer is not over that a press at the pointer /// would act on. /// diff --git a/crates/ui-contract/src/contract/items/header.rs b/crates/ui-contract/src/contract/items/header.rs index 18842358f..455a33725 100644 --- a/crates/ui-contract/src/contract/items/header.rs +++ b/crates/ui-contract/src/contract/items/header.rs @@ -20,7 +20,7 @@ use crate::runtime::env::{Env, Label}; use crate::runtime::layout_memory; use crate::runtime::memory::{MemoryStage, sub}; use crate::runtime::words::HeaderWords; -use crate::style::{ComponentStyle, ResizeLineLook, ScrollBarKind, StateSet, StyleColor, StyleCx, TypeRole, resize_line_look, scroll_bar_look}; +use crate::style::{ComponentStyle, ResizeLineLook, StateSet, StyleColor, StyleCx, TypeRole, resize_line_look, scroll_bar_look}; use craft_behavior::grid::resolve_columns; use craft_behavior::{ Axis, ColumnKey, ColumnWidth, CursorIcon, DropOperation, Flow, HitLevel, HitPriority, IconId, Insets, Intent, Metrics, NavMove, NavShape, @@ -380,7 +380,7 @@ impl Header { let area = pcx.content_rect; let view = Rect::from_min_size(area.min, Vec2::new(area.width(), self.height)); self.view = Some(view); - let grip = scroll_bar_look(pcx.tokens, ScrollBarKind::Overlay).thickness; + let grip = scroll_bar_look(pcx.tokens).thickness; let rtl = pcx.flow.is_rtl(); let widths = self.widths(area.width()); let wide = widths.iter().sum::().max(area.width()); diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index bad7ca125..9fc5994ae 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -21,7 +21,7 @@ use crate::runtime::env::WheelScope; use crate::runtime::env::{AppValues, ArenaId, Env, ItemFacts}; use crate::runtime::memory::{MemoryStage, sub}; use crate::runtime::{focus, keys, layout_memory, lazy, overlay, wheel}; -use crate::style::{ComponentStyle as _, ScrollBarKind, StyleCx, SurfaceKind, UiTokens, scroll_bar_look, surface_style}; +use crate::style::{ComponentStyle as _, StyleCx, SurfaceKind, UiTokens, scroll_bar_look, surface_style}; use craft_behavior::{ Axis, CancelReason, EditEvent, Intent, ItemLayout, Mixed, Proposal, ScrollPos, ScrollSpan, SelectionModel, Sense, WheelRoom, WheelTaker, }; @@ -279,7 +279,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { fn geometry(&mut self, _: &C, mcx: &crate::contract::MeasureCx<'_, C>, (_, content): (Rect, Rect), rtl: bool) -> Option> { let view = self.below_header(content); - let clear = self.clear_of_bar(view, scroll_bar_look(mcx.tokens, ScrollBarKind::Overlay).thickness, rtl); + let clear = self.clear_of_bar(view, scroll_bar_look(mcx.tokens).thickness, rtl); let offset = ScrollPos { x: self.offset_x, y: self.offset.clamp(0.0, self.max_offset(view.height())) }; let mut layout = self.layout.take()?; Arc::make_mut(&mut layout).set_view(view.size(), offset); @@ -297,7 +297,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { return; } let view = self.below_header(pcx.content_rect); - let track = bar_track(view, scroll_bar_look(pcx.tokens, ScrollBarKind::Overlay).thickness, pcx.flow.is_rtl()); + let track = bar_track(view, scroll_bar_look(pcx.tokens).thickness, pcx.flow.is_rtl()); let content = f64::from(self.content.y).max(f64::from(view.height())); self.offset = self.offset.clamp(0.0, self.max_offset(view.height())); let span = ScrollSpan::new(0.0..content, self.offset..self.offset + f64::from(view.height())); @@ -753,7 +753,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { Env::restore(&ctx, installed); } if self.scrolls { - let look = scroll_bar_look(&env.tokens, ScrollBarKind::Overlay); + let look = scroll_bar_look(&env.tokens); // The rows blend into the surface they sit on. let background = surface_style(&env.tokens, SurfaceKind::under(own, env.surface)).fill; let hidden = HiddenContent::new(self.content, rect.size(), Vec2::new(self.offset_x as f32, self.offset as f32), rtl); @@ -762,9 +762,8 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { let track = bar_track(rect, look.thickness, rtl); let content = f64::from(self.content.y); let span = ScrollSpan::new(0.0..content, self.offset..self.offset + f64::from(rect.height())); - let result = cx.core.results.get(&(SCROLL, Some(crate::contract::cx::THUMB))).cloned().unwrap_or(crate::primitives::PartResult::IDLE); - let state = crate::contract::steps::part_state(&result, cx.core.looks_enabled(), None); - ui.painter().extend(ScrollBar::shapes(&look, track, Axis::Y, rtl, &span, &state, ui.ctx().pixels_per_point())); + let state = cx.core.scroll_bar_state(SCROLL); + ui.painter().extend(ScrollBar::shapes(&look, track, Axis::Y, rtl, &span, &state)); } } self.layout = Some(layout); diff --git a/crates/ui-contract/src/primitives/draw.rs b/crates/ui-contract/src/primitives/draw.rs index 73d3c74d9..c3cc2c4a3 100644 --- a/crates/ui-contract/src/primitives/draw.rs +++ b/crates/ui-contract/src/primitives/draw.rs @@ -406,7 +406,8 @@ impl<'a> Draw<'a> { self.painter.extend(super::rule::Rule::edge(&look, rect, side, self.painter.pixels_per_point())); } - /// Paints the scroll bar in `track` along `axis` for `span`, in `state` (`ScrollBar::shapes`). + /// Paints the scroll bar in `track` along `axis` for `span`, in the bar's `state` (`ScrollBar::shapes`; + /// `CxCore::scroll_bar_state` gives it). pub fn scroll_bar( &self, look: &crate::style::ScrollBarLook, @@ -416,7 +417,7 @@ impl<'a> Draw<'a> { span: &craft_behavior::ScrollSpan, state: &crate::style::StateSet, ) { - self.painter.extend(super::ScrollBar::shapes(look, track, axis, rtl, span, state, self.painter.pixels_per_point())); + self.painter.extend(super::ScrollBar::shapes(look, track, axis, rtl, span, state)); } /// The width in points of the line `rule` paints for `kind`. diff --git a/crates/ui-contract/src/primitives/scroll_bar.rs b/crates/ui-contract/src/primitives/scroll_bar.rs index 3dc143db8..e90e78c44 100644 --- a/crates/ui-contract/src/primitives/scroll_bar.rs +++ b/crates/ui-contract/src/primitives/scroll_bar.rs @@ -1,7 +1,7 @@ //! `ScrollBar`: the track and thumb of a scroll part, with state. -use super::rule::{RectSide, Rule}; -use crate::style::{ScrollBarKind, ScrollBarLook, StateSet}; +use super::rule::RectSide; +use crate::style::{ScrollBarLook, StateSet}; use craft_behavior::{Axis, ScrollSpan}; use egui::{Color32, CornerRadius, Direction, Rect, Shape, Vec2, pos2}; @@ -84,29 +84,29 @@ impl ScrollBar { .collect() } - /// The shapes of the bar in `track` along `axis` for `span`, in `state`: the track, a docked bar's rule along the - /// side facing the view (its top for a horizontal bar, its left for a vertical one), and the fully round thumb. - pub fn shapes( - look: &ScrollBarLook, - track: Rect, - axis: Axis, - rtl: bool, - span: &ScrollSpan, - state: &StateSet, - pixels_per_point: f32, - ) -> Vec { - let thumb = Self::thumb(look.min_thumb, track, axis, rtl, span).shrink(look.inset); + /// 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. + pub fn shapes(look: &ScrollBarLook, track: Rect, axis: Axis, rtl: bool, span: &ScrollSpan, state: &StateSet) -> Vec { + 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 round = |rect: Rect| CornerRadius::from(rect.width().min(rect.height()) / 2.0); - let track_round = match look.kind { - ScrollBarKind::Overlay => round(thumb), - ScrollBarKind::Docked => CornerRadius::ZERO, - }; - let mut shapes = vec![Shape::rect_filled(track, track_round, state.resolve(&look.track).egui())]; - if let Some(edge) = &look.edge { - let side = if axis == Axis::X { RectSide::Top } else { RectSide::Left }; - shapes.extend(Rule::edge(edge, track, side, pixels_per_point)); + vec![ + Shape::rect_filled(track, round(full), state.resolve(&look.track).egui()), + Shape::rect_filled(thumb, round(thumb), state.resolve(&look.thumb).egui()), + ] + } + + /// 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 { + 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() }); + 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), } - shapes.push(Shape::rect_filled(thumb, round(thumb), state.resolve(&look.thumb).egui())); - shapes } } diff --git a/crates/ui-contract/src/style/cx.rs b/crates/ui-contract/src/style/cx.rs index 534c66687..65b9da728 100644 --- a/crates/ui-contract/src/style/cx.rs +++ b/crates/ui-contract/src/style/cx.rs @@ -3,7 +3,7 @@ use super::key::{EnvKey, Ink}; use super::look::{DataColor, FocusLook, StyleColor, SurfaceKind, SurfaceLook, TrackLook}; use super::paint::{ - HandleLook, MarkerLook, ResetMarkLook, RuleKind, RuleLook, ScrollBarKind, ScrollBarLook, handle_look, marker_look, reset_mark_look, rule_look, + HandleLook, MarkerLook, ResetMarkLook, RuleKind, RuleLook, ScrollBarLook, handle_look, marker_look, reset_mark_look, rule_look, scroll_bar_look, }; use super::surface::{SurfaceStyle, surface_style}; @@ -429,9 +429,9 @@ impl<'a> StyleCx<'a> { marker_look(self.tokens) } - /// The look of a scroll bar of `kind`. - pub fn scroll_bar(&self, kind: ScrollBarKind) -> ScrollBarLook { - scroll_bar_look(self.tokens, kind) + /// The look of a scroll bar. + pub fn scroll_bar(&self) -> ScrollBarLook { + scroll_bar_look(self.tokens) } /// The state layer: the theme's hover and press fills, clear at rest and while disabled. A part declared diff --git a/crates/ui-contract/src/style/mod.rs b/crates/ui-contract/src/style/mod.rs index d13c35b33..4efd11df1 100644 --- a/crates/ui-contract/src/style/mod.rs +++ b/crates/ui-contract/src/style/mod.rs @@ -26,7 +26,7 @@ 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, ScrollBarKind, ScrollBarLook, resize_line_look, + HandleLook, MarkerLook, ResetMarkLook, ResizeLineLook, RuleKind, RuleLook, RuleWidth, ScrollBarLook, resize_line_look, }; pub(crate) use paint::{min_thumb, scroll_bar_look}; pub use replacements::{DuplicateStyle, StyleReplacements}; diff --git a/crates/ui-contract/src/style/paint.rs b/crates/ui-contract/src/style/paint.rs index 7edc7d7c3..347b72509 100644 --- a/crates/ui-contract/src/style/paint.rs +++ b/crates/ui-contract/src/style/paint.rs @@ -170,35 +170,22 @@ pub(crate) fn marker_look(tokens: &UiTokens) -> MarkerLook { } } -/// Where a scroll bar sits. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -#[non_exhaustive] -pub enum ScrollBarKind { - /// Over the content at its end edge: a clear track that tints under the pointer, a thumb as thick as the track. - Overlay, - /// Beside the view along its bottom and right, as a document window's bars: a solid chrome track with an edge rule - /// on the side facing the view, and a thumb inset from the track's edges. - Docked, -} - -/// A scroll bar's track and thumb. +/// 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. #[derive(Clone, Debug, PartialEq)] #[non_exhaustive] pub struct ScrollBarLook { - /// Where the bar sits. - pub kind: ScrollBarKind, /// The track fill per state. pub track: Stateful, /// The thumb fill per state. pub thumb: Stateful, - /// The bar's thickness. + /// The bar's thickness: its track, and its thumb under the pointer or while pressed. pub thickness: f32, + /// The thumb's thickness at rest. + pub rest: f32, /// The shortest the thumb gets along the bar. pub min_thumb: f32, - /// Between the track's edges and the painted thumb. - pub inset: f32, - /// The rule along the side of the track that faces the view. - pub edge: Option, /// The length of the fade at an edge past which content is hidden. pub fade: f32, } @@ -208,7 +195,7 @@ pub(crate) fn min_thumb(tokens: &UiTokens) -> f32 { tokens.size.handle.md * 2.0 } -pub(crate) fn scroll_bar_look(tokens: &UiTokens, kind: ScrollBarKind) -> ScrollBarLook { +pub(crate) fn scroll_bar_look(tokens: &UiTokens) -> 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( @@ -217,14 +204,16 @@ pub(crate) fn scroll_bar_look(tokens: &UiTokens, kind: ScrollBarKind) -> ScrollB style_color(c.text.normal), style_color(c.state.disabled_fill), )); - let (track, inset, edge) = match kind { - ScrollBarKind::Overlay => { - let hover = style_color(c.state.hover); - (Stateful::new(clear).off(craft_theme::States::new(clear, hover, hover, clear)), 0.0, None) - } - ScrollBarKind::Docked => (Stateful::new(style_color(c.surface.chrome)), tokens.space.gap.tight, Some(rule_look(tokens, RuleKind::Edge))), - }; - ScrollBarLook { kind, track, thumb, thickness: tokens.size.handle.sm, min_thumb: min_thumb(tokens), inset, edge, fade: tokens.size.scroll_fade } + let hover = style_color(c.state.hover); + let track = Stateful::new(clear).off(craft_theme::States::new(clear, hover, hover, clear)); + ScrollBarLook { + track, + thumb, + thickness: tokens.size.handle.sm, + rest: tokens.stroke.thick, + min_thumb: min_thumb(tokens), + fade: tokens.size.scroll_fade, + } } /// The reset dot's colours and diameter. diff --git a/crates/ui-contract/tests/it/paint/painters.rs b/crates/ui-contract/tests/it/paint/painters.rs index 4f9ffb5d1..6aee5e400 100644 --- a/crates/ui-contract/tests/it/paint/painters.rs +++ b/crates/ui-contract/tests/it/paint/painters.rs @@ -184,3 +184,43 @@ fn ant_edges_are_whole_device_pixels_at_a_fractional_scale() { assert!(lower.contains(&(s - width)), "the dash at {s} continues {width} pixels earlier one row down: {lower:?}"); } } + +/// 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] +fn a_resting_scroll_bar_is_a_thin_line_at_its_outer_edge_that_widens_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, 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()); + 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| { + let shapes = ScrollBar::shapes(&look, track, axis, rtl, &span, &StateSet::new(state, Flags::empty(), StateMix::REST)); + match shapes.last() { + Some(Shape::Rect(shape)) => shape.rect, + other => panic!("the thumb is a rect: {other:?}"), + } + }; + let (rest, hover) = (thumb(State::Rest), thumb(State::Hover)); + let across = |rect: Rect| if axis == Axis::X { rect.height() } else { rect.width() }; + assert!(across(rest) < across(hover), "{axis:?}: the resting thumb is thinner: {rest:?} and {hover:?}"); + assert!((across(hover) - across(track)).abs() < EPS, "{axis:?}: under the pointer the thumb fills the track"); + let outer = match (axis, rtl) { + (Axis::X, _) => (rest.bottom() - track.bottom()).abs(), + (_, true) => (rest.left() - track.left()).abs(), + _ => (rest.right() - track.right()).abs(), + }; + assert!(outer < EPS, "{axis:?} rtl {rtl}: the resting line lies along the outer edge: {rest:?} in {track:?}"); + let along = |rect: Rect| if axis == Axis::X { rect.x_range() } else { rect.y_range() }; + assert_eq!(along(rest), along(hover), "{axis:?}: the thumb keeps its place along the bar"); + } +}