feat: periodically update os-layout in the background

fix: remove dead code in layout detection
fixes #78
resolves #79
This commit is contained in:
2024-01-03 13:55:50 +01:00
parent 0e57e810e0
commit a1fe6f7110
2 changed files with 28 additions and 38 deletions

View File

@@ -1,40 +1,25 @@
import {persistentWritable} from "$lib/storage" import {get, writable} from "svelte/store"
import {get} from "svelte/store"
export const osLayout = persistentWritable<Record<string, string>>("os-layout", {}) export const osLayout = writable<Map<string, string>>(new Map())
const keysCurrentlyDown = new Set<string>() async function updateLayout() {
const layout: Map<string, string> = await (navigator as any).keyboard.getLayoutMap()
function keydown({code, key}: KeyboardEvent) { const currentLayout = get(osLayout)
const keys = [...keysCurrentlyDown] if (
keysCurrentlyDown.add(code) layout.size !== currentLayout.size ||
[...layout.keys()].some(key => layout.get(key) !== currentLayout.get(key))
const keyString = JSON.stringify([...keys.sort(), code]) ) {
if (keyString in get(osLayout) || get(osLayout)[JSON.stringify([code])] === key) return osLayout.set(layout)
}
osLayout.update(layout => { }
layout[keyString] = key
return layout export function runLayoutDetection(): () => void {
}) if ("keyboard" in navigator) {
} updateLayout()
const timer = setInterval(updateLayout, 5000)
function keyup({code}: KeyboardEvent) { return () => clearInterval(timer)
keysCurrentlyDown.delete(code) } else {
} console.warn("Keyboard API not supported")
return () => {}
export function runLayoutDetection() {
if ("keyboard" in navigator) {
;(navigator.keyboard as any).getLayoutMap().then((layout: Map<string, string>) => {
osLayout.update(osLayout => {
Object.assign(
osLayout,
Object.fromEntries([...layout.entries()].map(([key, value]) => [JSON.stringify([key]), value])),
)
return osLayout
})
})
} }
// TODO: do we want this?
//window.addEventListener("keydown", keydown)
//window.addEventListener("keyup", keyup)
} }

View File

@@ -4,7 +4,7 @@
import "$lib/style/scrollbar.scss" import "$lib/style/scrollbar.scss"
import "$lib/style/tippy.scss" import "$lib/style/tippy.scss"
import "$lib/style/theme.scss" import "$lib/style/theme.scss"
import {onMount} from "svelte" import {onDestroy, onMount} from "svelte"
import {applyTheme, argbFromHex, themeFromSourceColor} from "@material/material-color-utilities" import {applyTheme, argbFromHex, themeFromSourceColor} from "@material/material-color-utilities"
import Navigation from "./Navigation.svelte" import Navigation from "./Navigation.svelte"
import {canAutoConnect} from "$lib/serial/device" import {canAutoConnect} from "$lib/serial/device"
@@ -29,9 +29,10 @@
const locale = ((browser && localStorage.getItem("locale")) as Locales) || detectLocale() const locale = ((browser && localStorage.getItem("locale")) as Locales) || detectLocale()
loadLocale(locale) loadLocale(locale)
setLocale(locale) setLocale(locale)
let stopLayoutDetection: () => void
if (browser) { if (browser) {
runLayoutDetection() stopLayoutDetection = runLayoutDetection()
tippy.setDefaultProps({ tippy.setDefaultProps({
animation: "shift-away", animation: "shift-away",
theme: "surface-variant", theme: "surface-variant",
@@ -66,6 +67,10 @@
} }
}) })
onDestroy(() => {
stopLayoutDetection?.()
})
let webManifestLink = "" let webManifestLink = ""
</script> </script>