diff --git a/app/composables/scene_style_preference.ts b/app/composables/scene_style_preference.ts new file mode 100644 index 000000000..d5ab15f94 --- /dev/null +++ b/app/composables/scene_style_preference.ts @@ -0,0 +1,46 @@ +import { defineUserSetting, useUserSetting } from "@ogw_front/composables/user_setting"; +import type { Ref } from "vue"; +import type { SceneStyle } from "@ogw_internal/stores/data_style/types"; +import { z } from "zod"; + +const MAX_CHANNEL = 255; + +// The scene style the user last chose in the scene menu, without its id. Empty until a first choice: +// The app's default gives each missing value +type SceneStylePreference = Partial>; + +const colorSchema = z.object({ + red: z.number().int().min(0).max(MAX_CHANNEL), + green: z.number().int().min(0).max(MAX_CHANNEL), + blue: z.number().int().min(0).max(MAX_CHANNEL), + alpha: z.number().min(0).max(1), +}); + +const sceneStylePreferenceSchema = z.object({ + background_color: colorSchema.optional(), + text: z + .object({ + color: colorSchema, + font_family: z.enum(["arial", "courier", "times"]), + }) + .optional(), + orientation_widget: z.enum(["axes", "compass"]).optional(), +}); + +const sceneStyleSetting = defineUserSetting({ + key: "sceneStyle", + schema: sceneStylePreferenceSchema, + fallback: {}, +}); + +let preference: Ref | undefined = undefined; + +// The starting scene style of a new or reset scene, kept across launches (user settings). +// A loaded project still shows its own scene style +function useSceneStylePreference(): Ref { + preference ??= useUserSetting(sceneStyleSetting); + return preference; +} + +export type { SceneStylePreference }; +export { sceneStylePreferenceSchema, useSceneStylePreference }; diff --git a/app/composables/viewer_reset.ts b/app/composables/viewer_reset.ts index 7f76b98d9..75f9f466d 100644 --- a/app/composables/viewer_reset.ts +++ b/app/composables/viewer_reset.ts @@ -8,26 +8,29 @@ import viewer_schemas from "@geode/opengeodeweb-viewer/opengeodeweb_viewer_typed import { getDefaultSceneStyle, getSceneColors } from "@ogw_front/utils/default_styles"; import { hideViewerMarker, + isOrientationOverlayEnabled, resetOrientationMarkerVisibility, } from "@ogw_front/composables/orientation_overlay"; +import { useSceneStylePreference } from "@ogw_front/composables/scene_style_preference"; // The scene colors of the viewer have no alpha (the text one takes it as its opacity) function toViewerColor({ red, green, blue }: ColorRGBA): ColorRGB { return { red, green, blue }; } -// A reset viewer draws its own scene colors: give it the app's (`ogwFront.sceneColors`), which the default -// Scene style shows -async function applyAppSceneColors(request: ViewerRequest): Promise { +// A reset viewer draws its own scene: give it the default scene style, which the scene style store shows. +// It is the style the user chose (user settings), else the app's colors (`ogwFront.sceneColors`) +async function applyDefaultSceneStyle(request: ViewerRequest): Promise { const sceneColors = getSceneColors(); - const { background_color, text } = getDefaultSceneStyle(); - if (sceneColors.background) { + const preference = useSceneStylePreference().value; + const { background_color, text, orientation_widget } = getDefaultSceneStyle(); + if (preference.background_color ?? sceneColors.background) { await request({ schema: viewer_schemas.opengeodeweb_viewer.viewer.set_background_color, params: { color: toViewerColor(background_color) }, }); } - if (sceneColors.text) { + if (preference.text ?? sceneColors.text) { await request({ schema: viewer_schemas.opengeodeweb_viewer.viewer.set_text_style, params: { @@ -37,6 +40,13 @@ async function applyAppSceneColors(request: ViewerRequest): Promise { }, }); } + // With the overlay, the front draws the marker: the viewer one is hidden afterwards + if (preference.orientation_widget && !isOrientationOverlayEnabled()) { + await request({ + schema: viewer_schemas.opengeodeweb_viewer.viewer.set_orientation_widget, + params: { marker: orientation_widget }, + }); + } } // To call after each viewer reset (reset_visualization), which brings back the viewer's own scene. Neither @@ -44,9 +54,9 @@ async function applyAppSceneColors(request: ViewerRequest): Promise { async function afterViewerReset(request: ViewerRequest): Promise { resetOrientationMarkerVisibility(); try { - await applyAppSceneColors(request); + await applyDefaultSceneStyle(request); } catch (error) { - consola.warn("[SceneColors] The viewer kept its own scene colors:", error); + consola.warn("[SceneStyle] The viewer kept its own scene style:", error); } await hideViewerMarker(request); } diff --git a/app/utils/default_styles/index.ts b/app/utils/default_styles/index.ts index 98f19bc9c..931d71af5 100644 --- a/app/utils/default_styles/index.ts +++ b/app/utils/default_styles/index.ts @@ -24,6 +24,7 @@ import type { ColorRGBA } from "@geode/opengeodeweb-viewer/schema_types"; import type { GeodeObjectType } from "@geode/opengeodeweb-microservice/data_types"; import type { SceneStyle } from "@ogw_internal/stores/data_style/types"; import { imageDefaultStyle } from "./images"; +import { useSceneStylePreference } from "@ogw_front/composables/scene_style_preference"; function default_styles(): Partial> { return { @@ -77,14 +78,20 @@ function getSceneColors(): Readonly { return appSceneColors; } +// The scene style the user chose (user settings) wins over the app's scene colors function getDefaultSceneStyle(): SceneStyle { - const background = appSceneColors.background ?? SCENE_DEFAULT_BACKGROUND_COLOR; - const text = appSceneColors.text ?? SCENE_DEFAULT_TEXT_COLOR; + const preference = useSceneStylePreference().value; + const background = + preference.background_color ?? appSceneColors.background ?? SCENE_DEFAULT_BACKGROUND_COLOR; + const text = preference.text ?? { + color: appSceneColors.text ?? SCENE_DEFAULT_TEXT_COLOR, + font_family: SCENE_DEFAULT_FONT_FAMILY, + }; return { id: "scene", background_color: { ...background }, - text: { color: { ...text }, font_family: SCENE_DEFAULT_FONT_FAMILY }, - orientation_widget: SCENE_DEFAULT_ORIENTATION_WIDGET, + text: { color: { ...text.color }, font_family: text.font_family }, + orientation_widget: preference.orientation_widget ?? SCENE_DEFAULT_ORIENTATION_WIDGET, }; } diff --git a/internal/stores/data_style/scene.ts b/internal/stores/data_style/scene.ts index 13546bb2b..465e83271 100644 --- a/internal/stores/data_style/scene.ts +++ b/internal/stores/data_style/scene.ts @@ -14,6 +14,7 @@ import { sceneStyleDb } from "@ogw_internal/database/accessors"; import { toViewerColor } from "@ogw_front/composables/viewer_reset"; import { useDataStyleState } from "./state"; import { useHybridViewerStore } from "@ogw_front/stores/hybrid_viewer"; +import { useSceneStylePreference } from "@ogw_front/composables/scene_style_preference"; import { useViewerStore } from "@ogw_front/stores/viewer"; // Local constants @@ -33,6 +34,7 @@ export function useSceneStyle(): { const hybridViewerStore = useHybridViewerStore(); const { sceneStyle } = useDataStyleState(); const sceneStyleTable = sceneStyleDb(); + const preference = useSceneStylePreference(); async function saveSceneStyle(values: Partial>): Promise { // Values may hold nested reactive proxies (e.g. an exported snapshot), which IndexedDB cannot clone @@ -85,30 +87,48 @@ export function useSceneStyle(): { } } - async function setSceneBackgroundColor(color: ColorRGBA): Promise { + // A scene choice of the user becomes the starting scene style of the next scenes (user settings) + function keepAsPreference(): void { + const { background_color, text, orientation_widget } = sceneStyle.value; + preference.value = { + background_color: { ...background_color }, + text: { color: { ...text.color }, font_family: text.font_family }, + orientation_widget, + }; + } + + async function applyBackgroundColor(color: ColorRGBA): Promise { await hybridViewerStore.setBackgroundColor(toViewerColor(color)); await saveSceneStyle({ background_color: color }); } + async function setSceneBackgroundColor(color: ColorRGBA): Promise { + await applyBackgroundColor(color); + keepAsPreference(); + } + async function setSceneTextColor(color: ColorRGBA): Promise { const result = await requestTextStyle({ ...sceneStyle.value.text, color }); + keepAsPreference(); return result; } async function setSceneFontFamily(font_family: SceneFontFamily): Promise { const result = await requestTextStyle({ ...sceneStyle.value.text, font_family }); + keepAsPreference(); return result; } async function setSceneOrientationWidget(marker: SceneOrientationWidget): Promise { const result = await requestOrientationWidget(marker); + keepAsPreference(); return result; } async function applySceneStyle(): Promise { const { background_color, text, orientation_widget } = sceneStyle.value; await Promise.all([ - setSceneBackgroundColor(background_color), + applyBackgroundColor(background_color), requestTextStyle(text), requestOrientationWidget(orientation_widget), ]); diff --git a/tests/unit/composables/scene_style_preference.nuxt.test.ts b/tests/unit/composables/scene_style_preference.nuxt.test.ts new file mode 100644 index 000000000..d91bdd549 --- /dev/null +++ b/tests/unit/composables/scene_style_preference.nuxt.test.ts @@ -0,0 +1,43 @@ +// Third party imports +import { describe, expect, test, vi } from "vitest"; + +// Local imports +import { sceneStylePreferenceSchema } from "@ogw_front/composables/scene_style_preference"; + +vi.setConfig({ testTimeout: 10_000 }); + +const STYLE = { + background_color: { red: 10, green: 20, blue: 30, alpha: 1 }, + text: { + color: { red: 250, green: 240, blue: 230, alpha: 0.5 }, + font_family: "courier", + }, + orientation_widget: "compass", +}; + +describe("scene style preference", () => { + test("accepts a scene style, a part of it, or none", () => { + expect(sceneStylePreferenceSchema.safeParse(STYLE).success).toBe(true); + expect(sceneStylePreferenceSchema.safeParse({ orientation_widget: "axes" }).success).toBe(true); + expect(sceneStylePreferenceSchema.safeParse({}).success).toBe(true); + }); + + test("drops the scene id and unknown fields", () => { + expect(sceneStylePreferenceSchema.parse({ id: "scene", extra: 1, ...STYLE })).toStrictEqual( + STYLE, + ); + }); + + test.each([ + ["an unknown font", { ...STYLE, text: { ...STYLE.text, font_family: "comic" } }], + ["an unknown orientation widget", { ...STYLE, orientation_widget: "globe" }], + [ + "a color out of range", + { ...STYLE, background_color: { red: 300, green: 0, blue: 0, alpha: 1 } }, + ], + ["a text without its font", { ...STYLE, text: { color: STYLE.text.color } }], + ["no scene style", "dark"], + ])("rejects %s", (_name, value) => { + expect(sceneStylePreferenceSchema.safeParse(value).success).toBe(false); + }); +}); diff --git a/tests/unit/composables/viewer_reset.nuxt.test.ts b/tests/unit/composables/viewer_reset.nuxt.test.ts index d23689088..751a0ef33 100644 --- a/tests/unit/composables/viewer_reset.nuxt.test.ts +++ b/tests/unit/composables/viewer_reset.nuxt.test.ts @@ -11,6 +11,7 @@ import { mockAs } from "@ogw_tests/utils"; import { recordRequests } from "@ogw_tests/unit/helpers/viewer_requests"; import { setOrientationOverlay } from "@ogw_tests/unit/helpers/orientation_overlay"; import { setSceneColors } from "@ogw_front/utils/default_styles"; +import { useSceneStylePreference } from "@ogw_front/composables/scene_style_preference"; vi.setConfig({ testTimeout: 10_000 }); @@ -20,11 +21,20 @@ const { set_background_color, set_orientation_widget, set_text_style } = viewer_schemas.opengeodeweb_viewer.viewer; const BRAND_BACKGROUND = { red: 160, green: 180, blue: 187, alpha: 1 }; const BRAND_TEXT = { red: 31, green: 46, blue: 51, alpha: 1 }; +const USER_STYLE = { + background_color: { red: 10, green: 20, blue: 30, alpha: 1 }, + text: { + color: { red: 250, green: 240, blue: 230, alpha: 0.5 }, + font_family: "courier" as const, + }, + orientation_widget: "compass" as const, +}; describe("after a viewer reset", () => { afterEach(() => { setSceneColors({}); setOrientationOverlay(false); + useSceneStylePreference().value = {}; }); test("leaves the viewer as is for an app without scene colors nor overlay", async () => { @@ -47,7 +57,11 @@ describe("after a viewer reset", () => { }); expect(request).toHaveBeenCalledWith({ schema: set_text_style, - params: { color: { red: 31, green: 46, blue: 51 }, font_family: "arial", opacity: 1 }, + params: { + color: { red: 31, green: 46, blue: 51 }, + font_family: "arial", + opacity: 1, + }, }); }); @@ -66,6 +80,43 @@ describe("after a viewer reset", () => { ]); }); + test("gives the viewer the whole scene style the user chose", async () => { + useSceneStylePreference().value = USER_STYLE; + const { request, schemas } = recordRequests(); + await afterViewerReset(request); + + expect(schemas).toStrictEqual([set_background_color, set_text_style, set_orientation_widget]); + expect(request).toHaveBeenCalledWith({ + schema: set_background_color, + params: { color: { red: 10, green: 20, blue: 30 } }, + }); + expect(request).toHaveBeenCalledWith({ + schema: set_text_style, + params: { + color: { red: 250, green: 240, blue: 230 }, + font_family: "courier", + opacity: 0.5, + }, + }); + expect(request).toHaveBeenCalledWith({ + schema: set_orientation_widget, + params: { marker: "compass" }, + }); + }); + + test("lets the overlay draw the orientation widget the user chose", async () => { + useSceneStylePreference().value = USER_STYLE; + setOrientationOverlay(true); + const { request, schemas } = recordRequests(); + await afterViewerReset(request); + + expect(schemas).toStrictEqual([set_background_color, set_text_style, set_orientation_widget]); + expect(request).toHaveBeenCalledWith({ + schema: set_orientation_widget, + params: { marker: "none" }, + }); + }); + test("shows the orientation marker again", async () => { const { request } = recordRequests(); await setOrientationMarkerVisibility(false, request); diff --git a/tests/unit/stores/data_style_scene.nuxt.test.ts b/tests/unit/stores/data_style_scene.nuxt.test.ts new file mode 100644 index 000000000..8cdc67590 --- /dev/null +++ b/tests/unit/stores/data_style_scene.nuxt.test.ts @@ -0,0 +1,98 @@ +// Third party imports +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; + +// Local imports +import { mockAs, setupActivePinia } from "@ogw_tests/utils"; +import { getDefaultSceneStyle } from "@ogw_front/utils/default_styles"; +import { mockViewer } from "@ogw_tests/unit/stores/image_style_helpers"; +import { useDataStyleState } from "@ogw_internal/stores/data_style/state"; +import { useDataStyleStore } from "@ogw_front/stores/data_style"; +import { useHybridViewerStore } from "@ogw_front/stores/hybrid_viewer"; +import { useSceneStylePreference } from "@ogw_front/composables/scene_style_preference"; + +vi.setConfig({ testTimeout: 10_000 }); + +const BACKGROUND = { red: 10, green: 20, blue: 30, alpha: 1 }; +const TEXT_COLOR = { red: 250, green: 240, blue: 230, alpha: 0.5 }; +const PROJECT_STYLE = { + id: "scene" as const, + background_color: { red: 1, green: 2, blue: 3, alpha: 1 }, + text: { + color: { red: 4, green: 5, blue: 6, alpha: 1 }, + font_family: "times" as const, + }, + orientation_widget: "compass" as const, +}; +const USER_STYLE = { + background_color: BACKGROUND, + text: { color: TEXT_COLOR, font_family: "courier" as const }, + orientation_widget: "axes" as const, +}; + +function mockHybridBackground(): void { + const hybridViewerStore = useHybridViewerStore(); + hybridViewerStore.setBackgroundColor = mockAs( + vi.fn(async () => { + await Promise.resolve(); + }), + ); +} + +describe("scene style of the user", () => { + beforeEach(async () => { + setupActivePinia(); + mockViewer(); + mockHybridBackground(); + // The scene style is first read from the database: a change made before would be replaced + await useDataStyleState().loadFromDatabase(); + }); + + afterEach(() => { + useSceneStylePreference().value = {}; + }); + + test("keeps each scene choice of the menu as the user scene style", async () => { + const dataStyleStore = useDataStyleStore(); + const { text } = getDefaultSceneStyle(); + + await dataStyleStore.setSceneBackgroundColor(BACKGROUND); + + expect(useSceneStylePreference().value).toStrictEqual({ + background_color: BACKGROUND, + text, + orientation_widget: getDefaultSceneStyle().orientation_widget, + }); + + await dataStyleStore.setSceneTextColor(TEXT_COLOR); + await dataStyleStore.setSceneFontFamily("courier"); + await dataStyleStore.setSceneOrientationWidget("compass"); + + expect(useSceneStylePreference().value).toStrictEqual({ + ...USER_STYLE, + orientation_widget: "compass", + }); + }); + + test("leaves the user scene style when a project is loaded", async () => { + const dataStyleStore = useDataStyleStore(); + useSceneStylePreference().value = USER_STYLE; + + await dataStyleStore.importSceneStyle(PROJECT_STYLE); + await dataStyleStore.applySceneStyle(); + + expect(dataStyleStore.sceneStyle).toStrictEqual(PROJECT_STYLE); + expect(useSceneStylePreference().value).toStrictEqual(USER_STYLE); + }); + + test("gives a project saved without scene style the user one", async () => { + const dataStyleStore = useDataStyleStore(); + useSceneStylePreference().value = USER_STYLE; + + await dataStyleStore.importSceneStyle(undefined); + + expect(dataStyleStore.sceneStyle).toStrictEqual({ + id: "scene", + ...USER_STYLE, + }); + }); +}); diff --git a/tests/unit/utils/default_scene_style.nuxt.test.ts b/tests/unit/utils/default_scene_style.nuxt.test.ts index 35c5f8853..6b6956702 100644 --- a/tests/unit/utils/default_scene_style.nuxt.test.ts +++ b/tests/unit/utils/default_scene_style.nuxt.test.ts @@ -8,18 +8,29 @@ import { } from "@ogw_front/utils/default_styles/constants"; import { getDefaultSceneStyle, setSceneColors } from "@ogw_front/utils/default_styles"; import { defaultBackgroundBrightness } from "@ogw_internal/stores/hybrid_viewer/brightness"; +import { useSceneStylePreference } from "@ogw_front/composables/scene_style_preference"; vi.setConfig({ testTimeout: 10_000 }); const BRAND_BACKGROUND = { red: 160, green: 180, blue: 187, alpha: 1 }; const BRAND_TEXT = { red: 31, green: 46, blue: 51, alpha: 1 }; +const USER_STYLE = { + background_color: { red: 10, green: 20, blue: 30, alpha: 1 }, + text: { + color: { red: 250, green: 240, blue: 230, alpha: 0.5 }, + font_family: "courier" as const, + }, + orientation_widget: "compass" as const, +}; // Mean of the red, green and blue channels of each background, from 0 to 1 const OGW_BACKGROUND_BRIGHTNESS = 0.706; const BRAND_BACKGROUND_BRIGHTNESS = 0.689; +const USER_BACKGROUND_BRIGHTNESS = 0.078; describe("default scene style", () => { afterEach(() => { setSceneColors({}); + useSceneStylePreference().value = {}; }); test("uses the OpenGeodeWeb colors when the app sets none", () => { @@ -51,4 +62,28 @@ describe("default scene style", () => { expect(defaultBackgroundBrightness()).toBeCloseTo(BRAND_BACKGROUND_BRIGHTNESS); }); + + test("uses the scene style the user chose, over the app scene colors", () => { + setSceneColors({ background: BRAND_BACKGROUND, text: BRAND_TEXT }); + useSceneStylePreference().value = USER_STYLE; + + expect(getDefaultSceneStyle()).toStrictEqual({ + id: "scene", + ...USER_STYLE, + }); + }); + + test("copies the user scene style, so a style change does not alter it", () => { + useSceneStylePreference().value = USER_STYLE; + getDefaultSceneStyle().background_color.red = 0; + getDefaultSceneStyle().text.font_family = "times"; + + expect(useSceneStylePreference().value).toStrictEqual(USER_STYLE); + }); + + test("gives the brightness of the user background", () => { + useSceneStylePreference().value = USER_STYLE; + + expect(defaultBackgroundBrightness()).toBeCloseTo(USER_BACKGROUND_BRIGHTNESS); + }); });