| 1 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 2 | use gpui_kit::component::dialog::{Dialog, DialogFooter}; |
| 3 | use gpui_kit::component::input::{Input, InputEvent, InputState}; |
| 4 | use gpui_kit::component::{WindowExt as _, v_flex}; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use crate::app_state::AppState; |
| 8 | |
| 9 | const DIALOG_WIDTH: f32 = 380.; |
| 10 | |
| 11 | #[derive(Clone)] |
| 12 | pub enum SectionNameTarget { |
| 13 | New, |
| 14 | Rename { folder_id: String, name: String }, |
| 15 | } |
| 16 | |
| 17 | impl SectionNameTarget { |
| 18 | fn title(&self) -> &'static str { |
| 19 | match self { |
| 20 | SectionNameTarget::New => "New section", |
| 21 | SectionNameTarget::Rename { .. } => "Rename section", |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn confirm_label(&self) -> &'static str { |
| 26 | match self { |
| 27 | SectionNameTarget::New => "Create", |
| 28 | SectionNameTarget::Rename { .. } => "Rename", |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | pub fn clean_section_name(raw: &str) -> Option<String> { |
| 34 | let name = raw.trim(); |
| 35 | (!name.is_empty()).then(|| name.to_owned()) |
| 36 | } |
| 37 | |
| 38 | struct SectionNameForm { |
| 39 | state: Entity<AppState>, |
| 40 | target: SectionNameTarget, |
| 41 | name: Entity<InputState>, |
| 42 | _subscription: Subscription, |
| 43 | } |
| 44 | |
| 45 | impl SectionNameForm { |
| 46 | fn new( |
| 47 | state: Entity<AppState>, |
| 48 | target: SectionNameTarget, |
| 49 | window: &mut Window, |
| 50 | cx: &mut Context<Self>, |
| 51 | ) -> Self { |
| 52 | let current = match &target { |
| 53 | SectionNameTarget::New => String::new(), |
| 54 | SectionNameTarget::Rename { name, .. } => name.clone(), |
| 55 | }; |
| 56 | let name = cx.new(|cx| { |
| 57 | InputState::new(window, cx) |
| 58 | .placeholder("Section name") |
| 59 | .default_value(current) |
| 60 | }); |
| 61 | let subscription = cx.subscribe_in(&name, window, Self::on_enter); |
| 62 | SectionNameForm { |
| 63 | state, |
| 64 | target, |
| 65 | name, |
| 66 | _subscription: subscription, |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn on_enter( |
| 71 | &mut self, |
| 72 | _: &Entity<InputState>, |
| 73 | event: &InputEvent, |
| 74 | window: &mut Window, |
| 75 | cx: &mut Context<Self>, |
| 76 | ) { |
| 77 | if matches!(event, InputEvent::PressEnter { .. }) && self.submit(cx) { |
| 78 | window.close_dialog(cx); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn submit(&mut self, cx: &mut Context<Self>) -> bool { |
| 83 | let Some(name) = clean_section_name(&self.name.read(cx).value()) else { |
| 84 | return false; |
| 85 | }; |
| 86 | let target = self.target.clone(); |
| 87 | self.state.update(cx, |state, cx| match target { |
| 88 | SectionNameTarget::New => state.create_section(&name, cx), |
| 89 | SectionNameTarget::Rename { folder_id, .. } => { |
| 90 | state.rename_section(&folder_id, &name, cx) |
| 91 | } |
| 92 | }); |
| 93 | true |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl Render for SectionNameForm { |
| 98 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 99 | v_flex().child(Input::new(&self.name)) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | pub fn open_section_name_dialog( |
| 104 | state: Entity<AppState>, |
| 105 | target: SectionNameTarget, |
| 106 | window: &mut Window, |
| 107 | cx: &mut App, |
| 108 | ) { |
| 109 | let (title, confirm_label) = (target.title(), target.confirm_label()); |
| 110 | let form = cx.new(|cx| SectionNameForm::new(state, target, window, cx)); |
| 111 | let field = form.read(cx).name.clone(); |
| 112 | window.open_dialog(cx, move |dialog: Dialog, _, _| { |
| 113 | let submit_form = form.clone(); |
| 114 | let footer = DialogFooter::new() |
| 115 | .child( |
| 116 | Button::new("section-name-cancel") |
| 117 | .label("Cancel") |
| 118 | .on_click(|_, window, cx| window.close_dialog(cx)), |
| 119 | ) |
| 120 | .child( |
| 121 | Button::new("section-name-confirm") |
| 122 | .primary() |
| 123 | .label(confirm_label) |
| 124 | .on_click(move |_, window, cx| { |
| 125 | if submit_form.update(cx, |form, cx| form.submit(cx)) { |
| 126 | window.close_dialog(cx); |
| 127 | } |
| 128 | }), |
| 129 | ); |
| 130 | dialog |
| 131 | .title(title) |
| 132 | .w(px(DIALOG_WIDTH)) |
| 133 | .child(form.clone()) |
| 134 | .footer(footer) |
| 135 | }); |
| 136 | window.defer(cx, move |window, cx| { |
| 137 | field.update(cx, |input, cx| { |
| 138 | input.focus(window, cx); |
| 139 | input.select_all(window, cx); |
| 140 | }); |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | #[cfg(test)] |
| 145 | mod tests { |
| 146 | use super::clean_section_name; |
| 147 | |
| 148 | #[test] |
| 149 | fn section_names_are_trimmed_and_never_empty() { |
| 150 | assert_eq!(clean_section_name(" Todo "), Some("Todo".to_owned())); |
| 151 | assert_eq!(clean_section_name(" "), None); |
| 152 | } |
| 153 | } |