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_stack().full().track_focus(&self.focus_handle).child(
 64            v_stack()
 65                .w_96()
 66                .gap_4()
 67                .mx_auto()
 68                .child(
 69                    svg()
 70                        .path("icons/logo_96.svg")
 71                        .text_color(gpui::white())
 72                        .w(px(96.))
 73                        .h(px(96.))
 74                        .mx_auto(),
 75                )
 76                .child(
 77                    h_stack()
 78                        .justify_center()
 79                        .child(Label::new("Code at the speed of thought")),
 80                )
 81                .child(
 82                    v_stack()
 83                        .gap_2()
 84                        .child(
 85                            Button::new("choose-theme", "Choose a theme")
 86                                .full_width()
 87                                .on_click(cx.listener(|this, _, cx| {
 88                                    this.telemetry
 89                                        .report_app_event("welcome page: change theme");
 90                                    this.workspace
 91                                        .update(cx, |workspace, cx| {
 92                                            theme_selector::toggle(
 93                                                workspace,
 94                                                &Default::default(),
 95                                                cx,
 96                                            )
 97                                        })
 98                                        .ok();
 99                                })),
100                        )
101                        .child(
102                            Button::new("choose-keymap", "Choose a keymap")
103                                .full_width()
104                                .on_click(cx.listener(|this, _, cx| {
105                                    this.telemetry
106                                        .report_app_event("welcome page: change keymap");
107                                    this.workspace
108                                        .update(cx, |workspace, cx| {
109                                            base_keymap_picker::toggle(
110                                                workspace,
111                                                &Default::default(),
112                                                cx,
113                                            )
114                                        })
115                                        .ok();
116                                })),
117                        )
118                        .child(
119                            Button::new("install-cli", "Install the CLI")
120                                .full_width()
121                                .on_click(cx.listener(|this, _, cx| {
122                                    this.telemetry.report_app_event("welcome page: install cli");
123                                    cx.app_mut()
124                                        .spawn(
125                                            |cx| async move { install_cli::install_cli(&cx).await },
126                                        )
127                                        .detach_and_log_err(cx);
128                                })),
129                        ),
130                )
131                .child(
132                    v_stack()
133                        .p_3()
134                        .gap_2()
135                        .bg(cx.theme().colors().elevated_surface_background)
136                        .border_1()
137                        .border_color(cx.theme().colors().border)
138                        .rounded_md()
139                        .child(
140                            h_stack()
141                                .gap_2()
142                                .child(
143                                    Checkbox::new(
144                                        "enable-vim",
145                                        if VimModeSetting::get_global(cx).0 {
146                                            ui::Selection::Selected
147                                        } else {
148                                            ui::Selection::Unselected
149                                        },
150                                    )
151                                    .on_click(cx.listener(
152                                        move |this, selection, cx| {
153                                            this.telemetry
154                                                .report_app_event("welcome page: toggle vim");
155                                            this.update_settings::<VimModeSetting>(
156                                                selection,
157                                                cx,
158                                                |setting, value| *setting = Some(value),
159                                            );
160                                        },
161                                    )),
162                                )
163                                .child(Label::new("Enable vim mode")),
164                        )
165                        .child(
166                            h_stack()
167                                .gap_2()
168                                .child(
169                                    Checkbox::new(
170                                        "enable-telemetry",
171                                        if TelemetrySettings::get_global(cx).metrics {
172                                            ui::Selection::Selected
173                                        } else {
174                                            ui::Selection::Unselected
175                                        },
176                                    )
177                                    .on_click(cx.listener(
178                                        move |this, selection, cx| {
179                                            this.telemetry.report_app_event(
180                                                "welcome page: toggle metric telemetry",
181                                            );
182                                            this.update_settings::<TelemetrySettings>(
183                                                selection,
184                                                cx,
185                                                {
186                                                    let telemetry = this.telemetry.clone();
187
188                                                    move |settings, value| {
189                                                        settings.metrics = Some(value);
190
191                                                        telemetry.report_setting_event(
192                                                            "metric telemetry",
193                                                            value.to_string(),
194                                                        );
195                                                    }
196                                                },
197                                            );
198                                        },
199                                    )),
200                                )
201                                .child(Label::new("Send anonymous usage data")),
202                        )
203                        .child(
204                            h_stack()
205                                .gap_2()
206                                .child(
207                                    Checkbox::new(
208                                        "enable-crash",
209                                        if TelemetrySettings::get_global(cx).diagnostics {
210                                            ui::Selection::Selected
211                                        } else {
212                                            ui::Selection::Unselected
213                                        },
214                                    )
215                                    .on_click(cx.listener(
216                                        move |this, selection, cx| {
217                                            this.telemetry.report_app_event(
218                                                "welcome page: toggle diagnostic telemetry",
219                                            );
220                                            this.update_settings::<TelemetrySettings>(
221                                                selection,
222                                                cx,
223                                                {
224                                                    let telemetry = this.telemetry.clone();
225
226                                                    move |settings, value| {
227                                                        settings.diagnostics = Some(value);
228
229                                                        telemetry.report_setting_event(
230                                                            "diagnostic telemetry",
231                                                            value.to_string(),
232                                                        );
233                                                    }
234                                                },
235                                            );
236                                        },
237                                    )),
238                                )
239                                .child(Label::new("Send crash reports")),
240                        ),
241                ),
242        )
243    }
244}
245
246impl WelcomePage {
247    pub fn new(workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
248        let this = cx.new_view(|cx| {
249            cx.on_release(|this: &mut Self, _, _| {
250                this.telemetry.report_app_event("welcome page: close");
251            })
252            .detach();
253
254            WelcomePage {
255                focus_handle: cx.focus_handle(),
256                workspace: workspace.weak_handle(),
257                telemetry: workspace.client().telemetry().clone(),
258                _settings_subscription: cx
259                    .observe_global::<SettingsStore>(move |_, cx| cx.notify()),
260            }
261        });
262
263        this
264    }
265
266    fn update_settings<T: Settings>(
267        &mut self,
268        selection: &Selection,
269        cx: &mut ViewContext<Self>,
270        callback: impl 'static + Send + Fn(&mut T::FileContent, bool),
271    ) {
272        if let Some(workspace) = self.workspace.upgrade() {
273            let fs = workspace.read(cx).app_state().fs.clone();
274            let selection = *selection;
275            settings::update_settings_file::<T>(fs, cx, move |settings| {
276                let value = match selection {
277                    Selection::Unselected => false,
278                    Selection::Selected => true,
279                    _ => return,
280                };
281
282                callback(settings, value)
283            });
284        }
285    }
286}
287
288impl EventEmitter<ItemEvent> for WelcomePage {}
289
290impl FocusableView for WelcomePage {
291    fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
292        self.focus_handle.clone()
293    }
294}
295
296impl Item for WelcomePage {
297    type Event = ItemEvent;
298
299    fn tab_content(&self, _: Option<usize>, selected: bool, _: &WindowContext) -> AnyElement {
300        Label::new("Welcome to Zed!")
301            .color(if selected {
302                Color::Default
303            } else {
304                Color::Muted
305            })
306            .into_any_element()
307    }
308
309    fn show_toolbar(&self) -> bool {
310        false
311    }
312
313    fn clone_on_split(
314        &self,
315        _workspace_id: WorkspaceId,
316        cx: &mut ViewContext<Self>,
317    ) -> Option<View<Self>> {
318        Some(cx.new_view(|cx| WelcomePage {
319            focus_handle: cx.focus_handle(),
320            workspace: self.workspace.clone(),
321            telemetry: self.telemetry.clone(),
322            _settings_subscription: cx.observe_global::<SettingsStore>(move |_, cx| cx.notify()),
323        }))
324    }
325
326    fn to_item_events(event: &Self::Event, mut f: impl FnMut(workspace::item::ItemEvent)) {
327        f(*event)
328    }
329}