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