Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

UI-PILL: retain padding fix with upstream toolbar layout

Lucas Lepski353c1cf566b8
diff --cc crates/ui-egui/src/panels/bottombar.rs
index 4361cb0df,89de02631..65c25c6b5
--- a/crates/ui-egui/src/panels/bottombar.rs
+++ b/crates/ui-egui/src/panels/bottombar.rs
@@@ -270,48 -302,157 +303,201 @@@ fn sort_menu(app: &mut LightcraftApp, u
let _ = app.run("library.sort", json!({"group": k}));
}
}
+ }
+
+ #[cfg(test)]
+ mod visual_regressions {
+ use super::*;
+
+ fn scaled_input(size: egui::Vec2, scale: f32) -> egui::RawInput {
+ let mut input = egui::RawInput { screen_rect: Some(Rect::from_min_size(egui::Pos2::ZERO, size)), ..Default::default() };
+ input.viewports.entry(egui::ViewportId::ROOT).or_default().native_pixels_per_point = Some(scale);
+ input
+ }
+
+ #[test]
+ fn capture_bottom_bar_visual_fixtures() {
+ let Some(directory) = std::env::var_os("CRAFT_UI_VISUAL_FIXTURES").map(std::path::PathBuf::from) else { return };
+ std::fs::create_dir_all(&directory).unwrap();
+ for width in [280.0, 400.0, 600.0, 1000.0] {
+ for view in [
+ ViewMode::PhotoGrid,
+ ViewMode::SquareGrid,
+ ViewMode::Detail,
+ ViewMode::Compare,
+ ViewMode::Survey,
+ ViewMode::Reference,
+ ViewMode::People,
+ ] {
+ for scale in [1.0, 1.5, 2.0] {
+ let mut app = LightcraftApp::new(lightcraft_engine::Session::new(), Default::default());
+ app.ui.view = view;
+ let mut ready = false;
+ let mut harness =
+ egui_kittest::Harness::builder().with_size(vec2(width, 100.0)).with_pixels_per_point(scale).wgpu().build_ui_state(
+ move |ui, app: &mut LightcraftApp| {
+ if !ready {
+ crate::theme::install_fonts(ui.ctx());
+ crate::theme::apply(ui.ctx());
+ ready = true;
+ return;
+ }
+ crate::widgets::take_registry(ui.ctx());
+ show(app, ui);
+ },
+ app,
+ );
+ harness.input_mut().max_texture_side = Some(8192);
+ harness.run_steps(4);
+ let stem = format!("lightcraft-bottom-{view:?}-{width}-{scale}x");
+ harness.render().unwrap().save(directory.join(format!("{stem}.png"))).unwrap();
+ let panel = egui::containers::panel::PanelState::load(&harness.ctx, egui::Id::new("bottom_bar")).unwrap().outer_rect;
+ let controls: serde_json::Map<String, serde_json::Value> = crate::widgets::take_registry(&harness.ctx)
+ .into_iter()
+ .map(|(id, r)| (id, json!([r.left(), r.top(), r.width(), r.height()])))
+ .collect();
+ std::fs::write(
+ directory.join(format!("{stem}.json")),
+ serde_json::to_vec_pretty(&json!({
+ "screen": [width, 100.0], "scale": scale,
+ "regions": {"bottom-bar": [panel.left(), panel.top(), panel.width(), panel.height()]},
+ "controls": controls
+ }))
+ .unwrap(),
+ )
+ .unwrap();
+ }
+ }
+ }
+ }
+
+ #[test]
+ fn bottom_bar_controls_do_not_overlap_in_narrow_canvas() {
+ for width in [280.0, 400.0, 600.0, 1000.0] {
+ for view in [
+ ViewMode::PhotoGrid,
+ ViewMode::SquareGrid,
+ ViewMode::Detail,
+ ViewMode::Compare,
+ ViewMode::Survey,
+ ViewMode::Reference,
+ ViewMode::People,
+ ] {
+ for scale in [1.0, 1.5, 2.0] {
+ let ctx = egui::Context::default();
+ crate::theme::install_fonts(&ctx);
+ crate::theme::apply(&ctx);
+ let mut app = LightcraftApp::new(lightcraft_engine::Session::new(), Default::default());
+ app.ui.view = view;
+ let mut output = ctx.run_ui(scaled_input(vec2(width, 100.0), scale), |ui| show(&mut app, ui));
+ output.textures_delta.clear();
+ assert_eq!(ctx.content_rect().size(), vec2(width, 100.0));
+ assert_eq!(ctx.pixels_per_point(), scale);
+ let controls = crate::widgets::take_registry(&ctx);
+ assert!(!controls.is_empty());
+ for (index, (id, rect)) in controls.iter().enumerate() {
+ assert!(rect.left() >= 0.0 && rect.right() <= width, "{view:?} at {width}: {id} outside viewport: {rect:?}");
+ for (other_id, other) in controls.iter().skip(index + 1) {
+ assert!(!rect.intersect(*other).is_positive(), "{view:?} at {width}: {id} overlaps {other_id}: {rect:?} / {other:?}");
+ }
+ }
+ }
+ }
+ }
+ }
+ #[test]
+ fn compact_view_menu_opens_and_changes_view_by_keyboard() {
+ use egui_kittest::kittest::Queryable;
+
+ for scale in [1.0, 1.5, 2.0] {
+ let app = LightcraftApp::new(lightcraft_engine::Session::new(), Default::default());
+ let mut ready = false;
+ let mut harness = egui_kittest::Harness::builder().with_size(vec2(280.0, 640.0)).with_pixels_per_point(scale).build_ui_state(
+ move |ui, app: &mut LightcraftApp| {
+ if !ready {
+ crate::theme::install_fonts(ui.ctx());
+ crate::theme::apply(ui.ctx());
+ ready = true;
+ return;
+ }
+ crate::shortcuts::handle(app, ui.ctx());
+ crate::widgets::take_registry(ui.ctx());
+ show(app, ui);
+ },
+ app,
+ );
+ harness.run_steps(4);
+ assert_eq!(harness.ctx.content_rect().size(), vec2(280.0, 640.0));
+ assert_eq!(harness.ctx.pixels_per_point(), scale);
+ let mut focused = false;
+ for _ in 0..12 {
+ harness.key_press(egui::Key::Tab);
+ harness.run_steps(3);
+ focused = harness.get_by_label("View").is_focused();
+ if focused {
+ break;
+ }
+ }
+ assert!(focused, "Tab reaches the compact View control at {scale}x");
+ harness.key_press(egui::Key::Space);
+ harness.run_steps(3);
+ let mut people_focused = false;
+ for _ in 0..20 {
+ harness.key_press(egui::Key::Tab);
+ harness.run_steps(3);
+ people_focused = harness.get_by_label("People").is_focused();
+ if people_focused {
+ break;
+ }
+ }
+ assert!(people_focused, "Tab reaches People at {scale}x");
+ harness.key_press(egui::Key::Space);
+ harness.run_steps(3);
+ assert_eq!(harness.state().ui.view, ViewMode::People, "keyboard activation dispatches the view command");
+ }
+ }
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::Services;
+ use crate::headless::HeadlessView;
+
+ #[test]
+ fn rating_and_flag_buttons_keep_padding_inside_the_pill() {
+ for scale in [1.0, 2.0] {
+ for room in [240.0, 500.0] {
+ let mut app = LightcraftApp::new(lightcraft_engine::Session::new(), Services::default());
+ let mut view = HeadlessView::new();
+ for frame in 0..3 {
+ let raw = HeadlessView::raw_input(vec2(700.0, 40.0), scale, frame as f64 / 60.0, vec![]);
+ view.run(raw, |ui| {
+ let full = ui.max_rect();
+ centre(&mut app, ui, full, 20.0, 20.0 + room);
+ });
+ let widgets = crate::widgets::take_registry(&view.ctx);
+ let star = widgets.iter().find(|(id, _)| id == "star:bottom:1").unwrap().1;
+ let pill = view
+ .shapes
+ .iter()
+ .find_map(|shape| match &shape.shape {
+ egui::Shape::Rect(rect) if rect.rect.contains(star.center()) && rect.rect.height() == 30.0 => Some(rect.rect),
+ _ => None,
+ })
+ .unwrap();
+ let inner = pill.shrink2(vec2(10.0, 4.0));
+ for (id, rect) in &widgets {
+ if id.starts_with("star:bottom:") || id.starts_with("flag:") {
+ assert!(inner.contains_rect(*rect), "{id} {rect:?} exceeds the pill's padded bounds {inner:?} at scale {scale}");
+ }
+ }
+ assert!(pill.left() >= 20.0 && pill.right() <= 20.0 + room);
+ if let Some((_, copy)) = widgets.iter().find(|(id, _)| id == "button:copySettings") {
+ assert!(copy.left() >= pill.right() + 10.0);
+ }
+ }
+ }
+ }
+ }
+}