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
use std::ops::Range;
use std::rc::Rc;
use gpui::{
AnyElement, Bounds, Context, ElementInputHandler, EntityInputHandler, FocusHandle, Focusable,
KeyDownEvent, MouseButton, Pixels, Render, UTF16Selection, canvas,
};
use crate::prelude::*;
/// Visual validation state of a [`TextInput`], reflected in its border/focus
/// ring color.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum InputValidationState {
#[default]
Neutral,
Error,
Success,
Warning,
}
/// A focusable single-line (or multi-line) text field backed by a real
/// `String` buffer. Keyboard input is handled via key events (`key_char` +
/// editing keys), so typed characters genuinely appear and backspace deletes.
/// IME composition (e.g. Vietnamese/CJK input methods) is handled via the
/// [`EntityInputHandler`] impl, so composed text commits correctly.
///
/// This is a stateful view: create with `cx.new(|cx| TextInput::new(cx))` and
/// store the resulting `Entity<TextInput>`.
pub struct TextInput {
content: String,
placeholder: SharedString,
focus_handle: FocusHandle,
multiline: bool,
submit_on_enter: bool,
validation: InputValidationState,
read_only: bool,
/// Byte range of in-progress IME marked (composition) text within
/// `content`, if any. `None` when no composition is active.
marked_range: Option<Range<usize>>,
on_submit: Option<Rc<dyn Fn(&mut Window, &mut Context<Self>) + 'static>>,
}
impl TextInput {
pub fn new(cx: &mut App) -> Self {
Self {
content: String::new(),
placeholder: SharedString::default(),
focus_handle: cx.focus_handle(),
multiline: false,
submit_on_enter: false,
validation: InputValidationState::Neutral,
read_only: false,
marked_range: None,
on_submit: None,
}
}
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.placeholder = placeholder.into();
self
}
pub fn multiline(mut self, multiline: bool) -> Self {
self.multiline = multiline;
self
}
/// When `true`, plain Enter fires [`Self::on_submit`] instead of
/// inserting a newline; Shift/Ctrl/Cmd+Enter still inserts a newline (in
/// `multiline` mode). Defaults to `false`, which preserves the previous
/// behavior of always inserting a newline on Enter in `multiline` mode
/// (e.g. `CodeEditor`'s free-form multiline input).
pub fn submit_on_enter(mut self, submit_on_enter: bool) -> Self {
self.submit_on_enter = submit_on_enter;
self
}
/// Registers a callback fired when the user presses plain Enter while
/// [`Self::submit_on_enter`] is `true`. Has no effect otherwise.
pub fn on_submit(
mut self,
handler: impl Fn(&mut Window, &mut Context<Self>) + 'static,
) -> Self {
self.on_submit = Some(Rc::new(handler));
self
}
/// When `true`, the input no longer accepts keyboard edits (used for
/// read-only code previews). Focus/selection styling is unaffected.
pub fn read_only(mut self, read_only: bool) -> Self {
self.read_only = read_only;
self
}
/// Sets the error validation state (red border/ring) when `invalid` is
/// true, otherwise clears back to [`InputValidationState::Neutral`].
pub fn invalid(mut self, invalid: bool) -> Self {
self.validation = if invalid {
InputValidationState::Error
} else {
InputValidationState::Neutral
};
self
}
/// Sets the success validation state (green border/ring) when `success`
/// is true, otherwise clears back to [`InputValidationState::Neutral`].
pub fn success(mut self, success: bool) -> Self {
self.validation = if success {
InputValidationState::Success
} else {
InputValidationState::Neutral
};
self
}
/// Sets the warning validation state (amber border/ring) when `warning`
/// is true, otherwise clears back to [`InputValidationState::Neutral`].
pub fn warning(mut self, warning: bool) -> Self {
self.validation = if warning {
InputValidationState::Warning
} else {
InputValidationState::Neutral
};
self
}
/// The current text content.
pub fn text(&self) -> &str {
&self.content
}
/// Programmatically sets the text content (e.g. `SearchInput`/`Combobox`
/// setting the display text after a selection). Notifies for re-render.
pub fn set_text(&mut self, text: impl Into<String>, cx: &mut Context<Self>) {
self.content = text.into();
cx.notify();
}
/// Clears the text content (e.g. `SearchInput`'s clear button). Notifies
/// for re-render.
pub fn clear(&mut self, cx: &mut Context<Self>) {
self.content.clear();
cx.notify();
}
/// Dynamically toggles read-only mode after construction (e.g.
/// `CodeEditor` switching an already-created input between editable and
/// preview modes). Notifies for re-render.
pub fn set_read_only(&mut self, read_only: bool, cx: &mut Context<Self>) {
self.read_only = read_only;
cx.notify();
}
fn on_key_down(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context<Self>) {
if self.read_only {
return;
}
let keystroke = &event.keystroke;
if self.submit_on_enter && keystroke.key == "enter" {
let wants_newline = self.multiline
&& (keystroke.modifiers.shift
|| keystroke.modifiers.control
|| keystroke.modifiers.platform);
if wants_newline {
self.content.push('\n');
} else if let Some(on_submit) = self.on_submit.clone() {
on_submit(window, cx);
}
cx.notify();
return;
}
// Ignore keyboard shortcuts (cmd/ctrl chords) — only capture text input.
if keystroke.modifiers.control || keystroke.modifiers.platform {
return;
}
// While an IME composition is in progress (marked text), defer
// printable input to the input method — it will commit via
// `replace_text_in_range`. Handling `key_char` here too would double
// the input. Editing keys (backspace/enter/space) still apply.
let composing = self.marked_range.is_some();
match keystroke.key.as_str() {
"backspace" => {
if let Some(range) = self.marked_range.take() {
self.content.replace_range(range, "");
} else {
self.content.pop();
}
}
"enter" if self.multiline => self.content.push('\n'),
"space" if !composing => self.content.push(' '),
_ => {
if !composing
&& let Some(text) = &keystroke.key_char
{
self.content.push_str(text);
}
}
}
cx.notify();
}
}
impl Render for TextInput {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let focused = self.focus_handle.is_focused(window);
let is_empty = self.content.is_empty();
let text_color = if is_empty {
semantic::text_placeholder(cx)
} else {
semantic::text(cx)
};
let border_color = match self.validation {
InputValidationState::Error => palette::danger(500),
InputValidationState::Success => palette::success(500),
InputValidationState::Warning => palette::warning(500),
InputValidationState::Neutral => semantic::border(cx),
};
let ring_color = match self.validation {
InputValidationState::Error => palette::danger(500),
InputValidationState::Success => palette::success(500),
InputValidationState::Warning => palette::warning(500),
InputValidationState::Neutral => palette::primary(500),
};
let show_cursor = focused && !is_empty && !self.read_only;
let cursor = || div().w(px(1.)).h(px(16.)).bg(palette::primary(500));
// Multiline content is split and rendered one row per line (rather
// than a single text child carrying embedded `\n`s) so each typed
// newline reliably produces a new visual row, with the blinking
// caret appended to the last row.
let content: AnyElement = if self.multiline {
let text: SharedString = if is_empty {
self.placeholder.clone()
} else {
self.content.clone().into()
};
let lines: Vec<String> = text.split('\n').map(str::to_string).collect();
let last_ix = lines.len().saturating_sub(1);
v_flex()
.w_full()
.children(lines.into_iter().enumerate().map(|(ix, line)| {
h_flex()
.min_h(px(20.))
.items_center()
.gap(DynamicSpacing::Base02.rems(cx))
.child(SharedString::from(line))
.when(ix == last_ix && show_cursor, |this| this.child(cursor()))
}))
.into_any_element()
} else {
let display: SharedString = if is_empty {
self.placeholder.clone()
} else {
self.content.clone().into()
};
h_flex()
.flex_wrap()
.items_center()
.gap(DynamicSpacing::Base02.rems(cx))
.child(display)
.when(show_cursor, |this| this.child(cursor()))
.into_any_element()
};
let field = div()
.track_focus(&self.focus_handle)
.on_key_down(cx.listener(Self::on_key_down))
.on_mouse_down(
MouseButton::Left,
cx.listener(|this, _event, window, cx| {
window.focus(&this.focus_handle, cx);
cx.notify();
}),
)
.w_full()
.when(self.multiline, |this| this.min_h(px(96.)))
.px(DynamicSpacing::Base12.px(cx))
.py(DynamicSpacing::Base08.px(cx))
.rounded_md()
.bg(semantic::surface(cx))
.border_1()
.border_color(border_color)
.text_color(text_color)
.child(content)
.child({
// Register an IME input handler for this field so platform
// input methods (Vietnamese/CJK IME, dead-key composition)
// commit text through `EntityInputHandler` instead of being
// dropped. `handle_input` only activates while focused.
let focus_handle = self.focus_handle.clone();
let entity = cx.entity();
canvas(
move |_bounds, _window, _cx| {},
move |bounds, _state, window, cx| {
window.handle_input(
&focus_handle,
ElementInputHandler::new(bounds, entity.clone()),
cx,
);
},
)
.absolute()
.size_full()
});
focus_ring(field, focused, ring_color)
}
}
impl Focusable for TextInput {
fn focus_handle(&self, _cx: &App) -> FocusHandle {
self.focus_handle.clone()
}
}
impl EntityInputHandler for TextInput {
fn accepts_text_input(&self, _window: &mut Window, _cx: &mut Context<Self>) -> bool {
!self.read_only
}
/// No cursor/selection tracking — this is an append-only field. Returning
/// `None` tells the platform there is no active selection (IME commits at
/// the end).
fn selected_text_range(
&mut self,
_ignore_disabled_input: bool,
_window: &mut Window,
_cx: &mut Context<Self>,
) -> Option<UTF16Selection> {
None
}
/// The byte range of in-progress IME marked (composition) text.
fn marked_text_range(
&self,
_window: &mut Window,
_cx: &mut Context<Self>,
) -> Option<Range<usize>> {
self.marked_range.clone()
}
/// Returns the text in the given UTF-16 range (used by the platform to
/// read back what's around the composition).
fn text_for_range(
&mut self,
range_utf16: Range<usize>,
_adjusted_range: &mut Option<Range<usize>>,
_window: &mut Window,
_cx: &mut Context<Self>,
) -> Option<String> {
let bytes = utf16_range_to_byte_range(&self.content, range_utf16)?;
Some(self.content[bytes].to_string())
}
/// IME committed text (or a plain paste/insert). Replaces any in-progress
/// marked range, otherwise appends at the end.
fn replace_text_in_range(
&mut self,
range: Option<Range<usize>>,
text: &str,
_window: &mut Window,
cx: &mut Context<Self>,
) {
if self.read_only {
return;
}
if let Some(marked) = self.marked_range.take() {
self.content.replace_range(marked, text);
} else if let Some(range) = range {
let bytes = utf16_range_to_byte_range(&self.content, range);
if let Some(bytes) = bytes {
self.content.replace_range(bytes, text);
} else {
self.content.push_str(text);
}
} else {
self.content.push_str(text);
}
cx.notify();
}
/// IME composition in progress — replace the given range (or append) with
/// `new_text` and mark it as the active composition range.
fn replace_and_mark_text_in_range(
&mut self,
range: Option<Range<usize>>,
new_text: &str,
_new_selected_range: Option<Range<usize>>,
_window: &mut Window,
cx: &mut Context<Self>,
) {
if self.read_only {
return;
}
let start_byte = if let Some(marked) = self.marked_range.clone() {
self.content.replace_range(marked.clone(), new_text);
marked.start
} else if let Some(range) = range {
let bytes = utf16_range_to_byte_range(&self.content, range);
if let Some(bytes) = bytes {
self.content.replace_range(bytes.clone(), new_text);
bytes.start
} else {
self.content.push_str(new_text);
self.content.len().saturating_sub(new_text.len())
}
} else {
let start = self.content.len();
self.content.push_str(new_text);
start
};
let end_byte = start_byte + new_text.len();
self.marked_range = Some(start_byte..end_byte);
cx.notify();
}
/// Composition finalized/abandoned — drop the mark without changing text.
fn unmark_text(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
if self.marked_range.take().is_some() {
cx.notify();
}
}
/// Bounds for a UTF-16 range within the field, for IME candidate placement.
/// Best-effort: returns the element bounds (candidates appear near the
/// field, not pixel-perfect per-character).
fn bounds_for_range(
&mut self,
_range_utf16: Range<usize>,
element_bounds: Bounds<Pixels>,
_window: &mut Window,
_cx: &mut Context<Self>,
) -> Option<Bounds<Pixels>> {
Some(element_bounds)
}
fn character_index_for_point(
&mut self,
_point: gpui::Point<Pixels>,
_window: &mut Window,
_cx: &mut Context<Self>,
) -> Option<usize> {
None
}
}
/// Maps a UTF-16 code-unit range to a byte range within `text` (Rust strings
/// are UTF-8). Returns `None` if the range is out of bounds.
fn utf16_range_to_byte_range(text: &str, range_utf16: Range<usize>) -> Option<Range<usize>> {
let mut start_byte = None;
let mut end_byte = None;
let mut utf16_index = 0usize;
for (byte_idx, ch) in text.char_indices() {
if start_byte.is_none() && utf16_index >= range_utf16.start {
start_byte = Some(byte_idx);
}
utf16_index += ch.len_utf16();
if end_byte.is_none() && utf16_index >= range_utf16.end {
end_byte = Some(byte_idx + ch.len_utf8());
break;
}
}
let start = start_byte.unwrap_or(text.len());
let end = end_byte.unwrap_or(text.len());
if start <= end && start <= text.len() && end <= text.len() {
Some(start..end)
} else {
None
}
}
/// A multi-line text field. Construct with
/// `cx.new(|cx| Textarea::new(cx).multiline(true))`.
pub type Textarea = TextInput;