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#[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 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 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 horizontal_arrows: true,
140 vertical_arrows: true,
141 tab: false, ..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 pub fn code_editor(self) -> Self {
159 self.font(TextStyle::Monospace).lock_focus(true)
160 }
161
162 #[inline]
164 pub fn id(mut self, id: Id) -> Self {
165 self.id = Some(id);
166 self
167 }
168
169 #[inline]
171 pub fn id_source(self, id_salt: impl std::hash::Hash) -> Self {
172 self.id_salt(id_salt)
173 }
174
175 #[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 #[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 #[inline]
212 pub fn prefix(mut self, prefix: impl IntoAtoms<'static>) -> Self {
213 self.prefix = prefix.into_atoms();
214 self
215 }
216
217 #[inline]
219 pub fn suffix(mut self, suffix: impl IntoAtoms<'static>) -> Self {
220 self.suffix = suffix.into_atoms();
221 self
222 }
223
224 #[inline]
227 pub fn background_color(mut self, color: Color32) -> Self {
228 self.background_color = Some(color);
229 self
230 }
231
232 #[inline]
234 pub fn password(mut self, password: bool) -> Self {
235 self.password = password;
236 self
237 }
238
239 #[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 #[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 #[inline]
295 pub fn interactive(mut self, interactive: bool) -> Self {
296 self.interactive = interactive;
297 self
298 }
299
300 #[inline]
302 pub fn frame(mut self, frame: Frame) -> Self {
303 self.frame = Some(frame);
304 self
305 }
306
307 #[inline]
309 pub fn margin(mut self, margin: impl Into<Margin>) -> Self {
310 self.margin = margin.into();
311 self
312 }
313
314 #[inline]
317 pub fn desired_width(mut self, desired_width: f32) -> Self {
318 self.desired_width = Some(desired_width);
319 self
320 }
321
322 #[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 #[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 #[inline]
346 pub fn cursor_at_end(mut self, b: bool) -> Self {
347 self.cursor_at_end = b;
348 self
349 }
350
351 #[inline]
357 pub fn clip_text(mut self, b: bool) -> Self {
358 if !self.multiline {
360 self.clip_text = b;
361 }
362 self
363 }
364
365 #[inline]
369 pub fn char_limit(mut self, limit: usize) -> Self {
370 self.char_limit = limit;
371 self
372 }
373
374 #[inline]
376 pub fn horizontal_align(mut self, align: Align) -> Self {
377 self.align.0[0] = align;
378 self
379 }
380
381 #[inline]
383 pub fn vertical_align(mut self, align: Align) -> Self {
384 self.align.0[1] = align;
385 self
386 }
387
388 #[inline]
390 pub fn min_size(mut self, min_size: Vec2) -> Self {
391 self.min_size = min_size;
392 self
393 }
394
395 #[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
408impl Widget for TextEdit<'_> {
411 fn ui(self, ui: &mut Ui) -> Response {
412 self.show(ui).response.response
413 }
414}
415
416impl TextEdit<'_> {
417 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.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; 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 let id = ui.next_auto_id();
498 ui.skip_ahead_auto_ids(1);
499 id
500 }
501 });
502
503 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 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 let mut atoms: Atoms<'_> = Atoms::new(());
571
572 for atom in prefix {
575 atoms.push_right(atom);
576 }
577
578 if text.as_str().is_empty() && !hint_text.is_empty() {
579 let mut shrunk = any_shrink;
581 let mut first = true;
582
583 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 atom = atom.atom_shrink(true);
591 shrunk = true;
592 }
593
594 if first {
595 atom = atom.atom_id(inner_rect_id);
598 first = false;
599 }
600
601 atoms.push_right(atom.atom_align(Align2::LEFT_TOP));
604 }
605
606 let available_width = allocate_width - margin.sum().x;
609 let galley = layouter(ui, text, available_width);
610
611 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 atoms.push_right(
623 AtomKind::closure(|ui, args| {
624 let mut galley = layouter(ui, text, args.available_size.x);
625
626 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 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 if !suffix.is_empty() {
652 atoms.push_right(Atom::grow());
653 }
654
655 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 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 let mut galley = get_galley.expect("Galley should be available here");
727
728 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)); 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 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 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) .min;
781 let align_offset = inner_rect.left_top() - galley_pos;
782
783 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 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 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 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 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 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#[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 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 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 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 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 Some(CCursorRange::one(ccursor))
1077 } else {
1078 ui.memory_mut(|mem| mem.surrender_focus(id)); 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 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 *galley = layouter(ui, text, wrap_width);
1215
1216 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
1231fn 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 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 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}