Merge pull request #3584 from dotCooCoo/fix/cursor-follows-zoom-off

ui-contract: with cursor_follows_zoom off, a pointer keeps its size at every interface zoom
This commit is contained in:
BFlat authored and GitHub committed 2026-11-26 10:01:18 +07:00
commit 7c5ec6a47d
5 files changed
+95 -9

No files matched your search

+3
View File
@@ -110,6 +110,9 @@ fn paint_pointer(ctx: &Context, env: &Env, painter: &egui::Painter) {
let closed = pointer::frame_raw(ctx).buttons.contains(craft_behavior::Buttons::PRIMARY);
let tip = matches!(cursor, PaintedCursor::Circle { .. } | PaintedCursor::Outline { .. });
let whole = pointer_art(env, &cursor, closed, false);
// A brush tip is canvas geometry at the canvas's zoom; any other pointer takes the interface zoom only where it
// follows it.
let whole = if tip { whole } else { whole.scaled(cursor::art_scale(ctx)) };
let confined = cursor::current_region(ctx).filter(|region| tip && !shows_whole(ctx, region, &whole, at));
let ring = |painter: &egui::Painter| {
let art = pointer_art(env, &cursor.clone().marked(false, None), closed, false);
@@ -37,6 +37,24 @@ pub(crate) enum Mark {
}
impl Mark {
/// The mark `factor` times its size about the hotspot, its lines included.
fn scaled(self, factor: f32) -> Self {
let line = |stroke: Stroke| Stroke::new(stroke.width * factor, stroke.color);
let rect = |rect: Rect| Rect::from_min_max((rect.min.to_vec2() * factor).to_pos2(), (rect.max.to_vec2() * factor).to_pos2());
let points = |points: Vec<Vec2>| points.into_iter().map(|p| p * factor).collect();
match self {
Mark::Line(a, b, stroke) => Mark::Line(a * factor, b * factor, line(stroke)),
Mark::Rule(a, b, stroke) => Mark::Rule(a * factor, b * factor, line(stroke)),
Mark::Ring(centre, radius, stroke) => Mark::Ring(centre * factor, radius * factor, line(stroke)),
Mark::Frame(r, stroke) => Mark::Frame(rect(r), line(stroke)),
Mark::Disc(centre, radius, color) => Mark::Disc(centre * factor, radius * factor, color),
Mark::Block(r, round, color) => Mark::Block(rect(r), round * factor, color),
Mark::Polygon(p, fill, stroke) => Mark::Polygon(points(p), fill, line(stroke)),
Mark::Path(p, closed, stroke) => Mark::Path(points(p), closed, line(stroke)),
Mark::Icon(r, icon, color) => Mark::Icon(rect(r), icon, color),
}
}
/// The rect the mark covers, its line included.
fn bounds(&self) -> Rect {
let around =
@@ -66,6 +84,14 @@ impl Art {
self.marks.push(mark);
}
/// The art `factor` times its size about the hotspot; the art unchanged for a factor of one.
pub(crate) fn scaled(self, factor: f32) -> Self {
if factor == 1.0 {
return self;
}
Art { marks: self.marks.into_iter().map(|mark| mark.scaled(factor)).collect() }
}
/// The rect the marks cover, around the hotspot at the origin; `None` for art with no marks.
pub(crate) fn bounds(&self) -> Option<Rect> {
self.marks.iter().map(Mark::bounds).reduce(|a, b| a.union(b))
+9 -7
View File
@@ -172,16 +172,18 @@ pub(crate) fn image_target(ctx: &Context) -> Option<ImageTarget> {
if !shell.cursor_images(native) {
return None;
}
// A host that keeps its cursors at their size ignores the interface zoom.
let follows_zoom = crate::runtime::env::Env::of_root(ctx).cursor_follows_zoom;
let scale = match (shell.os == Os::Mac, follows_zoom) {
(_, false) => 1.0,
(true, true) => zoom,
(false, true) => pixels_per_point,
};
let scale = if shell.os == Os::Mac { zoom } else { pixels_per_point };
Some(ImageTarget { scale, max_side: if shell.os == Os::Linux { MAX_SIDE_LINUX } else { MAX_SIDE } })
}
/// The factor a pointer's art is drawn at against its size in points: 1 where painted pointers follow the interface
/// zoom (`RootEnv::cursor_follows_zoom`), else one over the zoom, so the pointer keeps the size it has at zoom 1 on
/// the display.
pub(crate) fn art_scale(ctx: &Context) -> f32 {
let zoom = ctx.zoom_factor();
if crate::runtime::env::Env::of_root(ctx).cursor_follows_zoom || !zoom.is_finite() || zoom <= 0.0 { 1.0 } else { 1.0 / zoom }
}
/// A painted pointer the window can take as its cursor image: drawn at a scale, compared with the last one handed over.
pub(crate) trait CursorArt: Any + Clone + PartialEq + Send + Sync {
/// The image at `scale` device pixels per point, `None` where the art has none at that size.
+2 -2
View File
@@ -241,8 +241,8 @@ impl RootEnv {
self
}
/// Sets whether painted cursor images scale with the interface zoom (the Scale Cursor Proportional to UI
/// preference). On by default.
/// Sets whether painted pointers grow with the interface zoom (the Scale Cursor Proportional to UI preference).
/// Off, they keep the size they have at zoom 1; a brush tip follows the canvas either way. On by default.
pub fn cursor_follows_zoom(mut self, on: bool) -> Self {
self.cursor_follows_zoom = on;
self
@@ -4,6 +4,7 @@
use craft_behavior::{CountMark, Cursor, CursorBadge, CursorIcon, CursorPriority, IconId, PaintedCursor, PointerGlyph};
use craft_host::target::{Backend, Caps, Os, Runtime, ShellInfo};
use craft_ui::runtime::cursor;
use craft_ui::runtime::env::{RootEnv, Scale};
use craft_ui::testing::pointer::moved;
use craft_ui::testing::{Frames, install_spec, trial};
use egui::{CustomCursorImage, Pos2, Rect, Vec2, ViewportId};
@@ -437,3 +438,57 @@ fn a_tip_outline_shows_only_inside_its_region() {
let seen = painted(&mut frames, PaintedCursor::circle(RADIUS));
assert!(seen.image.is_some() && seen.shapes == 0, "an outline with no region is the window's image");
}
/// Frames on `shell` on a display of `native` device pixels per point, at an interface zoom of `zoom`, with painted
/// pointers following the zoom or not (`RootEnv::cursor_follows_zoom`).
fn zoomed(shell: ShellInfo, native: f32, zoom: f32, follows: bool) -> Frames {
let mut frames = frames(shell, native);
frames.root(RootEnv::new().scale(Scale::new(zoom)).cursor_follows_zoom(follows));
// egui applies the zoom from the next pass.
frames.frame(Vec::new(), |_| {});
frames
}
/// The width on the display, in device pixels, of what one frame painted for `cursor`.
fn painted_width(frames: &mut Frames, cursor: PaintedCursor) -> f32 {
frames.frame_in_viewport(ViewportId::ROOT, vec![moved(AT)], |ui| {
cursor::request(ui.ctx(), Cursor::Painted(cursor.clone()), CursorPriority::Tool);
});
let mut paint = frames.take_paint().expect("the frames record their paint");
paint.textures_delta.clear();
let bounds = paint.shapes.iter().map(|clipped| clipped.shape.visual_bounding_rect()).filter(|rect| rect.is_positive()).reduce(Rect::union);
bounds.expect("the pointer is painted").width() * paint.pixels_per_point
}
/// With `cursor_follows_zoom` off (the Scale Cursor Proportional to UI preference), a pointer keeps the size it has
/// at zoom 1 at every interface zoom: as the window's image on each OS, and painted where the window takes no image.
/// On, it grows with the zoom. A brush tip is canvas geometry, so it follows the zoom either way.
#[trial]
fn a_pointer_keeps_its_size_at_every_zoom_unless_it_follows_the_zoom() {
let rows: [(&str, ShellInfo, f32); 3] =
[("X11 at 1", linux(), 1.0), ("Windows at 1.5", linux().os(Os::Windows), 1.5), ("macOS at 2", linux().os(Os::Mac), 2.0)];
for (name, shell, native) in rows {
let image = |zoom: f32, follows: bool, cursor: PaintedCursor| {
let mut frames = zoomed(shell.clone(), native, zoom, follows);
painted(&mut frames, cursor).image.unwrap_or_else(|| panic!("{name}: the window takes the image"))
};
for glyph in [PaintedCursor::Hand, PaintedCursor::arrow(false), PaintedCursor::tool(IconId::new("brush"), Vec2::new(4.0, 20.0), None)] {
let base = image(1.0, true, glyph.clone());
assert_eq!(image(1.0, false, glyph.clone()).size, base.size, "{name} {glyph:?}: at zoom 1 the setting makes no difference");
let kept = image(2.0, false, glyph.clone());
assert_eq!((kept.size, kept.hotspot), (base.size, base.hotspot), "{name} {glyph:?}: off, the pointer keeps its size at zoom 2");
assert!(image(2.0, true, glyph.clone()).size[0] > base.size[0], "{name} {glyph:?}: on, the pointer grows with the zoom");
}
let tip = PaintedCursor::circle(20.0);
assert_eq!(image(2.0, false, tip.clone()).size, image(2.0, true, tip).size, "{name}: a brush tip keeps the canvas's size");
}
// The web paints the pointer into the window.
let web = linux().runtime(Runtime::Web);
let width = |zoom: f32, follows: bool, cursor: PaintedCursor| painted_width(&mut zoomed(web.clone(), 1.0, zoom, follows), cursor);
let base = width(1.0, true, PaintedCursor::Hand);
assert!((width(2.0, false, PaintedCursor::Hand) - base).abs() < 0.5, "off, the painted pointer keeps its size at zoom 2");
assert!(width(2.0, true, PaintedCursor::Hand) > base * 1.5, "on, the painted pointer grows with the zoom");
let tip = PaintedCursor::circle(20.0);
assert!((width(2.0, false, tip.clone()) - width(2.0, true, tip)).abs() < 0.5, "a painted brush tip keeps the canvas's size");
}