Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Rulers: keep tick marks and labels out of the origin corner (#2617)

Arthur Wagnercfd12263afe1
diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rs
index ff7444078..dde9c09ab 100644
--- a/crates/ui-egui/src/rulers.rs
+++ b/crates/ui-egui/src/rulers.rs
@@ -245,6 +245,7 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &
let whole = unit == photocraft_engine::prefs::Unit::Pixels;
let label = |v: f64, step: f64| -> String { if step >= 1.0 || whole { format!("{}", v.round() as i64) } else { crate::widgets::fmt_num2(v) } };
for (vertical, extent) in [(false, size[0]), (true, size[1])] {
+ let p = p.with_clip_rect(if vertical { left } else { top });
let px_per_unit = unit.to_px(1.0, dpi, extent, ppi).max(1e-9);
// The top ruler runs along document x, which pixel aspect correction stretches.
let zoom = if vertical { xf.zoom } else { xf.zoom_x() };
@@ -286,8 +287,8 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &
// Pointer position markers.
if let Some(h) = ui.ctx().pointer_hover_pos().filter(|h| full.contains(*h)) {
let m = Stroke::new(1.0, t.text);
- p.line_segment([pos2(h.x, top.top()), pos2(h.x, top.bottom())], m);
- p.line_segment([pos2(left.left(), h.y), pos2(left.right(), h.y)], m);
+ p.with_clip_rect(top).line_segment([pos2(h.x, top.top()), pos2(h.x, top.bottom())], m);
+ p.with_clip_rect(left).line_segment([pos2(left.left(), h.y), pos2(left.right(), h.y)], m);
}
// Drag a new guide out of a ruler; right-click to switch the ruler unit.
let mut chosen_unit: Option<&'static str> = None;
@@ -325,6 +326,45 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &
mod tests {
use super::*;
+ #[test]
+ fn ruler_ticks_labels_and_pointer_stay_out_of_the_origin_corner() {
+ let mut failures = Vec::new();
+ for theme in crate::theme::ThemeKind::ALL {
+ for (zoom, flip, rotation, width) in [(1.0, false, 0.0, 560.0), (1.0, false, 0.0, 800.0), (0.25, true, 0.0, 560.0), (2.0, false, 30.0, 560.0)] {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 256, "height": 128})).unwrap();
+ let full = Rect::from_min_size(pos2(32.0, 32.0), vec2(width - 80.0, 320.0));
+ let corner = Rect::from_min_size(full.min, vec2(RULER, RULER));
+ let builder = egui_kittest::Harness::builder().with_size(vec2(width, 400.0));
+ let mut h = builder.build_ui_state(
+ move |ui, app| {
+ let rect = Rect::from_min_max(full.min + vec2(RULER, RULER), full.max);
+ let xf = ViewXform { rect, zoom, center: [(width - 80.0) / 2.0, 160.0], flip, rotation, aspect: 1.0 };
+ draw_rulers(app, ui, full, &xf);
+ },
+ app,
+ );
+ PhotocraftApp::setup_context(&h.ctx, theme);
+ h.event(egui::Event::PointerMoved(corner.center()));
+ h.run_steps(4);
+ let mut ticks = 0;
+ let mut labels = 0;
+ for shape in &h.output().shapes {
+ if matches!(shape.shape, egui::Shape::LineSegment { .. } | egui::Shape::Text(_)) {
+ ticks += usize::from(matches!(shape.shape, egui::Shape::LineSegment { .. }));
+ labels += usize::from(matches!(shape.shape, egui::Shape::Text(_)));
+ let visible = shape.shape.visual_bounding_rect().intersect(shape.clip_rect);
+ if visible.intersect(corner.shrink(0.5)).is_positive() {
+ failures.push(format!("{theme:?}: zoom={zoom}, flip={flip}, rotation={rotation}, {visible:?}"));
+ }
+ }
+ }
+ assert!(ticks > 2 && labels > 0, "rulers still draw ticks and labels");
+ }
+ }
+ assert!(failures.is_empty(), "ruler drawing overlaps the origin corner: {failures:?}");
+ }
+
#[test]
fn percentage_grid_spacing_uses_both_canvas_dimensions() {
let mut g = GuidesGridAndSlices { grid_unit: Unit::Percent, gridline_every: 20.0, subdivisions: 2, ..Default::default() };
diff --git a/docs/ui-design.md b/docs/ui-design.md
index f897c83da..92d838a24 100644
--- a/docs/ui-design.md
+++ b/docs/ui-design.md
@@ -106,6 +106,10 @@ panels. These offscreen checks do not establish native window or platform input
## Menus
+Horizontal and vertical ruler ticks, labels and pointer markers are clipped to their respective
+strips. Their intersection is a clear origin square, including when the view is panned, flipped
+or rotated; the origin square retains its ruler-unit context menu.
+
An embedding application's `panels.menu_bar` and `panels.rail` flags belong to its current
session. Restoring a saved workspace or remembered panel layout preserves both flags, including
when the serialized layout contains their values. Panel visibility, dock layout, tabs and Timeline