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