Skip to main content

egui/widgets/text_edit/
builder.rs

1use std::sync::Arc;
2
3use emath::{Rect, TSTransform};
4use epaint::text::{Galley, LayoutJob, TextWrapMode, cursor::CCursor};
5
6use crate::{
7    Align, Align2, Atom, AtomExt as _, AtomKind, AtomLayout, Atoms, Color32, Context, CursorIcon,
8    Event, EventFilter, FontSelection, Frame, Id, ImeEvent, IntoAtoms, IntoSizedResult, Key,
9    KeyboardShortcut, Margin, Modifiers, NumExt as _, Response, Sense, SizedAtomKind, TextBuffer,
10    TextStyle, Ui, Vec2, Widget, WidgetInfo, WidgetWithState, epaint,
11    os::OperatingSystem,
12    output::OutputEvent,
13    response, text_selection,
14    text_selection::{CCursorRange, text_cursor_state::cursor_rect, visuals::paint_text_selection},
15    vec2,
16};
17
18use super::{TextEditOutput, TextEditState};
19
20type LayouterFn<'t> = &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>;
21
22/// A text region that the user can edit the contents of.
23///
24/// See also [`Ui::text_edit_singleline`] and [`Ui::text_edit_multiline`].
25///
26/// Example:
27///
28/// ```
29/// # egui::__run_test_ui(|ui| {
30/// # let mut my_string = String::new();
31/// let response = ui.add(egui::TextEdit::singleline(&mut my_string));
32/// if response.changed() {
33///     // …
34/// }
35/// if response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
36///     // …
37/// }
38/// # });
39/// ```
40///
41/// To fill an [`Ui`] with a [`TextEdit`] use [`Ui::add_sized`]:
42///
43/// ```
44/// # egui::__run_test_ui(|ui| {
45/// # let mut my_string = String::new();
46/// ui.add_sized(ui.available_size(), egui::TextEdit::multiline(&mut my_string));
47/// # });
48/// ```
49///
50///
51/// You can also use [`TextEdit`] to show text that can be selected, but not edited.
52/// To do so, pass in a `&mut` reference to a `&str`, for instance:
53///
54/// ```
55/// fn selectable_text(ui: &mut egui::Ui, mut text: &str) {
56///     ui.add(egui::TextEdit::multiline(&mut text));
57/// }
58/// ```
59///
60/// ## Advanced usage
61/// See [`TextEdit::show`].
62///
63/// ## Other
64/// The background color of a [`crate::TextEdit`] is [`crate::Visuals::text_edit_bg_color`] or can be set with [`crate::TextEdit::background_color`].
65#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
66pub struct TextEdit<'t> {
67    text: &'t mut dyn TextBuffer,
68    prefix: Atoms<'static>,
69    suffix: Atoms<'static>,
70    hint_text: Atoms<'static>,
71    id: Option<Id>,
72    id_salt: Option<Id>,
73    font_selection: FontSelection,
74    text_color: Option<Color32>,
75    layouter: Option<LayouterFn<'t>>,
76    password: bool,
77    frame: Option<Frame>,
78    margin: Margin,
79    multiline: bool,
80    interactive: bool,
81    desired_width: Option<f32>,
82    desired_height_rows: usize,
83    event_filter: EventFilter,
84    cursor_at_end: bool,
85    min_size: Vec2,
86    align: Align2,
87    clip_text: bool,
88    char_limit: usize,
89    return_key: Option<KeyboardShortcut>,
90    background_color: Option<Color32>,
91}
92
93impl WidgetWithState for TextEdit<'_> {
94    type State = TextEditState;
95}
96
97impl TextEdit<'_> {
98    pub fn load_state(ctx: &Context, id: Id) -> Option<TextEditState> {
99        TextEditState::load(ctx, id)
100    }
101
102    pub fn store_state(ctx: &Context, id: Id, state: TextEditState) {
103        state.store(ctx, id);
104    }
105}
106
107impl<'t> TextEdit<'t> {
108    /// No newlines (`\n`) allowed. Pressing enter key will result in the [`TextEdit`] losing focus (`response.lost_focus`).
109    pub fn singleline(text: &'t mut dyn TextBuffer) -> Self {
110        Self {
111            desired_height_rows: 1,
112            multiline: false,
113            clip_text: true,
114            ..Self::multiline(text)
115        }
116    }
117
118    /// A [`TextEdit`] for multiple lines. Pressing enter key will create a new line by default (can be changed with [`return_key`](TextEdit::return_key)).
119    pub fn multiline(text: &'t mut dyn TextBuffer) -> Self {
120        Self {
121            text,
122            prefix: Default::default(),
123            suffix: Default::default(),
124            hint_text: Default::default(),
125            id: None,
126            id_salt: None,
127            font_selection: Default::default(),
128            text_color: None,
129            layouter: None,
130            password: false,
131            frame: None,
132            margin: Margin::symmetric(4, 2),
133            multiline: true,
134            interactive: true,
135            desired_width: None,
136            desired_height_rows: 4,
137            event_filter: EventFilter {
138                // moving the cursor is really important
139                horizontal_arrows: true,
140                vertical_arrows: true,
141                tab: false, // tab is used to change focus, not to insert a tab character
142                ..Default::default()
143            },
144            cursor_at_end: true,
145            min_size: Vec2::ZERO,
146            align: Align2::LEFT_TOP,
147            clip_text: false,
148            char_limit: usize::MAX,
149            return_key: Some(KeyboardShortcut::new(Modifiers::NONE, Key::Enter)),
150            background_color: None,
151        }
152    }
153
154    /// Build a [`TextEdit`] focused on code editing.
155    /// By default it comes with:
156    /// - monospaced font
157    /// - focus lock (tab will insert a tab character instead of moving focus)
158    pub fn code_editor(self) -> Self {
159        self.font(TextStyle::Monospace).lock_focus(true)
160    }
161
162    /// Use if you want to set an explicit [`Id`] for this widget.
163    #[inline]
164    pub fn id(mut self, id: Id) -> Self {
165        self.id = Some(id);
166        self
167    }
168
169    /// A source for the unique [`Id`], e.g. `.id_source("second_text_edit_field")` or `.id_source(loop_index)`.
170    #[inline]
171    pub fn id_source(self, id_salt: impl std::hash::Hash) -> Self {
172        self.id_salt(id_salt)
173    }
174
175    /// A source for the unique [`Id`], e.g. `.id_salt("second_text_edit_field")` or `.id_salt(loop_index)`.
176    #[inline]
177    pub fn id_salt(mut self, id_salt: impl std::hash::Hash) -> Self {
178        self.id_salt = Some(Id::new(id_salt));
179        self
180    }
181
182    /// Show a faint hint text when the text field is empty.
183    ///
184    /// If the hint text needs to be persisted even when the text field has input,
185    /// the following workaround can be used:
186    /// ```
187    /// # egui::__run_test_ui(|ui| {
188    /// # let mut my_string = String::new();
189    /// # use egui::{ Color32, FontId };
190    /// let text_edit = egui::TextEdit::multiline(&mut my_string)
191    ///     .desired_width(f32::INFINITY);
192    /// let output = text_edit.show(ui);
193    /// let painter = ui.painter_at(output.response.rect);
194    /// let text_color = Color32::from_rgba_premultiplied(100, 100, 100, 100);
195    /// let galley = painter.layout(
196    ///     String::from("Enter text"),
197    ///     FontId::default(),
198    ///     text_color,
199    ///     f32::INFINITY
200    /// );
201    /// painter.galley(output.galley_pos, galley, text_color);
202    /// # });
203    /// ```
204    #[inline]
205    pub fn hint_text(mut self, hint_text: impl IntoAtoms<'static>) -> Self {
206        self.hint_text = hint_text.into_atoms();
207        self
208    }
209
210    /// Add a prefix to the text edit. This will always be shown before the editable text.
211    #[inline]
212    pub fn prefix(mut self, prefix: impl IntoAtoms<'static>) -> Self {
213        self.prefix = prefix.into_atoms();
214        self
215    }
216
217    /// Add a suffix to the text edit. This will always be shown after the editable text.
218    #[inline]
219    pub fn suffix(mut self, suffix: impl IntoAtoms<'static>) -> Self {
220        self.suffix = suffix.into_atoms();
221        self
222    }
223
224    /// Set the background color of the [`TextEdit`]. The default is [`crate::Visuals::text_edit_bg_color`].
225    // TODO(bircni): remove this once #3284 is implemented
226    #[inline]
227    pub fn background_color(mut self, color: Color32) -> Self {
228        self.background_color = Some(color);
229        self
230    }
231
232    /// If true, hide the letters from view and prevent copying from the field.
233    #[inline]
234    pub fn password(mut self, password: bool) -> Self {
235        self.password = password;
236        self
237    }
238
239    /// Pick a [`crate::FontId`] or [`TextStyle`].
240    #[inline]
241    pub fn font(mut self, font_selection: impl Into<FontSelection>) -> Self {
242        self.font_selection = font_selection.into();
243        self
244    }
245
246    #[inline]
247    pub fn text_color(mut self, text_color: Color32) -> Self {
248        self.text_color = Some(text_color);
249        self
250    }
251
252    #[inline]
253    pub fn text_color_opt(mut self, text_color: Option<Color32>) -> Self {
254        self.text_color = text_color;
255        self
256    }
257
258    /// Override how text is being shown inside the [`TextEdit`].
259    ///
260    /// This can be used to implement things like syntax highlighting.
261    ///
262    /// This function will be called at least once per frame,
263    /// so it is strongly suggested that you cache the results of any syntax highlighter
264    /// so as not to waste CPU highlighting the same string every frame.
265    ///
266    /// The arguments is the enclosing [`Ui`] (so you can access e.g. [`Context::fonts`]),
267    /// the text and the wrap width.
268    ///
269    /// ```
270    /// # egui::__run_test_ui(|ui| {
271    /// # let mut my_code = String::new();
272    /// # fn my_memoized_highlighter(s: &str) -> egui::text::LayoutJob { Default::default() }
273    /// let mut layouter = |ui: &egui::Ui, buf: &dyn egui::TextBuffer, wrap_width: f32| {
274    ///     let mut layout_job: egui::text::LayoutJob = my_memoized_highlighter(buf.as_str());
275    ///     layout_job.wrap.max_width = wrap_width;
276    ///     ui.fonts_mut(|f| f.layout_job(layout_job))
277    /// };
278    /// ui.add(egui::TextEdit::multiline(&mut my_code).layouter(&mut layouter));
279    /// # });
280    /// ```
281    #[inline]
282    pub fn layouter(
283        mut self,
284        layouter: &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>,
285    ) -> Self {
286        self.layouter = Some(layouter);
287
288        self
289    }
290
291    /// Default is `true`. If set to `false` then you cannot interact with the text (neither edit or select it).
292    ///
293    /// Consider using [`Ui::add_enabled`] instead to also give the [`TextEdit`] a greyed out look.
294    #[inline]
295    pub fn interactive(mut self, interactive: bool) -> Self {
296        self.interactive = interactive;
297        self
298    }
299
300    /// Customize the [`Frame`] around the text edit.
301    #[inline]
302    pub fn frame(mut self, frame: Frame) -> Self {
303        self.frame = Some(frame);
304        self
305    }
306
307    /// Set margin of text. Default is `Margin::symmetric(4.0, 2.0)`
308    #[inline]
309    pub fn margin(mut self, margin: impl Into<Margin>) -> Self {
310        self.margin = margin.into();
311        self
312    }
313
314    /// Set to 0.0 to keep as small as possible.
315    /// Set to [`f32::INFINITY`] to take up all available space (i.e. disable automatic word wrap).
316    #[inline]
317    pub fn desired_width(mut self, desired_width: f32) -> Self {
318        self.desired_width = Some(desired_width);
319        self
320    }
321
322    /// Set the number of rows to show by default.
323    /// The default for singleline text is `1`.
324    /// The default for multiline text is `4`.
325    #[inline]
326    pub fn desired_rows(mut self, desired_height_rows: usize) -> Self {
327        self.desired_height_rows = desired_height_rows;
328        self
329    }
330
331    /// When `false` (default), pressing TAB will move focus
332    /// to the next widget.
333    ///
334    /// When `true`, the widget will keep the focus and pressing TAB
335    /// will insert the `'\t'` character.
336    #[inline]
337    pub fn lock_focus(mut self, tab_will_indent: bool) -> Self {
338        self.event_filter.tab = tab_will_indent;
339        self
340    }
341
342    /// When `true` (default), the cursor will initially be placed at the end of the text.
343    ///
344    /// When `false`, the cursor will initially be placed at the beginning of the text.
345    #[inline]
346    pub fn cursor_at_end(mut self, b: bool) -> Self {
347        self.cursor_at_end = b;
348        self
349    }
350
351    /// When `true` (default), overflowing text will be clipped.
352    ///
353    /// When `false`, widget width will expand to make all text visible.
354    ///
355    /// This only works for singleline [`TextEdit`].
356    #[inline]
357    pub fn clip_text(mut self, b: bool) -> Self {
358        // always show everything in multiline
359        if !self.multiline {
360            self.clip_text = b;
361        }
362        self
363    }
364
365    /// Sets the limit for the amount of characters can be entered
366    ///
367    /// This only works for singleline [`TextEdit`]
368    #[inline]
369    pub fn char_limit(mut self, limit: usize) -> Self {
370        self.char_limit = limit;
371        self
372    }
373
374    /// Set the horizontal align of the inner text.
375    #[inline]
376    pub fn horizontal_align(mut self, align: Align) -> Self {
377        self.align.0[0] = align;
378        self
379    }
380
381    /// Set the vertical align of the inner text.
382    #[inline]
383    pub fn vertical_align(mut self, align: Align) -> Self {
384        self.align.0[1] = align;
385        self
386    }
387
388    /// Set the minimum size of the [`TextEdit`].
389    #[inline]
390    pub fn min_size(mut self, min_size: Vec2) -> Self {
391        self.min_size = min_size;
392        self
393    }
394
395    /// Set the return key combination.
396    ///
397    /// This combination will cause a newline on multiline,
398    /// whereas on singleline it will cause the widget to lose focus.
399    ///
400    /// This combination is optional and can be disabled by passing [`None`] into this function.
401    #[inline]
402    pub fn return_key(mut self, return_key: impl Into<Option<KeyboardShortcut>>) -> Self {
403        self.return_key = return_key.into();
404        self
405    }
406}
407
408// ----------------------------------------------------------------------------
409
410impl Widget for TextEdit<'_> {
411    fn ui(self, ui: &mut Ui) -> Response {
412        self.show(ui).response.response
413    }
414}
415
416impl TextEdit<'_> {
417    /// Show the [`TextEdit`], returning a rich [`TextEditOutput`].
418    ///
419    /// ```
420    /// # egui::__run_test_ui(|ui| {
421    /// # let mut my_string = String::new();
422    /// let output = egui::TextEdit::singleline(&mut my_string).show(ui);
423    /// if let Some(text_cursor_range) = output.cursor_range {
424    ///     use egui::TextBuffer as _;
425    ///     let selected_chars = text_cursor_range.as_sorted_char_range();
426    ///     let selected_text = my_string.char_range(selected_chars);
427    ///     ui.label("Selected text: ");
428    ///     ui.monospace(selected_text);
429    /// }
430    /// # });
431    /// ```
432    pub fn show(self, ui: &mut Ui) -> TextEditOutput {
433        let TextEdit {
434            text,
435            prefix,
436            suffix,
437            mut hint_text,
438            id,
439            id_salt,
440            font_selection,
441            text_color,
442            layouter,
443            password,
444            frame,
445            margin,
446            multiline,
447            interactive,
448            desired_width,
449            desired_height_rows,
450            event_filter,
451            cursor_at_end,
452            min_size,
453            align,
454            clip_text,
455            char_limit,
456            return_key,
457            background_color,
458        } = self;
459
460        let text_color = text_color
461            .or_else(|| ui.visuals().override_text_color)
462            // .unwrap_or_else(|| ui.style().interact(&response).text_color()); // too bright
463            .unwrap_or_else(|| ui.visuals().widgets.inactive.text_color());
464
465        let prev_text = text.as_str().to_owned();
466        let hint_text_str = hint_text.text().unwrap_or_default().to_string();
467
468        let font_id = font_selection.resolve(ui.style());
469        let row_height = ui.fonts_mut(|f| f.row_height(&font_id));
470        const MIN_WIDTH: f32 = 24.0; // Never make a [`TextEdit`] more narrow than this.
471        let available_width = ui.available_width().at_least(MIN_WIDTH);
472        let desired_width = desired_width
473            .unwrap_or_else(|| ui.spacing().text_edit_width)
474            .at_least(min_size.x);
475        let allocate_width = desired_width.at_most(available_width);
476
477        let font_id_clone = font_id.clone();
478        let mut default_layouter = move |ui: &Ui, text: &dyn TextBuffer, wrap_width: f32| {
479            let text = mask_if_password(password, text.as_str());
480            let layout_job = if multiline {
481                LayoutJob::simple(text, font_id_clone.clone(), text_color, wrap_width)
482            } else {
483                LayoutJob::simple_singleline(text, font_id_clone.clone(), text_color)
484            };
485            ui.fonts_mut(|f| f.layout_job(layout_job))
486        };
487
488        let layouter = layouter.unwrap_or(&mut default_layouter);
489
490        let min_inner_height = (desired_height_rows.at_least(1) as f32) * row_height;
491
492        let id = id.unwrap_or_else(|| {
493            if let Some(id_salt) = id_salt {
494                ui.make_persistent_id(id_salt)
495            } else {
496                // Since we are only storing the cursor a persistent Id is not super important
497                let id = ui.next_auto_id();
498                ui.skip_ahead_auto_ids(1);
499                id
500            }
501        });
502
503        // On touch screens (e.g. mobile in `eframe` web), should
504        // dragging select text, or scroll the enclosing [`ScrollArea`] (if any)?
505        // Since currently copying selected text in not supported on `eframe` web,
506        // we prioritize touch-scrolling:
507        let allow_drag_to_select =
508            ui.input(|i| !i.has_touch_screen()) || ui.memory(|mem| mem.has_focus(id));
509
510        let sense = if interactive {
511            if allow_drag_to_select {
512                Sense::click_and_drag()
513            } else {
514                Sense::click()
515            }
516        } else {
517            Sense::hover()
518        };
519
520        let mut state = TextEditState::load(ui.ctx(), id).unwrap_or_default();
521        let mut cursor_range = None;
522        let mut prev_cursor_range = None;
523
524        let mut text_changed = false;
525        let text_mutable = text.is_mutable();
526
527        let mut handle_events = |ui: &Ui, galley: &mut Arc<Galley>, layouter, wrap_width, text| {
528            if interactive && ui.memory(|mem| mem.has_focus(id)) {
529                ui.memory_mut(|mem| mem.set_focus_lock_filter(id, event_filter));
530
531                let default_cursor_range = if cursor_at_end {
532                    CCursorRange::one(galley.end())
533                } else {
534                    CCursorRange::default()
535                };
536                prev_cursor_range = state.cursor.range(galley);
537
538                let (changed, new_cursor_range) = events(
539                    ui,
540                    &mut state,
541                    text,
542                    galley,
543                    layouter,
544                    id,
545                    wrap_width,
546                    multiline,
547                    password,
548                    default_cursor_range,
549                    char_limit,
550                    event_filter,
551                    return_key,
552                );
553
554                if changed {
555                    text_changed = true;
556                }
557                cursor_range = Some(new_cursor_range);
558            }
559        };
560
561        // We need to calculate the galley within the atom closure, so we can calculate it based on
562        // the available width (in case of wrapping multiline text edits). But we show it later,
563        // so we can clip it to the available size. Thus, extract it from the atom closure here.
564        let mut get_galley = None;
565        let inner_rect_id = Id::new("text_edit_rect");
566        let mut response = {
567            let any_shrink = hint_text.any_shrink();
568            // Ideally we could just do `let mut atoms = prefix` here, but that won't compile
569            // but due to servo/rust-smallvec#146 (also see the comment below).
570            let mut atoms: Atoms<'_> = Atoms::new(());
571
572            // TODO(servo/rust-smallvec#146): Use extend_right instead of the loop once we have
573            // smallvec 2.0. Using `extend_right` here won't compile, due to lifetime issues.
574            for atom in prefix {
575                atoms.push_right(atom);
576            }
577
578            if text.as_str().is_empty() && !hint_text.is_empty() {
579                // Add hint_text (if any):
580                let mut shrunk = any_shrink;
581                let mut first = true;
582
583                // Since we can't set a fallback color per atom, we have to override it here.
584                // Sucks, since it means users won't be able to override it.
585                hint_text.map_texts(|t| t.color(ui.style().visuals.weak_text_color()));
586
587                for mut atom in hint_text {
588                    if !shrunk && matches!(atom.kind, AtomKind::Text(_)) {
589                        // elide the hint_text if needed
590                        atom = atom.atom_shrink(true);
591                        shrunk = true;
592                    }
593
594                    if first {
595                        // The first atom in the hint text gets inner_rect_id, so we can know
596                        // where to paint the cursor
597                        atom = atom.atom_id(inner_rect_id);
598                        first = false;
599                    }
600
601                    // The hint text should be shown left top instead of centered (important for
602                    // multi line text edits)
603                    atoms.push_right(atom.atom_align(Align2::LEFT_TOP));
604                }
605
606                // Calculate the empty galley, so it can be read later. The available width is
607                // technically wrong, but doesn't matter since the galley is empty
608                let available_width = allocate_width - margin.sum().x;
609                let galley = layouter(ui, text, available_width);
610
611                // We can't update the galley immediately here, since it would show both hint text
612                // and the newly typed letter. So we pass a clone instead, and accept having a frame
613                // delay on the very first keystroke.
614                let mut galley_clone = Arc::clone(&galley);
615                handle_events(ui, &mut galley_clone, layouter, available_width, text);
616
617                get_galley = Some(galley);
618            } else {
619                // We need a closure here, so we can calculate the galley based on the available
620                // width (after adding suffix and prefix), for correct wrapping in multi line text
621                // edits
622                atoms.push_right(
623                    AtomKind::closure(|ui, args| {
624                        let mut galley = layouter(ui, text, args.available_size.x);
625
626                        // Handling events here allows us to update the galley immediately on
627                        // keystrokes, avoiding frame delays, and ensuring the scroll_to within
628                        // ScrollAreas works correctly.
629                        handle_events(ui, &mut galley, layouter, args.available_size.x, text);
630
631                        let intrinsic_size = galley.intrinsic_size();
632                        let mut size = galley.size();
633                        size.y = size.y.at_least(min_inner_height);
634                        if clip_text {
635                            size.x = size.x.at_most(args.available_size.x);
636                        }
637
638                        // We paint the galley later, so we can do clipping and offsetting
639                        get_galley = Some(galley);
640                        IntoSizedResult {
641                            intrinsic_size,
642                            sized: SizedAtomKind::Empty { size: Some(size) },
643                        }
644                    })
645                    .atom_id(inner_rect_id)
646                    .atom_shrink(clip_text),
647                );
648            }
649
650            // Ensure the suffix is always right-aligned
651            if !suffix.is_empty() {
652                atoms.push_right(Atom::grow());
653            }
654
655            // TODO(servo/rust-smallvec#146): Use extend_right instead of the loop once we have
656            // smallvec 2.0. Using `extend_right` here won't compile, due to lifetime issues.
657            for atom in suffix {
658                atoms.push_right(atom);
659            }
660
661            let custom_frame = frame.is_some();
662            let frame = frame.unwrap_or_else(|| Frame::new().inner_margin(margin));
663
664            let min_height = min_inner_height + frame.total_margin().sum().y;
665
666            // This wrap mode only affects the hint_text
667            let wrap_mode = if multiline {
668                TextWrapMode::Wrap
669            } else {
670                TextWrapMode::Truncate
671            };
672
673            let mut allocated = AtomLayout::new(atoms)
674                .id(id)
675                .min_size(Vec2::new(allocate_width, min_height))
676                .max_width(allocate_width)
677                .sense(sense)
678                .frame(frame)
679                .align2(Align2::LEFT_TOP)
680                .wrap_mode(wrap_mode)
681                .allocate(ui);
682
683            allocated.frame = if !custom_frame {
684                let visuals = ui.style().interact(&allocated.response);
685                let background_color =
686                    background_color.unwrap_or_else(|| ui.visuals().text_edit_bg_color());
687
688                let (corner_radius, background_color, stroke) = if text_mutable {
689                    if allocated.response.has_focus() {
690                        (
691                            visuals.corner_radius,
692                            background_color,
693                            ui.visuals().selection.stroke,
694                        )
695                    } else {
696                        (visuals.corner_radius, background_color, visuals.bg_stroke)
697                    }
698                } else {
699                    let visuals = &ui.style().visuals.widgets.inactive;
700                    (
701                        visuals.corner_radius,
702                        Color32::TRANSPARENT,
703                        visuals.bg_stroke,
704                    )
705                };
706                allocated
707                    .frame
708                    .fill(background_color)
709                    .corner_radius(corner_radius)
710                    .inner_margin(
711                        allocated.frame.inner_margin
712                            + Margin::same((visuals.expansion - stroke.width).round() as i8),
713                    )
714                    .outer_margin(Margin::same(-(visuals.expansion as i8)))
715                    .stroke(stroke)
716            } else {
717                allocated.frame
718            };
719
720            allocated.paint(ui)
721        };
722
723        let inner_rect = response.rect(inner_rect_id).unwrap_or(Rect::ZERO);
724
725        // Our atom closure was now called, so the galley should always be available here
726        let mut galley = get_galley.expect("Galley should be available here");
727
728        // Don't send `OutputEvent::Clicked` when a user presses the space bar
729        response.flags -= response::Flags::FAKE_PRIMARY_CLICKED;
730        let text_clip_rect = inner_rect;
731        let painter = ui.painter_at(text_clip_rect.expand(1.0)); // expand to avoid clipping cursor
732
733        if interactive && let Some(pointer_pos) = response.interact_pointer_pos() {
734            if response.hovered() && text.is_mutable() {
735                ui.output_mut(|o| o.mutable_text_under_cursor = true);
736            }
737
738            // TODO(emilk): drag selected text to either move or clone (ctrl on windows, alt on mac)
739
740            let cursor_at_pointer =
741                galley.cursor_from_pos(pointer_pos - inner_rect.min + state.text_offset);
742
743            if ui.visuals().text_cursor.preview
744                && response.hovered()
745                && ui.input(|i| i.pointer.is_moving())
746            {
747                // text cursor preview:
748                let cursor_rect = TSTransform::from_translation(inner_rect.min.to_vec2())
749                    * cursor_rect(&galley, &cursor_at_pointer, row_height);
750                text_selection::visuals::paint_cursor_end(&painter, ui.visuals(), cursor_rect);
751            }
752
753            let is_being_dragged = ui.is_being_dragged(response.id);
754            let did_interact = state.cursor.pointer_interaction(
755                ui,
756                &response,
757                cursor_at_pointer,
758                &galley,
759                is_being_dragged,
760            );
761
762            if did_interact || response.clicked() {
763                ui.memory_mut(|mem| mem.request_focus(response.id));
764
765                state.last_interaction_time = ui.input(|i| i.time);
766            }
767        }
768
769        if interactive && response.hovered() {
770            ui.set_cursor_icon(CursorIcon::Text);
771        }
772
773        if text_changed {
774            response.mark_changed();
775        }
776
777        let mut galley_pos = align
778            .align_size_within_rect(galley.size(), inner_rect)
779            .intersect(inner_rect) // limit pos to the response rect area
780            .min;
781        let align_offset = inner_rect.left_top() - galley_pos;
782
783        // Visual clipping for singleline text editor with text larger than width
784        if clip_text && align_offset.x == 0.0 {
785            let cursor_pos = match (cursor_range, ui.memory(|mem| mem.has_focus(id))) {
786                (Some(cursor_range), true) => galley.pos_from_cursor(cursor_range.primary).min.x,
787                _ => 0.0,
788            };
789
790            let mut offset_x = state.text_offset.x;
791            let visible_range = offset_x..=offset_x + inner_rect.width();
792
793            if !visible_range.contains(&cursor_pos) {
794                if cursor_pos < *visible_range.start() {
795                    offset_x = cursor_pos;
796                } else {
797                    offset_x = cursor_pos - inner_rect.width();
798                }
799            }
800
801            offset_x = offset_x
802                .at_most(galley.size().x - inner_rect.width())
803                .at_least(0.0);
804
805            state.text_offset = vec2(offset_x, align_offset.y);
806            galley_pos -= vec2(offset_x, 0.0);
807        } else {
808            state.text_offset = align_offset;
809        }
810
811        let selection_changed = if let (Some(cursor_range), Some(prev_cursor_range)) =
812            (cursor_range, prev_cursor_range)
813        {
814            prev_cursor_range != cursor_range
815        } else {
816            false
817        };
818
819        if ui.is_rect_visible(inner_rect) {
820            let has_focus = ui.memory(|mem| mem.has_focus(id));
821
822            if has_focus && let Some(cursor_range) = state.cursor.range(&galley) {
823                // Add text selection rectangles to the galley:
824                paint_text_selection(&mut galley, ui.visuals(), &cursor_range, None);
825            }
826
827            painter.galley(
828                galley_pos - vec2(galley.rect.left(), 0.0),
829                Arc::clone(&galley),
830                text_color,
831            );
832
833            if has_focus && let Some(cursor_range) = state.cursor.range(&galley) {
834                let primary_cursor_rect = cursor_rect(&galley, &cursor_range.primary, row_height)
835                    .translate(galley_pos.to_vec2());
836
837                if response.changed() || selection_changed {
838                    // Scroll to keep primary cursor in view:
839                    ui.scroll_to_rect(primary_cursor_rect, None);
840                }
841
842                if text.is_mutable() && interactive {
843                    let now = ui.input(|i| i.time);
844                    if response.changed() || selection_changed {
845                        state.last_interaction_time = now;
846                    }
847
848                    // Only show (and blink) cursor if the egui viewport has focus.
849                    // This is for two reasons:
850                    // * Don't give the impression that the user can type into a window without focus
851                    // * Don't repaint the ui because of a blinking cursor in an app that is not in focus
852                    let viewport_has_focus = ui.input(|i| i.focused);
853                    if viewport_has_focus {
854                        text_selection::visuals::paint_text_cursor(
855                            ui,
856                            &painter,
857                            primary_cursor_rect,
858                            now - state.last_interaction_time,
859                        );
860                    }
861
862                    // Set IME output (in screen coords) when text is editable and visible
863                    let to_global = ui
864                        .ctx()
865                        .layer_transform_to_global(ui.layer_id())
866                        .unwrap_or_default();
867
868                    ui.output_mut(|o| {
869                        o.ime = Some(crate::output::IMEOutput {
870                            rect: to_global * inner_rect,
871                            cursor_rect: to_global * primary_cursor_rect,
872                        });
873                    });
874                }
875            }
876        }
877
878        // Ensures correct IME behavior when the text input area gains or loses focus.
879        if state.ime_enabled && (response.gained_focus() || response.lost_focus()) {
880            state.ime_enabled = false;
881            if let Some(mut ccursor_range) = state.cursor.char_range() {
882                ccursor_range.secondary.index = ccursor_range.primary.index;
883                state.cursor.set_char_range(Some(ccursor_range));
884            }
885            ui.input_mut(|i| i.events.retain(|e| !matches!(e, Event::Ime(_))));
886        }
887
888        state.clone().store(ui.ctx(), id);
889
890        if response.changed() {
891            response.widget_info(|| {
892                WidgetInfo::text_edit(
893                    ui.is_enabled(),
894                    mask_if_password(password, prev_text.as_str()),
895                    mask_if_password(password, text.as_str()),
896                    hint_text_str.as_str(),
897                )
898            });
899        } else if selection_changed && let Some(cursor_range) = cursor_range {
900            let char_range = cursor_range.primary.index..=cursor_range.secondary.index;
901            let info = WidgetInfo::text_selection_changed(
902                ui.is_enabled(),
903                char_range,
904                mask_if_password(password, text.as_str()),
905            );
906            response.output_event(OutputEvent::TextSelectionChanged(info));
907        } else {
908            response.widget_info(|| {
909                WidgetInfo::text_edit(
910                    ui.is_enabled(),
911                    mask_if_password(password, prev_text.as_str()),
912                    mask_if_password(password, text.as_str()),
913                    hint_text_str.as_str(),
914                )
915            });
916        }
917
918        let role = if password {
919            accesskit::Role::PasswordInput
920        } else if multiline {
921            accesskit::Role::MultilineTextInput
922        } else {
923            accesskit::Role::TextInput
924        };
925
926        crate::text_selection::accesskit_text::update_accesskit_for_text_widget(
927            ui.ctx(),
928            id,
929            cursor_range,
930            role,
931            TSTransform::from_translation(galley_pos.to_vec2()),
932            &galley,
933        );
934
935        TextEditOutput {
936            response,
937            galley,
938            galley_pos,
939            text_clip_rect,
940            state,
941            cursor_range,
942        }
943    }
944}
945
946fn mask_if_password(is_password: bool, text: &str) -> String {
947    fn mask_password(text: &str) -> String {
948        std::iter::repeat_n(
949            epaint::text::PASSWORD_REPLACEMENT_CHAR,
950            text.chars().count(),
951        )
952        .collect::<String>()
953    }
954
955    if is_password {
956        mask_password(text)
957    } else {
958        text.to_owned()
959    }
960}
961
962// ----------------------------------------------------------------------------
963
964/// Check for (keyboard) events to edit the cursor and/or text.
965#[expect(clippy::too_many_arguments)]
966fn events(
967    ui: &crate::Ui,
968    state: &mut TextEditState,
969    text: &mut dyn TextBuffer,
970    galley: &mut Arc<Galley>,
971    layouter: &mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>,
972    id: Id,
973    wrap_width: f32,
974    multiline: bool,
975    password: bool,
976    default_cursor_range: CCursorRange,
977    char_limit: usize,
978    event_filter: EventFilter,
979    return_key: Option<KeyboardShortcut>,
980) -> (bool, CCursorRange) {
981    let os = ui.os();
982
983    let mut cursor_range = state.cursor.range(galley).unwrap_or(default_cursor_range);
984
985    // We feed state to the undoer both before and after handling input
986    // so that the undoer creates automatic saves even when there are no events for a while.
987    state.undoer.lock().feed_state(
988        ui.input(|i| i.time),
989        &(cursor_range, text.as_str().to_owned()),
990    );
991
992    let copy_if_not_password = |ui: &Ui, text: String| {
993        if !password {
994            ui.copy_text(text);
995        }
996    };
997
998    let mut any_change = false;
999
1000    let events = ui.input(|i| i.filtered_events(&event_filter));
1001
1002    for event in &events {
1003        let did_mutate_text = match event {
1004            // First handle events that only changes the selection cursor, not the text:
1005            event if cursor_range.on_event(os, event, galley, id) => None,
1006
1007            Event::Copy => {
1008                if !cursor_range.is_empty() {
1009                    copy_if_not_password(ui, cursor_range.slice_str(text.as_str()).to_owned());
1010                }
1011                None
1012            }
1013            Event::Cut => {
1014                if cursor_range.is_empty() {
1015                    None
1016                } else {
1017                    copy_if_not_password(ui, cursor_range.slice_str(text.as_str()).to_owned());
1018                    Some(CCursorRange::one(text.delete_selected(&cursor_range)))
1019                }
1020            }
1021            Event::Paste(text_to_insert) => {
1022                if !text_to_insert.is_empty() {
1023                    let mut ccursor = text.delete_selected(&cursor_range);
1024                    if multiline {
1025                        text.insert_text_at(&mut ccursor, text_to_insert, char_limit);
1026                    } else {
1027                        let single_line = text_to_insert.replace(['\r', '\n'], " ");
1028                        text.insert_text_at(&mut ccursor, &single_line, char_limit);
1029                    }
1030
1031                    Some(CCursorRange::one(ccursor))
1032                } else {
1033                    None
1034                }
1035            }
1036            Event::Text(text_to_insert) => {
1037                // Newlines are handled by `Key::Enter`.
1038                if !text_to_insert.is_empty() && text_to_insert != "\n" && text_to_insert != "\r" {
1039                    let mut ccursor = text.delete_selected(&cursor_range);
1040
1041                    text.insert_text_at(&mut ccursor, text_to_insert, char_limit);
1042
1043                    Some(CCursorRange::one(ccursor))
1044                } else {
1045                    None
1046                }
1047            }
1048            Event::Key {
1049                key: Key::Tab,
1050                pressed: true,
1051                modifiers,
1052                ..
1053            } if multiline => {
1054                let mut ccursor = text.delete_selected(&cursor_range);
1055                if modifiers.shift {
1056                    // TODO(emilk): support removing indentation over a selection?
1057                    text.decrease_indentation(&mut ccursor);
1058                } else {
1059                    text.insert_text_at(&mut ccursor, "\t", char_limit);
1060                }
1061                Some(CCursorRange::one(ccursor))
1062            }
1063            Event::Key {
1064                key,
1065                pressed: true,
1066                modifiers,
1067                ..
1068            } if return_key.is_some_and(|return_key| {
1069                *key == return_key.logical_key && modifiers.matches_logically(return_key.modifiers)
1070            }) =>
1071            {
1072                if multiline {
1073                    let mut ccursor = text.delete_selected(&cursor_range);
1074                    text.insert_text_at(&mut ccursor, "\n", char_limit);
1075                    // TODO(emilk): if code editor, auto-indent by same leading tabs, + one if the lines end on an opening bracket
1076                    Some(CCursorRange::one(ccursor))
1077                } else {
1078                    ui.memory_mut(|mem| mem.surrender_focus(id)); // End input with enter
1079                    break;
1080                }
1081            }
1082
1083            Event::Key {
1084                key,
1085                pressed: true,
1086                modifiers,
1087                ..
1088            } if (modifiers.matches_logically(Modifiers::COMMAND) && *key == Key::Y)
1089                || (modifiers.matches_logically(Modifiers::SHIFT | Modifiers::COMMAND)
1090                    && *key == Key::Z) =>
1091            {
1092                if let Some((redo_ccursor_range, redo_txt)) = state
1093                    .undoer
1094                    .lock()
1095                    .redo(&(cursor_range, text.as_str().to_owned()))
1096                {
1097                    text.replace_with(redo_txt);
1098                    Some(*redo_ccursor_range)
1099                } else {
1100                    None
1101                }
1102            }
1103
1104            Event::Key {
1105                key: Key::Z,
1106                pressed: true,
1107                modifiers,
1108                ..
1109            } if modifiers.matches_logically(Modifiers::COMMAND) => {
1110                if let Some((undo_ccursor_range, undo_txt)) = state
1111                    .undoer
1112                    .lock()
1113                    .undo(&(cursor_range, text.as_str().to_owned()))
1114                {
1115                    text.replace_with(undo_txt);
1116                    Some(*undo_ccursor_range)
1117                } else {
1118                    None
1119                }
1120            }
1121
1122            Event::Key {
1123                modifiers,
1124                key,
1125                pressed: true,
1126                ..
1127            } => check_for_mutating_key_press(os, &cursor_range, text, galley, modifiers, *key),
1128
1129            Event::Ime(ime_event) => {
1130                /// Both `ImeEvent::Preedit("")` and `ImeEvent::Commit("")`
1131                /// might be emitted from different integrations to signify that
1132                /// the current IME composition should be cleared.
1133                ///
1134                /// Example integrations where only `ImeEvent::Preedit("")` of
1135                /// those two events is emitted when the last character is
1136                /// deleted with a backspace:
1137                /// - `egui-winit` on macOS 15.7.3.
1138                /// - `egui-winit` on Debian13 with gnome48 and wayland.
1139                ///
1140                /// An example integration where only `ImeEvent::Commit("")` of
1141                /// those two events is emitted when the last character is
1142                /// deleted with a backspace:
1143                /// - `eframe`'s web integration on Safari 26.2 (on macOS
1144                ///   15.7.3).
1145                ///
1146                /// ## Note
1147                ///
1148                /// The term “pre-edit string” is used by X11 and Wayland, and
1149                /// we use “pre-edit text” and “pre-edit range” here in the
1150                /// same manner.
1151                /// See: <https://wayland.app/protocols/input-method-unstable-v2>
1152                ///
1153                /// We previously referred to “pre-edit text” as “prediction”,
1154                /// which is not standard and can mean different things.
1155                fn clear_preedit_text(
1156                    text: &mut dyn TextBuffer,
1157                    preedit_range: &CCursorRange,
1158                ) -> CCursor {
1159                    text.delete_selected(preedit_range)
1160                }
1161
1162                match ime_event {
1163                    ImeEvent::Enabled => {
1164                        state.ime_enabled = true;
1165                        state.ime_cursor_range = cursor_range;
1166                        None
1167                    }
1168                    ImeEvent::Preedit(preedit_text) => {
1169                        if preedit_text == "\n" || preedit_text == "\r" {
1170                            None
1171                        } else {
1172                            let mut ccursor = clear_preedit_text(text, &cursor_range);
1173
1174                            let start_cursor = ccursor;
1175                            if !preedit_text.is_empty() {
1176                                text.insert_text_at(&mut ccursor, preedit_text, char_limit);
1177                            }
1178                            state.ime_cursor_range = cursor_range;
1179                            Some(CCursorRange::two(start_cursor, ccursor))
1180                        }
1181                    }
1182                    ImeEvent::Commit(commit_text) => {
1183                        if commit_text == "\n" || commit_text == "\r" {
1184                            None
1185                        } else {
1186                            state.ime_enabled = false;
1187
1188                            let mut ccursor = clear_preedit_text(text, &cursor_range);
1189
1190                            if !commit_text.is_empty()
1191                                && cursor_range.secondary.index
1192                                    == state.ime_cursor_range.secondary.index
1193                            {
1194                                text.insert_text_at(&mut ccursor, commit_text, char_limit);
1195                            }
1196
1197                            Some(CCursorRange::one(ccursor))
1198                        }
1199                    }
1200                    ImeEvent::Disabled => {
1201                        state.ime_enabled = false;
1202                        None
1203                    }
1204                }
1205            }
1206
1207            _ => None,
1208        };
1209
1210        if let Some(new_ccursor_range) = did_mutate_text {
1211            any_change = true;
1212
1213            // Layout again to avoid frame delay, and to keep `text` and `galley` in sync.
1214            *galley = layouter(ui, text, wrap_width);
1215
1216            // Set cursor_range using new galley:
1217            cursor_range = new_ccursor_range;
1218        }
1219    }
1220
1221    state.cursor.set_char_range(Some(cursor_range));
1222
1223    state.undoer.lock().feed_state(
1224        ui.input(|i| i.time),
1225        &(cursor_range, text.as_str().to_owned()),
1226    );
1227
1228    (any_change, cursor_range)
1229}
1230
1231// ----------------------------------------------------------------------------
1232
1233/// Returns `Some(new_cursor)` if we did mutate `text`.
1234fn check_for_mutating_key_press(
1235    os: OperatingSystem,
1236    cursor_range: &CCursorRange,
1237    text: &mut dyn TextBuffer,
1238    galley: &Galley,
1239    modifiers: &Modifiers,
1240    key: Key,
1241) -> Option<CCursorRange> {
1242    match key {
1243        Key::Backspace => {
1244            let ccursor = if modifiers.mac_cmd {
1245                text.delete_paragraph_before_cursor(galley, cursor_range)
1246            } else if let Some(cursor) = cursor_range.single() {
1247                if modifiers.alt || modifiers.ctrl {
1248                    // alt on mac, ctrl on windows
1249                    text.delete_previous_word(cursor)
1250                } else {
1251                    text.delete_previous_char(cursor)
1252                }
1253            } else {
1254                text.delete_selected(cursor_range)
1255            };
1256            Some(CCursorRange::one(ccursor))
1257        }
1258
1259        Key::Delete if !modifiers.shift || os != OperatingSystem::Windows => {
1260            let ccursor = if modifiers.mac_cmd {
1261                text.delete_paragraph_after_cursor(galley, cursor_range)
1262            } else if let Some(cursor) = cursor_range.single() {
1263                if modifiers.alt || modifiers.ctrl {
1264                    // alt on mac, ctrl on windows
1265                    text.delete_next_word(cursor)
1266                } else {
1267                    text.delete_next_char(cursor)
1268                }
1269            } else {
1270                text.delete_selected(cursor_range)
1271            };
1272            let ccursor = CCursor {
1273                prefer_next_row: true,
1274                ..ccursor
1275            };
1276            Some(CCursorRange::one(ccursor))
1277        }
1278
1279        Key::H if modifiers.ctrl => {
1280            let ccursor = text.delete_previous_char(cursor_range.primary);
1281            Some(CCursorRange::one(ccursor))
1282        }
1283
1284        Key::K if modifiers.ctrl => {
1285            let ccursor = text.delete_paragraph_after_cursor(galley, cursor_range);
1286            Some(CCursorRange::one(ccursor))
1287        }
1288
1289        Key::U if modifiers.ctrl => {
1290            let ccursor = text.delete_paragraph_before_cursor(galley, cursor_range);
1291            Some(CCursorRange::one(ccursor))
1292        }
1293
1294        Key::W if modifiers.ctrl => {
1295            let ccursor = if let Some(cursor) = cursor_range.single() {
1296                text.delete_previous_word(cursor)
1297            } else {
1298                text.delete_selected(cursor_range)
1299            };
1300            Some(CCursorRange::one(ccursor))
1301        }
1302
1303        _ => None,
1304    }
1305}