2023-03-18 23:14:59 +08:00
|
|
|
import { invoke } from '@tauri-apps/api';
|
|
|
|
import { listen } from '@tauri-apps/api/event';
|
2023-03-19 10:15:26 +08:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
createEffect,
|
|
|
|
createSignal,
|
|
|
|
JSX,
|
|
|
|
onCleanup,
|
|
|
|
splitProps,
|
|
|
|
} from 'solid-js';
|
2023-03-18 23:14:59 +08:00
|
|
|
|
|
|
|
type ScreenViewProps = {
|
|
|
|
displayId: number;
|
2023-03-19 10:15:26 +08:00
|
|
|
} & JSX.HTMLAttributes<HTMLImageElement>;
|
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-18 23:14:59 +08:00
|
|
|
const [image, setImage] = createSignal<string>();
|
|
|
|
createEffect(() => {
|
|
|
|
const unlisten = listen<{ base64_image: string; display_id: number }>(
|
|
|
|
'encoded-screenshot-updated',
|
|
|
|
(event) => {
|
2023-03-19 10:15:26 +08:00
|
|
|
if (event.payload.display_id === localProps.displayId) {
|
2023-03-18 23:14:59 +08:00
|
|
|
setImage(event.payload.base64_image);
|
|
|
|
}
|
|
|
|
|
2023-03-19 10:15:26 +08:00
|
|
|
console.log(event.payload.display_id, localProps.displayId);
|
2023-03-18 23:14:59 +08:00
|
|
|
},
|
|
|
|
);
|
2023-03-19 10:15:26 +08:00
|
|
|
subscribeScreenshotUpdate(localProps.displayId);
|
2023-03-18 23:14:59 +08:00
|
|
|
|
|
|
|
onCleanup(() => {
|
|
|
|
unlisten.then((unlisten) => {
|
|
|
|
unlisten();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-19 10:15:26 +08:00
|
|
|
return <img src={image()} class="object-contain" {...rootProps} />;
|
2023-03-18 23:14:59 +08:00
|
|
|
};
|