Skip to content

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

Integration: #1503 #1657 clippy: Id::unique for eyedropper ids, split cursor and picker-close helpers

aiongg476ab056c25f
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 4de9c8bfb..21537257f 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -327,6 +327,24 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
}
// Cursor.
if let Some(p) = resp.hover_pos().filter(|p| screen.contains(*p) && !elsewhere) {
+ let c = pointer_cursor(app, ui, &xf, p);
+ let icon = crate::cursors::show(ui.ctx(), &mut app.cursors, c, p);
+ ui.ctx().set_cursor_icon(icon);
+ draw_cursor_badge(&painter, p, c, &t);
+ if c == Cursor::LoadedGraphic {
+ draw_loaded_thumbnail(app, &painter, p, &t);
+ }
+ }
+ // A second viewport has no panels, dialogs or Quick Apply after its canvas: observe here,
+ // once, as the end of its pass. The root viewport observes at the end of `DesignApp::ui`.
+ if ui.ctx().viewport_id() != egui::ViewportId::ROOT {
+ ime_observe(app, ui.ctx());
+ }
+}
+
+/// The cursor under the pointer at `p` over the canvas: the navigation hands, then the guide
+/// resize cursors, then the active tool's.
+fn pointer_cursor(app: &mut DesignApp, ui: &egui::Ui, xf: &Xf, p: Pos2) -> Cursor {
let wants_text = app.session.wants_text();
let space = navigation_space(app, ui.ctx());
let zoom = spring_zoom_mode(app, ui.ctx(), space);
@@ -348,7 +366,7 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
let column = dragging.or_else(|| {
let menu_open = context_menu_open(ui.ctx());
(!space && !zoom && !middle && !alt_hand && !tool_drag && !menu_open && xf.rot == 0)
- .then(|| column_guide_at(app, &xf, p).map(|h| h.0))
+ .then(|| column_guide_at(app, xf, p).map(|h| h.0))
.flatten()
});
let c = match column {
@@ -365,18 +383,7 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
Some(designcraft_doc::Orientation::Vertical) => Cursor::ResizeH,
None => c,
};
- let icon = crate::cursors::show(ui.ctx(), &mut app.cursors, c, p);
- ui.ctx().set_cursor_icon(icon);
- draw_cursor_badge(&painter, p, c, &t);
- if c == Cursor::LoadedGraphic {
- draw_loaded_thumbnail(app, &painter, p, &t);
- }
- }
- // A second viewport has no panels, dialogs or Quick Apply after its canvas: observe here,
- // once, as the end of its pass. The root viewport observes at the end of `DesignApp::ui`.
- if ui.ctx().viewport_id() != egui::ViewportId::ROOT {
- ime_observe(app, ui.ctx());
- }
+ c
}
/// Longest side of the loaded place cursor's thumbnail, in points.
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index e675c2db8..f8532d9cc 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -8903,7 +8903,7 @@ fn eyedropper(app: &mut DesignApp, ui: &mut egui::Ui, at: egui::Pos2, tip: Strin
crate::icons::paint(ui.painter(), egui::Rect::from_center_size(er.center(), egui::vec2(15.0, 15.0)), "tool-eyedropper", t.icon);
// Esc during the drag cancels just the drag (the picker's own Esc/Enter handling stands down
// while `EYEDROPPER_DRAG` is set), and the release takes nothing.
- let cancel_id = egui::Id::new("color_picker_eyedropper_cancelled");
+ let cancel_id = egui::Id::unique("color_picker_eyedropper_cancelled");
if resp.dragged() {
ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair);
if ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)) {
@@ -8911,7 +8911,7 @@ fn eyedropper(app: &mut DesignApp, ui: &mut egui::Ui, at: egui::Pos2, tip: Strin
}
ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter));
}
- ui.data_mut(|d| d.insert_temp(egui::Id::new(EYEDROPPER_DRAG), resp.dragged()));
+ ui.data_mut(|d| d.insert_temp(egui::Id::unique(EYEDROPPER_DRAG), resp.dragged()));
let cancelled = resp.drag_stopped() && ui.data_mut(|d| d.remove_temp::<bool>(cancel_id)).unwrap_or(false);
// Let go over the picker itself it takes nothing, though a page may lie under it. (Its modal
// backdrop shares the picker's layer and covers the screen: the picker is where it draws.)
@@ -8927,6 +8927,25 @@ fn eyedropper(app: &mut DesignApp, ui: &mut egui::Ui, at: egui::Pos2, tip: Strin
taken
}
+/// Closes the picker for a click on OK or Cancel, else for Enter (OK) or Esc (Cancel) unless an
+/// eyedropper drag is on.
+fn picker_close(ui: &mut egui::Ui, d: &mut Dialog, ok: bool, cancel: bool) {
+ let dragging = ui.data(|d| d.get_temp::<bool>(egui::Id::unique(EYEDROPPER_DRAG))).unwrap_or(false);
+ let (enter, esc) = if dragging {
+ (false, false)
+ } else {
+ (
+ ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter)),
+ ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)),
+ )
+ };
+ if ok || cancel {
+ d.fields.insert("_close".into(), json!(if cancel { "cancel" } else { "ok" }));
+ } else if enter || esc {
+ d.fields.insert("_close".into(), json!(if enter { "ok" } else { "cancel" }));
+ }
+}
+
/// The Color Picker, laid out as InDesign's: the colour space view (a field of two components,
/// a slider for the third: the one whose radio button is on, R at first), the new colour over the
/// one it replaces, OK / Cancel / Add CMYK Swatch at the top right, H/S/B beside L/a/b, R/G/B
@@ -9042,20 +9061,7 @@ fn color_picker(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {
// The footer's padded OK key, which every catalog has.
let ok = pill(ui, 0, tr(" OK ").trim(), true);
let cancel = pill(ui, 1, &tr("Cancel"), false);
- let dragging = ui.data(|d| d.get_temp::<bool>(egui::Id::new(EYEDROPPER_DRAG))).unwrap_or(false);
- let (enter, esc) = if dragging {
- (false, false)
- } else {
- (
- ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter)),
- ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)),
- )
- };
- if ok || cancel {
- d.fields.insert("_close".into(), json!(if cancel { "cancel" } else { "ok" }));
- } else if enter || esc {
- d.fields.insert("_close".into(), json!(if enter { "ok" } else { "cancel" }));
- }
+ picker_close(ui, d, ok, cancel);
// The C/M/Y/K the fields show: the percentages typed or given, else the colour converted.
let cmyk = nums::<4>(d, "cmyk").map(|v| v.map(|x| x as f32)).unwrap_or_else(|| {
designcraft_color::Color::Rgb { r: p.rgb[0], g: p.rgb[1], b: p.rgb[2] }