Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge remote-tracking branch 'origin/main' into swatch-inline-rename

moppeeerd25577fefe45
diff --cc crates/ui-egui/src/panels/swatches.rs
index 15b2f5873,fba3eb5b7..7652251c5
--- a/crates/ui-egui/src/panels/swatches.rs
+++ b/crates/ui-egui/src/panels/swatches.rs
@@@ -259,47 -242,33 +259,74 @@@ fn folder_button(ui: &mut egui::Ui, tip
})
}
+/// The name field of a swatch being renamed in place, over `rect`; false when `sw` isn't. Enter or
+/// a click elsewhere renames it (`swatch.options`), Escape leaves it as it was.
+fn rename_field(app: &mut DesignApp, ui: &mut egui::Ui, sw: &designcraft_color::swatch::Swatch, rect: egui::Rect) -> bool {
+ let key = egui::Id::new(RENAMING);
+ let Some((name, mut text)) = ui.data(|d| d.get_temp::<(String, String)>(key)).filter(|(n, _)| *n == sw.name) else { return false };
+ let id = egui::Id::new(("swatch_rename", &sw.name));
+ let edit = egui::TextEdit::singleline(&mut text).id(id).font(egui::FontId::proportional(12.5)).margin(vec2(2.0, 1.0));
+ let resp = ui.put(rect, edit);
+ if !resp.has_focus() && !resp.lost_focus() {
+ // The field opens with the name selected.
+ resp.request_focus();
+ if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), id) {
+ state
+ .cursor
+ .set_char_range(Some(egui::text::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(text.chars().count()))));
+ state.store(ui.ctx(), id);
+ }
+ }
+ if resp.lost_focus() {
+ ui.data_mut(|d| d.remove_temp::<(String, String)>(key));
+ let to = text.trim();
+ if !ui.input(|i| i.key_pressed(egui::Key::Escape)) && !to.is_empty() && to != name {
+ match app.run("swatch.options", json!({"name": name, "to": to})) {
+ // Still picked under its new name, unless the click that ended the rename picked
+ // another swatch.
+ Ok(_) => ui.data_mut(|d| {
+ if d.get_temp::<String>(egui::Id::new(PICKED)).is_none_or(|p| p == name) {
+ d.insert_temp(egui::Id::new(PICKED), to.to_string());
+ }
+ }),
+ Err(e) => {
+ app.status_error("Swatch Options", e);
+ }
+ }
+ }
+ } else {
+ ui.data_mut(|d| d.insert_temp(key, (name, text)));
+ }
+ true
+}
+
+ /// The swatch's chip following the pointer while the row is dragged.
+ fn paint_drag_chip(ui: &egui::Ui, doc: &designcraft_doc::Document, name: &str, p: egui::Pos2) {
+ let t = Tokens::get(ui.ctx());
+ let (c, g) = crate::widgets::swatch_colors(doc, name, 1.0);
+ let painter = ui.ctx().layer_painter(egui::LayerId::new(egui::Order::Tooltip, egui::Id::unique("swatch_drag")));
+ let chip = egui::Rect::from_min_size(p + vec2(8.0, 8.0), vec2(16.0, 16.0));
+ crate::widgets::paint_chip(&painter, chip, c, g);
+ painter.rect_stroke(chip, 0.0, egui::Stroke::new(1.0, t.field_border), egui::StrokeKind::Outside);
+ }
+
+ /// The type indicator shown at the right of a swatch row: colour mode (spot dot), gradient or tint.
+ fn swatch_kind_label(lang: &str, value: &SwatchValue) -> String {
+ match value {
+ SwatchValue::Color { color, color_type } => {
+ let m = match color {
+ designcraft_color::Color::Cmyk { .. } => "CMYK",
+ designcraft_color::Color::Rgb { .. } => "RGB",
+ designcraft_color::Color::Gray { .. } => crate::i18n::tr(lang, "Gray"),
+ };
+ format!("{}{}", if *color_type == ColorType::Spot { "● " } else { "" }, m)
+ }
+ SwatchValue::Gradient { .. } => "Gradient".into(),
+ SwatchValue::Tint { tint, .. } => format!("{:.0}%", tint * 100.0),
+ _ => String::new(),
+ }
+ }
+
/// One Swatches panel row: chip, name, type; click applies it to the fill or stroke in front of
/// the proxy (of the frames or their text, see [`super::colors_affect_text`]), the context menu
/// to the stroke, colour groups and delete.
@@@ -334,39 -298,15 +356,27 @@@ fn swatch_row
let (c, g) = crate::widgets::swatch_colors(doc, &sw.name, 1.0);
let chip = egui::Rect::from_min_size(row.min + vec2(4.0 + indent, 3.0), vec2(16.0, 16.0));
crate::widgets::paint_chip(ui.painter(), chip, c, g);
- ui.painter().text(row.min + vec2(28.0 + indent, 11.0), egui::Align2::LEFT_CENTER, &sw.name, egui::FontId::proportional(12.5), t.text);
+ let name_rect = egui::Rect::from_min_max(row.min + vec2(26.0 + indent, 1.0), egui::pos2(row.max.x - 72.0, row.max.y - 1.0));
+ if !rename_field(app, ui, sw, name_rect) {
+ ui.painter().text(row.min + vec2(28.0 + indent, 11.0), egui::Align2::LEFT_CENTER, &sw.name, egui::FontId::proportional(12.5), t.text);
+ }
// Type indicators: spot dot / process square, colour mode.
- let kind = match &sw.value {
- SwatchValue::Color { color, color_type } => {
- let m = match color {
- designcraft_color::Color::Cmyk { .. } => "CMYK",
- designcraft_color::Color::Rgb { .. } => "RGB",
- designcraft_color::Color::Gray { .. } => crate::i18n::tr(&app.ui.language, "Gray"),
- };
- format!("{}{}", if *color_type == ColorType::Spot { "● " } else { "" }, m)
- }
- SwatchValue::Gradient { .. } => "Gradient".into(),
- SwatchValue::Tint { tint, .. } => format!("{:.0}%", tint * 100.0),
- _ => String::new(),
- };
+ let kind = swatch_kind_label(&app.ui.language, &sw.value);
if sw.locked {
icons::paint(ui.painter(), egui::Rect::from_min_size(egui::pos2(row.max.x - 64.0, row.min.y + 4.0), vec2(13.0, 13.0)), "lock", t.text_dim);
}
ui.painter().text(row.right_center() - vec2(6.0, 0.0), egui::Align2::RIGHT_CENTER, kind, egui::FontId::proportional(10.5), t.text_dim);
if resp.clicked() && !resp.double_clicked() {
+ // A click on the name of the swatch picked already renames it in place, as InDesign's
+ // (a slow double-click), unless it turns into a double-click.
+ let picked = ui.data(|d| d.get_temp::<String>(egui::Id::new(PICKED))).is_some_and(|p| p == sw.name);
+ // A click on another swatch cancels a rename still waiting.
+ ui.data_mut(|d| d.remove_temp::<(String, f64)>(egui::Id::new(RENAME_CLICK)));
+ if picked && !sw.locked && resp.interact_pointer_pos().is_some_and(|p| name_rect.contains(p)) {
+ let now = ui.input(|i| i.time);
+ ui.data_mut(|d| d.insert_temp(egui::Id::new(RENAME_CLICK), (sw.name.clone(), now)));
+ }
- ui.data_mut(|d| d.insert_temp(egui::Id::new(PICKED), sw.name.clone()));
+ ui.data_mut(|d| d.insert_temp(egui::Id::unique(PICKED), sw.name.clone()));
let _ = super::apply_swatch(app, app.ui.stroke_front, &sw.name, None);
}
// Double-click: Swatch Options (the first click has applied the swatch).