Skip to content

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

feat(ui): allow toolbar to scroll (#2238)

Tropicald4ec2db0a797
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 6866c84f1..09859478b 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -80,10 +80,8 @@ pub fn toolbar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
let (w1, bx, m) = if t.pro { (40.0, 30.0, 5i8) } else { (50.0, 36.0, 7i8) };
let sections = visible_sections(&app.session.prefs().toolbar.hidden);
- // Two columns when the header chevron asks for them, or when one column doesn't fit.
- let slots: usize = sections.iter().map(Vec::len).sum();
- let double =
- app.ui.panels.toolbar_double || toolbar_needs_double(slots, sections.len(), bx, t.pro, t.round_chips, ui.available_rect_before_wrap().height());
+ // Two columns when the header chevron asks for them; a column that doesn't fit scrolls.
+ let double = app.ui.panels.toolbar_double;
let w = if double { w1 + bx + 2.0 } else { w1 };
egui::Panel::left("toolbar").resizable(false).exact_size(w).frame(egui::Frame::NONE.fill(t.chrome).inner_margin(egui::Margin::symmetric(m, 8))).show(
ui,
@@ -119,175 +117,177 @@ pub fn toolbar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
if ui.input(|i| i.pointer.any_pressed()) {
ui.data_mut(|d| d.remove::<egui::Id>(held_id));
}
- // Pro has no group dividers, so its two columns fill every row across groups.
- let sections: Vec<Vec<(usize, Vec<Tool>)>> = if t.pro { vec![sections.into_iter().flatten().collect()] } else { sections };
- for (si, section) in sections.iter().enumerate() {
- // Photoshop 2026 draws one uninterrupted column (no group dividers).
- if si > 0 && !t.pro {
- ui.add_space(4.0);
- let (r, _) = ui.allocate_exact_size(vec2(if double { bx * 2.0 + 2.0 } else { bx }, 1.0), Sense::hover());
- ui.painter().line_segment([r.left_center() + vec2(8.0, 0.0), r.right_center() - vec2(8.0, 0.0)], Stroke::new(1.0, t.separator));
- ui.add_space(4.0);
- }
- let rows: Vec<&[(usize, Vec<Tool>)]> = if double { section.chunks(2).collect() } else { section.chunks(1).collect() };
- for row in rows {
- ui.horizontal(|ui| {
- for (slot_index, slot) in row.iter() {
- let key = egui::Id::new(("tool-slot", *slot_index));
- let tool = slot_tool(ui, app.ui.tool, slot, key);
- let sel = slot.contains(&app.ui.tool);
- let tip = if tool.key() == '\0' { tl!(tool.label()).to_string() } else { format!("{} ({})", tl!(tool.label()), tool.key()) };
- let resp = icons::button(ui, icons::tool_icon(tool), bx, sel, &tip);
- if slot.len() > 1 {
- paint_toolbar_flyout_indicator(ui, &t, &resp);
- }
- if resp.clicked() && ui.data(|d| d.get_temp::<egui::Id>(held_id)) != Some(key) {
- app.ui.tool = tool;
- }
- // Double-clicking the Hand tool fits the image on screen (Photoshop).
- if resp.double_clicked() && tool == Tool::Hand {
- app.ui.tool = tool;
- // With no document open there is nothing to fit.
- let _ = app.run("view.fitOnScreen", json!({}));
- }
- // Right-click or long-press opens the flyout (Photoshop).
- let held_for = resp.is_pointer_button_down_on().then(|| ui.input(|i| i.pointer.press_start_time().map(|t0| i.time - t0))).flatten();
- if slot.len() > 1
- && let Some(seconds) = held_for
- && seconds < 0.35
- {
- ui.ctx().request_repaint_after(std::time::Duration::from_secs_f64(0.35 - seconds));
- }
- let long_press = held_for.is_some_and(|seconds| seconds >= 0.35);
- if slot.len() > 1 && (resp.secondary_clicked() || long_press) {
- ui.data_mut(|d| {
- d.insert_temp(flyout_id, (key, resp.rect));
- if long_press {
- d.insert_temp(held_id, key);
- }
- });
- }
- if slot.len() > 1 && long_press {
- ui.ctx().request_repaint();
- }
- if let Some((open_key, anchor)) = ui.data(|d| d.get_temp::<(egui::Id, Rect)>(flyout_id))
- && open_key == key
- {
- let area = egui::Area::new(key.with("flyout"))
- .order(egui::Order::Foreground)
- .fixed_pos(anchor.right_top() + vec2(6.0, 0.0))
- .show(ui.ctx(), |ui| {
- egui::Frame::popup(ui.style()).show(ui, |ui| {
- let label_w = slot
- .iter()
- .map(|it| {
- ui.painter().layout_no_wrap(tl!(it.label()).to_string(), egui::FontId::proportional(12.5), t.text).size().x
- })
- .fold(0.0f32, f32::max);
- let fw = (label_w + 42.0 + 40.0).max(180.0);
- for &item in slot.iter() {
- let (r, ir) = ui.allocate_exact_size(vec2(fw, 26.0), Sense::click());
- if ir.hovered() {
- ui.painter().rect_filled(r, 3.0, t.hover);
- }
- if item == tool {
- ui.painter().rect_filled(
- Rect::from_center_size(pos2(r.left() + 8.0, r.center().y), vec2(4.0, 4.0)),
- 0.0,
- t.text,
+ egui::ScrollArea::vertical().id_salt("toolbar-columns").show(ui, |ui| {
+ // Pro has no group dividers, so its two columns fill every row across groups.
+ let sections: Vec<Vec<(usize, Vec<Tool>)>> = if t.pro { vec![sections.into_iter().flatten().collect()] } else { sections };
+ for (si, section) in sections.iter().enumerate() {
+ // Photoshop 2026 draws one uninterrupted column (no group dividers).
+ if si > 0 && !t.pro {
+ ui.add_space(4.0);
+ let (r, _) = ui.allocate_exact_size(vec2(if double { bx * 2.0 + 2.0 } else { bx }, 1.0), Sense::hover());
+ ui.painter().line_segment([r.left_center() + vec2(8.0, 0.0), r.right_center() - vec2(8.0, 0.0)], Stroke::new(1.0, t.separator));
+ ui.add_space(4.0);
+ }
+ let rows: Vec<&[(usize, Vec<Tool>)]> = if double { section.chunks(2).collect() } else { section.chunks(1).collect() };
+ for row in rows {
+ ui.horizontal(|ui| {
+ for (slot_index, slot) in row.iter() {
+ let key = egui::Id::new(("tool-slot", *slot_index));
+ let tool = slot_tool(ui, app.ui.tool, slot, key);
+ let sel = slot.contains(&app.ui.tool);
+ let tip = if tool.key() == '\0' { tl!(tool.label()).to_string() } else { format!("{} ({})", tl!(tool.label()), tool.key()) };
+ let resp = icons::button(ui, icons::tool_icon(tool), bx, sel, &tip);
+ if slot.len() > 1 {
+ paint_toolbar_flyout_indicator(ui, &t, &resp);
+ }
+ if resp.clicked() && ui.data(|d| d.get_temp::<egui::Id>(held_id)) != Some(key) {
+ app.ui.tool = tool;
+ }
+ // Double-clicking the Hand tool fits the image on screen (Photoshop).
+ if resp.double_clicked() && tool == Tool::Hand {
+ app.ui.tool = tool;
+ // With no document open there is nothing to fit.
+ let _ = app.run("view.fitOnScreen", json!({}));
+ }
+ // Right-click or long-press opens the flyout (Photoshop).
+ let held_for =
+ resp.is_pointer_button_down_on().then(|| ui.input(|i| i.pointer.press_start_time().map(|t0| i.time - t0))).flatten();
+ if slot.len() > 1
+ && let Some(seconds) = held_for
+ && seconds < 0.35
+ {
+ ui.ctx().request_repaint_after(std::time::Duration::from_secs_f64(0.35 - seconds));
+ }
+ let long_press = held_for.is_some_and(|seconds| seconds >= 0.35);
+ if slot.len() > 1 && (resp.secondary_clicked() || long_press) {
+ ui.data_mut(|d| {
+ d.insert_temp(flyout_id, (key, resp.rect));
+ if long_press {
+ d.insert_temp(held_id, key);
+ }
+ });
+ }
+ if slot.len() > 1 && long_press {
+ ui.ctx().request_repaint();
+ }
+ if let Some((open_key, anchor)) = ui.data(|d| d.get_temp::<(egui::Id, Rect)>(flyout_id))
+ && open_key == key
+ {
+ let area = egui::Area::new(key.with("flyout"))
+ .order(egui::Order::Foreground)
+ .fixed_pos(anchor.right_top() + vec2(6.0, 0.0))
+ .show(ui.ctx(), |ui| {
+ egui::Frame::popup(ui.style()).show(ui, |ui| {
+ let label_w = slot
+ .iter()
+ .map(|it| {
+ ui.painter()
+ .layout_no_wrap(tl!(it.label()).to_string(), egui::FontId::proportional(12.5), t.text)
+ .size()
+ .x
+ })
+ .fold(0.0f32, f32::max);
+ let fw = (label_w + 42.0 + 40.0).max(180.0);
+ for &item in slot.iter() {
+ let (r, ir) = ui.allocate_exact_size(vec2(fw, 26.0), Sense::click());
+ if ir.hovered() {
+ ui.painter().rect_filled(r, 3.0, t.hover);
+ }
+ if item == tool {
+ ui.painter().rect_filled(
+ Rect::from_center_size(pos2(r.left() + 8.0, r.center().y), vec2(4.0, 4.0)),
+ 0.0,
+ t.text,
+ );
+ }
+ icons::paint(
+ ui,
+ Rect::from_center_size(pos2(r.left() + 26.0, r.center().y), vec2(18.0, 18.0)),
+ icons::tool_icon(item),
+ 14.0,
+ t.icon,
);
- }
- icons::paint(
- ui,
- Rect::from_center_size(pos2(r.left() + 26.0, r.center().y), vec2(18.0, 18.0)),
- icons::tool_icon(item),
- 14.0,
- t.icon,
- );
- ui.painter().text(
- pos2(r.left() + 42.0, r.center().y),
- Align2::LEFT_CENTER,
- tl!(item.label()),
- egui::FontId::proportional(12.5),
- t.text,
- );
- if item.key() != '\0' {
ui.painter().text(
- pos2(r.right() - 8.0, r.center().y),
- Align2::RIGHT_CENTER,
- item.key().to_string(),
- egui::FontId::proportional(12.0),
- t.text_dim,
+ pos2(r.left() + 42.0, r.center().y),
+ Align2::LEFT_CENTER,
+ tl!(item.label()),
+ egui::FontId::proportional(12.5),
+ t.text,
);
+ if item.key() != '\0' {
+ ui.painter().text(
+ pos2(r.right() - 8.0, r.center().y),
+ Align2::RIGHT_CENTER,
+ item.key().to_string(),
+ egui::FontId::proportional(12.0),
+ t.text_dim,
+ );
+ }
+ if ir.clicked() {
+ app.ui.tool = item;
+ ui.data_mut(|d| d.remove::<(egui::Id, Rect)>(flyout_id));
+ }
}
- if ir.clicked() {
- app.ui.tool = item;
- ui.data_mut(|d| d.remove::<(egui::Id, Rect)>(flyout_id));
- }
- }
+ });
});
- });
- let held_release = resp.clicked() && ui.data(|d| d.get_temp::<egui::Id>(held_id)) == Some(key);
- let clicked_outside = ui.input(|i| i.pointer.any_click()) && !held_release && !area.response.hovered() && !resp.hovered();
- if clicked_outside || ui.input(|i| i.key_pressed(egui::Key::Escape)) {
- ui.data_mut(|d| d.remove::<(egui::Id, Rect)>(flyout_id));
+ let held_release = resp.clicked() && ui.data(|d| d.get_temp::<egui::Id>(held_id)) == Some(key);
+ let clicked_outside = ui.input(|i| i.pointer.any_click()) && !held_release && !area.response.hovered() && !resp.hovered();
+ if clicked_outside || ui.input(|i| i.key_pressed(egui::Key::Escape)) {
+ ui.data_mut(|d| d.remove::<(egui::Id, Rect)>(flyout_id));
+ }
}
}
- }
- });
+ });
+ }
}
- }
- let ctx = ui.ctx().clone();
- if t.pro && icons::button(ui, "ellipsis", bx, false, tl!("Edit Toolbar…")).clicked() {
- let _ = crate::menus::invoke(app, &ctx, "edit.toolbar", json!({}));
- }
- ui.add_space(if t.pro { 8.0 } else { 14.0 });
- color_chips(app, ui, double);
- if t.pro {
- ui.add_space(8.0);
+ let ctx = ui.ctx().clone();
+ if t.pro && icons::button(ui, "ellipsis", bx, false, tl!("Edit Toolbar…")).clicked() {
+ let _ = crate::menus::invoke(app, &ctx, "edit.toolbar", json!({}));
+ }
+ ui.add_space(if t.pro { 8.0 } else { 14.0 });
+ color_chips(app, ui, double);
+ if t.pro {
+ ui.add_space(8.0);
- let layout = if double {
- egui::Layout::left_to_right(egui::Align::Min)
- } else {
- egui::Layout::top_down(egui::Align::Min)
- };
+ let layout = if double { egui::Layout::left_to_right(egui::Align::Min) } else { egui::Layout::top_down(egui::Align::Min) };
- ui.with_layout(layout, |ui| {
- let quick_mask = app.session.active().is_some_and(|s| s.doc.quick_mask.is_some());
- if icons::button(
- ui,
- "square-dashed",
- bx,
- quick_mask,
- if quick_mask { tl!("Edit in Standard Mode (Q)") } else { tl!("Edit in Quick Mask Mode (Q)") },
- )
- .clicked()
- {
- let _ = crate::menus::invoke(app, &ctx, "select.editInQuickMaskMode", json!({}));
- }
- let sm = icons::button(ui, "app-window", bx, false, tl!("Change Screen Mode (F)"));
- paint_toolbar_flyout_indicator(ui, &t, &sm);
+ ui.with_layout(layout, |ui| {
+ let quick_mask = app.session.active().is_some_and(|s| s.doc.quick_mask.is_some());
+ if icons::button(
+ ui,
+ "square-dashed",
+ bx,
+ quick_mask,
+ if quick_mask { tl!("Edit in Standard Mode (Q)") } else { tl!("Edit in Quick Mask Mode (Q)") },
+ )
+ .clicked()
+ {
+ let _ = crate::menus::invoke(app, &ctx, "select.editInQuickMaskMode", json!({}));
+ }
+ let sm = icons::button(ui, "app-window", bx, false, tl!("Change Screen Mode (F)"));
+ paint_toolbar_flyout_indicator(ui, &t, &sm);
- if sm.clicked() {
- let _ = crate::menus::invoke(app, &ctx, "view.screenMode.cycle", json!({}));
- }
- // Right-click (or long-press) lists the modes, like Photoshop's flyout.
- egui::Popup::context_menu(&sm).show(|ui| {
- ui.set_min_width(220.0);
- for (id, label) in [
- ("view.screenMode.standard", tl!("Standard Screen Mode")),
- ("view.screenMode.fullScreenWithMenuBar", tl!("Full Screen Mode With Menu Bar")),
- ("view.screenMode.fullScreen", tl!("Full Screen Mode")),
- ] {
- let on = crate::view_cmds::checked(app, id).unwrap_or(false);
- if ui.add(egui::Button::selectable(on, label)).clicked() {
- let _ = crate::menus::invoke(app, &ctx, id, json!({}));
- ui.close();
- }
+ if sm.clicked() {
+ let _ = crate::menus::invoke(app, &ctx, "view.screenMode.cycle", json!({}));
}
+ // Right-click (or long-press) lists the modes, like Photoshop's flyout.
+ egui::Popup::context_menu(&sm).show(|ui| {
+ ui.set_min_width(220.0);
+ for (id, label) in [
+ ("view.screenMode.standard", tl!("Standard Screen Mode")),
+ ("view.screenMode.fullScreenWithMenuBar", tl!("Full Screen Mode With Menu Bar")),
+ ("view.screenMode.fullScreen", tl!("Full Screen Mode")),
+ ] {
+ let on = crate::view_cmds::checked(app, id).unwrap_or(false);
+ if ui.add(egui::Button::selectable(on, label)).clicked() {
+ let _ = crate::menus::invoke(app, &ctx, id, json!({}));
+ ui.close();
+ }
+ }
+ });
});
- });
- }
+ }
+ });
},
);
}
@@ -299,19 +299,6 @@ pub fn paint_toolbar_flyout_indicator(ui: &mut egui::Ui, tokens: &Tokens, resp:
ui.painter().add(egui::Shape::convex_polygon(tri, tokens.text_faint, Stroke::NONE));
}
-/// Does the toolbar need two columns? Height of one column (header, tool slots, Edit Toolbar,
-/// colour chips, Quick Mask and Screen Mode) against the height the toolbar gets.
-pub fn toolbar_needs_double(slots: usize, sections: usize, bx: f32, pro: bool, studio: bool, avail_h: f32) -> bool {
- let pitch = bx + 3.0;
- let needed = if pro {
- // margins + header + slots + "…" + gap + colour chips + gap + 2 buttons
- 16.0 + 21.0 + (slots + 1) as f32 * pitch + 8.0 + chips_height(true, false, false) + 8.0 + 2.0 * pitch
- } else {
- 16.0 + slots as f32 * pitch + sections.saturating_sub(1) as f32 * 12.0 + 14.0 + chips_height(false, studio, false)
- };
- needed > avail_h
-}
-
fn c32(c: [f32; 4]) -> Color32 {
Color32::from_rgba_unmultiplied((c[0] * 255.0) as u8, (c[1] * 255.0) as u8, (c[2] * 255.0) as u8, (c[3] * 255.0) as u8)
}