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