diff --git a/src/background.ts b/src/background.ts index 00adf091..094a275e 100644 --- a/src/background.ts +++ b/src/background.ts @@ -26,6 +26,7 @@ import { refreshRemoteConfig, } from "./background/remoteConfig"; import { BSPLUS_INSTALL_REGISTERED_AT_KEY } from "@/seqta/utils/remoteConfig/storageKeys"; +import { DEFAULT_BACKGROUND_PENDING_KEY } from "@/seqta/ui/defaultBackground"; import { handleSearchThemeStockPhotos } from "./background/searchThemeStockPhotos"; import { bsplusApiQueryParams, withBsplusApiQuery } from "@/seqta/utils/bsplusClientContext"; import { initTimetableClassmatesBackgroundSync } from "./background/timetableClassmatesSync"; @@ -790,6 +791,7 @@ browser.runtime.onInstalled.addListener(function (event) { if (event.reason === "install") { void refreshRemoteConfig(apiBase); + void browser.storage.local.set({ [DEFAULT_BACKGROUND_PENDING_KEY]: true }); } }); diff --git a/src/declarations.d.ts b/src/declarations.d.ts index bb62e8c0..c1e8d554 100644 --- a/src/declarations.d.ts +++ b/src/declarations.d.ts @@ -3,6 +3,7 @@ declare module "*.webm"; declare module "*.woff"; declare module "*.scss"; declare module "*.png"; +declare module "*.webp"; declare module "*.html"; declare module "*.svelte"; diff --git a/src/plugins/built-in/animatedBackground/index.ts b/src/plugins/built-in/animatedBackground/index.ts index 67ee6add..e91f5721 100644 --- a/src/plugins/built-in/animatedBackground/index.ts +++ b/src/plugins/built-in/animatedBackground/index.ts @@ -37,6 +37,7 @@ const animatedBackgroundPlugin: Plugin = { description: "Adds an animated background to BetterSEQTA+", version: "1.0.0", disableToggle: true, + defaultEnabled: false, styles: styles, settings: instance.settings, diff --git a/src/plugins/core/manager.ts b/src/plugins/core/manager.ts index 76a4cc9c..8d5657c5 100644 --- a/src/plugins/core/manager.ts +++ b/src/plugins/core/manager.ts @@ -553,7 +553,9 @@ export class PluginManager { if (!plugin?.disableToggle) continue; const enabled = - (change.newValue as PluginSettingsStorage)?.enabled ?? true; + (change.newValue as PluginSettingsStorage)?.enabled ?? + plugin.defaultEnabled ?? + true; const wasEnabled = (change.oldValue as PluginSettingsStorage)?.enabled ?? plugin.defaultEnabled ?? diff --git a/src/resources/backgrounds/default-background.webp b/src/resources/backgrounds/default-background.webp new file mode 100644 index 00000000..c38c675f Binary files /dev/null and b/src/resources/backgrounds/default-background.webp differ diff --git a/src/seqta/ui/AddBetterSEQTAElements.ts b/src/seqta/ui/AddBetterSEQTAElements.ts index 92e7da26..b0d4bb92 100644 --- a/src/seqta/ui/AddBetterSEQTAElements.ts +++ b/src/seqta/ui/AddBetterSEQTAElements.ts @@ -10,6 +10,7 @@ import { waitForElm } from "@/seqta/utils/waitForElm"; import { GetThresholdOfColor } from "@/seqta/ui/colors/getThresholdColour"; import { appendBackgroundToUI } from "./ImageBackgrounds"; +import { applyDefaultBackgroundOnce } from "./defaultBackground"; import stringToHTML from "@/seqta/utils/stringToHTML"; import { settingsState } from "@/seqta/utils/listeners/SettingsState"; import { updateAllColors } from "./colors/Manager"; @@ -155,7 +156,12 @@ export async function AddBetterSEQTAElements() { menuList.insertBefore(fragment, menuList.firstChild); try { - await Promise.all([appendBackgroundToUI(), handleUserInfoAndStudentData()]); + await Promise.all([ + applyDefaultBackgroundOnce() + .catch((error) => console.warn("[BetterSEQTA+] Default background failed:", error)) + .then(appendBackgroundToUI), + handleUserInfoAndStudentData(), + ]); } catch (error) { console.error("[BetterSEQTA+] Failed to initialize UI elements:", error); } diff --git a/src/seqta/ui/defaultBackground.test.ts b/src/seqta/ui/defaultBackground.test.ts new file mode 100644 index 00000000..77aa6041 --- /dev/null +++ b/src/seqta/ui/defaultBackground.test.ts @@ -0,0 +1,51 @@ +/** + * @jest-environment jsdom + */ +import browser from "webextension-polyfill"; +import { writeData } from "@/interface/hooks/BackgroundDataLoader"; +import { + applyDefaultBackgroundOnce, + DEFAULT_BACKGROUND_PENDING_KEY, +} from "./defaultBackground"; + +jest.mock("@/resources/backgrounds/default-background.webp", () => "default-background.webp", { + virtual: true, +}); +jest.mock("@/lib/extensionAssetUrl", () => ({ resolveExtensionAssetUrl: (url: string) => url })); +jest.mock("@/interface/hooks/BackgroundDataLoader", () => ({ writeData: jest.fn() })); + +const blob = { size: 1 }; + +beforeEach(() => { + localStorage.clear(); + (writeData as jest.Mock).mockClear(); + globalThis.fetch = jest.fn(async () => ({ blob: async () => blob })) as unknown as typeof fetch; +}); + +it("does nothing without the fresh-install flag", async () => { + await applyDefaultBackgroundOnce(); + + expect(writeData).not.toHaveBeenCalled(); + expect(localStorage.getItem("selectedBackground")).toBeNull(); +}); + +it("installs and selects the bundled background on a fresh install", async () => { + await browser.storage.local.set({ [DEFAULT_BACKGROUND_PENDING_KEY]: true }); + + await applyDefaultBackgroundOnce(); + + expect(writeData).toHaveBeenCalledWith("image-116", "image", blob); + expect(localStorage.getItem("selectedBackground")).toBe("image-116"); + expect(await browser.storage.local.get(DEFAULT_BACKGROUND_PENDING_KEY)).toEqual({}); +}); + +it("keeps a background the user already chose", async () => { + await browser.storage.local.set({ [DEFAULT_BACKGROUND_PENDING_KEY]: true }); + localStorage.setItem("selectedBackground", ""); + + await applyDefaultBackgroundOnce(); + + expect(writeData).not.toHaveBeenCalled(); + expect(localStorage.getItem("selectedBackground")).toBe(""); + expect(await browser.storage.local.get(DEFAULT_BACKGROUND_PENDING_KEY)).toEqual({}); +}); diff --git a/src/seqta/ui/defaultBackground.ts b/src/seqta/ui/defaultBackground.ts new file mode 100644 index 00000000..cde89fb1 --- /dev/null +++ b/src/seqta/ui/defaultBackground.ts @@ -0,0 +1,24 @@ +import browser from "webextension-polyfill"; +import defaultBackgroundUrl from "@/resources/backgrounds/default-background.webp"; +import { resolveExtensionAssetUrl } from "@/lib/extensionAssetUrl"; +import { writeData } from "@/interface/hooks/BackgroundDataLoader"; + +/** Set by the background script on a fresh install; consumed on the first SEQTA page load. */ +export const DEFAULT_BACKGROUND_PENDING_KEY = "defaultBackgroundPending"; + +/** Theme store id of the bundled image, so the store lists it as installed. */ +const DEFAULT_BACKGROUND_ID = "image-116"; + +/** Install and select the bundled background once, unless the user already picked one. */ +export async function applyDefaultBackgroundOnce(): Promise { + const stored = await browser.storage.local.get(DEFAULT_BACKGROUND_PENDING_KEY); + if (!stored[DEFAULT_BACKGROUND_PENDING_KEY]) return; + + if (localStorage.getItem("selectedBackground") === null) { + const response = await fetch(resolveExtensionAssetUrl(defaultBackgroundUrl)); + await writeData(DEFAULT_BACKGROUND_ID, "image", await response.blob()); + localStorage.setItem("selectedBackground", DEFAULT_BACKGROUND_ID); + } + + await browser.storage.local.remove(DEFAULT_BACKGROUND_PENDING_KEY); +} diff --git a/src/seqta/ui/fonts/presets.ts b/src/seqta/ui/fonts/presets.ts index e03b68d1..2f4820a5 100644 --- a/src/seqta/ui/fonts/presets.ts +++ b/src/seqta/ui/fonts/presets.ts @@ -1,4 +1,4 @@ -export const DEFAULT_FONT_ID = "rubik"; +export const DEFAULT_FONT_ID = "inter"; export interface FontPreset { id: string; @@ -9,14 +9,6 @@ export interface FontPreset { } export const FONT_PRESETS: FontPreset[] = [ - { - id: "rubik", - name: "Rubik", - stack: "Rubik, sans-serif", - googleUrl: - "https://fonts.googleapis.com/css2?family=Rubik:wght@400;500;600;700&display=swap", - sample: "Assessment due tomorrow at 3:30pm", - }, { id: "inter", name: "Inter", @@ -25,6 +17,14 @@ export const FONT_PRESETS: FontPreset[] = [ "https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap", sample: "Assessment due tomorrow at 3:30pm", }, + { + id: "rubik", + name: "Rubik", + stack: "Rubik, sans-serif", + googleUrl: + "https://fonts.googleapis.com/css2?family=Rubik:wght@400;500;600;700&display=swap", + sample: "Assessment due tomorrow at 3:30pm", + }, { id: "poppins", name: "Poppins", diff --git a/src/seqta/utils/defaultSettings.ts b/src/seqta/utils/defaultSettings.ts index edc2ef48..c2282cf9 100644 --- a/src/seqta/utils/defaultSettings.ts +++ b/src/seqta/utils/defaultSettings.ts @@ -1,4 +1,5 @@ import type { SettingsState } from "@/types/storage"; +import { DEFAULT_FONT_ID } from "@/seqta/ui/fonts/presets"; function detectLowEndDevice(): boolean { const lowCoreCount = @@ -17,9 +18,9 @@ export function getDefaultSettingsState(): SettingsState { return { onoff: true, settingsPopupExpanded: false, - animatedbk: true, + animatedbk: false, bksliderinput: "50", - transparencyEffects: false, + transparencyEffects: true, lessonalert: true, defaultmenuorder: [], menuitems: { @@ -45,7 +46,7 @@ export function getDefaultSettingsState(): SettingsState { subjectfilters: {}, selectedTheme: "", selectedColor: - "linear-gradient(40deg, rgba(201,61,0,1) 0%, RGBA(170, 5, 58, 1) 100%)", + "linear-gradient(45deg, rgba(18,106,98,1) 0%, rgba(17,88,131,1) 100%)", originalSelectedColor: "", DarkMode: true, animations: !isLowEndDevice, @@ -79,7 +80,7 @@ export function getDefaultSettingsState(): SettingsState { themeOfTheMonthDisabled: false, autoCloudSettingsSync: true, showTitlebarFounderBadge: true, - selectedFont: "rubik", + selectedFont: DEFAULT_FONT_ID, timeFormat: "24", privacyStatementShown: false, bsCloudAutoSyncAnnouncementShown: false, diff --git a/src/types/storage.ts b/src/types/storage.ts index 05ece3e0..01134d9f 100644 --- a/src/types/storage.ts +++ b/src/types/storage.ts @@ -95,7 +95,7 @@ export interface SettingsState { adaptiveThemeColour?: boolean; adaptiveThemeGradient?: boolean; adaptiveThemeColourTransition?: boolean; - /** Google Font preset id for SEQTA interface typography (`rubik` default). */ + /** Google Font preset id for SEQTA interface typography (`inter` default). */ selectedFont?: string; // depreciated keys