mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
Merge remote-tracking branch 'origin/next' into sync/next
# Conflicts: # crates/layers/src/ui/panel/row.rs
This commit is contained in:
@@ -266,7 +266,8 @@ fn layer_row<'r, T: LayerTree>(cx: &'r PanelContext<'r, T>, id: &T::Id) -> ListR
|
||||
LayerView::Template => "layer_template",
|
||||
};
|
||||
let face = ToggleFace::icons(IconId::new(on), IconId::new("eye_off"));
|
||||
let mut eye = ToggleCell::new(EYE, face, ToggleState::of(info.visible), named(&words.visibility, &info.name));
|
||||
// A hidden layer's eye box stays empty, also under the pointer (the reference application, photocraft#16321).
|
||||
let mut eye = ToggleCell::new(EYE, face, ToggleState::of(info.visible), named(&words.visibility, &info.name)).empty_off();
|
||||
if !bar && let Some(color) = info.label {
|
||||
eye = eye.fill(DataColor::new(paint_color(color.fill())));
|
||||
}
|
||||
@@ -403,7 +404,7 @@ fn sub_row<'r, T: LayerTree>(cx: &'r PanelContext<'r, T>, id: &T::Id, group: Sub
|
||||
row = row.ink(RowInk::Dimmed);
|
||||
}
|
||||
if sub.eye_shown {
|
||||
row = row.toggle(ToggleCell::eye(EYE, sub.on).name(named(&words.visibility, &sub.label)));
|
||||
row = row.toggle(ToggleCell::eye(EYE, sub.on).empty_off().name(named(&words.visibility, &sub.label)));
|
||||
}
|
||||
let size = cx.options.thumbnail_size;
|
||||
let header_mask = group == SubRowGroup::SmartFilters && index.is_none() && sub.mask && size != ThumbSize::None;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
pub(crate) mod blend_rows;
|
||||
pub(crate) mod comps;
|
||||
pub(crate) mod panel_drag_menu;
|
||||
pub(crate) mod panel_eye;
|
||||
pub(crate) mod panel_filter;
|
||||
pub(crate) mod panel_input;
|
||||
pub(crate) mod panel_memo;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
//! `LayersPanel` eye boxes: a hidden layer's box stays empty, also under the pointer (photocraft#16321).
|
||||
|
||||
use craft_layers::ui::mock::FakeLayers;
|
||||
use craft_layers::ui::panel::LayersPanel;
|
||||
use craft_layers::ui::tree::{LayerInfo, LayerKind};
|
||||
use craft_ui::contract::Tracked;
|
||||
use craft_ui::testing::{Fixture, pointer, trial};
|
||||
use egui::{Rect, Vec2};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A visible layer over a hidden one.
|
||||
fn rig() -> Fixture {
|
||||
let mut tree = FakeLayers::default();
|
||||
tree.add(None, LayerInfo::new("Shown", LayerKind::Pixel), false);
|
||||
let mut hidden = LayerInfo::new("Hidden", LayerKind::Pixel);
|
||||
hidden.visible = false;
|
||||
tree.add(None, hidden, false);
|
||||
let tree = Rc::new(RefCell::new(Tracked::new(tree)));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let tree = tree.borrow();
|
||||
let _ = LayersPanel::new(&tree).show(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(420.0, 560.0));
|
||||
fixture.settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
/// How many shapes the frame paints wholly inside the eye box `cell`: its icon, not the row's fill around it.
|
||||
fn drawn_in(fixture: &mut Fixture, cell: Rect) -> usize {
|
||||
fixture.frame_shapes().iter().filter(|shape| cell.contains_rect(shape.visual_bounding_rect())).count()
|
||||
}
|
||||
|
||||
/// The pointer over a hidden layer's eye box draws nothing there (no crossed-out eye), while a visible layer's box
|
||||
/// under the pointer draws its eye.
|
||||
#[trial]
|
||||
fn a_hidden_layers_eye_box_stays_empty_under_the_pointer() {
|
||||
let mut fixture = rig();
|
||||
let shown = fixture.by_label("Visibility Shown").unwrap_or_else(|| panic!("no eye box for Shown")).rect;
|
||||
let hidden = fixture.by_label("Visibility Hidden").unwrap_or_else(|| panic!("no eye box for Hidden")).rect;
|
||||
|
||||
fixture.frame(vec![pointer::moved(shown.center())]).idle(2);
|
||||
assert!(drawn_in(&mut fixture, shown) > 0, "the visible layer's eye is drawn");
|
||||
|
||||
fixture.frame(vec![pointer::moved(hidden.center())]).idle(2);
|
||||
assert_eq!(drawn_in(&mut fixture, hidden), 0, "the hidden layer's box stays empty under the pointer");
|
||||
}
|
||||
@@ -3,9 +3,13 @@
|
||||
//! 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
|
||||
//! ([`CanvasScrollbars::clamp`]).
|
||||
//! the track to page by one screenful. With overscroll on, the extent also takes in the pasteboard around the document,
|
||||
//! as in the reference application: at either end of a track [`KEPT_IN_VIEW`] points of the document stay in view at
|
||||
//! the opposite edge, whether the document is larger or smaller than the window. With overscroll off the app clamps the
|
||||
//! view before it draws ([`CanvasScrollbars::clamp`]).
|
||||
//!
|
||||
//! A thumb drag keeps the extent it began with and follows the pointer from the press: the thumb stays under the
|
||||
//! pointer, also after the pointer went past a track's end and came back.
|
||||
//!
|
||||
//! The bars measure the view along the screen axes, so on a rotated view they show the extent of the unrotated
|
||||
//! document.
|
||||
@@ -44,6 +48,10 @@ const ARROW_STEP_PIXELS: f64 = 16.0;
|
||||
const ARROW_HOLD_DELAY: f64 = 0.4;
|
||||
const ARROW_HOLD_INTERVAL: f64 = 0.06;
|
||||
|
||||
/// With overscroll on, how much of the document stays in view, in screen points, when a thumb is at either end of its
|
||||
/// track: the reference application keeps 100 screen pixels at every zoom, on both axes.
|
||||
const KEPT_IN_VIEW: f64 = 100.0;
|
||||
|
||||
/// The popup of the tip beside a dragged vertical thumb.
|
||||
const TIP: &str = "tip";
|
||||
|
||||
@@ -76,26 +84,30 @@ struct Span {
|
||||
/// First and last visible coordinate.
|
||||
v0: f64,
|
||||
v1: f64,
|
||||
/// [`KEPT_IN_VIEW`] in document units on this axis.
|
||||
kept: f64,
|
||||
}
|
||||
|
||||
impl Span {
|
||||
/// The axis of a view: `size` screen points at `zoom` around `center`.
|
||||
fn of(len: f64, center: f64, size: f64, zoom: f64) -> Option<Span> {
|
||||
/// The axis of a view: `size` screen points at `zoom` around `center`; `kept` is [`KEPT_IN_VIEW`] in the same
|
||||
/// points (the horizontal axis divides both by the pixel aspect).
|
||||
fn of(len: f64, center: f64, size: f64, zoom: f64, kept: f64) -> Option<Span> {
|
||||
let zoom = if zoom.is_finite() && zoom > 0.0 { zoom } else { return None };
|
||||
let half = size / zoom / 2.0;
|
||||
let span = Span { len, v0: center - half, v1: center + half };
|
||||
let span = Span { len, v0: center - half, v1: center + half, kept: kept / zoom };
|
||||
(span.len.is_finite() && span.len > 0.0 && span.v0.is_finite() && span.v1.is_finite() && span.v1 > span.v0).then_some(span)
|
||||
}
|
||||
|
||||
/// The scrollable extent: the document and the visible span. With `overscroll`, a document shorter than the span
|
||||
/// may slide edge to edge within it.
|
||||
/// The scrollable extent: the document and the visible span. With `overscroll` also the pasteboard on both sides,
|
||||
/// up to where only [`KEPT_IN_VIEW`] of the document (all of a shorter one) is left in view at the opposite edge.
|
||||
fn content(&self, overscroll: bool) -> Range<f64> {
|
||||
let (lo, hi) = (self.v0.min(0.0), self.v1.max(self.len));
|
||||
if !overscroll {
|
||||
return lo..hi;
|
||||
}
|
||||
let visible = self.v1 - self.v0;
|
||||
lo.min(self.len - visible)..hi.max(visible)
|
||||
let kept = if self.kept.is_finite() { self.kept.clamp(0.0, self.len) } else { self.len };
|
||||
let room = (self.v1 - self.v0 - kept).max(0.0);
|
||||
lo.min(-room)..hi.max(self.len + room)
|
||||
}
|
||||
|
||||
fn scroll(&self, overscroll: bool) -> ScrollSpan {
|
||||
@@ -142,6 +154,26 @@ impl Bar {
|
||||
}
|
||||
}
|
||||
|
||||
/// A pressed thumb: the extent and the visible range it was pressed at. Its drag maps the pointer's movement from the
|
||||
/// press onto that extent, so the track does not grow or shrink under the pointer while the view moves, and the thumb
|
||||
/// stays under the pointer.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
struct HeldThumb {
|
||||
horizontal: bool,
|
||||
content: Range<f64>,
|
||||
visible: Range<f64>,
|
||||
}
|
||||
|
||||
impl HeldThumb {
|
||||
fn of(axis: Axis, span: &ScrollSpan) -> Self {
|
||||
HeldThumb { horizontal: axis == Axis::X, content: span.content.clone(), visible: span.visible.clone() }
|
||||
}
|
||||
|
||||
fn on(&self, axis: Axis) -> bool {
|
||||
self.horizontal == (axis == Axis::X)
|
||||
}
|
||||
}
|
||||
|
||||
/// The bars of a view over a document, in the order horizontal, vertical.
|
||||
struct Bars {
|
||||
horizontal: Option<Bar>,
|
||||
@@ -198,6 +230,11 @@ fn paint_arrow(draw: &Draw<'_>, rect: Rect, axis: Axis, after: bool, color: Styl
|
||||
draw.stroke(&path, StrokeWidth::Thin, color);
|
||||
}
|
||||
|
||||
/// The length of a scroll bar's track along `axis`.
|
||||
fn scroll_track_length(track: Rect, axis: Axis) -> f32 {
|
||||
if axis == Axis::X { track.width() } else { track.height() }
|
||||
}
|
||||
|
||||
/// How the document's x axis runs on screen: with the view flipped, backwards.
|
||||
fn along_x(view: &CanvasView, width: f64, c: f64) -> f64 {
|
||||
if view.flip_x { width - c } else { c }
|
||||
@@ -208,20 +245,43 @@ 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, style: &ScrollbarsStyle) -> Bars {
|
||||
/// The bars inside `rect` (bottom and right edges), `width` thick, for `view` over the document `doc`. A `held`
|
||||
/// thumb's bar keeps the extent the thumb was pressed at.
|
||||
fn of(
|
||||
view: &CanvasView,
|
||||
doc: DocRect,
|
||||
overscroll: bool,
|
||||
horizontal: bool,
|
||||
rect: Rect,
|
||||
style: &ScrollbarsStyle,
|
||||
held: Option<&HeldThumb>,
|
||||
) -> 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 x = Span::of(
|
||||
doc.width(),
|
||||
along_x(view, doc.width(), view.center.x - doc.x0),
|
||||
f64::from(view.rect.width()) / aspect(view),
|
||||
view.zoom,
|
||||
KEPT_IN_VIEW / aspect(view),
|
||||
)
|
||||
.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 bar = |span: Option<Span>, axis, rect| span.map(|span| Bar::new(rect, axis, span.scroll(overscroll), document_chrome));
|
||||
let bar = |span: Option<Span>, axis, rect| {
|
||||
span.map(|span| {
|
||||
let mut scroll = span.scroll(overscroll);
|
||||
if let Some(held) = held.filter(|held| held.on(axis)) {
|
||||
scroll.content = held.content.start.min(scroll.visible.start)..held.content.end.max(scroll.visible.end);
|
||||
}
|
||||
Bar::new(rect, axis, scroll, document_chrome)
|
||||
})
|
||||
};
|
||||
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))),
|
||||
@@ -298,13 +358,24 @@ pub struct CanvasScrollbars<'a> {
|
||||
/// thumb on the reading-start side.
|
||||
#[prop]
|
||||
tip: Option<&'a dyn Fn(&CanvasView) -> Label>,
|
||||
/// The thumb pressed in this pass, as `update` read it from the press slot, for `paint` and `access`.
|
||||
held: Option<HeldThumb>,
|
||||
}
|
||||
|
||||
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, document_chrome: false, tip: None }
|
||||
.label(label)
|
||||
CanvasScrollbars {
|
||||
common: Common::new(),
|
||||
view,
|
||||
document,
|
||||
overscroll: false,
|
||||
vertical_only: false,
|
||||
document_chrome: false,
|
||||
tip: None,
|
||||
held: None,
|
||||
}
|
||||
.label(label)
|
||||
}
|
||||
|
||||
/// Applies overscroll off to `view` over the document `doc`: the view can't leave the document's edges, and a
|
||||
@@ -353,8 +424,8 @@ impl<'a> CanvasScrollbars<'a> {
|
||||
changed
|
||||
}
|
||||
|
||||
fn bars(&self, view: &CanvasView, rect: Rect, style: &ScrollbarsStyle) -> Bars {
|
||||
Bars::of(view, self.document, self.overscroll, !self.vertical_only, rect, style)
|
||||
fn bars(&self, view: &CanvasView, rect: Rect, style: &ScrollbarsStyle, held: Option<&HeldThumb>) -> Bars {
|
||||
Bars::of(view, self.document, self.overscroll, !self.vertical_only, rect, style, held)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,7 +471,8 @@ 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);
|
||||
let held = cx.press_slot::<HeldThumb>();
|
||||
let bars = self.bars(&view, rect, cx.style, held.as_ref());
|
||||
for (key, axis, bar) in bars.each() {
|
||||
cx.scroll(key, bar.track, axis, bar.span.clone());
|
||||
for (part, rect, _) in bar.arrows(key, axis) {
|
||||
@@ -424,7 +496,10 @@ impl Component for CanvasScrollbars<'_> {
|
||||
tip.close(cx);
|
||||
}
|
||||
let Some(view) = cx.value().copied() else { return };
|
||||
let bars = self.bars(&view, cx.rect, cx.style);
|
||||
let held = cx.press_slot::<HeldThumb>().clone();
|
||||
let bars = self.bars(&view, cx.rect, cx.style, held.as_ref());
|
||||
let rtl = cx.flow.is_rtl();
|
||||
let min_thumb = cx.style.look.min_thumb;
|
||||
for (key, axis, bar) in bars.each() {
|
||||
for (part, rect, after) in bar.arrows(key, axis) {
|
||||
if arrow_pressed(cx, part, rect) {
|
||||
@@ -441,18 +516,39 @@ impl Component for CanvasScrollbars<'_> {
|
||||
}
|
||||
let phase = cx.part(thumb_of(key)).press.phase;
|
||||
match phase {
|
||||
// The press keeps the extent the thumb was pressed at for its drag.
|
||||
PressPhase::Down { .. } => *cx.press_slot::<HeldThumb>() = Some(HeldThumb::of(axis, &bar.span)),
|
||||
PressPhase::DragStart { .. } => cx.begin(),
|
||||
PressPhase::Release { click: false, .. } => cx.commit(),
|
||||
_ => {}
|
||||
}
|
||||
let Some(range) = cx.scroll_moved(key) else { continue };
|
||||
let range = match phase {
|
||||
// The thumb follows the pointer from the press, over the extent it was pressed at, so it stays under the
|
||||
// pointer: past a track's end the view waits until the pointer comes back to the thumb.
|
||||
PressPhase::DragStart { start, pos, .. } | PressPhase::Drag { start, pos, .. } => {
|
||||
let Some(held) = held.as_ref().filter(|held| held.on(axis)) else { continue };
|
||||
let moved_by = pos - start.origin;
|
||||
let along = match axis {
|
||||
Axis::X if rtl => -moved_by.x,
|
||||
Axis::X => moved_by.x,
|
||||
_ => moved_by.y,
|
||||
};
|
||||
let pressed = ScrollSpan::new(held.content.clone(), held.visible.clone());
|
||||
craft_behavior::scroll::drag(&pressed, scroll_track_length(bar.track, axis), min_thumb, along)
|
||||
}
|
||||
_ => {
|
||||
let Some(range) = cx.scroll_moved(key) else { continue };
|
||||
range
|
||||
}
|
||||
};
|
||||
let next = moved(&view, self.document, axis, &range);
|
||||
if matches!(phase, PressPhase::Drag { .. }) {
|
||||
if phase.is_drag() {
|
||||
cx.propose(next);
|
||||
} else if next != view {
|
||||
cx.set(next);
|
||||
}
|
||||
}
|
||||
self.held = held;
|
||||
}
|
||||
|
||||
/// The tip beside the dragged vertical thumb, on the reading-start side, in a tooltip layer of the bars' own: the
|
||||
@@ -471,7 +567,7 @@ 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);
|
||||
let bars = self.bars(&view, cx.rect, style, self.held.as_ref());
|
||||
let draw = ui.draw();
|
||||
let rtl = cx.flow.is_rtl();
|
||||
let fill = |rect, color| draw.fill(&PaintShape::Rect(rect, CornerRadius::ZERO), color);
|
||||
@@ -498,7 +594,7 @@ impl Component for CanvasScrollbars<'_> {
|
||||
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);
|
||||
let bars = self.bars(&view, cx.rect, cx.style, self.held.as_ref());
|
||||
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);
|
||||
|
||||
@@ -334,3 +334,113 @@ fn a_thumb_drag_follows_every_pointer_move() {
|
||||
assert_eq!(view.get().center.y, before.y, "chrome {chrome}: a horizontal drag keeps the vertical position");
|
||||
}
|
||||
}
|
||||
|
||||
/// Document scroll bars over a `document` centred in the window at `zoom`, with the canvas and the bars following
|
||||
/// `overscroll`.
|
||||
fn centred_scrollbars(document: Size, zoom: f64, overscroll: bool) -> (Fixture, Rc<Cell<CanvasView>>) {
|
||||
let view = Rc::new(Cell::new(CanvasView::new(DOCUMENT_WINDOW, Point::new(document.width / 2.0, document.height / 2.0), zoom)));
|
||||
let observed = Rc::clone(&view);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut next = observed.get();
|
||||
let mut navigation = NavigationOptions::default();
|
||||
navigation.overscroll = overscroll;
|
||||
Canvas::new("Document").document(document).view(next).navigation(navigation).show_at(ui, DOCUMENT_WINDOW, |_, body| {
|
||||
if let Some(space) = canvas_space(&body.env) {
|
||||
next = space.view;
|
||||
}
|
||||
});
|
||||
let doc = DocRect::from_origin_size(Point::ZERO, document);
|
||||
let _ =
|
||||
CanvasScrollbars::new("Scroll bars", Bind::bound(&mut next), doc).overscroll(overscroll).document_chrome().show_at(ui, DOCUMENT_WINDOW);
|
||||
observed.set(next);
|
||||
});
|
||||
fixture.room(vec2(760.0, 560.0)).settle();
|
||||
(fixture, view)
|
||||
}
|
||||
|
||||
/// Presses the middle of the track `label` (where a centred view's thumb is), drags by `by` and lets go.
|
||||
fn drag_thumb(fixture: &mut Fixture, label: &str, by: Vec2) {
|
||||
let track = fixture.by_label(label).expect("the track").rect;
|
||||
let start = track.center();
|
||||
fixture.frame(vec![pointer::moved(start)]).frame(vec![pointer::press(start)]);
|
||||
for step in 1..=20 {
|
||||
fixture.frame(vec![pointer::moved(start + by * (step as f32 / 20.0))]);
|
||||
}
|
||||
fixture.frame(vec![pointer::release(start + by)]).settle();
|
||||
}
|
||||
|
||||
/// The first and last document coordinate the view shows along `axis`.
|
||||
fn shown_range(view: &CanvasView, axis: Axis) -> (f64, f64) {
|
||||
let (center, size) = match axis {
|
||||
Axis::X => (view.center.x, f64::from(view.rect.width())),
|
||||
_ => (view.center.y, f64::from(view.rect.height())),
|
||||
};
|
||||
let half = size / view.zoom / 2.0;
|
||||
(center - half, center + half)
|
||||
}
|
||||
|
||||
/// With Overscroll on, a thumb at either end of its track leaves 100 screen points of the document in view at the
|
||||
/// opposite edge, as the reference application does, at any zoom, for documents larger and smaller than the window.
|
||||
/// With Overscroll off the ends stay the document's own edges.
|
||||
#[trial]
|
||||
fn a_thumb_at_either_end_leaves_100_points_of_the_document_in_view_with_overscroll() {
|
||||
for (document, zoom) in
|
||||
[(Size::new(4000.0, 3000.0), 1.0), (Size::new(4000.0, 3000.0), 2.0), (Size::new(4000.0, 3000.0), 0.5), (Size::new(300.0, 200.0), 1.0)]
|
||||
{
|
||||
for (label, axis, toward_start, toward_end) in [
|
||||
("Horizontal scroll bar", Axis::X, vec2(-3000.0, 0.0), vec2(3000.0, 0.0)),
|
||||
("Vertical scroll bar", Axis::Y, vec2(0.0, -3000.0), vec2(0.0, 3000.0)),
|
||||
] {
|
||||
let len = if axis == Axis::X { document.width } else { document.height };
|
||||
let kept = 100.0 / zoom;
|
||||
let (mut fixture, view) = centred_scrollbars(document, zoom, true);
|
||||
drag_thumb(&mut fixture, label, toward_start);
|
||||
let (_, last) = shown_range(&view.get(), axis);
|
||||
assert!((last - kept).abs() < 1.0, "{document:?} at {zoom}, {axis:?} start: the first {kept} units stay in view, shows up to {last}");
|
||||
let (mut fixture, view) = centred_scrollbars(document, zoom, true);
|
||||
drag_thumb(&mut fixture, label, toward_end);
|
||||
let (first, _) = shown_range(&view.get(), axis);
|
||||
assert!(
|
||||
(first - (len - kept)).abs() < 1.0,
|
||||
"{document:?} at {zoom}, {axis:?} end: the last {kept} units stay in view, shows from {first}"
|
||||
);
|
||||
}
|
||||
}
|
||||
let large = Size::new(4000.0, 3000.0);
|
||||
let (mut fixture, view) = centred_scrollbars(large, 1.0, false);
|
||||
drag_thumb(&mut fixture, "Horizontal scroll bar", vec2(-3000.0, 0.0));
|
||||
assert!(shown_range(&view.get(), Axis::X).0.abs() < 1.0, "Overscroll off stops at the document's left edge: {:?}", view.get());
|
||||
let (mut fixture, view) = centred_scrollbars(large, 1.0, false);
|
||||
drag_thumb(&mut fixture, "Horizontal scroll bar", vec2(3000.0, 0.0));
|
||||
assert!((shown_range(&view.get(), Axis::X).1 - large.width).abs() < 1.0, "and at its right edge: {:?}", view.get());
|
||||
}
|
||||
|
||||
/// A thumb dragged past the end of its track and back in the same drag moves again once the pointer is back over it,
|
||||
/// and lands where a drag straight to that point puts it: the thumb stays under the pointer, as in the reference
|
||||
/// application.
|
||||
#[trial]
|
||||
fn a_thumb_dragged_past_the_end_and_back_follows_the_pointer_in_the_same_drag() {
|
||||
let large = Size::new(4000.0, 3000.0);
|
||||
let (mut fixture, straight) = centred_scrollbars(large, 1.0, true);
|
||||
drag_thumb(&mut fixture, "Horizontal scroll bar", vec2(60.0, 0.0));
|
||||
let straight = straight.get().center.x;
|
||||
|
||||
let (mut fixture, view) = centred_scrollbars(large, 1.0, true);
|
||||
let track = fixture.by_label("Horizontal scroll bar").expect("the track").rect;
|
||||
let start = track.center();
|
||||
fixture.frame(vec![pointer::moved(start)]).frame(vec![pointer::press(start)]);
|
||||
let mut seen = Vec::new();
|
||||
for step in 1..=30 {
|
||||
fixture.frame(vec![pointer::moved(start + vec2(40.0 * step as f32, 0.0))]);
|
||||
seen.push(view.get().center.x);
|
||||
}
|
||||
let at_end = view.get().center.x;
|
||||
let mut back = Vec::new();
|
||||
for step in (0..30).rev() {
|
||||
fixture.frame(vec![pointer::moved(start + vec2(40.0 * step as f32 + 60.0, 0.0))]);
|
||||
back.push(view.get().center.x);
|
||||
}
|
||||
fixture.frame(vec![pointer::release(start + vec2(60.0, 0.0))]).settle();
|
||||
assert!(back.iter().any(|&x| x < at_end - 1.0), "the drag moves back: past the end {seen:?}, back {back:?}");
|
||||
assert!((view.get().center.x - straight).abs() < 1.0, "back where a straight drag puts it: {} vs {straight}", view.get().center.x);
|
||||
}
|
||||
|
||||
@@ -563,12 +563,17 @@ pub fn takes_letters(keys: &KeyUse) -> bool {
|
||||
}
|
||||
|
||||
/// A registered menu bar takes the keyboard (F10, a lone Alt): focus moves to `target`, a stop of the bar, in this
|
||||
/// pass, and the stop that held it is remembered for [`give_back_bar`]. Focus already inside the bar leaves the
|
||||
/// remembered stop as it is.
|
||||
/// pass, and the stop that held it is remembered for [`give_back_bar`]. With nothing focused, that is the window's
|
||||
/// document canvas, the key target then: a shortcut pressed on the bar (Space, the canvas's momentary Hand) matches
|
||||
/// there and gives the keyboard back to it, as Escape does. Focus already inside the bar leaves the remembered stop
|
||||
/// as it is.
|
||||
#[doc(hidden)]
|
||||
pub fn take_for_bar(ctx: &Context, bar: Id, target: FocusTarget) {
|
||||
let held = focused_stop(ctx).filter(|(id, _)| *id != bar);
|
||||
let in_bar = held.is_none() && is_focused(ctx, bar);
|
||||
with(ctx, |state| {
|
||||
let canvas = || state.stops.iter().chain(&state.last_stops).find(|rec| rec.stop.keys.chrome_key).map(|rec| rec.stop.key());
|
||||
let held = held.or_else(|| (!in_bar).then(canvas).flatten());
|
||||
if held.is_some() || state.bar_return.is_none() {
|
||||
state.bar_return = held.map(|held| (bar, held));
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ impl ListRow<'_> {
|
||||
ToggleFace::Icons { on, off } => {
|
||||
let shown = match toggle.state {
|
||||
ToggleState::On | ToggleState::Mixed => Some(on),
|
||||
ToggleState::Off if toggle.always_visible || cx.part(part).hovered => Some(off),
|
||||
ToggleState::Off if toggle.shows_off(cx.part(part).hovered) => Some(off),
|
||||
ToggleState::Off => None,
|
||||
};
|
||||
if let Some(id) = shown {
|
||||
|
||||
@@ -80,6 +80,9 @@ pub struct ToggleCell {
|
||||
pub fill: Option<DataColor>,
|
||||
/// The off icon shows in every row, not only under the pointer. No effect on a check face.
|
||||
pub always_visible: bool,
|
||||
/// The off icon never shows, also under the pointer: an off cell stays empty (a hidden layer's eye box). Wins
|
||||
/// over `always_visible`. No effect on a check face.
|
||||
pub empty_off: bool,
|
||||
/// A screen reader announces a check box instead of a toggle button: for a setting the row carries rather than
|
||||
/// an action on the row.
|
||||
pub check_box: bool,
|
||||
@@ -99,6 +102,7 @@ impl ToggleCell {
|
||||
tooltip: None,
|
||||
fill: None,
|
||||
always_visible: false,
|
||||
empty_off: false,
|
||||
check_box: false,
|
||||
row_press: false,
|
||||
}
|
||||
@@ -162,10 +166,37 @@ impl ToggleCell {
|
||||
self
|
||||
}
|
||||
|
||||
/// The off icon never shows, also under the pointer: an off cell stays an empty box.
|
||||
#[must_use]
|
||||
pub fn empty_off(mut self) -> Self {
|
||||
self.empty_off = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// A press on the row's own area shows on the cell, for a row whose click turns on what the cell shows.
|
||||
#[must_use]
|
||||
pub fn row_press(mut self) -> Self {
|
||||
self.row_press = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether an off cell draws its off icon, under the pointer when `hovered`.
|
||||
pub(super) fn shows_off(&self, hovered: bool) -> bool {
|
||||
!self.empty_off && (self.always_visible || hovered)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn an_empty_off_cell_never_draws_its_off_icon() {
|
||||
let eye = ToggleCell::eye(ColumnKey::new("eye"), false);
|
||||
assert!(!eye.shows_off(false) && eye.shows_off(true), "a plain off cell shows its off icon only under the pointer");
|
||||
assert!(eye.clone().always_visible().shows_off(false), "always_visible shows it in every row");
|
||||
let empty = eye.empty_off();
|
||||
assert!(!empty.shows_off(true), "an empty_off cell stays empty under the pointer");
|
||||
assert!(!empty.always_visible().shows_off(true), "empty_off wins over always_visible");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
//!
|
||||
//! Keys follow spec 9.7: Alt and a letter open the menu with that mnemonic (also while another is open). F10 or a
|
||||
//! lone Alt closes an open menu; with none open it moves focus to the first title, and the next F10 or lone Alt, an
|
||||
//! Escape, or running a row returns focus to where it was. A `&` in a title's words row marks its mnemonic; without
|
||||
//! Escape, a shortcut (Space, a canvas's momentary Hand, among them) or running a row returns focus to where it was.
|
||||
//! Down and Enter open the focused title's menu; Space opens none. A `&` in a title's words row marks its mnemonic; without
|
||||
//! one the title takes the first letter no earlier title holds. The mnemonic is underlined while Alt is held.
|
||||
//!
|
||||
//! The Help menu can start with a search field over a list of `HelpEntry` commands: typing lists up to `search::MAX_RESULTS`
|
||||
@@ -433,7 +434,10 @@ impl<'a> MenuBar<'a> {
|
||||
|
||||
impl Component for MenuBar<'_> {
|
||||
const ROLE: Role = Role::MENU_BAR;
|
||||
const KEYS: crate::runtime::focus::KeyUse = crate::runtime::focus::KeyUse::of(crate::contract::Input::Pointer).with_moves();
|
||||
// Enter and Down open the focused title's menu; Space does not. Space is a canvas's momentary Hand, so it goes on
|
||||
// to the app's shortcuts, and a shortcut pressed on the bar gives the keyboard back to where it was.
|
||||
const KEYS: crate::runtime::focus::KeyUse =
|
||||
crate::runtime::focus::KeyUse::of(crate::contract::Input::Pointer).with_moves().with_activate(crate::runtime::focus::ActivateKeys::Enter);
|
||||
type Style = MenuBarStyle;
|
||||
type Output = MenuBarOutput;
|
||||
|
||||
|
||||
@@ -429,3 +429,124 @@ fn holding_alt_runs_the_memoized_view_of_a_menu_bar() {
|
||||
fixture.hold(Modifiers::NONE);
|
||||
assert!(frame(&mut fixture) > 0, "Alt coming up runs the view, which drops the underlines");
|
||||
}
|
||||
|
||||
/// What a window's own Space does: the canvas's momentary Hand, say.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, craft_ui::contract::CommandSet)]
|
||||
#[command(prefix = "trialHand", scope = "trial.hand")]
|
||||
#[non_exhaustive]
|
||||
pub enum HandCommand {
|
||||
#[command(label = "Hand", default = "Space")]
|
||||
Hand,
|
||||
}
|
||||
|
||||
/// Space on the menu bar that F10 or a lone Alt focused opens no menu: the press goes on to the window's own Space
|
||||
/// command (a canvas's Hand), as in the reference application, where Space is always the Hand, and the shortcut gives
|
||||
/// the keyboard back to the stop the bar took it from. Enter and Down still open the focused title's menu.
|
||||
#[trial]
|
||||
fn space_on_the_focused_bar_opens_no_menu_and_reaches_the_window() {
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::ShowContainer as _;
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::runtime::commands::Commands as _;
|
||||
use craft_ui::runtime::focus::{FocusTarget, request};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
let (hands, clicks, button) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(0)), Rc::new(Cell::new(None)));
|
||||
let (seen_hands, seen_clicks, seen_button) = (Rc::clone(&hands), Rc::clone(&clicks), Rc::clone(&button));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let titles = vec![MenuTitle::new("&File", |ui| {
|
||||
MenuItem::new("&New").show(ui);
|
||||
})];
|
||||
MenuBar::new("Main menu", titles).show(ui);
|
||||
Stack::new().command_scope(<HandCommand as craft_ui::contract::CommandSet>::SCOPE).show(ui, |ui, cx| {
|
||||
while cx.take_command::<HandCommand>().is_some() {
|
||||
seen_hands.set(seen_hands.get() + 1);
|
||||
}
|
||||
let shown = Button::new("Layer").show(ui);
|
||||
if shown.clicked() {
|
||||
seen_clicks.set(seen_clicks.get() + 1);
|
||||
}
|
||||
seen_button.set(Some(shown.response.id));
|
||||
});
|
||||
});
|
||||
fixture.os(Os::Linux);
|
||||
fixture.settle();
|
||||
let layer = button.get().expect("the button ran");
|
||||
for (enter, alt) in [(None, false), (None, true), (Some(Key::Enter), false), (Some(Key::ArrowDown), false)] {
|
||||
request(fixture.ctx(), FocusTarget::Id(layer));
|
||||
fixture.settle();
|
||||
let before = (hands.get(), clicks.get());
|
||||
if alt {
|
||||
fixture.hold(Modifiers::ALT).settle();
|
||||
fixture.hold(Modifiers::NONE).settle();
|
||||
} else {
|
||||
fixture.key(Key::F10, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
}
|
||||
assert!(fixture.focused_stop().is_some_and(|(id, _)| id != layer), "the bar takes the keyboard (alt {alt})");
|
||||
if let Some(key) = enter {
|
||||
fixture.key(key, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("New").is_some(), "{key:?} opens the focused title's menu");
|
||||
assert_eq!((hands.get(), clicks.get()), before, "{key:?} reaches neither the window nor the button");
|
||||
fixture.key(Key::Escape, Modifiers::NONE);
|
||||
fixture.key(Key::Escape, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
continue;
|
||||
}
|
||||
fixture.key(Key::Space, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("New").is_none(), "Space opens no menu (alt {alt})");
|
||||
assert_eq!(fixture.focused_stop().map(|(id, _)| id), Some(layer), "Space takes the keyboard off the bar, back to the button (alt {alt})");
|
||||
assert_eq!(hands.get(), before.0 + 1, "the press reaches the window's Space command (alt {alt})");
|
||||
assert_eq!(clicks.get(), before.1, "and activates nothing else (alt {alt})");
|
||||
}
|
||||
}
|
||||
|
||||
/// With nothing focused the window's document canvas is the key target: a bar that F10 or a lone Alt focused then
|
||||
/// gives the keyboard back to the canvas, so Space, the canvas's Hand, matches there and leaves the bar in one press.
|
||||
#[trial]
|
||||
fn space_on_a_bar_focused_from_nothing_goes_to_the_document_canvas() {
|
||||
use craft_ui::contract::ShowContainer as _;
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::runtime::commands::Commands as _;
|
||||
use craft_ui::runtime::focus::{KeyUse, host_canvas};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
let canvas = egui::Id::unique("document canvas");
|
||||
let hands = Rc::new(Cell::new(0));
|
||||
let seen_hands = Rc::clone(&hands);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let titles = vec![MenuTitle::new("&File", |ui| {
|
||||
MenuItem::new("&New").show(ui);
|
||||
})];
|
||||
MenuBar::new("Main menu", titles).show(ui);
|
||||
Stack::new().command_scope(<HandCommand as craft_ui::contract::CommandSet>::SCOPE).show(ui, |ui, cx| {
|
||||
while cx.take_command::<HandCommand>().is_some() {
|
||||
seen_hands.set(seen_hands.get() + 1);
|
||||
}
|
||||
let rect = ui.available_rect_before_wrap();
|
||||
ui.interact(rect, canvas, egui::Sense::click());
|
||||
host_canvas(ui.ctx(), canvas, KeyUse::CANVAS);
|
||||
});
|
||||
});
|
||||
fixture.os(Os::Linux);
|
||||
fixture.settle();
|
||||
for alt in [false, true] {
|
||||
assert!(fixture.focused_stop().is_none_or(|(id, _)| id == canvas), "nothing but the canvas holds the keyboard (alt {alt})");
|
||||
if alt {
|
||||
fixture.hold(Modifiers::ALT).settle();
|
||||
fixture.hold(Modifiers::NONE).settle();
|
||||
} else {
|
||||
fixture.key(Key::F10, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
}
|
||||
assert!(fixture.focused_stop().is_some_and(|(id, _)| id != canvas), "the bar takes the keyboard (alt {alt})");
|
||||
let before = hands.get();
|
||||
fixture.key(Key::Space, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("New").is_none(), "Space opens no menu (alt {alt})");
|
||||
assert_eq!(hands.get(), before + 1, "Space reaches the canvas's command (alt {alt})");
|
||||
assert_eq!(fixture.focused_stop().map(|(id, _)| id), Some(canvas), "and the keyboard goes to the canvas (alt {alt})");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user