2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-10-09 08:16:41 +00:00

chore: use new webview fork notify mechanism

This commit is contained in:
Jan Laupetin
2025-10-07 21:39:04 +01:00
parent 5310ae7b8b
commit 2639640ca3
4 changed files with 35 additions and 6 deletions

View File

@@ -68,6 +68,7 @@ namespace
[&](const std::string& req) -> std::string [&](const std::string& req) -> std::string
{ {
const auto name = req.substr(2, req.size() - 4); const auto name = req.substr(2, req.size() - 4);
w.notify("greeting", webview::json_escape(name));
return webview::json_escape(std::format("Hello from C++ {}!", name)); return webview::json_escape(std::format("Hello from C++ {}!", name));
}); });

View File

@@ -1,13 +1,22 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from "vue"; import { onUnmounted, ref } from "vue";
import { nativeMethods } from "./native"; import { webviewBinds, webViewAddEventListener, webViewRemoveEventListener } from "./native";
const greetMsg = ref(""); const greetMsg = ref("");
const lastPersonGreeted = ref("");
const name = ref(""); const name = ref("");
async function greet() { async function greet() {
greetMsg.value = await nativeMethods.greet(name.value); greetMsg.value = await webviewBinds.greet(name.value);
} }
function onPersonGreeted(person: string) {
lastPersonGreeted.value = person;
}
webViewAddEventListener("greeting", onPersonGreeted);
onUnmounted(() => webViewRemoveEventListener("greeting", onPersonGreeted));
</script> </script>
<template> <template>
@@ -19,6 +28,7 @@ async function greet() {
<button type="submit">Greet</button> <button type="submit">Greet</button>
</form> </form>
<p>{{ greetMsg }}</p> <p>{{ greetMsg }}</p>
<p>The last person greeted is: {{ lastPersonGreeted }}</p>
</main> </main>
</template> </template>

View File

@@ -2,6 +2,24 @@ export interface NativeMethods {
greet: (name: string) => Promise<string>; greet: (name: string) => Promise<string>;
} }
const windowWithWebViewExtensions = window as typeof window & {webview_binds: NativeMethods}; interface NativeEventMap {
greeting: string;
}
export const nativeMethods: NativeMethods = windowWithWebViewExtensions.webview_binds; type WebViewExtensions = {
webviewBinds: NativeMethods;
webViewAddEventListener<K extends keyof NativeEventMap>(
eventKey: K,
callback: (payload: NativeEventMap[K]) => void,
): void;
webViewRemoveEventListener<K extends keyof NativeEventMap>(
eventKey: K,
callback: (payload: NativeEventMap[K]) => void,
): boolean;
};
const windowWithWebViewExtensions = window as typeof window & WebViewExtensions;
export const webviewBinds = windowWithWebViewExtensions.webviewBinds;
export const webViewAddEventListener = windowWithWebViewExtensions.webViewAddEventListener;
export const webViewRemoveEventListener = windowWithWebViewExtensions.webViewRemoveEventListener;