Skip to content

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

fix(ui): swatches touch with no padding, as in Photoshop (#5103) (#5253)

Sabbir Ahmed11c46b71216f
diff --git a/crates/ui-egui/src/preset_panels.rs b/crates/ui-egui/src/preset_panels.rs
index 7e9e9b672..1aa709db9 100644
--- a/crates/ui-egui/src/preset_panels.rs
+++ b/crates/ui-egui/src/preset_panels.rs
@@ -334,12 +334,20 @@ const FOOTER_GUESS: f32 = 32.0;
/// Draws the folders and footer; `thumb` paints item `(group, item)` into a rect.
/// Where a browser sits: the canvas (drop target), its height cap and the New button's tooltip.
+/// `flush` lays the grid out as Photoshop's Swatches panel: the cells touch, square-cornered and
+/// without a border of their own, so no screen is spent on gaps (#5103); gradients, patterns and
+/// the other preset grids keep their [`GRID_GAP`] and card borders.
pub(crate) struct Place<'a> {
pub(crate) canvas: Rect,
pub(crate) max_h: f32,
pub(crate) new_tip: &'a str,
+ pub(crate) flush: bool,
}
+/// Space between the cells of a preset grid, and the grid's inset from the panel's left edge.
+const GRID_GAP: f32 = 4.0;
+const GRID_INSET: f32 = 8.0;
+
pub(crate) fn browser(
ui: &mut egui::Ui,
st: &mut PresetUi,
@@ -349,7 +357,7 @@ pub(crate) fn browser(
thumb: &mut dyn FnMut(&egui::Ui, Rect, usize, usize),
) -> Vec<Ev> {
let t = Tokens::get(ui.ctx());
- let Place { canvas, max_h, new_tip } = place;
+ let Place { canvas, max_h, new_tip, flush } = place;
let mut ev = Vec::new();
let size = *st.sizes.get(panel).unwrap_or(&40.0);
let list = size <= MIN_THUMB + 0.5;
@@ -439,15 +447,15 @@ pub(crate) fn browser(
continue;
}
let w = ui.available_width();
+ let pad = if list { 0.0 } else { GRID_INSET };
+ let gap = if list || flush { 0.0 } else { GRID_GAP };
let (cell, cols) = if list {
(vec2(w, 24.0), 1)
} else {
- let cols = ((w - 8.0 + 4.0) / (size + 4.0)).floor().max(1.0) as usize;
+ let cols = ((w - pad + gap) / (size + gap)).floor().max(1.0) as usize;
(vec2(size, size), cols)
};
let rows = g.items.len().div_ceil(cols);
- let pad = if list { 0.0 } else { 8.0 };
- let gap = if list { 0.0 } else { 4.0 };
let (area, _) = ui.allocate_exact_size(vec2(w, rows as f32 * (cell.y + gap) + 2.0), Sense::hover());
for (ii, it) in g.items.iter().enumerate() {
let (cx, cy) = ((ii % cols) as f32, (ii / cols) as f32);
@@ -466,14 +474,20 @@ pub(crate) fn browser(
ui.painter().text(pos2(tr.right() + 8.0, r.center().y), Align2::LEFT_CENTER, &it.name, FontId::proportional(11.5), t.text);
} else {
thumb(ui, r, gi, ii);
- let (stroke, kind) = if is_sel {
- (Stroke::new(2.0, t.accent), egui::StrokeKind::Outside)
+ // Touching cells draw no border of their own: only the selected and the
+ // hovered one are outlined, over their neighbours, as in Photoshop.
+ let stroke = if is_sel {
+ Some((Stroke::new(2.0, t.accent), egui::StrokeKind::Outside))
} else if resp.hovered() {
- (Stroke::new(1.0, t.text_dim), egui::StrokeKind::Outside)
+ Some((Stroke::new(1.0, t.text_dim), egui::StrokeKind::Outside))
+ } else if flush {
+ None
} else {
- (Stroke::new(1.0, t.card_border), egui::StrokeKind::Inside)
+ Some((Stroke::new(1.0, t.card_border), egui::StrokeKind::Inside))
};
- ui.painter().rect_stroke(r, CornerRadius::same(2), stroke, kind);
+ if let Some((stroke, kind)) = stroke {
+ ui.painter().rect_stroke(r, CornerRadius::same(if flush { 0 } else { 2 }), stroke, kind);
+ }
}
let resp = if list { resp } else { resp.on_hover_text(&it.name) };
if resp.double_clicked() {
@@ -667,10 +681,11 @@ pub fn gradients_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let canvas = canvas_drop_rect(app);
let mut st = std::mem::take(&mut app.ui.presets_ui);
let max_h = (ui.available_height() - 40.0).clamp(90.0, 260.0);
- let events = browser(ui, &mut st, "gradients", &groups, Place { canvas, max_h, new_tip: tl!("Create new gradient") }, &mut |ui, r, gi, ii| {
- let stops = groups_src[gi].items[ii].resolve(fg, bg);
- paint_gradient(ui, r, &stops);
- });
+ let events =
+ browser(ui, &mut st, "gradients", &groups, Place { canvas, max_h, new_tip: tl!("Create new gradient"), flush: false }, &mut |ui, r, gi, ii| {
+ let stops = groups_src[gi].items[ii].resolve(fg, bg);
+ paint_gradient(ui, r, &stops);
+ });
app.ui.presets_ui = st;
for e in events {
match &e {
@@ -715,11 +730,17 @@ pub fn patterns_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let mut st = std::mem::take(&mut app.ui.presets_ui);
let max_h = (ui.available_height() - 40.0).clamp(90.0, 280.0);
let ctx = ui.ctx().clone();
- let events =
- browser(ui, &mut st, "patterns", &groups, Place { canvas, max_h, new_tip: tl!("Create new pattern from the selection") }, &mut |ui, r, gi, ii| {
+ let events = browser(
+ ui,
+ &mut st,
+ "patterns",
+ &groups,
+ Place { canvas, max_h, new_tip: tl!("Create new pattern from the selection"), flush: false },
+ &mut |ui, r, gi, ii| {
let tex = pattern_texture(&ctx, &pats[gi][ii]);
ui.painter().image(tex.id(), r, Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)), Color32::WHITE);
- });
+ },
+ );
app.ui.presets_ui = st;
for e in events {
match &e {
@@ -820,7 +841,7 @@ pub fn styles_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
&mut st,
"styles",
&groups,
- Place { canvas, max_h: 300.0, new_tip: tl!("Create new style from the selected layer") },
+ Place { canvas, max_h: 300.0, new_tip: tl!("Create new style from the selected layer"), flush: false },
&mut |ui, r, gi, ii| {
ui.painter().rect_filled(r, 2.0, t.field);
ui.painter().image(texes[gi][ii].id(), r, Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)), Color32::WHITE);
@@ -864,7 +885,7 @@ pub fn shapes_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
&mut st,
"shapes",
&groups,
- Place { canvas, max_h: 300.0, new_tip: tl!("Create new shape from the current path") },
+ Place { canvas, max_h: 300.0, new_tip: tl!("Create new shape from the current path"), flush: false },
&mut |ui, r, gi, ii| {
let tex = shape_texture(&ctx, &src[gi].items[ii]);
ui.painter().image(tex.id(), r.shrink(2.0), Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)), t.text);
diff --git a/crates/ui-egui/src/swatches_ui.rs b/crates/ui-egui/src/swatches_ui.rs
index 749ab4a05..a18ddee64 100644
--- a/crates/ui-egui/src/swatches_ui.rs
+++ b/crates/ui-egui/src/swatches_ui.rs
@@ -68,11 +68,10 @@ pub fn panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let mut st = std::mem::take(&mut app.ui.presets_ui);
st.sizes.entry(PANEL.to_string()).or_insert(DEFAULT_SIZE);
let max_h = (ui.available_height() - 30.0).max(60.0);
- let card_border = crate::theme::Tokens::get(ui.ctx()).card_border;
- let events = browser(ui, &mut st, PANEL, &groups, Place { canvas, max_h, new_tip: tl!("Create new swatch") }, &mut |ui, r: Rect, gi, ii| {
+ // Flat, touching squares, as in Photoshop's Swatches panel (#5103).
+ let events = browser(ui, &mut st, PANEL, &groups, Place { canvas, max_h, new_tip: tl!("Create new swatch"), flush: true }, &mut |ui, r: Rect, gi, ii| {
let c = table.get(gi).and_then(|g| g.get(ii)).copied().unwrap_or(Color32::GRAY);
- ui.painter().rect_filled(r, CornerRadius::same(2), c);
- ui.painter().rect_stroke(r, CornerRadius::same(2), egui::Stroke::new(1.0, card_border), egui::StrokeKind::Inside);
+ ui.painter().rect_filled(r, CornerRadius::ZERO, c);
});
app.ui.presets_ui = st;
let mods = ui.input(|i| i.modifiers);
@@ -305,6 +304,20 @@ mod tests {
h.ctx.input(|i| i.viewport_rect()).min + vec2(8.0 + 12.0 + 8.0, 22.0 + 2.0 + 12.0 + 8.0)
}
+ /// #5103: the swatches touch, as in Photoshop's panel. A click just past the first cell's
+ /// right edge lands on the second swatch; with the preset grids' 4 px gap it hit nothing.
+ #[test]
+ fn swatches_touch_without_a_gap() {
+ let app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let second = app.session.presets.swatches.first().and_then(|g| g.items.get(1)).map(|s| s.name.clone()).unwrap();
+ let mut h = Harness::builder().with_size(vec2(300.0, 300.0)).build_ui_state(|ui, app: &mut PhotocraftApp| panel(app, ui), app);
+ h.run_steps(2);
+ assert_eq!(h.state().ui.presets_ui.selected.get(PANEL), None);
+ let p = first_cell(&h);
+ click(&mut h, p + vec2(DEFAULT_SIZE / 2.0 + 1.0, 0.0), Modifiers::NONE);
+ assert_eq!(h.state().ui.presets_ui.selected.get(PANEL), Some(&second), "the second swatch, not a gap");
+ }
+
/// Clicking a swatch while characters are selected recolours them, not just the foreground.
#[test]
fn clicking_a_swatch_recolours_selected_type() {