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