chore(control): Keyboard queue (#31)

Co-authored-by: 2mal3 <56305732+2mal3@users.noreply.github.com>
This commit is contained in:
E44
2026-01-19 16:28:24 -05:00
committed by GitHub
parent f2a648b429
commit 3a30aca1dc
4 changed files with 28 additions and 13 deletions
@@ -201,7 +201,7 @@ export async function run_on_all_selected_displays(
); );
const displays: Display[] = maybe_displays.filter((d): d is Display => d !== null); const displays: Display[] = maybe_displays.filter((d): d is Display => d !== null);
Promise.all( await Promise.all(
displays.map(async (display) => { displays.map(async (display) => {
if (!display || (ignore_offline && display.status === 'host_offline')) return; if (!display || (ignore_offline && display.status === 'host_offline')) return;
await run_function(display); await run_function(display);
+9
View File
@@ -113,3 +113,12 @@ export function get_sanitized_file_url(file_path: string, is_preview = false) {
return `/file/${is_preview ? 'preview/' : ''}${[...pathSegments].join('/')}`; return `/file/${is_preview ? 'preview/' : ''}${[...pathSegments].join('/')}`;
} }
let keyboard_queue = Promise.resolve();
export function add_to_keyboard_queue(task: () => Promise<void>) {
keyboard_queue = keyboard_queue.then(task).catch((err) => {
console.error('Error in input queue:', err);
});
}
@@ -34,6 +34,7 @@
import { db } from '$lib/ts/database'; import { db } from '$lib/ts/database';
import { liveQuery, type Observable } from 'dexie'; import { liveQuery, type Observable } from 'dexie';
import TextInput from '$lib/components/TextInput.svelte'; import TextInput from '$lib/components/TextInput.svelte';
import { add_to_keyboard_queue } from '$lib/ts/utils';
let all_display_states: Observable<'on' | 'off' | 'mixed'> | undefined = $state(); let all_display_states: Observable<'on' | 'off' | 'mixed'> | undefined = $state();
$effect(() => { $effect(() => {
@@ -206,11 +207,11 @@
? 'text-stone-500 cursor-not-allowed' ? 'text-stone-500 cursor-not-allowed'
: 'hover:bg-stone-600 active:bg-stone-500 cursor-pointer'} py-2 rounded-xl flex justify-center items-center transition-colors duration-200" : 'hover:bg-stone-600 active:bg-stone-500 cursor-pointer'} py-2 rounded-xl flex justify-center items-center transition-colors duration-200"
disabled={$selected_display_ids.length === 0} disabled={$selected_display_ids.length === 0}
onmousedown={async () => { onmousedown={() => {
await send_single_key_press('ArrowLeft', 'press'); add_to_keyboard_queue(async () => await send_single_key_press('ArrowLeft', 'press'));
}} }}
onmouseup={async () => { onmouseup={() => {
await send_single_key_press('ArrowLeft', 'release'); add_to_keyboard_queue(async () => await send_single_key_press('ArrowLeft', 'release'));
}} }}
> >
<ArrowBigLeft /> <ArrowBigLeft />
@@ -222,11 +223,11 @@
? 'text-stone-500 cursor-not-allowed' ? 'text-stone-500 cursor-not-allowed'
: 'hover:bg-stone-600 active:bg-stone-500 cursor-pointer'} py-2 rounded-xl flex justify-center items-center transition-colors duration-200" : 'hover:bg-stone-600 active:bg-stone-500 cursor-pointer'} py-2 rounded-xl flex justify-center items-center transition-colors duration-200"
disabled={$selected_display_ids.length === 0} disabled={$selected_display_ids.length === 0}
onmousedown={async () => { onmousedown={() => {
await send_single_key_press('ArrowRight', 'press'); add_to_keyboard_queue(async () => await send_single_key_press('ArrowRight', 'press'));
}} }}
onmouseup={async () => { onmouseup={() => {
await send_single_key_press('ArrowRight', 'release'); add_to_keyboard_queue(async () => await send_single_key_press('ArrowRight', 'release'));
}} }}
> >
<ArrowBigRight /> <ArrowBigRight />
+7 -2
View File
@@ -7,6 +7,7 @@
import { ArrowDownToLine, ArrowUpFromLine, Grid2x2, Grid2X2, Option } from 'lucide-svelte'; import { ArrowDownToLine, ArrowUpFromLine, Grid2x2, Grid2X2, Option } from 'lucide-svelte';
import Button from '$lib/components/Button.svelte'; import Button from '$lib/components/Button.svelte';
import { onDestroy } from 'svelte'; import { onDestroy } from 'svelte';
import { add_to_keyboard_queue } from '$lib/ts/utils';
let { let {
popup_close_function popup_close_function
@@ -58,17 +59,21 @@
const action: 'press' | 'release' = key_down ? 'press' : 'release'; const action: 'press' | 'release' = key_down ? 'press' : 'release';
add_to_last_keys(action.toUpperCase() + ' ' + key); add_to_last_keys(action.toUpperCase() + ' ' + key);
add_to_keyboard_queue(async () => {
await run_on_all_selected_displays((d) => send_keyboard_input(d.ip, [{ key, action }]), true); await run_on_all_selected_displays((d) => send_keyboard_input(d.ip, [{ key, action }]), true);
});
} }
async function release_all_pressed_keys() { function release_all_pressed_keys() {
const inputs: { key: string; action: 'press' | 'release' }[] = []; const inputs: { key: string; action: 'press' | 'release' }[] = [];
for (let i = current_keys.length - 1; i >= 0; i--) { for (let i = current_keys.length - 1; i >= 0; i--) {
inputs.push({ key: current_keys[i], action: 'release' }); inputs.push({ key: current_keys[i], action: 'release' });
current_keys.splice(i, 1); current_keys.splice(i, 1);
} }
add_to_keyboard_queue(async () => {
await run_on_all_selected_displays((d) => send_keyboard_input(d.ip, inputs), true); await run_on_all_selected_displays((d) => send_keyboard_input(d.ip, inputs), true);
});
} }
onDestroy(() => { onDestroy(() => {
@@ -91,7 +96,7 @@
}} }}
onblur={async () => { onblur={async () => {
active = false; active = false;
await release_all_pressed_keys(); release_all_pressed_keys();
}} }}
onkeydown={(e) => on_keyboard_input(e, true)} onkeydown={(e) => on_keyboard_input(e, true)}
onkeyup={(e) => on_keyboard_input(e, false)} onkeyup={(e) => on_keyboard_input(e, false)}