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