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(true),
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 this.telemetry.report_app_event(
136 "welcome page: change theme".to_string(),
137 );
138 this.workspace
139 .update(cx, |_workspace, cx| {
140 window.dispatch_action(zed_actions::theme_selector::Toggle::default().boxed_clone(), cx);
141 })
142 .ok();
143 })),
144 )
145 .child(
146 Button::new("choose-keymap", "Choose a Keymap")
147 .icon(IconName::Keyboard)
148 .icon_size(IconSize::XSmall)
149 .icon_color(Color::Muted)
150 .icon_position(IconPosition::Start)
151 .on_click(cx.listener(|this, _, window, cx| {
152 this.telemetry.report_app_event(
153 "welcome page: change keymap".to_string(),
154 );
155 this.workspace
156 .update(cx, |workspace, cx| {
157 base_keymap_picker::toggle(
158 workspace,
159 &Default::default(),
160 window, cx,
161 )
162 })
163 .ok();
164 })),
165 )
166 .child(
167 Button::new(
168 "sign-in-to-copilot",
169 "Sign in to GitHub Copilot",
170 )
171 .icon(IconName::Copilot)
172 .icon_size(IconSize::XSmall)
173 .icon_color(Color::Muted)
174 .icon_position(IconPosition::Start)
175 .on_click(
176 cx.listener(|this, _, window, cx| {
177 this.telemetry.report_app_event(
178 "welcome page: sign in to copilot".to_string(),
179 );
180 copilot::initiate_sign_in(window, cx);
181 }),
182 ),
183 )
184 .child(
185 Button::new("edit settings", "Edit Settings")
186 .icon(IconName::Settings)
187 .icon_size(IconSize::XSmall)
188 .icon_color(Color::Muted)
189 .icon_position(IconPosition::Start)
190 .on_click(cx.listener(|this, _, window, cx| {
191 this.telemetry.report_app_event(
192 "welcome page: edit settings".to_string(),
193 );
194 window.dispatch_action(Box::new(
195 zed_actions::OpenSettings,
196 ), cx);
197 })),
198 ),
199 )
200 .child(
201 v_flex()
202 .gap_2()
203 .child(
204 self.section_label(cx).child(
205 Label::new("Resources")
206 .size(LabelSize::XSmall)
207 .color(Color::Muted),
208 ),
209 )
210 .when(cfg!(target_os = "macos"), |el| {
211 el.child(
212 Button::new("install-cli", "Install the CLI")
213 .icon(IconName::Terminal)
214 .icon_size(IconSize::XSmall)
215 .icon_color(Color::Muted)
216 .icon_position(IconPosition::Start)
217 .on_click(cx.listener(|this, _, _, cx| {
218 this.telemetry.report_app_event(
219 "welcome page: install cli".to_string(),
220 );
221 cx
222 .spawn(|_, cx| async move {
223 install_cli::install_cli(&cx).await
224 })
225 .detach_and_log_err(cx);
226 })),
227 )
228 })
229 .child(
230 Button::new("view-docs", "View Documentation")
231 .icon(IconName::FileCode)
232 .icon_size(IconSize::XSmall)
233 .icon_color(Color::Muted)
234 .icon_position(IconPosition::Start)
235 .on_click(cx.listener(|this, _, _, cx| {
236 this.telemetry.report_app_event(
237 "welcome page: view docs".to_string(),
238 );
239 cx.open_url(DOCS_URL);
240 })),
241 )
242 .child(
243 Button::new("explore-extensions", "Explore Extensions")
244 .icon(IconName::Blocks)
245 .icon_size(IconSize::XSmall)
246 .icon_color(Color::Muted)
247 .icon_position(IconPosition::Start)
248 .on_click(cx.listener(|this, _, window, cx| {
249 this.telemetry.report_app_event(
250 "welcome page: open extensions".to_string(),
251 );
252 window.dispatch_action(Box::new(
253 zed_actions::Extensions,
254 ), cx);
255 })),
256 )
257 .child(
258 Button::new("book-onboarding", "Book Onboarding")
259 .icon(IconName::PhoneIncoming)
260 .icon_size(IconSize::XSmall)
261 .icon_color(Color::Muted)
262 .icon_position(IconPosition::Start)
263 .on_click(cx.listener(|_, _, _, cx| {
264 cx.open_url(BOOK_ONBOARDING);
265 })),
266 ),
267 ),
268 )
269 .child(
270 v_group()
271 .gap_2()
272 .child(
273 h_flex()
274 .justify_between()
275 .child(
276 CheckboxWithLabel::new(
277 "enable-vim",
278 Label::new("Enable Vim Mode"),
279 if VimModeSetting::get_global(cx).0 {
280 ui::ToggleState::Selected
281 } else {
282 ui::ToggleState::Unselected
283 },
284 cx.listener(move |this, selection, _window, cx| {
285 this.telemetry
286 .report_app_event("welcome page: toggle vim".to_string());
287 this.update_settings::<VimModeSetting>(
288 selection,
289 cx,
290 |setting, value| *setting = Some(value),
291 );
292 }),
293 )
294 .fill()
295 .elevation(ElevationIndex::ElevatedSurface),
296 )
297 .child(
298 IconButton::new("vim-mode", IconName::Info)
299 .icon_size(IconSize::XSmall)
300 .icon_color(Color::Muted)
301 .tooltip(
302 Tooltip::text(
303 "You can also toggle Vim Mode via the command palette or Editor Controls menu.")
304 ),
305 ),
306 )
307 .child(
308 CheckboxWithLabel::new(
309 "enable-crash",
310 Label::new("Send Crash Reports"),
311 if TelemetrySettings::get_global(cx).diagnostics {
312 ui::ToggleState::Selected
313 } else {
314 ui::ToggleState::Unselected
315 },
316 cx.listener(move |this, selection, _window, cx| {
317 this.telemetry.report_app_event(
318 "welcome page: toggle diagnostic telemetry".to_string(),
319 );
320 this.update_settings::<TelemetrySettings>(selection, cx, {
321 move |settings, value| {
322 settings.diagnostics = Some(value);
323 telemetry::event!(
324 "Settings Changed",
325 setting = "diagnostic telemetry",
326 value
327 );
328 }
329 });
330 }),
331 )
332 .fill()
333 .elevation(ElevationIndex::ElevatedSurface),
334 )
335 .child(
336 CheckboxWithLabel::new(
337 "enable-telemetry",
338 Label::new("Send Telemetry"),
339 if TelemetrySettings::get_global(cx).metrics {
340 ui::ToggleState::Selected
341 } else {
342 ui::ToggleState::Unselected
343 },
344 cx.listener(move |this, selection, _window, cx| {
345 this.telemetry.report_app_event(
346 "welcome page: toggle metric telemetry".to_string(),
347 );
348 this.update_settings::<TelemetrySettings>(selection, cx, {
349 move |settings, value| {
350 settings.metrics = Some(value);
351 telemetry::event!(
352 "Settings Changed",
353 setting = "metric telemetry",
354 value
355 );
356 }
357 });
358 }),
359 )
360 .fill()
361 .elevation(ElevationIndex::ElevatedSurface),
362 ),
363 ),
364 )
365 }
366}
367
368impl WelcomePage {
369 pub fn new(workspace: &Workspace, cx: &mut Context<Workspace>) -> Entity<Self> {
370 let this = cx.new(|cx| {
371 cx.on_release(|this: &mut Self, _| {
372 this.telemetry
373 .report_app_event("welcome page: close".to_string());
374 })
375 .detach();
376
377 WelcomePage {
378 focus_handle: cx.focus_handle(),
379 workspace: workspace.weak_handle(),
380 telemetry: workspace.client().telemetry().clone(),
381 _settings_subscription: cx
382 .observe_global::<SettingsStore>(move |_, cx| cx.notify()),
383 }
384 });
385
386 this
387 }
388
389 fn section_label(&self, cx: &mut App) -> Div {
390 div()
391 .pl_1()
392 .font_buffer(cx)
393 .text_color(Color::Muted.color(cx))
394 }
395
396 fn update_settings<T: Settings>(
397 &mut self,
398 selection: &ToggleState,
399 cx: &mut Context<Self>,
400 callback: impl 'static + Send + Fn(&mut T::FileContent, bool),
401 ) {
402 if let Some(workspace) = self.workspace.upgrade() {
403 let fs = workspace.read(cx).app_state().fs.clone();
404 let selection = *selection;
405 settings::update_settings_file::<T>(fs, cx, move |settings, _| {
406 let value = match selection {
407 ToggleState::Unselected => false,
408 ToggleState::Selected => true,
409 _ => return,
410 };
411
412 callback(settings, value)
413 });
414 }
415 }
416}
417
418impl EventEmitter<ItemEvent> for WelcomePage {}
419
420impl Focusable for WelcomePage {
421 fn focus_handle(&self, _: &App) -> gpui::FocusHandle {
422 self.focus_handle.clone()
423 }
424}
425
426impl Item for WelcomePage {
427 type Event = ItemEvent;
428
429 fn tab_content_text(&self, _window: &Window, _cx: &App) -> Option<SharedString> {
430 Some("Welcome".into())
431 }
432
433 fn telemetry_event_text(&self) -> Option<&'static str> {
434 Some("welcome page")
435 }
436
437 fn show_toolbar(&self) -> bool {
438 false
439 }
440
441 fn clone_on_split(
442 &self,
443 _workspace_id: Option<WorkspaceId>,
444 _: &mut Window,
445 cx: &mut Context<Self>,
446 ) -> Option<Entity<Self>> {
447 Some(cx.new(|cx| WelcomePage {
448 focus_handle: cx.focus_handle(),
449 workspace: self.workspace.clone(),
450 telemetry: self.telemetry.clone(),
451 _settings_subscription: cx.observe_global::<SettingsStore>(move |_, cx| cx.notify()),
452 }))
453 }
454
455 fn to_item_events(event: &Self::Event, mut f: impl FnMut(workspace::item::ItemEvent)) {
456 f(*event)
457 }
458}