Skip to content

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

refactor(ui): move the shape options bar and paint swatch into their own modules

Skyfuller1303d30683fb7f02
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 2cd006087..292c9dab8 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -177,6 +177,8 @@ pub mod select_mask_view;
pub mod served_fonts;
pub mod shape_dialog;
pub(crate) mod shape_fill_ui;
+mod shape_options;
+mod shape_paint_ui;
pub mod shape_stroke_ui;
pub mod shortcut_dispatch;
pub mod shortcuts;
diff --git a/crates/ui-egui/src/shape_options.rs b/crates/ui-egui/src/shape_options.rs
new file mode 100644
index 000000000..a125e7cb0
--- /dev/null
+++ b/crates/ui-egui/src/shape_options.rs
@@ -0,0 +1,158 @@
+//! Shape toolbar edits share the engine commands used by the Properties panel.
+
+use photocraft_doc::LayerContent;
+use serde_json::{Value, json};
+
+use crate::PhotocraftApp;
+use crate::shape_paint_ui::{PaintChange, swatch};
+use crate::state::Tool;
+use crate::theme::Tokens;
+use crate::vector_ui::enabled_stroke_width;
+
+fn solid(color: [f32; 4]) -> photocraft_doc::Fill {
+ photocraft_doc::Fill::Solid(photocraft_doc::Color::rgba(color[0], color[1], color[2], color[3]))
+}
+
+/// The shape layer the options bar edits.
+struct SelectedShape {
+ doc: photocraft_doc::DocId,
+ layer: photocraft_doc::LayerId,
+ fill: Option<photocraft_doc::Fill>,
+ stroke: Option<photocraft_doc::ShapeStroke>,
+ live: Option<photocraft_doc::LiveShape>,
+}
+
+fn selected_shape(session: &photocraft_engine::Session) -> Option<SelectedShape> {
+ let st = session.active()?;
+ let layer = st.active_layer?;
+ match &st.doc.layer(layer)?.content {
+ LayerContent::Shape(sh) => Some(SelectedShape { doc: st.doc.id, layer, fill: sh.fill.clone(), stroke: sh.stroke.clone(), live: sh.live.clone() }),
+ _ => None,
+ }
+}
+
+pub(crate) fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) {
+ let t = Tokens::get(ui.ctx());
+ let lbl = |ui: &mut egui::Ui, s: &str| {
+ ui.label(egui::RichText::new(s).color(t.text_dim).size(12.0));
+ };
+ let o = &mut app.ui.tool_options;
+ let target = selected_shape(&app.session);
+ // Fill and Stroke edit the selected shape layer too (#3347), and still set the next shape's.
+ let (fg, bg) = (app.session.tools.foreground, app.session.tools.background);
+ let (mut has_fill, fill, stroke) = match &target {
+ Some(s) => (s.fill.is_some(), s.fill.clone(), s.stroke.clone()),
+ None => (o.shape_fill, o.shape_fill.then(|| solid(fg)), (o.stroke_width > 0.0).then(|| o.shape_stroke.stroke(o.stroke_width, solid(bg)))),
+ };
+ let mut width = stroke.as_ref().map_or(0.0, |s| s.width);
+ let style = stroke.as_ref().map_or_else(|| o.shape_stroke.clone(), crate::shape_stroke_ui::StrokeOptions::from);
+ let key = |k: &str| target.as_ref().map(|s| format!("shape-options-{:?}-{}-{k}", s.doc, s.layer.0));
+ let mut edit: Option<Value> = None;
+ lbl(ui, tl!("Fill:"));
+ if crate::widgets::checkbox(ui, &mut has_fill, "").changed() {
+ o.shape_fill = has_fill;
+ edit = Some(json!({"fill": if has_fill { json!(fg) } else { Value::Null }}));
+ }
+ let fill_color = swatch(ui, fill.as_ref(), fg, tl!("Fill color"));
+ lbl(ui, tl!("Stroke:"));
+ let stroke_color = swatch(ui, stroke.as_ref().map(|s| &s.paint), bg, tl!("Stroke color"));
+ if crate::widgets::value_field(ui, &mut width, 0.0..=288.0, "px", 58.0).changed() {
+ o.stroke_width = width;
+ let mut p = json!({"width": width});
+ if stroke.is_none() {
+ p["color"] = json!(bg);
+ }
+ edit = Some(if width <= 0.0 { json!({"stroke": null}) } else { json!({"stroke": p, "coalesce": key("stroke-w")}) });
+ }
+ let preview = target.as_ref().filter(|s| s.stroke.is_some()).map(|s| (s.doc, s.layer));
+ if let Some(change) =
+ crate::shape_stroke_ui::button(ui, "tool-stroke", &style, &mut app.ui.stroke_editor, true, tool != Tool::EllipseShape, preview, stroke.is_some())
+ {
+ let mut p = change.options.params();
+ if stroke.is_none() {
+ p["width"] = json!(3.0);
+ p["color"] = json!(bg);
+ }
+ edit = Some(json!({"stroke": p, "coalesce": change.coalesce}));
+ o.shape_stroke = change.options;
+ o.stroke_width = enabled_stroke_width(o.stroke_width);
+ }
+ match tool {
+ Tool::Rectangle => {
+ crate::widgets::vline(ui, 22.0);
+ lbl(ui, tl!("Radius:"));
+ let mut r = match target.as_ref().and_then(|s| s.live.as_ref()) {
+ Some(photocraft_doc::LiveShape::Rect { radii, .. }) => radii[0] as f32,
+ _ => o.corner_radius,
+ };
+ if crate::widgets::value_field(ui, &mut r, 0.0..=10000.0, "px", 62.0).changed() {
+ o.corner_radius = r;
+ let rf = r as f64;
+ edit = Some(json!({"radii": [rf, rf, rf, rf], "coalesce": key("radius")}));
+ }
+ }
+ Tool::Polygon => {
+ crate::widgets::vline(ui, 22.0);
+ lbl(ui, tl!("Sides:"));
+ let mut s = o.polygon_sides as f32;
+ if crate::widgets::value_field(ui, &mut s, 3.0..=100.0, "", 48.0).changed() {
+ o.polygon_sides = s.round().clamp(3.0, 100.0) as u32;
+ }
+ }
+ Tool::Line => {
+ crate::widgets::vline(ui, 22.0);
+ lbl(ui, tl!("Weight:"));
+ crate::widgets::value_field(ui, &mut o.line_weight, 1.0..=1000.0, "px", 58.0);
+ }
+ Tool::CustomShape => {
+ crate::widgets::vline(ui, 22.0);
+ crate::preset_panels::shape_picker(app, ui);
+ }
+ _ if matches!(tool, Tool::PathSelection | Tool::DirectSelection) => {
+ if let Some(photocraft_doc::LiveShape::Rect { radii, .. }) = target.as_ref().and_then(|s| s.live.as_ref()) {
+ crate::widgets::vline(ui, 22.0);
+ lbl(ui, tl!("Radius:"));
+ let mut r = radii[0] as f32;
+ if crate::widgets::value_field(ui, &mut r, 0.0..=10000.0, "px", 62.0).changed() {
+ o.corner_radius = r;
+ let rf = r as f64;
+ edit = Some(json!({"radii": [rf, rf, rf, rf], "coalesce": key("radius")}));
+ }
+ }
+ }
+ _ => {}
+ }
+ for (is_stroke, change) in [(false, fill_color), (true, stroke_color)] {
+ let Some(change) = change else { continue };
+ if let Err(e) = set_tool_paint(app, is_stroke, change) {
+ app.ui.status = e;
+ app.ui.status_error = true;
+ }
+ edit = Some(match (is_stroke, change) {
+ (false, PaintChange::None) => json!({"fill": null}),
+ (false, PaintChange::Color(color)) => json!({"fill": color, "coalesce": key("fill")}),
+ (true, PaintChange::None) => json!({"stroke": null}),
+ (true, PaintChange::Color(color)) => json!({"stroke": {"color": color, "width": enabled_stroke_width(width)}, "coalesce": key("stroke")}),
+ });
+ }
+ if let (Some(mut p), Some(s)) = (edit, &target) {
+ p["layer"] = json!(s.layer.0);
+ if let Err(e) = app.run("shape.edit", p) {
+ app.ui.status = e;
+ app.ui.status_error = true;
+ }
+ }
+}
+
+/// Tool-bar paint changes affect the next shape; Properties edits the selected shape instead.
+pub(crate) fn set_tool_paint(app: &mut PhotocraftApp, stroke: bool, change: PaintChange) -> Result<(), String> {
+ if let PaintChange::Color(color) = change {
+ app.run("tools.setColors", if stroke { json!({"background": color}) } else { json!({"foreground": color}) })?;
+ }
+ if stroke {
+ app.ui.tool_options.stroke_width = if change == PaintChange::None { 0.0 } else { enabled_stroke_width(app.ui.tool_options.stroke_width) };
+ } else {
+ app.ui.tool_options.shape_fill = change != PaintChange::None;
+ }
+ Ok(())
+}
diff --git a/crates/ui-egui/src/shape_paint_ui.rs b/crates/ui-egui/src/shape_paint_ui.rs
new file mode 100644
index 000000000..28bb1c905
--- /dev/null
+++ b/crates/ui-egui/src/shape_paint_ui.rs
@@ -0,0 +1,130 @@
+//! Shape fill and stroke swatches shared by the options bar and the Properties panel.
+
+use egui::{Color32, Sense, Stroke, vec2};
+
+use crate::theme::Tokens;
+
+pub(crate) fn color_of(f: &photocraft_doc::Fill) -> Option<[f32; 4]> {
+ match f {
+ photocraft_doc::Fill::Solid(c) => {
+ let [r, g, b] = c.to_rgb();
+ Some([r, g, b, c.alpha])
+ }
+ _ => None,
+ }
+}
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub(crate) enum PaintChange {
+ Color([f32; 4]),
+ None,
+}
+
+fn rgba32(c: [f32; 4]) -> Color32 {
+ let b = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
+ Color32::from_rgba_unmultiplied(b(c[0]), b(c[1]), b(c[2]), b(c[3]))
+}
+
+fn picker_color(c: [f32; 4]) -> egui::ecolor::Hsva {
+ let linear = egui::ecolor::linear_from_gamma;
+ let display = c.map(|v| if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 });
+ egui::ecolor::Hsva::from_rgba_unmultiplied(linear(display[0]), linear(display[1]), linear(display[2]), display[3])
+}
+
+fn picker_rgba(hsva: egui::ecolor::Hsva) -> [f32; 4] {
+ let [r, g, b, a] = hsva.to_rgba_unmultiplied();
+ let gamma = egui::ecolor::gamma_from_linear;
+ [gamma(r), gamma(g), gamma(b), a]
+}
+
+/// The shared compact picker keeps the float RGB paint and its alpha. Color32 is only display
+/// data: opening the picker must never quantize an existing high-depth colour or enable paint.
+pub(crate) fn swatch(ui: &mut egui::Ui, fill: Option<&photocraft_doc::Fill>, fallback: [f32; 4], tip: &str) -> Option<PaintChange> {
+ let t = Tokens::get(ui.ctx());
+ let (r, resp) = ui.allocate_exact_size(vec2(30.0, 22.0), Sense::click());
+ resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::ColorButton, ui.is_enabled(), tip));
+ let current = fill.and_then(color_of);
+ crate::widgets::checker(ui.painter(), r.shrink(1.0), 4.0);
+ match (current, fill) {
+ (Some(c), _) => {
+ ui.painter().rect_filled(r.shrink(1.0), t.radius_sm, rgba32(c));
+ }
+ (None, Some(photocraft_doc::Fill::Gradient { stops, .. })) if !stops.is_empty() => {
+ // Gradient fills preview as a left-to-right ramp through their stops.
+ let rgb = |c: &photocraft_doc::Color| {
+ let v = c.to_rgba8();
+ Color32::from_rgba_unmultiplied(v[0], v[1], v[2], v[3])
+ };
+ let mut mesh = egui::Mesh::default();
+ let mut ramp: Vec<(f32, Color32)> = stops.iter().map(|(p, c)| (p.clamp(0.0, 1.0), rgb(c))).collect();
+ ramp.insert(0, (0.0, ramp[0].1));
+ ramp.push((1.0, ramp[ramp.len() - 1].1));
+ for (i, (p, c)) in ramp.iter().enumerate() {
+ let x = r.left() + r.width() * p;
+ mesh.colored_vertex(egui::pos2(x, r.top()), *c);
+ mesh.colored_vertex(egui::pos2(x, r.bottom()), *c);
+ if i > 0 {
+ let k = (i as u32) * 2;
+ mesh.add_triangle(k - 2, k - 1, k);
+ mesh.add_triangle(k - 1, k, k + 1);
+ }
+ }
+ ui.painter().add(mesh);
+ }
+ (None, Some(_)) => {}
+ (None, None) => {
+ ui.painter().rect_filled(r.shrink(1.0), t.radius_sm, t.field);
+ ui.painter().line_segment([r.shrink(3.0).left_bottom(), r.shrink(3.0).right_top()], Stroke::new(1.5, t.danger));
+ }
+ };
+ let border = if resp.has_focus() {
+ t.accent
+ } else if resp.hovered() {
+ t.text_dim
+ } else {
+ t.field_border
+ };
+ ui.painter().rect_stroke(r, t.radius_sm, Stroke::new(1.0, border), egui::StrokeKind::Inside);
+ let resp = resp.on_hover_text(tip);
+ let screen_id = resp.id.with("screen-color");
+ let original = current.unwrap_or(fallback);
+ let sampled = |rgb: [f32; 3]| PaintChange::Color([rgb[0], rgb[1], rgb[2], original[3]]);
+ let mut out = crate::screen_picker::take(ui.ctx(), screen_id).map(sampled);
+ let cache = resp.id.with("shape-paint-hsva");
+ let mut hsva = ui
+ .data(|d| d.get_temp::<([f32; 4], egui::ecolor::Hsva)>(cache))
+ .filter(|(previous, _)| *previous == original)
+ .map_or_else(|| picker_color(original), |(_, hsva)| hsva);
+ if let Some(PaintChange::Color(color)) = out {
+ hsva = picker_color(color);
+ }
+ crate::widgets::swatch_popup(&resp).show(|ui| {
+ ui.horizontal(|ui| {
+ if ui.selectable_label(fill.is_none(), tl!("No Color")).clicked() && fill.is_some() {
+ out = Some(PaintChange::None);
+ }
+ if ui.selectable_label(current.is_some(), tl!("Solid")).clicked() && current.is_none() {
+ out = Some(PaintChange::Color(original));
+ }
+ });
+ ui.separator();
+ let before = picker_rgba(hsva);
+ if egui::color_picker::color_picker_hsva_2d(ui, &mut hsva, egui::color_picker::Alpha::OnlyBlend) {
+ let color = picker_rgba(hsva);
+ // egui's HSV gamma/linear round trip can move a float by an ulp without input.
+ if color.iter().zip(before).any(|(a, b)| (a - b).abs() > 0.000001) {
+ out = Some(PaintChange::Color(color));
+ }
+ }
+ if let Some(rgb) = crate::screen_picker::button(ui, screen_id) {
+ out = Some(sampled(rgb));
+ hsva = picker_color([rgb[0], rgb[1], rgb[2], original[3]]);
+ }
+ });
+ let cached_color = match out {
+ Some(PaintChange::Color(color)) => color,
+ _ => original,
+ };
+ ui.data_mut(|d| d.insert_temp(cache, (cached_color, hsva)));
+ out
+}
diff --git a/crates/ui-egui/src/vector_paint_tests.rs b/crates/ui-egui/src/vector_paint_tests.rs
index c62277fb1..c347a9dd0 100644
--- a/crates/ui-egui/src/vector_paint_tests.rs
+++ b/crates/ui-egui/src/vector_paint_tests.rs
@@ -1,5 +1,7 @@
//! Pointer, keyboard, colour precision and history regressions for shape appearance controls.
use super::*;
+use crate::shape_options::set_tool_paint;
+use crate::shape_paint_ui::color_of;
use egui::{Event, Key, Modifiers, PointerButton};
use egui_kittest::{Harness, kittest::Queryable};
use photocraft_doc::ShapeLayer;
diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rs
index e9d998b04..6621a6f5a 100644
--- a/crates/ui-egui/src/vector_ui.rs
+++ b/crates/ui-egui/src/vector_ui.rs
@@ -11,6 +11,7 @@ use serde_json::{Value, json};
use crate::PhotocraftApp;
use crate::canvas::ViewXform;
+use crate::shape_paint_ui::{PaintChange, swatch};
use crate::state::{Tool, ToolOptions};
use crate::theme::Tokens;
@@ -1117,129 +1118,15 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
let mut mode = "shape".to_string();
crate::widgets::dropdown(ui, "shape-mode", &mut mode, &[("shape".to_string(), tl!("Shape"))], 80.0);
crate::widgets::vline(ui, 22.0);
- // Fill and Stroke edit the selected shape layer too (#3347), and still set the next shape's.
- let (fg, bg) = (app.session.tools.foreground, app.session.tools.background);
- let (mut has_fill, fill, stroke) = match &target {
- Some(s) => (s.fill.is_some(), s.fill.clone(), s.stroke.clone()),
- None => (o.shape_fill, o.shape_fill.then(|| solid_paint(fg)), (o.stroke_width > 0.0).then(|| o.shape_stroke.stroke(o.stroke_width, solid_paint(bg)))),
- };
- let mut width = stroke.as_ref().map_or(0.0, |s| s.width);
- let style = stroke.as_ref().map_or_else(|| o.shape_stroke.clone(), crate::shape_stroke_ui::StrokeOptions::from);
- let key = |k: &str| target.as_ref().map(|s| format!("shape-options-{:?}-{}-{k}", s.doc, s.layer.0));
- let mut edit: Option<Value> = None;
- lbl(ui, tl!("Fill:"));
- if crate::widgets::checkbox(ui, &mut has_fill, "").changed() {
- o.shape_fill = has_fill;
- edit = Some(json!({"fill": if has_fill { json!(fg) } else { Value::Null }}));
- }
- let fill_color = swatch(ui, fill.as_ref(), fg, tl!("Fill color"));
- lbl(ui, tl!("Stroke:"));
- let stroke_color = swatch(ui, stroke.as_ref().map(|s| &s.paint), bg, tl!("Stroke color"));
- if crate::widgets::value_field(ui, &mut width, 0.0..=288.0, "px", 58.0).changed() {
- o.stroke_width = width;
- let mut p = json!({"width": width});
- if stroke.is_none() {
- p["color"] = json!(bg);
- }
- edit = Some(if width <= 0.0 { json!({"stroke": null}) } else { json!({"stroke": p, "coalesce": key("stroke-w")}) });
- }
- let preview = target.as_ref().filter(|s| s.stroke.is_some()).map(|s| (s.doc, s.layer));
- if let Some(change) =
- crate::shape_stroke_ui::button(ui, "tool-stroke", &style, &mut app.ui.stroke_editor, true, tool != Tool::EllipseShape, preview, stroke.is_some())
- {
- let mut p = change.options.params();
- if stroke.is_none() {
- p["width"] = json!(3.0);
- p["color"] = json!(bg);
- }
- edit = Some(json!({"stroke": p, "coalesce": change.coalesce}));
- o.shape_stroke = change.options;
- o.stroke_width = enabled_stroke_width(o.stroke_width);
- }
- match tool {
- Tool::Rectangle => {
- crate::widgets::vline(ui, 22.0);
- lbl(ui, tl!("Radius:"));
- let mut r = match target.as_ref().and_then(|s| s.live.as_ref()) {
- Some(photocraft_doc::LiveShape::Rect { radii, .. }) => radii[0] as f32,
- _ => o.corner_radius,
- };
- if crate::widgets::value_field(ui, &mut r, 0.0..=10000.0, "px", 62.0).changed() {
- o.corner_radius = r;
- let rf = r as f64;
- edit = Some(json!({"radii": [rf, rf, rf, rf], "coalesce": key("radius")}));
- }
- }
- Tool::Polygon => {
- crate::widgets::vline(ui, 22.0);
- lbl(ui, tl!("Sides:"));
- let mut s = o.polygon_sides as f32;
- if crate::widgets::value_field(ui, &mut s, 3.0..=100.0, "", 48.0).changed() {
- o.polygon_sides = s.round().clamp(3.0, 100.0) as u32;
- }
- }
- Tool::Line => {
- crate::widgets::vline(ui, 22.0);
- lbl(ui, tl!("Weight:"));
- crate::widgets::value_field(ui, &mut o.line_weight, 1.0..=1000.0, "px", 58.0);
- }
- Tool::CustomShape => {
- crate::widgets::vline(ui, 22.0);
- crate::preset_panels::shape_picker(app, ui);
- }
- _ if matches!(tool, Tool::PathSelection | Tool::DirectSelection) => {
- if let Some(photocraft_doc::LiveShape::Rect { radii, .. }) = target.as_ref().and_then(|s| s.live.as_ref()) {
- crate::widgets::vline(ui, 22.0);
- lbl(ui, tl!("Radius:"));
- let mut r = radii[0] as f32;
- if crate::widgets::value_field(ui, &mut r, 0.0..=10000.0, "px", 62.0).changed() {
- o.corner_radius = r;
- let rf = r as f64;
- edit = Some(json!({"radii": [rf, rf, rf, rf], "coalesce": key("radius")}));
- }
- }
- }
- _ => {}
- }
- for (is_stroke, change) in [(false, fill_color), (true, stroke_color)] {
- let Some(change) = change else { continue };
- if let Err(e) = set_tool_paint(app, is_stroke, change) {
- app.ui.status = e;
- app.ui.status_error = true;
- }
- edit = Some(match (is_stroke, change) {
- (false, PaintChange::None) => json!({"fill": null}),
- (false, PaintChange::Color(color)) => json!({"fill": color, "coalesce": key("fill")}),
- (true, PaintChange::None) => json!({"stroke": null}),
- (true, PaintChange::Color(color)) => json!({"stroke": {"color": color, "width": enabled_stroke_width(width)}, "coalesce": key("stroke")}),
- });
- }
- if let (Some(mut p), Some(s)) = (edit, &target) {
- p["layer"] = json!(s.layer.0);
- if let Err(e) = app.run("shape.edit", p) {
- app.ui.status = e;
- app.ui.status_error = true;
- }
- }
+ crate::shape_options::options_bar(app, ui, tool);
true
}
-/// The shape layer the options bar edits.
-struct SelectedShape {
- doc: photocraft_doc::DocId,
- layer: photocraft_doc::LayerId,
- fill: Option<photocraft_doc::Fill>,
- stroke: Option<photocraft_doc::ShapeStroke>,
- live: Option<photocraft_doc::LiveShape>,
-}
-
-fn selected_shape(session: &photocraft_engine::Session) -> Option<SelectedShape> {
+/// Whether the active layer supplies the selected-shape options.
+fn selected_shape(session: &photocraft_engine::Session) -> Option<photocraft_doc::LayerId> {
let st = session.active()?;
let layer = st.active_layer?;
- match &st.doc.layer(layer)?.content {
- LayerContent::Shape(sh) => Some(SelectedShape { doc: st.doc.id, layer, fill: sh.fill.clone(), stroke: sh.stroke.clone(), live: sh.live.clone() }),
- _ => None,
- }
+ matches!(st.doc.layer(layer)?.content, LayerContent::Shape(_)).then_some(layer)
}
// ---------------------------------------------------------------------------------------------
@@ -1249,148 +1136,10 @@ fn solid_paint(c: [f32; 4]) -> photocraft_doc::Fill {
photocraft_doc::Fill::Solid(photocraft_doc::Color::rgba(c[0], c[1], c[2], c[3]))
}
-fn color_of(f: &photocraft_doc::Fill) -> Option<[f32; 4]> {
- match f {
- photocraft_doc::Fill::Solid(c) => {
- let [r, g, b] = c.to_rgb();
- Some([r, g, b, c.alpha])
- }
- _ => None,
- }
-}
-
-#[derive(Clone, Copy, Debug, PartialEq)]
-enum PaintChange {
- Color([f32; 4]),
- None,
-}
-
-fn enabled_stroke_width(width: f32) -> f32 {
+pub(crate) fn enabled_stroke_width(width: f32) -> f32 {
if width.is_finite() && width > 0.0 { width } else { 3.0 }
}
-/// Tool-bar paint changes affect the next shape; Properties edits the selected shape instead.
-fn set_tool_paint(app: &mut PhotocraftApp, stroke: bool, change: PaintChange) -> Result<(), String> {
- if let PaintChange::Color(color) = change {
- app.run("tools.setColors", if stroke { json!({"background": color}) } else { json!({"foreground": color}) })?;
- }
- if stroke {
- app.ui.tool_options.stroke_width = if change == PaintChange::None { 0.0 } else { enabled_stroke_width(app.ui.tool_options.stroke_width) };
- } else {
- app.ui.tool_options.shape_fill = change != PaintChange::None;
- }
- Ok(())
-}
-
-fn rgba32(c: [f32; 4]) -> Color32 {
- let b = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
- Color32::from_rgba_unmultiplied(b(c[0]), b(c[1]), b(c[2]), b(c[3]))
-}
-
-fn picker_color(c: [f32; 4]) -> egui::ecolor::Hsva {
- let linear = egui::ecolor::linear_from_gamma;
- let display = c.map(|v| if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 });
- egui::ecolor::Hsva::from_rgba_unmultiplied(linear(display[0]), linear(display[1]), linear(display[2]), display[3])
-}
-
-fn picker_rgba(hsva: egui::ecolor::Hsva) -> [f32; 4] {
- let [r, g, b, a] = hsva.to_rgba_unmultiplied();
- let gamma = egui::ecolor::gamma_from_linear;
- [gamma(r), gamma(g), gamma(b), a]
-}
-
-/// The shared compact picker keeps the float RGB paint and its alpha. Color32 is only display
-/// data: opening the picker must never quantize an existing high-depth colour or enable paint.
-fn swatch(ui: &mut egui::Ui, fill: Option<&photocraft_doc::Fill>, fallback: [f32; 4], tip: &str) -> Option<PaintChange> {
- let t = Tokens::get(ui.ctx());
- let (r, resp) = ui.allocate_exact_size(vec2(30.0, 22.0), Sense::click());
- resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::ColorButton, ui.is_enabled(), tip));
- let current = fill.and_then(color_of);
- crate::widgets::checker(ui.painter(), r.shrink(1.0), 4.0);
- match (current, fill) {
- (Some(c), _) => {
- ui.painter().rect_filled(r.shrink(1.0), t.radius_sm, rgba32(c));
- }
- (None, Some(photocraft_doc::Fill::Gradient { stops, .. })) if !stops.is_empty() => {
- // Gradient fills preview as a left-to-right ramp through their stops.
- let rgb = |c: &photocraft_doc::Color| {
- let v = c.to_rgba8();
- Color32::from_rgba_unmultiplied(v[0], v[1], v[2], v[3])
- };
- let mut mesh = egui::Mesh::default();
- let mut ramp: Vec<(f32, Color32)> = stops.iter().map(|(p, c)| (p.clamp(0.0, 1.0), rgb(c))).collect();
- ramp.insert(0, (0.0, ramp[0].1));
- ramp.push((1.0, ramp[ramp.len() - 1].1));
- for (i, (p, c)) in ramp.iter().enumerate() {
- let x = r.left() + r.width() * p;
- mesh.colored_vertex(egui::pos2(x, r.top()), *c);
- mesh.colored_vertex(egui::pos2(x, r.bottom()), *c);
- if i > 0 {
- let k = (i as u32) * 2;
- mesh.add_triangle(k - 2, k - 1, k);
- mesh.add_triangle(k - 1, k, k + 1);
- }
- }
- ui.painter().add(mesh);
- }
- (None, Some(_)) => {}
- (None, None) => {
- ui.painter().rect_filled(r.shrink(1.0), t.radius_sm, t.field);
- ui.painter().line_segment([r.shrink(3.0).left_bottom(), r.shrink(3.0).right_top()], Stroke::new(1.5, t.danger));
- }
- };
- let border = if resp.has_focus() {
- t.accent
- } else if resp.hovered() {
- t.text_dim
- } else {
- t.field_border
- };
- ui.painter().rect_stroke(r, t.radius_sm, Stroke::new(1.0, border), egui::StrokeKind::Inside);
- let resp = resp.on_hover_text(tip);
- let screen_id = resp.id.with("screen-color");
- let original = current.unwrap_or(fallback);
- let sampled = |rgb: [f32; 3]| PaintChange::Color([rgb[0], rgb[1], rgb[2], original[3]]);
- let mut out = crate::screen_picker::take(ui.ctx(), screen_id).map(sampled);
- let cache = resp.id.with("shape-paint-hsva");
- let mut hsva = ui
- .data(|d| d.get_temp::<([f32; 4], egui::ecolor::Hsva)>(cache))
- .filter(|(previous, _)| *previous == original)
- .map_or_else(|| picker_color(original), |(_, hsva)| hsva);
- if let Some(PaintChange::Color(color)) = out {
- hsva = picker_color(color);
- }
- crate::widgets::swatch_popup(&resp).show(|ui| {
- ui.horizontal(|ui| {
- if ui.selectable_label(fill.is_none(), tl!("No Color")).clicked() && fill.is_some() {
- out = Some(PaintChange::None);
- }
- if ui.selectable_label(current.is_some(), tl!("Solid")).clicked() && current.is_none() {
- out = Some(PaintChange::Color(original));
- }
- });
- ui.separator();
- let before = picker_rgba(hsva);
- if egui::color_picker::color_picker_hsva_2d(ui, &mut hsva, egui::color_picker::Alpha::OnlyBlend) {
- let color = picker_rgba(hsva);
- // egui's HSV gamma/linear round trip can move a float by an ulp without input.
- if color.iter().zip(before).any(|(a, b)| (a - b).abs() > 0.000001) {
- out = Some(PaintChange::Color(color));
- }
- }
- if let Some(rgb) = crate::screen_picker::button(ui, screen_id) {
- out = Some(sampled(rgb));
- hsva = picker_color([rgb[0], rgb[1], rgb[2], original[3]]);
- }
- });
- let cached_color = match out {
- Some(PaintChange::Color(color)) => color,
- _ => original,
- };
- ui.data_mut(|d| d.insert_temp(cache, (cached_color, hsva)));
- out
-}
-
/// One corner of the live rectangle, preserving the other three radii.
/// The engine takes the full [top-left, top-right, bottom-right, bottom-left] array.
fn corner_radii_patch(radii: [f64; 4], corner: usize, radius: f32) -> Value {