welcome.rs

  1mod base_keymap_picker;
  2mod base_keymap_setting;
  3
  4use client::TelemetrySettings;
  5use db::kvp::KEY_VALUE_STORE;
  6use gpui::{
  7    svg, AnyElement, AppContext, EventEmitter, FocusHandle, FocusableView, InteractiveElement,
  8    ParentElement, Render, Styled, Subscription, View, ViewContext, VisualContext, WeakView,
  9    WindowContext,
 10};
 11use settings::{Settings, SettingsStore};
 12use std::sync::Arc;
 13use ui::{prelude::*, Checkbox};
 14use vim::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;
 22
 23pub const FIRST_OPEN: &str = "first_open";
 24
 25pub fn init(cx: &mut AppContext) {
 26    BaseKeymap::register(cx);
 27
 28    cx.observe_new_views(|workspace: &mut Workspace, _cx| {
 29        workspace.register_action(|workspace, _: &Welcome, cx| {
 30            let welcome_page = cx.new_view(|cx| WelcomePage::new(workspace, cx));
 31            workspace.add_item(Box::new(welcome_page), cx)
 32        });
 33    })
 34    .detach();
 35
 36    base_keymap_picker::init(cx);
 37}
 38
 39pub fn show_welcome_view(app_state: &Arc<AppState>, cx: &mut AppContext) {
 40    open_new(&app_state, cx, |workspace, cx| {
 41        workspace.toggle_dock(DockPosition::Left, cx);
 42        let welcome_page = cx.new_view(|cx| WelcomePage::new(workspace, cx));
 43        workspace.add_item_to_center(Box::new(welcome_page.clone()), cx);
 44        cx.focus_view(&welcome_page);
 45        cx.notify();
 46    })
 47    .detach();
 48
 49    db::write_and_log(cx, || {
 50        KEY_VALUE_STORE.write_kvp(FIRST_OPEN.to_string(), "false".to_string())
 51    });
 52}
 53
 54pub struct WelcomePage {
 55    workspace: WeakView<Workspace>,
 56    focus_handle: FocusHandle,
 57    _settings_subscription: Subscription,
 58}
 59
 60impl Render for WelcomePage {
 61    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
 62        h_stack().full().track_focus(&self.focus_handle).child(
 63            v_stack()
 64                .w_96()
 65                .gap_4()
 66                .mx_auto()
 67                .child(
 68                    svg()
 69                        .path("icons/logo_96.svg")
 70                        .text_color(gpui::white())
 71                        .w(px(96.))
 72                        .h(px(96.))
 73                        .mx_auto(),
 74                )
 75                .child(
 76                    h_stack()
 77                        .justify_center()
 78                        .child(Label::new("Code at the speed of thought")),
 79                )
 80                .child(
 81                    v_stack()
 82                        .gap_2()
 83                        .child(
 84                            Button::new("choose-theme", "Choose a theme")
 85                                .full_width()
 86                                .on_click(cx.listener(|this, _, cx| {
 87                                    this.workspace
 88                                        .update(cx, |workspace, cx| {
 89                                            theme_selector::toggle(
 90                                                workspace,
 91                                                &Default::default(),
 92                                                cx,
 93                                            )
 94                                        })
 95                                        .ok();
 96                                })),
 97                        )
 98                        .child(
 99                            Button::new("choose-keymap", "Choose a keymap")
100                                .full_width()
101                                .on_click(cx.listener(|this, _, cx| {
102                                    this.workspace
103                                        .update(cx, |workspace, cx| {
104                                            base_keymap_picker::toggle(
105                                                workspace,
106                                                &Default::default(),
107                                                cx,
108                                            )
109                                        })
110                                        .ok();
111                                })),
112                        )
113                        .child(
114                            Button::new("install-cli", "Install the CLI")
115                                .full_width()
116                                .on_click(cx.listener(|_, _, cx| {
117                                    cx.app_mut()
118                                        .spawn(
119                                            |cx| async move { install_cli::install_cli(&cx).await },
120                                        )
121                                        .detach_and_log_err(cx);
122                                })),
123                        ),
124                )
125                .child(
126                    v_stack()
127                        .p_3()
128                        .gap_2()
129                        .bg(cx.theme().colors().elevated_surface_background)
130                        .border_1()
131                        .border_color(cx.theme().colors().border)
132                        .rounded_md()
133                        .child(
134                            h_stack()
135                                .gap_2()
136                                .child(
137                                    Checkbox::new(
138                                        "enable-vim",
139                                        if VimModeSetting::get_global(cx).0 {
140                                            ui::Selection::Selected
141                                        } else {
142                                            ui::Selection::Unselected
143                                        },
144                                    )
145                                    .on_click(cx.listener(
146                                        move |this, selection, cx| {
147                                            this.update_settings::<VimModeSetting>(
148                                                selection,
149                                                cx,
150                                                |setting, value| *setting = Some(value),
151                                            );
152                                        },
153                                    )),
154                                )
155                                .child(Label::new("Enable vim mode")),
156                        )
157                        .child(
158                            h_stack()
159                                .gap_2()
160                                .child(
161                                    Checkbox::new(
162                                        "enable-telemetry",
163                                        if TelemetrySettings::get_global(cx).metrics {
164                                            ui::Selection::Selected
165                                        } else {
166                                            ui::Selection::Unselected
167                                        },
168                                    )
169                                    .on_click(cx.listener(
170                                        move |this, selection, cx| {
171                                            this.update_settings::<TelemetrySettings>(
172                                                selection,
173                                                cx,
174                                                |settings, value| settings.metrics = Some(value),
175                                            );
176                                        },
177                                    )),
178                                )
179                                .child(Label::new("Send anonymous usage data")),
180                        )
181                        .child(
182                            h_stack()
183                                .gap_2()
184                                .child(
185                                    Checkbox::new(
186                                        "enable-crash",
187                                        if TelemetrySettings::get_global(cx).diagnostics {
188                                            ui::Selection::Selected
189                                        } else {
190                                            ui::Selection::Unselected
191                                        },
192                                    )
193                                    .on_click(cx.listener(
194                                        move |this, selection, cx| {
195                                            this.update_settings::<TelemetrySettings>(
196                                                selection,
197                                                cx,
198                                                |settings, value| {
199                                                    settings.diagnostics = Some(value)
200                                                },
201                                            );
202                                        },
203                                    )),
204                                )
205                                .child(Label::new("Send crash reports")),
206                        ),
207                ),
208        )
209    }
210}
211
212impl WelcomePage {
213    pub fn new(workspace: &Workspace, cx: &mut ViewContext<Self>) -> Self {
214        WelcomePage {
215            focus_handle: cx.focus_handle(),
216            workspace: workspace.weak_handle(),
217            _settings_subscription: cx.observe_global::<SettingsStore>(move |_, cx| cx.notify()),
218        }
219    }
220
221    fn update_settings<T: Settings>(
222        &mut self,
223        selection: &Selection,
224        cx: &mut ViewContext<Self>,
225        callback: impl 'static + Send + Fn(&mut T::FileContent, bool),
226    ) {
227        if let Some(workspace) = self.workspace.upgrade() {
228            let fs = workspace.read(cx).app_state().fs.clone();
229            let selection = *selection;
230            settings::update_settings_file::<T>(fs, cx, move |settings| {
231                let value = match selection {
232                    Selection::Unselected => false,
233                    Selection::Selected => true,
234                    _ => return,
235                };
236
237                callback(settings, value)
238            });
239        }
240    }
241}
242
243impl EventEmitter<ItemEvent> for WelcomePage {}
244
245impl FocusableView for WelcomePage {
246    fn focus_handle(&self, _: &AppContext) -> gpui::FocusHandle {
247        self.focus_handle.clone()
248    }
249}
250
251impl Item for WelcomePage {
252    type Event = ItemEvent;
253
254    fn tab_content(&self, _: Option<usize>, selected: bool, _: &WindowContext) -> AnyElement {
255        Label::new("Welcome to Zed!")
256            .color(if selected {
257                Color::Default
258            } else {
259                Color::Muted
260            })
261            .into_any_element()
262    }
263
264    fn show_toolbar(&self) -> bool {
265        false
266    }
267
268    fn clone_on_split(
269        &self,
270        _workspace_id: WorkspaceId,
271        cx: &mut ViewContext<Self>,
272    ) -> Option<View<Self>> {
273        Some(cx.new_view(|cx| WelcomePage {
274            focus_handle: cx.focus_handle(),
275            workspace: self.workspace.clone(),
276            _settings_subscription: cx.observe_global::<SettingsStore>(move |_, cx| cx.notify()),
277        }))
278    }
279
280    fn to_item_events(event: &Self::Event, mut f: impl FnMut(workspace::item::ItemEvent)) {
281        f(*event)
282    }
283}