DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into color-panel-gradient-stop
moppeeere7cb2ba0c436
diff --cc crates/ui-egui/src/panels/swatches.rsindex 977eb370e,fba3eb5b7..92d7ff2ef--- a/crates/ui-egui/src/panels/swatches.rs+++ b/crates/ui-egui/src/panels/swatches.rs@@@ -708,13 -659,65 +731,69 @@@ fn thumbnail(p: &egui::Painter, r: eguip.rect_stroke(r, 0.0, egui::Stroke::new(1.0, border), egui::StrokeKind::Outside);}+/// Temp-data key of what's selected on the Gradient panel's ramp ([`RampSel`]): the Color panel+/// edits that stop.+pub(super) const GRAD_SEL: &str = "grad_sel";++ /// The gradient across `ramp`, sampled in bands.+ fn paint_ramp(painter: &egui::Painter, ramp: egui::Rect, g: &designcraft_color::Gradient) {+ let n = 96;+ for i in 0..n {+ let (a, b) = (i as f32 / n as f32, (i + 1) as f32 / n as f32);+ let (c, alpha) = g.sample((a + b) / 2.0);+ let x0 = ramp.left() + ramp.width() * a;+ let x1 = ramp.left() + ramp.width() * b + 0.5;+ painter.rect_filled(egui::Rect::from_x_y_ranges(x0..=x1, ramp.y_range()), 0.0, color32(&c, alpha));+ }+ }++ /// The stops after dragging marker `h` to `o` (`off`: dropped off the ramp, which removes a stop),+ /// and the selection to move to if the drop changed it.+ fn ramp_release(g: &designcraft_color::Gradient, h: RampSel, o: f32, off: bool) -> (Vec<serde_json::Value>, Option<RampSel>) {+ let mut stops = stops_json(g);+ let mut sel = None;+ match h {+ RampSel::Stop(i) if off => {+ if i < stops.len() {+ stops.remove(i);+ }+ sel = Some(RampSel::Stop(0));+ }+ RampSel::Stop(i) => set_stop(&mut stops, i, "location", json!(o as f64 * 100.0)),+ RampSel::Mid(i) => set_stop(&mut stops, i, "midpoint", json!(o as f64 * 100.0)),+ }+ (stops, sel)+ }++ /// Dragging the gradient's thumbnail: its swatch is the payload, and a thumbnail follows the pointer.+ fn drag_gradient_thumbnail(ui: &egui::Ui, resp: &egui::Response, g: &designcraft_color::Gradient, swatch: &str, t: &Tokens) {+ resp.dnd_set_drag_payload(GradientDrag(swatch.to_string()));+ if resp.dragged()+ && let Some(p) = ui.ctx().pointer_interact_pos()+ {+ let painter = ui.ctx().layer_painter(egui::LayerId::new(egui::Order::Tooltip, egui::Id::unique("gradient_drag")));+ thumbnail(&painter, egui::Rect::from_min_size(p + vec2(8.0, 8.0), vec2(18.0, 18.0)), g, t.field_border);+ }+ }++ /// The Gradient panel's Type menu; the kind chosen, as the command's parameters.+ fn gradient_type_combo(ui: &mut egui::Ui, lang: &str, g: &designcraft_color::Gradient, is_gradient: bool) -> Option<serde_json::Value> {+ let mut send = None;+ let shown = if is_gradient { crate::i18n::tr(lang, g.kind.label()) } else { "" };+ let combo = craft_ui::ComboBox::from_id_salt("grad_type").selected_text(crate::rtl::widget(ui, shown)).width(104.0).show_ui(ui, |ui| {+ for (k, v) in [("Linear", "linear"), ("Radial", "radial")] {+ if ui.selectable_label(is_gradient && g.kind.label() == k, crate::rtl::widget(ui, crate::i18n::tr(lang, k))).clicked() {+ send = Some(json!({"kind": v}));+ }+ }+ });+ let _ = crate::widgets::name_combo(combo, crate::i18n::tr(lang, "Type"));+ send+ }+/// What's selected on the ramp: a stop, or the midpoint after a stop.#[derive(Clone, Copy, PartialEq, Debug)]-enum RampSel {+pub(super) enum RampSel {Stop(usize),Mid(usize),}@@@ -757,8 -760,8 +836,8 @@@ pub fn gradient_panel(app: &mut DesignAlet is_gradient = cur.is_some();let g = cur.unwrap_or_default();let radial = g.kind == designcraft_color::GradientKind::Radial;- let sel_id = egui::Id::new(GRAD_SEL);- let drag_id = egui::Id::new("grad_drag");- let sel_id = egui::Id::unique("grad_sel");++ let sel_id = egui::Id::unique(GRAD_SEL);+ let drag_id = egui::Id::unique("grad_drag");let mut sel: RampSel = ui.data(|d| d.get_temp(sel_id)).unwrap_or(RampSel::Stop(0));let last = g.stops.len().saturating_sub(1);sel = match sel {