-
Notifications
You must be signed in to change notification settings - Fork 11
feat(Camera): add setting to override default scale resolver #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
draedful
wants to merge
2
commits into
main
Choose a base branch
from
camera_scale_resolver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+262
−19
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { test, expect } from "@playwright/test"; | ||
|
|
||
| import { ECameraScaleLevel } from "../../../src/services/camera/cameraScaleEnums"; | ||
| import { GraphPageObject } from "../../page-objects/GraphPageObject"; | ||
|
|
||
| const BLOCK = { | ||
| id: "block-1", | ||
| is: "Block" as const, | ||
| x: 100, | ||
| y: 100, | ||
| width: 200, | ||
| height: 100, | ||
| name: "Block 1", | ||
| anchors: [], | ||
| selected: false, | ||
| }; | ||
|
|
||
| test.describe("getCameraBlockScaleLevel setting", () => { | ||
| test.describe("default strategy", () => { | ||
| let graphPO: GraphPageObject; | ||
|
|
||
| test.beforeEach(async ({ page }) => { | ||
| graphPO = new GraphPageObject(page); | ||
| await graphPO.initialize({ blocks: [BLOCK], connections: [] }); | ||
| }); | ||
|
|
||
| test("settings hook should reference the exported defaultGetCameraBlockScaleLevel", async ({ | ||
| page, | ||
| }) => { | ||
| const same = await page.evaluate(() => { | ||
| const { defaultGetCameraBlockScaleLevel } = window.GraphModule; | ||
| return ( | ||
| window.graph.rootStore.settings.$settings.value.getCameraBlockScaleLevel === | ||
| defaultGetCameraBlockScaleLevel | ||
| ); | ||
| }); | ||
| expect(same).toBe(true); | ||
| }); | ||
|
|
||
| test("should map camera scale to Minimalistic / Schematic / Detailed using block SCALES", async () => { | ||
| const camera = graphPO.getCamera(); | ||
|
|
||
| await camera.zoomToScale(0.05); | ||
| await graphPO.waitForFrames(3); | ||
| let level = await graphPO.page.evaluate(() => | ||
| window.graph.cameraService.getCameraBlockScaleLevel(), | ||
| ); | ||
| expect(level).toBe(ECameraScaleLevel.Minimalistic); | ||
|
|
||
| await camera.zoomToScale(0.3); | ||
| await graphPO.waitForFrames(3); | ||
| level = await graphPO.page.evaluate(() => window.graph.cameraService.getCameraBlockScaleLevel()); | ||
| expect(level).toBe(ECameraScaleLevel.Schematic); | ||
|
|
||
| await camera.zoomToScale(0.85); | ||
| await graphPO.waitForFrames(3); | ||
| level = await graphPO.page.evaluate(() => window.graph.cameraService.getCameraBlockScaleLevel()); | ||
| expect(level).toBe(ECameraScaleLevel.Detailed); | ||
| }); | ||
| }); | ||
|
|
||
| test.describe("custom strategy (defined in browser)", () => { | ||
| test.beforeEach(async ({ page }) => { | ||
| await page.goto("/base.html"); | ||
| await page.waitForFunction(() => window.graphLibraryLoaded === true); | ||
|
|
||
| await page.evaluate(() => { | ||
| const { Graph, ECameraScaleLevel } = window.GraphModule; | ||
| const rootEl = document.getElementById("root"); | ||
| if (!rootEl) { | ||
| throw new Error("Root element not found"); | ||
| } | ||
| const blocks = [ | ||
| { | ||
| id: "block-1", | ||
| is: "Block" as const, | ||
| x: 100, | ||
| y: 100, | ||
| width: 200, | ||
| height: 100, | ||
| name: "Block 1", | ||
| anchors: [], | ||
| selected: false, | ||
| }, | ||
| ]; | ||
| const graph = new Graph( | ||
| { | ||
| blocks, | ||
| connections: [], | ||
| settings: { | ||
| getCameraBlockScaleLevel: () => ECameraScaleLevel.Detailed, | ||
| }, | ||
| }, | ||
| rootEl, | ||
| ); | ||
| graph.start(); | ||
| graph.zoomTo("center"); | ||
| window.graph = graph; | ||
| window.graphInitialized = true; | ||
| }); | ||
|
|
||
| await page.waitForFunction(() => window.graphInitialized === true); | ||
| const graphPO = new GraphPageObject(page); | ||
| await graphPO.waitForFrames(3); | ||
| }); | ||
|
|
||
| test("should return Detailed at low scale when default would be Minimalistic", async ({ page }) => { | ||
| const graphPO = new GraphPageObject(page); | ||
| const camera = graphPO.getCamera(); | ||
|
|
||
| await camera.zoomToScale(0.05); | ||
| await graphPO.waitForFrames(3); | ||
|
|
||
| const level = await page.evaluate(() => window.graph.cameraService.getCameraBlockScaleLevel()); | ||
| expect(level).toBe(ECameraScaleLevel.Detailed); | ||
|
|
||
| const notDefault = await page.evaluate(() => { | ||
| const { defaultGetCameraBlockScaleLevel } = window.GraphModule; | ||
| return ( | ||
| window.graph.rootStore.settings.$settings.value.getCameraBlockScaleLevel !== | ||
| defaultGetCameraBlockScaleLevel | ||
| ); | ||
| }); | ||
| expect(notDefault).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export enum ECameraScaleLevel { | ||
| Minimalistic = 100, | ||
| Schematic = 200, | ||
| Detailed = 300, | ||
| } |
67 changes: 67 additions & 0 deletions
67
src/services/camera/defaultGetCameraBlockScaleLevel.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import type { Graph } from "../../graph"; | ||
| import { initGraphConstants } from "../../graphConfig"; | ||
|
|
||
| import { getInitCameraState } from "./CameraService"; | ||
| import type { TCameraState } from "./CameraService"; | ||
| import { ECameraScaleLevel } from "./cameraScaleEnums"; | ||
| import { defaultGetCameraBlockScaleLevel } from "./defaultGetCameraBlockScaleLevel"; | ||
|
|
||
| function createGraphWithBlockScales(scales: [number, number, number]): Graph { | ||
| return { | ||
| graphConstants: { | ||
| block: { SCALES: scales }, | ||
| }, | ||
| } as unknown as Graph; | ||
| } | ||
|
|
||
| function cameraStateWithScale(scale: number): TCameraState { | ||
| return { ...getInitCameraState(), scale }; | ||
| } | ||
|
|
||
| describe("defaultGetCameraBlockScaleLevel", () => { | ||
| describe("uses block.SCALES[1] and block.SCALES[2] as thresholds", () => { | ||
| const SCALES: [number, number, number] = [0.01, 0.2, 0.6]; | ||
| const graph = createGraphWithBlockScales(SCALES); | ||
| const [, s1, s2] = SCALES; | ||
|
|
||
| it("returns Minimalistic when scale < SCALES[1]", () => { | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(s1 - 1e-6))).toBe( | ||
| ECameraScaleLevel.Minimalistic | ||
| ); | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(0.05))).toBe(ECameraScaleLevel.Minimalistic); | ||
| }); | ||
|
|
||
| it("returns Schematic when scale >= SCALES[1] and scale < SCALES[2]", () => { | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(s1))).toBe(ECameraScaleLevel.Schematic); | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale((s1 + s2) / 2))).toBe( | ||
| ECameraScaleLevel.Schematic | ||
| ); | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(s2 - 1e-6))).toBe(ECameraScaleLevel.Schematic); | ||
| }); | ||
|
|
||
| it("returns Detailed when scale >= SCALES[2]", () => { | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(s2))).toBe(ECameraScaleLevel.Detailed); | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(1))).toBe(ECameraScaleLevel.Detailed); | ||
| }); | ||
|
|
||
| it("does not use SCALES[0] for tier resolution (only [1] and [2])", () => { | ||
| const graphLowS0 = createGraphWithBlockScales([0.99, s1, s2]); | ||
| const graphHighS0 = createGraphWithBlockScales([0.001, s1, s2]); | ||
| const state = cameraStateWithScale(0.1); | ||
| expect(defaultGetCameraBlockScaleLevel(graphLowS0, state)).toBe( | ||
| defaultGetCameraBlockScaleLevel(graphHighS0, state) | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("matches initGraphConstants.block.SCALES", () => { | ||
| it("uses library default SCALES [0.125, 0.225, 0.7] semantics", async () => { | ||
| const graph = createGraphWithBlockScales(initGraphConstants.block.SCALES); | ||
|
|
||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(0.1))).toBe(ECameraScaleLevel.Minimalistic); | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(0.225))).toBe(ECameraScaleLevel.Schematic); | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(0.5))).toBe(ECameraScaleLevel.Schematic); | ||
| expect(defaultGetCameraBlockScaleLevel(graph, cameraStateWithScale(0.7))).toBe(ECameraScaleLevel.Detailed); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import type { Graph } from "../../graph"; | ||
|
|
||
| import type { TCameraState } from "./CameraService"; | ||
| import { ECameraScaleLevel } from "./cameraScaleEnums"; | ||
|
|
||
| /** | ||
| * Resolves qualitative zoom tier for block rendering (Canvas detail level, React activation, etc.). | ||
| * Override via graph settings `getCameraBlockScaleLevel`. | ||
| */ | ||
| export type TGetCameraBlockScaleLevel = (graph: Graph, cameraState: TCameraState) => ECameraScaleLevel; | ||
|
|
||
| /** | ||
| * Default block zoom-tier strategy: uses `graphConstants.block.SCALES` thresholds. | ||
| * Same function reference as `DefaultSettings.getCameraBlockScaleLevel` and the default `getCameraBlockScaleLevel` | ||
| * in graph settings — use this export when you need the built-in strategy by identity (e.g. `===` or explicit config). | ||
| */ | ||
| export function defaultGetCameraBlockScaleLevel(graph: Graph, cameraState: TCameraState): ECameraScaleLevel { | ||
| const scales = graph.graphConstants.block.SCALES; | ||
| const cameraScale = cameraState.scale; | ||
| let scaleLevel = ECameraScaleLevel.Minimalistic; | ||
| if (cameraScale >= scales[1]) { | ||
| scaleLevel = ECameraScaleLevel.Schematic; | ||
| } | ||
| if (cameraScale >= scales[2]) { | ||
| scaleLevel = ECameraScaleLevel.Detailed; | ||
| } | ||
| return scaleLevel; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now
setupSettingsare called insetupGraphonly if there isconfig.settingsIn case it is a graph without
settings- there will be nosetupSettingscallIn this case, will the call to
getCameraBlockScaleLevel()be treated as "not a function"?