2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-10-19 13:05:20 +00:00

chore: save shared modman info in context

This commit is contained in:
Jan Laupetin
2025-10-10 10:57:07 +01:00
parent 9845b0a889
commit 219f0c1c85
7 changed files with 68 additions and 64 deletions

View File

@@ -0,0 +1,7 @@
#include "ModManContext.h"
ModManContext& ModManContext::Get()
{
static ModManContext context;
return context;
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include "Web/WebViewLib.h"
#include <memory>
class ModManContext
{
public:
static ModManContext& Get();
std::unique_ptr<webview::webview> m_main_webview;
std::unique_ptr<webview::webview> m_dev_tools_webview;
};

View File

@@ -0,0 +1,11 @@
#include "Binds.h"
#include "Web/Binds/DialogBinds.h"
namespace ui
{
void RegisterAllBinds(webview::webview& wv)
{
RegisterDialogHandlerBinds(wv);
}
} // namespace ui

View File

@@ -0,0 +1,8 @@
#pragma once
#include "Web/WebViewLib.h"
namespace ui
{
void RegisterAllBinds(webview::webview& wv);
}

View File

@@ -1,5 +1,6 @@
#include "GitVersion.h"
#include "Web/Binds/DialogBinds.h"
#include "Context/ModManContext.h"
#include "GitVersion.h"
#include "Web/Binds/Binds.h"
#include "Web/Platform/AssetHandler.h"
#include "Web/UiCommunication.h"
#include "Web/ViteAssets.h"
@@ -7,7 +8,6 @@
#include <format>
#include <iostream>
#include <optional>
#include <string>
#include <thread>
@@ -21,15 +21,17 @@ using namespace PLATFORM_NAMESPACE;
namespace
{
#ifdef _DEBUG
std::optional<webview::webview> devToolWindow;
void RunDevToolsWindow()
void SpawnDevToolsWindow()
{
con::debug("Creating dev tools window");
auto& context = ModManContext::Get();
try
{
auto& newWindow = devToolWindow.emplace(false, nullptr);
context.m_dev_tools_webview = std::make_unique<webview::webview>(false, nullptr);
auto& newWindow = *context.m_dev_tools_webview;
newWindow.set_title("Devtools");
newWindow.set_size(640, 480, WEBVIEW_HINT_NONE);
newWindow.set_size(480, 320, WEBVIEW_HINT_MIN);
@@ -42,59 +44,44 @@ namespace
}
#endif
int RunMainWindow()
int SpawnMainWindow()
{
con::debug("Creating main window");
auto& context = ModManContext::Get();
try
{
webview::webview w(
context.m_main_webview = std::make_unique<webview::webview>(
#ifdef _DEBUG
true,
#else
false,
#endif
nullptr);
w.set_title("OpenAssetTools ModMan");
w.set_size(1280, 640, WEBVIEW_HINT_NONE);
w.set_size(480, 320, WEBVIEW_HINT_MIN);
auto& newWindow = *context.m_main_webview;
ui::RegisterDialogHandlerBinds(w);
newWindow.set_title("OpenAssetTools ModMan");
newWindow.set_size(1280, 640, WEBVIEW_HINT_NONE);
newWindow.set_size(480, 320, WEBVIEW_HINT_MIN);
// A binding that counts up or down and immediately returns the new value.
ui::Bind<std::string, std::string>(w,
"greet",
[&w](std::string name) -> std::string
{
ui::Notify(w, "greeting", name);
return std::format("Hello from C++ {}!", name);
});
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE)
InstallAssetHandler(w);
constexpr auto urlPrefix = URL_PREFIX;
#elif defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
InstallAssetHandler(w);
constexpr auto urlPrefix = URL_PREFIX;
#else
#error Unsupported platform
#endif
InstallAssetHandler(newWindow);
ui::RegisterAllBinds(newWindow);
#ifdef _DEBUG
w.navigate(VITE_DEV_SERVER ? std::format("http://localhost:{}", VITE_DEV_SERVER_PORT) : std::format("{}index.html", urlPrefix));
newWindow.navigate(VITE_DEV_SERVER ? std::format("http://localhost:{}", VITE_DEV_SERVER_PORT) : std::format("{}index.html", URL_PREFIX));
if (VITE_DEV_SERVER)
{
w.dispatch(
newWindow.dispatch(
[]
{
RunDevToolsWindow();
SpawnDevToolsWindow();
});
}
#else
w.navigate(std::format("{}index.html", urlPrefix));
newWindow.navigate(std::format("{}index.html", URL_PREFIX));
#endif
w.run();
newWindow.run();
}
catch (const webview::exception& e)
{
@@ -129,7 +116,7 @@ int main()
con::info("Starting ModMan " GIT_VERSION);
const auto result = RunMainWindow();
const auto result = SpawnMainWindow();
return result;
}

View File

@@ -1,28 +1,13 @@
<script setup lang="ts">
import { onUnmounted, ref } from "vue";
import { webviewBinds, webviewAddEventListener, webviewRemoveEventListener } from "./native";
import { ref } from "vue";
import { webviewBinds } from "./native";
const greetMsg = ref("");
const lastPersonGreeted = ref("");
const lastPath = ref("");
const name = ref("");
async function greet() {
greetMsg.value = await webviewBinds.greet(name.value);
}
function onPersonGreeted(person: string) {
lastPersonGreeted.value = person;
}
async function onOpenFastfileClick() {
lastPath.value =
(await webviewBinds.openFileDialog({ filters: [{ name: "Fastfiles", filter: "*.ff" }] })) ?? "";
}
webviewAddEventListener("greeting", onPersonGreeted);
onUnmounted(() => webviewRemoveEventListener("greeting", onPersonGreeted));
</script>
<template>
@@ -30,12 +15,6 @@ onUnmounted(() => webviewRemoveEventListener("greeting", onPersonGreeted));
<h1>Welcome to ModMan</h1>
<small>Nothing to see here yet, this is mainly for testing</small>
<form class="row" @submit.prevent="greet">
<input id="greet-input" v-model="name" placeholder="Enter a name..." autocomplete="off" />
<button type="submit">Greet</button>
</form>
<p>{{ greetMsg }}</p>
<p>The last person greeted is: {{ lastPersonGreeted }}</p>
<p>
<button @click="onOpenFastfileClick">Open fastfile</button>
<span>The last path: {{ lastPath }}</span>

View File

@@ -1,12 +1,10 @@
import type { DialogBinds } from "./DialogBinds";
export type NativeMethods = {
greet(name: string): Promise<string>;
} & DialogBinds;
export type NativeMethods = DialogBinds;
interface NativeEventMap {
greeting: string;
}
type WebViewExtensions = {