-
Notifications
You must be signed in to change notification settings - Fork 2
Add classfile parsing #205
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
de7e0d7
Add class prop to widgets
abigailalexander 2054c94
Create theme using colours from classfile
abigailalexander a8d0dec
Add class property to widgets and useStyle
abigailalexander b049a7a
Add class prop tp thermometer
abigailalexander 5dea77f
Add tests
abigailalexander 5eb1421
Add missing fileId after rebase
abigailalexander cb8132d
Make minor corrections
abigailalexander 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
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
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,114 @@ | ||
| import React from "react"; | ||
| import { contextRender, createRootStoreState } from "../../testResources"; | ||
| import { vi } from "vitest"; | ||
| import { act, screen } from "@testing-library/react"; | ||
| import { ensureWidgetsRegistered } from "../widgets"; | ||
| import { useClassFile } from "./useClassFile"; | ||
| import { CsWebLibConfig } from "../../redux"; | ||
| import { phoebusTheme } from "../../phoebusTheme"; | ||
| import { createTheme } from "@mui/material"; | ||
| import { getFileState } from "./useFile.test"; | ||
| ensureWidgetsRegistered(); | ||
|
|
||
| const initialState: CsWebLibConfig = { | ||
| storeMode: "DEV", | ||
| PVWS_SOCKET: "", | ||
| PVWS_SSL: true, | ||
| THROTTLE_PERIOD: 100, | ||
| defaultMjpgEndpoint: "", | ||
| csWebLibFeatureFlags: { | ||
| enableDynamicScripts: false | ||
| } | ||
| }; | ||
|
|
||
| declare global { | ||
| // eslint-disable-next-line @typescript-eslint/no-namespace | ||
| namespace NodeJS { | ||
| // eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
| interface Global {} | ||
| } | ||
| } | ||
|
|
||
| interface GlobalFetch extends NodeJS.Global { | ||
| fetch: any; | ||
| } | ||
| const globalWithFetch = global as GlobalFetch; | ||
|
|
||
| const ClassFileTester = (): JSX.Element => { | ||
| const contents = useClassFile(); | ||
| return <div>contents: {JSON.stringify(contents.palette)}</div>; | ||
| }; | ||
|
|
||
| describe("useClassFile", (): void => { | ||
| it("returns default phoebus theme if no classfile", (): void => { | ||
| const { getByText } = contextRender( | ||
| <ClassFileTester />, | ||
| {}, | ||
| {}, | ||
| createRootStoreState(getFileState(), undefined, undefined, initialState), | ||
| {} | ||
| ); | ||
|
|
||
| expect( | ||
| getByText(`contents: ${JSON.stringify(phoebusTheme.palette)}`) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("returns theme with classes if classfile", async (): Promise<void> => { | ||
| const mockSuccessResponse = `<?xml version="1.0" encoding="UTF-8"?> | ||
| <display version="2.0.0"> | ||
| <name>Widget Classes</name> | ||
| <y use_class="true">0</y> | ||
| <widget type="action_button" version="3.0.0"> | ||
| <name>MY_CLASS</name> | ||
| <x>390</x> | ||
| <y>180</y> | ||
| <foreground_color use_class="true"> | ||
| <color name="Text" red="0" green="0" blue="0"> | ||
| </color> | ||
| </foreground_color> | ||
| <background_color use_class="true"> | ||
| <color name="STOP" red="0" green="0" blue="255"> | ||
| </color> | ||
| </background_color> | ||
| <tooltip>$(actions)</tooltip> | ||
| </widget> | ||
| </display>`; | ||
| const mockJsonPromise = Promise.resolve(mockSuccessResponse); | ||
| const mockFetchPromise = Promise.resolve({ | ||
| text: (): Promise<unknown> => mockJsonPromise | ||
| }); | ||
| const mockFetch = (): Promise<unknown> => mockFetchPromise; | ||
| vi.spyOn(globalWithFetch, "fetch").mockImplementation(mockFetch); | ||
| await act(async () => { | ||
| contextRender( | ||
| <ClassFileTester />, | ||
| {}, | ||
| {}, | ||
| createRootStoreState(getFileState(), undefined, undefined, { | ||
| classFile: "myclass.bcf", | ||
| ...initialState | ||
| }), | ||
| {} | ||
| ); | ||
| }); | ||
|
|
||
| const responseContent = JSON.stringify( | ||
| createTheme({ | ||
| customName: "class", | ||
| palette: { | ||
| ...phoebusTheme.palette, | ||
| ...{ | ||
| MY_CLASSactionbutton: { | ||
| main: "rgba(0,0,255,1)", | ||
| contrastText: "rgba(0,0,0,1)" | ||
| } | ||
| } | ||
| } | ||
| }).palette | ||
| ); | ||
| expect( | ||
| screen.getByText(`contents: ${responseContent}`) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); |
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,87 @@ | ||
| import { useSelector } from "react-redux"; | ||
| import { useEffect, useState } from "react"; | ||
| import { createTheme, Theme } from "@mui/material"; | ||
| import { fetchAndConvert } from "./useFile"; | ||
| import { WidgetDescription } from "../widgets/createComponent"; | ||
| import { selectClassFile } from "../../redux/slices/configurationSlice"; | ||
| import { phoebusTheme } from "../../phoebusTheme"; | ||
|
|
||
| // Map widget props to MUI theme props | ||
| const keyMap: Record<string, string> = { | ||
| backgroundColor: "main", | ||
| foregroundColor: "contrastText" | ||
| }; | ||
|
|
||
| const CLASS_PROPS = [ | ||
| "offColor", | ||
| "onColor", | ||
| "foregroundColor", | ||
| "backgroundColor", | ||
| "lineColor", | ||
| "emptyColor", | ||
| "knobColor", | ||
| "color", | ||
| "fillColor", | ||
| "needleColor", | ||
| "selectedColor", | ||
| "deselectedColor", | ||
| "borderColor" | ||
| ]; | ||
|
|
||
| export function useClassFile(userTheme?: Theme): Theme { | ||
| const classFile = useSelector(selectClassFile); | ||
| //let theme = userTheme ?? phoebusTheme; | ||
|
abigailalexander marked this conversation as resolved.
Outdated
|
||
| const [theme, setTheme] = useState<Theme>(userTheme ?? phoebusTheme); | ||
|
abigailalexander marked this conversation as resolved.
|
||
|
|
||
| useEffect(() => { | ||
| const fetchData = async (): Promise<void> => { | ||
| const widgetDescription = await fetchAndConvert( | ||
| classFile as string, | ||
| "ca", | ||
| {} | ||
| ); | ||
| setTheme(createClassPalettes(widgetDescription)); | ||
| }; | ||
|
|
||
| if (classFile !== undefined) { | ||
| fetchData(); | ||
| } | ||
| }, [classFile]); | ||
|
|
||
| return theme; | ||
| } | ||
|
|
||
| export function createClassPalettes(classFile: WidgetDescription): Theme { | ||
| // If classfile is empty, do nothing | ||
| if (!classFile.children) return phoebusTheme; | ||
|
|
||
| const palette: { [key: string]: any } = {}; | ||
| classFile.children?.forEach((child: WidgetDescription) => { | ||
| const widgetType: string = child.type; | ||
| // Construct palette name from widget type and classname | ||
| const paletteName = `${child.name}${widgetType}`; | ||
|
|
||
| // Only colors go in the theme palette | ||
| const matches = Object.entries(child) | ||
| .filter(([key]) => CLASS_PROPS.includes(key)) | ||
| .map(([key, value]) => ({ key, value })); | ||
|
|
||
| // Assign colors to palette | ||
| palette[paletteName] = { | ||
| // Put Phoebus theme defaults, overwrite with class props | ||
| ...phoebusTheme.palette[widgetType], | ||
| ...Object.fromEntries( | ||
| matches.map(({ key, value }) => [keyMap[key] ?? key, value.colorString]) | ||
|
abigailalexander marked this conversation as resolved.
Outdated
|
||
| ) | ||
| }; | ||
| }); | ||
| // Create Theme | ||
| const classTheme = createTheme({ | ||
| customName: "class", | ||
| palette: { | ||
| ...phoebusTheme.palette, | ||
| ...palette | ||
| } | ||
| }); | ||
| return classTheme; | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.