From 27e5ab8bac926fd5b0c9f5f22432c910d46fce85 Mon Sep 17 00:00:00 2001 From: cpojer Date: Sun, 16 Jun 2024 09:29:57 +0900 Subject: [PATCH] Open source virtual keyboard support on Steam Deck. GitOrigin-RevId: 1f31ec2249f8ba0bad5e25e2c6ab28e4ea1aeb98 --- ui/controls/setupSteamDeck.tsx | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 ui/controls/setupSteamDeck.tsx diff --git a/ui/controls/setupSteamDeck.tsx b/ui/controls/setupSteamDeck.tsx new file mode 100644 index 00000000..41495e17 --- /dev/null +++ b/ui/controls/setupSteamDeck.tsx @@ -0,0 +1,43 @@ +import { App } from '../App.tsx'; +import { FloatingGamepadTextInputMode } from './GamepadTextInput.tsx'; + +const inputTypes = new Set([ + 'text', + 'password', + 'search', + 'email', + 'number', + 'tel', + 'url', +]); +let currentElement: HTMLInputElement | null = null; +let setupHasRun = false; + +export default function setupSteamDeck() { + if (setupHasRun || !App.isSteamDeck()) { + return; + } + + setupHasRun = true; + + document.addEventListener('focusin', (event) => { + const input = event.target as HTMLInputElement | null; + if (input?.tagName === 'INPUT' && input !== currentElement) { + if (!inputTypes.has(input.type)) { + return; + } + + currentElement = input; + setTimeout(() => (currentElement = null), 1000); + + const dimensions = input.getBoundingClientRect(); + App.showFloatingGamepadTextInput( + FloatingGamepadTextInputMode.SingleLine, + Math.max(0, dimensions.x - window.scrollX || 0), + Math.max(0, dimensions.y - window.scrollY || 0), + dimensions.width || 200, + dimensions.height || 40, + ); + } + }); +}