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