2023-03-18 23:14:59 +08:00
|
|
|
import { invoke } from '@tauri-apps/api';
|
|
|
|
import { listen } from '@tauri-apps/api/event';
|
2023-03-20 09:27:47 +08:00
|
|
|
import { convertFileSrc } from '@tauri-apps/api/tauri';
|
2023-03-19 10:15:26 +08:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
createEffect,
|
|
|
|
createSignal,
|
|
|
|
JSX,
|
|
|
|
onCleanup,
|
2023-03-20 09:27:47 +08:00
|
|
|
onMount,
|
2023-03-19 10:15:26 +08:00
|
|
|
splitProps,
|
|
|
|
} from 'solid-js';
|
2023-03-18 23:14:59 +08:00
|
|
|
|
|
|
|
type ScreenViewProps = {
|
|
|
|
displayId: number;
|
2023-03-20 09:27:47 +08:00
|
|
|
height: number;
|
|
|
|
width: number;
|
|
|
|
} & Omit<JSX.HTMLAttributes<HTMLCanvasElement>, 'height' | 'width'>;
|
2023-03-18 23:14:59 +08:00
|
|
|
|
|
|
|
async function subscribeScreenshotUpdate(displayId: number) {
|
|
|
|
await invoke('subscribe_encoded_screenshot_updated', {
|
|
|
|
displayId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ScreenView: Component<ScreenViewProps> = (props) => {
|
2023-03-19 10:15:26 +08:00
|
|
|
const [localProps, rootProps] = splitProps(props, ['displayId']);
|
2023-03-20 09:27:47 +08:00
|
|
|
let canvas: HTMLCanvasElement;
|
|
|
|
const [ctx, setCtx] = createSignal<CanvasRenderingContext2D | null>(null);
|
2023-03-18 23:14:59 +08:00
|
|
|
createEffect(() => {
|
2023-03-20 09:27:47 +08:00
|
|
|
const unlisten = listen<{
|
|
|
|
base64_image: string;
|
|
|
|
display_id: number;
|
|
|
|
height: number;
|
|
|
|
width: number;
|
|
|
|
}>('encoded-screenshot-updated', (event) => {
|
|
|
|
if (event.payload.display_id === localProps.displayId) {
|
|
|
|
const url = convertFileSrc(
|
|
|
|
`displays/${localProps.displayId}?width=${canvas.width}&height=${canvas.height}`,
|
|
|
|
'ambient-light',
|
|
|
|
);
|
|
|
|
fetch(url, {
|
|
|
|
mode: 'cors',
|
|
|
|
})
|
|
|
|
.then((res) => res.body?.getReader().read())
|
|
|
|
.then((buffer) => {
|
|
|
|
console.log(buffer?.value?.length);
|
|
|
|
|
|
|
|
let _ctx = ctx();
|
|
|
|
if (_ctx && buffer?.value) {
|
|
|
|
_ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
const img = new ImageData(
|
|
|
|
new Uint8ClampedArray(buffer.value),
|
|
|
|
canvas.width,
|
|
|
|
canvas.height,
|
|
|
|
);
|
|
|
|
_ctx.putImageData(img, 0, 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log(event.payload.display_id, localProps.displayId);
|
|
|
|
});
|
2023-03-19 10:15:26 +08:00
|
|
|
subscribeScreenshotUpdate(localProps.displayId);
|
2023-03-18 23:14:59 +08:00
|
|
|
|
2023-03-20 09:27:47 +08:00
|
|
|
onMount(() => {
|
|
|
|
setCtx(canvas.getContext('2d'));
|
|
|
|
});
|
|
|
|
|
2023-03-18 23:14:59 +08:00
|
|
|
onCleanup(() => {
|
|
|
|
unlisten.then((unlisten) => {
|
|
|
|
unlisten();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-20 09:27:47 +08:00
|
|
|
return <canvas ref={canvas!} class="object-contain" {...rootProps} />;
|
2023-03-18 23:14:59 +08:00
|
|
|
};
|