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

Render SelectFields as MUI Selects #4059

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0591eb8
Render SelectFields as MUI Selects
CommandMC Oct 14, 2024
becc919
fix some styling issues
Etaash-mathamsetty Oct 19, 2024
f9a4eb1
bug fix
Etaash-mathamsetty Oct 19, 2024
05ef6d1
fix lint
Etaash-mathamsetty Oct 19, 2024
507d939
Fix passing an outdated Wine Version name to the selector
CommandMC Nov 22, 2024
43fcf81
Misc: Selector ordering
CommandMC Nov 24, 2024
5e8dcb8
Fix: Text in SelectField is not vertically centered
CommandMC Nov 24, 2024
87246d3
Hide black box using MUI `sx` prop
CommandMC Nov 24, 2024
abe2d1d
Hide 2nd arrow with `sx` as well
CommandMC Nov 24, 2024
de048cd
Misc: Consolidate rules
CommandMC Nov 24, 2024
e2223f9
Fix: Text in select on global settings is centered instead of left-al…
CommandMC Nov 24, 2024
1e8e651
Fix: Text in select is slightly offset to the right
CommandMC Nov 24, 2024
0bb2f4f
Use MUI Dialogs to render our Dialogs
CommandMC Nov 24, 2024
bfeba06
Controller navigation fixes, part 1
CommandMC Nov 30, 2024
7fb062b
Controller navigation fixes, part 2
CommandMC Nov 30, 2024
e82760d
Misc style fixes
CommandMC Dec 1, 2024
1bf5c54
Controller navigation hacks: Transferring focus
CommandMC Dec 1, 2024
b5b3961
Controller navigation fixes, part 3
CommandMC Dec 1, 2024
cae602c
Fix theme selector empty when not specifically having selected a theme
CommandMC Dec 1, 2024
8194a59
Fix the input element only being clickable in the area containing text
CommandMC Dec 1, 2024
201eb19
Fixup "Disable closing dialogs by clicking outside" accessibility fea…
CommandMC Jan 4, 2025
c3a5646
Fix arrow being in wrong spot on RTL text alignments
CommandMC Jan 4, 2025
1f72fc7
Use text indent value from MUI
CommandMC Jan 4, 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
33 changes: 31 additions & 2 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
GameSettings,
DiskSpaceData,
StatusPromise,
GamepadInputEvent,
Runner
} from 'common/types'
import * as path from 'path'
Expand Down Expand Up @@ -1282,7 +1281,11 @@ ipcMain.handle('gamepadAction', async (event, args) => {
const mainWindow = getMainWindow()!

const { action, metadata } = args
const inputEvents: GamepadInputEvent[] = []
const inputEvents: (
| Electron.MouseInputEvent
| Electron.MouseWheelInputEvent
| Electron.KeyboardInputEvent
)[] = []

/*
* How to extend:
Expand Down Expand Up @@ -1368,6 +1371,32 @@ ipcMain.handle('gamepadAction', async (event, args) => {
keyCode: 'Esc'
})
break
case 'tab':
inputEvents.push(
{
type: 'keyDown',
keyCode: 'Tab'
},
{
type: 'keyUp',
keyCode: 'Tab'
}
)
break
case 'shiftTab':
inputEvents.push(
{
type: 'keyDown',
keyCode: 'Tab',
modifiers: ['shift']
},
{
type: 'keyUp',
keyCode: 'Tab',
modifiers: ['shift']
}
)
break
}

if (inputEvents.length) {
Expand Down
26 changes: 2 additions & 24 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,30 +347,6 @@ export interface GOGImportData {
dlcs: string[]
}

export type GamepadInputEvent =
| GamepadInputEventKey
| GamepadInputEventWheel
| GamepadInputEventMouse

interface GamepadInputEventKey {
type: 'keyDown' | 'keyUp' | 'char'
keyCode: string
}

interface GamepadInputEventWheel {
type: 'mouseWheel'
deltaY: number
x: number
y: number
}

interface GamepadInputEventMouse {
type: 'mouseDown' | 'mouseUp'
x: number
y: number
button: 'left' | 'middle' | 'right'
}

export interface SteamRuntime {
path: string
type: 'sniper' | 'scout' | 'soldier'
Expand Down Expand Up @@ -541,6 +517,8 @@ interface GamepadActionArgsWithoutMetadata {
| 'back'
| 'altAction'
| 'esc'
| 'tab'
| 'shiftTab'
metadata?: undefined
}

Expand Down
149 changes: 64 additions & 85 deletions src/frontend/components/UI/Dialog/components/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { faXmark } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import ContextProvider from 'frontend/state/ContextProvider'
import React, {
KeyboardEvent,
ReactNode,
SyntheticEvent,
useCallback,
useContext,
useEffect,
useRef,
useState
} from 'react'
import {
Dialog as MuiDialog,
DialogContent,
IconButton,
Paper,
styled
} from '@mui/material'
import CloseIcon from '@mui/icons-material/Close'

import ContextProvider from 'frontend/state/ContextProvider'

interface DialogProps {
className?: string
Expand All @@ -19,100 +23,75 @@ interface DialogProps {
onClose: () => void
}

const StyledPaper = styled(Paper)(() => ({
backgroundColor: 'var(--modal-background)',
color: 'var(--text-default)',
maxWidth: '100%',
'&:has(.settingsDialogContent)': {
height: '80%'
}
}))

export const Dialog: React.FC<DialogProps> = ({
children,
className,
showCloseButton = false,
onClose
}) => {
const dialogRef = useRef<HTMLDialogElement | null>(null)
const onCloseRef = useRef(onClose)
onCloseRef.current = onClose
const [focusOnClose, setFocusOnClose] = useState<HTMLElement | null>(null)
const [open, setOpen] = useState(true)
const { disableDialogBackdropClose } = useContext(ContextProvider)

useEffect(() => {
setFocusOnClose(document.querySelector<HTMLElement>('*:focus'))
// HACK: Focussing the dialog using JS does not seem to work
// Instead, simulate one or two tab presses
// One tab to focus the dialog
window.api.gamepadAction({ action: 'tab' })
// Second tab to skip the close button if it's shown
if (showCloseButton) window.api.gamepadAction({ action: 'tab' })
}, [])

const close = () => {
onCloseRef.current()
if (focusOnClose) {
setTimeout(() => focusOnClose.focus(), 200)
}
}
const close = useCallback(() => {
setOpen(false)
onClose()
}, [onClose])

useEffect(() => {
const dialog = dialogRef.current
if (dialog) {
const cancel = () => {
return (
<MuiDialog
open={open}
onClose={(e, reason) => {
if (disableDialogBackdropClose && reason === 'backdropClick') return
close()
}
dialog.addEventListener('cancel', cancel)

if (disableDialogBackdropClose) {
dialog['showPopover']()
CommandMC marked this conversation as resolved.
Show resolved Hide resolved

return () => {
dialog.removeEventListener('cancel', cancel)
dialog['hidePopover']()
}}
scroll="paper"
maxWidth="md"
PaperComponent={StyledPaper}
PaperProps={{
className
}}
sx={{
'& .Dialog__element': {
maxWidth: 'min(700px, 85vw)',
paddingTop: 'var(--dialog-margin-vertical)'
}
} else {
dialog.showModal()

return () => {
dialog.removeEventListener('cancel', cancel)
dialog.close()
}
}
}
return
}, [dialogRef.current, disableDialogBackdropClose])

const onDialogClick = useCallback(
(e: SyntheticEvent) => {
if (e.target === dialogRef.current) {
const ev = e.nativeEvent as MouseEvent
const tg = e.target as HTMLElement
if (
ev.offsetX < 0 ||
ev.offsetX > tg.offsetWidth ||
ev.offsetY < 0 ||
ev.offsetY > tg.offsetHeight
) {
close()
}
}
},
[onClose]
)

const closeIfEsc = (event: KeyboardEvent<HTMLDialogElement>) => {
if (event.key === 'Escape') {
close()
}
}

return (
<div className="Dialog">
<dialog
className={`Dialog__element ${className}`}
ref={dialogRef}
onClick={onDialogClick}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore, this feature is new and not yet typed
popover="manual"
onKeyUp={closeIfEsc}
>
}}
>
<>
{showCloseButton && (
<div className="Dialog__Close">
<button className="Dialog__CloseButton" onClick={close}>
<FontAwesomeIcon className="Dialog__CloseIcon" icon={faXmark} />
</button>
</div>
<IconButton
aria-label="close"
onClick={close}
sx={{
position: 'absolute',
right: 8,
top: 8,
color: 'var(--text-default)'
}}
>
<CloseIcon />
</IconButton>
)}
{children}
</dialog>
</div>
<DialogContent>{children}</DialogContent>
</>
</MuiDialog>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export const DialogContent: React.FC<Props> = ({
children,
className
}: Props) => {
return <div className={`Dialog__content ${className}`}>{children}</div>
return <div className={className}>{children}</div>
}
14 changes: 11 additions & 3 deletions src/frontend/components/UI/Dialog/components/DialogHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { ReactNode } from 'react'
import { DialogTitle } from '@mui/material'

interface DialogHeaderProps {
onClose?: () => void
Expand All @@ -7,8 +8,15 @@ interface DialogHeaderProps {

export const DialogHeader: React.FC<DialogHeaderProps> = ({ children }) => {
return (
<div className="Dialog__header">
<h1 className="Dialog__headerTitle">{children}</h1>
</div>
<DialogTitle
sx={{
fontFamily: 'var(--primary-font-family)',
fontSize: 'var(--text-xl)',
fontWeight: 'var(--bold)',
paddingLeft: 0
}}
>
{children}
</DialogTitle>
)
}
5 changes: 3 additions & 2 deletions src/frontend/components/UI/LanguageSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useTranslation } from 'react-i18next'
import { configStore } from 'frontend/helpers/electronStores'
import ContextProvider from 'frontend/state/ContextProvider'
import { SelectField } from '..'
import { MenuItem } from '@mui/material'

const storage: Storage = window.localStorage

Expand Down Expand Up @@ -132,9 +133,9 @@ export default function LanguageSelector({
if (flagPossition === FlagPosition.APPEND) label = `${label} ${flag}`

return (
<option key={lang} value={lang}>
<MenuItem key={lang} value={lang}>
{label}
</option>
</MenuItem>
)
}

Expand Down
13 changes: 11 additions & 2 deletions src/frontend/components/UI/SelectField/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
}

.selectFieldWrapper select,
.selectStyle {
.selectStyle,
.selectFieldWrapper .MuiOutlinedInput-root {
border-radius: var(--space-3xs);
grid-area: select;
width: 100%;
Expand All @@ -13,7 +14,6 @@
font-family: var(--primary-font-family), 'Noto Color Emoji';
font-weight: normal;
font-size: var(--text-md);
line-height: 19px;
color: var(--text-secondary);
text-indent: 22px;
border: none;
Expand Down Expand Up @@ -44,6 +44,15 @@
min-width: 100px;
}

.MuiPopover-root .MuiPaper-root {
color: var(--text-secondary);
background-color: var(--input-background);

.MuiMenuItem-root {
font-family: var(--primary-font-family);
}
}

.selectStyle {
padding-inline-end: 4rem;
}
Expand Down
Loading
Loading