more tweaks to mwinlib and input focus fix. work on keybind listener class, should test now
This commit is contained in:
@@ -95,6 +95,7 @@ class MWinController {
|
|||||||
this.#savedStyles = this.#node.style.cssText;
|
this.#savedStyles = this.#node.style.cssText;
|
||||||
this.#savedTransition = this.#node.style.transition;
|
this.#savedTransition = this.#node.style.transition;
|
||||||
this.#node.style.transition = 'none';
|
this.#node.style.transition = 'none';
|
||||||
|
this.#node.style.willChange = 'transform';
|
||||||
|
|
||||||
this.#wrapper.style.position = 'relative';
|
this.#wrapper.style.position = 'relative';
|
||||||
this.#node.style.position = 'absolute';
|
this.#node.style.position = 'absolute';
|
||||||
@@ -347,6 +348,10 @@ class MWinController {
|
|||||||
if (!this.#pendingApply) {
|
if (!this.#pendingApply) {
|
||||||
this.#pendingApply = true;
|
this.#pendingApply = true;
|
||||||
this.#rafID = requestAnimationFrame(() => {
|
this.#rafID = requestAnimationFrame(() => {
|
||||||
|
if (!this.state.moving && !this.state.resizing) {
|
||||||
|
this.#node.style.willChange = 'auto';
|
||||||
|
}
|
||||||
|
|
||||||
this.#rafID = null;
|
this.#rafID = null;
|
||||||
this.#pendingApply = false;
|
this.#pendingApply = false;
|
||||||
this.#apply();
|
this.#apply();
|
||||||
@@ -430,6 +435,7 @@ class MWinController {
|
|||||||
this.#node.style.transition = this.#savedTransition;
|
this.#node.style.transition = this.#savedTransition;
|
||||||
this.#node.style.willChange = 'auto';
|
this.#node.style.willChange = 'auto';
|
||||||
this.#node.style.touchAction = '';
|
this.#node.style.touchAction = '';
|
||||||
|
this.#node.style.cursor = 'auto';
|
||||||
|
|
||||||
window.removeEventListener('pointermove', this.#onPointerMove);
|
window.removeEventListener('pointermove', this.#onPointerMove);
|
||||||
window.removeEventListener('pointerup', this.#onPointerUp);
|
window.removeEventListener('pointerup', this.#onPointerUp);
|
||||||
@@ -522,7 +528,7 @@ class MWinController {
|
|||||||
this.state.height = this.#wrapperHeight;
|
this.state.height = this.#wrapperHeight;
|
||||||
this.state.maximized = true;
|
this.state.maximized = true;
|
||||||
}
|
}
|
||||||
this.#scheduleApply();
|
this.#apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
close(): void {
|
close(): void {
|
||||||
|
|||||||
@@ -80,10 +80,17 @@ export function getWrapperManager(wrapper: HTMLElement): WrapperManager {
|
|||||||
|
|
||||||
export function resolveElement(
|
export function resolveElement(
|
||||||
input: HTMLElement | (() => HTMLElement) | string | undefined,
|
input: HTMLElement | (() => HTMLElement) | string | undefined,
|
||||||
fallback?: HTMLElement
|
relativeTo: HTMLElement
|
||||||
): HTMLElement | null {
|
): HTMLElement | null {
|
||||||
if (!input) return null;
|
if (!input) return null;
|
||||||
|
|
||||||
|
if (input instanceof HTMLElement) return input;
|
||||||
|
|
||||||
if (typeof input === 'function') return input();
|
if (typeof input === 'function') return input();
|
||||||
if (typeof input === 'string') return fallback?.closest(input) ?? document.querySelector(input);
|
|
||||||
return input;
|
if (typeof input === 'string') {
|
||||||
|
return relativeTo.closest(input) as HTMLElement | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
export class KeybindListener {
|
||||||
|
#abortController: AbortController;
|
||||||
|
#eventStack: KeyboardEvent[] = [];
|
||||||
|
#bindTable: Map<Keybind, KeybindOptions> = new Map();
|
||||||
|
|
||||||
|
constructor(elem: HTMLElement) {
|
||||||
|
this.#abortController = new AbortController();
|
||||||
|
|
||||||
|
elem.addEventListener('keydown', (e) => this.#captureEvent(e), {
|
||||||
|
signal: this.#abortController.signal,
|
||||||
|
passive: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#captureEvent(e: KeyboardEvent) {
|
||||||
|
if (e.altKey || e.shiftKey || e.ctrlKey) {
|
||||||
|
this.#eventStack.push(e);
|
||||||
|
}
|
||||||
|
this.#processEvent(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
#processEvent(e: KeyboardEvent) {
|
||||||
|
const keybind: Keybind = {
|
||||||
|
key: e.key,
|
||||||
|
modifiers: this.#pushModifiers()
|
||||||
|
};
|
||||||
|
|
||||||
|
const value = this.#bindTable.get(keybind);
|
||||||
|
if (!value) return;
|
||||||
|
|
||||||
|
value?.function?.(e);
|
||||||
|
console.log(`[keybind-listener] - executed a function under a keybind: ${keybind}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
#pushModifiers(): Modifier[] {
|
||||||
|
const mods: Modifier[] = [];
|
||||||
|
|
||||||
|
for (const event of this.#eventStack) {
|
||||||
|
mods.push(event.key as Modifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mods;
|
||||||
|
}
|
||||||
|
|
||||||
|
registerKeybinds(keybinds: { keybind: Keybind; options: KeybindOptions }[]): void {
|
||||||
|
for (const value of keybinds) {
|
||||||
|
this.#bindTable.set(value.keybind, {
|
||||||
|
function: value.options.function,
|
||||||
|
no_inhibit: value.options.no_inhibit ?? false
|
||||||
|
});
|
||||||
|
console.log(`[keybind-listener] - keybind registered under the key: ${value.keybind}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Keybind = {
|
||||||
|
key: string;
|
||||||
|
modifiers?: Modifier[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type KeybindOptions = {
|
||||||
|
function: (e: KeyboardEvent) => void;
|
||||||
|
no_inhibit?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Modifier = 'Control' | 'Alt' | 'Shift';
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import type { Keybind, KeybindOptions } from "../terminal/keybind_listenter";
|
||||||
|
|
||||||
|
export const SHELL_KEYBINDS: { keybind: Keybind; options: KeybindOptions }[] = [
|
||||||
|
{
|
||||||
|
keybind: {
|
||||||
|
key: 'c',
|
||||||
|
modifiers: ['Control']
|
||||||
|
}
|
||||||
|
options: {
|
||||||
|
function
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
+22
-16
@@ -1,6 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { movable } from '$lib/mwinlib/mwin_controller.svelte';
|
import { mwin_movable } from '$lib/mwinlib/mwin_controller.svelte';
|
||||||
import type { MovableAPI } from '$lib/mwinlib/mwin_util';
|
import type { MWinAPI } from '$lib/mwinlib/mwin_util';
|
||||||
import Input from './terminal/Input.svelte';
|
import Input from './terminal/Input.svelte';
|
||||||
|
|
||||||
let { username }: { username: string } = $props();
|
let { username }: { username: string } = $props();
|
||||||
@@ -50,39 +50,41 @@
|
|||||||
const hostname = 'terminal'; //TODO: Change this to pull from config on db
|
const hostname = 'terminal'; //TODO: Change this to pull from config on db
|
||||||
//let isProcessActive = $state(false);
|
//let isProcessActive = $state(false);
|
||||||
|
|
||||||
let movableAPI: MovableAPI;
|
let mwin: MWinAPI;
|
||||||
let inputValue = $state('');
|
let inputValue = $state('');
|
||||||
|
let inputComponent: any;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="terminal-wrapper relative flex h-screen w-full flex-col items-center justify-center">
|
<div
|
||||||
|
id="terminal-wrapper"
|
||||||
|
class=" relative flex h-screen w-full flex-col items-center justify-center"
|
||||||
|
>
|
||||||
<div
|
<div
|
||||||
class="absolute flex size-full h-256 w-512 flex-col rounded-md shadow-subtle transition-discrete will-change-auto"
|
class="absolute flex size-full h-256 w-512 flex-col rounded-md shadow-subtle transition-discrete will-change-auto"
|
||||||
use:movable={{
|
use:mwin_movable={{
|
||||||
wrapper: '.terminal-wrapper',
|
wrapper: '#terminal-wrapper',
|
||||||
handle: '.terminal-bar',
|
handle: '#terminal-bar',
|
||||||
baseZIndex: 10,
|
baseZIndex: 10,
|
||||||
resizable: { threshold: 24, minWidth: 500, minHeight: 300 },
|
resizable: { threshold: 24, minWidth: 500, minHeight: 300 },
|
||||||
onAPI: (api) => (movableAPI = api)
|
onAPI: (api) => (mwin = api)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
class="terminal-bar flex h-9 w-full flex-row items-center rounded-t-md bg-surface-2 text-center font-terminal text-sm font-bold text-primary select-none"
|
id="terminal-bar"
|
||||||
|
class=" flex h-9 w-full flex-row items-center rounded-t-md bg-surface-2 text-center font-terminal text-sm font-bold text-primary select-none"
|
||||||
>
|
>
|
||||||
<div class="dots-wrapper mx-3 flex h-full flex-row items-center justify-center gap-3">
|
<div class="dots-wrapper mx-3 flex h-full flex-row items-center justify-center gap-3">
|
||||||
<button
|
<button class="size-3 cursor-pointer rounded-full p-0" title="" onclick={() => mwin.close()}
|
||||||
class="size-3 cursor-pointer rounded-full p-0"
|
|
||||||
title=""
|
|
||||||
onclick={() => movableAPI.close()}
|
|
||||||
></button>
|
></button>
|
||||||
<button
|
<button
|
||||||
class="size-3 cursor-pointer rounded-full p-0"
|
class="size-3 cursor-pointer rounded-full p-0"
|
||||||
title=""
|
title=""
|
||||||
onclick={() => movableAPI.maximize()}
|
onclick={() => mwin.maximize()}
|
||||||
></button>
|
></button>
|
||||||
<button
|
<button
|
||||||
class="size-3 cursor-pointer rounded-full p-0"
|
class="size-3 cursor-pointer rounded-full p-0"
|
||||||
title=""
|
title=""
|
||||||
onclick={() => movableAPI.minimize()}
|
onclick={() => mwin.minimize()}
|
||||||
></button>
|
></button>
|
||||||
</div>
|
</div>
|
||||||
<div class=" mr-2 flex grow text-sm">
|
<div class=" mr-2 flex grow text-sm">
|
||||||
@@ -92,9 +94,13 @@
|
|||||||
<div
|
<div
|
||||||
class="inner-content scroll-hidden grow origin-top overflow-y-auto rounded-b-md bg-surface-1 p-4 text-sm text-text-primary"
|
class="inner-content scroll-hidden grow origin-top overflow-y-auto rounded-b-md bg-surface-1 p-4 text-sm text-text-primary"
|
||||||
id="cout"
|
id="cout"
|
||||||
|
onclick={() => inputComponent?.focusInput()}
|
||||||
|
onkeydown={() => inputComponent?.focusInput()}
|
||||||
|
role="textbox"
|
||||||
|
tabindex="-1"
|
||||||
>
|
>
|
||||||
<div id="outputWrapper"></div>
|
<div id="outputWrapper"></div>
|
||||||
<Input bind:value={inputValue} blinkDelay={500} />
|
<Input bind:value={inputValue} blinkDelay={500} bind:this={inputComponent} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { KeybindListener } from '$lib/termos/terminal/keybind_listenter';
|
||||||
|
import { SHELL_KEYBINDS } from '$lib/termos/util/shell_keybinds';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
let {
|
let {
|
||||||
@@ -13,11 +15,11 @@
|
|||||||
oninput?: (value: string) => void;
|
oninput?: (value: string) => void;
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
let container: HTMLDivElement | undefined;
|
let container: HTMLElement | undefined;
|
||||||
let cursorPos: number = $derived(value.length);
|
let cursorPos: number = $derived(value.length);
|
||||||
let text: string = $state('');
|
let text: string = $state('');
|
||||||
let focused: boolean = $state(false);
|
let focused: boolean = $state(false);
|
||||||
let blinkPaused: boolean = $state(false); // true → cursor steady, false → blinking
|
let blinkPaused: boolean = $state(false);
|
||||||
let blinkTimer: ReturnType<typeof setTimeout> | null = null;
|
let blinkTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
@@ -35,6 +37,10 @@
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export function focusInput(): void {
|
||||||
|
container?.focus();
|
||||||
|
}
|
||||||
|
|
||||||
function pauseBlink() {
|
function pauseBlink() {
|
||||||
blinkPaused = true;
|
blinkPaused = true;
|
||||||
if (blinkTimer) clearTimeout(blinkTimer);
|
if (blinkTimer) clearTimeout(blinkTimer);
|
||||||
@@ -177,6 +183,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
const keybindListener = new KeybindListener(container!);
|
||||||
|
|
||||||
|
keybindListener.registerKeybinds(SHELL_KEYBINDS);
|
||||||
|
|
||||||
if (autofocus && container) {
|
if (autofocus && container) {
|
||||||
container.focus();
|
container.focus();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,5 @@
|
|||||||
<script lang="ts">
|
<script>
|
||||||
import { mwin_movable } from '$lib/mwinlib/mwin_controller.svelte';
|
import Terminal from '../../modules/Terminal.svelte';
|
||||||
import type { MWinAPI } from '$lib/mwinlib/mwin_util';
|
|
||||||
|
|
||||||
let API: MWinAPI;
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="wrapper relative h-screen w-1/2 bg-blue-500">
|
<Terminal username="test"></Terminal>
|
||||||
<div
|
|
||||||
class="absolute h-128 w-5xl bg-red-400 will-change-auto"
|
|
||||||
use:mwin_movable={{
|
|
||||||
wrapper: '.wrapper',
|
|
||||||
baseZIndex: 10,
|
|
||||||
resizable: { threshold: 24 },
|
|
||||||
onAPI: (api) => (API = api)
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div></div>
|
|
||||||
<button onclick={() => API.maximize()}>click me</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user