Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions app/composables/scene_style_preference.ts
Original file line number Diff line number Diff line change
@@ -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<Omit<SceneStyle, "id">>;

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<SceneStylePreference>({
key: "sceneStyle",
schema: sceneStylePreferenceSchema,
fallback: {},
});

let preference: Ref<SceneStylePreference> | 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<SceneStylePreference> {
preference ??= useUserSetting(sceneStyleSetting);
return preference;
}

export type { SceneStylePreference };
export { sceneStylePreferenceSchema, useSceneStylePreference };
26 changes: 18 additions & 8 deletions app/composables/viewer_reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
// 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<void> {
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: {
Expand All @@ -37,16 +40,23 @@ async function applyAppSceneColors(request: ViewerRequest): Promise<void> {
},
});
}
// 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
// Step fails the reset: the viewer stays usable with its own colors, and the marker is hidden either way
async function afterViewerReset(request: ViewerRequest): Promise<void> {
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);
}
Expand Down
15 changes: 11 additions & 4 deletions app/utils/default_styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<GeodeObjectType, unknown>> {
return {
Expand Down Expand Up @@ -77,14 +78,20 @@ function getSceneColors(): Readonly<SceneColors> {
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,
};
}

Expand Down
24 changes: 22 additions & 2 deletions internal/stores/data_style/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +34,7 @@ export function useSceneStyle(): {
const hybridViewerStore = useHybridViewerStore();
const { sceneStyle } = useDataStyleState();
const sceneStyleTable = sceneStyleDb();
const preference = useSceneStylePreference();

async function saveSceneStyle(values: Partial<Omit<SceneStyle, "id">>): Promise<void> {
// Values may hold nested reactive proxies (e.g. an exported snapshot), which IndexedDB cannot clone
Expand Down Expand Up @@ -85,30 +87,48 @@ export function useSceneStyle(): {
}
}

async function setSceneBackgroundColor(color: ColorRGBA): Promise<void> {
// 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<void> {
await hybridViewerStore.setBackgroundColor(toViewerColor(color));
await saveSceneStyle({ background_color: color });
}

async function setSceneBackgroundColor(color: ColorRGBA): Promise<void> {
await applyBackgroundColor(color);
keepAsPreference();
}

async function setSceneTextColor(color: ColorRGBA): Promise<unknown> {
const result = await requestTextStyle({ ...sceneStyle.value.text, color });
keepAsPreference();
return result;
}

async function setSceneFontFamily(font_family: SceneFontFamily): Promise<unknown> {
const result = await requestTextStyle({ ...sceneStyle.value.text, font_family });
keepAsPreference();
return result;
}

async function setSceneOrientationWidget(marker: SceneOrientationWidget): Promise<unknown> {
const result = await requestOrientationWidget(marker);
keepAsPreference();
return result;
}

async function applySceneStyle(): Promise<void> {
const { background_color, text, orientation_widget } = sceneStyle.value;
await Promise.all([
setSceneBackgroundColor(background_color),
applyBackgroundColor(background_color),
requestTextStyle(text),
requestOrientationWidget(orientation_widget),
]);
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/composables/scene_style_preference.nuxt.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
53 changes: 52 additions & 1 deletion tests/unit/composables/viewer_reset.nuxt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand All @@ -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 () => {
Expand All @@ -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,
},
});
});

Expand All @@ -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);
Expand Down
Loading
Loading