Place Shift-clicked point text above the selection

This commit is contained in:
yoriswind committed 2026-12-02 06:48:15 -08:00
1 parent e29018a83f
commit 7ac845d5ba
4 files changed
+128 -4

No files matched your search

+8 -2
View File
@@ -1045,7 +1045,7 @@ pub fn specs() -> Vec<CommandSpec> {
label: "New Type Layer",
menu: &["Layer", "New"],
shortcut: None,
params: r##"{"x":px,"y":px (baseline anchor of point text),"text":str,"box":[x,y,w,h]? (paragraph text),"name":str?,"align":"left|center|right|justify…"?,"orientation":"horizontal|vertical"="horizontal", …character keys: "font","size":pt=12,"color",…}"##,
params: r##"{"x":px,"y":px (baseline anchor of point text),"text":str,"box":[x,y,w,h]? (paragraph text),"name":str?,"align":"left|center|right|justify…"?,"orientation":"horizontal|vertical"="horizontal","aboveSelected":bool? (point text above several selected ungrouped layers), …character keys: "font","size":pt=12,"color",…}"##,
enabled: has_doc,
journal: true,
run: |s, p| {
@@ -1054,9 +1054,15 @@ pub fn specs() -> Vec<CommandSpec> {
let name = given.clone().unwrap_or_else(|| layer_name(&t.text));
// The reference application names new vertical text after its tool (#12058).
let label = if t.orientation == Orientation::Vertical { "New Vertical Type Layer" } else { NEW_TYPE_LAYER_LABEL };
let top = if p.get("aboveSelected").and_then(Value::as_bool) == Some(true) && matches!(&t.shape, TextShape::Point) {
s.active().and_then(|st| crate::layer_place::topmost_ungrouped_selection(&st.doc, st.selected_layers()))
} else {
None
};
let id = s.edit(label, |doc, active| {
refresh(doc, &mut t);
let id = crate::layer_place::insert_new(doc, *active, Layer::new(name, LayerContent::Text(t)), crate::layer_place::Place::AutoEditable);
let anchor = top.or(*active);
let id = crate::layer_place::insert_new(doc, anchor, Layer::new(name, LayerContent::Text(t)), crate::layer_place::Place::AutoEditable);
*active = Some(id);
Ok(id)
})?;
+13
View File
@@ -5,8 +5,21 @@
//! layer (or group) between a base and its clipped layers never becomes their (empty) base.
//! Artboards are never nested or clipped: a new one goes to the top level.
use std::collections::HashSet;
use photocraft_doc::{Document, Layer, LayerContent, LayerId, LayerPath, MAX_GROUP_DEPTH};
/// Topmost of several selected root layers when none of them is a group.
pub(crate) fn topmost_ungrouped_selection(doc: &Document, selected: &[LayerId]) -> Option<LayerId> {
if selected.len() < 2 {
return None;
}
let ids: HashSet<LayerId> = selected.iter().copied().collect();
let mut root = doc.layers.iter().rev().filter(|layer| layer.children().is_none() && ids.contains(&layer.id));
let top = root.next()?;
(root.count() + 1 == ids.len()).then_some(top.id)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Place {
/// New Layer, New Group, new adjustment and fill layers: inside the anchor when it is an open
@@ -0,0 +1,92 @@
//! #17916: Shift-click inserts new point text above the topmost selected root layer.
use egui::Modifiers;
use photocraft_doc::LayerId;
use serde_json::json;
use super::super::layout::hit_layer;
use super::super::mask_replace_selection_tests::{click, new_app};
use crate::PhotocraftApp;
use crate::state::Tool;
fn document() -> (Box<PhotocraftApp>, [LayerId; 3]) {
let mut app = new_app();
app.run("layer.new.layer", json!({"name": "Pix"})).unwrap();
let mut text = |name, x| {
let result = app.run("type.create", json!({"text": name, "name": name, "x": x, "y": 120, "size": 20, "font": "Inter"})).unwrap();
LayerId(result["layer"].as_u64().unwrap())
};
let alpha = text("Alpha", 60);
let bravo = text("Bravo", 400);
let charlie = text("Charlie", 650);
app.ui.tool = Tool::Type;
(app, [alpha, bravo, charlie])
}
fn select(app: &mut PhotocraftApp, layer: LayerId, mode: &str) {
app.run("layer.select", json!({"layer": layer.0, "mode": mode})).unwrap();
}
fn inserted_text(app: &PhotocraftApp) -> LayerId {
let edit = app.ui.text_edit.as_ref().expect("new point text is open");
assert!(edit.created && !edit.mask);
let st = app.session.active().unwrap();
let id = LayerId(edit.layer);
assert_eq!(st.selected_layers(), &[id]);
assert_eq!(st.active_layer, Some(id));
id
}
fn assert_above(app: &PhotocraftApp, parent: LayerId, new: LayerId) {
let layers = &app.session.active().unwrap().doc.layers;
let slot = layers.iter().position(|layer| layer.id == parent).unwrap();
assert_eq!(layers.get(slot + 1).map(|layer| layer.id), Some(new));
}
#[test]
fn shift_click_places_text_above_the_topmost_layer_in_either_selection_order() {
for select_top_last in [false, true] {
let (mut app, [alpha, bravo, charlie]) = document();
let [first, last] = if select_top_last { [alpha, charlie] } else { [charlie, alpha] };
select(&mut app, first, "replace");
select(&mut app, last, "add");
let st = app.session.active().unwrap();
assert_eq!(st.active_layer, Some(last));
assert_eq!(st.selected_layers().len(), 2);
assert_eq!(hit_layer(&mut app, 410.0, 112.0), Some(bravo));
click(&mut app, 410.0, 112.0, Modifiers::SHIFT);
assert_above(&app, charlie, inserted_text(&app));
}
}
#[test]
fn one_selected_layer_and_plain_click_keep_the_active_layer_anchor() {
for shift in [false, true] {
let (mut app, [alpha, _, charlie]) = document();
if shift {
select(&mut app, alpha, "replace");
} else {
select(&mut app, charlie, "replace");
select(&mut app, alpha, "add");
}
let mods = if shift { Modifiers::SHIFT } else { Modifiers::NONE };
click(&mut app, 80.0, 300.0, mods);
assert_above(&app, alpha, inserted_text(&app));
}
}
#[test]
fn shift_click_with_a_selection_across_groups_keeps_the_active_layer_anchor() {
let (mut app, [alpha, _, charlie]) = document();
select(&mut app, alpha, "replace");
let grouped = app.run("layer.groupLayers", json!({"layer": alpha.0})).unwrap();
let group = LayerId(grouped["layer"].as_u64().unwrap());
select(&mut app, charlie, "replace");
select(&mut app, alpha, "add");
assert_eq!(app.session.active().unwrap().selected_layers().len(), 2);
click(&mut app, 410.0, 112.0, Modifiers::SHIFT);
let new = inserted_text(&app);
let st = app.session.active().unwrap();
let children = st.doc.layer(group).unwrap().children().unwrap();
assert_eq!(children.iter().map(|layer| layer.id).collect::<Vec<_>>(), [alpha, new]);
}
+15 -2
View File
@@ -37,6 +37,8 @@ pub(crate) struct TypeState {
/// Whether ⌥ was held at the press of the running gesture. Set with `press`, taken by its release: an Alt+drag
/// opens the Paragraph Text Size dialog instead of making the box (#12044), the modifier read at the press.
pub(super) press_alt: bool,
/// A Shift-only press can place new point text above the selection.
press_shift: bool,
/// The press of this pass ended an open session (the editor commits on a background press): it starts no new
/// text, as in Photoshop. Set by the canvas controls before the tool runs, taken by the press, cleared by `drain`.
pub(super) began_with_session: bool,
@@ -63,6 +65,7 @@ pub(crate) fn drain(app: &mut PhotocraftApp, state: &mut TypeState, _response: &
if !crate::canvas::tools::in_gesture(app) {
state.press = None;
state.press_alt = false;
state.press_shift = false;
}
}
@@ -315,7 +318,7 @@ pub(crate) fn create_layer(app: &mut PhotocraftApp, mut p: serde_json::Value, ta
/// A release that makes new text: from `start` to `end` in document units. A drag of at least 4 screen pixels both
/// ways makes a paragraph box; anything less is a click, which makes point text, or text on or in the path under it.
/// `alt_at_press` is ⌥ held at the press of the gesture.
fn create_text(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2], alt_at_press: bool) {
fn create_text(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2], alt_at_press: bool, shift_at_press: bool) {
let (w, h) = ((end[0] - start[0]).abs(), (end[1] - start[1]).abs());
let is_box = makes_box(app, start, end);
// Photoshop (#12044): Alt+drag with the Type tool sizes the new paragraph box in the
@@ -352,6 +355,9 @@ fn create_text(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2], alt_at_p
} else {
p["x"] = json!(start[0].round());
p["y"] = json!(start[1].round());
if shift_at_press && snapshot.tool == crate::state::Tool::Type && target.is_none() {
p["aboveSelected"] = json!(true);
}
}
let _ = create_layer(app, p, target, &snapshot);
}
@@ -368,6 +374,7 @@ impl CanvasTool for TypeTool<'_> {
let action = self.scope.with_app(|app| press_at(app, &ctx, at.x, at.y, pointer.mods));
self.state.press = (action == ToolAction::Claimed).then_some(at);
self.state.press_alt = action == ToolAction::Claimed && pointer.mods.alt;
self.state.press_shift = action == ToolAction::Claimed && pointer.mods.shift && !pointer.mods.alt && !pointer.mods.cmd && !pointer.mods.ctrl;
action
}
@@ -390,11 +397,12 @@ impl CanvasTool for TypeTool<'_> {
fn release(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) {
let Some(start) = self.state.press.take() else { return };
let alt = std::mem::take(&mut self.state.press_alt);
let shift = std::mem::take(&mut self.state.press_shift);
let Some(space) = cx.space() else { return };
let end = space.view.to_doc(pointer.pos);
let ctx = self.scope.ctx().clone();
self.scope.with_app(|app| {
create_text(app, [start.x, start.y], [end.x, end.y], alt);
create_text(app, [start.x, start.y], [end.x, end.y], alt, shift);
// The new session's editor takes the keyboard from the pass it opens, as an edit of existing text does.
if app.ui.text_edit.is_some() {
super::editing::focus_editor(&ctx);
@@ -405,6 +413,7 @@ impl CanvasTool for TypeTool<'_> {
fn cancel(&mut self, _reason: CancelReason, _cx: &mut ToolContext<'_>) {
self.state.press = None;
self.state.press_alt = false;
self.state.press_shift = false;
self.state.began_with_session = false;
}
@@ -462,3 +471,7 @@ mod mask_active_layer_style_tests;
#[cfg(test)]
#[path = "new_text_active_style_tests.rs"]
mod new_text_active_style_tests;
#[cfg(test)]
#[path = "new_text_topmost_selected_tests.rs"]
mod new_text_topmost_selected_tests;