Skip to content

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

M4.4: segmented controls for Upright modes and crop overlays (no awkward wrapping)

Brandon Thomasc59445ca156b
diff --git a/crates/ui-egui/src/panels/right.rs b/crates/ui-egui/src/panels/right.rs
index e797ecfba..45e652910 100644
--- a/crates/ui-egui/src/panels/right.rs
+++ b/crates/ui-egui/src/panels/right.rs
@@ -117,40 +117,39 @@ fn crop(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) {
super::edit::apply_slider_out(app, &spec, out, |app, v| app.run("crop.straighten", json!({"angle": v})));
}
padded(ui, |ui| {
- ui.horizontal_wrapped(|ui| {
- ui.label("Overlay");
- for (label, o) in [
- ("Thirds", crate::state::CropOverlay::Thirds),
- ("Grid", crate::state::CropOverlay::Grid),
- ("Golden", crate::state::CropOverlay::Golden),
- ("None", crate::state::CropOverlay::None),
- ] {
- if text_button(ui, &format!("cropOverlay-{}", label.to_lowercase()), label, app.ui.crop_overlay == o).clicked() {
- app.ui.crop_overlay = o;
- }
+ ui.label("Overlay");
+ {
+ use crate::state::CropOverlay as O;
+ let opts = [(O::Thirds, "Thirds", "thirds"), (O::Grid, "Grid", "grid"), (O::Golden, "Golden", "golden"), (O::None, "None", "none")];
+ let items: Vec<(&str, &str)> = opts.iter().map(|(_, l, k)| (*l, *k)).collect();
+ let active = opts.iter().position(|(o, _, _)| *o == app.ui.crop_overlay);
+ if let Some(i) = crate::widgets::segmented(ui, "cropOverlay", &items, active, 4) {
+ app.ui.crop_overlay = opts[i].0;
}
- });
+ }
});
divider(ui);
header(ui, "Geometry");
padded(ui, |ui| {
ui.label("Upright");
- ui.horizontal_wrapped(|ui| {
+ {
use lightcraft_develop::Upright;
- for (label, mode, key) in [
+ let modes = [
("Off", Upright::Off, "off"),
("Auto", Upright::Auto, "auto"),
("Guided", Upright::Guided, "guided"),
("Level", Upright::Level, "level"),
("Vertical", Upright::Vertical, "vertical"),
("Full", Upright::Full, "full"),
- ] {
- if text_button(ui, &format!("upright-{key}"), label, d.geometry.upright == mode).clicked() {
- let _ = app.run("geometry.upright", json!({"mode": key}));
- app.ui.tool = if mode == Upright::Guided { "guidedUpright".into() } else { String::new() };
- }
+ ];
+ let items: Vec<(&str, &str)> = modes.iter().map(|(l, _, k)| (*l, *k)).collect();
+ let active = modes.iter().position(|(_, m, _)| *m == d.geometry.upright);
+ if let Some(i) = crate::widgets::segmented(ui, "upright", &items, active, 3) {
+ let (_, mode, key) = modes[i];
+ let _ = app.run("geometry.upright", json!({"mode": key}));
+ app.ui.tool = if mode == Upright::Guided { "guidedUpright".into() } else { String::new() };
}
- });
+ }
let guided = d.geometry.upright == lightcraft_develop::Upright::Guided;
ui.horizontal_wrapped(|ui| {
if !guided && d.geometry.upright != lightcraft_develop::Upright::Off && text_button(ui, "uprightUpdate", "Update", false).clicked() {
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index be4fa561b..482b906fe 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -267,6 +267,41 @@ pub fn text_button(ui: &mut Ui, id: &str, label: &str, active: bool) -> Response
resp
}
+/// A segmented control: `labels` share the available width equally, `per_row` per row. Each segment is
+/// addressable as `button:{id}-{key}` (keys parallel to labels). Returns the clicked index.
+pub fn segmented(ui: &mut Ui, id: &str, items: &[(&str, &str)], active: Option<usize>, per_row: usize) -> Option<usize> {
+ let t = Tokens::get(ui.ctx());
+ let per_row = per_row.max(1);
+ let gap = 4.0;
+ let w = ui.available_width();
+ let seg_w = ((w - gap * (per_row as f32 - 1.0)) / per_row as f32).max(24.0);
+ let mut clicked = None;
+ for (row_i, row) in items.chunks(per_row).enumerate() {
+ ui.horizontal(|ui| {
+ ui.spacing_mut().item_spacing.x = gap;
+ for (j, (label, key)) in row.iter().enumerate() {
+ let i = row_i * per_row + j;
+ let (r, resp) = ui.allocate_exact_size(vec2(seg_w, 24.0), Sense::click());
+ register(ui.ctx(), format!("button:{id}-{key}"), r);
+ let fill = if active == Some(i) {
+ t.pressed
+ } else if resp.hovered() {
+ t.hover
+ } else {
+ t.button
+ };
+ let p = ui.painter();
+ p.rect(r, CornerRadius::same(4), fill, Stroke::new(1.0, t.button_border), StrokeKind::Inside);
+ p.text(r.center(), Align2::CENTER_CENTER, *label, t.semibold(11.5), t.text);
+ if resp.clicked() {
+ clicked = Some(i);
+ }
+ }
+ });
+ }
+ clicked
+}
+
/// An icon-only button. `active` draws the selected background (tool strip).
pub fn icon_button(ui: &mut Ui, id: &str, icon: Icon, size: egui::Vec2, active: bool, enabled: bool, tooltip: &str) -> Response {
let t = Tokens::get(ui.ctx());