ltk/widget/
handlers.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
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

use std::sync::Arc;
use crate::types::{ Point, Rect };
use super::{ slider, text };

/// Per-leaf interaction snapshot captured during layout. One variant per
/// interactive widget kind; the layout pass clones the relevant callbacks /
/// values from the [`Element`](super::Element) tree into here so input handlers can dispatch
/// in O(1) without re-walking the tree.
///
/// `None` is used for focusable widgets that emit no message (e.g. a disabled
/// button or a focusable container) — the entry still appears in
/// `widget_rects` for hit testing and focus traversal.
pub enum WidgetHandlers<Msg: Clone>
{
	None,
	Button
	{
		on_press:      Option<Msg>,
		on_long_press: Option<Msg>,
		on_drag_start: Option<Msg>,
		/// Keyboard `Escape`-key message — the runtime scans every laid-out
		/// `Button` snapshot in reverse and fires the first non-`None`
		/// `on_escape` it finds before the default ESC fallthrough chain.
		/// Currently sourced from
		/// [`crate::widget::pressable::Pressable::on_escape`]; native
		/// [`crate::widget::button::Button`] always sets this to `None`.
		on_escape:     Option<Msg>,
		repeating:     bool,
	},
	Toggle   { on_toggle: Option<Msg>, value: bool },
	Checkbox { on_toggle: Option<Msg>, value: bool },
	Radio    { on_select: Option<Msg>, selected: bool },
	ListItem { on_press:  Option<Msg> },
	WindowButton { on_press: Option<Msg> },
	TextEdit
	{
		value:     String,
		on_change: Option<Arc<dyn Fn( String ) -> Msg>>,
		on_submit: Option<Msg>,
		/// `true` when the source `TextEdit` was built with
		/// `.secure( true )`. Propagates the wipe-on-drop behaviour to
		/// every per-frame handler snapshot so credential text does not
		/// linger across multiple cloned `WidgetHandlers` allocations.
		secure:    bool,
		/// `true` when the source `TextEdit` was built with
		/// `.multiline( true )`. The keyboard dispatch reads this so
		/// pressing Enter inserts a `\n` instead of firing
		/// [`Self::submit_msg`]. Mutually exclusive with `secure`.
		multiline: bool,
		/// Horizontal alignment snapshot — needed by the runtime's
		/// hit-testing path so a click on a centred / right-aligned
		/// field lands on the correct glyph.
		align:     text::TextAlign,
		/// Font size snapshot — needed by the hit-testing path so
		/// the runtime measures glyphs at the same size the renderer
		/// drew them. Always the default `theme::FONT_SIZE` for
		/// fields that do not call `.font_size( … )`.
		font_size: f32,
		/// `true` when the source field opted into select-all-on-
		/// focus. The runtime reads this in `set_focus` to decide
		/// whether the new selection should anchor at `0` (replace
		/// on next keystroke) or at the cursor (insert).
		select_on_focus: bool,
		/// Snapshot of the
		/// [`crate::widget::text_edit::TextEdit::password_toggle`]
		/// callback. When `Some`, pointer / touch dispatch checks
		/// the eye-icon hit zone (via
		/// [`crate::widget::text_edit::password_toggle_hit_zone`])
		/// before falling through to cursor placement and fires
		/// this message instead.
		password_toggle_msg: Option<Msg>,
	},
	Slider
	{
		on_change: Option<Arc<dyn Fn( f32 ) -> Msg>>,
		axis:      slider::SliderAxis,
		value:     f32,
	},
}

impl<Msg: Clone> Drop for WidgetHandlers<Msg>
{
	/// Mirror the wipe-on-drop behaviour of [`super::text_edit::TextEdit`] for the
	/// per-frame handler snapshots the runtime keeps. When the snapshot
	/// was built from a secure text edit we scrub the value bytes here so
	/// the heap allocation that backs the cloned `String` is overwritten
	/// before it is returned to the allocator. Non-secure variants run an
	/// inert path; the field-level drops that fire after this fn handle
	/// the rest of the data.
	fn drop( &mut self )
	{
		if let WidgetHandlers::TextEdit { value, secure: true, .. } = self
		{
			// SAFETY: identical reasoning to `TextEdit::drop` — we only
			// write zeros into the underlying byte buffer, leaving valid
			// UTF-8 (NUL bytes) in place until the auto-drop frees it.
			let bytes = unsafe { value.as_mut_vec() };
			crate::secure_mem::secure_zero( bytes );
		}
	}
}

impl<Msg: Clone> Clone for WidgetHandlers<Msg>
{
	fn clone( &self ) -> Self
	{
		match self
		{
			WidgetHandlers::None              => WidgetHandlers::None,
			WidgetHandlers::Button   { on_press, on_long_press, on_drag_start, on_escape, repeating } => WidgetHandlers::Button
			{
				on_press:      on_press.clone(),
				on_long_press: on_long_press.clone(),
				on_drag_start: on_drag_start.clone(),
				on_escape:     on_escape.clone(),
				repeating:     *repeating,
			},
			WidgetHandlers::Toggle   { on_toggle, value } => WidgetHandlers::Toggle   { on_toggle: on_toggle.clone(), value: *value },
			WidgetHandlers::Checkbox { on_toggle, value } => WidgetHandlers::Checkbox { on_toggle: on_toggle.clone(), value: *value },
			WidgetHandlers::Radio    { on_select, selected } => WidgetHandlers::Radio { on_select: on_select.clone(), selected: *selected },
			WidgetHandlers::ListItem { on_press  } => WidgetHandlers::ListItem { on_press:  on_press.clone()  },
			WidgetHandlers::WindowButton { on_press } => WidgetHandlers::WindowButton { on_press: on_press.clone() },
			WidgetHandlers::TextEdit { value, on_change, on_submit, secure, multiline, align, font_size, select_on_focus, password_toggle_msg } =>
			{
				WidgetHandlers::TextEdit
				{
					value:               value.clone(),
					on_change:           on_change.clone(),
					on_submit:           on_submit.clone(),
					secure:              *secure,
					multiline:           *multiline,
					align:               *align,
					font_size:           *font_size,
					select_on_focus:     *select_on_focus,
					password_toggle_msg: password_toggle_msg.clone(),
				}
			}
			WidgetHandlers::Slider { on_change, axis, value } =>
			{
				WidgetHandlers::Slider { on_change: on_change.clone(), axis: *axis, value: *value }
			}
		}
	}
}

impl<Msg: Clone> WidgetHandlers<Msg>
{
	pub fn is_text_input( &self ) -> bool
	{
		matches!( self, WidgetHandlers::TextEdit { .. } )
	}

	pub fn is_disabled( &self ) -> bool
	{
		match self
		{
			WidgetHandlers::Button   { on_press, on_long_press, on_drag_start, on_escape, .. } =>
				on_press.is_none() && on_long_press.is_none() && on_drag_start.is_none() && on_escape.is_none(),
			WidgetHandlers::Toggle   { on_toggle, .. } => on_toggle.is_none(),
			WidgetHandlers::Checkbox { on_toggle, .. } => on_toggle.is_none(),
			WidgetHandlers::Radio    { on_select, .. } => on_select.is_none(),
			WidgetHandlers::ListItem { on_press } => on_press.is_none(),
			WidgetHandlers::WindowButton { on_press } => on_press.is_none(),
			WidgetHandlers::Slider   { on_change, .. } => on_change.is_none(),
			WidgetHandlers::TextEdit { on_change, on_submit, .. } => on_change.is_none() && on_submit.is_none(),
			WidgetHandlers::None => true,
		}
	}

	/// `true` when this is a [`WidgetHandlers::TextEdit`] whose source
	/// widget was built with `.multiline( true )`. The keyboard
	/// dispatch reads this so pressing Enter inserts a `\n` instead of
	/// submitting.
	pub fn is_multiline_text_input( &self ) -> bool
	{
		matches!( self, WidgetHandlers::TextEdit { multiline: true, .. } )
	}

	pub fn is_slider( &self ) -> bool
	{
		matches!( self, WidgetHandlers::Slider { .. } )
	}

	/// `true` when this widget is a row inside a scrollable list that
	/// keyboard arrow navigation should treat as a stepping point.
	/// Currently restricted to [`ListItem`](crate::ListItem); buttons,
	/// toggles, sliders, etc. are not stepped over by Arrow Up/Down so
	/// they do not interfere with the row-by-row navigation pattern in
	/// combo popups, settings menus and similar lists.
	pub fn is_navigable_list_item( &self ) -> bool
	{
		matches!( self, WidgetHandlers::ListItem { .. } )
	}

	/// Convenience: extract the press / activation message for the variants
	/// that have one (Button, Toggle, Checkbox, Radio, ListItem). Returns
	/// `None` for sliders / text edits / `None` / disabled widgets.
	pub fn press_msg( &self ) -> Option<Msg>
	{
		match self
		{
			WidgetHandlers::Button   { on_press, .. } => on_press.clone(),
			WidgetHandlers::Toggle   { on_toggle, .. } => on_toggle.clone(),
			WidgetHandlers::Checkbox { on_toggle, .. } => on_toggle.clone(),
			WidgetHandlers::Radio    { on_select, .. } => on_select.clone(),
			WidgetHandlers::ListItem { on_press  } => on_press.clone(),
			WidgetHandlers::WindowButton { on_press } => on_press.clone(),
			_                                       => None,
		}
	}

	/// Submit message (Enter on a focused TextEdit). `None` for every other
	/// variant.
	pub fn submit_msg( &self ) -> Option<Msg>
	{
		match self
		{
			WidgetHandlers::TextEdit { on_submit, .. } => on_submit.clone(),
			_                                          => None,
		}
	}

	/// Build the `on_change` message for a TextEdit given the new value.
	pub fn text_change_msg( &self, new_value: &str ) -> Option<Msg>
	{
		match self
		{
			WidgetHandlers::TextEdit { on_change: Some( f ), .. } => Some( f( new_value.to_string() ) ),
			_ => None,
		}
	}

	/// Build the `on_change` message for a Slider given a value in `[0,1]`.
	pub fn slider_change_msg( &self, value: f32 ) -> Option<Msg>
	{
		match self
		{
			WidgetHandlers::Slider { on_change: Some( f ), .. } => Some( f( value ) ),
			_ => None,
		}
	}

	/// Compute the `[0.0, 1.0]` value for the slider this handler belongs to,
	/// given a pointer position inside its layout rect. Dispatches on the
	/// stored [`slider::SliderAxis`] so the same call site in `input.rs` drives
	/// both horizontal [`Slider`](slider::Slider) and vertical
	/// [`VSlider`](super::vslider::VSlider).
	///
	/// Returns `0.0` for non-slider variants — callers combine this with
	/// [`Self::slider_change_msg`], which also gates on the variant, so the
	/// zero is never consumed in practice.
	pub fn slider_value_from_pos( &self, rect: Rect, pos: Point ) -> f32
	{
		match self
		{
			WidgetHandlers::Slider { axis, .. } =>
			{
				slider::value_from_pos_in_rect( rect, pos, *axis )
			}
			_ => 0.0,
		}
	}

	/// `true` when this is a [`WidgetHandlers::Button`] whose source
	/// widget opted into press-and-hold repeat. The runtime reads
	/// this on press to decide whether to fire `press_msg`
	/// immediately + arm a calloop repeat timer, and on release to
	/// suppress the regular tap-on-release fire.
	pub fn is_repeating( &self ) -> bool
	{
		matches!( self, WidgetHandlers::Button { repeating: true, .. } )
	}

	/// Long-press / right-click message for this widget, or `None` if
	/// none configured. Currently only [`WidgetHandlers::Button`]
	/// carries one. Firing this does not by itself put the press into
	/// drag mode — see [`Self::drag_start_msg`] for that.
	pub fn long_press_msg( &self ) -> Option<Msg>
	{
		match self
		{
			WidgetHandlers::Button { on_long_press, .. } => on_long_press.clone(),
			_                                            => None,
		}
	}

	/// Drag-arm message for this widget, or `None` if none configured.
	/// Fired by the touch hold-timer alongside `long_press_msg`, and
	/// by mouse left-button motion past the drag-promotion threshold
	/// (without firing the menu). Promotes the gesture into drag mode.
	pub fn drag_start_msg( &self ) -> Option<Msg>
	{
		match self
		{
			WidgetHandlers::Button { on_drag_start, .. } => on_drag_start.clone(),
			_                                            => None,
		}
	}

	/// Keyboard `Escape` message for this widget, or `None` if none
	/// configured. Used by the keyboard ESC handler to scan
	/// `widget_rects` for a [`crate::widget::dialog::Dialog`] (or other
	/// `Pressable::on_escape`-bearing wrapper) and fire its cancel
	/// message before the default ESC fallthrough chain.
	pub fn escape_msg( &self ) -> Option<Msg>
	{
		match self
		{
			WidgetHandlers::Button { on_escape, .. } => on_escape.clone(),
			_                                        => None,
		}
	}

	/// Current text-edit value (for cursor placement on focus, backspace
	/// rebuild, etc.). `None` for non-text-edit variants.
	pub fn current_value( &self ) -> Option<&str>
	{
		match self
		{
			WidgetHandlers::TextEdit { value, .. } => Some( value.as_str() ),
			_ => None,
		}
	}
}