diff --git a/crates/layers/src/ui/panel/row.rs b/crates/layers/src/ui/panel/row.rs index 62d274131..38e28c11d 100644 --- a/crates/layers/src/ui/panel/row.rs +++ b/crates/layers/src/ui/panel/row.rs @@ -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; diff --git a/crates/layers/tests/it/main.rs b/crates/layers/tests/it/main.rs index cc236b5a0..36c2b8fa5 100644 --- a/crates/layers/tests/it/main.rs +++ b/crates/layers/tests/it/main.rs @@ -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; diff --git a/crates/layers/tests/it/panel_eye.rs b/crates/layers/tests/it/panel_eye.rs new file mode 100644 index 000000000..54d74cbf1 --- /dev/null +++ b/crates/layers/tests/it/panel_eye.rs @@ -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"); +} diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index a4311c53b..6b64e1624 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -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 { + /// 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 { 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 { 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, + visible: Range, +} + +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, @@ -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, axis, rect| span.map(|span| Bar::new(rect, axis, span.scroll(overscroll), document_chrome)); + let bar = |span: Option, 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, } 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