Skip to main content

subspace_shell/settings/
extensions.rs

1//! Product-composed Settings pages. Each retained view owns its operations.
2
3use gpui_kit::{AnyView, App, Global, Window};
4use subspace_lucide::IconName;
5
6/// A product capability's Application destination in Settings.
7#[derive(Clone, Copy)]
8#[non_exhaustive]
9pub struct SettingsExtension {
10    pub id: &'static str,
11    pub title: &'static str,
12    pub icon: IconName,
13    pub create: fn(&mut Window, &mut App) -> AnyView,
14    github_credentials: bool,
15}
16
17impl SettingsExtension {
18    pub const fn new(
19        id: &'static str,
20        title: &'static str,
21        icon: IconName,
22        create: fn(&mut Window, &mut App) -> AnyView,
23    ) -> Self {
24        Self {
25            id,
26            title,
27            icon,
28            create,
29            github_credentials: false,
30        }
31    }
32
33    /// Compose the shared GitHub account and token draft above this view.
34    // qual:api
35    pub const fn with_github_credentials(mut self) -> Self {
36        self.github_credentials = true;
37        self
38    }
39
40    pub const fn has_github_credentials(&self) -> bool {
41        self.github_credentials
42    }
43}
44
45pub(crate) struct SettingsExtensions(pub &'static [SettingsExtension]);
46impl Global for SettingsExtensions {}
47
48pub(super) fn registrations(cx: &App) -> &'static [SettingsExtension] {
49    cx.try_global::<SettingsExtensions>()
50        .map_or(&[], |extensions| extensions.0)
51}
52
53pub(super) fn create_views(window: &mut Window, cx: &mut App) -> Vec<(&'static str, AnyView)> {
54    registrations(cx)
55        .iter()
56        .map(|extension| (extension.id, (extension.create)(window, cx)))
57        .collect()
58}
59
60#[cfg(test)]
61mod tests {
62    use super::{SettingsExtension, SettingsExtensions};
63    use gpui_kit::{
64        AnyView, App, AppContext as _, Context, Global, IntoElement, Render, TestAppContext,
65        Window, div,
66    };
67    struct Extension;
68    struct Creates(usize);
69    impl Global for Creates {}
70    impl Render for Extension {
71        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
72            div()
73        }
74    }
75    fn create(_: &mut Window, cx: &mut App) -> AnyView {
76        cx.global_mut::<Creates>().0 += 1;
77        cx.new(|_| Extension).into()
78    }
79    const EXTENSIONS: &[SettingsExtension] = &[SettingsExtension::new(
80        "fixture",
81        "Fixture",
82        subspace_lucide::IconName::Settings,
83        create,
84    )];
85
86    #[test]
87    fn github_credentials_require_explicit_extension_opt_in() {
88        assert!(!EXTENSIONS[0].has_github_credentials());
89        assert!(
90            EXTENSIONS[0]
91                .with_github_credentials()
92                .has_github_credentials()
93        );
94    }
95
96    #[gpui_kit::test]
97    fn application_extensions_are_retained_and_absent_products_need_no_extension(
98        cx: &mut TestAppContext,
99    ) {
100        let directory = tempfile::tempdir().unwrap();
101        cx.update(|cx| {
102            cx.set_global(Creates(0));
103            cx.set_global(SettingsExtensions(EXTENSIONS));
104        });
105        let settings = crate::settings::preview_tests::open_settings(directory.path(), cx);
106        settings
107            .update(cx, |settings, window, cx| {
108                assert_eq!(cx.global::<Creates>().0, 1);
109                assert!(
110                    settings
111                        .navigation
112                        .contains(&crate::settings::SettingsPage::Extension("fixture"))
113                );
114                let view = settings.extensions[0].1.clone();
115                settings.reconcile_boards(window, cx);
116                assert_eq!(settings.extensions[0].1.entity_id(), view.entity_id());
117                assert_eq!(cx.global::<Creates>().0, 1);
118                settings.restore_preview(window, cx).unwrap();
119            })
120            .unwrap();
121        cx.update(|cx| {
122            cx.set_global(SettingsExtensions(&[]));
123            assert!(super::registrations(cx).is_empty());
124        });
125    }
126}