From 8238038ffc77be7e1ec09e20da4b001b6bc5adce Mon Sep 17 00:00:00 2001 From: crastinator <141459787+crastinator@users.noreply.github.com> Date: Sun, 29 Nov 2026 21:32:03 +0200 Subject: [PATCH] Type: new text takes the active text layer's font and size (#17913) A Type tool click or Shift+click that starts new point text while a horizontal point text layer outside any group is active now takes that layer's font family, style and size, as the options bar shows them, the way the Horizontal Type Mask already does (#12077). A pixel layer, a box drag, vertical or paragraph text and a layer inside a group keep the tool's options; the colour stays the foreground colour. --- .../ui-egui/src/tools/type_tool/defaults.rs | 10 ++ .../mask_active_layer_style_tests.rs | 9 +- .../type_tool/new_text_active_style_tests.rs | 119 ++++++++++++++++++ crates/ui-egui/src/tools/type_tool/pointer.rs | 19 ++- 4 files changed, 146 insertions(+), 11 deletions(-) create mode 100644 crates/ui-egui/src/tools/type_tool/new_text_active_style_tests.rs diff --git a/crates/ui-egui/src/tools/type_tool/defaults.rs b/crates/ui-egui/src/tools/type_tool/defaults.rs index 48b9408096..06b1678265 100644 --- a/crates/ui-egui/src/tools/type_tool/defaults.rs +++ b/crates/ui-egui/src/tools/type_tool/defaults.rs @@ -132,6 +132,16 @@ pub(super) fn mask_style_from_active_layer(app: &PhotocraftApp) -> Option Option { + let st = app.session.active()?; + let id = st.active_layer?; + let point = matches!(text_layer(&st.doc, id)?.shape, photocraft_doc::text::TextShape::Point); + (point && st.doc.layers.index().parent(id).is_none()).then(|| mask_style_from_active_layer(app)).flatten() +} + /// Style numbers become f32 in the engine. JSON itself permits finite f64 values too large /// for that conversion; a hand-edited setting must not inject infinity into a new layer. /// Style collections (features, axes, colour) contain only scalar values, so no recursion. diff --git a/crates/ui-egui/src/tools/type_tool/mask_active_layer_style_tests.rs b/crates/ui-egui/src/tools/type_tool/mask_active_layer_style_tests.rs index 1d946637b0..2477459d2d 100644 --- a/crates/ui-egui/src/tools/type_tool/mask_active_layer_style_tests.rs +++ b/crates/ui-egui/src/tools/type_tool/mask_active_layer_style_tests.rs @@ -212,17 +212,16 @@ fn new_vertical() -> (Box, LayerId) { (app, LayerId(id)) } -/// The plain Type tool is not a Type Mask: with the same 80 pt layer active its click still uses -/// the tool options (#10824 covers the settings of a type target, not this). Guard test: passes -/// before and after the change. +/// The plain Type tool makes a type layer, not a mask, and since #17913 its click starts in the +/// same 80 pt layer's settings too. #[test] -fn the_plain_type_tool_still_starts_at_the_tool_options() { +fn the_plain_type_tool_also_starts_at_the_active_layers_style() { let (mut app, _) = txt_active(80.0); app.ui.tool = Tool::Type; click(&mut app, 60.0, 300.0, Modifiers::NONE); let ed = app.ui.text_edit.clone().expect("a new type session"); assert!(!ed.mask, "a type layer, not a mask"); - assert_eq!(mask_style(&app), ("Tool Font".into(), "Regular".into(), 48.0), "the Type tool's own defaults"); + assert_eq!(mask_style(&app), ("Inter".into(), "Bold".into(), 80.0), "the active layer's settings"); } /// The zero case: a text layer with no characters still shows its font, style and size in the bar diff --git a/crates/ui-egui/src/tools/type_tool/new_text_active_style_tests.rs b/crates/ui-egui/src/tools/type_tool/new_text_active_style_tests.rs new file mode 100644 index 0000000000..9ac632e324 --- /dev/null +++ b/crates/ui-egui/src/tools/type_tool/new_text_active_style_tests.rs @@ -0,0 +1,119 @@ +//! #17913: with a point text layer active, new text the Type tool starts with a click or a +//! Shift+click takes that layer's font family, style and size, as the options bar shows for it, +//! instead of the tool's stored options. A pixel layer active keeps the tool's options, the colour +//! stays the foreground colour, and the stored options are not changed. +//! +//! The issue's *not measured* rows keep today's behaviour, pinned here: a paragraph (box) drag and +//! an active text layer inside a group. + +use egui::Modifiers; +use photocraft_doc::LayerId; +use serde_json::json; + +use super::super::mask_replace_selection_tests::{click, new_app}; +use super::super::text_layer; +use crate::PhotocraftApp; +use crate::canvas::testing::{ToolEvent, tool_event}; +use crate::state::Tool; + +const TOOL: (&str, &str, f32) = ("Tool Font", "Regular", 48.0); + +/// The family, style and size of the layer's first character run. +fn style(app: &PhotocraftApp, id: LayerId) -> (String, String, f32) { + let t = text_layer(&app.session.active().unwrap().doc, id).expect("a type layer"); + let c = t.char_runs().into_iter().next().expect("a first character run").style; + (c.font_family, c.font_style, c.size_pt) +} + +/// The layer the open Type tool session made. +fn new_layer(app: &PhotocraftApp) -> LayerId { + let ed = app.ui.text_edit.as_ref().expect("an open type session"); + assert!(ed.created && !ed.mask, "a new type layer"); + LayerId(ed.layer) +} + +fn tool_options(app: &PhotocraftApp) -> (String, String, f32) { + let o = &app.ui.tool_options; + (o.type_font.clone(), o.type_style.clone(), o.type_size) +} + +/// A document whose active layer is the point text `Alpha` (Inter Bold 60 pt, blue), with the +/// Type tool selected and its own options set apart from the layer's. +fn alpha_active() -> (Box, LayerId) { + let mut app = new_app(); + let id = app.run("type.create", json!({"text": "Alpha", "x": 60, "y": 120, "font": "Inter", "fontStyle": "Bold", "size": 60, "color": "#0000ff"})).unwrap() + ["layer"] + .as_u64() + .unwrap(); + app.ui.tool = Tool::Type; + app.ui.tool_options.type_font = TOOL.0.into(); + app.ui.tool_options.type_style = TOOL.1.into(); + app.ui.tool_options.type_size = TOOL.2; + (app, LayerId(id)) +} + +fn tool() -> (String, String, f32) { + (TOOL.0.into(), TOOL.1.into(), TOOL.2) +} + +#[test] +fn a_click_on_empty_canvas_takes_the_active_layers_font_style_and_size() { + let (mut app, alpha) = alpha_active(); + click(&mut app, 60.0, 320.0, Modifiers::NONE); + let id = new_layer(&app); + assert_ne!(id, alpha); + assert_eq!(style(&app, id), style(&app, alpha), "the active layer's family, style and size"); +} + +/// A Shift+click on the active layer itself, and on another text layer that is not active, both +/// start new text in the active layer's settings. +#[test] +fn a_shift_click_on_a_text_layer_takes_the_active_layers_style() { + let (mut app, alpha) = alpha_active(); + click(&mut app, 80.0, 100.0, Modifiers::SHIFT); + let on_alpha = new_layer(&app); + assert_ne!(on_alpha, alpha, "Shift+click starts new text over Alpha"); + assert_eq!(style(&app, on_alpha), style(&app, alpha)); + + let (mut app, alpha) = alpha_active(); + app.run("type.create", json!({"text": "Bravo", "x": 400, "y": 120, "font": "Inter", "fontStyle": "Regular", "size": 20})).unwrap(); + app.run("layer.select", json!({"layer": alpha.0})).unwrap(); + click(&mut app, 410.0, 112.0, Modifiers::SHIFT); + assert_eq!(style(&app, new_layer(&app)), style(&app, alpha), "Alpha's settings, not Bravo's"); +} + +#[test] +fn a_pixel_layer_active_keeps_the_tools_options() { + let (mut app, _) = alpha_active(); + app.run("layer.new.layer", json!({})).unwrap(); + click(&mut app, 60.0, 320.0, Modifiers::NONE); + assert_eq!(style(&app, new_layer(&app)), tool()); +} + +/// The new text is in the foreground colour, not the active layer's blue, and the tool's stored +/// options are what they were. +#[test] +fn the_colour_and_the_stored_options_are_unchanged() { + let (mut app, _) = alpha_active(); + app.run("tools.setColors", json!({"foreground": "#ff0000"})).unwrap(); + click(&mut app, 60.0, 320.0, Modifiers::NONE); + let id = new_layer(&app); + let t = text_layer(&app.session.active().unwrap().doc, id).unwrap(); + let c = t.char_runs().into_iter().next().unwrap().style.color.to_rgb(); + assert!(c[0] > 0.9 && c[1] < 0.1 && c[2] < 0.1, "the foreground red: {c:?}"); + assert_eq!(tool_options(&app), tool()); +} + +#[test] +fn a_box_drag_and_a_text_layer_in_a_group_keep_the_tools_options() { + let (mut app, _) = alpha_active(); + tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 250.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: 300.0, y: 350.0 }, Modifiers::NONE); + assert_eq!(style(&app, new_layer(&app)), tool(), "a paragraph box"); + + let (mut app, alpha) = alpha_active(); + app.run("layer.groupLayers", json!({"layer": alpha.0})).unwrap(); + app.run("layer.select", json!({"layer": alpha.0})).unwrap(); + click(&mut app, 60.0, 320.0, Modifiers::NONE); + assert_eq!(style(&app, new_layer(&app)), tool(), "Alpha inside a group"); +} diff --git a/crates/ui-egui/src/tools/type_tool/pointer.rs b/crates/ui-egui/src/tools/type_tool/pointer.rs index a74668b565..9af5a34c35 100644 --- a/crates/ui-egui/src/tools/type_tool/pointer.rs +++ b/crates/ui-egui/src/tools/type_tool/pointer.rs @@ -13,7 +13,7 @@ use craft::ui::canvas::{CanvasTool, ToolAction, ToolContext, ToolHostResponse, T use craft::ui::primitives::{Draw, PaintShape, StrokeColor, StrokeWidth}; use craft::ui::style::DataColor; -use super::defaults::{default_style_params, mask_style_from_active_layer}; +use super::defaults::{default_style_params, mask_style_from_active_layer, type_style_from_active_layer}; use super::editing::begin_edit; use super::layout::{hit_layer, offset_at}; @@ -253,11 +253,14 @@ pub(crate) fn create_params(app: &PhotocraftApp, is_box: bool, s: &TypeSnapshot) // #12077: the measured case - a click, not a paragraph box, with the Horizontal Type Mask over // a horizontal text layer - starts the mask in that layer's font, style and size, as the // options bar shows them for the layer. The vertical mask and a box drag are not measured in - // the issue, so they keep today's tool options. - if mask - && !is_box - && s.tool == crate::state::Tool::HorizontalTypeMask - && let Some(style) = mask_style_from_active_layer(app) + // the issue, so they keep today's tool options. #17913: a Type tool click does the same. + let style = match s.tool { + _ if is_box => None, + crate::state::Tool::HorizontalTypeMask if mask => mask_style_from_active_layer(app), + crate::state::Tool::Type => type_style_from_active_layer(app), + _ => None, + }; + if let Some(style) = style && let (Some(params), Some(style)) = (p.as_object_mut(), style.as_object()) { params.extend(style.clone()); @@ -455,3 +458,7 @@ mod mask_click_text_layer_tests; #[cfg(test)] #[path = "mask_active_layer_style_tests.rs"] mod mask_active_layer_style_tests; + +#[cfg(test)] +#[path = "new_text_active_style_tests.rs"] +mod new_text_active_style_tests;