ltk/widget/text_edit/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Text input field — single-line or multiline. The widget itself
//! owns layout / draw; the runtime side of text editing
//! (insert, delete, cursor movement, selection, clipboard)
//! lives in the `event_loop::text_editing` private module.

use std::sync::Arc;

use crate::render::Canvas;
use crate::secure_mem::secure_zero;
use crate::types::{ Color, Length, Rect, WidgetId };

use super::Element;

pub( crate ) mod theme;
pub( crate ) mod wrapping;
pub( crate ) mod hit_test;
pub( crate ) mod cursor;
pub( crate ) mod metrics;
pub( crate ) mod scroll;
pub( crate ) mod tabs;
pub( crate ) mod gutter;
mod draw;
#[ cfg( test ) ]
mod tests;

pub use draw::password_toggle_hit_zone;
pub( crate ) use hit_test::byte_offset_at;
pub( crate ) use scroll::TextScroll;
pub( crate ) use cursor::{ cursor_visual_down, cursor_visual_end, cursor_visual_home, cursor_visual_page, cursor_visual_up };

/// Resolve the font size into a concrete logical px. `Some( len )` resolves
/// against [`Canvas::viewport_logical`] — the font space, so `× dpi_scale` at
/// raster yields the right physical size — exactly like the button label;
/// `None` follows the widget-scaling mode at the theme default. Shared by the
/// draw and hit-test paths, which all carry a canvas.
/// A run of a multiline field's value drawn in a colour of its own, as a
/// syntax highlighter marks keywords, strings and comments. `range` is in
/// bytes of the value and should fall on character boundaries; runs are
/// expected in order and not overlapping, and what no run covers takes the
/// field's colour.
#[ derive( Clone, Debug, PartialEq ) ]
pub struct Highlight
{
	pub range: std::ops::Range<usize>,
	pub color: Color,
}

/// The size of a field's text and, for a multiline field, its face,
/// which is the canvas's unless [`TextEdit::font`] gave another; every
/// path that lays the text out measures and draws it through this, so
/// they all agree.
#[ derive( Clone, Default ) ]
pub( crate ) struct TextFont
{
	pub size: Option<Length>,
	pub face: Option<Arc<crate::font_face::FontFace>>,
}

impl TextFont
{
	pub( crate ) fn measure( &self, canvas: &Canvas, text: &str, px: f32 ) -> f32
	{
		match &self.face
		{
			Some( face ) => canvas.measure_text_with_font( text, px, face ),
			None         => canvas.measure_text( text, px ),
		}
	}

	pub( crate ) fn measure_char( &self, canvas: &Canvas, ch: char, px: f32 ) -> f32
	{
		match &self.face
		{
			Some( face ) => canvas.measure_char_with_font( ch, px, face ),
			None         => canvas.measure_char( ch, px ),
		}
	}

	pub( crate ) fn draw( &self, canvas: &mut Canvas, text: &str, x: f32, y: f32, px: f32, color: Color )
	{
		match &self.face
		{
			Some( face ) => canvas.draw_text_with_font( text, x, y, px, color, face ),
			None         => canvas.draw_text( text, x, y, px, color ),
		}
	}

	/// Which face, for the caches keyed by it.
	pub( crate ) fn face_key( &self, canvas: &Canvas ) -> usize
	{
		match &self.face
		{
			Some( face ) => Arc::as_ptr( face ) as usize,
			None         => canvas.font() as *const crate::font_face::FontFace as usize,
		}
	}
}

pub( crate ) fn resolve_font_size( canvas: &Canvas, fs: Option<Length> ) -> f32
{
	fs
		.map( |l| canvas.resolve_font( l ) )
		.unwrap_or_else( || canvas.font_px( theme::FONT_SIZE ) )
}

/// A text input field.
///
/// Single-line by default; switches to a multi-row text-area via
/// [`Self::multiline`]. Single-line mode honours the optional inline
/// builders for picker-style fields:
///
/// * [`Self::align`] — horizontal alignment of the displayed text;
/// * [`Self::borderless`] — drop the surrounding pill / border so the
///   field can sit inside a parent that already paints its own
///   surface;
/// * [`Self::fixed_width`] — pin the preferred width to a specific
///   number of pixels instead of claiming `max_width`;
/// * [`Self::font_size`] — override the text font size;
/// * [`Self::select_on_focus`] — auto-select the value on focus so
///   the next keystroke replaces it (numeric pickers, short-form
///   inputs).
/// * [`Self::password_toggle`] — pin a built-in show / hide-password
///   eye icon to the right edge of the field; the bullet
///   substitution flips with the externally-owned `visible` state
///   on each tap.
///
/// ```rust,no_run
/// # use ltk::{ text_edit, Element };
/// # #[ derive( Clone ) ] enum Msg { UsernameChanged( String ), Submit }
/// # struct App { username: String }
/// # impl App { fn _ex( &self ) -> Element<Msg> {
/// text_edit( "Username", &self.username )
///     .on_change( |s| Msg::UsernameChanged( s ) )
///     .on_submit( Msg::Submit )
/// .into()
/// # }}
/// ```
///
/// ## Password field with show / hide toggle
///
/// ```rust,no_run
/// # use ltk::{ text_edit, Element };
/// # #[ derive( Clone ) ] enum Msg { PasswordChanged( String ), TogglePassword }
/// # struct App { password: String, password_visible: bool }
/// # impl App { fn _ex( &self ) -> Element<Msg> {
/// text_edit( "Password", &self.password )
///     .on_change( |s| Msg::PasswordChanged( s ) )
///     .password_toggle( self.password_visible, Msg::TogglePassword )
/// .into()
/// # }}
/// ```
///
/// `password_toggle` overrides [`Self::secure`] when both are set —
/// the toggle's `visible` parameter drives the bullet substitution
/// from then on. The widget still wipes the buffer on drop and
/// skips the IME registration (the same hardening
/// [`Self::secure`] gives) regardless of the current visibility,
/// so flipping the eye does not weaken the field's threat model
/// at runtime.
pub struct TextEdit<Msg: Clone>
{
	/// Placeholder text shown when the field is empty.
	pub( crate ) placeholder: String,
	/// Current field value.
	pub( crate ) value:       String,
	/// Callback invoked with the new value on every keystroke.
	/// `Arc` (not `Box`) so the layout pass can clone it into the per-leaf
	/// handler snapshot for O(1) dispatch on input events.
	pub( crate ) on_change:   Option<Arc<dyn Fn(String) -> Msg>>,
	/// Callback invoked with the cursor's byte offset whenever it moves.
	pub( crate ) on_cursor:   Option<Arc<dyn Fn( usize ) -> Msg>>,
	/// Message emitted when the user presses Enter.
	pub( crate ) on_submit:   Option<Msg>,
	/// Message emitted when the field takes the keyboard focus.
	pub( crate ) on_focus:    Option<Msg>,
	/// Colour of the value. `None` follows the theme.
	pub( crate ) color:       Option<Color>,
	/// When `true`, the value is rendered as bullet characters (password mode).
	pub( crate ) secure:      bool,
	/// What the field is for; picks the on-screen keyboard's extra row and the input method's content purpose.
	pub( crate ) purpose:     crate::osk::Purpose,
	/// When `true`, the widget renders as a multi-row text area: the
	/// box grows to [`Self::rows`] visible rows, line breaks in the
	/// value are honoured at draw time, and pressing Enter inserts a
	/// `\n` rather than firing [`Self::on_submit`]. Ignored when
	/// [`Self::secure`] is set — passwords are always single-line.
	pub( crate ) multiline:   bool,
	/// Visible row count when `multiline` is `true`. Drives
	/// `preferred_size`'s height calculation so a multiline field
	/// claims a sensible vertical slot in the parent layout. Ignored
	/// when `multiline` is `false`.
	pub( crate ) rows:        u32,
	/// Byte offset of the text cursor within `value` (used by insert_str/backspace).
	pub( crate ) cursor_pos:  usize,
	/// Optional stable identifier for focus management.
	pub( crate ) id:          Option<WidgetId>,
	/// Override the pointer cursor shape on hover. `None` falls back
	/// to the I-beam default that matches every desktop convention.
	pub( crate ) cursor:      Option<crate::types::CursorShape>,
	/// Horizontal alignment of the displayed text inside the inner
	/// content rect. Only takes effect on the single-line path when
	/// the value fits inside the inner width — once the value
	/// overflows, the internal `single_line_scroll_x` helper takes
	/// over and the alignment offset collapses to `0` so scrolling
	/// reads naturally. Default `TextAlign::Left`.
	pub( crate ) align:       super::text::TextAlign,
	/// Skip the field's background fill and border stroke. Useful
	/// when the [`TextEdit`] is dropped inside another container
	/// that paints its own surface (e.g. the digit cells inside
	/// [`crate::widget::time_picker::TimePicker`]) and a second pill
	/// would only add visual noise.
	pub( crate ) borderless:  bool,
	/// Override the preferred width reported to the parent layout.
	/// Without this the single-line `TextEdit` claims `max_width` and
	/// fills whatever rect the parent allocates — which is the right
	/// default for forms but wrong when the field needs to be sized
	/// to fit a fixed number of glyphs (date / time pickers, inline
	/// numeric inputs).
	pub( crate ) fixed_width: Option<Length>,
	/// Horizontal inner padding of the single-line field in physical pixels; `None` scales the theme padding with the field height.
	pub( crate ) pad_h:       Option<f32>,
	/// Font size in pixels for the single-line draw path. `0.0` (the
	/// Label font size. `None` follows the process [`crate::WidgetScaling`]
	/// mode at the theme default (via [`crate::Canvas::font_px`]); a
	/// [`Length`] pins it and is resolved in font space
	/// ([`crate::Canvas::viewport_logical`]), exactly like the button label.
	/// Resolved via [`Self::effective_font_size`] by every consumer, all of
	/// which carry a canvas. Multiline mode ignores this and always uses the
	/// theme constant.
	pub( crate ) font_size:   Option<Length>,
	/// Optional single-line field height. `None` uses the theme default
	/// (`theme::HEIGHT`); a [`Length`] scales the box with the surface, so
	/// a field can match a fluid button or grow with a fluid form. Resolved
	/// in physical layout space, like all geometry. Ignored in multiline
	/// mode, where the height follows [`Self::rows`].
	pub( crate ) height:      Option<Length>,
	/// When `true`, focusing the field selects the whole value so the
	/// next keystroke replaces it. Standard behaviour for numeric
	/// pickers and short-form fields where the user usually wants to
	/// retype rather than edit. Default `false` — long-form fields
	/// keep the cursor at the end on focus.
	pub( crate ) select_on_focus: bool,
	/// When `true`, editing keeps the cursor at the end of the displayed
	/// value, whatever the app rewrites it to in `on_change`. Default
	/// `false` — the cursor stays where the edit left it.
	pub( crate ) pin_cursor_to_end: bool,
	/// Self-managed "show / hide password" eye affordance — when
	/// `Some( ( visible, on_toggle ) )` the field renders an
	/// `actions/visible` ↔ `actions/invisible` icon at its right
	/// edge, taps on that icon dispatch `on_toggle` instead of
	/// placing the cursor, and the bullet substitution flips with
	/// `visible` (overriding `secure`). Set on a field that already
	/// has `secure( true )` and the explicit flag becomes redundant
	/// — the toggle controls the visibility from then on.
	pub( crate ) password_toggle: Option<( bool, Msg )>,
	/// When `true`, the field renders its box and value but takes no keyboard
	/// focus and accepts no input — a read-only display styled as a text field.
	pub( crate ) read_only:   bool,
	/// Runs of a multiline value drawn in colours of their own.
	pub( crate ) highlights:  Option<Arc<Vec<Highlight>>>,
	/// What the Tab key types while the field has the focus, instead of moving the focus on.
	pub( crate ) tab_text:    Option<String>,
	/// Whether a multiline field numbers its lines in a column at its left.
	pub( crate ) line_numbers: bool,
	/// The face of a multiline field's text; `None` is the canvas's.
	pub( crate ) font:        Option<Arc<crate::font_face::FontFace>>,
}

impl<Msg: Clone> TextEdit<Msg>
{
	/// Create a text field with the given placeholder and initial value.
	///
	/// The cursor is placed at the end of the initial value.
	pub fn new( placeholder: String, value: String ) -> Self
	{
		let cursor_pos = value.len();
		Self
		{
			placeholder,
			value,
			on_change:       None,
			on_cursor:       None,
			on_submit:       None,
			on_focus:        None,
			color:           None,
			secure:          false,
			purpose:         crate::osk::Purpose::Normal,
			multiline:       false,
			rows:            theme::ROWS_DEFAULT,
			cursor_pos,
			id:              None,
			cursor:          None,
			align:           super::text::TextAlign::Left,
			borderless:      false,
			fixed_width:     None,
			pad_h:           None,
			font_size:       None,
			height:          None,
			select_on_focus: false,
			pin_cursor_to_end: false,
			password_toggle: None,
			read_only:       false,
			highlights:      None,
			tab_text:        None,
			line_numbers:    false,
			font:            None,
		}
	}

	/// Add a "show / hide password" eye toggle pinned to the right
	/// edge of the field. `visible` controls whether the value
	/// renders as bullets (`false`) or plain text (`true`); a tap on
	/// the icon emits `on_toggle` so the caller can flip its own
	/// `bool` state and re-render. Works with or without an explicit
	/// [`Self::secure`] — when this is set, the toggle's `visible`
	/// drives the bullet substitution and the `secure` field is
	/// ignored.
	pub fn password_toggle( mut self, visible: bool, on_toggle: Msg ) -> Self
	{
		self.password_toggle = Some( ( visible, on_toggle ) );
		self
	}

	/// Effective secure flag honoured by drawing / measurement /
	/// hit-testing — [`Self::password_toggle`] takes precedence over
	/// the manual [`Self::secure`] when both are set.
	pub fn effective_secure( &self ) -> bool
	{
		match &self.password_toggle
		{
			Some( ( visible, _ ) ) => !visible,
			None                   => self.secure,
		}
	}

	/// Set the label font size. Accepts logical `f32` pixels or any
	/// [`Length`] (e.g. `Length::vmin( 4.5 ).clamp( 16.0, 32.0 )` to scale
	/// with the surface), resolved in font space exactly like the button
	/// label. Without any font-size call the size follows the process
	/// [`crate::WidgetScaling`] mode at the theme default. Ignored in
	/// multiline mode.
	pub fn font_size( mut self, size: impl Into<Length> ) -> Self
	{
		self.font_size = Some( size.into() );
		self
	}

	/// Set a font size that follows the process [`crate::WidgetScaling`]
	/// mode at the given design px — the fluid counterpart of
	/// [`Self::font_size`]. Shorthand for `.font_size( Length::fluid( n ) )`.
	pub fn font_size_fluid( mut self, design_px: f32 ) -> Self
	{
		self.font_size = Some( Length::fluid( design_px.max( 1.0 ) ) );
		self
	}

	/// Resolve the single-line font size against the widget-scaling mode.
	pub( crate ) fn effective_font_size( &self, canvas: &Canvas ) -> f32
	{
		resolve_font_size( canvas, self.font_size )
	}

	/// Set the single-line field height. Accepts logical `f32` pixels or any
	/// [`Length`] (e.g. `Length::vmin( 9.0 ).clamp( 44.0, 72.0 )` to scale
	/// the box with the surface, or to match a fluid button). Ignored in
	/// multiline mode, where the height follows [`Self::rows`].
	pub fn height( mut self, h: impl Into<Length> ) -> Self
	{
		self.height = Some( h.into() );
		self
	}

	/// Select the whole value when the field receives focus, so the
	/// next keystroke replaces it. Default `false`.
	pub fn select_on_focus( mut self, on: bool ) -> Self
	{
		self.select_on_focus = on;
		self
	}

	/// Keep the cursor at the end of the displayed value while editing,
	/// for fields whose `on_change` normalises what was typed ("2" → "02").
	/// Default `false`.
	pub fn pin_cursor_to_end( mut self, on: bool ) -> Self
	{
		self.pin_cursor_to_end = on;
		self
	}

	/// Set the horizontal alignment of the displayed text. Default
	/// [`TextAlign::Left`](super::text::TextAlign::Left).
	pub fn align( mut self, a: super::text::TextAlign ) -> Self
	{
		self.align = a;
		self
	}

	/// Skip the field's background fill and border stroke — useful
	/// when the field is nested inside a container that already
	/// paints its own surface.
	pub fn borderless( mut self, on: bool ) -> Self
	{
		self.borderless = on;
		self
	}

	/// Set the horizontal inner padding of a single-line field, in
	/// physical pixels, for a host that lays out its own (`0` lets the
	/// text use the whole rect). Without it the padding follows the
	/// field height.
	pub fn padding_h( mut self, px: f32 ) -> Self
	{
		self.pad_h = Some( px.max( 0.0 ) );
		self
	}

	/// Override the preferred width reported to the parent layout.
	/// Accepts logical `f32` pixels or any [`Length`] — pair a
	/// [`Length::fluid`] width with a fluid font size so box and glyphs
	/// scale together. Without this the field claims `max_width`.
	pub fn fixed_width( mut self, w: impl Into<Length> ) -> Self
	{
		self.fixed_width = Some( w.into() );
		self
	}

	/// Override the pointer cursor shape shown on hover. Defaults to
	/// [`CursorShape::Text`](crate::CursorShape::Text) (I-beam).
	pub fn cursor( mut self, shape: crate::types::CursorShape ) -> Self
	{
		self.cursor = Some( shape );
		self
	}

	/// Switch to multiline (text-area) mode. The box is laid out with
	/// [`Self::rows`] visible rows of height, line breaks in the value
	/// are rendered as separate rows, and Enter inserts a `\n` instead
	/// of firing [`Self::on_submit`]. Ignored when [`Self::secure`] is
	/// `true`.
	pub fn multiline( mut self, m: bool ) -> Self
	{
		self.multiline = m;
		self
	}

	/// Configure the number of visible rows in multiline mode. Defaults
	/// to 5; ignored when [`Self::multiline`] is `false`.
	pub fn rows( mut self, n: u32 ) -> Self
	{
		self.rows = n.max( 1 );
		self
	}

	/// Draw runs of a multiline value in colours of their own (see
	/// [`Highlight`]), as an editor highlights code. Shared through an
	/// `Arc`, since a view is rebuilt on every frame and a long text has
	/// many runs. Single-line fields draw in one colour.
	pub fn highlights( mut self, runs: Arc<Vec<Highlight>> ) -> Self
	{
		self.highlights = Some( runs );
		self
	}

	/// Have the Tab key type `text`, such as four spaces, while the field
	/// has the focus, as an editor does, instead of moving the focus to
	/// the next widget.
	pub fn tab_text( mut self, text: impl Into<String> ) -> Self
	{
		self.tab_text = Some( text.into() );
		self
	}

	/// Number the lines of a multiline field in a column at its left, as
	/// wide as the number of the last line needs; a line that wraps is
	/// numbered on its first row.
	pub fn line_numbers( mut self, on: bool ) -> Self
	{
		self.line_numbers = on;
		self
	}

	/// Draw a multiline field's text in `face`, such as [`crate::monospace_font`]
	/// for an editor of code, instead of the theme's; its size is still
	/// [`Self::font_size`]'s.
	pub fn font( mut self, face: Arc<crate::font_face::FontFace> ) -> Self
	{
		self.font = Some( face );
		self
	}

	/// The size and face its text is laid out in.
	pub( crate ) fn text_font( &self ) -> TextFont
	{
		TextFont { size: self.font_size, face: self.font.clone().filter( |_| self.is_multiline() ) }
	}

	/// Set the callback invoked on every keystroke with the updated value.
	pub fn on_change( mut self, f: impl Fn(String) -> Msg + 'static ) -> Self
	{
		self.on_change = Some( Arc::new( f ) );
		self
	}

	/// Set the callback invoked with the cursor's byte offset in the value
	/// once it has been placed and whenever it moves or the value changes
	/// length, as an editor's status bar showing the line needs.
	pub fn on_cursor( mut self, f: impl Fn( usize ) -> Msg + 'static ) -> Self
	{
		self.on_cursor = Some( Arc::new( f ) );
		self
	}

	/// Set the message emitted when Enter is pressed.
	pub fn on_submit( mut self, msg: Msg ) -> Self
	{
		self.on_submit = Some( msg );
		self
	}

	/// Enable or disable password mode.
	///
	/// When `true`, this widget:
	///
	/// 1. Renders the value as bullet characters (`•`) instead of the
	///    raw glyphs.
	/// 2. Forces single-line mode (multiline + secure is mutually
	///    exclusive — passwords don't have line breaks).
	/// 3. Wipes the underlying byte buffer with zero before the
	///    `String` allocation is returned to the allocator. The wipe
	///    runs in `Drop` for both the `TextEdit` itself and for the
	///    per-frame [`crate::widget::WidgetHandlers::TextEdit`] snapshot
	///    the runtime keeps for input dispatch — so the in-tree copies
	///    that ltk owns never linger as plain text in freed memory.
	///
	/// # Threat model — what `secure` covers
	///
	/// Inside the widget tree the runtime keeps two copies of the
	/// value for the lifetime of one frame: the `TextEdit` itself and
	/// the `WidgetHandlers` snapshot. Both run the `secure_zero` wipe
	/// on `Drop`, so when the next frame replaces them (the typical
	/// case — `view()` rebuilds every frame) the freed allocations are
	/// overwritten before being released back to the allocator. The
	/// wipe uses volatile writes + a `compiler_fence` so the optimiser
	/// cannot elide it as dead code (the implementation is in the
	/// crate-private `secure_mem` module).
	///
	/// # What `secure` does **not** cover
	///
	/// * **The application's own state.** The `String` you pass in
	///   through `text_edit( placeholder, &self.password )` lives on
	///   **your** struct, not on the widget. Wiping it is
	///   your job — typically a `Drop` impl on the credential
	///   container, or an explicit
	///   `secure_mem::secure_zero( password.as_bytes_mut() )` after
	///   the auth handshake completes.
	/// * **Callback-allocated copies.** Every keystroke passes through
	///   `on_change( |s: String| ... )`, which receives a fresh
	///   `String` clone. If your closure stores or forwards that
	///   `String` (e.g. clone it into a worker thread for PAM), each
	///   stored copy is the consumer's responsibility to wipe. ltk
	///   only owns the buffers it allocated itself.
	/// * **OS-level disclosure surfaces.** Swap-out, hibernation
	///   images, and core dumps are outside any user-space wipe's
	///   reach. For threat models that require resistance to these,
	///   compile against an `mlock`-aware allocator, disable swap on
	///   the credential mount, and restrict core-dump capability with
	///   `prctl( PR_SET_DUMPABLE, 0 )` on the process.
	/// * **Compositor-side records.** Wayland text-input protocols can
	///   surface preedit / commit strings to the compositor's IME stack.
	///   `secure` does *not* suppress text-input-v3 — the field still
	///   registers so the on-screen keyboard activates on it — but it is
	///   flagged `Password` with `SensitiveData | HiddenText`, asking the
	///   IME / OSK to skip prediction, autocorrect and storing the value.
	///   The value still reaches the (trusted) compositor/IME; for a
	///   stricter threat model, suppress text-input on secure fields
	///   instead (at the cost of losing the OSK there).
	///
	/// See the in-repo `SECURITY.md` for the full threat-model write-up
	/// (the *Hardening features* section enumerates each guarantee and
	/// its boundary).
	pub fn secure( mut self, s: bool ) -> Self
	{
		self.secure = s;
		self
	}

	/// What the field is for. A `Terminal` field gets the on-screen keyboard's row of Escape, Tab, Ctrl, Alt and arrows, and tells the compositor's input method the same.
	pub fn purpose( mut self, purpose: crate::osk::Purpose ) -> Self
	{
		self.purpose = purpose;
		self
	}

	/// Emit `msg` when the field takes the keyboard focus.
	pub fn on_focus( mut self, msg: Msg ) -> Self
	{
		self.on_focus = Some( msg );
		self
	}

	/// Colour of the value; the placeholder keeps the theme's.
	pub fn color( mut self, c: Color ) -> Self
	{
		self.color = Some( c );
		self
	}

	/// Render the field read-only: shows the value styled as a field but
	/// takes no focus and accepts no input.
	pub fn read_only( mut self, on: bool ) -> Self
	{
		self.read_only = on;
		self
	}

	/// Assign a stable identifier for focus management.
	pub fn id( mut self, id: WidgetId ) -> Self
	{
		self.id = Some( id );
		self
	}

	/// Return the preferred `(width, height)` given available `max_width`.
	///
	/// Single-line: theme-defined `HEIGHT`.
	/// Multiline: enough room for [`Self::rows`] lines plus padding.
	pub fn preferred_size( &self, max_width: f32, canvas: &Canvas ) -> (f32, f32)
	{
		if self.multiline && !self.effective_secure()
		{
			let m = metrics::MultilineMetrics::resolve( canvas, self.font_size );
			( max_width, m.height_for_rows( self.rows ) )
		} else {
			let w = self.fixed_width
				.map( |l| canvas.resolve_geom( l ).min( max_width ) )
				.unwrap_or( max_width );
			let h = self.height
				.map( |l| canvas.resolve_geom( l ) )
				.unwrap_or_else( || canvas.geom_px( theme::HEIGHT ) );
			( w, h )
		}
	}

	/// `true` when the widget is laid out as a multi-row text area —
	/// i.e. [`Self::multiline`] was set and [`Self::effective_secure`]
	/// is `false`. A `password_toggle` field collapses to single-line
	/// like an explicit `secure( true )` does.
	pub fn is_multiline( &self ) -> bool
	{
		self.multiline && !self.effective_secure()
	}

	/// Translate a pointer position inside `rect` to the byte offset
	/// in the field's `value` that the cursor should land on. Thin
	/// wrapper around `byte_offset_at` using this widget's value /
	/// flags.
	pub fn byte_offset_at_self(
		&self,
		canvas:     &Canvas,
		rect:       Rect,
		pos:        crate::types::Point,
		cursor_pos: usize,
		scroll:     TextScroll,
	) -> usize
	{
		let font = self.text_font();
		let rect = gutter::text_rect( canvas, rect, &self.value, &font, self.line_numbers && self.is_multiline() );
		byte_offset_at(
			canvas, rect, pos, &self.value, self.is_multiline(), self.effective_secure(), cursor_pos, scroll, self.align, &font, self.pad_h,
		)
	}

	/// Border stroke is centered on `rect`, so half the stroke width plus ~1 px
	/// of antialiasing bleed sits outside. The widest stroke is the focused
	/// border, so use that as the envelope.
	pub fn paint_bounds( &self, rect: Rect ) -> Rect
	{
		rect.expand( theme::FOCUS_BORDER_W * 0.5 + 1.0 )
	}

	/// Return the display string — bullet characters in secure mode, plain value otherwise.
	pub fn display_text( &self ) -> String
	{
		if self.effective_secure()
		{
			"\u{2022}".repeat( self.value.chars().count() )
		} else {
			self.value.clone()
		}
	}

	/// Wrap this widget in an [`Element`].
	pub fn into_element( self ) -> Element<Msg>
	{
		Element::TextEdit( self )
	}

	/// Insert a string at the current cursor position, advance the cursor, and
	/// return the `on_change` message if one is set.
	pub fn insert_str( &mut self, s: &str ) -> Option<Msg>
	{
		self.value.insert_str( self.cursor_pos.min( self.value.len() ), s );
		self.cursor_pos = (self.cursor_pos + s.len()).min( self.value.len() );
		self.on_change.as_ref().map( |f| f( self.value.clone() ) )
	}

	/// Delete the character before the cursor and return the `on_change` message
	/// if one is set. Does nothing if the cursor is already at position 0.
	pub fn backspace( &mut self ) -> Option<Msg>
	{
		if self.cursor_pos == 0 { return None; }
		let chars: Vec<char> = self.value.chars().collect();
		let char_pos = self.value[..self.cursor_pos].chars().count();
		if char_pos == 0 { return None; }
		let mut chars = chars;
		let removed = chars.remove( char_pos - 1 );
		self.cursor_pos -= removed.len_utf8();
		self.value = chars.iter().collect();
		self.on_change.as_ref().map( |f| f( self.value.clone() ) )
	}

	pub( crate ) fn map_msg<U>( self, f: &super::MapFn<Msg, U> ) -> TextEdit<U>
	where
		U: Clone + 'static,
		Msg: 'static,
	{
		// Wrap on_change the same way the slider does. on_submit is a
		// plain `Option<Msg>`, so it goes through the user mapper once.
		let on_change = self.on_change.clone().map( |old| -> Arc<dyn Fn( String ) -> U>
		{
			let mapper = Arc::clone( f );
			Arc::new( move |s| ( *mapper )( ( *old )( s ) ) )
		} );
		let on_cursor = self.on_cursor.clone().map( |old| -> Arc<dyn Fn( usize ) -> U>
		{
			let mapper = Arc::clone( f );
			Arc::new( move |at| ( *mapper )( ( *old )( at ) ) )
		} );
		TextEdit
		{
			placeholder:     self.placeholder.clone(),
			value:           self.value.clone(),
			on_change,
			on_cursor,
			on_submit:       self.on_submit.clone().map( |m| ( *f )( m ) ),
			on_focus:        self.on_focus.clone().map( |m| ( *f )( m ) ),
			color:           self.color,
			secure:          self.secure,
			purpose:         self.purpose,
			multiline:       self.multiline,
			rows:            self.rows,
			cursor_pos:      self.cursor_pos,
			id:              self.id,
			cursor:          self.cursor,
			align:           self.align,
			borderless:      self.borderless,
			fixed_width:     self.fixed_width,
			pad_h:           self.pad_h,
			font_size:       self.font_size,
			height:          self.height,
			select_on_focus: self.select_on_focus,
			pin_cursor_to_end: self.pin_cursor_to_end,
			password_toggle: self.password_toggle.clone().map( |( v, m )| ( v, ( *f )( m ) ) ),
			read_only:       self.read_only,
			highlights:      self.highlights.clone(),
			tab_text:        self.tab_text.clone(),
			line_numbers:    self.line_numbers,
			font:            self.font.clone(),
		}
	}
}

impl<Msg: Clone> Drop for TextEdit<Msg>
{
	/// When `secure( true )` is set, scrub the value bytes before the
	/// underlying `String` allocation is returned to the allocator. The
	/// non-secure path is a no-op so the cost is paid only by widgets
	/// that opted into credential handling.
	fn drop( &mut self )
	{
		if self.secure || self.password_toggle.is_some()
		{
			// SAFETY: as_mut_vec exposes the underlying byte buffer of the
			// String. We only overwrite each byte with zero, which is valid
			// UTF-8 (a sequence of NUL codepoints), so the String invariant
			// is preserved through to the Vec drop that runs immediately
			// after this fn returns.
			let bytes = unsafe { self.value.as_mut_vec() };
			secure_zero( bytes );
		}
	}
}