BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / settings / caldav_form.rs

caldav_form.rs

caldav_form.rs 9.7 KiB Download Raw
1use gpui_kit::component::input::{Input, InputContentType, InputState};
2use gpui_kit::component::{h_flex, v_flex};
3use gpui_kit::*;
4
5use super::rows::{control_row, stacked_field, text_button};
6use crate::app_state::AppState;
7use crate::i18n::tr;
8
9pub struct CalDavForm {
10 app: Entity<AppState>,
11 url: Entity<InputState>,
12 username: Entity<InputState>,
13 password: Entity<InputState>,
14 show_form: bool,
15}
16
17impl CalDavForm {
18 pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
19 let password = cx.new(|cx| InputState::new(window, cx));
20 password.update(cx, |input, cx| input.toggle_masked(window, cx));
21 CalDavForm {
22 app,
23 url: cx.new(|cx| {
24 InputState::new(window, cx).placeholder("https://cloud.example/remote.php/dav/")
25 }),
26 username: cx.new(|cx| InputState::new(window, cx)),
27 password,
28 show_form: false,
29 }
30 }
31}
32
33impl Render for CalDavForm {
34 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
35 if self.show_form {
36 let connect = self.app.clone();
37 let url_input = self.url.clone();
38 let user_input = self.username.clone();
39 let pass_input = self.password.clone();
40 let set_nextcloud = self.url.clone();
41 let set_fastmail = self.url.clone();
42 let set_icloud = self.url.clone();
43 let hide = cx.entity().clone();
44 v_flex()
45 .w_full()
46 .gap(px(8.))
47 .child(control_row(
48 tr("settings.calendar.caldav_title"),
49 tr("settings.calendar.caldav_hint"),
50 h_flex()
51 .gap_2()
52 .child(
53 text_button(
54 "settings-caldav-nextcloud",
55 tr("settings.calendar.caldav_template_nextcloud"),
56 )
57 .on_click(move |_, window, cx| {
58 set_nextcloud.update(cx, |input, cx| {
59 input.set_value(
60 "https://cloud.example/remote.php/dav/",
61 window,
62 cx,
63 );
64 });
65 }),
66 )
67 .child(
68 text_button(
69 "settings-caldav-fastmail",
70 tr("settings.calendar.caldav_template_fastmail"),
71 )
72 .on_click(move |_, window, cx| {
73 set_fastmail.update(cx, |input, cx| {
74 input.set_value("https://caldav.fastmail.com/dav/", window, cx);
75 });
76 }),
77 )
78 .child(
79 text_button(
80 "settings-caldav-icloud",
81 tr("settings.calendar.caldav_template_icloud"),
82 )
83 .on_click(move |_, window, cx| {
84 set_icloud.update(cx, |input, cx| {
85 input.set_value("https://caldav.icloud.com/", window, cx);
86 });
87 }),
88 ),
89 ))
90 .child(stacked_field(
91 tr("settings.calendar.caldav_url"),
92 "",
93 Input::new(&self.url),
94 ))
95 .child(stacked_field(
96 tr("settings.calendar.caldav_username"),
97 "",
98 Input::new(&self.username),
99 ))
100 .child(stacked_field(
101 tr("settings.calendar.caldav_password"),
102 "",
103 Input::new(&self.password)
104 .content_type(InputContentType::Password)
105 .mask_toggle(),
106 ))
107 .child(control_row(
108 tr("settings.calendar.caldav_title"),
109 "",
110 h_flex()
111 .gap_2()
112 .child(
113 text_button(
114 "settings-caldav-connect",
115 tr("settings.calendar.caldav_connect"),
116 )
117 .on_click(move |_, _, cx| {
118 let url = url_input.read(cx).value().to_string();
119 let username = user_input.read(cx).value().to_string();
120 let password = pass_input.read(cx).value().to_string();
121 connect.update(cx, |state, cx| {
122 state.connect_caldav_account(url, username, password, cx)
123 })
124 }),
125 )
126 .child(
127 text_button("settings-caldav-cancel", tr("common.cancel")).on_click(
128 move |_, _, cx| {
129 hide.update(cx, |form, cx| {
130 form.show_form = false;
131 cx.notify();
132 });
133 },
134 ),
135 ),
136 ))
137 } else {
138 let show = cx.entity().clone();
139 control_row(
140 tr("settings.calendar.caldav_title"),
141 tr("settings.calendar.caldav_hint"),
142 text_button(
143 "settings-caldav-show",
144 tr("settings.calendar.caldav_connect"),
145 )
146 .on_click(move |_, _, cx| {
147 show.update(cx, |form, cx| {
148 form.show_form = true;
149 cx.notify();
150 });
151 }),
152 )
153 }
154 }
155}
156
157pub struct IcsForm {
158 app: Entity<AppState>,
159 url: Entity<InputState>,
160 name: Entity<InputState>,
161 show_form: bool,
162}
163
164impl IcsForm {
165 pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
166 IcsForm {
167 app,
168 url: cx.new(|cx| {
169 InputState::new(window, cx).placeholder("https://example.com/calendar.ics")
170 }),
171 name: cx.new(|cx| InputState::new(window, cx).placeholder("Holidays")),
172 show_form: false,
173 }
174 }
175}
176
177impl Render for IcsForm {
178 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
179 if self.show_form {
180 let ics_connect = self.app.clone();
181 let url_input = self.url.clone();
182 let name_input = self.name.clone();
183 let hide = cx.entity().clone();
184 v_flex()
185 .w_full()
186 .gap(px(8.))
187 .child(control_row(
188 tr("settings.calendar.ics_title"),
189 tr("settings.calendar.ics_hint"),
190 div(),
191 ))
192 .child(stacked_field(
193 tr("settings.calendar.ics_url"),
194 "",
195 Input::new(&self.url),
196 ))
197 .child(stacked_field(
198 tr("settings.calendar.ics_name"),
199 "",
200 Input::new(&self.name),
201 ))
202 .child(control_row(
203 tr("settings.calendar.ics_title"),
204 "",
205 h_flex()
206 .gap_2()
207 .child(
208 text_button(
209 "settings-ics-connect",
210 tr("settings.calendar.ics_connect"),
211 )
212 .on_click(move |_, _, cx| {
213 let url = url_input.read(cx).value().to_string();
214 let name = name_input.read(cx).value().to_string();
215 ics_connect.update(cx, |state, cx| {
216 state.connect_ics_subscription(url, name, cx)
217 })
218 }),
219 )
220 .child(
221 text_button("settings-ics-cancel", tr("common.cancel")).on_click(
222 move |_, _, cx| {
223 hide.update(cx, |form, cx| {
224 form.show_form = false;
225 cx.notify();
226 });
227 },
228 ),
229 ),
230 ))
231 } else {
232 let show = cx.entity().clone();
233 control_row(
234 tr("settings.calendar.ics_title"),
235 tr("settings.calendar.ics_hint"),
236 text_button("settings-ics-show", tr("settings.calendar.ics_connect")).on_click(
237 move |_, _, cx| {
238 show.update(cx, |form, cx| {
239 form.show_form = true;
240 cx.notify();
241 });
242 },
243 ),
244 )
245 }
246 }
247}