welcome.rs

  1mod base_keymap_picker;
  2mod base_keymap_setting;
  3mod multibuffer_hint;
  4
  5use client::{telemetry::Telemetry, TelemetrySettings};
  6use db::kvp::KEY_VALUE_STORE;
  7use gpui::{
  8    actions, svg, Action, App, Context, Entity, EventEmitter, FocusHandle, Focusable,
  9    InteractiveElement, ParentElement, Render, Styled, Subscription, Task, WeakEntity, Window,
 10};
 11use settings::{Settings, SettingsStore};
 12use std::sync::Arc;
 13use ui::{prelude::*, CheckboxWithLabel, ElevationIndex, Tooltip};
 14use vim_mode_setting::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;
 22pub use multibuffer_hint::*;
 23
 24actions!(welcome, [ResetHints]);
 25
 26pub const FIRST_OPEN: &str = "first_open";
 27pub const DOCS_URL: &str = "https://zed.dev/docs/";
 28const BOOK_ONBOARDING: &str = "https://dub.sh/zed-c-onboarding";
 29
 30pub fn init(cx: &mut App) {
 31    BaseKeymap::register(cx);
 32
 33    cx.observe_new(|workspace: &mut Workspace, _, _cx| {
 34        workspace.register_action(|workspace, _: &Welcome, window, cx| {
 35            let welcome_page = WelcomePage::new(workspace, cx);
 36            workspace.add_item_to_active_pane(Box::new(welcome_page), None, true, window, cx)
 37        });
 38        workspace
 39            .register_action(|_workspace, _: &ResetHints, _, cx| MultibufferHint::set_count(0, cx));
 40    })
 41    .detach();
 42
 43    base_keymap_picker::init(cx);
 44}
 45
 46pub fn show_welcome_view(app_state: Arc<AppState>, cx: &mut App) -> Task<anyhow::Result<()>> {
 47    open_new(
 48        Default::default(),
 49        app_state,
 50        cx,
 51        |workspace, window, cx| {
 52            workspace.toggle_dock(DockPosition::Left, window, cx);
 53            let welcome_page = WelcomePage::new(workspace, cx);
 54            workspace.add_item_to_center(Box::new(welcome_page.clone()), window, cx);
 55
 56            window.focus(&welcome_page.focus_handle(cx));
 57
 58            cx.notify();
 59
 60            db::write_and_log(cx, || {
 61                KEY_VALUE_STORE.write_kvp(FIRST_OPEN.to_string(), "false".to_string())
 62            });
 63        },
 64    )
 65}
 66
 67pub struct WelcomePage {
 68    workspace: WeakEntity<Workspace>,
 69    focus_handle: FocusHandle,
 70    telemetry: Arc<Telemetry>,
 71    _settings_subscription: Subscription,
 72}
 73
 74impl Render for WelcomePage {
 75    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 76        h_flex()
 77            .size_full()
 78            .bg(cx.theme().colors().editor_background)
 79            .key_context("Welcome")
 80            .track_focus(&self.focus_handle(cx))
 81            .child(
 82                v_flex()
 83                    .gap_8()
 84                    .mx_auto()
 85                    .child(
 86                        v_flex()
 87                            .w_full()
 88                            .child(
 89                                svg()
 90                                    .path("icons/logo_96.svg")
 91                                    .text_color(cx.theme().colors().icon_disabled)
 92                                    .w(px(40.))
 93                                    .h(px(40.))
 94                                    .mx_auto()
 95                                    .mb_4(),
 96                            )
 97                            .child(
 98                                h_flex()
 99                                    .w_full()
100                                    .justify_center()
101                                    .child(Headline::new("Welcome to Zed")),
102                            )
103                            .child(
104                                h_flex().w_full().justify_center().child(
105                                    Label::new("The editor for what's next")
106                                        .color(Color::Muted)
107                                        .italic(),
108                                ),
109                            ),
110                    )
111                    .child(
112                        h_flex()
113                            .items_start()
114                            .gap_8()
115                            .child(
116                                v_flex()
117                                    .gap_2()
118                                    .pr_8()
119                                    .border_r_1()
120                                    .border_color(cx.theme().colors().border_variant)
121                                    .child(
122                                        self.section_label( cx).child(
123                                            Label::new("Get Started")
124                                                .size(LabelSize::XSmall)
125                                                .color(Color::Muted),
126                                        ),
127                                    )
128                                    .child(
129                                        Button::new("choose-theme", "Choose a Theme")
130                                            .icon(IconName::SwatchBook)
131                                            .icon_size(IconSize::XSmall)
132                                            .icon_color(Color::Muted)
133                                            .icon_position(IconPosition::Start)
134                                            .on_click(cx.listener(|this, _, window, cx| {
135                                                telemetry::event!("Welcome Theme Changed");
136                                                this.workspace
137                                                    .update(cx, |_workspace, cx| {
138                                                        window.dispatch_action(zed_actions::theme_selector::Toggle::default().boxed_clone(), cx);
139                                                    })
140                                                    .ok();
141                                            })),
142                                    )
143                                    .child(
144                                        Button::new("choose-keymap", "Choose a Keymap")
145                                            .icon(IconName::Keyboard)
146                                            .icon_size(IconSize::XSmall)
147                                            .icon_color(Color::Muted)
148                                            .icon_position(IconPosition::Start)
149                                            .on_click(cx.listener(|this, _, window, cx| {
150                                                telemetry::event!("Welcome Keymap Changed");
151                                                this.workspace
152                                                    .update(cx, |workspace, cx| {
153                                                        base_keymap_picker::toggle(
154                                                            workspace,
155                                                            &Default::default(),
156                                                            window, cx,
157                                                        )
158                                                    })
159                                                    .ok();
160                                            })),
161                                    )
162                                    .child(
163                                        Button::new(
164                                            "sign-in-to-copilot",
165                                            "Sign in to GitHub Copilot",
166                                        )
167                                        .icon(IconName::Copilot)
168                                        .icon_size(IconSize::XSmall)
169                                        .icon_color(Color::Muted)
170                                        .icon_position(IconPosition::Start)
171                                        .on_click(
172                                            cx.listener(|_, _, window, cx| {
173                                                telemetry::event!("Welcome Copilot Signed In");
174                                                copilot::initiate_sign_in(window, cx);
175                                            }),
176                                        ),
177                                    )
178                                    .child(
179                                        Button::new("edit settings", "Edit Settings")
180                                            .icon(IconName::Settings)
181                                            .icon_size(IconSize::XSmall)
182                                            .icon_color(Color::Muted)
183                                            .icon_position(IconPosition::Start)
184                                            .on_click(cx.listener(|_, _, window, cx| {
185                                                telemetry::event!("Welcome Settings Edited");
186                                                window.dispatch_action(Box::new(
187                                                    zed_actions::OpenSettings,
188                                                ), cx);
189                                            })),
190                                    ),
191                            )
192                            .child(
193                                v_flex()
194                                    .gap_2()
195                                    .child(
196                                        self.section_label(cx).child(
197                                            Label::new("Resources")
198                                                .size(LabelSize::XSmall)
199                                                .color(Color::Muted),
200                                        ),
201                                    )
202                                    .when(cfg!(target_os = "macos"), |el| {
203                                        el.child(
204                                            Button::new("install-cli", "Install the CLI")
205                                                .icon(IconName::Terminal)
206                                                .icon_size(IconSize::XSmall)
207                                                .icon_color(Color::Muted)
208                                                .icon_position(IconPosition::Start)
209                                                .on_click(cx.listener(|_, _, _, cx| {
210                                                    telemetry::event!("Welcome CLI Installed");
211                                                    cx
212                                                        .spawn(|_, cx| async move {
213                                                            install_cli::install_cli(&cx).await
214                                                        })
215                                                        .detach_and_log_err(cx);
216                                                })),
217                                        )
218                                    })
219                                    .child(
220                                        Button::new("view-docs", "View Documentation")
221                                            .icon(IconName::FileCode)
222                                            .icon_size(IconSize::XSmall)
223                                            .icon_color(Color::Muted)
224                                            .icon_position(IconPosition::Start)
225                                            .on_click(cx.listener(|_, _, _, cx| {
226                                                telemetry::event!("Welcome Documentation Viewed");
227                                                cx.open_url(DOCS_URL);
228                                            })),
229                                    )
230                                    .child(
231                                        Button::new("explore-extensions", "Explore Extensions")
232                                            .icon(IconName::Blocks)
233                                            .icon_size(IconSize::XSmall)
234                                            .icon_color(Color::Muted)
235                                            .icon_position(IconPosition::Start)
236                                            .on_click(cx.listener(|_, _, window, cx| {
237                                                telemetry::event!("Welcome Extensions Page Opened");
238                                                window.dispatch_action(Box::new(
239                                                    zed_actions::Extensions,
240                                                ), cx);
241                                            })),
242                                    )
243                                    .child(
244                                        Button::new("book-onboarding", "Book Onboarding")
245                                            .icon(IconName::PhoneIncoming)
246                                            .icon_size(IconSize::XSmall)
247                                            .icon_color(Color::Muted)
248                                            .icon_position(IconPosition::Start)
249                                            .on_click(cx.listener(|_, _, _, cx| {
250                                                cx.open_url(BOOK_ONBOARDING);
251                                            })),
252                                    ),
253                            ),
254                    )
255                    .child(
256                        v_group()
257                            .gap_2()
258                            .child(
259                                h_flex()
260                                    .justify_between()
261                                    .child(
262                                        CheckboxWithLabel::new(
263                                            "enable-vim",
264                                            Label::new("Enable Vim Mode"),
265                                            if VimModeSetting::get_global(cx).0 {
266                                                ui::ToggleState::Selected
267                                            } else {
268                                                ui::ToggleState::Unselected
269                                            },
270                                            cx.listener(move |this, selection, _window, cx| {
271                                                telemetry::event!("Welcome Vim Mode Toggled");
272                                                this.update_settings::<VimModeSetting>(
273                                                    selection,
274                                                    cx,
275                                                    |setting, value| *setting = Some(value),
276                                                );
277                                            }),
278                                        )
279                                        .fill()
280                                        .elevation(ElevationIndex::ElevatedSurface),
281                                    )
282                                    .child(
283                                        IconButton::new("vim-mode", IconName::Info)
284                                            .icon_size(IconSize::XSmall)
285                                            .icon_color(Color::Muted)
286                                            .tooltip(
287                                                Tooltip::text(
288                                                    "You can also toggle Vim Mode via the command palette or Editor Controls menu.")
289                                            ),
290                                    ),
291                            )
292                            .child(
293                                CheckboxWithLabel::new(
294                                    "enable-crash",
295                                    Label::new("Send Crash Reports"),
296                                    if TelemetrySettings::get_global(cx).diagnostics {
297                                        ui::ToggleState::Selected
298                                    } else {
299                                        ui::ToggleState::Unselected
300                                    },
301                                    cx.listener(move |this, selection, _window, cx| {
302                                        telemetry::event!("Welcome Diagnostic Telemetry Toggled");
303                                        this.update_settings::<TelemetrySettings>(selection, cx, {
304                                            move |settings, value| {
305                                                settings.diagnostics = Some(value);
306                                                telemetry::event!(
307                                                    "Settings Changed",
308                                                    setting = "diagnostic telemetry",
309                                                    value
310                                                );
311                                            }
312                                        });
313                                    }),
314                                )
315                                .fill()
316                                .elevation(ElevationIndex::ElevatedSurface),
317                            )
318                            .child(
319                                CheckboxWithLabel::new(
320                                    "enable-telemetry",
321                                    Label::new("Send Telemetry"),
322                                    if TelemetrySettings::get_global(cx).metrics {
323                                        ui::ToggleState::Selected
324                                    } else {
325                                        ui::ToggleState::Unselected
326                                    },
327                                    cx.listener(move |this, selection, _window, cx| {
328                                        telemetry::event!("Welcome Metric Telemetry Toggled");
329                                        this.update_settings::<TelemetrySettings>(selection, cx, {
330                                            move |settings, value| {
331                                                settings.metrics = Some(value);
332                                                telemetry::event!(
333                                                    "Settings Changed",
334                                                    setting = "metric telemetry",
335                                                    value
336                                                );
337                                            }
338                                        });
339                                    }),
340                                )
341                                .fill()
342                                .elevation(ElevationIndex::ElevatedSurface),
343                            ),
344                    ),
345            )
346    }
347}
348
349impl WelcomePage {
350    pub fn new(workspace: &Workspace, cx: &mut Context<Workspace>) -> Entity<Self> {
351        let this = cx.new(|cx| {
352            cx.on_release(|_: &mut Self, _| {
353                telemetry::event!("Welcome Page Closed");
354            })
355            .detach();
356
357            WelcomePage {
358                focus_handle: cx.focus_handle(),
359                workspace: workspace.weak_handle(),
360                telemetry: workspace.client().telemetry().clone(),
361                _settings_subscription: cx
362                    .observe_global::<SettingsStore>(move |_, cx| cx.notify()),
363            }
364        });
365
366        this
367    }
368
369    fn section_label(&self, cx: &mut App) -> Div {
370        div()
371            .pl_1()
372            .font_buffer(cx)
373            .text_color(Color::Muted.color(cx))
374    }
375
376    fn update_settings<T: Settings>(
377        &mut self,
378        selection: &ToggleState,
379        cx: &mut Context<Self>,
380        callback: impl 'static + Send + Fn(&mut T::FileContent, bool),
381    ) {
382        if let Some(workspace) = self.workspace.upgrade() {
383            let fs = workspace.read(cx).app_state().fs.clone();
384            let selection = *selection;
385            settings::update_settings_file::<T>(fs, cx, move |settings, _| {
386                let value = match selection {
387                    ToggleState::Unselected => false,
388                    ToggleState::Selected => true,
389                    _ => return,
390                };
391
392                callback(settings, value)
393            });
394        }
395    }
396}
397
398impl EventEmitter<ItemEvent> for WelcomePage {}
399
400impl Focusable for WelcomePage {
401    fn focus_handle(&self, _: &App) -> gpui::FocusHandle {
402        self.focus_handle.clone()
403    }
404}
405
406impl Item for WelcomePage {
407    type Event = ItemEvent;
408
409    fn tab_content_text(&self, _window: &Window, _cx: &App) -> Option<SharedString> {
410        Some("Welcome".into())
411    }
412
413    fn telemetry_event_text(&self) -> Option<&'static str> {
414        Some("Welcome Page Opened")
415    }
416
417    fn show_toolbar(&self) -> bool {
418        false
419    }
420
421    fn clone_on_split(
422        &self,
423        _workspace_id: Option<WorkspaceId>,
424        _: &mut Window,
425        cx: &mut Context<Self>,
426    ) -> Option<Entity<Self>> {
427        Some(cx.new(|cx| WelcomePage {
428            focus_handle: cx.focus_handle(),
429            workspace: self.workspace.clone(),
430            telemetry: self.telemetry.clone(),
431            _settings_subscription: cx.observe_global::<SettingsStore>(move |_, cx| cx.notify()),
432        }))
433    }
434
435    fn to_item_events(event: &Self::Event, mut f: impl FnMut(workspace::item::ItemEvent)) {
436        f(*event)
437    }
438}