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

use crate::app::App;
use crate::event_loop::{ AppData, SurfaceFocus };
use crate::tree::{ find_handlers, find_widget };
use crate::Keysym;
use crate::platform::Platform;

use super::super::event::KeyInput;

impl<A: App, P: Platform> AppData<A, P>
{
	/// Whether the focused widget is an editable text input; any other
	/// focused widget lets text keys bubble to the app.
	pub( crate ) fn focused_text_input( &self, focus: SurfaceFocus ) -> bool
	{
		let ss = self.surface( focus );
		ss.focused_idx
			.and_then( |idx| find_handlers( &ss.frame.widget_rects, idx ) )
			.is_some_and( |h| h.is_text_input() )
	}

	pub( super ) fn handle_key_backspace( &mut self, focus: SurfaceFocus, event: &KeyInput )
	{
		if self.focused_text_input( focus ) { self.handle_backspace( focus ); }
		else if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
		{
			self.pending_msgs.push( msg );
		}
	}

	pub( super ) fn handle_key_delete( &mut self, focus: SurfaceFocus, event: &KeyInput )
	{
		// Supr / Forward-Delete: same shape as Backspace but
		// removes the char *after* the cursor.
		if self.focused_text_input( focus ) { self.handle_delete_forward( focus ); }
		else if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
		{
			self.pending_msgs.push( msg );
		}
	}

	pub( super ) fn handle_key_return( &mut self, focus: SurfaceFocus, event: &KeyInput )
	{
		let focused = self.surface( focus ).focused_idx;
		// Enter targets, in order: (a) the focused widget's submit
		// message (TextEdit), (b) its press message (Button etc),
		// (c) the press message of the keyboard-hovered list item
		// in the topmost scroll. The hovered fallback lets users
		// confirm a combo / list choice with the keyboard after
		// arrow-navigating to it without ever taking widget focus.
		//
		// Multiline text-input override: when the focused widget
		// is a `text_edit().multiline( true )`, Enter inserts a
		// literal `\n` into the buffer instead of submitting, so
		// the user can compose paragraphs.
		let is_multi = focused.and_then( |idx|
			find_handlers( &self.surface( focus ).frame.widget_rects, idx )
				.map( |h| h.is_multiline_text_input() ) ).unwrap_or( false );
		if is_multi
		{
			self.handle_text_insert( focus, "\n" );
		} else {
			let ss      = self.surface( focus );
			let hovered = ss.hovered_idx.filter( |&idx| find_widget( &ss.frame.widget_rects, idx ).is_some_and( |w| w.focus_on_press ) );
			let target  = focused.or( hovered );
			let mut handled = false;
			if let Some( idx ) = target
			{
				let msg = find_handlers( &self.surface( focus ).frame.widget_rects, idx )
					.and_then( |h| h.submit_msg().or_else( || h.press_msg() ) );
				if let Some( m ) = msg
				{
					self.pending_msgs.push( m );
					handled = true;
				}
			}
			if !handled
			{
				if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
				{
					self.pending_msgs.push( msg );
				}
			}
		}
	}

	pub( super ) fn handle_key_arrow_vertical( &mut self, focus: SurfaceFocus, event: &KeyInput )
	{
		let is_text  = self.focused_text_input( focus );
		let reverse  = event.keysym == Keysym::Up;
		let extend   = self.shift_pressed;
		let intercepted = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed );
		if let Some( msg ) = intercepted
		{
			self.pending_msgs.push( msg );
		}
		else
		{
			let consumed = if is_text
			{
				if reverse { self.handle_cursor_up( focus, extend ) }
				else       { self.handle_cursor_down( focus, extend ) }
			} else { false };
			if !consumed && !self.move_keyboard_hover( focus, reverse )
			{
				// already None, no extra fire
			}
		}
	}

	pub( super ) fn handle_key_arrow_horizontal( &mut self, focus: SurfaceFocus, event: &KeyInput )
	{
		let is_text = self.focused_text_input( focus );
		let extend  = self.shift_pressed;
		let intercepted = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed );
		if let Some( msg ) = intercepted
		{
			self.pending_msgs.push( msg );
		}
		else if is_text
		{
			if event.keysym == Keysym::Left
			{
				self.handle_cursor_left( focus, extend );
			} else {
				self.handle_cursor_right( focus, extend );
			}
		}
	}

	pub( super ) fn handle_key_home( &mut self, focus: SurfaceFocus )
	{
		if self.focused_text_input( focus ) { self.handle_cursor_home( focus, self.shift_pressed ); }
	}

	pub( super ) fn handle_key_end( &mut self, focus: SurfaceFocus )
	{
		if self.focused_text_input( focus ) { self.handle_cursor_end( focus, self.shift_pressed ); }
	}

	/// A page up or down in a multiline field; with Ctrl, or for any other widget, the app's.
	pub( super ) fn handle_key_page( &mut self, focus: SurfaceFocus, event: &KeyInput )
	{
		let up = event.keysym == Keysym::Page_Up;
		if !self.ctrl_pressed && self.focused_text_input( focus ) && self.handle_cursor_page( focus, up, self.shift_pressed ) { return; }
		if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
		{
			self.pending_msgs.push( msg );
		}
	}

	pub( super ) fn handle_key_space( &mut self, focus: SurfaceFocus, event: &KeyInput )
	{
		let ss    = self.surface( focus );
		let press = ss.focused_idx
			.and_then( |idx| find_handlers( &ss.frame.widget_rects, idx ) )
			.and_then( |h| h.press_msg() );
		if let Some( msg ) = press
		{
			self.pending_msgs.push( msg );
		} else if self.focused_text_input( focus ) {
			self.handle_text_insert( focus, " " );
		} else if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
		{
			self.pending_msgs.push( msg );
		}
	}

	pub( super ) fn handle_key_default( &mut self, focus: SurfaceFocus, event: &KeyInput )
	{
		if self.ctrl_pressed
		{
			if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, true, self.shift_pressed )
			{
				self.pending_msgs.push( msg );
			}
		} else if self.focused_text_input( focus )
		{
			let typed = event.text.clone().filter( |txt| !txt.is_empty() && txt.chars().all( |c| !c.is_control() ) );
			match typed
			{
				Some( txt ) => self.handle_text_insert( focus, &txt ),
				None        =>
				{
					if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, false, self.shift_pressed )
					{
						self.pending_msgs.push( msg );
					}
				}
			}
		} else if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, false, self.shift_pressed )
		{
			self.pending_msgs.push( msg );
		}
	}
}