|
| 1 | +use editor::Editor; |
| 2 | +use gpui::div; |
| 3 | +use ui::{ |
| 4 | + ActiveTheme as _, App, FluentBuilder as _, InteractiveElement as _, IntoElement, |
| 5 | + ParentElement as _, RenderOnce, Styled as _, Window, |
| 6 | +}; |
| 7 | + |
| 8 | +#[derive(IntoElement)] |
| 9 | +pub struct SettingsEditor { |
| 10 | + initial_text: Option<String>, |
| 11 | + placeholder: Option<&'static str>, |
| 12 | + confirm: Option<Box<dyn Fn(Option<String>, &mut App)>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl SettingsEditor { |
| 16 | + pub fn new() -> Self { |
| 17 | + Self { |
| 18 | + initial_text: None, |
| 19 | + placeholder: None, |
| 20 | + confirm: None, |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn with_initial_text(mut self, initial_text: String) -> Self { |
| 25 | + self.initial_text = Some(initial_text); |
| 26 | + self |
| 27 | + } |
| 28 | + |
| 29 | + pub fn with_placeholder(mut self, placeholder: &'static str) -> Self { |
| 30 | + self.placeholder = Some(placeholder); |
| 31 | + self |
| 32 | + } |
| 33 | + |
| 34 | + pub fn on_confirm(mut self, confirm: impl Fn(Option<String>, &mut App) + 'static) -> Self { |
| 35 | + self.confirm = Some(Box::new(confirm)); |
| 36 | + self |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl RenderOnce for SettingsEditor { |
| 41 | + fn render(self, window: &mut Window, cx: &mut App) -> impl ui::IntoElement { |
| 42 | + let editor = window.use_state(cx, { |
| 43 | + move |window, cx| { |
| 44 | + let mut editor = Editor::single_line(window, cx); |
| 45 | + if let Some(text) = self.initial_text { |
| 46 | + editor.set_text(text, window, cx); |
| 47 | + } |
| 48 | + |
| 49 | + if let Some(placeholder) = self.placeholder { |
| 50 | + editor.set_placeholder_text(placeholder, window, cx); |
| 51 | + } |
| 52 | + editor |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + let weak_editor = editor.downgrade(); |
| 57 | + let theme_colors = cx.theme().colors(); |
| 58 | + |
| 59 | + div() |
| 60 | + .child(editor) |
| 61 | + .bg(theme_colors.editor_background) |
| 62 | + .border_1() |
| 63 | + .rounded_lg() |
| 64 | + .border_color(theme_colors.border) |
| 65 | + .when_some(self.confirm, |this, confirm| { |
| 66 | + this.on_action::<menu::Confirm>({ |
| 67 | + move |_, _, cx| { |
| 68 | + let Some(editor) = weak_editor.upgrade() else { |
| 69 | + return; |
| 70 | + }; |
| 71 | + let new_value = editor.read_with(cx, |editor, cx| editor.text(cx)); |
| 72 | + let new_value = (!new_value.is_empty()).then_some(new_value); |
| 73 | + confirm(new_value, cx); |
| 74 | + } |
| 75 | + }) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments