BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / section_dialog.rs

section_dialog.rs

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