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::*, CheckboxWithLabel};
 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            .size_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(CheckboxWithLabel::new(
148                                "enable-vim",
149                                Label::new("Enable vim mode"),
150                                if VimModeSetting::get_global(cx).0 {
151                                    ui::Selection::Selected
152                                } else {
153                                    ui::Selection::Unselected
154                                },
155                                cx.listener(move |this, selection, cx| {
156                                    this.telemetry
157                                        .report_app_event("welcome page: toggle vim".to_string());
158                                    this.update_settings::<VimModeSetting>(
159                                        selection,
160                                        cx,
161                                        |setting, value| *setting = Some(value),
162                                    );
163                                }),
164                            ))
165                            .child(CheckboxWithLabel::new(
166                                "enable-telemetry",
167                                Label::new("Send anonymous usage data"),
168                                if TelemetrySettings::get_global(cx).metrics {
169                                    ui::Selection::Selected
170                                } else {
171                                    ui::Selection::Unselected
172                                },
173                                cx.listener(move |this, selection, cx| {
174                                    this.telemetry.report_app_event(
175                                        "welcome page: toggle metric telemetry".to_string(),
176                                    );
177                                    this.update_settings::<TelemetrySettings>(selection, cx, {
178                                        let telemetry = this.telemetry.clone();
179
180                                        move |settings, value| {
181                                            settings.metrics = Some(value);
182
183                                            telemetry.report_setting_event(
184                                                "metric telemetry",
185                                                value.to_string(),
186                                            );
187                                        }
188                                    });
189                                }),
190                            ))
191                            .child(CheckboxWithLabel::new(
192                                "enable-crash",
193                                Label::new("Send crash reports"),
194                                if TelemetrySettings::get_global(cx).diagnostics {
195                                    ui::Selection::Selected
196                                } else {
197                                    ui::Selection::Unselected
198                                },
199                                cx.listener(move |this, selection, cx| {
200                                    this.telemetry.report_app_event(
201                                        "welcome page: toggle diagnostic telemetry".to_string(),
202                                    );
203                                    this.update_settings::<TelemetrySettings>(selection, cx, {
204                                        let telemetry = this.telemetry.clone();
205
206                                        move |settings, value| {
207                                            settings.diagnostics = Some(value);
208
209                                            telemetry.report_setting_event(
210                                                "diagnostic telemetry",
211                                                value.to_string(),
212                                            );
213                                        }
214                                    });
215                                }),
216                            )),
217                    ),
218            )
219    }
220}
221
222impl WelcomePage {
223    pub fn new(workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
224        let this = cx.new_view(|cx| {
225            cx.on_release(|this: &mut Self, _, _| {
226                this.telemetry
227                    .report_app_event("welcome page: close".to_string());
228            })
229            .detach();
230
231            WelcomePage {
232                focus_handle: cx.focus_handle(),
233                workspace: workspace.weak_handle(),
234                telemetry: workspace.client().telemetry().clone(),
235                _settings_subscription: cx
236                    .observe_global::<SettingsStore>(move |_, cx| cx.notify()),
237            }
238        });
239
240        this
241    }
242
243    fn update_settings<T: Settings>(
244        &mut self,
245        selection: &Selection,
246        cx: &mut ViewContext<Self>,
247        callback: impl 'static + Send + Fn(&mut T::FileContent, bool),
248    ) {
249        if let Some(workspace) = self.workspace.upgrade() {
250            let fs = workspace.read(cx).app_state().fs.clone();
251            let selection = *selection;
252            settings::update_settings_file::<T>(fs, cx, move |settings| {
253                let value = match selection {
254                    Selection::Unselected => false,
255                    Selection::Selected => true,
256                    _ => return,
257                };
258
259                callback(settings, value)
260            });
261        }
262    }
263}
264
265impl EventEmitter<ItemEvent> for WelcomePage {}
266
267impl FocusableView for WelcomePage {
268    fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
269        self.focus_handle.clone()
270    }
271}
272
273impl Item for WelcomePage {
274    type Event = ItemEvent;
275
276    fn tab_content(&self, _: Option<usize>, selected: bool, _: &WindowContext) -> AnyElement {
277        Label::new("Welcome to Zed!")
278            .color(if selected {
279                Color::Default
280            } else {
281                Color::Muted
282            })
283            .into_any_element()
284    }
285
286    fn telemetry_event_text(&self) -> Option<&'static str> {
287        Some("welcome page")
288    }
289
290    fn show_toolbar(&self) -> bool {
291        false
292    }
293
294    fn clone_on_split(
295        &self,
296        _workspace_id: WorkspaceId,
297        cx: &mut ViewContext<Self>,
298    ) -> Option<View<Self>> {
299        Some(cx.new_view(|cx| WelcomePage {
300            focus_handle: cx.focus_handle(),
301            workspace: self.workspace.clone(),
302            telemetry: self.telemetry.clone(),
303            _settings_subscription: cx.observe_global::<SettingsStore>(move |_, cx| cx.notify()),
304        }))
305    }
306
307    fn to_item_events(event: &Self::Event, mut f: impl FnMut(workspace::item::ItemEvent)) {
308        f(*event)
309    }
310}