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, 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};
15use vim::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 gpui::ViewContext<Self>) -> impl IntoElement {
73 h_flex()
74 .size_full()
75 .bg(cx.theme().colors().editor_background)
76 .track_focus(&self.focus_handle(cx))
77 .child(
78 v_flex()
79 .gap_8()
80 .mx_auto()
81 .child(
82 v_flex()
83 .w_full()
84 .child(
85 svg()
86 .path("icons/logo_96.svg")
87 .text_color(cx.theme().colors().icon_disabled)
88 .w(px(40.))
89 .h(px(40.))
90 .mx_auto()
91 .mb_4(),
92 )
93 .child(
94 h_flex()
95 .w_full()
96 .justify_center()
97 .child(Headline::new("Welcome to Zed")),
98 )
99 .child(
100 h_flex().w_full().justify_center().child(
101 Label::new("The editor for what's next")
102 .color(Color::Muted)
103 .italic(true),
104 ),
105 ),
106 )
107 .child(
108 h_flex()
109 .items_start()
110 .gap_8()
111 .child(
112 v_flex()
113 .gap_2()
114 .pr_8()
115 .border_r_1()
116 .border_color(cx.theme().colors().border_variant)
117 .child(
118 self.section_label(cx).child(
119 Label::new("Get Started")
120 .size(LabelSize::XSmall)
121 .color(Color::Muted),
122 ),
123 )
124 .child(
125 Button::new("choose-theme", "Choose a Theme")
126 .icon(IconName::SwatchBook)
127 .icon_size(IconSize::XSmall)
128 .icon_color(Color::Muted)
129 .icon_position(IconPosition::Start)
130 .on_click(cx.listener(|this, _, cx| {
131 this.telemetry.report_app_event(
132 "welcome page: change theme".to_string(),
133 );
134 this.workspace
135 .update(cx, |workspace, cx| {
136 theme_selector::toggle(
137 workspace,
138 &Default::default(),
139 cx,
140 )
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, _, 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 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, _, cx| {
177 this.telemetry.report_app_event(
178 "welcome page: sign in to copilot".to_string(),
179 );
180 inline_completion_button::initiate_sign_in(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, _, cx| {
191 this.telemetry.report_app_event(
192 "welcome page: edit settings".to_string(),
193 );
194 cx.dispatch_action(Box::new(
195 zed_actions::OpenSettings,
196 ));
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.app_mut()
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, _, cx| {
249 this.telemetry.report_app_event(
250 "welcome page: open extensions".to_string(),
251 );
252 cx.dispatch_action(Box::new(
253 extensions_ui::Extensions,
254 ));
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(CheckboxWithLabel::new(
273 "enable-vim",
274 Label::new("Enable Vim Mode"),
275 if VimModeSetting::get_global(cx).0 {
276 ui::Selection::Selected
277 } else {
278 ui::Selection::Unselected
279 },
280 cx.listener(move |this, selection, cx| {
281 this.telemetry
282 .report_app_event("welcome page: toggle vim".to_string());
283 this.update_settings::<VimModeSetting>(
284 selection,
285 cx,
286 |setting, value| *setting = Some(value),
287 );
288 }),
289 ))
290 .child(CheckboxWithLabel::new(
291 "enable-crash",
292 Label::new("Send Crash Reports"),
293 if TelemetrySettings::get_global(cx).diagnostics {
294 ui::Selection::Selected
295 } else {
296 ui::Selection::Unselected
297 },
298 cx.listener(move |this, selection, cx| {
299 this.telemetry.report_app_event(
300 "welcome page: toggle diagnostic telemetry".to_string(),
301 );
302 this.update_settings::<TelemetrySettings>(selection, cx, {
303 let telemetry = this.telemetry.clone();
304
305 move |settings, value| {
306 settings.diagnostics = Some(value);
307
308 telemetry.report_setting_event(
309 "diagnostic telemetry",
310 value.to_string(),
311 );
312 }
313 });
314 }),
315 ))
316 .child(CheckboxWithLabel::new(
317 "enable-telemetry",
318 Label::new("Send Telemetry"),
319 if TelemetrySettings::get_global(cx).metrics {
320 ui::Selection::Selected
321 } else {
322 ui::Selection::Unselected
323 },
324 cx.listener(move |this, selection, cx| {
325 this.telemetry.report_app_event(
326 "welcome page: toggle metric telemetry".to_string(),
327 );
328 this.update_settings::<TelemetrySettings>(selection, cx, {
329 let telemetry = this.telemetry.clone();
330
331 move |settings, value| {
332 settings.metrics = Some(value);
333
334 telemetry.report_setting_event(
335 "metric telemetry",
336 value.to_string(),
337 );
338 }
339 });
340 }),
341 )),
342 ),
343 )
344 }
345}
346
347impl WelcomePage {
348 pub fn new(workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
349 let this = cx.new_view(|cx| {
350 cx.on_release(|this: &mut Self, _, _| {
351 this.telemetry
352 .report_app_event("welcome page: close".to_string());
353 })
354 .detach();
355
356 WelcomePage {
357 focus_handle: cx.focus_handle(),
358 workspace: workspace.weak_handle(),
359 telemetry: workspace.client().telemetry().clone(),
360 _settings_subscription: cx
361 .observe_global::<SettingsStore>(move |_, cx| cx.notify()),
362 }
363 });
364
365 this
366 }
367
368 fn section_label(&self, cx: &WindowContext) -> Div {
369 div()
370 .pl_1()
371 .font_buffer(cx)
372 .text_color(Color::Muted.color(cx))
373 }
374
375 fn update_settings<T: Settings>(
376 &mut self,
377 selection: &Selection,
378 cx: &mut ViewContext<Self>,
379 callback: impl 'static + Send + Fn(&mut T::FileContent, bool),
380 ) {
381 if let Some(workspace) = self.workspace.upgrade() {
382 let fs = workspace.read(cx).app_state().fs.clone();
383 let selection = *selection;
384 settings::update_settings_file::<T>(fs, cx, move |settings, _| {
385 let value = match selection {
386 Selection::Unselected => false,
387 Selection::Selected => true,
388 _ => return,
389 };
390
391 callback(settings, value)
392 });
393 }
394 }
395}
396
397impl EventEmitter<ItemEvent> for WelcomePage {}
398
399impl FocusableView for WelcomePage {
400 fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
401 self.focus_handle.clone()
402 }
403}
404
405impl Item for WelcomePage {
406 type Event = ItemEvent;
407
408 fn tab_content_text(&self, _cx: &WindowContext) -> Option<SharedString> {
409 Some("Welcome".into())
410 }
411
412 fn telemetry_event_text(&self) -> Option<&'static str> {
413 Some("welcome page")
414 }
415
416 fn show_toolbar(&self) -> bool {
417 false
418 }
419
420 fn clone_on_split(
421 &self,
422 _workspace_id: Option<WorkspaceId>,
423 cx: &mut ViewContext<Self>,
424 ) -> Option<View<Self>> {
425 Some(cx.new_view(|cx| WelcomePage {
426 focus_handle: cx.focus_handle(),
427 workspace: self.workspace.clone(),
428 telemetry: self.telemetry.clone(),
429 _settings_subscription: cx.observe_global::<SettingsStore>(move |_, cx| cx.notify()),
430 }))
431 }
432
433 fn to_item_events(event: &Self::Event, mut f: impl FnMut(workspace::item::ItemEvent)) {
434 f(*event)
435 }
436}