diff --git a/src/interface/components/store/ThemeCard.svelte b/src/interface/components/store/ThemeCard.svelte index bae0d58d..37b0ab3f 100644 --- a/src/interface/components/store/ThemeCard.svelte +++ b/src/interface/components/store/ThemeCard.svelte @@ -4,9 +4,10 @@ masterGridDisplayDownloadCount, gridCardPreviewImageUrls, } from '@/interface/utils/themeStoreFlavours' + import { isStoreThemeInstalled } from '@/interface/utils/themeListFilters' import emblaCarouselSvelte from 'embla-carousel-svelte'; import Autoplay from 'embla-carousel-autoplay'; - let { theme, onClick, toggleFavorite, isLoggedIn, onRequestSignIn, allStoreThemeRows } = $props<{ + let { theme, onClick, toggleFavorite, isLoggedIn, onRequestSignIn, allStoreThemeRows, installedThemeIds = [] } = $props<{ theme: Theme; onClick: () => void; toggleFavorite: (theme: Theme) => void; @@ -14,6 +15,7 @@ onRequestSignIn?: () => void; /** Raw API themes (includes hidden slaves) for aggregated master download totals */ allStoreThemeRows?: Theme[]; + installedThemeIds?: string[]; }>(); const displayDownloadCount = $derived( @@ -47,6 +49,8 @@ ]; }); + const isInstalled = $derived(isStoreThemeInstalled(theme, installedThemeIds)); + function handleCardClick(e: MouseEvent) { if ((e.target as HTMLElement).closest('[data-theme-favorite]')) return; onClick(); @@ -85,6 +89,19 @@ {/if} + {#if isInstalled} +
+ + + + + Installed + +
+ {/if} +{/if} + + + + {:else} +{/if} + + + + {:else} +{:else} + +{/if} - - + {#if listMode === 'custom'} +

+ No custom themes yet +

+

+ Create your own theme or follow our guide to get started. +

+
+ +
+ {:else if listMode === 'downloaded'} +

+ No downloaded themes +

+

+ Browse the theme store to find and install themes. +

+
+ +
+ {:else} +

+ No themes installed +

+

+ Install a theme from the store or create one of your own. +

+
+ + +
+ {/if} {/if} - {#if showNavigation && themes && themes.themes.length > 0} + {#if themes && ((showNavigation && visibleThemes.length > 0) || (listMode === 'custom' && visibleThemes.length > 0) || (listMode === 'downloaded' && visibleThemes.length > 0))}
- + {#if listMode === 'custom'} + - + +
+
{'\uecb3'}
+
+ Got a Theme Idea? +

Transform it into a stunning theme!

+
+
+
+ {:else if listMode === 'downloaded'} + + {:else} + + + + {/if} {/if} diff --git a/src/interface/pages/settings.svelte b/src/interface/pages/settings.svelte index 103709a3..6045e8b3 100644 --- a/src/interface/pages/settings.svelte +++ b/src/interface/pages/settings.svelte @@ -39,7 +39,7 @@ } from "@/utils/githubReleaseUpdate"; type PageId = "settings" | "themes" | "backgrounds"; type StoreTab = "themes" | "backgrounds"; - type ThemeView = "theme-settings" | "theme-store"; + type ThemeView = "theme-settings" | "theme-store" | "create-theme"; type BackgroundView = "background-settings" | "background-store"; type NavItem = { @@ -94,8 +94,8 @@ label: "Themes", items: [ { id: "theme-store", label: "Store" }, - { id: "theme-settings", label: "Theme settings" }, - { id: "create-theme", label: "Create theme" }, + { id: "theme-settings", label: "Downloaded themes" }, + { id: "create-theme", label: "Custom themes" }, ], }, ]; @@ -142,11 +142,21 @@ ); const sectionTitle = $derived.by(() => { - if (activePage === "themes") return "Themes"; + if (activePage === "themes") { + if (activeThemeView === "theme-settings") return "Downloaded themes"; + if (activeThemeView === "create-theme") return "Custom themes"; + return "Themes"; + } if (activePage === "backgrounds") return "Backgrounds"; return [...userNav, ...appNav].find((item) => item.id === activeSection)?.label ?? "Settings"; }); + const activeThemeListMode = $derived.by(() => { + if (activeThemeView === "create-theme") return "custom" as const; + if (activeThemeView === "theme-settings") return "downloaded" as const; + return "all" as const; + }); + const isStoreView = $derived( (activePage === "themes" && activeThemeView === "theme-store") || (activePage === "backgrounds" && activeBackgroundView === "background-store"), @@ -256,11 +266,7 @@ } if (activePage === "themes") { - if (id === "create-theme") { - void openThemeCreator(); - } else { - activeThemeView = id as ThemeView; - } + activeThemeView = id as ThemeView; return; } @@ -272,18 +278,17 @@ } }; - const openThemeCreator = async () => { - const { OpenThemeCreator } = await import("@/plugins/built-in/themes/ThemeCreator"); - OpenThemeCreator(); - closeExtensionPopup(); - }; - const applyDestination = (destination: SettingsDestination) => { activePage = destination.page; if (destination.page === "settings" && destination.section) { activeSection = destination.section; } else if (destination.page === "themes" && destination.view) { - activeThemeView = destination.view === "store" ? "theme-store" : "theme-settings"; + activeThemeView = + destination.view === "store" + ? "theme-store" + : destination.view === "custom" + ? "create-theme" + : "theme-settings"; } else if (destination.page === "backgrounds" && destination.view) { activeBackgroundView = destination.view === "store" ? "background-store" : "background-settings"; } @@ -467,7 +472,7 @@ /> {/if} {:else if activePage === "themes"} - + {:else} {/if} diff --git a/src/interface/pages/settings/theme.svelte b/src/interface/pages/settings/theme.svelte index 74be1d73..fc54f727 100644 --- a/src/interface/pages/settings/theme.svelte +++ b/src/interface/pages/settings/theme.svelte @@ -2,9 +2,11 @@ import BackgroundSelector from "@/interface/components/themes/BackgroundSelector.svelte" import ThemeSelector from "@/interface/components/themes/ThemeSelector.svelte" import { standalone } from "@/interface/utils/standalone.svelte" + import type { ThemeListMode } from "@/interface/utils/themeListFilters" - let { section = "all" } = $props<{ + let { section = "all", listMode = "all" } = $props<{ section?: "all" | "themes" | "backgrounds"; + listMode?: ThemeListMode; }>(); // backgrounds @@ -41,7 +43,7 @@ {/if} {#if showThemes} - + {/if} {:else} diff --git a/src/interface/pages/store.svelte b/src/interface/pages/store.svelte index 6713d253..3c2531b0 100644 --- a/src/interface/pages/store.svelte +++ b/src/interface/pages/store.svelte @@ -53,6 +53,7 @@ let loading = $state(true); let displayTheme = $state(null); let currentThemes = $state([]); + let selectedThemeId = $state(''); let error = $state(null); let fetchAttempt = $state(0); @@ -74,6 +75,7 @@ const fetchCurrentThemes = async () => { const themes = await themeManager.getAvailableThemes(); currentThemes = themes.filter(theme => theme !== null).map(theme => theme.id); + selectedThemeId = themeManager.getSelectedThemeId() || ''; }; const setDisplayTheme = (theme: Theme | null) => { @@ -174,6 +176,7 @@ // On mount onMount(async () => { window.addEventListener('bsplus:highlight-theme', onHighlightThemeEvent); + themeUpdates.addListener(fetchCurrentThemes); await fetchThemes(); await fetchCurrentThemes(); @@ -183,6 +186,7 @@ return () => { window.removeEventListener('bsplus:highlight-theme', onHighlightThemeEvent); + themeUpdates.removeListener(fetchCurrentThemes); }; }); @@ -219,6 +223,13 @@ await fetchCurrentThemes(); } + async function applyThemeFromStore(themeId: string) { + await themeManager.setTheme(themeId); + selectedThemeId = themeId; + themeUpdates.triggerUpdate(); + void browser.runtime.sendMessage({ type: 'cloudSettingsRequestDebouncedUpload' }).catch(() => {}); + } + $effect(() => { loadBackground(); selectedBackground @@ -287,6 +298,7 @@ {toggleFavorite} isLoggedIn={cloudLoggedIn} onRequestSignIn={() => (showSignInOverlay = true)} + installedThemeIds={currentThemes} /> {#if displayTheme} @@ -300,6 +312,7 @@ {toggleFavorite} isLoggedIn={cloudLoggedIn} onRequestSignIn={() => (showSignInOverlay = true)} + {selectedThemeId} onInstall={async (themeId: string) => { if (displayTheme) await installThemeFromStore(themeId, displayTheme); }} @@ -307,6 +320,9 @@ console.debug('deleting theme', themeId); await removeThemeFromStore(themeId); }} + onApply={async (themeId: string) => { + await applyThemeFromStore(themeId); + }} /> {/if} {:else if activeTab === 'backgrounds'} diff --git a/src/interface/utils/themeListFilters.test.ts b/src/interface/utils/themeListFilters.test.ts new file mode 100644 index 00000000..1167b79d --- /dev/null +++ b/src/interface/utils/themeListFilters.test.ts @@ -0,0 +1,68 @@ +import type { CustomTheme } from '@/types/CustomThemes'; +import type { Theme } from '@/interface/types/Theme'; +import { filterThemesByMode, isStoreThemeInstalled } from './themeListFilters'; + +function makeCustomTheme(overrides: Partial & Pick): CustomTheme { + return { + description: '', + defaultColour: '#000000', + CanChangeColour: true, + allowBackgrounds: true, + CustomCSS: '', + CustomImages: [], + coverImage: null, + isEditable: false, + hideThemeName: false, + ...overrides, + }; +} + +function makeStoreTheme(overrides: Partial & Pick): Theme { + return { + description: '', + coverImage: '', + ...overrides, + }; +} + +describe('filterThemesByMode', () => { + const downloaded = makeCustomTheme({ id: 'store-1', name: 'Store Theme', isEditable: false }); + const custom = makeCustomTheme({ id: 'custom-1', name: 'Custom Theme', isEditable: true }); + + it('returns all themes in all mode', () => { + expect(filterThemesByMode([downloaded, custom], 'all')).toEqual([downloaded, custom]); + }); + + it('returns only non-editable themes in downloaded mode', () => { + expect(filterThemesByMode([downloaded, custom], 'downloaded')).toEqual([downloaded]); + }); + + it('returns only editable themes in custom mode', () => { + expect(filterThemesByMode([downloaded, custom], 'custom')).toEqual([custom]); + }); +}); + +describe('isStoreThemeInstalled', () => { + it('returns true when master id is installed', () => { + const theme = makeStoreTheme({ id: 'master-1', name: 'Master' }); + expect(isStoreThemeInstalled(theme, ['master-1'])).toBe(true); + }); + + it('returns true when a flavour id is installed', () => { + const theme = makeStoreTheme({ + id: 'master-1', + name: 'Master', + flavours: [{ id: 'flavour-1', name: 'Dark', accent_color: '#000', cover_image: '' }], + }); + expect(isStoreThemeInstalled(theme, ['flavour-1'])).toBe(true); + }); + + it('returns false when neither master nor flavours are installed', () => { + const theme = makeStoreTheme({ + id: 'master-1', + name: 'Master', + flavours: [{ id: 'flavour-1', name: 'Dark', accent_color: '#000', cover_image: '' }], + }); + expect(isStoreThemeInstalled(theme, ['other-theme'])).toBe(false); + }); +}); diff --git a/src/interface/utils/themeListFilters.ts b/src/interface/utils/themeListFilters.ts new file mode 100644 index 00000000..1ff72799 --- /dev/null +++ b/src/interface/utils/themeListFilters.ts @@ -0,0 +1,16 @@ +import type { CustomTheme } from '@/types/CustomThemes'; +import type { Theme } from '@/interface/types/Theme'; + +export type ThemeListMode = 'downloaded' | 'custom' | 'all'; + +export function filterThemesByMode(themes: CustomTheme[], mode: ThemeListMode): CustomTheme[] { + if (mode === 'all') return themes; + if (mode === 'custom') return themes.filter((theme) => theme.isEditable === true); + return themes.filter((theme) => theme.isEditable !== true); +} + +export function isStoreThemeInstalled(theme: Theme, installedIds: string[]): boolean { + const installed = new Set(installedIds); + if (installed.has(theme.id)) return true; + return (theme.flavours ?? []).some((flavour) => installed.has(flavour.id)); +} diff --git a/src/seqta/utils/settingsNavigation.ts b/src/seqta/utils/settingsNavigation.ts index 8290d9ab..9094bd09 100644 --- a/src/seqta/utils/settingsNavigation.ts +++ b/src/seqta/utils/settingsNavigation.ts @@ -1,6 +1,6 @@ export type SettingsDestination = | { page: "settings"; section?: string } - | { page: "themes" | "backgrounds"; view?: "settings" | "store" }; + | { page: "themes" | "backgrounds"; view?: "settings" | "store" | "custom" }; export const SETTINGS_NAVIGATION_EVENT = "bsplus:navigate-settings";