Skip to main content

cranpose_ui/
text_field_modifier_node.rs

1//! Text field modifier node for editable text input.
2//!
3//! This module implements the modifier node for `BasicTextField`, following
4//! Jetpack Compose's `CoreTextFieldNode` architecture.
5//!
6//! The node handles:
7//! - **Layout**: Measures text content and returns appropriate size
8//! - **Draw**: Renders text, cursor, and selection highlights
9//! - **Pointer Input**: Handles tap to position cursor, drag for selection
10//! - **Semantics**: Provides text content for accessibility
11//!
12//! # Architecture
13//!
14//! Unlike display-only `TextModifierNode`, this node:
15//! - References a `TextFieldState` for mutable text
16//! - Tracks focus state for cursor visibility
17//! - Handles pointer events for cursor positioning
18
19use cranpose_core::{mutableStateOf, MutableState};
20use cranpose_foundation::text::{TextFieldLineLimits, TextFieldState, TextRange};
21use cranpose_foundation::{
22    Constraints, DelegatableNode, DrawModifierNode, DrawScope, InvalidationKind,
23    LayoutModifierNode, Measurable, ModifierNode, ModifierNodeContext, ModifierNodeElement,
24    NodeCapabilities, NodeState, PointerEvent, PointerEventKind, PointerInputNode, PointerSource,
25    SemanticsConfiguration, SemanticsNode, Size,
26};
27use cranpose_ui_graphics::{Brush, Color, Point};
28use std::cell::{Cell, RefCell};
29use std::hash::{Hash, Hasher};
30use std::rc::Rc;
31
32/// Live geometry a `BasicTextField` needs to place and drive its finger
33/// selection handles: whether the field is focused and was touched, its
34/// on-screen origin (window coordinates) and the metrics that map a window
35/// position back to a text offset.
36#[derive(Clone, Copy, PartialEq, Debug)]
37pub struct TextFieldHandleMetrics {
38    pub focused: bool,
39    pub touch: bool,
40    /// Field node's top-left in window coordinates.
41    pub node_origin: Point,
42    pub padding_left: f32,
43    pub padding_top: f32,
44    pub scroll_offset: f32,
45    pub line_height: f32,
46    /// Width the field wrapped its text at (`None` for single-line fields).
47    /// Lets the handles resolve the same visual (wrapped) lines the caret does.
48    pub wrap_width: Option<f32>,
49}
50
51/// Shared channel by which a `TextFieldModifierNode` publishes its live handle
52/// [`TextFieldHandleMetrics`] to the `BasicTextField` composable that renders
53/// the handles. Reads subscribe reactively (backed by a revision `MutableState`)
54/// so the composable recomposes when the field's focus/geometry changes.
55#[derive(Clone)]
56pub struct TextFieldHandleController {
57    inner: Rc<TextFieldHandleControllerInner>,
58}
59
60impl PartialEq for TextFieldHandleController {
61    fn eq(&self, other: &Self) -> bool {
62        Rc::ptr_eq(&self.inner, &other.inner)
63    }
64}
65
66struct TextFieldHandleControllerInner {
67    metrics: Cell<Option<TextFieldHandleMetrics>>,
68    revision: MutableState<u64>,
69}
70
71impl TextFieldHandleController {
72    /// Creates a controller. Must run with an active runtime (i.e. inside a
73    /// composition, via `remember`).
74    pub fn new() -> Self {
75        Self {
76            inner: Rc::new(TextFieldHandleControllerInner {
77                metrics: Cell::new(None),
78                revision: mutableStateOf(0u64),
79            }),
80        }
81    }
82
83    /// Publishes fresh metrics, waking any reader only when they actually
84    /// changed (so a resting frame does not spin recomposition).
85    pub(crate) fn publish(&self, metrics: TextFieldHandleMetrics) {
86        if self.inner.metrics.get() != Some(metrics) {
87            self.inner.metrics.set(Some(metrics));
88            self.inner
89                .revision
90                .update(|value| *value = value.wrapping_add(1));
91        }
92    }
93
94    /// Reads the latest metrics, subscribing the current recompose scope to
95    /// future changes.
96    pub fn metrics(&self) -> Option<TextFieldHandleMetrics> {
97        let _ = self.inner.revision.value();
98        self.inner.metrics.get()
99    }
100}
101
102impl Default for TextFieldHandleController {
103    fn default() -> Self {
104        Self::new()
105    }
106}
107
108/// Default cursor color (white - visible on dark backgrounds)
109const DEFAULT_CURSOR_COLOR: Color = Color(1.0, 1.0, 1.0, 1.0);
110
111/// Default selection highlight color (light blue with transparency)
112const DEFAULT_SELECTION_COLOR: Color = Color(0.0, 0.5, 1.0, 0.3);
113
114/// Default line height for empty text fields
115const DEFAULT_LINE_HEIGHT: f32 = 20.0;
116
117/// Cursor width in pixels
118const CURSOR_WIDTH: f32 = 2.0;
119
120/// Computes the horizontal scroll (pan) offset that keeps the cursor visible
121/// inside the viewport of a single-line text field.
122///
123/// Mirrors Jetpack Compose's `TextFieldScrollerPosition.coerceOffset` behavior:
124/// - the offset only changes when the cursor would leave the viewport,
125/// - the offset is clamped so the text never detaches from the left edge and
126///   never scrolls further than needed to show the end of the text (plus the
127///   cursor width, so a cursor at the end of the text stays visible).
128///
129/// All values are in px within the field's content coordinate space.
130pub(crate) fn compute_horizontal_scroll_offset(
131    current_offset: f32,
132    cursor_x: f32,
133    text_width: f32,
134    viewport_width: f32,
135) -> f32 {
136    if viewport_width <= 0.0 {
137        return 0.0;
138    }
139    let max_offset = (text_width + CURSOR_WIDTH - viewport_width).max(0.0);
140    let mut offset = current_offset.clamp(0.0, max_offset);
141    let visible_end = offset + viewport_width - CURSOR_WIDTH;
142    if cursor_x > visible_end {
143        // Cursor ran past the right edge: pan so it sits at the right edge.
144        offset = cursor_x - viewport_width + CURSOR_WIDTH;
145    } else if cursor_x < offset {
146        // Cursor ran past the left edge: pan so it sits at the left edge.
147        offset = cursor_x;
148    }
149    offset.clamp(0.0, max_offset)
150}
151
152/// Intersects `rect` with `bounds`, returning `None` when nothing remains.
153///
154/// Used to clip selection/cursor/composition primitives to the field's
155/// viewport so they never draw outside the field bounds.
156pub(crate) fn intersect_rect(
157    rect: cranpose_ui_graphics::Rect,
158    bounds: cranpose_ui_graphics::Rect,
159) -> Option<cranpose_ui_graphics::Rect> {
160    let x0 = rect.x.max(bounds.x);
161    let y0 = rect.y.max(bounds.y);
162    let x1 = (rect.x + rect.width).min(bounds.x + bounds.width);
163    let y1 = (rect.y + rect.height).min(bounds.y + bounds.height);
164    (x1 > x0 && y1 > y0).then_some(cranpose_ui_graphics::Rect {
165        x: x0,
166        y: y0,
167        width: x1 - x0,
168        height: y1 - y0,
169    })
170}
171
172/// Resolver that recomputes (and stores) the horizontal pan offset for a
173/// text field given the current content viewport width in px.
174pub type TextPanResolver = Rc<dyn Fn(f32) -> f32>;
175
176/// Resolves the caret's visual `(line_index, line_start_byte)` for byte
177/// `offset`.
178///
179/// For a wrapping (multi-line) field this lays the text out at the same wrap
180/// width the field measured and returns the VISUAL line the caret sits on, so
181/// the drawn caret lands on the same glyph the renderer draws. For a
182/// non-wrapping field (single line, or when no wrap width is known yet) it falls
183/// back to counting logical `\n` lines. Shared by the in-content caret and the
184/// overlay selection handles so both agree.
185pub(crate) fn caret_visual_line_for_offset(
186    text: &str,
187    style: &TextStyle,
188    node_id: Option<cranpose_core::NodeId>,
189    wrap_width: Option<f32>,
190    offset: usize,
191) -> (usize, usize) {
192    let offset = offset.min(text.len());
193    match wrap_width {
194        Some(width) if width.is_finite() && width > 0.0 => {
195            let annotated = crate::text::AnnotatedString::from(text);
196            let ranges = crate::text::wrapped_line_ranges(
197                node_id,
198                &annotated,
199                style,
200                crate::text::TextLayoutOptions::default(),
201                Some(width),
202            );
203            crate::text_selection::caret_visual_line(&ranges, offset)
204        }
205        _ => {
206            let before = &text[..offset];
207            let line_index = before.matches('\n').count();
208            let line_start = before.rfind('\n').map(|i| i + 1).unwrap_or(0);
209            (line_index, line_start)
210        }
211    }
212}
213
214/// Window-space (pre-clip) rects covering byte range `start..end`, one per
215/// VISUAL (wrapped) line the range touches, each spanning the full
216/// `line_height`.
217///
218/// Shared by the selection highlight and the composition-preedit underline so
219/// both track soft-wrapping exactly as the renderer and caret do. Splitting on
220/// logical `\n` alone draws the rect on the wrong line whenever a line above
221/// the range soft-wraps (the x stays right, the y lands one visual line too
222/// high). Iterating the same wrapped ranges the renderer lays out keeps them in
223/// sync.
224#[allow(clippy::too_many_arguments)]
225pub(crate) fn range_visual_line_rects(
226    text: &str,
227    style: &TextStyle,
228    node_id: Option<cranpose_core::NodeId>,
229    wrap_width: Option<f32>,
230    padding_left: f32,
231    padding_top: f32,
232    pan: f32,
233    line_height: f32,
234    start: usize,
235    end: usize,
236) -> Vec<cranpose_ui_graphics::Rect> {
237    if start >= end {
238        return Vec::new();
239    }
240    let annotated = crate::text::AnnotatedString::from(text);
241    let line_ranges = crate::text::wrapped_line_ranges(
242        node_id,
243        &annotated,
244        style,
245        crate::text::TextLayoutOptions::default(),
246        wrap_width,
247    );
248    let mut rects = Vec::new();
249    for (line_idx, line_range) in line_ranges.iter().enumerate() {
250        let line_start = line_range.start;
251        let line_end = line_range.end;
252        if end <= line_start || start >= line_end {
253            continue;
254        }
255        let seg_start = start.max(line_start);
256        let seg_end = end.min(line_end);
257        let x0 = crate::text::measure_text(
258            &crate::text::AnnotatedString::from(&text[line_start..seg_start]),
259            style,
260        )
261        .width
262            + padding_left
263            - pan;
264        let x1 = crate::text::measure_text(
265            &crate::text::AnnotatedString::from(&text[line_start..seg_end]),
266            style,
267        )
268        .width
269            + padding_left
270            - pan;
271        let width = x1 - x0;
272        if width > 0.0 {
273            rects.push(cranpose_ui_graphics::Rect {
274                x: x0,
275                y: padding_top + line_idx as f32 * line_height,
276                width,
277                height: line_height,
278            });
279        }
280    }
281    rects
282}
283
284/// Shared references for text field input handling.
285///
286/// This struct bundles the shared state references passed to the pointer input handler,
287/// reducing the argument count for `create_handler` from 8 individual `Rc` parameters
288/// to a single struct (fixing clippy::too_many_arguments).
289#[derive(Clone)]
290pub(crate) struct TextFieldRefs {
291    /// Whether this field is currently focused
292    pub is_focused: Rc<RefCell<bool>>,
293    /// Content offset from left (padding) for accurate click positioning
294    pub content_offset: Rc<Cell<f32>>,
295    /// Content offset from top (padding) for cursor Y positioning
296    pub content_y_offset: Rc<Cell<f32>>,
297    /// Drag anchor position (byte offset) for click-drag selection
298    pub drag_anchor: Rc<Cell<Option<usize>>>,
299    /// Last click time for double/triple-click detection
300    pub last_click_time: Rc<Cell<Option<web_time::Instant>>>,
301    /// Last click screen position, for multi-tap slop gating
302    pub last_click_pos: Rc<Cell<Option<(f32, f32)>>>,
303    /// Click count (1=single, 2=double, 3=triple)
304    pub click_count: Rc<Cell<u8>>,
305    /// Node ID for scoped layout invalidation
306    pub node_id: Rc<Cell<Option<cranpose_core::NodeId>>>,
307    /// Horizontal scroll (pan) offset in px for single-line fields.
308    /// Keeps the cursor visible when the text is wider than the field.
309    pub scroll_offset: Rc<Cell<f32>>,
310    /// Device source of the most recent pointer press on the field. Drives
311    /// touch-only affordances: finger selection handles are shown for touch /
312    /// stylus presses, while a mouse keeps a clean caret.
313    pub last_pointer_source: Rc<Cell<PointerSource>>,
314    /// Field node's top-left in window coordinates, derived from the most recent
315    /// pointer event (`global_position - position`). Used to place selection
316    /// handles in the top-level overlay, which is in window space.
317    pub node_origin: Rc<Cell<Point>>,
318}
319
320impl TextFieldRefs {
321    /// Creates a new set of shared references.
322    pub fn new() -> Self {
323        Self {
324            is_focused: Rc::new(RefCell::new(false)),
325            content_offset: Rc::new(Cell::new(0.0_f32)),
326            content_y_offset: Rc::new(Cell::new(0.0_f32)),
327            drag_anchor: Rc::new(Cell::new(None::<usize>)),
328            last_click_time: Rc::new(Cell::new(None::<web_time::Instant>)),
329            last_click_pos: Rc::new(Cell::new(None::<(f32, f32)>)),
330            click_count: Rc::new(Cell::new(0_u8)),
331            node_id: Rc::new(Cell::new(None::<cranpose_core::NodeId>)),
332            scroll_offset: Rc::new(Cell::new(0.0_f32)),
333            last_pointer_source: Rc::new(Cell::new(PointerSource::Unknown)),
334            node_origin: Rc::new(Cell::new(Point { x: 0.0, y: 0.0 })),
335        }
336    }
337}
338
339/// Modifier node for editable text fields.
340///
341/// This node is the core of `BasicTextField`, handling:
342/// - Text measurement and layout
343/// - Cursor and selection rendering
344/// - Pointer input for cursor positioning
345use crate::text::TextStyle; // Add import
346
347pub struct TextFieldModifierNode {
348    /// The text field state (shared)
349    state: TextFieldState,
350    /// Shared references for input handling
351    refs: TextFieldRefs,
352    /// Text style
353    style: TextStyle, // Add style
354    /// Cursor brush color
355    cursor_brush: Brush,
356    /// Selection highlight brush
357    selection_brush: Brush,
358    /// Line limits configuration
359    line_limits: TextFieldLineLimits,
360    /// Cached text value for change detection
361    cached_text: String,
362    /// Cached selection for change detection
363    cached_selection: TextRange,
364    /// Node state for delegation
365    node_state: NodeState,
366    /// Measured size cache (shared with the draw closure as the pan viewport)
367    measured_size: Rc<Cell<Size>>,
368    /// Cached line height from last measurement (shared with draw closure)
369    measured_line_height: Rc<Cell<f32>>,
370    /// Wrap width the last measurement laid the text out at (`None` for
371    /// single-line fields, which pan instead of wrapping). Shared with the draw
372    /// closure so the caret and selection handles resolve the same *visual*
373    /// (wrapped) lines the renderer draws, instead of counting only logical
374    /// `\n` lines.
375    measured_wrap_width: Rc<Cell<Option<f32>>>,
376    /// Cached pointer input handler
377    cached_handler: Rc<dyn Fn(PointerEvent)>,
378    /// Cached horizontal pan resolver (recomputes + stores the scroll offset)
379    cached_pan_resolver: TextPanResolver,
380    /// Channel to publish live handle metrics to the `BasicTextField`
381    /// composable that renders the finger selection handles. `None` when the
382    /// field is used without handle support.
383    handle_controller: Option<TextFieldHandleController>,
384}
385
386impl std::fmt::Debug for TextFieldModifierNode {
387    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
388        f.debug_struct("TextFieldModifierNode")
389            .field("text", &self.state.text())
390            .field("style", &self.style)
391            .field("is_focused", &*self.refs.is_focused.borrow())
392            .finish()
393    }
394}
395
396// Re-export from extracted module
397use crate::text_field_handler::TextFieldHandler;
398
399impl TextFieldModifierNode {
400    /// Creates a new text field modifier node.
401    pub fn new(state: TextFieldState, style: TextStyle) -> Self {
402        let value = state.value();
403        let refs = TextFieldRefs::new();
404        let line_limits = TextFieldLineLimits::default();
405        let cached_handler =
406            Self::create_handler(state.clone(), refs.clone(), line_limits, style.clone());
407        let cached_pan_resolver =
408            Self::create_pan_resolver(state.clone(), refs.clone(), line_limits, style.clone());
409
410        Self {
411            state,
412            refs,
413            style,
414            cursor_brush: Brush::solid(DEFAULT_CURSOR_COLOR),
415            selection_brush: Brush::solid(DEFAULT_SELECTION_COLOR),
416            line_limits,
417            cached_text: value.text,
418            cached_selection: value.selection,
419            node_state: NodeState::new(),
420            measured_size: Rc::new(Cell::new(Size {
421                width: 0.0,
422                height: 0.0,
423            })),
424            measured_line_height: Rc::new(Cell::new(DEFAULT_LINE_HEIGHT)),
425            measured_wrap_width: Rc::new(Cell::new(None)),
426            cached_handler,
427            cached_pan_resolver,
428            handle_controller: None,
429        }
430    }
431
432    /// Creates a node with custom line limits.
433    pub fn with_line_limits(mut self, line_limits: TextFieldLineLimits) -> Self {
434        self.line_limits = line_limits;
435        self.cached_pan_resolver = Self::create_pan_resolver(
436            self.state.clone(),
437            self.refs.clone(),
438            line_limits,
439            self.style.clone(),
440        );
441        self
442    }
443
444    /// Installs the controller the field publishes live handle metrics to.
445    pub fn with_handle_controller(mut self, controller: TextFieldHandleController) -> Self {
446        self.handle_controller = Some(controller);
447        self
448    }
449
450    /// Creates the horizontal pan resolver closure.
451    ///
452    /// The resolver takes the content viewport width (px) and returns the
453    /// horizontal scroll offset that keeps the cursor visible, storing the
454    /// result in `refs.scroll_offset` so pointer input and rendering agree.
455    /// It recomputes from the live state so layout, the render scene builder,
456    /// and the draw closure all observe the same value within a frame.
457    fn create_pan_resolver(
458        state: TextFieldState,
459        refs: TextFieldRefs,
460        line_limits: TextFieldLineLimits,
461        style: TextStyle,
462    ) -> TextPanResolver {
463        Rc::new(move |viewport_width: f32| {
464            if !line_limits.is_single_line() {
465                // Multi-line fields do not pan horizontally.
466                refs.scroll_offset.set(0.0);
467                return 0.0;
468            }
469            let text = state.text();
470            let pos = state.selection().start.min(text.len());
471            let text_width = crate::text::measure_text(
472                &crate::text::AnnotatedString::from(text.as_str()),
473                &style,
474            )
475            .width;
476            let cursor_x = crate::text::measure_text(
477                &crate::text::AnnotatedString::from(&text[..pos]),
478                &style,
479            )
480            .width;
481            let offset = compute_horizontal_scroll_offset(
482                refs.scroll_offset.get(),
483                cursor_x,
484                text_width,
485                viewport_width,
486            );
487            refs.scroll_offset.set(offset);
488            offset
489        })
490    }
491
492    /// Returns the pan resolver for single-line fields, `None` for multi-line.
493    ///
494    /// Exposed to the modifier slices so the render scene builder can pan the
495    /// text glyphs by the same offset used for the cursor and selection.
496    pub fn text_pan_resolver(&self) -> Option<TextPanResolver> {
497        self.line_limits
498            .is_single_line()
499            .then(|| self.cached_pan_resolver.clone())
500    }
501
502    /// Returns the current horizontal scroll (pan) offset in px.
503    pub fn scroll_offset(&self) -> f32 {
504        self.refs.scroll_offset.get()
505    }
506
507    /// Returns the current line limits configuration.
508    pub fn line_limits(&self) -> TextFieldLineLimits {
509        self.line_limits
510    }
511
512    /// Creates the pointer input handler closure.
513    fn create_handler(
514        state: TextFieldState,
515        refs: TextFieldRefs,
516        line_limits: TextFieldLineLimits,
517        style: TextStyle, // Add style
518    ) -> Rc<dyn Fn(PointerEvent)> {
519        // Tap-count classification plus word/line/paragraph boundaries drive the
520        // multi-tap selection granularity gestures.
521        use crate::text_selection::{
522            classify_tap_count, find_line_boundaries, find_paragraph_boundaries,
523            resolve_selection_tap_count, tap_selection_granularity, SelectionGranularity,
524            MULTI_TAP_SLOP_PX, MULTI_TAP_TIMEOUT_MS,
525        };
526        use crate::word_boundaries::find_word_boundaries;
527
528        Rc::new(move |event: PointerEvent| {
529            // Seed the field node's window-space origin from this pointer event
530            // so the very first handle placement after a tap has a value even
531            // before the next layout pass runs. The layout pass
532            // (`window_origin_sink`) is the authoritative source that keeps it
533            // fresh as the field scrolls; both agree (`global - local` equals
534            // the composited window origin at rest).
535            refs.node_origin.set(Point {
536                x: event.global_position.x - event.position.x,
537                y: event.global_position.y - event.position.y,
538            });
539
540            // Account for content padding offsets and the horizontal pan
541            // offset (single-line fields pan to keep the cursor visible, so
542            // clicks must map back into text space).
543            let click_x =
544                (event.position.x - refs.content_offset.get() + refs.scroll_offset.get()).max(0.0);
545            let click_y = (event.position.y - refs.content_y_offset.get()).max(0.0);
546
547            match event.kind {
548                PointerEventKind::Down => {
549                    // Remember the device that pressed so the draw closure can
550                    // show finger selection handles for touch/stylus and keep a
551                    // clean caret for a mouse.
552                    refs.last_pointer_source.set(event.source);
553
554                    // Request focus with O(1) handler, passing node_id and line
555                    // limits for key handling plus the live geometry cells the
556                    // layout keeps fresh, so coordinate-based platform text input
557                    // (iOS caret positioning) can read the caret's window rect.
558                    let handler = TextFieldHandler::new(
559                        state.clone(),
560                        refs.node_id.get(),
561                        line_limits,
562                        crate::text_field_handler::CaretGeometryRefs {
563                            node_origin: refs.node_origin.clone(),
564                            content_offset: refs.content_offset.clone(),
565                            content_y_offset: refs.content_y_offset.clone(),
566                            scroll_offset: refs.scroll_offset.clone(),
567                            style: style.clone(),
568                        },
569                    );
570                    crate::text_field_focus::request_focus(refs.is_focused.clone(), handler);
571
572                    let now = web_time::Instant::now();
573                    let text = state.text();
574                    let pos = crate::text::get_offset_for_position(
575                        &crate::text::AnnotatedString::from(text.as_str()),
576                        &style,
577                        click_x,
578                        click_y,
579                    );
580
581                    // Classify the press into a 1-based tap count by both the
582                    // time since and the distance from the previous press (a tap
583                    // far from the last one starts a fresh single tap, matching
584                    // Android's double-tap slop).
585                    let previous = refs.last_click_pos.get().and_then(|(px, py)| {
586                        let count = refs.click_count.get();
587                        (count > 0).then_some((count, px, py))
588                    });
589                    let elapsed_ms = refs
590                        .last_click_time
591                        .get()
592                        .map(|last| now.duration_since(last).as_millis())
593                        .unwrap_or(u128::MAX);
594                    let tap_count = classify_tap_count(
595                        previous,
596                        elapsed_ms,
597                        event.position.x,
598                        event.position.y,
599                        MULTI_TAP_TIMEOUT_MS,
600                        MULTI_TAP_SLOP_PX,
601                    );
602
603                    // A lone tap that lands INSIDE an existing (non-collapsed)
604                    // selection selects the word under the finger (Android/iOS
605                    // "tap the selection to re-grab a word"). Tapping the SAME
606                    // spot again grows the granularity word → line → paragraph →
607                    // word …, keyed on location so it keeps escalating even when
608                    // the taps arrive too slowly to count as a rapid multi-tap.
609                    // A lone tap elsewhere just places the caret.
610                    let selection = state.selection();
611                    let tap_in_selection =
612                        !selection.collapsed() && pos >= selection.min() && pos <= selection.max();
613                    // Same-spot repeat, independent of the multi-tap timeout:
614                    // within slop of the previous press.
615                    let repeat_in_place = refs
616                        .last_click_pos
617                        .get()
618                        .map(|(px, py)| {
619                            let dx = event.position.x - px;
620                            let dy = event.position.y - py;
621                            dx * dx + dy * dy <= MULTI_TAP_SLOP_PX * MULTI_TAP_SLOP_PX
622                        })
623                        .unwrap_or(false);
624                    let effective_count = resolve_selection_tap_count(
625                        tap_count,
626                        refs.click_count.get(),
627                        tap_in_selection,
628                        repeat_in_place,
629                    );
630
631                    match tap_selection_granularity(effective_count) {
632                        SelectionGranularity::Paragraph => {
633                            // Fourth tap: grow to the whole paragraph.
634                            let (start, end) = find_paragraph_boundaries(&text, pos);
635                            state.edit(|buffer| {
636                                buffer.select(TextRange::new(start, end));
637                            });
638                            refs.drag_anchor.set(Some(start));
639                        }
640                        SelectionGranularity::Line => {
641                            // Triple tap: select the line.
642                            let (line_start, line_end) = find_line_boundaries(&text, pos);
643                            state.edit(|buffer| {
644                                buffer.select(TextRange::new(line_start, line_end));
645                            });
646                            refs.drag_anchor.set(Some(line_start));
647                        }
648                        SelectionGranularity::Word => {
649                            // Double tap (or a tap inside an existing selection):
650                            // select the word.
651                            let (word_start, word_end) = find_word_boundaries(&text, pos);
652                            state.edit(|buffer| {
653                                buffer.select(TextRange::new(word_start, word_end));
654                            });
655                            refs.drag_anchor.set(Some(word_start));
656                        }
657                        SelectionGranularity::Caret => {
658                            // Single tap: place the cursor.
659                            refs.drag_anchor.set(Some(pos));
660                            state.edit(|buffer| {
661                                buffer.place_cursor_before_char(pos);
662                            });
663                        }
664                    }
665
666                    refs.click_count.set(effective_count);
667                    refs.last_click_time.set(Some(now));
668                    refs.last_click_pos
669                        .set(Some((event.position.x, event.position.y)));
670                    event.consume();
671                }
672                PointerEventKind::Move => {
673                    // If we have a drag anchor, extend selection during drag
674                    if let Some(anchor) = refs.drag_anchor.get() {
675                        if *refs.is_focused.borrow() {
676                            let text = state.text();
677                            let current_pos = crate::text::get_offset_for_position(
678                                &crate::text::AnnotatedString::from(text.as_str()),
679                                &style,
680                                click_x,
681                                click_y,
682                            );
683
684                            // Update selection directly (without undo stack push)
685                            state.set_selection(TextRange::new(anchor, current_pos));
686
687                            // Selection change only needs redraw, not layout
688                            crate::request_render_invalidation();
689
690                            event.consume();
691                        }
692                    }
693                }
694                PointerEventKind::Up => {
695                    // Clear drag anchor on mouse up
696                    refs.drag_anchor.set(None);
697                }
698                _ => {}
699            }
700        })
701    }
702
703    /// Creates a node with custom cursor color.
704    pub fn with_cursor_color(mut self, color: Color) -> Self {
705        self.cursor_brush = Brush::solid(color);
706        self
707    }
708
709    /// Sets the focus state.
710    pub fn set_focused(&mut self, focused: bool) {
711        let current = *self.refs.is_focused.borrow();
712        if current != focused {
713            *self.refs.is_focused.borrow_mut() = focused;
714        }
715    }
716
717    /// Returns whether the field is focused.
718    pub fn is_focused(&self) -> bool {
719        *self.refs.is_focused.borrow()
720    }
721
722    /// Returns the is_focused Rc for closure capture.
723    pub fn is_focused_rc(&self) -> Rc<RefCell<bool>> {
724        self.refs.is_focused.clone()
725    }
726
727    /// Returns the content_offset Rc for closure capture.
728    pub fn content_offset_rc(&self) -> Rc<Cell<f32>> {
729        self.refs.content_offset.clone()
730    }
731
732    /// Returns the content_y_offset Rc for closure capture.
733    pub fn content_y_offset_rc(&self) -> Rc<Cell<f32>> {
734        self.refs.content_y_offset.clone()
735    }
736
737    /// Returns the shared cell the field's composited window origin is written
738    /// into (window coordinates of the field node's top-left).
739    ///
740    /// The layout pass writes the field's TRUE on-screen origin here every frame
741    /// — resolved through all ancestor placements (a scrolling `LazyColumn` /
742    /// `vertical_scroll` offsets its items via placement, which the layout tree
743    /// bakes into each node's absolute rect) plus ancestor graphics-layer
744    /// translations. The draw closure reads it back to publish handle metrics,
745    /// so the finger selection/cursor handles anchor at (and their window→offset
746    /// inverse mapping agrees with) the field's real glyphs even while the list
747    /// scrolls. Without this the origin was only ever sampled from the last
748    /// pointer event and went stale the moment the field scrolled.
749    pub(crate) fn window_origin_sink(&self) -> Rc<Cell<Point>> {
750        self.refs.node_origin.clone()
751    }
752
753    /// Returns the current text.
754    pub fn text(&self) -> String {
755        self.state.text()
756    }
757
758    pub fn style(&self) -> &TextStyle {
759        &self.style
760    }
761
762    /// Returns the current selection.
763    pub fn selection(&self) -> TextRange {
764        self.state.selection()
765    }
766
767    /// Returns the cursor brush for rendering.
768    pub fn cursor_brush(&self) -> Brush {
769        self.cursor_brush.clone()
770    }
771
772    /// Returns the selection brush for rendering selection highlight.
773    pub fn selection_brush(&self) -> Brush {
774        self.selection_brush.clone()
775    }
776
777    /// Inserts text at the current cursor position (for paste operations).
778    pub fn insert_text(&mut self, text: &str) {
779        self.state.edit(|buffer| {
780            buffer.insert(text);
781        });
782    }
783
784    /// Copies the selected text and returns it (for web copy operation).
785    /// Returns None if no selection.
786    pub fn copy_selection(&self) -> Option<String> {
787        self.state.copy_selection()
788    }
789
790    /// Cuts the selected text: copies and deletes it.
791    /// Returns the cut text, or None if no selection.
792    pub fn cut_selection(&mut self) -> Option<String> {
793        let text = self.copy_selection();
794        if text.is_some() {
795            self.state.edit(|buffer| {
796                buffer.delete(buffer.selection());
797            });
798        }
799        text
800    }
801
802    /// Returns a clone of the text field state for use in draw closures.
803    /// This allows reading selection at DRAW time rather than LAYOUT time.
804    pub fn get_state(&self) -> cranpose_foundation::text::TextFieldState {
805        self.state.clone()
806    }
807
808    /// Updates the content offset (padding.left) for accurate click-to-position cursor placement.
809    /// Called from slices collection where padding is known.
810    pub fn set_content_offset(&self, offset: f32) {
811        self.refs.content_offset.set(offset);
812    }
813
814    /// Updates the content Y offset (padding.top) for cursor Y positioning.
815    /// Called from slices collection where padding is known.
816    pub fn set_content_y_offset(&self, offset: f32) {
817        self.refs.content_y_offset.set(offset);
818    }
819
820    /// The wrap width a multi-line field lays its text out at, or `None` when
821    /// the text must not wrap (single-line fields pan horizontally instead).
822    ///
823    /// Multi-line fields wrap at the available content width exactly like the
824    /// render scene builder, so the measured height reflects every wrapped line
825    /// and the field grows to fit its content instead of clipping it.
826    fn wrap_width(&self, available_width: f32) -> Option<f32> {
827        (!self.line_limits.is_single_line() && available_width.is_finite() && available_width > 0.0)
828            .then_some(available_width)
829    }
830
831    /// Measures the text content using node-identity-based caching.
832    ///
833    /// `wrap_width` bounds the layout width so multi-line text wraps; `None`
834    /// measures the natural single-line width (single-line fields, intrinsic
835    /// width queries).
836    fn measure_text_content(&self, wrap_width: Option<f32>) -> Size {
837        let text = self.state.text();
838        let node_id = self.refs.node_id.get();
839        let annotated = crate::text::AnnotatedString::from(text.as_str());
840        let metrics = match wrap_width {
841            Some(max_width) => crate::text::measure_text_with_options_for_node(
842                node_id,
843                &annotated,
844                &self.style,
845                crate::text::TextLayoutOptions::default(),
846                Some(max_width),
847            ),
848            None => crate::text::measure_text_for_node(node_id, &annotated, &self.style),
849        };
850        self.measured_line_height.set(metrics.line_height);
851        Size {
852            width: metrics.width,
853            height: metrics.height,
854        }
855    }
856
857    /// Updates cached state and returns true if changed.
858    fn update_cached_state(&mut self) -> bool {
859        let value = self.state.value();
860        let text_changed = value.text != self.cached_text;
861        let selection_changed = value.selection != self.cached_selection;
862
863        if text_changed {
864            self.cached_text = value.text;
865        }
866        if selection_changed {
867            self.cached_selection = value.selection;
868        }
869
870        text_changed || selection_changed
871    }
872
873    /// Positions cursor at a given x offset within the text.
874    /// Uses proper text layout hit testing for accurate proportional font support.
875    pub fn position_cursor_at_offset(&self, x_offset: f32) {
876        let text = self.state.text();
877        if text.is_empty() {
878            self.state.edit(|buffer| {
879                buffer.place_cursor_at_start();
880            });
881            return;
882        }
883
884        // Use proper text layout hit testing instead of character-based calculation.
885        // Map the viewport-relative offset into text space by adding the pan offset.
886        let byte_offset = crate::text::get_offset_for_position(
887            &crate::text::AnnotatedString::from(text.as_str()),
888            &self.style,
889            x_offset + self.refs.scroll_offset.get(),
890            0.0,
891        );
892
893        self.state.edit(|buffer| {
894            buffer.place_cursor_before_char(byte_offset);
895        });
896    }
897
898    // NOTE: Key event handling is done via TextFieldHandler::handle_key() which is
899    // registered with the focus system for O(1) dispatch. DO NOT add a handle_key_event()
900    // method here - it would be duplicate code that never gets called.
901}
902
903impl DelegatableNode for TextFieldModifierNode {
904    fn node_state(&self) -> &NodeState {
905        &self.node_state
906    }
907}
908
909impl ModifierNode for TextFieldModifierNode {
910    fn on_attach(&mut self, context: &mut dyn ModifierNodeContext) {
911        // Store node_id for scoped layout invalidation (avoids O(app) global invalidation)
912        self.refs.node_id.set(context.node_id());
913
914        context.invalidate(InvalidationKind::Layout);
915        context.invalidate(InvalidationKind::Draw);
916        context.invalidate(InvalidationKind::Semantics);
917    }
918
919    fn as_draw_node(&self) -> Option<&dyn DrawModifierNode> {
920        Some(self)
921    }
922
923    fn as_draw_node_mut(&mut self) -> Option<&mut dyn DrawModifierNode> {
924        Some(self)
925    }
926
927    fn as_layout_node(&self) -> Option<&dyn LayoutModifierNode> {
928        Some(self)
929    }
930
931    fn as_layout_node_mut(&mut self) -> Option<&mut dyn LayoutModifierNode> {
932        Some(self)
933    }
934
935    fn as_semantics_node(&self) -> Option<&dyn SemanticsNode> {
936        Some(self)
937    }
938
939    fn as_semantics_node_mut(&mut self) -> Option<&mut dyn SemanticsNode> {
940        Some(self)
941    }
942
943    fn as_pointer_input_node(&self) -> Option<&dyn PointerInputNode> {
944        Some(self)
945    }
946
947    fn as_pointer_input_node_mut(&mut self) -> Option<&mut dyn PointerInputNode> {
948        Some(self)
949    }
950}
951
952impl LayoutModifierNode for TextFieldModifierNode {
953    fn measure(
954        &self,
955        _context: &mut dyn ModifierNodeContext,
956        _measurable: &dyn Measurable,
957        constraints: Constraints,
958    ) -> cranpose_ui_layout::LayoutModifierMeasureResult {
959        // Measure the text content, wrapping multi-line fields at the available
960        // width so the field grows to fit every wrapped line instead of
961        // clipping content past the first line.
962        let wrap_width = self.wrap_width(constraints.max_width);
963        // Remember the wrap width so the draw closure can resolve the same
964        // visual (wrapped) lines when placing the caret and selection handles.
965        self.measured_wrap_width.set(wrap_width);
966        let text_size = self.measure_text_content(wrap_width);
967
968        // Add minimum height for empty text (cursor needs space)
969        let min_height = if text_size.height < 1.0 {
970            DEFAULT_LINE_HEIGHT
971        } else {
972            text_size.height
973        };
974
975        // Constrain to provided constraints
976        let width = text_size
977            .width
978            .max(constraints.min_width)
979            .min(constraints.max_width);
980        let height = min_height
981            .max(constraints.min_height)
982            .min(constraints.max_height);
983
984        let size = Size { width, height };
985        self.measured_size.set(size);
986
987        // Refresh the horizontal pan offset so it is up to date for pointer
988        // input and rendering even before the next draw pass runs.
989        let _ = (self.cached_pan_resolver)(size.width);
990
991        cranpose_ui_layout::LayoutModifierMeasureResult::with_size(size)
992    }
993
994    fn min_intrinsic_width(&self, _measurable: &dyn Measurable, _height: f32) -> f32 {
995        self.measure_text_content(None).width
996    }
997
998    fn max_intrinsic_width(&self, _measurable: &dyn Measurable, _height: f32) -> f32 {
999        self.measure_text_content(None).width
1000    }
1001
1002    fn min_intrinsic_height(&self, _measurable: &dyn Measurable, width: f32) -> f32 {
1003        self.measure_text_content(self.wrap_width(width))
1004            .height
1005            .max(DEFAULT_LINE_HEIGHT)
1006    }
1007
1008    fn max_intrinsic_height(&self, _measurable: &dyn Measurable, width: f32) -> f32 {
1009        self.measure_text_content(self.wrap_width(width))
1010            .height
1011            .max(DEFAULT_LINE_HEIGHT)
1012    }
1013}
1014
1015impl DrawModifierNode for TextFieldModifierNode {
1016    fn draw(&self, _draw_scope: &mut dyn DrawScope) {
1017        // No-op: Cursor and selection are rendered via create_draw_closure() which
1018        // creates DrawPrimitive::Rect directly. This enables draw-time evaluation
1019        // of focus state and cursor blink timing.
1020    }
1021
1022    fn create_draw_closure(
1023        &self,
1024    ) -> Option<Rc<dyn Fn(cranpose_foundation::Size) -> Vec<cranpose_ui_graphics::DrawPrimitive>>>
1025    {
1026        use cranpose_ui_graphics::DrawPrimitive;
1027
1028        // Capture state via Rc clone (cheap) for draw-time evaluation
1029        let is_focused = self.refs.is_focused.clone();
1030        let state = self.state.clone();
1031        let content_offset = self.refs.content_offset.clone();
1032        let content_y_offset = self.refs.content_y_offset.clone();
1033        let cursor_brush = self.cursor_brush.clone();
1034        let selection_brush = self.selection_brush.clone();
1035        let style = self.style.clone();
1036        let cached_line_height = self.measured_line_height.clone();
1037        let measured_size = self.measured_size.clone();
1038        let measured_wrap_width = self.measured_wrap_width.clone();
1039        let node_id = self.refs.node_id.clone();
1040        let pan_resolver = self.cached_pan_resolver.clone();
1041        let handle_controller = self.handle_controller.clone();
1042        let node_origin = self.refs.node_origin.clone();
1043        let last_pointer_source = self.refs.last_pointer_source.clone();
1044
1045        Some(Rc::new(move |size| {
1046            // Check focus at DRAW time
1047            if !*is_focused.borrow() {
1048                // Publish an unfocused snapshot so the composable clears any
1049                // finger handles when the field loses focus.
1050                if let Some(controller) = &handle_controller {
1051                    controller.publish(TextFieldHandleMetrics {
1052                        focused: false,
1053                        touch: false,
1054                        node_origin: node_origin.get(),
1055                        padding_left: 0.0,
1056                        padding_top: 0.0,
1057                        scroll_offset: 0.0,
1058                        line_height: cached_line_height.get(),
1059                        wrap_width: measured_wrap_width.get(),
1060                    });
1061                }
1062                return vec![];
1063            }
1064
1065            let mut primitives = Vec::new();
1066
1067            let text = state.text();
1068            let selection = state.selection();
1069            let padding_left = content_offset.get();
1070            let padding_top = content_y_offset.get();
1071            // Reuse line_height from the most recent layout measurement
1072            // instead of re-measuring the full text.
1073            let line_height = cached_line_height.get();
1074
1075            // Content viewport (excludes padding). Fall back to the node size
1076            // when measurement has not run yet.
1077            let measured = measured_size.get();
1078            let viewport_width = if measured.width > 0.0 {
1079                measured.width
1080            } else {
1081                (size.width - padding_left).max(0.0)
1082            };
1083            let viewport_height = if measured.height > 0.0 {
1084                measured.height
1085            } else {
1086                (size.height - padding_top).max(0.0)
1087            };
1088            // Horizontal pan that keeps the cursor visible (0 for multi-line).
1089            let pan = pan_resolver(viewport_width);
1090
1091            // Publish live geometry so the `BasicTextField` composable can place
1092            // and drive the finger selection handles.
1093            if let Some(controller) = &handle_controller {
1094                controller.publish(TextFieldHandleMetrics {
1095                    focused: true,
1096                    touch: last_pointer_source.get().is_touch_like(),
1097                    node_origin: node_origin.get(),
1098                    padding_left,
1099                    padding_top,
1100                    scroll_offset: pan,
1101                    line_height,
1102                    wrap_width: measured_wrap_width.get(),
1103                });
1104            }
1105            // Everything the field draws (selection, IME underline, cursor)
1106            // is clipped to the content viewport so primitives never extend
1107            // outside the field bounds.
1108            let clip_bounds = cranpose_ui_graphics::Rect {
1109                x: padding_left,
1110                y: padding_top,
1111                width: viewport_width,
1112                height: viewport_height,
1113            };
1114
1115            // Draw selection highlight
1116            if !selection.collapsed() {
1117                let sel_start = selection.min();
1118                let sel_end = selection.max();
1119
1120                // Highlight per VISUAL (wrapped) line so it lands on the same
1121                // glyphs the renderer draws.
1122                for sel_rect in range_visual_line_rects(
1123                    &text,
1124                    &style,
1125                    node_id.get(),
1126                    measured_wrap_width.get(),
1127                    padding_left,
1128                    padding_top,
1129                    pan,
1130                    line_height,
1131                    sel_start,
1132                    sel_end,
1133                ) {
1134                    if let Some(clipped) = intersect_rect(sel_rect, clip_bounds) {
1135                        primitives.push(DrawPrimitive::Rect {
1136                            rect: clipped,
1137                            brush: selection_brush.clone(),
1138                        });
1139                    }
1140                }
1141            }
1142
1143            // Draw composition (IME preedit) underline
1144            // This shows the user which text is being composed by the input method
1145            if let Some(comp_range) = state.composition() {
1146                let comp_start = comp_range.min();
1147                let comp_end = comp_range.max();
1148
1149                if comp_start < comp_end && comp_end <= text.len() {
1150                    // Underline color: slightly transparent white/gray
1151                    let underline_brush = cranpose_ui_graphics::Brush::solid(
1152                        cranpose_ui_graphics::Color(0.8, 0.8, 0.8, 0.8),
1153                    );
1154                    let underline_height: f32 = 2.0;
1155
1156                    // Per-visual-line rects, shrunk to a strip at the bottom of
1157                    // each line — same wrap-aware layout as the selection.
1158                    for line_rect in range_visual_line_rects(
1159                        &text,
1160                        &style,
1161                        node_id.get(),
1162                        measured_wrap_width.get(),
1163                        padding_left,
1164                        padding_top,
1165                        pan,
1166                        line_height,
1167                        comp_start,
1168                        comp_end,
1169                    ) {
1170                        let underline_rect = cranpose_ui_graphics::Rect {
1171                            x: line_rect.x,
1172                            y: line_rect.y + line_height - underline_height,
1173                            width: line_rect.width,
1174                            height: underline_height,
1175                        };
1176                        if let Some(clipped) = intersect_rect(underline_rect, clip_bounds) {
1177                            primitives.push(DrawPrimitive::Rect {
1178                                rect: clipped,
1179                                brush: underline_brush.clone(),
1180                            });
1181                        }
1182                    }
1183                }
1184            }
1185
1186            // Draw cursor - check visibility at DRAW time for blinking
1187            if crate::cursor_animation::is_cursor_visible() {
1188                let pos = selection.start.min(text.len());
1189                // Resolve the caret's VISUAL (wrapped) line so it lands on the
1190                // same glyph the renderer draws — the field wraps long lines, and
1191                // counting only logical `\n` lines would draw the caret on the
1192                // wrong line (and, with the full logical-line-prefix width, off
1193                // the right edge) while typing/the magnifier stay correct.
1194                let (line_index, line_start) = caret_visual_line_for_offset(
1195                    &text,
1196                    &style,
1197                    node_id.get(),
1198                    measured_wrap_width.get(),
1199                    pos,
1200                );
1201                let cursor_x = crate::text::measure_text(
1202                    &crate::text::AnnotatedString::from(&text[line_start..pos]),
1203                    &style,
1204                )
1205                .width
1206                    + padding_left
1207                    - pan;
1208                let cursor_y = padding_top + line_index as f32 * line_height;
1209
1210                let cursor_rect = cranpose_ui_graphics::Rect {
1211                    x: cursor_x,
1212                    y: cursor_y,
1213                    width: CURSOR_WIDTH,
1214                    height: line_height,
1215                };
1216
1217                if let Some(clipped) = intersect_rect(cursor_rect, clip_bounds) {
1218                    primitives.push(DrawPrimitive::Rect {
1219                        rect: clipped,
1220                        brush: cursor_brush.clone(),
1221                    });
1222                }
1223            }
1224
1225            primitives
1226        }))
1227    }
1228}
1229
1230impl SemanticsNode for TextFieldModifierNode {
1231    fn merge_semantics(&self, config: &mut SemanticsConfiguration) {
1232        let text = self.state.text();
1233        config.content_description = Some(text);
1234        config.is_editable_text = true;
1235        config.text_selection = Some(self.state.selection());
1236    }
1237}
1238
1239impl PointerInputNode for TextFieldModifierNode {
1240    fn on_pointer_event(
1241        &mut self,
1242        _context: &mut dyn ModifierNodeContext,
1243        _event: &PointerEvent,
1244    ) -> bool {
1245        // No-op: All pointer handling is done via pointer_input_handler() closure.
1246        // This follows Jetpack Compose's delegation pattern where the node simply
1247        // forwards to a delegated pointer input handler (see TextFieldDecoratorModifier.kt:741-747).
1248        //
1249        // The cached_handler closure handles:
1250        // - Focus request on Down
1251        // - Cursor positioning
1252        // - Double-click word selection
1253        // - Triple-click select all
1254        // - Drag selection
1255        false
1256    }
1257
1258    fn hit_test(&self, x: f32, y: f32) -> bool {
1259        // Check if point is within measured bounds
1260        let size = self.measured_size.get();
1261        x >= 0.0 && x <= size.width && y >= 0.0 && y <= size.height
1262    }
1263
1264    fn pointer_input_handler(&self) -> Option<Rc<dyn Fn(PointerEvent)>> {
1265        // Return cached handler for pointer input dispatch
1266        Some(self.cached_handler.clone())
1267    }
1268}
1269
1270// ============================================================================
1271// TextFieldElement - Creates and updates TextFieldModifierNode
1272// ============================================================================
1273
1274/// Element that creates and updates `TextFieldModifierNode` instances.
1275///
1276/// This follows the modifier element pattern where the element is responsible for:
1277/// - Creating new nodes (via `create`)
1278/// - Updating existing nodes when properties change (via `update`)
1279/// - Declaring capabilities (LAYOUT | DRAW | SEMANTICS)
1280#[derive(Clone)]
1281pub struct TextFieldElement {
1282    /// The text field state
1283    state: TextFieldState,
1284    /// Text style
1285    style: TextStyle,
1286    /// Cursor color
1287    cursor_color: Color,
1288    /// Line limits configuration
1289    line_limits: TextFieldLineLimits,
1290    /// Channel the node publishes live handle metrics to (finger selection
1291    /// handles). `None` disables handle support.
1292    handle_controller: Option<TextFieldHandleController>,
1293}
1294
1295impl TextFieldElement {
1296    /// Creates a new text field element.
1297    pub fn new(state: TextFieldState, style: TextStyle) -> Self {
1298        Self {
1299            state,
1300            style,
1301            cursor_color: DEFAULT_CURSOR_COLOR,
1302            line_limits: TextFieldLineLimits::default(),
1303            handle_controller: None,
1304        }
1305    }
1306
1307    /// Creates an element with custom cursor color.
1308    pub fn with_cursor_color(mut self, color: Color) -> Self {
1309        self.cursor_color = color;
1310        self
1311    }
1312
1313    /// Creates an element with custom line limits.
1314    pub fn with_line_limits(mut self, line_limits: TextFieldLineLimits) -> Self {
1315        self.line_limits = line_limits;
1316        self
1317    }
1318
1319    /// Installs the finger-handle metrics channel shared with the composable.
1320    pub fn with_handle_controller(mut self, controller: TextFieldHandleController) -> Self {
1321        self.handle_controller = Some(controller);
1322        self
1323    }
1324}
1325
1326impl std::fmt::Debug for TextFieldElement {
1327    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1328        f.debug_struct("TextFieldElement")
1329            .field("text", &self.state.text())
1330            .field("style", &self.style)
1331            .field("cursor_color", &self.cursor_color)
1332            .finish()
1333    }
1334}
1335
1336impl Hash for TextFieldElement {
1337    fn hash<H: Hasher>(&self, state: &mut H) {
1338        // Hash by state Rc pointer identity - matches PartialEq
1339        // This ensures equal elements hash equal (correctness requirement)
1340        std::ptr::hash(std::rc::Rc::as_ptr(&self.state.inner), state);
1341        // Hash cursor color
1342        self.cursor_color.0.to_bits().hash(state);
1343        self.cursor_color.1.to_bits().hash(state);
1344        self.cursor_color.2.to_bits().hash(state);
1345        self.cursor_color.3.to_bits().hash(state);
1346        self.style.render_hash().hash(state);
1347        self.line_limits.hash(state);
1348    }
1349}
1350
1351impl PartialEq for TextFieldElement {
1352    fn eq(&self, other: &Self) -> bool {
1353        // Compare by state identity (same Rc), cursor color, and line limits
1354        // This ensures node reuse when same state is passed, while detecting
1355        // actual changes that require updates
1356        self.state == other.state
1357            && self.style == other.style
1358            && self.cursor_color == other.cursor_color
1359            && self.line_limits == other.line_limits
1360    }
1361}
1362
1363impl Eq for TextFieldElement {}
1364
1365impl ModifierNodeElement for TextFieldElement {
1366    type Node = TextFieldModifierNode;
1367
1368    fn create(&self) -> Self::Node {
1369        let mut node = TextFieldModifierNode::new(self.state.clone(), self.style.clone())
1370            .with_cursor_color(self.cursor_color)
1371            .with_line_limits(self.line_limits);
1372        if let Some(controller) = self.handle_controller.clone() {
1373            node = node.with_handle_controller(controller);
1374        }
1375        node
1376    }
1377
1378    fn update(&self, node: &mut Self::Node) {
1379        // Update the state reference
1380        node.state = self.state.clone();
1381        node.style = self.style.clone();
1382        node.cursor_brush = Brush::solid(self.cursor_color);
1383        node.line_limits = self.line_limits;
1384        node.handle_controller = self.handle_controller.clone();
1385
1386        // Recreate the cached handler with the new state but same refs
1387        node.cached_handler = TextFieldModifierNode::create_handler(
1388            node.state.clone(),
1389            node.refs.clone(),
1390            node.line_limits,
1391            self.style.clone(),
1392        );
1393
1394        // Recreate the pan resolver so it captures the new state/style/limits
1395        node.cached_pan_resolver = TextFieldModifierNode::create_pan_resolver(
1396            node.state.clone(),
1397            node.refs.clone(),
1398            node.line_limits,
1399            self.style.clone(),
1400        );
1401
1402        // Check if content changed and update cache
1403        if node.update_cached_state() {
1404            // Content changed - node will need layout/draw invalidation
1405            // This happens automatically through the modifier reconciliation
1406        }
1407    }
1408
1409    fn capabilities(&self) -> NodeCapabilities {
1410        NodeCapabilities::LAYOUT
1411            | NodeCapabilities::DRAW
1412            | NodeCapabilities::SEMANTICS
1413            | NodeCapabilities::POINTER_INPUT
1414    }
1415
1416    fn always_update(&self) -> bool {
1417        // Always update to capture new state/handler while preserving focus state
1418        true
1419    }
1420}
1421
1422#[cfg(test)]
1423mod tests {
1424    use super::*;
1425    use crate::text::TextStyle;
1426    use cranpose_core::{DefaultScheduler, Runtime};
1427    use std::sync::Arc;
1428
1429    /// Sets up a test runtime and keeps it alive for the duration of the test.
1430    fn with_test_runtime<T>(f: impl FnOnce() -> T) -> T {
1431        let _runtime = Runtime::new(Arc::new(DefaultScheduler));
1432        f()
1433    }
1434
1435    #[test]
1436    fn text_field_node_creation() {
1437        let _app_context = crate::render_state::app_context_test_scope();
1438        with_test_runtime(|| {
1439            let state = TextFieldState::new("Hello");
1440            let node = TextFieldModifierNode::new(state, TextStyle::default());
1441            assert_eq!(node.text(), "Hello");
1442            assert!(!node.is_focused());
1443        });
1444    }
1445
1446    // Regression: a selection (or preedit) whose logical line sits *below* a
1447    // soft-wrapped line must highlight on the correct VISUAL line. The old
1448    // logical-`\n` split placed it one line too high whenever a line above
1449    // wrapped — the reported "correct x, wrong y line" iOS selection bug.
1450    #[test]
1451    fn selection_rects_follow_wrapped_visual_lines() {
1452        let _app_context = crate::render_state::app_context_test_scope();
1453        // Monospaced test measurer: 14.0 * 0.6 = 8.4 px per char. Wrap width 30
1454        // fits 3 chars (25.2) but not 4 (33.6), so "aaaaa" wraps to "aaa"/"aa".
1455        let text = "aaaaa\nbb";
1456        let style = TextStyle::default();
1457        let line_height = 10.0_f32;
1458
1459        // Select "bb" — logical line 1, but VISUAL line 2 (two visual lines
1460        // above it: "aaa", "aa").
1461        let rects = range_visual_line_rects(
1462            text,
1463            &style,
1464            None,
1465            Some(30.0),
1466            0.0,
1467            0.0,
1468            0.0,
1469            line_height,
1470            6,
1471            8,
1472        );
1473        assert_eq!(rects.len(), 1, "one visual line touched, got {rects:?}");
1474        assert_eq!(
1475            rects[0].y,
1476            2.0 * line_height,
1477            "highlight must land on visual line 2, not logical line 1"
1478        );
1479        assert!(rects[0].width > 0.0);
1480
1481        // A selection spanning the wrap boundary produces one rect per visual
1482        // line, at consecutive y positions.
1483        let spanning = range_visual_line_rects(
1484            text,
1485            &style,
1486            None,
1487            Some(30.0),
1488            0.0,
1489            0.0,
1490            0.0,
1491            line_height,
1492            0,
1493            5,
1494        );
1495        assert_eq!(spanning.len(), 2, "wrapped line spans two visual rows");
1496        assert_eq!(spanning[0].y, 0.0);
1497        assert_eq!(spanning[1].y, line_height);
1498    }
1499
1500    #[test]
1501    fn text_field_node_focus() {
1502        let _app_context = crate::render_state::app_context_test_scope();
1503        with_test_runtime(|| {
1504            let state = TextFieldState::new("Test");
1505            let mut node = TextFieldModifierNode::new(state, TextStyle::default());
1506            assert!(!node.is_focused());
1507
1508            node.set_focused(true);
1509            assert!(node.is_focused());
1510
1511            node.set_focused(false);
1512            assert!(!node.is_focused());
1513        });
1514    }
1515
1516    #[test]
1517    fn text_field_element_creates_node() {
1518        let _app_context = crate::render_state::app_context_test_scope();
1519        with_test_runtime(|| {
1520            let state = TextFieldState::new("Hello World");
1521            let element = TextFieldElement::new(state, TextStyle::default());
1522
1523            let node = element.create();
1524            assert_eq!(node.text(), "Hello World");
1525        });
1526    }
1527
1528    /// End-to-end guard for the touch-vs-mouse finger-handle pipeline through
1529    /// the real pointer handler and draw closure: a Touch-source press makes the
1530    /// focused field publish `touch = true` (so `SelectionHandles` shows the
1531    /// finger cursor/selection handles and the Copy/Cut/Paste popup), while a
1532    /// Mouse-source press publishes `touch = false` (a clean caret, no handles).
1533    /// The published `touch` flag is exactly what the overlay is gated on, so
1534    /// this pins the source → `last_pointer_source` → metrics link the Android
1535    /// touch handles depend on.
1536    #[test]
1537    fn touch_press_publishes_touch_handle_metrics_but_mouse_does_not() {
1538        use cranpose_foundation::{PointerEvent, PointerEventKind, PointerSource};
1539        use cranpose_ui_graphics::Point;
1540
1541        let _app_context = crate::render_state::app_context_test_scope();
1542        with_test_runtime(|| {
1543            let state = TextFieldState::new("hello world");
1544            let controller = TextFieldHandleController::new();
1545            let node = TextFieldModifierNode::new(state.clone(), TextStyle::default())
1546                .with_handle_controller(controller.clone());
1547            // Give the field a measured size so the draw closure has geometry.
1548            node.measured_size.set(Size {
1549                width: 120.0,
1550                height: 20.0,
1551            });
1552
1553            let handler = node
1554                .pointer_input_handler()
1555                .expect("field exposes a pointer handler");
1556            let draw = node
1557                .create_draw_closure()
1558                .expect("field exposes a draw closure");
1559            let at = Point { x: 12.0, y: 8.0 };
1560            let size = Size {
1561                width: 120.0,
1562                height: 20.0,
1563            };
1564
1565            // Touch tap: the field focuses and remembers the touch source, so
1566            // its draw closure publishes touch = true.
1567            handler(
1568                PointerEvent::new(PointerEventKind::Down, at, at).with_source(PointerSource::Touch),
1569            );
1570            let _ = draw(size);
1571            let metrics = controller
1572                .metrics()
1573                .expect("focused field publishes handle metrics");
1574            assert!(metrics.focused, "a tap focuses the field");
1575            assert!(
1576                metrics.touch,
1577                "a touch tap must publish touch = true so the finger handles show"
1578            );
1579
1580            // Mouse tap on the same field: the source flips to mouse, so the
1581            // field publishes touch = false (clean caret, no finger handles).
1582            handler(
1583                PointerEvent::new(PointerEventKind::Down, at, at).with_source(PointerSource::Mouse),
1584            );
1585            let _ = draw(size);
1586            let metrics = controller
1587                .metrics()
1588                .expect("focused field publishes handle metrics");
1589            assert!(
1590                !metrics.touch,
1591                "a mouse tap must publish touch = false (clean caret, no finger handle)"
1592            );
1593
1594            crate::text_field_focus::clear_focus();
1595        });
1596    }
1597
1598    /// A double tap on a word must select that word. This regressed after
1599    /// selection handles began appearing inside `LazyColumn` items in 0.1.39:
1600    /// the cursor handle shown by the first tap overlapped the text line and
1601    /// consumed the second tap. The field's own gesture classification (proven
1602    /// here) is correct — two quick taps at the same spot escalate to a word
1603    /// selection — so the fix is geometric (keep the handle's touch box off the
1604    /// text line; see `selection_handle::handle_shape`).
1605    #[test]
1606    fn double_tap_selects_the_word_under_the_finger() {
1607        use cranpose_foundation::{PointerEvent, PointerEventKind, PointerSource};
1608        use cranpose_ui_graphics::Point;
1609
1610        let _app_context = crate::render_state::app_context_test_scope();
1611        with_test_runtime(|| {
1612            let state = TextFieldState::new("hello world");
1613            let node = TextFieldModifierNode::new(state.clone(), TextStyle::default());
1614            node.measured_size.set(Size {
1615                width: 200.0,
1616                height: 20.0,
1617            });
1618            let handler = node
1619                .pointer_input_handler()
1620                .expect("field exposes a pointer handler");
1621
1622            // Two touch taps at the same spot, back to back (well within the
1623            // multi-tap timeout and slop): near the start of "hello".
1624            let at = Point { x: 2.0, y: 8.0 };
1625            handler(
1626                PointerEvent::new(PointerEventKind::Down, at, at).with_source(PointerSource::Touch),
1627            );
1628            handler(
1629                PointerEvent::new(PointerEventKind::Down, at, at).with_source(PointerSource::Touch),
1630            );
1631
1632            let selection = state.selection();
1633            assert!(
1634                !selection.collapsed(),
1635                "a double tap must produce a (word) selection, got {selection:?}"
1636            );
1637            let selected = &state.text()[selection.min()..selection.max()];
1638            assert_eq!(
1639                selected, "hello",
1640                "double tap should select the whole word under the finger"
1641            );
1642
1643            crate::text_field_focus::clear_focus();
1644        });
1645    }
1646
1647    /// The multi-tap selection granularity ladder (bug 8): repeated in-place taps
1648    /// escalate word → line → paragraph, then cycle back to word. Mirrors mature
1649    /// editors (Android `TextView`, iOS, VS Code).
1650    #[test]
1651    fn repeated_taps_escalate_word_line_paragraph_then_cycle() {
1652        use cranpose_foundation::{PointerEvent, PointerEventKind, PointerSource};
1653        use cranpose_ui_graphics::Point;
1654
1655        let _app_context = crate::render_state::app_context_test_scope();
1656        with_test_runtime(|| {
1657            // Two lines in the first paragraph, a blank line, then a second
1658            // paragraph — so line and paragraph selections differ.
1659            let text = "alpha beta\ngamma delta\n\nsecond para";
1660            let state = TextFieldState::new(text);
1661            let node = TextFieldModifierNode::new(state.clone(), TextStyle::default())
1662                .with_line_limits(TextFieldLineLimits::MultiLine {
1663                    min_lines: 1,
1664                    max_lines: usize::MAX,
1665                });
1666            node.measured_size.set(Size {
1667                width: 400.0,
1668                height: 80.0,
1669            });
1670            let handler = node
1671                .pointer_input_handler()
1672                .expect("field exposes a pointer handler");
1673
1674            // Tap in place on the first line ("alpha").
1675            let at = Point { x: 2.0, y: 4.0 };
1676            let tap = || {
1677                handler(
1678                    PointerEvent::new(PointerEventKind::Down, at, at)
1679                        .with_source(PointerSource::Touch),
1680                );
1681            };
1682            let selected = |state: &TextFieldState| {
1683                let s = state.selection();
1684                state.text()[s.min()..s.max()].to_string()
1685            };
1686
1687            tap(); // 1 → caret
1688            assert!(state.selection().collapsed(), "first tap places the caret");
1689            tap(); // 2 → word
1690            assert_eq!(selected(&state), "alpha", "double tap selects the word");
1691            tap(); // 3 → line
1692            assert_eq!(
1693                selected(&state),
1694                "alpha beta",
1695                "triple tap selects the line"
1696            );
1697            tap(); // 4 → paragraph
1698            assert_eq!(
1699                selected(&state),
1700                "alpha beta\ngamma delta",
1701                "fourth tap grows to the paragraph"
1702            );
1703            tap(); // 5 → cycles back to word
1704            assert_eq!(
1705                selected(&state),
1706                "alpha",
1707                "fifth tap cycles back to the word"
1708            );
1709
1710            crate::text_field_focus::clear_focus();
1711        });
1712    }
1713
1714    /// A single tap that lands inside an existing selection re-grabs the word
1715    /// under the finger (Android/iOS behaviour), rather than collapsing to a
1716    /// caret (bug 8).
1717    #[test]
1718    fn single_tap_inside_selection_selects_the_word() {
1719        use cranpose_foundation::{PointerEvent, PointerEventKind, PointerSource};
1720        use cranpose_ui_graphics::Point;
1721
1722        let _app_context = crate::render_state::app_context_test_scope();
1723        with_test_runtime(|| {
1724            let state = TextFieldState::new("hello world");
1725            let node = TextFieldModifierNode::new(state.clone(), TextStyle::default());
1726            node.measured_size.set(Size {
1727                width: 200.0,
1728                height: 20.0,
1729            });
1730            let handler = node
1731                .pointer_input_handler()
1732                .expect("field exposes a pointer handler");
1733
1734            // Pre-existing broad selection over the whole text.
1735            state.edit(|buffer| buffer.select(TextRange::new(0, 11)));
1736            assert!(!state.selection().collapsed());
1737
1738            // A lone tap over "hello" (fresh tap count) must select that word,
1739            // not drop the selection.
1740            let at = Point { x: 2.0, y: 8.0 };
1741            handler(
1742                PointerEvent::new(PointerEventKind::Down, at, at).with_source(PointerSource::Touch),
1743            );
1744
1745            let selection = state.selection();
1746            assert!(
1747                !selection.collapsed(),
1748                "a tap inside a selection must not collapse it, got {selection:?}"
1749            );
1750            assert_eq!(
1751                &state.text()[selection.min()..selection.max()],
1752                "hello",
1753                "a tap inside a selection re-selects the word under the finger"
1754            );
1755
1756            crate::text_field_focus::clear_focus();
1757        });
1758    }
1759
1760    /// Bug (c) at the handler level: repeated taps at the SAME spot inside an
1761    /// existing selection climb the granularity ladder word → line → paragraph →
1762    /// word even when each tap arrives after the multi-tap timeout has lapsed
1763    /// (the growth is keyed on location, not the double-tap timer). Forcing a
1764    /// timeout between taps (clearing `last_click_time`) makes the raw tap count
1765    /// reset to 1 each time, so this exercises the location-based path rather
1766    /// than the rapid-multi-tap path.
1767    #[test]
1768    fn slow_taps_inside_selection_cycle_word_line_paragraph_by_location() {
1769        use cranpose_foundation::{PointerEvent, PointerEventKind, PointerSource};
1770        use cranpose_ui_graphics::Point;
1771
1772        let _app_context = crate::render_state::app_context_test_scope();
1773        with_test_runtime(|| {
1774            let text = "alpha beta\ngamma delta\n\nsecond para";
1775            let state = TextFieldState::new(text);
1776            let node = TextFieldModifierNode::new(state.clone(), TextStyle::default())
1777                .with_line_limits(TextFieldLineLimits::MultiLine {
1778                    min_lines: 1,
1779                    max_lines: usize::MAX,
1780                });
1781            node.measured_size.set(Size {
1782                width: 400.0,
1783                height: 80.0,
1784            });
1785            let handler = node
1786                .pointer_input_handler()
1787                .expect("field exposes a pointer handler");
1788
1789            // A broad pre-existing selection over the whole text.
1790            state.edit(|buffer| buffer.select(TextRange::new(0, text.len())));
1791
1792            let at = Point { x: 2.0, y: 4.0 };
1793            let selected = |state: &TextFieldState| {
1794                let s = state.selection();
1795                state.text()[s.min()..s.max()].to_string()
1796            };
1797            // Each call forces the multi-tap timer to look expired, so the raw
1798            // tap count resets to 1 while the tap position stays put.
1799            let slow_tap = || {
1800                node.refs.last_click_time.set(None);
1801                handler(
1802                    PointerEvent::new(PointerEventKind::Down, at, at)
1803                        .with_source(PointerSource::Touch),
1804                );
1805            };
1806
1807            slow_tap(); // inside selection → word
1808            assert_eq!(
1809                selected(&state),
1810                "alpha",
1811                "tap inside selection grabs the word"
1812            );
1813            slow_tap(); // same spot → line
1814            assert_eq!(
1815                selected(&state),
1816                "alpha beta",
1817                "same-spot tap grows to the line even after the timeout"
1818            );
1819            slow_tap(); // same spot → paragraph
1820            assert_eq!(
1821                selected(&state),
1822                "alpha beta\ngamma delta",
1823                "same-spot tap grows to the paragraph"
1824            );
1825            slow_tap(); // same spot → cycles back to word
1826            assert_eq!(
1827                selected(&state),
1828                "alpha",
1829                "same-spot tap cycles back to the word"
1830            );
1831
1832            crate::text_field_focus::clear_focus();
1833        });
1834    }
1835
1836    #[test]
1837    fn text_field_element_equality() {
1838        let _app_context = crate::render_state::app_context_test_scope();
1839        with_test_runtime(|| {
1840            let state1 = TextFieldState::new("Hello");
1841            let state2 = TextFieldState::new("Hello"); // Different Rc, same text
1842
1843            let elem1 = TextFieldElement::new(state1.clone(), TextStyle::default());
1844            let elem2 = TextFieldElement::new(state1.clone(), TextStyle::default()); // Same state (Rc identity)
1845            let elem3 = TextFieldElement::new(state2, TextStyle::default()); // Different state
1846
1847            // Elements are equal only when they share the same state Rc
1848            // This ensures proper Eq/Hash contract compliance
1849            assert_eq!(elem1, elem2, "Same state should be equal");
1850            assert_ne!(elem1, elem3, "Different states should not be equal");
1851        });
1852    }
1853
1854    #[test]
1855    fn text_field_element_update_refreshes_existing_node_style() {
1856        let _app_context = crate::render_state::app_context_test_scope();
1857        with_test_runtime(|| {
1858            let state = TextFieldState::new("themed text");
1859            let dark_style = TextStyle::from_span_style(crate::text::SpanStyle {
1860                color: Some(Color::from_rgba_u8(228, 240, 252, 255)),
1861                ..crate::text::SpanStyle::default()
1862            });
1863            let light_style = TextStyle::from_span_style(crate::text::SpanStyle {
1864                color: Some(Color::from_rgba_u8(14, 58, 96, 255)),
1865                ..crate::text::SpanStyle::default()
1866            });
1867            let initial = TextFieldElement::new(state.clone(), dark_style);
1868            let updated = TextFieldElement::new(state, light_style.clone());
1869            let mut node = initial.create();
1870
1871            updated.update(&mut node);
1872
1873            assert_eq!(node.text(), "themed text");
1874            assert_eq!(node.style(), &light_style);
1875        });
1876    }
1877
1878    /// A multi-line field must measure the *wrapped* height at the available
1879    /// width, so a long transcript grows the field instead of being clipped to
1880    /// a single line. Regression for the "edits only appear after focus loss"
1881    /// bug where a wrapped OCR transcript rendered only its first line.
1882    #[test]
1883    fn multiline_field_measures_wrapped_height() {
1884        let _app_context = crate::render_state::app_context_test_scope();
1885        with_test_runtime(|| {
1886            let long = "abcd ".repeat(40); // ~200 chars, no explicit newlines
1887            let state = TextFieldState::new(&long);
1888            let node = TextFieldModifierNode::new(state, TextStyle::default());
1889            assert!(
1890                !node.line_limits().is_single_line(),
1891                "default fields are multi-line"
1892            );
1893
1894            let natural = node.measure_text_content(None);
1895            let wrapped = node.measure_text_content(node.wrap_width(20.0));
1896
1897            assert!(
1898                wrapped.height > natural.height,
1899                "wrapped multi-line height {} must exceed the single-line height {}",
1900                wrapped.height,
1901                natural.height
1902            );
1903        });
1904    }
1905
1906    /// Single-line fields pan horizontally instead of wrapping, so they never
1907    /// derive a wrap width even under a narrow constraint.
1908    #[test]
1909    fn single_line_field_never_wraps() {
1910        let _app_context = crate::render_state::app_context_test_scope();
1911        with_test_runtime(|| {
1912            let state = TextFieldState::new("abcd ".repeat(40));
1913            let node = TextFieldModifierNode::new(state, TextStyle::default())
1914                .with_line_limits(TextFieldLineLimits::SingleLine);
1915            assert_eq!(
1916                node.wrap_width(20.0),
1917                None,
1918                "single-line fields must not wrap"
1919            );
1920        });
1921    }
1922
1923    /// Test that cursor draw command position is calculated correctly.
1924    ///
1925    /// This test verifies that when we measure text width for cursor position:
1926    /// 1. The cursor x position = width of text before cursor
1927    /// 2. For text at cursor end, x = full text width
1928    #[test]
1929    fn test_cursor_x_position_calculation() {
1930        let _app_context = crate::render_state::app_context_test_scope();
1931        with_test_runtime(|| {
1932            // Test that text measurement works correctly for cursor positioning
1933            let style = crate::text::TextStyle::default();
1934
1935            // Empty text - cursor should be at x=0
1936            let empty_width =
1937                crate::text::measure_text(&crate::text::AnnotatedString::from(""), &style).width;
1938            assert!(
1939                empty_width.abs() < 0.1,
1940                "Empty text should have 0 width, got {}",
1941                empty_width
1942            );
1943
1944            // Non-empty text - cursor at end should be at text width
1945            let hi_width =
1946                crate::text::measure_text(&crate::text::AnnotatedString::from("Hi"), &style).width;
1947            assert!(
1948                hi_width > 0.0,
1949                "Text 'Hi' should have positive width: {}",
1950                hi_width
1951            );
1952
1953            // Partial text - cursor after 'H' should be at width of 'H'
1954            let h_width =
1955                crate::text::measure_text(&crate::text::AnnotatedString::from("H"), &style).width;
1956            assert!(h_width > 0.0, "Text 'H' should have positive width");
1957            assert!(
1958                h_width < hi_width,
1959                "'H' width {} should be less than 'Hi' width {}",
1960                h_width,
1961                hi_width
1962            );
1963
1964            // Verify TextFieldState selection tracks cursor correctly
1965            let state = TextFieldState::new("Hi");
1966            assert_eq!(
1967                state.selection().start,
1968                2,
1969                "Cursor should be at position 2 (end of 'Hi')"
1970            );
1971
1972            // The text before cursor at position 2 in "Hi" is "Hi" itself
1973            let text = state.text();
1974            let cursor_pos = state.selection().start;
1975            let text_before_cursor = &text[..cursor_pos.min(text.len())];
1976            assert_eq!(text_before_cursor, "Hi");
1977
1978            // So cursor x = width of "Hi"
1979            let cursor_x = crate::text::measure_text(
1980                &crate::text::AnnotatedString::from(text_before_cursor),
1981                &style,
1982            )
1983            .width;
1984            assert!(
1985                (cursor_x - hi_width).abs() < 0.1,
1986                "Cursor x {} should equal 'Hi' width {}",
1987                cursor_x,
1988                hi_width
1989            );
1990        });
1991    }
1992
1993    /// Test cursor is created when focused node is in slices.
1994    #[test]
1995    fn test_focused_node_creates_cursor() {
1996        let _app_context = crate::render_state::app_context_test_scope();
1997        with_test_runtime(|| {
1998            let state = TextFieldState::new("Test");
1999            let element = TextFieldElement::new(state.clone(), TextStyle::default());
2000            let node = element.create();
2001
2002            // Initially not focused
2003            assert!(!node.is_focused());
2004
2005            // Set focus
2006            *node.refs.is_focused.borrow_mut() = true;
2007            assert!(node.is_focused());
2008
2009            // Verify the node has correct text
2010            assert_eq!(node.text(), "Test");
2011
2012            // Verify selection is at end
2013            assert_eq!(node.selection().start, 4);
2014        });
2015    }
2016}