mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
ui-canvas: restore comfortable vertical scrollbar thickness and right edge margin
This commit is contained in:
@@ -68,6 +68,7 @@ pub struct ScrollbarsStyle {
|
||||
metrics: Metrics,
|
||||
look: ScrollBarLook,
|
||||
document: Option<DocumentLook>,
|
||||
margin: f32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -257,9 +258,12 @@ impl Bars {
|
||||
held: Option<&HeldThumb>,
|
||||
) -> Bars {
|
||||
let document_chrome = style.document.is_some();
|
||||
let margin = style.margin;
|
||||
let mut width = style.look.thickness;
|
||||
if document_chrome {
|
||||
width = width.min(rect.width().max(0.0)).min(rect.height().max(0.0));
|
||||
} else {
|
||||
width = width.min((rect.width() - 2.0 * margin).max(0.0)).min((rect.height() - 2.0 * margin).max(0.0));
|
||||
}
|
||||
let x = Span::of(
|
||||
doc.width(),
|
||||
@@ -270,9 +274,15 @@ impl Bars {
|
||||
)
|
||||
.filter(|_| horizontal);
|
||||
let y = Span::of(doc.height(), view.center.y - doc.y0, f64::from(view.rect.height()), view.zoom, KEPT_IN_VIEW);
|
||||
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 corner = (document_chrome && x.is_some() && y.is_some()).then(|| Rect::from_min_max(pos2(right, bottom), rect.max));
|
||||
let v_left = rect.right() - margin - width;
|
||||
let v_right = rect.right() - margin;
|
||||
let v_top = rect.top() + margin;
|
||||
let v_bottom = if x.is_some() { rect.bottom() - 2.0 * margin - width } else { rect.bottom() - margin };
|
||||
let h_left = rect.left() + margin;
|
||||
let h_right = if y.is_some() { rect.right() - 2.0 * margin - width } else { rect.right() - margin };
|
||||
let h_top = rect.bottom() - margin - width;
|
||||
let h_bottom = rect.bottom() - margin;
|
||||
let corner = (document_chrome && x.is_some() && y.is_some()).then(|| Rect::from_min_max(pos2(v_left, h_top), rect.max));
|
||||
let bar = |span: Option<Span>, axis, rect| {
|
||||
span.map(|span| {
|
||||
let mut scroll = span.scroll(overscroll);
|
||||
@@ -283,8 +293,8 @@ impl Bars {
|
||||
})
|
||||
};
|
||||
Bars {
|
||||
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))),
|
||||
horizontal: bar(x, Axis::X, Rect::from_min_max(pos2(h_left, h_top), pos2(h_right, h_bottom))),
|
||||
vertical: bar(y, Axis::Y, Rect::from_min_max(pos2(v_left, v_top), pos2(v_right, v_bottom))),
|
||||
corner,
|
||||
}
|
||||
}
|
||||
@@ -437,17 +447,18 @@ impl Component for CanvasScrollbars<'_> {
|
||||
|
||||
fn style(cx: &StyleCx<'_>, key: CanvasScrollbarsStyleKey) -> ScrollbarsStyle {
|
||||
let mut look = cx.scroll_bar_of(ScrollBarKind::Docked);
|
||||
look.thickness = cx.tokens.size.icon.md;
|
||||
let margin = if key.document_chrome { 0.0 } else { cx.tokens.space.gap.tight };
|
||||
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 }
|
||||
ScrollbarsStyle { metrics: Metrics::new(), look, document, margin }
|
||||
}
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
Vec2::splat(cx.style.look.thickness)
|
||||
Vec2::splat(cx.style.look.thickness + cx.style.margin)
|
||||
}
|
||||
|
||||
fn bind(&mut self) -> Option<BindMut<'_, CanvasView>> {
|
||||
|
||||
@@ -120,6 +120,35 @@ fn document_bars_show_arrows_and_an_inset_thumb_for_a_fitting_image() {
|
||||
);
|
||||
}
|
||||
|
||||
/// #2980: docked canvas scrollbars (without document chrome) provide comfortable thickness
|
||||
/// (`size.icon.md` / 16 pt) and a breathing margin along the viewport edge.
|
||||
#[trial]
|
||||
fn docked_canvas_scrollbars_provide_comfortable_thickness_and_edge_margin() {
|
||||
let large = Size::new(4000.0, 3000.0);
|
||||
let view = Rc::new(Cell::new(CanvasView::new(DOCUMENT_WINDOW, Point::new(2000.0, 1500.0), 1.0)));
|
||||
let observed = Rc::clone(&view);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut next = observed.get();
|
||||
let doc = DocRect::from_origin_size(Point::ZERO, large);
|
||||
let _ = CanvasScrollbars::new("Scroll bars", Bind::bound(&mut next), doc).show_at(ui, DOCUMENT_WINDOW);
|
||||
observed.set(next);
|
||||
});
|
||||
fixture.room(vec2(760.0, 560.0)).settle();
|
||||
|
||||
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;
|
||||
|
||||
// Comfortable desktop thickness (16 pt in standard compact density, matching size.icon.md):
|
||||
assert!(vertical.width() >= 14.0, "vertical bar width should be comfortable (>= 14 pt): {vertical:?}");
|
||||
assert!(horizontal.height() >= 14.0, "horizontal bar height should be comfortable (>= 14 pt): {horizontal:?}");
|
||||
|
||||
// Breathing room along viewport edge (not merging directly into window border):
|
||||
assert!(vertical.right() < DOCUMENT_WINDOW.right(), "vertical bar has margin from right edge: {vertical:?} vs {DOCUMENT_WINDOW:?}");
|
||||
assert!(vertical.right() >= DOCUMENT_WINDOW.right() - 4.0, "vertical bar margin is subtle: {vertical:?}");
|
||||
assert!(horizontal.bottom() < DOCUMENT_WINDOW.bottom(), "horizontal bar has margin from bottom edge: {horizontal:?} vs {DOCUMENT_WINDOW:?}");
|
||||
assert!(horizontal.bottom() >= DOCUMENT_WINDOW.bottom() - 4.0, "horizontal bar margin is subtle: {horizontal:?}");
|
||||
}
|
||||
|
||||
/// #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() {
|
||||
|
||||
@@ -229,7 +229,10 @@ pub(crate) fn scroll_bar_look_of(tokens: &UiTokens, kind: ScrollBarKind) -> Scro
|
||||
)),
|
||||
};
|
||||
let hover = style_color(c.state.hover);
|
||||
let (thickness, thin) = (tokens.size.handle.sm, tokens.stroke.thick);
|
||||
let (thickness, thin) = match kind {
|
||||
ScrollBarKind::Floating => (tokens.size.handle.sm, tokens.stroke.thick),
|
||||
ScrollBarKind::Docked => (tokens.size.icon.md, tokens.size.icon.md),
|
||||
};
|
||||
let (track, rest) = match kind {
|
||||
ScrollBarKind::Floating => (Stateful::new(clear).off(craft_theme::States::new(clear, hover, hover, clear)), thin),
|
||||
ScrollBarKind::Docked => {
|
||||
|
||||
Reference in New Issue
Block a user