Files
photocraft/crates/ui-egui/src/guide_select.rs
T
a61df64cc9 feat(ui): select guides, the guide context menu and Edit Selected Guides (#3734)
* feat(engine): view.editGuide changes a guide in one step

Photoshop's Edit Selected Guides (the guide context menu) reopens New
Guide on the selected guide. view.editGuide takes the guide by orientation
and index and changes whatever is given: "newOrientation", "position"
(pixels or a length with a unit, % of the new orientation's extent) and
"color" (a name or "#rrggbb"), as one undoable "Edit Guide" step. A guide
that keeps its orientation keeps its index; one that turns goes to the
end of the other list. Without "color" the guide keeps its own colour, or
none. Bad params are refused without a history step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(ui): select guides, the guide context menu and Edit Selected Guides

As in Photoshop, a click on a guide with the Move tool (or ⌘ with a
selection tool) selects it, drawn in Photoshop's selected-guide blue
(#0000ff); a press anywhere else deselects it, and Delete or Backspace
deletes it, before Edit › Clear can reach the pixels. A guide that is
dragged stays selected, and a click no longer nudges a guide to the
pointer. One guide at a time.

Right-click on a guide selects it and opens Photoshop's guide menu: Edit
Selected Guides, Lock Guides, Clear Guides, Clear Canvas Guides, Clear
Selected Guides and Clear Selected Artboard Guides. Edit Selected Guides
opens the New Guide dialog on the guide (its orientation, its position in
the ruler unit and its colour, Cyan for a guide dragged from a ruler) and
OK runs view.editGuide, sending the colour only when it was changed.

The selection is view state (UiState.selected_guide, readable over the
control channel), keyed by document, orientation and position like guide
colours, so it lapses by itself when its guide goes. The menu's two new
labels are in every catalogue.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(ui): Alt-click turns a guide; double-click opens the guide preferences

As in Photoshop, with the Move tool:
- Alt-clicking a guide turns it: a vertical guide becomes horizontal
  through the point clicked, and the other way round. It keeps its colour
  and stays selected; one undoable Edit Guide step (view.editGuide).
  Locked or hidden guides can't be turned.
- Double-clicking a guide opens Preferences › Guides, Grid & Slices.

docs/ui-parity.md lists the guide interactions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(i18n): Hungarian and Turkish strings for the guide menu

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ui): the spelling rows call engine_call with the app

main's spelling suggestions (choose_spelling) came in calling
engine_call(menu, command); on this branch engine_call takes the app
first (the guide menu needs it), so the merge didn't compile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* i18n(sr): translate the guide context menu items

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ui): give main's move-hover test guide drag a press point

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Brandon Thomas <echelon@gmail.com>
2026-10-14 15:17:10 -04:00

111 lines
4.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Selecting a guide on the canvas, as in Photoshop: a click on a guide with the Move tool selects
//! it (drawn in Photoshop's selected-guide blue), a press anywhere else deselects it, Delete
//! removes it, and right-click opens the guide menu (`canvas_tool_menu`), whose Edit Selected
//! Guides reopens New Guide on it.
//!
//! One guide at a time. The selection is view state, not a history step: it names the guide by
//! document, orientation and position, as guide colours do, so it lapses by itself when the guide
//! moves away, is deleted or undone, and the index is looked up when it is used.
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::PhotocraftApp;
/// Photoshop draws a selected guide in this blue, whatever the guide's own colour.
pub const SELECTED_COLOR: egui::Color32 = egui::Color32::from_rgb(0x00, 0x00, 0xff);
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct SelectedGuide {
/// The document's index among the open documents.
pub doc: usize,
pub vertical: bool,
pub position: f64,
}
/// The selected guide's orientation and index in the active document, while guides can be
/// interacted with (shown, unlocked).
pub fn selected(app: &PhotocraftApp) -> Option<(bool, usize)> {
let sel = app.ui.selected_guide?;
if !interactive(app) || app.session.active_index() != Some(sel.doc) {
return None;
}
let guides = &app.session.active()?.doc.guides;
let list = if sel.vertical { &guides.vertical } else { &guides.horizontal };
list.iter().position(|p| *p == sel.position).map(|i| (sel.vertical, i))
}
/// Is the guide at `position` the selected one (drawn blue)?
pub fn is_selected(app: &PhotocraftApp, vertical: bool, position: f64) -> bool {
selected(app).is_some() && app.ui.selected_guide.is_some_and(|s| s.vertical == vertical && s.position == position)
}
/// Guides take clicks only while they're shown and unlocked (as `rulers::guide_at` requires).
fn interactive(app: &PhotocraftApp) -> bool {
app.ui.extras.guides && app.ui.view.shows(app.ui.view.show.canvas_guides) && !app.ui.extras.lock_guides
}
/// Select guide `index` of the active document.
pub fn select(app: &mut PhotocraftApp, vertical: bool, index: usize) {
let Some(doc) = app.session.active_index() else { return };
let Some(guides) = app.session.active().map(|st| &st.doc.guides) else { return };
let list = if vertical { &guides.vertical } else { &guides.horizontal };
app.ui.selected_guide = list.get(index).map(|position| SelectedGuide { doc, vertical, position: *position });
}
pub fn clear(app: &mut PhotocraftApp) {
app.ui.selected_guide = None;
}
/// Delete the selected guide (Delete, Clear Selected Guides). False when no guide is selected.
pub fn delete_selected(app: &mut PhotocraftApp) -> Result<bool, String> {
let Some((vertical, index)) = selected(app) else { return Ok(false) };
app.run("view.deleteGuide", delete_params(vertical, index))?;
clear(app);
Ok(true)
}
pub fn delete_params(vertical: bool, index: usize) -> serde_json::Value {
json!({"orientation": if vertical { "vertical" } else { "horizontal" }, "index": index})
}
/// ⌥-click on guide `index`: turn it, so a vertical guide becomes horizontal through the point
/// clicked (`at`, document pixels) and the other way round, as one undoable step (`view.editGuide`);
/// it stays selected.
pub fn flip(app: &mut PhotocraftApp, vertical: bool, index: usize, at: [f64; 2]) {
let (from, to) = if vertical { ("vertical", "horizontal") } else { ("horizontal", "vertical") };
let position = if vertical { at[1] } else { at[0] };
let params = json!({"orientation": from, "index": index, "newOrientation": to, "position": (position * 1000.0).round() / 1000.0});
match app.run("view.editGuide", params) {
Ok(_) => {
let last = app.session.active().map(|st| if vertical { st.doc.guides.horizontal.len() } else { st.doc.guides.vertical.len() });
if let Some(n) = last.and_then(|n| n.checked_sub(1)) {
select(app, !vertical, n);
}
}
Err(e) => app.ui.status = e,
}
}
/// Delete or Backspace with no modifiers while a guide is selected deletes the guide, before Edit ›
/// Clear can reach the pixels, as in Photoshop.
pub fn keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {
use crate::shortcut_dispatch::Focus;
// Text fields and widgets that delete with these keys themselves keep them (#1534).
if selected(app).is_none() || app.ui.text_edit.is_some() || matches!(Focus::of(ctx), Focus::Text | Focus::DeletingWidget) {
return false;
}
let mods = ctx.input(|i| i.modifiers);
if !mods.is_none() || !ctx.input_mut(|i| i.consume_key(mods, egui::Key::Delete) || i.consume_key(mods, egui::Key::Backspace)) {
return false;
}
if let Err(e) = delete_selected(app) {
app.ui.status = e;
app.ui.status_error = true;
}
true
}
#[cfg(test)]
mod tests;