Skip to content

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

feat(ui): Brushes tab preview-size slider for grid cells and list rows (#7256)

VishnuByrrajuc50e0f05ea02
diff --git a/crates/ui-egui/src/brushes_tab.rs b/crates/ui-egui/src/brushes_tab.rs
index 3f45526e5..9093d5dbd 100644
--- a/crates/ui-egui/src/brushes_tab.rs
+++ b/crates/ui-egui/src/brushes_tab.rs
@@ -333,9 +333,24 @@ fn preset_interactions(ui: &egui::Ui, resp: &egui::Response, r: egui::Rect, p: &
/// Width a list row keeps for the gaps around its stroke preview and the preset name.
const STROKE_NAME_ROOM: f32 = 140.0;
-fn list_row(ui: &mut egui::Ui, p: &BrushPreset, current: bool, presets: &[BrushPreset], indent: f32, acts: &mut Vec<Action>) {
+/// The Brushes tab's preview size slider range: what the grid cells and list rows scale between
+/// (1 = the standard size). Not the picker's cards' scale ([`crate::brush_picker::SCALE_RANGE`]).
+pub(crate) const PREVIEW_SCALE_RANGE: std::ops::RangeInclusive<f32> = 0.5..=2.0;
+
+/// A list row's height at preview `scale` (1 = the standard 44 points).
+pub(crate) fn list_row_height(scale: f32) -> f32 {
+ 44.0 * if scale.is_finite() { scale.clamp(*PREVIEW_SCALE_RANGE.start(), *PREVIEW_SCALE_RANGE.end()) } else { 1.0 }
+}
+
+/// A grid cell's width at preview `scale` (1 = the layout's own cell).
+pub(crate) fn grid_cell_width(cell: f32, scale: f32) -> f32 {
+ cell * if scale.is_finite() { scale.clamp(*PREVIEW_SCALE_RANGE.start(), *PREVIEW_SCALE_RANGE.end()) } else { 1.0 }
+}
+
+fn list_row(ui: &mut egui::Ui, p: &BrushPreset, current: bool, presets: &[BrushPreset], indent: f32, scale: f32, acts: &mut Vec<Action>) {
let t = Tokens::get(ui.ctx());
- let (full, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 44.0), Sense::click_and_drag());
+ let k = list_row_height(scale) / 44.0;
+ let (full, resp) = ui.allocate_exact_size(vec2(ui.available_width(), list_row_height(scale)), Sense::click_and_drag());
// Rows inside nested folders are indented; the whole width stays the drop target.
let r = egui::Rect::from_min_max(full.min + vec2(indent.min(full.width() / 2.0), 0.0), full.max);
resp.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, true, current, &p.name));
@@ -349,8 +364,8 @@ fn list_row(ui: &mut egui::Ui, p: &BrushPreset, current: bool, presets: &[BrushP
}
let pb = &p.brush;
let tip = brush_preview::tip_texture(ui.ctx(), &format!("brushes-tip:{}", p.name), &pb.tip, (pb.hardness, pb.angle, pb.roundness), 36, t.text);
- let cell = egui::Rect::from_min_size(r.left_top() + vec2(22.0, 2.0), vec2(36.0, 30.0));
- let tr = egui::Rect::from_center_size(cell.center(), vec2(30.0, 30.0) * crate::brush_sections::thumb_scale(pb.size));
+ let cell = egui::Rect::from_min_size(r.left_top() + vec2(22.0, 2.0), vec2(36.0, 30.0) * k);
+ let tr = egui::Rect::from_center_size(cell.center(), vec2(30.0, 30.0) * k * crate::brush_sections::thumb_scale(pb.size));
ui.painter().image(tip.id(), tr, full_uv(), Color32::WHITE);
ui.painter().text(
pos2(cell.center().x, r.bottom() - 1.0),
@@ -363,7 +378,7 @@ fn list_row(ui: &mut egui::Ui, p: &BrushPreset, current: bool, presets: &[BrushP
// texture slot carries the width, so lists of different widths don't re-render each other's.
let w = (r.width() - (cell.right() - r.left()) - STROKE_NAME_ROOM).clamp(60.0, 170.0).round();
let stroke = brush_preview::stroke_texture(ui.ctx(), &format!("brushes-stroke-{w}:{}", p.name), pb, w as u32, 36, t.text);
- let sr = egui::Rect::from_min_size(pos2(cell.right() + 8.0, r.top() + 4.0), vec2(w, 36.0));
+ let sr = egui::Rect::from_min_size(pos2(cell.right() + 8.0, r.top() + 4.0 * k), vec2(w, 36.0 * k));
ui.painter().image(stroke.id(), sr, full_uv(), Color32::WHITE);
crate::layer_row_ui::label(ui.painter(), sr.right() + 12.0, r.center().y, r.right() - 4.0, &p.name, egui::FontId::proportional(12.0), t.text_dim);
preset_interactions(ui, &resp, r, p, presets, false, acts);
@@ -404,7 +419,7 @@ fn grid_cell(ui: &mut egui::Ui, p: &BrushPreset, current: bool, presets: &[Brush
}
let pb = &p.brush;
let tip = brush_preview::tip_texture(ui.ctx(), &format!("brushes-tip:{}", p.name), &pb.tip, (pb.hardness, pb.angle, pb.roundness), 36, t.text);
- let side = cell - 14.0;
+ let side = (cell - 14.0).max(8.0);
let ir = egui::Rect::from_center_size(pos2(r.center().x, r.top() + side / 2.0 + 4.0), vec2(side, side) * crate::brush_sections::thumb_scale(pb.size));
ui.painter().image(tip.id(), ir, full_uv(), Color32::WHITE);
ui.painter().text(
@@ -800,21 +815,28 @@ fn draw_nodes(ui: &mut egui::Ui, d: &Draw, nodes: &[Node], depth: usize, acts: &
} else if d.grid {
// Rows of whole columns, each with the same margin: a wrapped row indented only
// its first line and left the spare width on the right.
- let (left, cols) = grid_columns(ui.available_width(), d.layout.cell, d.layout.indent + indent, GRID_GAP);
+ let (left, cols) = grid_columns(ui.available_width(), grid_cell_width(d.layout.cell, d.scale), d.layout.indent + indent, GRID_GAP);
ui.vertical(|ui| {
ui.spacing_mut().item_spacing = vec2(GRID_GAP, GRID_GAP);
for row in run.chunks(cols) {
ui.horizontal(|ui| {
ui.add_space(left);
for p in row {
- grid_cell(ui, p, d.current.is_some_and(|n| p.name.eq_ignore_ascii_case(n)), d.presets, d.layout.cell, acts);
+ grid_cell(
+ ui,
+ p,
+ d.current.is_some_and(|n| p.name.eq_ignore_ascii_case(n)),
+ d.presets,
+ grid_cell_width(d.layout.cell, d.scale),
+ acts,
+ );
}
});
}
});
} else {
for p in run.iter() {
- list_row(ui, p, d.current.is_some_and(|n| p.name.eq_ignore_ascii_case(n)), d.presets, indent, acts);
+ list_row(ui, p, d.current.is_some_and(|n| p.name.eq_ignore_ascii_case(n)), d.presets, indent, d.scale, acts);
}
}
run.clear();
@@ -1004,6 +1026,20 @@ pub fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
});
});
+ // The size of the previews in the list below (Photoshop's Brushes panel slider), apart from the
+ // Size of the current brush above.
+ ui.horizontal(|ui| {
+ icons::paint(ui, egui::Rect::from_min_size(ui.cursor().min + vec2(0.0, 3.0), vec2(16.0, 16.0)), "image", 14.0, t.text_faint);
+ ui.add_space(20.0);
+ let w = (inner_w - 112.0).clamp(90.0, inner_w - 40.0);
+ ui.allocate_ui_with_layout(vec2(w, 18.0), egui::Layout::left_to_right(egui::Align::Center), |ui| {
+ let scale = &mut app.ui.brushes_panel.scale;
+ let resp = widgets::slider(ui, scale, PREVIEW_SCALE_RANGE, None).on_hover_text("Preview size");
+ if resp.changed() {
+ *scale = (*scale * 100.0).round() / 100.0;
+ }
+ });
+ });
ui.add_space(6.0);
let acts = preset_list(ui, &app.session.tools.presets, app.session.tools.current_preset.as_deref(), &mut app.ui.brushes_panel, responsive_list_layout(ui));
apply(app, acts);
diff --git a/crates/ui-egui/src/brushes_tab_tests.rs b/crates/ui-egui/src/brushes_tab_tests.rs
index 0d0a8b790..e5cf21b74 100644
--- a/crates/ui-egui/src/brushes_tab_tests.rs
+++ b/crates/ui-egui/src/brushes_tab_tests.rs
@@ -399,3 +399,39 @@ fn card_tips_never_cover_their_size_number() {
assert!(r.min.is_finite() && r.max.is_finite(), "{n} {size} {scale}: {r:?}");
}
}
+
+/// #7256: the Brushes tab's preview slider scales the grid cells and list rows; it is not the
+/// Size of the current brush.
+#[test]
+fn the_preview_slider_scales_the_previews_not_the_brush_size() {
+ for grid in [true, false] {
+ let mut sizes = Vec::new();
+ for scale in [0.5f32, 1.0, 2.0] {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
+ app.ui.panels.brush_settings = true;
+ app.ui.brush_tab = 1;
+ app.ui.brushes_panel.view = if grid { BrushesView::Grid } else { BrushesView::List };
+ app.ui.brushes_panel.scale = scale;
+ let size = app.session.tools.brush.size;
+ let name = app.session.tools.presets[0].name.clone();
+ let mut h = Harness::builder().with_size(vec2(1200.0, 900.0)).build_eframe(move |cc| {
+ PhotocraftApp::setup_context(&cc.egui_ctx, crate::theme::ThemeKind::ALL[0]);
+ app
+ });
+ h.run_steps(4);
+ let r = h.get_by_role_and_label(Role::Button, &name).rect();
+ sizes.push(if grid { r.width() } else { r.height() });
+ assert_eq!(h.state().session.tools.brush.size, size, "the preview slider leaves the brush size alone");
+ }
+ assert!(sizes[0] < sizes[1] && sizes[1] < sizes[2], "grid {grid}: previews grow with the slider: {sizes:?}");
+ }
+}
+
+#[test]
+fn preview_scale_is_clamped_to_its_range() {
+ assert_eq!(list_row_height(1.0), 44.0);
+ assert_eq!(list_row_height(100.0), 88.0);
+ assert_eq!(list_row_height(f32::NAN), 44.0);
+ assert_eq!(grid_cell_width(52.0, 0.0), 26.0);
+ assert_eq!(grid_cell_width(52.0, f32::INFINITY), 52.0);
+}