Skip to main content

fc_ui/capabilities/controls/
inline_edit.rs

1use crate::capabilities::controls::text_util;
2use crate::capabilities::foundation::theme::use_theme;
3use gpui::{prelude::FluentBuilder as _, *};
4use std::ops::Range;
5use std::rc::Rc;
6
7actions!(inline_edit, [Save, Cancel]);
8
9pub fn init(cx: &mut App) {
10    if !crate::capabilities::foundation::initialization::begin(cx, "inline-edit") {
11        return;
12    }
13    cx.bind_keys([
14        KeyBinding::new("enter", Save, Some("InlineEdit")),
15        KeyBinding::new("escape", Cancel, Some("InlineEdit")),
16    ]);
17}
18
19#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
20pub enum InlineEditTrigger {
21    #[default]
22    Click,
23    DoubleClick,
24}
25
26#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
27pub enum InlineEditBlurBehavior {
28    #[default]
29    Save,
30    Cancel,
31}
32
33pub struct InlineEditState {
34    value: String,
35    editing: bool,
36    edit_value: String,
37    focus_handle: FocusHandle,
38    selected_range: Range<usize>,
39    selection_reversed: bool,
40    marked_range: Option<Range<usize>>,
41    last_layout: Option<ShapedLine>,
42    last_bounds: Option<Bounds<Pixels>>,
43    is_selecting: bool,
44}
45
46impl InlineEditState {
47    pub fn new(cx: &mut Context<Self>) -> Self {
48        Self {
49            value: String::new(),
50            editing: false,
51            edit_value: String::new(),
52            focus_handle: cx.focus_handle(),
53            selected_range: 0..0,
54            selection_reversed: false,
55            marked_range: None,
56            last_layout: None,
57            last_bounds: None,
58            is_selecting: false,
59        }
60    }
61
62    pub fn with_value(cx: &mut Context<Self>, value: impl Into<String>) -> Self {
63        let value = value.into();
64        Self {
65            value: value.clone(),
66            editing: false,
67            edit_value: value,
68            focus_handle: cx.focus_handle(),
69            selected_range: 0..0,
70            selection_reversed: false,
71            marked_range: None,
72            last_layout: None,
73            last_bounds: None,
74            is_selecting: false,
75        }
76    }
77
78    pub fn value(&self) -> &str {
79        &self.value
80    }
81
82    pub fn set_value(&mut self, value: impl Into<String>, cx: &mut Context<Self>) {
83        self.value = value.into();
84        self.edit_value = self.value.clone();
85        self.invalidate_layout_cache();
86        cx.notify();
87    }
88
89    pub fn is_editing(&self) -> bool {
90        self.editing
91    }
92
93    pub fn start_editing(&mut self, cx: &mut Context<Self>) {
94        if self.editing {
95            return;
96        }
97        self.editing = true;
98        self.edit_value = self.value.clone();
99        let len = self.edit_value.len();
100        self.selected_range = 0..len;
101        self.selection_reversed = false;
102        self.marked_range = None;
103        self.invalidate_layout_cache();
104        cx.notify();
105    }
106
107    pub fn save(&mut self, cx: &mut Context<Self>) {
108        if !self.editing {
109            return;
110        }
111        self.value = self.edit_value.clone();
112        self.editing = false;
113        self.selected_range = 0..0;
114        self.marked_range = None;
115        self.invalidate_layout_cache();
116        cx.notify();
117    }
118
119    pub fn cancel(&mut self, cx: &mut Context<Self>) {
120        if !self.editing {
121            return;
122        }
123        self.edit_value = self.value.clone();
124        self.editing = false;
125        self.selected_range = 0..0;
126        self.marked_range = None;
127        self.invalidate_layout_cache();
128        cx.notify();
129    }
130
131    pub fn edit_value(&self) -> &str {
132        &self.edit_value
133    }
134
135    fn cursor_offset(&self) -> usize {
136        if self.selection_reversed {
137            self.selected_range.start
138        } else {
139            self.selected_range.end
140        }
141    }
142
143    fn move_to(&mut self, offset: usize, cx: &mut Context<Self>) {
144        self.selected_range = offset..offset;
145        cx.notify();
146    }
147
148    fn select_to(&mut self, offset: usize, cx: &mut Context<Self>) {
149        if self.selection_reversed {
150            self.selected_range.start = offset;
151        } else {
152            self.selected_range.end = offset;
153        }
154        if self.selected_range.end < self.selected_range.start {
155            self.selection_reversed = !self.selection_reversed;
156            self.selected_range = self.selected_range.end..self.selected_range.start;
157        }
158        cx.notify();
159    }
160
161    fn previous_boundary(&self, offset: usize) -> usize {
162        text_util::previous_grapheme_boundary(&self.edit_value, offset)
163    }
164
165    fn next_boundary(&self, offset: usize) -> usize {
166        text_util::next_grapheme_boundary(&self.edit_value, offset)
167    }
168
169    fn offset_to_utf16(&self, offset: usize) -> usize {
170        crate::capabilities::controls::text_util::offset_to_utf16(&self.edit_value, offset)
171    }
172
173    fn range_to_utf16(&self, range: &Range<usize>) -> Range<usize> {
174        crate::capabilities::controls::text_util::range_to_utf16(&self.edit_value, range)
175    }
176
177    fn range_from_utf16(&self, range_utf16: &Range<usize>) -> Range<usize> {
178        crate::capabilities::controls::text_util::range_from_utf16(&self.edit_value, range_utf16)
179    }
180
181    fn invalidate_layout_cache(&mut self) {
182        self.last_layout = None;
183        self.last_bounds = None;
184    }
185
186    fn layout_matches_content(&self, layout: &ShapedLine) -> bool {
187        layout.text.as_ref() == self.edit_value.as_str()
188    }
189
190    fn index_for_mouse_position(&mut self, position: Point<Pixels>) -> usize {
191        if self.edit_value.is_empty() {
192            return 0;
193        }
194        let (Some(bounds), Some(line)) = (self.last_bounds.as_ref(), self.last_layout.as_ref())
195        else {
196            return 0;
197        };
198        if !self.layout_matches_content(line) {
199            self.invalidate_layout_cache();
200            return self.cursor_offset();
201        }
202        if position.y < bounds.top() {
203            return 0;
204        }
205        if position.y > bounds.bottom() {
206            return self.edit_value.len();
207        }
208        line.closest_index_for_x(position.x - bounds.left())
209    }
210
211    fn on_mouse_down(&mut self, event: &MouseDownEvent, cx: &mut Context<Self>) {
212        self.is_selecting = true;
213        let click_index = self.index_for_mouse_position(event.position);
214        if event.modifiers.shift {
215            self.select_to(click_index, cx);
216        } else {
217            self.move_to(click_index, cx);
218        }
219    }
220
221    fn on_mouse_up(&mut self, _: &MouseUpEvent, _cx: &mut Context<Self>) {
222        self.is_selecting = false;
223    }
224
225    fn on_mouse_move(&mut self, event: &MouseMoveEvent, cx: &mut Context<Self>) {
226        if self.is_selecting {
227            let position = self.index_for_mouse_position(event.position);
228            self.select_to(position, cx);
229        }
230    }
231
232    pub fn handle_save(&mut self, _: &Save, _: &mut Window, cx: &mut Context<Self>) {
233        self.save(cx);
234    }
235
236    pub fn handle_cancel(&mut self, _: &Cancel, _: &mut Window, cx: &mut Context<Self>) {
237        self.cancel(cx);
238    }
239
240    pub fn handle_backspace(&mut self, window: &mut Window, cx: &mut Context<Self>) {
241        if self.selected_range.is_empty() {
242            self.select_to(self.previous_boundary(self.cursor_offset()), cx);
243        }
244        self.replace_text_in_range(None, "", window, cx);
245    }
246
247    pub fn handle_delete(&mut self, window: &mut Window, cx: &mut Context<Self>) {
248        if self.selected_range.is_empty() {
249            self.select_to(self.next_boundary(self.cursor_offset()), cx);
250        }
251        self.replace_text_in_range(None, "", window, cx);
252    }
253
254    pub fn handle_left(&mut self, cx: &mut Context<Self>) {
255        if self.selected_range.is_empty() {
256            self.move_to(self.previous_boundary(self.cursor_offset()), cx);
257        } else {
258            self.move_to(self.selected_range.start, cx);
259        }
260    }
261
262    pub fn handle_right(&mut self, cx: &mut Context<Self>) {
263        if self.selected_range.is_empty() {
264            self.move_to(self.next_boundary(self.selected_range.end), cx);
265        } else {
266            self.move_to(self.selected_range.end, cx);
267        }
268    }
269
270    pub fn handle_select_all(&mut self, cx: &mut Context<Self>) {
271        self.move_to(0, cx);
272        self.select_to(self.edit_value.len(), cx);
273    }
274
275    pub fn handle_home(&mut self, cx: &mut Context<Self>) {
276        self.move_to(0, cx);
277    }
278
279    pub fn handle_end(&mut self, cx: &mut Context<Self>) {
280        self.move_to(self.edit_value.len(), cx);
281    }
282
283    pub fn handle_copy(&self, cx: &mut Context<Self>) {
284        if !self.selected_range.is_empty() {
285            cx.write_to_clipboard(ClipboardItem::new_string(
286                self.edit_value[self.selected_range.clone()].to_string(),
287            ));
288        }
289    }
290
291    pub fn handle_cut(&mut self, window: &mut Window, cx: &mut Context<Self>) {
292        if !self.selected_range.is_empty() {
293            cx.write_to_clipboard(ClipboardItem::new_string(
294                self.edit_value[self.selected_range.clone()].to_string(),
295            ));
296            self.replace_text_in_range(None, "", window, cx);
297        }
298    }
299
300    pub fn handle_paste(&mut self, window: &mut Window, cx: &mut Context<Self>) {
301        if let Some(text) = cx.read_from_clipboard().and_then(|item| item.text()) {
302            let filtered = text.replace('\n', " ");
303            self.replace_text_in_range(None, &filtered, window, cx);
304        }
305    }
306}
307
308impl Focusable for InlineEditState {
309    fn focus_handle(&self, _: &App) -> FocusHandle {
310        self.focus_handle.clone()
311    }
312}
313
314impl EntityInputHandler for InlineEditState {
315    fn text_for_range(
316        &mut self,
317        range_utf16: Range<usize>,
318        actual_range: &mut Option<Range<usize>>,
319        _window: &mut Window,
320        _cx: &mut Context<Self>,
321    ) -> Option<String> {
322        let range = self.range_from_utf16(&range_utf16);
323        actual_range.replace(self.range_to_utf16(&range));
324        Some(self.edit_value[range].to_string())
325    }
326
327    fn selected_text_range(
328        &mut self,
329        _ignore_disabled_input: bool,
330        _window: &mut Window,
331        _cx: &mut Context<Self>,
332    ) -> Option<UTF16Selection> {
333        Some(UTF16Selection {
334            range: self.range_to_utf16(&self.selected_range),
335            reversed: self.selection_reversed,
336        })
337    }
338
339    fn marked_text_range(
340        &self,
341        _window: &mut Window,
342        _cx: &mut Context<Self>,
343    ) -> Option<Range<usize>> {
344        self.marked_range
345            .as_ref()
346            .map(|range| self.range_to_utf16(range))
347    }
348
349    fn unmark_text(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {
350        self.marked_range = None;
351        self.invalidate_layout_cache();
352    }
353
354    fn replace_text_in_range(
355        &mut self,
356        range_utf16: Option<Range<usize>>,
357        new_text: &str,
358        _window: &mut Window,
359        cx: &mut Context<Self>,
360    ) {
361        let range = range_utf16
362            .as_ref()
363            .map(|range_utf16| self.range_from_utf16(range_utf16))
364            .or(self.marked_range.clone())
365            .unwrap_or(self.selected_range.clone());
366
367        self.edit_value =
368            self.edit_value[0..range.start].to_owned() + new_text + &self.edit_value[range.end..];
369        self.selected_range = range.start + new_text.len()..range.start + new_text.len();
370        self.marked_range.take();
371        self.invalidate_layout_cache();
372        cx.notify();
373    }
374
375    fn replace_and_mark_text_in_range(
376        &mut self,
377        range_utf16: Option<Range<usize>>,
378        new_text: &str,
379        new_selected_range_utf16: Option<Range<usize>>,
380        _window: &mut Window,
381        cx: &mut Context<Self>,
382    ) {
383        let range = range_utf16
384            .as_ref()
385            .map(|range_utf16| self.range_from_utf16(range_utf16))
386            .or(self.marked_range.clone())
387            .unwrap_or(self.selected_range.clone());
388
389        self.edit_value =
390            self.edit_value[0..range.start].to_owned() + new_text + &self.edit_value[range.end..];
391        if !new_text.is_empty() {
392            self.marked_range = Some(range.start..range.start + new_text.len());
393        } else {
394            self.marked_range = None;
395        }
396        self.selected_range = new_selected_range_utf16
397            .as_ref()
398            .map(|range_utf16| text_util::range_from_utf16(new_text, range_utf16))
399            .map(|new_range| new_range.start + range.start..new_range.end + range.start)
400            .unwrap_or_else(|| range.start + new_text.len()..range.start + new_text.len());
401
402        self.invalidate_layout_cache();
403        cx.notify();
404    }
405
406    fn bounds_for_range(
407        &mut self,
408        range_utf16: Range<usize>,
409        bounds: Bounds<Pixels>,
410        _window: &mut Window,
411        _cx: &mut Context<Self>,
412    ) -> Option<Bounds<Pixels>> {
413        if self.edit_value.is_empty() {
414            return None;
415        }
416        let layout_matches = {
417            let last_layout = self.last_layout.as_ref()?;
418            self.layout_matches_content(last_layout)
419        };
420        if !layout_matches {
421            self.invalidate_layout_cache();
422            return None;
423        }
424
425        let last_layout = self.last_layout.as_ref()?;
426        let range = self.range_from_utf16(&range_utf16);
427        Some(Bounds::from_corners(
428            point(
429                bounds.left() + last_layout.x_for_index(range.start),
430                bounds.top(),
431            ),
432            point(
433                bounds.left() + last_layout.x_for_index(range.end),
434                bounds.bottom(),
435            ),
436        ))
437    }
438
439    fn character_index_for_point(
440        &mut self,
441        point: Point<Pixels>,
442        _window: &mut Window,
443        _cx: &mut Context<Self>,
444    ) -> Option<usize> {
445        if self.edit_value.is_empty() {
446            return None;
447        }
448        let line_point = self.last_bounds?.localize(&point)?;
449        let layout_matches = {
450            let last_layout = self.last_layout.as_ref()?;
451            self.layout_matches_content(last_layout)
452        };
453        if !layout_matches {
454            self.invalidate_layout_cache();
455            return None;
456        }
457
458        let last_layout = self.last_layout.as_ref()?;
459        let utf8_index = last_layout.index_for_x(point.x - line_point.x)?;
460        Some(self.offset_to_utf16(utf8_index))
461    }
462}
463
464impl Render for InlineEditState {
465    fn render(&mut self, _: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
466        div()
467    }
468}
469
470struct InlineEditTextElement {
471    state: Entity<InlineEditState>,
472    placeholder: SharedString,
473}
474
475struct TextElementPrepaintState {
476    line: Option<ShapedLine>,
477    cursor: Option<PaintQuad>,
478    selection: Option<PaintQuad>,
479}
480
481impl IntoElement for InlineEditTextElement {
482    type Element = Self;
483
484    fn into_element(self) -> Self::Element {
485        self
486    }
487}
488
489impl Element for InlineEditTextElement {
490    type RequestLayoutState = ();
491    type PrepaintState = TextElementPrepaintState;
492
493    fn id(&self) -> Option<ElementId> {
494        None
495    }
496
497    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
498        None
499    }
500
501    fn request_layout(
502        &mut self,
503        _id: Option<&GlobalElementId>,
504        _inspector_id: Option<&InspectorElementId>,
505        window: &mut Window,
506        cx: &mut App,
507    ) -> (LayoutId, Self::RequestLayoutState) {
508        let mut style = Style::default();
509        style.size.width = relative(1.).into();
510        style.size.height = window.line_height().into();
511        (window.request_layout(style, [], cx), ())
512    }
513
514    fn prepaint(
515        &mut self,
516        _id: Option<&GlobalElementId>,
517        _inspector_id: Option<&InspectorElementId>,
518        bounds: Bounds<Pixels>,
519        _request_layout: &mut Self::RequestLayoutState,
520        window: &mut Window,
521        cx: &mut App,
522    ) -> Self::PrepaintState {
523        let state = self.state.read(cx);
524        let content = &state.edit_value;
525        let selected_range = state.selected_range.clone();
526        let cursor = state.cursor_offset();
527        let text_style = window.text_style();
528        let theme = use_theme(cx);
529
530        let (display_text, text_color) = if content.is_empty() {
531            (self.placeholder.clone(), theme.tokens.muted_foreground)
532        } else {
533            (content.clone().into(), text_style.color)
534        };
535
536        let run = TextRun {
537            len: display_text.len(),
538            font: text_style.font(),
539            color: text_color,
540            background_color: None,
541            underline: None,
542            strikethrough: None,
543        };
544
545        let runs = if let Some(marked_range) = state.marked_range.as_ref() {
546            vec![
547                TextRun {
548                    len: marked_range.start,
549                    ..run.clone()
550                },
551                TextRun {
552                    len: marked_range.end - marked_range.start,
553                    underline: Some(UnderlineStyle {
554                        color: Some(run.color),
555                        thickness: px(1.0),
556                        wavy: false,
557                    }),
558                    ..run.clone()
559                },
560                TextRun {
561                    len: display_text.len().saturating_sub(marked_range.end),
562                    ..run
563                },
564            ]
565            .into_iter()
566            .filter(|run| run.len > 0)
567            .collect()
568        } else {
569            vec![run]
570        };
571
572        let font_size = text_style.font_size.to_pixels(window.rem_size());
573        let line = window
574            .text_system()
575            .shape_line(display_text, font_size, &runs, None);
576
577        let cursor_pos = line.x_for_index(cursor);
578        let (selection, cursor_quad) = if selected_range.is_empty() {
579            (
580                None,
581                Some(fill(
582                    Bounds::new(
583                        point(bounds.left() + cursor_pos, bounds.top()),
584                        size(px(2.), bounds.bottom() - bounds.top()),
585                    ),
586                    theme.tokens.primary,
587                )),
588            )
589        } else {
590            (
591                Some(fill(
592                    Bounds::from_corners(
593                        point(
594                            bounds.left() + line.x_for_index(selected_range.start),
595                            bounds.top(),
596                        ),
597                        point(
598                            bounds.left() + line.x_for_index(selected_range.end),
599                            bounds.bottom(),
600                        ),
601                    ),
602                    theme.tokens.primary.opacity(0.2),
603                )),
604                None,
605            )
606        };
607
608        TextElementPrepaintState {
609            line: Some(line),
610            cursor: cursor_quad,
611            selection,
612        }
613    }
614
615    fn paint(
616        &mut self,
617        _id: Option<&GlobalElementId>,
618        _inspector_id: Option<&InspectorElementId>,
619        bounds: Bounds<Pixels>,
620        _request_layout: &mut Self::RequestLayoutState,
621        prepaint: &mut Self::PrepaintState,
622        window: &mut Window,
623        cx: &mut App,
624    ) {
625        let focus_handle = self.state.read(cx).focus_handle.clone();
626
627        window.handle_input(
628            &focus_handle,
629            ElementInputHandler::new(bounds, self.state.clone()),
630            cx,
631        );
632
633        if let Some(selection) = prepaint.selection.take() {
634            window.paint_quad(selection);
635        }
636
637        let Some(line) = prepaint.line.take() else {
638            return;
639        };
640
641        let _ = line.paint(bounds.origin, window.line_height(), window, cx);
642
643        if focus_handle.is_focused(window) {
644            if let Some(cursor) = prepaint.cursor.take() {
645                window.paint_quad(cursor);
646            }
647        }
648
649        self.state.update(cx, |state, _| {
650            state.last_layout = Some(line);
651            state.last_bounds = Some(bounds);
652        });
653    }
654}
655
656#[derive(IntoElement)]
657pub struct InlineEdit {
658    state: Entity<InlineEditState>,
659    placeholder: SharedString,
660    disabled: bool,
661    trigger: InlineEditTrigger,
662    on_save: Option<Rc<dyn Fn(&str, &mut Window, &mut App)>>,
663    on_cancel: Option<Rc<dyn Fn(&mut Window, &mut App)>>,
664    style: StyleRefinement,
665}
666
667impl InlineEdit {
668    pub fn new(state: Entity<InlineEditState>) -> Self {
669        Self {
670            state,
671            placeholder: "Click to edit".into(),
672            disabled: false,
673            trigger: InlineEditTrigger::Click,
674            on_save: None,
675            on_cancel: None,
676            style: StyleRefinement::default(),
677        }
678    }
679
680    pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
681        self.placeholder = placeholder.into();
682        self
683    }
684
685    pub fn disabled(mut self, disabled: bool) -> Self {
686        self.disabled = disabled;
687        self
688    }
689
690    pub fn trigger(mut self, trigger: InlineEditTrigger) -> Self {
691        self.trigger = trigger;
692        self
693    }
694
695    pub fn on_save<F>(mut self, handler: F) -> Self
696    where
697        F: Fn(&str, &mut Window, &mut App) + 'static,
698    {
699        self.on_save = Some(Rc::new(handler));
700        self
701    }
702
703    pub fn on_cancel<F>(mut self, handler: F) -> Self
704    where
705        F: Fn(&mut Window, &mut App) + 'static,
706    {
707        self.on_cancel = Some(Rc::new(handler));
708        self
709    }
710}
711
712impl Styled for InlineEdit {
713    fn style(&mut self) -> &mut StyleRefinement {
714        &mut self.style
715    }
716}
717
718impl RenderOnce for InlineEdit {
719    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
720        let theme = use_theme(cx);
721        let user_style = self.style;
722        let is_editing = self.state.read(cx).is_editing();
723        let value = self.state.read(cx).value().to_string();
724        let focus_handle = self.state.read(cx).focus_handle(cx);
725        let state = self.state.clone();
726        let state_for_click = self.state.clone();
727        let state_for_double_click = self.state.clone();
728        let state_for_mouse_down = self.state.clone();
729        let state_for_mouse_up = self.state.clone();
730        let state_for_mouse_move = self.state.clone();
731        let on_save = self.on_save.clone();
732        let on_cancel = self.on_cancel.clone();
733        let trigger = self.trigger;
734        let placeholder = self.placeholder.clone();
735
736        if is_editing {
737            div()
738                .id(("inline-edit", state.entity_id()))
739                .key_context("InlineEdit")
740                .track_focus(&focus_handle)
741                .h(px(32.0))
742                .px(px(8.0))
743                .py(px(4.0))
744                .bg(theme.tokens.background)
745                .border_1()
746                .border_color(theme.tokens.ring)
747                .rounded(theme.tokens.radius_md)
748                .shadow(smallvec::smallvec![theme.tokens.focus_ring_light()])
749                .text_size(px(14.0))
750                .font_family(theme.tokens.font_family.clone())
751                .text_color(theme.tokens.foreground)
752                .on_action({
753                    let state = state.clone();
754                    let on_save = on_save.clone();
755                    move |_: &Save, window: &mut Window, cx: &mut App| {
756                        let value = state.read(cx).edit_value.clone();
757                        state.update(cx, |s, cx| s.save(cx));
758                        if let Some(handler) = on_save.as_ref() {
759                            handler(&value, window, cx);
760                        }
761                    }
762                })
763                .on_action({
764                    let state = state.clone();
765                    let on_cancel = on_cancel.clone();
766                    move |_: &Cancel, window: &mut Window, cx: &mut App| {
767                        state.update(cx, |s, cx| s.cancel(cx));
768                        if let Some(handler) = on_cancel.as_ref() {
769                            handler(window, cx);
770                        }
771                    }
772                })
773                .on_key_down({
774                    let state = state.clone();
775                    move |event, window, cx| {
776                        let key = event.keystroke.key.as_str();
777                        match key {
778                            "backspace" => {
779                                state.update(cx, |s, cx| s.handle_backspace(window, cx));
780                                cx.stop_propagation();
781                            }
782                            "delete" => {
783                                state.update(cx, |s, cx| s.handle_delete(window, cx));
784                                cx.stop_propagation();
785                            }
786                            "left" => {
787                                state.update(cx, |s, cx| s.handle_left(cx));
788                                cx.stop_propagation();
789                            }
790                            "right" => {
791                                state.update(cx, |s, cx| s.handle_right(cx));
792                                cx.stop_propagation();
793                            }
794                            "home" => {
795                                state.update(cx, |s, cx| s.handle_home(cx));
796                                cx.stop_propagation();
797                            }
798                            "end" => {
799                                state.update(cx, |s, cx| s.handle_end(cx));
800                                cx.stop_propagation();
801                            }
802                            "a" if event.keystroke.modifiers.platform => {
803                                state.update(cx, |s, cx| s.handle_select_all(cx));
804                                cx.stop_propagation();
805                            }
806                            "c" if event.keystroke.modifiers.platform => {
807                                state.update(cx, |s, cx| s.handle_copy(cx));
808                                cx.stop_propagation();
809                            }
810                            "x" if event.keystroke.modifiers.platform => {
811                                state.update(cx, |s, cx| s.handle_cut(window, cx));
812                                cx.stop_propagation();
813                            }
814                            "v" if event.keystroke.modifiers.platform => {
815                                state.update(cx, |s, cx| s.handle_paste(window, cx));
816                                cx.stop_propagation();
817                            }
818                            _ => {}
819                        }
820                    }
821                })
822                .on_mouse_down(MouseButton::Left, {
823                    move |event: &MouseDownEvent, _, cx| {
824                        state_for_mouse_down.update(cx, |s, cx| s.on_mouse_down(event, cx));
825                    }
826                })
827                .on_mouse_up(MouseButton::Left, {
828                    move |event: &MouseUpEvent, _, cx| {
829                        state_for_mouse_up.update(cx, |s, cx| s.on_mouse_up(event, cx));
830                    }
831                })
832                .on_mouse_move({
833                    move |event: &MouseMoveEvent, _, cx| {
834                        state_for_mouse_move.update(cx, |s, cx| s.on_mouse_move(event, cx));
835                    }
836                })
837                .child(
838                    div()
839                        .size_full()
840                        .flex()
841                        .items_center()
842                        .child(InlineEditTextElement {
843                            state: state.clone(),
844                            placeholder,
845                        }),
846                )
847                .map(|this| {
848                    let mut div = this;
849                    div.style().refine(&user_style);
850                    div
851                })
852        } else {
853            let display_text = if value.is_empty() {
854                self.placeholder.clone()
855            } else {
856                value.clone().into()
857            };
858
859            let text_color = if value.is_empty() {
860                theme.tokens.muted_foreground
861            } else {
862                theme.tokens.foreground
863            };
864
865            div()
866                .id(("inline-edit-display", state.entity_id()))
867                .h(px(32.0))
868                .px(px(8.0))
869                .py(px(4.0))
870                .flex()
871                .items_center()
872                .text_size(px(14.0))
873                .font_family(theme.tokens.font_family.clone())
874                .text_color(text_color)
875                .rounded(theme.tokens.radius_md)
876                .when(!self.disabled, |d| {
877                    d.cursor_pointer()
878                        .hover(|s| {
879                            s.bg(theme.tokens.muted.opacity(0.5))
880                                .border_b_1()
881                                .border_color(theme.tokens.primary.opacity(0.5))
882                        })
883                        .when(trigger == InlineEditTrigger::Click, |d| {
884                            d.on_click({
885                                move |_, window, cx| {
886                                    state_for_click.update(cx, |s, cx| s.start_editing(cx));
887                                    window.focus(&state_for_click.read(cx).focus_handle(cx));
888                                }
889                            })
890                        })
891                        .when(trigger == InlineEditTrigger::DoubleClick, |d| {
892                            d.on_mouse_down(MouseButton::Left, {
893                                move |event: &MouseDownEvent, window: &mut Window, cx: &mut App| {
894                                    if event.click_count >= 2 {
895                                        state_for_double_click
896                                            .update(cx, |s, cx| s.start_editing(cx));
897                                        window.focus(
898                                            &state_for_double_click.read(cx).focus_handle(cx),
899                                        );
900                                    }
901                                }
902                            })
903                        })
904                })
905                .when(self.disabled, |d| d.opacity(0.5))
906                .child(display_text)
907                .map(|this| {
908                    let mut div = this;
909                    div.style().refine(&user_style);
910                    div
911                })
912        }
913    }
914}