-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The dialog allows changing the canvas pixel size and hiding the canvas. The intent is to give people on low-end GPUs a way to improve frame rates if the current Hydra shader renders too slowly, or a way to hide it entirely if it's distracting. (The thread that inspried this: https://post.lurk.org/@yaxu/113554740489867054)
- Loading branch information
Showing
9 changed files
with
229 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,86 @@ | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogHeader, | ||
DialogFooter, | ||
DialogTitle, | ||
} from "@/components/ui/dialog"; | ||
import { Button } from "@/components/ui/button"; | ||
import { Input } from "@/components/ui/input"; | ||
import { Label } from "@/components/ui/label"; | ||
import { DialogProps } from "@radix-ui/react-dialog"; | ||
import { useState, FormEvent } from "react"; | ||
import { | ||
DisplaySettings, | ||
sanitizeDisplaySettings, | ||
} from "@/lib/display-settings"; | ||
|
||
interface DisplaySettingsDialogProps extends DialogProps { | ||
settings: DisplaySettings, | ||
onAccept: (settings: DisplaySettings) => void; | ||
} | ||
|
||
export default function DisplaySettingsDialog({ | ||
settings, | ||
onAccept, | ||
...props | ||
}: DisplaySettingsDialogProps) { | ||
|
||
const [unsavedSettings, setUnsavedSettings] = useState({...settings}); | ||
const sanitizeAndSetUnsavedSettings = (settings: DisplaySettings) => | ||
setUnsavedSettings(sanitizeDisplaySettings(settings)); | ||
|
||
const handleSubmit = (e: FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
onAccept(unsavedSettings); | ||
props.onOpenChange && props.onOpenChange(false); | ||
}; | ||
|
||
return ( | ||
<Dialog {...props}> | ||
<DialogContent> | ||
<form onSubmit={handleSubmit}> | ||
<DialogHeader> | ||
<DialogTitle>Change display settings</DialogTitle> | ||
</DialogHeader> | ||
<div className="grid gap-4 py-4"> | ||
<div className="grid grid-cols-4 items-center gap-4"> | ||
<Label htmlFor="name" className="text-right"> | ||
Canvas pixel size | ||
</Label> | ||
<Input | ||
id="canvasPixelSize" | ||
type="number" | ||
value={unsavedSettings.canvasPixelSize} | ||
className="col-span-3" | ||
onChange={(e) => sanitizeAndSetUnsavedSettings({ | ||
...unsavedSettings, | ||
canvasPixelSize: parseInt(e.target.value, 10), | ||
})} | ||
/> | ||
</div> | ||
<div className="grid grid-cols-4 items-center gap-4"> | ||
<Label htmlFor="name" className="text-right"> | ||
Show canvas | ||
</Label> | ||
<input | ||
id="showCanvas" | ||
type="checkbox" | ||
checked={unsavedSettings.showCanvas} | ||
className="w-5" | ||
onChange={(e) => sanitizeAndSetUnsavedSettings({ | ||
...unsavedSettings, | ||
showCanvas: e.target.checked, | ||
})} | ||
/> | ||
</div> | ||
</div> | ||
<DialogFooter> | ||
<Button type="submit">Save changes</Button> | ||
</DialogFooter> | ||
</form> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
This file contains 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 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 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 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,19 @@ | ||
import { SettingsMessage } from "@/lib/settings"; | ||
import { useEffect } from "react"; | ||
|
||
export function useSettings(callback: (message: SettingsMessage) => void) { | ||
useEffect(() => { | ||
const handleMessage = (event: MessageEvent) => { | ||
if (event.data.type !== "settings") return; | ||
callback(event.data.body as SettingsMessage); | ||
}; | ||
|
||
window.addEventListener("message", handleMessage); | ||
|
||
return () => { | ||
window.removeEventListener("message", handleMessage); | ||
}; | ||
}, [callback]); | ||
|
||
return; | ||
} |
This file contains 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,27 @@ | ||
export interface DisplaySettings { | ||
canvasPixelSize: number; | ||
showCanvas: boolean; | ||
} | ||
|
||
export const defaultDisplaySettings: DisplaySettings = { | ||
canvasPixelSize: 1, | ||
showCanvas: true, | ||
} | ||
|
||
export function sanitizeDisplaySettings(settings: DisplaySettings): DisplaySettings { | ||
// Pixel size should be at least 1 to prevent division-by-zero errors | ||
const minPixelSize = 1; | ||
|
||
// A maximum pixel size of 50 gives you 2-digit width/heights for a 4k | ||
// canvas; should be low enough | ||
const maxPixelSize = 50; | ||
|
||
return { | ||
...settings, | ||
canvasPixelSize: Math.max( | ||
minPixelSize, | ||
Math.min( | ||
maxPixelSize, | ||
Math.round(settings.canvasPixelSize))), | ||
}; | ||
} |
This file contains 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 @@ | ||
import { DisplaySettings } from "@/lib/display-settings"; | ||
|
||
export interface SettingsMessage { | ||
displaySettings?: DisplaySettings | ||
} |
This file contains 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.