Keep the Grid visible when Extras are hidden

This commit is contained in:
yoriswind committed 2026-11-30 14:53:34 -08:00
1 parent 2d8bd1f5a2
commit c2ed0f3521
3 files changed
+77 -4

No files matched your search

+1 -1
View File
@@ -116,7 +116,7 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, host: CanvasHost,
let zero = Point::new(doc.ruler_origin[0], doc.ruler_origin[1]);
let page = DocRect::new(0.0, 0.0, w, h);
if app.ui.extras.grid && app.ui.view.extras {
if app.ui.extras.grid {
let major = grid_major_px(&g, f64::from(doc.resolution_dpi.max(1.0)), [w, h], ppi);
let [r, gr, b] = pref_rgb(&g.grid_color, [140, 140, 140]);
CanvasGrid::new("Grid")
+4 -3
View File
@@ -738,9 +738,6 @@ fn invoke_view_command(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, p
};
*slot = !*slot;
let enabled = *slot;
if id == "view.show.grid" && enabled {
app.ui.view.extras = true;
}
if id == "view.rulers" {
crate::prefs_ui::remember_rulers(app, enabled);
}
@@ -900,3 +897,7 @@ fn select_layer_under_cursor(app: &mut PhotocraftApp, ctx: &egui::Context) -> Re
}
Ok(picked)
}
#[cfg(test)]
#[path = "view_show_grid_extras_off_tests.rs"]
mod view_show_grid_extras_off_tests;
@@ -0,0 +1,72 @@
use egui::{Color32, Pos2, Rect, Shape, vec2};
use photocraft_engine::prefs::LineStyle;
use serde_json::json;
use crate::PhotocraftApp;
use crate::canvas::document_display::CanvasHost;
use super::super::state::checked;
use super::invoke;
const GRID: &str = "view.show.grid";
const EXTRAS: &str = "view.extras";
fn shows_color(shape: &Shape, color: Color32) -> bool {
match shape {
Shape::Vec(shapes) => shapes.iter().any(|shape| shows_color(shape, color)),
Shape::PaintChunk(shapes) => shapes.iter().any(|clipped| shows_color(&clipped.shape, color)),
Shape::LineSegment { stroke, .. } => stroke.color == color,
Shape::Mesh(mesh) => mesh.vertices.iter().any(|vertex| vertex.color == color),
_ => false,
}
}
#[test]
fn toggling_grid_draws_it_without_restoring_hidden_extras() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
let ctx = app.ctx().clone();
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
app.run("view.newGuide", json!({"orientation": "vertical", "position": 150})).unwrap();
app.session.prefs.edit(|prefs| {
let g = &mut prefs.guides_grid_and_slices;
g.grid_color = "#39af42".into();
g.guide_color = "#bc254d".into();
g.grid_style = LineStyle::Lines;
g.guide_style = LineStyle::Lines;
});
app.ui.extras.rulers = false;
app.ui.extras.grid = false;
app.ui.extras.guides = true;
app.ui.view.extras = false;
app.ui.view.show.canvas_guides = true;
app.sync_views();
let view = &mut app.ui.views[0];
view.zoom = 1.0;
view.center = [200.0, 150.0];
view.clear_pending();
let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0));
let draw = |app: &mut PhotocraftApp| {
let input = egui::RawInput { screen_rect: Some(rect), ..Default::default() };
let view = app.ui.views[0].clone();
let output = ctx.run_ui(input, |ui| {
crate::canvas::canvas_view(app, ui, CanvasHost::main(0), rect, view.clone(), true);
});
let has_color = |color| output.shapes.iter().any(|clipped| shows_color(&clipped.shape, color));
(has_color(Color32::from_rgb(0x39, 0xaf, 0x42)), has_color(Color32::from_rgb(0xbc, 0x25, 0x4d)))
};
assert_eq!(draw(&mut app), (false, false));
assert_eq!(invoke(&mut app, &ctx, GRID, json!({})).unwrap(), json!(true));
assert_eq!(checked(&app, GRID), Some(true));
assert_eq!(checked(&app, EXTRAS), Some(false));
assert_eq!(draw(&mut app), (true, false), "Grid appears while the guide stays hidden");
assert_eq!(invoke(&mut app, &ctx, EXTRAS, json!({})).unwrap(), json!(true));
assert_eq!(draw(&mut app), (true, true), "turning on Extras restores the guide");
assert_eq!(invoke(&mut app, &ctx, EXTRAS, json!({})).unwrap(), json!(false));
assert_eq!(invoke(&mut app, &ctx, GRID, json!({})).unwrap(), json!(false));
assert_eq!(checked(&app, GRID), Some(false));
assert_eq!(checked(&app, EXTRAS), Some(false));
assert_eq!(draw(&mut app), (false, false));
}