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

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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 @@ -1499,7 +1498,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 @@ -1585,6 +1588,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 @@ -346,30 +346,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 @@ -540,6 +516,8 @@ interface GamepadActionArgsWithoutMetadata {
| 'back'
| 'altAction'
| 'esc'
| 'tab'
| 'shiftTab'
metadata?: undefined
}

Expand Down
149 changes: 63 additions & 86 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,73 @@ 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)
}
}

useEffect(() => {
const dialog = dialogRef.current
if (dialog) {
const cancel = () => {
close()
}
dialog.addEventListener('cancel', cancel)

if (disableDialogBackdropClose) {
dialog['showPopover']()

return () => {
dialog.removeEventListener('cancel', cancel)
dialog['hidePopover']()
}
} 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()
}
}
const close = useCallback(() => {
setOpen(false)
onClose()
}, [onClose])

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}
>
<MuiDialog
open={open}
onClose={close}
scroll="paper"
maxWidth="md"
PaperComponent={StyledPaper}
PaperProps={{
className
}}
disableEscapeKeyDown={disableDialogBackdropClose}
sx={{
'& .Dialog__element': {
maxWidth: 'min(700px, 85vw)',
paddingTop: 'var(--dialog-margin-vertical)'
}
}}
>
<>
{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