Canvas: Overscroll off centres a document smaller than the view; the wheel's modifiers carry a held Alt the wheel events miss (Windows); a part read in a run that stopped before the parts step (an egui sizing pass) reads idle

This commit is contained in:
aiongg
2026-11-13 18:44:56 +00:00
parent 9b957ec673
commit f759fce00f
7 changed files with 108 additions and 9 deletions
+10 -5
View File
@@ -133,8 +133,11 @@ fn moved(view: &CanvasView, doc: DocRect, axis: Axis, range: &Range<f64>) -> Can
next
}
/// Clamp one axis of the view centre with overscroll off, as Photoshop 25.4 does: a document larger than the view
/// can't leave its edges, and one smaller than the view stays inside it, where zooming put it.
/// Clamp one axis of the view centre with overscroll off: a document larger than the view can't leave its edges, and
/// one smaller than the view (or exactly its size) is centred in it, so zooming out around the pointer keeps it in the
/// middle. Photoshop 25.4 was measured leaving a smaller document where zooming put it; reports of Photoshop itself
/// say it stays centred with Overscroll off, which is what turning Overscroll off asks for. Overscroll on keeps the
/// free view.
fn clamp_axis(center: f64, len: f64, size: f64, zoom: f64) -> f64 {
if !(zoom.is_finite() && zoom > 0.0 && len.is_finite() && len > 0.0 && size.is_finite() && size > 0.0) {
return center;
@@ -143,8 +146,10 @@ fn clamp_axis(center: f64, len: f64, size: f64, zoom: f64) -> f64 {
return len / 2.0;
}
let half = size / zoom / 2.0;
let (a, b) = (half, len - half);
center.clamp(a.min(b), a.max(b))
if 2.0 * half >= len {
return len / 2.0;
}
center.clamp(half, len - half)
}
/// The four corners of a document rectangle.
@@ -180,7 +185,7 @@ impl<'a> CanvasScrollbars<'a> {
}
/// Applies overscroll off to `view` over the document `doc`: the view can't leave the document's edges, and a
/// document smaller than the view stays inside it. True when the view moved (then repaint).
/// document smaller than the view is centred in it. True when the view moved (then repaint).
pub fn clamp(view: &mut CanvasView, doc: DocRect) -> bool {
if !(doc.width() > 0.0 && doc.height() > 0.0) || !view.zoom.is_finite() || view.zoom <= 0.0 {
return false;
@@ -233,6 +233,33 @@ fn a_wheel_line_pans_the_canvas_photoshops_line_and_ctrl_alt_zooms_as_alt_does()
}
}
#[trial]
fn a_held_alt_key_is_in_the_wheels_modifiers_when_the_wheel_events_miss_it() {
// Windows can report a held Alt in its key events and not in the wheel's modifier state.
let alt_key = |pressed: bool, repeat: bool| egui::Event::Key {
key: Key::AltLeft,
physical_key: Some(Key::AltLeft),
pressed,
repeat,
modifiers: Modifiers::NONE,
};
let (mut fixture, view, _, _) = wheel_rig(Platform::Windows);
fixture.frame(vec![alt_key(true, false)]);
let before = view.get();
wheel_line(&mut fixture, &view, 1.0, Modifiers::NONE);
assert!((view.get().zoom / before.zoom - WheelNavigation::NOTCH).abs() < 1e-3, "Alt held from its press: the wheel zooms");
// A pass whose first Alt event is a repeat: Alt was already down.
fixture.frame(vec![alt_key(true, true)]);
let held = view.get();
wheel_line(&mut fixture, &view, 1.0, Modifiers::NONE);
assert!(view.get().zoom > held.zoom, "Alt still held: the wheel zooms");
fixture.frame(vec![alt_key(false, false)]);
let released = view.get();
wheel_line(&mut fixture, &view, -1.0, Modifiers::NONE);
assert_eq!(view.get().zoom, released.zoom, "Alt released: a plain wheel line does not zoom");
assert_ne!(view.get().center, released.center, "it pans");
}
#[trial]
fn a_focused_field_takes_the_plain_wheel_over_the_canvas_and_leaves_it_the_view_keys() {
let (mut fixture, view, number, _) = wheel_rig(Platform::Linux);
@@ -305,3 +332,20 @@ fn a_wheel_zoom_keeps_the_point_under_the_pointer_where_an_app_widget_covers_the
assert!(after.zoom > before.zoom, "Alt + wheel zooms");
assert!(after.to_screen(under).distance(at) < 0.5, "the document point stays under the pointer: {before:?} -> {after:?}");
}
#[trial]
fn a_canvas_in_a_sizing_pass_of_its_parent_shows_nothing_and_reports_an_idle_press() {
// An egui window lays its contents out in a sizing pass in its first frame, as a document window in the app's
// main window does.
let idle = Rc::new(Cell::new(None));
let seen = Rc::clone(&idle);
let mut fixture = Fixture::new(move |ui| {
ui.scope_builder(egui::UiBuilder::new().sizing_pass(), |ui| {
let given = CanvasView::new(ui.available_rect_before_wrap(), Point::new(50.0, 50.0), 1.0);
let canvas = Canvas::new("Document").document(Size::new(100.0, 100.0)).view(given).show(ui, |_, _| {});
seen.set(Some(canvas.output.press.phase.start().is_none()));
});
});
fixture.settle();
assert_eq!(idle.get(), Some(true), "the canvas ran and its press is idle");
}
@@ -25,3 +25,26 @@ fn a_rotated_view_is_clamped_by_the_document_as_it_is_shown() {
let shown = quad.iter().skip(1).fold(Rect::from_min_max(quad[0], quad[0]), |rect, &corner| rect.union(Rect::from_min_max(corner, corner)));
assert!(shown.expand(TOLERANCE).contains_rect(VIEW), "the turned document covers the view: {shown:?}");
}
#[trial]
fn overscroll_off_below_fit_keeps_the_image_centred() {
let shown = |view: &CanvasView, doc: DocRect| {
let quad = view.quad(doc);
quad.iter().skip(1).fold(Rect::from_min_max(quad[0], quad[0]), |rect, &corner| rect.union(Rect::from_min_max(corner, corner)))
};
// Narrower and shorter than the view on both axes, zoomed out off centre, level and turned.
let small = DocRect::new(0.0, 0.0, 200.0, 100.0);
for rotation in [0.0, 30.0] {
let mut view = view_at(Point::new(-60.0, 140.0), 1.0).rotation(rotation);
assert!(CanvasScrollbars::clamp(&mut view, small));
let image = shown(&view, small);
assert!((image.center() - VIEW.center()).length() < TOLERANCE, "rotation {rotation}: {image:?} is centred in {VIEW:?}");
}
// Wider than the view but shorter: the wide axis keeps its edge, the short one is centred.
let wide = DocRect::new(0.0, 0.0, 1000.0, 100.0);
let mut view = view_at(Point::new(-60.0, 140.0), 1.0);
assert!(CanvasScrollbars::clamp(&mut view, wide));
let image = shown(&view, wide);
assert!((image.left() - VIEW.left()).abs() < TOLERANCE, "the wide axis stops at its edge: {image:?}");
assert!((image.center().y - VIEW.center().y).abs() < TOLERANCE, "the short axis is centred: {image:?}");
}
@@ -87,6 +87,10 @@ pub struct CxCore<'a> {
pub(crate) state: StateSet,
/// True once step 12 wrote `state` in this pass.
pub(crate) stated: bool,
/// True once the run reached step 7. A measuring run, a sizing pass of the parent or host `Ui`, and a host that
/// shows nothing (a layer in its parent's sizing pass) stop before it: their core is blank and every part reads
/// idle.
pub(crate) parted: bool,
/// Read with `Styled::ink`, valid from step 12.
pub(crate) ink: Ink,
/// An owned copy of the intents routed to this id.
@@ -68,9 +68,9 @@ impl Pointer for CxCore<'_> {
match self.results.get(&(part.key, part.index)) {
Some(result) => result,
None => {
// A measuring run has no parts: its core is blank, so every part reads idle.
// A run that stopped before step 7 has no parts: its core is blank, so every part reads idle.
debug_assert!(
self.face_mode || crate::runtime::measure::active(&self.ctx) || self.results.is_empty() && self.stated,
self.face_mode || !self.parted || crate::runtime::measure::active(&self.ctx) || self.results.is_empty() && self.stated,
"`parts` did not declare the part {part:?}"
);
&PartResult::IDLE
+2
View File
@@ -358,6 +358,7 @@ fn blank_core<'a>(
command,
state: StateSet::default(),
stated: false,
parted: false,
ink,
intents: keys::Intents::default(),
memory,
@@ -866,6 +867,7 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
p.real = true;
let inspected = p.trace.recording() && inspect::open_element(ui, id, facts.name);
let e = &mut *p.p;
e.core().parted = true;
// 7 parts
p.trace.mark(Step::Parts);
let flow = Flow::new(rect, rtl);
+23 -2
View File
@@ -4,7 +4,7 @@ use craft_behavior::one_press::split_after_first_press;
use craft_behavior::{Buttons, PointerButton, PointerEvent, PointerKind, PointerSample, WheelPhase, WheelUnit};
use craft_host::target::Os;
use craft_keymap::{KeyInput, Mods};
use egui::{Context, Event, Id, MouseWheelUnit, Pos2, TouchPhase, Vec2, ViewportId};
use egui::{Context, Event, Id, Key, MouseWheelUnit, Pos2, TouchPhase, Vec2, ViewportId};
pub use egui::RawInput;
@@ -324,12 +324,32 @@ pub(crate) fn hit_targets(ctx: &Context) -> (Option<Id>, Option<Id>) {
/// The wheel input of the pass: the summed scroll in points (`line` and `page` points per line and page), the
/// last event's unit, phase and modifiers. `None` when no wheel event arrived.
///
/// A wheel event's modifiers are the windowing layer's modifier state, which on Windows can miss a held Alt while the
/// Alt key's own press and release still arrive: Alt counts as held from its key's press to its release, starting from
/// whether it was down before the pass's first Alt key event (egui's held keys when the pass has none).
pub(crate) fn wheel_read(ctx: &Context, line: f32, page: f32) -> Option<(Vec2, WheelUnit, WheelPhase, Mods)> {
let os = crate::runtime::windows::os_of(ctx);
let zoom_modifier = ctx.options(|options| options.input_options.zoom_modifier);
let is_alt = |key: &Key| matches!(key, Key::AltLeft | Key::AltRight);
ctx.input(|input| {
let mut read: Option<(Vec2, WheelUnit, WheelPhase, Mods)> = None;
// A repeat means the key was already down; a release means it was down until then.
let mut alt_key = input
.raw
.events
.iter()
.find_map(|event| match event {
Event::Key { key, pressed, repeat, .. } if is_alt(key) => Some(!*pressed || *repeat),
_ => None,
})
.unwrap_or_else(|| input.key_down(Key::AltLeft) || input.key_down(Key::AltRight));
for event in &input.raw.events {
if let Event::Key { key, pressed, .. } = event
&& is_alt(key)
{
alt_key = *pressed;
}
if let Event::MouseWheel { unit, delta, modifiers, phase, .. } = event {
let (unit, points) = match unit {
MouseWheelUnit::Point => (WheelUnit::Point, 1.0),
@@ -345,7 +365,8 @@ pub(crate) fn wheel_read(ctx: &Context, line: f32, page: f32) -> Option<(Vec2, W
// Wheel events under the zoom modifier zoom (`PointerFrame.zoom`) and never scroll.
let share = if modifiers.matches_any(zoom_modifier) { Vec2::ZERO } else { *delta * points };
let sum = read.map_or(Vec2::ZERO, |(sum, ..)| sum) + share;
read = Some((sum, unit, phase, Mods::from_egui(modifiers, os)));
let held = egui::Modifiers { alt: modifiers.alt || alt_key, ..*modifiers };
read = Some((sum, unit, phase, Mods::from_egui(&held, os)));
}
}
read