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 util::ResultExt;
16use vim_mode_setting::VimModeSetting;
17use workspace::{
18 AppState, Welcome, Workspace, WorkspaceId,
19 dock::DockPosition,
20 item::{Item, ItemEvent},
21 open_new,
22};
23
24pub use base_keymap_setting::BaseKeymap;
25pub use multibuffer_hint::*;
26
27actions!(welcome, [ResetHints]);
28
29pub const FIRST_OPEN: &str = "first_open";
30pub const DOCS_URL: &str = "https://zed.dev/docs/";
31const BOOK_ONBOARDING: &str = "https://dub.sh/zed-c-onboarding";
32
33pub fn init(cx: &mut App) {
34 BaseKeymap::register(cx);
35
36 cx.observe_new(|workspace: &mut Workspace, _, _cx| {
37 workspace.register_action(|workspace, _: &Welcome, window, cx| {
38 let welcome_page = WelcomePage::new(workspace, cx);
39 workspace.add_item_to_active_pane(Box::new(welcome_page), None, true, window, cx)
40 });
41 workspace
42 .register_action(|_workspace, _: &ResetHints, _, cx| MultibufferHint::set_count(0, cx));
43 })
44 .detach();
45
46 base_keymap_picker::init(cx);
47}
48
49pub fn show_welcome_view(app_state: Arc<AppState>, cx: &mut App) -> Task<anyhow::Result<()>> {
50 open_new(
51 Default::default(),
52 app_state,
53 cx,
54 |workspace, window, cx| {
55 workspace.toggle_dock(DockPosition::Left, window, cx);
56 let welcome_page = WelcomePage::new(workspace, cx);
57 workspace.add_item_to_center(Box::new(welcome_page.clone()), window, cx);
58
59 window.focus(&welcome_page.focus_handle(cx));
60
61 cx.notify();
62
63 db::write_and_log(cx, || {
64 KEY_VALUE_STORE.write_kvp(FIRST_OPEN.to_string(), "false".to_string())
65 });
66 },
67 )
68}
69
70pub struct WelcomePage {
71 workspace: WeakEntity<Workspace>,
72 focus_handle: FocusHandle,
73 telemetry: Arc<Telemetry>,
74 _settings_subscription: Subscription,
75}
76
77impl Render for WelcomePage {
78 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
79 let edit_prediction_provider_is_zed =
80 all_language_settings(None, cx).edit_predictions.provider
81 == EditPredictionProvider::Zed;
82
83 let edit_prediction_label = if edit_prediction_provider_is_zed {
84 "Edit Prediction Enabled"
85 } else {
86 "Try Edit Prediction"
87 };
88
89 h_flex()
90 .size_full()
91 .bg(cx.theme().colors().editor_background)
92 .key_context("Welcome")
93 .track_focus(&self.focus_handle(cx))
94 .child(
95 v_flex()
96 .gap_8()
97 .mx_auto()
98 .child(
99 v_flex()
100 .w_full()
101 .child(
102 svg()
103 .path("icons/logo_96.svg")
104 .text_color(cx.theme().colors().icon_disabled)
105 .w(px(40.))
106 .h(px(40.))
107 .mx_auto()
108 .mb_4(),
109 )
110 .child(
111 h_flex()
112 .w_full()
113 .justify_center()
114 .child(Headline::new("Welcome to Zed")),
115 )
116 .child(
117 h_flex().w_full().justify_center().child(
118 Label::new("The editor for what's next")
119 .color(Color::Muted)
120 .italic(),
121 ),
122 ),
123 )
124 .child(
125 h_flex()
126 .items_start()
127 .gap_8()
128 .child(
129 v_flex()
130 .gap_2()
131 .pr_8()
132 .border_r_1()
133 .border_color(cx.theme().colors().border_variant)
134 .child(
135 self.section_label( cx).child(
136 Label::new("Get Started")
137 .size(LabelSize::XSmall)
138 .color(Color::Muted),
139 ),
140 )
141 .child(
142 Button::new("choose-theme", "Choose a Theme")
143 .icon(IconName::SwatchBook)
144 .icon_size(IconSize::XSmall)
145 .icon_color(Color::Muted)
146 .icon_position(IconPosition::Start)
147 .on_click(cx.listener(|this, _, window, cx| {
148 telemetry::event!("Welcome Theme Changed");
149 this.workspace
150 .update(cx, |_workspace, cx| {
151 window.dispatch_action(zed_actions::theme_selector::Toggle::default().boxed_clone(), cx);
152 })
153 .ok();
154 })),
155 )
156 .child(
157 Button::new("choose-keymap", "Choose a Keymap")
158 .icon(IconName::Keyboard)
159 .icon_size(IconSize::XSmall)
160 .icon_color(Color::Muted)
161 .icon_position(IconPosition::Start)
162 .on_click(cx.listener(|this, _, window, cx| {
163 telemetry::event!("Welcome Keymap Changed");
164 this.workspace
165 .update(cx, |workspace, cx| {
166 base_keymap_picker::toggle(
167 workspace,
168 &Default::default(),
169 window, cx,
170 )
171 })
172 .ok();
173 })),
174 )
175 .child(
176 Button::new(
177 "try-zed-edit-prediction",
178 edit_prediction_label,
179 )
180 .disabled(edit_prediction_provider_is_zed)
181 .icon(IconName::ZedPredict)
182 .icon_size(IconSize::XSmall)
183 .icon_color(Color::Muted)
184 .icon_position(IconPosition::Start)
185 .on_click(
186 cx.listener(|_, _, window, cx| {
187 telemetry::event!("Welcome Screen Try Edit Prediction clicked");
188 window.dispatch_action(zed_actions::OpenZedPredictOnboarding.boxed_clone(), cx);
189 }),
190 ),
191 )
192 .child(
193 Button::new("edit settings", "Edit Settings")
194 .icon(IconName::Settings)
195 .icon_size(IconSize::XSmall)
196 .icon_color(Color::Muted)
197 .icon_position(IconPosition::Start)
198 .on_click(cx.listener(|_, _, window, cx| {
199 telemetry::event!("Welcome Settings Edited");
200 window.dispatch_action(Box::new(
201 zed_actions::OpenSettings,
202 ), cx);
203 })),
204 ),
205 )
206 .child(
207 v_flex()
208 .gap_2()
209 .child(
210 self.section_label(cx).child(
211 Label::new("Resources")
212 .size(LabelSize::XSmall)
213 .color(Color::Muted),
214 ),
215 )
216 .when(cfg!(target_os = "macos"), |el| {
217 el.child(
218 Button::new("install-cli", "Install the CLI")
219 .icon(IconName::Terminal)
220 .icon_size(IconSize::XSmall)
221 .icon_color(Color::Muted)
222 .icon_position(IconPosition::Start)
223 .on_click(cx.listener(|this, _, window, cx| {
224 telemetry::event!("Welcome CLI Installed");
225 this.workspace.update(cx, |_, cx|{
226 install_cli::install_cli(window, cx);
227 }).log_err();
228 })),
229 )
230 })
231 .child(
232 Button::new("view-docs", "View Documentation")
233 .icon(IconName::FileCode)
234 .icon_size(IconSize::XSmall)
235 .icon_color(Color::Muted)
236 .icon_position(IconPosition::Start)
237 .on_click(cx.listener(|_, _, _, cx| {
238 telemetry::event!("Welcome Documentation Viewed");
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(|_, _, window, cx| {
249 telemetry::event!("Welcome Extensions Page Opened");
250 window.dispatch_action(Box::new(
251 zed_actions::Extensions::default(),
252 ), cx);
253 })),
254 )
255 .child(
256 Button::new("book-onboarding", "Book Onboarding")
257 .icon(IconName::PhoneIncoming)
258 .icon_size(IconSize::XSmall)
259 .icon_color(Color::Muted)
260 .icon_position(IconPosition::Start)
261 .on_click(cx.listener(|_, _, _, cx| {
262 cx.open_url(BOOK_ONBOARDING);
263 })),
264 ),
265 ),
266 )
267 .child(
268 v_container()
269 .px_2()
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}