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                            .child(
127                                Button::new("install-cli", "Install the CLI")
128                                    .full_width()
129                                    .on_click(cx.listener(|this, _, cx| {
130                                        this.telemetry.report_app_event(
131                                            "welcome page: install cli".to_string(),
132                                        );
133                                        cx.app_mut()
134                                            .spawn(|cx| async move {
135                                                install_cli::install_cli(&cx).await
136                                            })
137                                            .detach_and_log_err(cx);
138                                    })),
139                            )
140                            .child(
141                                Button::new("sign-in-to-copilot", "Sign in to GitHub Copilot")
142                                    .full_width()
143                                    .on_click(cx.listener(|this, _, cx| {
144                                        this.telemetry.report_app_event(
145                                            "welcome page: sign in to copilot".to_string(),
146                                        );
147                                        inline_completion_button::initiate_sign_in(cx);
148                                    })),
149                            )
150                            .child(
151                                Button::new("explore extensions", "Explore extensions")
152                                    .full_width()
153                                    .on_click(cx.listener(|this, _, cx| {
154                                        this.telemetry.report_app_event(
155                                            "welcome page: open extensions".to_string(),
156                                        );
157                                        cx.dispatch_action(Box::new(extensions_ui::Extensions));
158                                    })),
159                            ),
160                    )
161                    .child(
162                        v_flex()
163                            .p_3()
164                            .gap_2()
165                            .bg(cx.theme().colors().elevated_surface_background)
166                            .border_1()
167                            .border_color(cx.theme().colors().border)
168                            .rounded_md()
169                            .child(CheckboxWithLabel::new(
170                                "enable-vim",
171                                Label::new("Enable vim mode"),
172                                if VimModeSetting::get_global(cx).0 {
173                                    ui::Selection::Selected
174                                } else {
175                                    ui::Selection::Unselected
176                                },
177                                cx.listener(move |this, selection, cx| {
178                                    this.telemetry
179                                        .report_app_event("welcome page: toggle vim".to_string());
180                                    this.update_settings::<VimModeSetting>(
181                                        selection,
182                                        cx,
183                                        |setting, value| *setting = Some(value),
184                                    );
185                                }),
186                            ))
187                            .child(CheckboxWithLabel::new(
188                                "enable-telemetry",
189                                Label::new("Send anonymous usage data"),
190                                if TelemetrySettings::get_global(cx).metrics {
191                                    ui::Selection::Selected
192                                } else {
193                                    ui::Selection::Unselected
194                                },
195                                cx.listener(move |this, selection, cx| {
196                                    this.telemetry.report_app_event(
197                                        "welcome page: toggle metric telemetry".to_string(),
198                                    );
199                                    this.update_settings::<TelemetrySettings>(selection, cx, {
200                                        let telemetry = this.telemetry.clone();
201
202                                        move |settings, value| {
203                                            settings.metrics = Some(value);
204
205                                            telemetry.report_setting_event(
206                                                "metric telemetry",
207                                                value.to_string(),
208                                            );
209                                        }
210                                    });
211                                }),
212                            ))
213                            .child(CheckboxWithLabel::new(
214                                "enable-crash",
215                                Label::new("Send crash reports"),
216                                if TelemetrySettings::get_global(cx).diagnostics {
217                                    ui::Selection::Selected
218                                } else {
219                                    ui::Selection::Unselected
220                                },
221                                cx.listener(move |this, selection, cx| {
222                                    this.telemetry.report_app_event(
223                                        "welcome page: toggle diagnostic telemetry".to_string(),
224                                    );
225                                    this.update_settings::<TelemetrySettings>(selection, cx, {
226                                        let telemetry = this.telemetry.clone();
227
228                                        move |settings, value| {
229                                            settings.diagnostics = Some(value);
230
231                                            telemetry.report_setting_event(
232                                                "diagnostic telemetry",
233                                                value.to_string(),
234                                            );
235                                        }
236                                    });
237                                }),
238                            )),
239                    ),
240            )
241    }
242}
243
244impl WelcomePage {
245    pub fn new(workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
246        let this = cx.new_view(|cx| {
247            cx.on_release(|this: &mut Self, _, _| {
248                this.telemetry
249                    .report_app_event("welcome page: close".to_string());
250            })
251            .detach();
252
253            WelcomePage {
254                focus_handle: cx.focus_handle(),
255                workspace: workspace.weak_handle(),
256                telemetry: workspace.client().telemetry().clone(),
257                _settings_subscription: cx
258                    .observe_global::<SettingsStore>(move |_, cx| cx.notify()),
259            }
260        });
261
262        this
263    }
264
265    fn update_settings<T: Settings>(
266        &mut self,
267        selection: &Selection,
268        cx: &mut ViewContext<Self>,
269        callback: impl 'static + Send + Fn(&mut T::FileContent, bool),
270    ) {
271        if let Some(workspace) = self.workspace.upgrade() {
272            let fs = workspace.read(cx).app_state().fs.clone();
273            let selection = *selection;
274            settings::update_settings_file::<T>(fs, cx, move |settings| {
275                let value = match selection {
276                    Selection::Unselected => false,
277                    Selection::Selected => true,
278                    _ => return,
279                };
280
281                callback(settings, value)
282            });
283        }
284    }
285}
286
287impl EventEmitter<ItemEvent> for WelcomePage {}
288
289impl FocusableView for WelcomePage {
290    fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
291        self.focus_handle.clone()
292    }
293}
294
295impl Item for WelcomePage {
296    type Event = ItemEvent;
297
298    fn tab_content(&self, params: TabContentParams, _: &WindowContext) -> AnyElement {
299        Label::new("Welcome to Zed!")
300            .color(if params.selected {
301                Color::Default
302            } else {
303                Color::Muted
304            })
305            .into_any_element()
306    }
307
308    fn telemetry_event_text(&self) -> Option<&'static str> {
309        Some("welcome page")
310    }
311
312    fn show_toolbar(&self) -> bool {
313        false
314    }
315
316    fn clone_on_split(
317        &self,
318        _workspace_id: Option<WorkspaceId>,
319        cx: &mut ViewContext<Self>,
320    ) -> Option<View<Self>> {
321        Some(cx.new_view(|cx| WelcomePage {
322            focus_handle: cx.focus_handle(),
323            workspace: self.workspace.clone(),
324            telemetry: self.telemetry.clone(),
325            _settings_subscription: cx.observe_global::<SettingsStore>(move |_, cx| cx.notify()),
326        }))
327    }
328
329    fn to_item_events(event: &Self::Event, mut f: impl FnMut(workspace::item::ItemEvent)) {
330        f(*event)
331    }
332}