welcome.rs

  1mod base_keymap_picker;
  2mod base_keymap_setting;
  3
  4use client::{telemetry::Telemetry, TelemetrySettings};
  5use db::kvp::KEY_VALUE_STORE;
  6use gpui::{
  7    svg, AnyElement, AppContext, EventEmitter, FocusHandle, FocusableView, InteractiveElement,
  8    ParentElement, Render, Styled, Subscription, View, ViewContext, VisualContext, WeakView,
  9    WindowContext,
 10};
 11use settings::{Settings, SettingsStore};
 12use std::sync::Arc;
 13use ui::{prelude::*, Checkbox};
 14use vim::VimModeSetting;
 15use workspace::{
 16    dock::DockPosition,
 17    item::{Item, ItemEvent},
 18    open_new, AppState, Welcome, Workspace, WorkspaceId,
 19};
 20
 21pub use base_keymap_setting::BaseKeymap;
 22
 23pub const FIRST_OPEN: &str = "first_open";
 24
 25pub fn init(cx: &mut AppContext) {
 26    BaseKeymap::register(cx);
 27
 28    cx.observe_new_views(|workspace: &mut Workspace, _cx| {
 29        workspace.register_action(|workspace, _: &Welcome, cx| {
 30            let welcome_page = WelcomePage::new(workspace, cx);
 31            workspace.add_item(Box::new(welcome_page), cx)
 32        });
 33    })
 34    .detach();
 35
 36    base_keymap_picker::init(cx);
 37}
 38
 39pub fn show_welcome_view(app_state: &Arc<AppState>, cx: &mut AppContext) {
 40    open_new(&app_state, cx, |workspace, cx| {
 41        workspace.toggle_dock(DockPosition::Left, cx);
 42        let welcome_page = WelcomePage::new(workspace, cx);
 43        workspace.add_item_to_center(Box::new(welcome_page.clone()), cx);
 44        cx.focus_view(&welcome_page);
 45        cx.notify();
 46    })
 47    .detach();
 48
 49    db::write_and_log(cx, || {
 50        KEY_VALUE_STORE.write_kvp(FIRST_OPEN.to_string(), "false".to_string())
 51    });
 52}
 53
 54pub struct WelcomePage {
 55    workspace: WeakView<Workspace>,
 56    focus_handle: FocusHandle,
 57    telemetry: Arc<Telemetry>,
 58    _settings_subscription: Subscription,
 59}
 60
 61impl Render for WelcomePage {
 62    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
 63        h_flex()
 64            .full()
 65            .bg(cx.theme().colors().editor_background)
 66            .track_focus(&self.focus_handle)
 67            .child(
 68                v_flex()
 69                    .w_96()
 70                    .gap_4()
 71                    .mx_auto()
 72                    .child(
 73                        svg()
 74                            .path("icons/logo_96.svg")
 75                            .text_color(gpui::white())
 76                            .w(px(96.))
 77                            .h(px(96.))
 78                            .mx_auto(),
 79                    )
 80                    .child(
 81                        h_flex()
 82                            .justify_center()
 83                            .child(Label::new("Code at the speed of thought")),
 84                    )
 85                    .child(
 86                        v_flex()
 87                            .gap_2()
 88                            .child(
 89                                Button::new("choose-theme", "Choose a theme")
 90                                    .full_width()
 91                                    .on_click(cx.listener(|this, _, cx| {
 92                                        this.telemetry.report_app_event(
 93                                            "welcome page: change theme".to_string(),
 94                                        );
 95                                        this.workspace
 96                                            .update(cx, |workspace, cx| {
 97                                                theme_selector::toggle(
 98                                                    workspace,
 99                                                    &Default::default(),
100                                                    cx,
101                                                )
102                                            })
103                                            .ok();
104                                    })),
105                            )
106                            .child(
107                                Button::new("choose-keymap", "Choose a keymap")
108                                    .full_width()
109                                    .on_click(cx.listener(|this, _, cx| {
110                                        this.telemetry.report_app_event(
111                                            "welcome page: change keymap".to_string(),
112                                        );
113                                        this.workspace
114                                            .update(cx, |workspace, cx| {
115                                                base_keymap_picker::toggle(
116                                                    workspace,
117                                                    &Default::default(),
118                                                    cx,
119                                                )
120                                            })
121                                            .ok();
122                                    })),
123                            )
124                            .child(
125                                Button::new("install-cli", "Install the CLI")
126                                    .full_width()
127                                    .on_click(cx.listener(|this, _, cx| {
128                                        this.telemetry.report_app_event(
129                                            "welcome page: install cli".to_string(),
130                                        );
131                                        cx.app_mut()
132                                            .spawn(|cx| async move {
133                                                install_cli::install_cli(&cx).await
134                                            })
135                                            .detach_and_log_err(cx);
136                                    })),
137                            ),
138                    )
139                    .child(
140                        v_flex()
141                            .p_3()
142                            .gap_2()
143                            .bg(cx.theme().colors().elevated_surface_background)
144                            .border_1()
145                            .border_color(cx.theme().colors().border)
146                            .rounded_md()
147                            .child(
148                                h_flex()
149                                    .gap_2()
150                                    .child(
151                                        Checkbox::new(
152                                            "enable-vim",
153                                            if VimModeSetting::get_global(cx).0 {
154                                                ui::Selection::Selected
155                                            } else {
156                                                ui::Selection::Unselected
157                                            },
158                                        )
159                                        .on_click(
160                                            cx.listener(move |this, selection, cx| {
161                                                this.telemetry.report_app_event(
162                                                    "welcome page: toggle vim".to_string(),
163                                                );
164                                                this.update_settings::<VimModeSetting>(
165                                                    selection,
166                                                    cx,
167                                                    |setting, value| *setting = Some(value),
168                                                );
169                                            }),
170                                        ),
171                                    )
172                                    .child(Label::new("Enable vim mode")),
173                            )
174                            .child(
175                                h_flex()
176                                    .gap_2()
177                                    .child(
178                                        Checkbox::new(
179                                            "enable-telemetry",
180                                            if TelemetrySettings::get_global(cx).metrics {
181                                                ui::Selection::Selected
182                                            } else {
183                                                ui::Selection::Unselected
184                                            },
185                                        )
186                                        .on_click(
187                                            cx.listener(move |this, selection, cx| {
188                                                this.telemetry.report_app_event(
189                                                    "welcome page: toggle metric telemetry"
190                                                        .to_string(),
191                                                );
192                                                this.update_settings::<TelemetrySettings>(
193                                                    selection,
194                                                    cx,
195                                                    {
196                                                        let telemetry = this.telemetry.clone();
197
198                                                        move |settings, value| {
199                                                            settings.metrics = Some(value);
200
201                                                            telemetry.report_setting_event(
202                                                                "metric telemetry",
203                                                                value.to_string(),
204                                                            );
205                                                        }
206                                                    },
207                                                );
208                                            }),
209                                        ),
210                                    )
211                                    .child(Label::new("Send anonymous usage data")),
212                            )
213                            .child(
214                                h_flex()
215                                    .gap_2()
216                                    .child(
217                                        Checkbox::new(
218                                            "enable-crash",
219                                            if TelemetrySettings::get_global(cx).diagnostics {
220                                                ui::Selection::Selected
221                                            } else {
222                                                ui::Selection::Unselected
223                                            },
224                                        )
225                                        .on_click(
226                                            cx.listener(move |this, selection, cx| {
227                                                this.telemetry.report_app_event(
228                                                    "welcome page: toggle diagnostic telemetry"
229                                                        .to_string(),
230                                                );
231                                                this.update_settings::<TelemetrySettings>(
232                                                    selection,
233                                                    cx,
234                                                    {
235                                                        let telemetry = this.telemetry.clone();
236
237                                                        move |settings, value| {
238                                                            settings.diagnostics = Some(value);
239
240                                                            telemetry.report_setting_event(
241                                                                "diagnostic telemetry",
242                                                                value.to_string(),
243                                                            );
244                                                        }
245                                                    },
246                                                );
247                                            }),
248                                        ),
249                                    )
250                                    .child(Label::new("Send crash reports")),
251                            ),
252                    ),
253            )
254    }
255}
256
257impl WelcomePage {
258    pub fn new(workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
259        let this = cx.new_view(|cx| {
260            cx.on_release(|this: &mut Self, _, _| {
261                this.telemetry
262                    .report_app_event("welcome page: close".to_string());
263            })
264            .detach();
265
266            WelcomePage {
267                focus_handle: cx.focus_handle(),
268                workspace: workspace.weak_handle(),
269                telemetry: workspace.client().telemetry().clone(),
270                _settings_subscription: cx
271                    .observe_global::<SettingsStore>(move |_, cx| cx.notify()),
272            }
273        });
274
275        this
276    }
277
278    fn update_settings<T: Settings>(
279        &mut self,
280        selection: &Selection,
281        cx: &mut ViewContext<Self>,
282        callback: impl 'static + Send + Fn(&mut T::FileContent, bool),
283    ) {
284        if let Some(workspace) = self.workspace.upgrade() {
285            let fs = workspace.read(cx).app_state().fs.clone();
286            let selection = *selection;
287            settings::update_settings_file::<T>(fs, cx, move |settings| {
288                let value = match selection {
289                    Selection::Unselected => false,
290                    Selection::Selected => true,
291                    _ => return,
292                };
293
294                callback(settings, value)
295            });
296        }
297    }
298}
299
300impl EventEmitter<ItemEvent> for WelcomePage {}
301
302impl FocusableView for WelcomePage {
303    fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
304        self.focus_handle.clone()
305    }
306}
307
308impl Item for WelcomePage {
309    type Event = ItemEvent;
310
311    fn tab_content(&self, _: Option<usize>, selected: bool, _: &WindowContext) -> AnyElement {
312        Label::new("Welcome to Zed!")
313            .color(if selected {
314                Color::Default
315            } else {
316                Color::Muted
317            })
318            .into_any_element()
319    }
320
321    fn telemetry_event_text(&self) -> Option<&'static str> {
322        Some("welcome page")
323    }
324
325    fn show_toolbar(&self) -> bool {
326        false
327    }
328
329    fn clone_on_split(
330        &self,
331        _workspace_id: WorkspaceId,
332        cx: &mut ViewContext<Self>,
333    ) -> Option<View<Self>> {
334        Some(cx.new_view(|cx| WelcomePage {
335            focus_handle: cx.focus_handle(),
336            workspace: self.workspace.clone(),
337            telemetry: self.telemetry.clone(),
338            _settings_subscription: cx.observe_global::<SettingsStore>(move |_, cx| cx.notify()),
339        }))
340    }
341
342    fn to_item_events(event: &Self::Event, mut f: impl FnMut(workspace::item::ItemEvent)) {
343        f(*event)
344    }
345}