2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-10-24 15:25:52 +00:00

feat: add nlohmann json based wrappers for webview binds,notify

This commit is contained in:
Jan Laupetin
2025-10-07 23:26:46 +01:00
parent cf584fe3eb
commit 49295448c8
11 changed files with 241 additions and 87 deletions

View File

@@ -2,11 +2,6 @@
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE)
#pragma warning(push, 0)
#include <webview/macros.h>
#include <webview/webview.h>
#pragma warning(pop)
#include "Web/UiAssets.h"
#include <Windows.h>
@@ -55,7 +50,7 @@ namespace
std::wstringstream wss;
wss << std::format(L"Content-Length: {}\n", contentLength);
wss << L"Content-Type: " << StringToWideString(GetMimeTypeForFileName(assetName));
wss << L"Content-Type: " << StringToWideString(ui::GetMimeTypeForFileName(assetName));
return wss.str();
}
@@ -151,7 +146,7 @@ namespace edge
{
void InstallCustomProtocolHandler(webview::webview& wv)
{
assetLookup = BuildUiFileLookup();
assetLookup = ui::BuildUiFileLookup();
const auto controller = static_cast<ICoreWebView2Controller*>(wv.browser_controller().value());
Microsoft::WRL::ComPtr<ICoreWebView2> core;

View File

@@ -4,9 +4,7 @@
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE)
#pragma warning(push, 0)
#include <webview/webview.h>
#pragma warning(pop)
#include "Web/WebViewLib.h"
namespace edge
{

View File

@@ -2,12 +2,6 @@
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <webview/macros.h>
#include <webview/webview.h>
#pragma GCC diagnostic pop
#include "Web/UiAssets.h"
#include <format>
@@ -27,7 +21,7 @@ namespace
gsize stream_length = foundUiFile->second.dataSize;
GInputStream* stream = g_memory_input_stream_new_from_data(foundUiFile->second.data, foundUiFile->second.dataSize, nullptr);
webkit_uri_scheme_request_finish(request, stream, stream_length, GetMimeTypeForFileName(foundUiFile->second.filename));
webkit_uri_scheme_request_finish(request, stream, stream_length, ui::GetMimeTypeForFileName(foundUiFile->second.filename));
g_object_unref(stream);
}
else
@@ -48,7 +42,7 @@ namespace gtk
const auto webView = WEBKIT_WEB_VIEW(widget);
const auto context = webkit_web_view_get_context(webView);
assetLookup = BuildUiFileLookup();
assetLookup = ui::BuildUiFileLookup();
webkit_web_context_register_uri_scheme(context, "modman", ModManUriSchemeRequestCb, NULL, nullptr);
}

View File

@@ -4,10 +4,7 @@
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <webview/webview.h>
#pragma GCC diagnostic pop
#include "Web/WebViewLib.h"
namespace gtk
{

View File

@@ -2,30 +2,33 @@
#include <format>
std::unordered_map<std::string, UiFile> BuildUiFileLookup()
namespace ui
{
std::unordered_map<std::string, UiFile> result;
for (const auto& asset : MOD_MAN_UI_FILES)
std::unordered_map<std::string, UiFile> BuildUiFileLookup()
{
result.emplace(std::format("/{}", asset.filename), asset);
std::unordered_map<std::string, UiFile> result;
for (const auto& asset : MOD_MAN_UI_FILES)
{
result.emplace(std::format("/{}", asset.filename), asset);
}
return result;
}
return result;
}
const char* GetMimeTypeForFileName(const std::string& fileName)
{
const char* mimeType;
const char* GetMimeTypeForFileName(const std::string& fileName)
{
const char* mimeType;
if (fileName.ends_with(".html"))
mimeType = "text/html";
else if (fileName.ends_with(".js"))
mimeType = "text/javascript";
else if (fileName.ends_with(".css"))
mimeType = "text/css";
else
mimeType = "application/octet-stream";
if (fileName.ends_with(".html"))
mimeType = "text/html";
else if (fileName.ends_with(".js"))
mimeType = "text/javascript";
else if (fileName.ends_with(".css"))
mimeType = "text/css";
else
mimeType = "application/octet-stream";
return mimeType;
}
return mimeType;
}
} // namespace ui

View File

@@ -5,5 +5,8 @@
#include <string>
#include <unordered_map>
std::unordered_map<std::string, UiFile> BuildUiFileLookup();
const char* GetMimeTypeForFileName(const std::string& fileName);
namespace ui
{
std::unordered_map<std::string, UiFile> BuildUiFileLookup();
const char* GetMimeTypeForFileName(const std::string& fileName);
} // namespace ui

View File

View File

@@ -0,0 +1,142 @@
#pragma once
#include "Utils/Logging/Log.h"
#include "WebViewLib.h"
#pragma warning(push, 0)
#include <nlohmann/json.hpp>
#pragma warning(pop)
namespace ui
{
inline void Bind(webview::webview& wv, const std::string& name, std::function<void()> fn)
{
wv.bind(name,
[fn](const std::string& req) -> std::string
{
fn();
return "";
});
}
template<typename TInput> void Bind(webview::webview& wv, const std::string& name, std::function<void(TInput)> fn)
{
wv.bind(name,
[fn](const std::string& req) -> std::string
{
TInput param;
try
{
const auto json = nlohmann::json::parse(req);
if (!json.is_array())
{
con::error("Webview params are not an array: {}", req);
return "";
}
param = json.at(0).get<TInput>();
}
catch (const nlohmann::json::exception& e)
{
con::error("Failed to parse json of webview call: {}", e.what());
return "";
}
fn(std::move(param));
return "";
});
}
template<typename TReturn> void BindRetOnly(webview::webview& wv, const std::string& name, std::function<TReturn()> fn)
{
wv.bind(name,
[fn](const std::string& req) -> std::string
{
auto result = fn();
return nlohmann::json(result).dump();
});
}
template<typename TInput, typename TReturn> void Bind(webview::webview& wv, const std::string& name, std::function<TReturn(TInput)> fn)
{
wv.bind(name,
[fn](const std::string& req) -> std::string
{
TInput param;
try
{
const auto json = nlohmann::json::parse(req);
if (!json.is_array())
{
con::error("Webview params are not an array: {}", req);
return "";
}
param = json.at(0).get<TInput>();
}
catch (const nlohmann::json::exception& e)
{
con::error("Failed to parse json of webview call: {}", e.what());
return "";
}
auto result = fn(std::move(param));
return nlohmann::json(result).dump();
});
}
inline void BindAsync(webview::webview& wv, const std::string& name, std::function<void(const std::string& id)> fn)
{
wv.bind(
name,
[fn](const std::string& id, const std::string& req, void* /* arg */)
{
fn(id);
},
nullptr);
}
template<typename TInput> void BindAsync(webview::webview& wv, const std::string& name, std::function<void(const std::string& id, TInput)> fn)
{
wv.bind(
name,
[fn](const std::string& id, const std::string& req, void* /* arg */)
{
TInput param;
try
{
const auto json = nlohmann::json::parse(req);
if (!json.is_array())
{
con::error("Webview params are not an array: {}", req);
return "";
}
param = json.at(0).get<TInput>();
}
catch (const nlohmann::json::exception& e)
{
con::error("Failed to parse json of webview call: {}", e.what());
return "";
}
fn(id, std::move(param));
},
nullptr);
}
template<typename TPayload> void PromiseResolve(webview::webview& wv, const std::string& id, const TPayload& payload)
{
wv.resolve(id, 0, nlohmann::json(payload).dump());
}
template<typename TPayload> void PromiseReject(webview::webview& wv, const std::string& id, const TPayload& payload)
{
wv.resolve(id, 1, nlohmann::json(payload).dump());
}
template<typename TPayload> void Notify(webview::webview& wv, const std::string& eventKey, const TPayload& payload)
{
wv.notify(eventKey, nlohmann::json(payload).dump());
}
} // namespace ui

View File

@@ -0,0 +1,20 @@
#pragma once
#ifdef _MSC_VER
#pragma warning(push, 0)
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <webview/webview.h>
#ifdef _MSC_VER
#pragma warning(pop)
#else
#pragma GCC diagnostic pop
#endif
#ifdef ERROR
#undef ERROR
#endif