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 workspace::{
 15    dock::DockPosition,
 16    item::{Item, ItemEvent},
 17    open_new, AppState, Welcome, Workspace, WorkspaceId,
 18};
 19
 20pub use base_keymap_setting::BaseKeymap;
 21
 22pub const FIRST_OPEN: &str = "first_open";
 23
 24pub fn init(cx: &mut AppContext) {
 25    BaseKeymap::register(cx);
 26
 27    cx.observe_new_views(|workspace: &mut Workspace, _cx| {
 28        workspace.register_action(|workspace, _: &Welcome, cx| {
 29            let welcome_page = cx.build_view(|cx| WelcomePage::new(workspace, cx));
 30            workspace.add_item(Box::new(welcome_page), cx)
 31        });
 32    })
 33    .detach();
 34
 35    base_keymap_picker::init(cx);
 36}
 37
 38pub fn show_welcome_view(app_state: &Arc<AppState>, cx: &mut AppContext) {
 39    open_new(&app_state, cx, |workspace, cx| {
 40        workspace.toggle_dock(DockPosition::Left, cx);
 41        let welcome_page = cx.build_view(|cx| WelcomePage::new(workspace, cx));
 42        workspace.add_item_to_center(Box::new(welcome_page.clone()), cx);
 43        cx.focus_view(&welcome_page);
 44        cx.notify();
 45    })
 46    .detach();
 47
 48    db::write_and_log(cx, || {
 49        KEY_VALUE_STORE.write_kvp(FIRST_OPEN.to_string(), "false".to_string())
 50    });
 51}
 52
 53pub struct WelcomePage {
 54    workspace: WeakView<Workspace>,
 55    focus_handle: FocusHandle,
 56    _settings_subscription: Subscription,
 57}
 58
 59impl Render for WelcomePage {
 60    type Element = Focusable<Div>;
 61
 62    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> Self::Element {
 63        h_stack().full().track_focus(&self.focus_handle).child(
 64            v_stack()
 65                .w_96()
 66                .gap_4()
 67                .mx_auto()
 68                .child(
 69                    svg()
 70                        .path("icons/logo_96.svg")
 71                        .text_color(gpui::white())
 72                        .w(px(96.))
 73                        .h(px(96.))
 74                        .mx_auto(),
 75                )
 76                .child(Label::new("Code at the speed of thought"))
 77                .child(
 78                    v_stack()
 79                        .gap_2()
 80                        .child(
 81                            Button::new("choose-theme", "Choose a theme")
 82                                .full_width()
 83                                .on_click(cx.listener(|this, _, cx| {
 84                                    this.workspace
 85                                        .update(cx, |workspace, cx| {
 86                                            theme_selector::toggle(
 87                                                workspace,
 88                                                &Default::default(),
 89                                                cx,
 90                                            )
 91                                        })
 92                                        .ok();
 93                                })),
 94                        )
 95                        .child(
 96                            Button::new("choose-keymap", "Choose a keymap")
 97                                .full_width()
 98                                .on_click(cx.listener(|this, _, cx| {
 99                                    this.workspace
100                                        .update(cx, |workspace, cx| {
101                                            base_keymap_picker::toggle(
102                                                workspace,
103                                                &Default::default(),
104                                                cx,
105                                            )
106                                        })
107                                        .ok();
108                                })),
109                        )
110                        .child(
111                            Button::new("install-cli", "Install the CLI")
112                                .full_width()
113                                .on_click(cx.listener(|_, _, cx| {
114                                    cx.app_mut()
115                                        .spawn(
116                                            |cx| async move { install_cli::install_cli(&cx).await },
117                                        )
118                                        .detach_and_log_err(cx);
119                                })),
120                        ),
121                )
122                .child(
123                    v_stack()
124                        .p_3()
125                        .gap_2()
126                        .bg(cx.theme().colors().elevated_surface_background)
127                        .border_1()
128                        .border_color(cx.theme().colors().border)
129                        .rounded_md()
130                        .child(
131                            // todo!("vim setting")
132                            h_stack()
133                                .gap_2()
134                                .child(
135                                    Checkbox::new(
136                                        "enable-vim",
137                                        if false
138                                        /* VimSettings::get_global(cx).enabled */
139                                        {
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::<VimSettings>(
148                                    //             selection,
149                                    //             cx,
150                                    //             |settings, value| settings.enabled = 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.build_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}