Skip to content
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

84 replace action button with mui button #85

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
de29a3d
Replaced default html button with MUI Button
NatLeung96 Mar 24, 2025
9cd6af8
Added border and disabled settings
NatLeung96 Mar 24, 2025
82addef
Updated file for prettier
NatLeung96 Mar 24, 2025
d2468d9
Prettier again
NatLeung96 Mar 24, 2025
b855c74
Prettier still
NatLeung96 Mar 24, 2025
6f74e87
Final time
NatLeung96 Mar 24, 2025
a99f07f
Refactored code to be compatible with tests
NatLeung96 Mar 24, 2025
8a3a0ee
Updated test snapshot
NatLeung96 Mar 24, 2025
b9e8cd3
Fixed error in snapshot
NatLeung96 Mar 24, 2025
e1ef730
Corrected the default grey colour
NatLeung96 Mar 31, 2025
e663116
Corrected the default text colour
NatLeung96 Mar 31, 2025
c098291
Added settings to change cursor when button is disabled
NatLeung96 Mar 31, 2025
7418adf
Tidied the colour prop declarations
NatLeung96 Mar 31, 2025
429278e
Fixed text wrapping
NatLeung96 Apr 1, 2025
1a33e06
Addedd transparent to parser test
NatLeung96 Apr 1, 2025
ac618e2
Renamed colourscheme file and defaultColours name
NatLeung96 Apr 2, 2025
580f734
Moved decision making out of the Button declaration and reorganised s…
NatLeung96 Apr 2, 2025
8e80b07
Moved ThemeProvider to embeddedDisplay
NatLeung96 Apr 2, 2025
9705ea9
Updated the snapshot
NatLeung96 Apr 2, 2025
1c86e35
Changed it so that button variant is "text" when transparent, otherwi…
NatLeung96 Apr 2, 2025
1fdd5ac
Added rotationStep to bobParser
NatLeung96 Apr 2, 2025
dbddd4b
Moved static styling components to use styled API
NatLeung96 Apr 2, 2025
36e5186
Set lineHeight to 1
NatLeung96 Apr 2, 2025
805ebf9
Updated snapshot
NatLeung96 Apr 2, 2025
dc5a219
Fixed text rotation in Button
NatLeung96 Apr 3, 2025
3b30e4d
Updated snapshot
NatLeung96 Apr 3, 2025
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
8 changes: 5 additions & 3 deletions src/colourscheme.ts → src/diamondTheme.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { createTheme } from "@mui/material/styles";

export const defaultColours = createTheme({
export const diamondTheme = createTheme({
palette: {
primary: {
main: "#C0C0C0",
main: "#D2D2D2",
// light: currently calculated automatically by MUI
// dark: currently calculated automatically by MUI
contrastText: "#FFFFFF"
contrastText: "#000000"
}
},
typography: {
fontFamily: "Liberation Sans",
fontSize: 14,
button: {
textTransform: "none"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

exports[`<ActionButton /> > it matches the snapshot 1`] = `
<button
className="_actionbutton_b413d3"
className="MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButton-colorPrimary MuiButton-fullWidth MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButton-colorPrimary MuiButton-fullWidth css-q0v14n-MuiButtonBase-root-MuiButton-root"
disabled={false}
onBlur={[Function]}
onClick={[MockFunction spy]}
style={
{
"backgroundColor": undefined,
"color": undefined,
"cursor": undefined,
"visibility": undefined,
"whiteSpace": "normal",
}
}
onContextMenu={[Function]}
onDragLeave={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchEnd={[Function]}
onTouchMove={[Function]}
onTouchStart={[Function]}
tabIndex={0}
type="button"
>
<span
style={
{
"whiteSpace": "normal",
"display": "block",
"lineHeight": 1,
"transform": "rotate(0deg)",
}
}
>
Expand Down
97 changes: 73 additions & 24 deletions src/ui/widgets/ActionButton/actionButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useContext } from "react";
import { WidgetActions, executeActions } from "../widgetActions";
import { commonCss, Widget } from "../widget";
import { Widget } from "../widget";
import { PVComponent, PVWidgetPropType } from "../widgetProps";
import classes from "./actionButton.module.css";
import { registerWidget } from "../register";
Expand All @@ -11,17 +11,22 @@ import {
ColorPropOpt,
FontPropOpt,
BorderPropOpt,
BoolPropOpt
BoolPropOpt,
FuncPropOpt,
FloatPropOpt
} from "../propTypes";
import { Color } from "../../../types/color";
import { Font } from "../../../types/font";
import { Border, BorderStyle } from "../../../types/border";
import { Border } from "../../../types/border";
import { MacroContext } from "../../../types/macros";
import { ExitFileContext, FileContext } from "../../../misc/fileContext";
import { styled, Button as MuiButton } from "@mui/material";

import { diamondTheme } from "../../../diamondTheme";

export interface ActionButtonProps {
text: string;
disabled?: boolean;
enabled?: boolean;
onClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
image?: string;
backgroundColor?: Color;
Expand All @@ -30,29 +35,59 @@ export interface ActionButtonProps {
font?: Font;
actions?: WidgetActions;
visible?: boolean;
rotationStep?: number;
transparent?: boolean;
}

const Button = styled(MuiButton)({
"&.MuiButton-root": {
display: "block",
alignItems: "center",
justifyContent: "center",
height: "100%",
width: "100%",
minWidth: 0,
minHeight: 0,
padding: 0,
overflow: "hidden",
whiteSpace: "nowrap",
wordBreak: "break-word",
textTransform: "none"
},
"&.Mui-disabled": {
cursor: "not-allowed",
pointerEvents: "all !important"
}
});

export const ActionButtonComponent = (
props: ActionButtonProps
): JSX.Element => {
const style = commonCss(props);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this commonCss function also removes the ability to set the button backgroundColour as transparent and some other CSS. It would be good to either keep using this function, or ensure we're still setting all the needed CSS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made it so that the transparent field from the .bob file actually gets passed to the widget and added a test to make the background transparent if it is set to true. The way it currently works is that, when transparent, the button colour matches the background but it keeps the button shadow. Personally, I think this looks cool but I can remove it if desired.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding that missing prop in! I think it might look better if we change the button variant if the background colour is transparent from contained (which has the shadow) to text. This ends up looking closest to Phoebus appearance

if (props.disabled) {
style["cursor"] = "not-allowed";
}
style["whiteSpace"] = "normal";
// Use default button style if no border defined.
if (props.border?.style === BorderStyle.None) {
style["borderStyle"] = undefined;
style["borderWidth"] = undefined;
style["borderColor"] = undefined;
style["padding"] = "0";
}
const {
enabled = true,
foregroundColor = diamondTheme.palette.primary.contrastText,
rotationStep = 0,
transparent = false
} = props;

const backgroundColor = transparent
? "transparent"
: (props.backgroundColor?.toString() ?? diamondTheme.palette.primary.main);
const font = props.font?.css() ?? diamondTheme.typography;
const border = props.border?.css() ?? null;

return (
<button
className={classes.actionbutton}
disabled={props.disabled}
<Button
variant={transparent ? "text" : "contained"}
disabled={!enabled}
fullWidth={true}
sx={{
color: foregroundColor.toString(),
backgroundColor: backgroundColor,
border: border,
fontFamily: font
}}
onClick={props.onClick}
style={style}
>
{props.image !== undefined ? (
<figure className={classes.figure}>
Expand All @@ -64,9 +99,17 @@ export const ActionButtonComponent = (
<figcaption>{props.text}</figcaption>
</figure>
) : (
<span style={{ whiteSpace: "normal" }}>{props.text || ""}</span>
<span
style={{
display: "block",
lineHeight: 1,
transform: `rotate(${rotationStep * -90}deg)`
}}
>
{props.text ?? ""}
</span>
)}
</button>
</Button>
);
};

Expand All @@ -78,7 +121,11 @@ const ActionButtonPropType = {
foregroundColor: ColorPropOpt,
font: FontPropOpt,
border: BorderPropOpt,
visible: BoolPropOpt
visible: BoolPropOpt,
enabled: BoolPropOpt,
onClick: FuncPropOpt,
transparent: BoolPropOpt,
rotationStep: FloatPropOpt
};

const ActionButtonWidgetProps = {
Expand Down Expand Up @@ -106,7 +153,7 @@ export const ActionButtonWidget = (
return (
<ActionButtonComponent
text={props.text ?? ""}
disabled={props.readonly}
enabled={props.enabled}
onClick={onClick}
image={props.image}
backgroundColor={props.backgroundColor}
Expand All @@ -115,6 +162,8 @@ export const ActionButtonWidget = (
border={props.border}
actions={props.actions as WidgetActions}
visible={props.visible}
transparent={props.transparent}
rotationStep={props.rotationStep}
/>
);
};
Expand Down
3 changes: 3 additions & 0 deletions src/ui/widgets/EmbeddedDisplay/bobParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ describe("opi widget parser", (): void => {
<x>10</x>
<y>20</y>
<not_a_property>hello</not_a_property>
<transparent>true</transparent>
<wrap_words>false</wrap_words>
<rotation_step>1</rotation_step>
</widget>
</display>`;

Expand All @@ -58,6 +60,7 @@ describe("opi widget parser", (): void => {
// Unrecognised property not passed on.
expect(widget.not_a_property).toEqual(undefined);
expect(widget.wrapWords).toEqual(false);
expect(widget.transparent).toEqual(true);
});

const readbackString = `
Expand Down
3 changes: 2 additions & 1 deletion src/ui/widgets/EmbeddedDisplay/bobParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ export function parseBob(
fallbackSymbol: ["fallback_symbol", opiParseString],
rotation: ["rotation", bobParseNumber],
styleOpt: ["style", bobParseNumber],
lineColor: ["line_color", opiParseColor]
lineColor: ["line_color", opiParseColor],
rotationStep: ["rotation_step", bobParseNumber]
};

const complexParsers = {
Expand Down
4 changes: 3 additions & 1 deletion src/ui/widgets/EmbeddedDisplay/embeddedDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { GroupBoxComponent } from "../GroupBox/groupBox";
import { useOpiFile } from "./useOpiFile";
import { useId } from "react-id-generator";
import { getOptionalValue, trimFromString } from "../utils";
import { ThemeProvider } from "@mui/material";
import { diamondTheme } from "../../../diamondTheme";

const RESIZE_STRINGS = [
"scroll-widget",
Expand Down Expand Up @@ -209,7 +211,7 @@ export const EmbeddedDisplay = (
return (
<MacroContext.Provider value={embeddedDisplayMacroContext}>
<GroupBoxComponent name={resolvedName} styleOpt={0}>
{component}
<ThemeProvider theme={diamondTheme}>{component}</ThemeProvider>
</GroupBoxComponent>
</MacroContext.Provider>
);
Expand Down