2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-11-17 18:52:06 +00:00

chore: only one header per modman platform handler

This commit is contained in:
Jan Laupetin
2025-11-02 19:01:22 +00:00
parent d964929681
commit 2aeaa417b6
21 changed files with 99 additions and 181 deletions

View File

@@ -1,7 +1,17 @@
#pragma once #pragma once
#ifdef _WIN32 #include "Web/Platform/Platform.h"
#include "Windows/AssetHandlerWindows.h" #include "Web/WebViewLib.h"
#elif defined(__linux__)
#include "Linux/AssetHandlerLinux.h" #include <webview/macros.h>
namespace ui
{
#if defined(PLATFORM_WINDOWS)
constexpr auto URL_PREFIX = "http://modman.local/";
#elif defined(PLATFORM_LINUX)
constexpr auto URL_PREFIX = "modman://localhost/";
#endif #endif
void InstallAssetHandler(webview::webview& wv);
} // namespace ui

View File

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

View File

@@ -1,6 +1,7 @@
#include "AssetHandlerLinux.h" #include "Web/Platform/AssetHandler.h"
#include "Web/Platform/Platform.h"
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK) #ifdef PLATFORM_LINUX
#include "Web/UiAssets.h" #include "Web/UiAssets.h"
@@ -8,8 +9,6 @@
#include <iostream> #include <iostream>
#include <unordered_map> #include <unordered_map>
using namespace PLATFORM_NAMESPACE_LINUX;
namespace namespace
{ {
std::unordered_map<std::string, UiFile> assetLookup; std::unordered_map<std::string, UiFile> assetLookup;
@@ -37,7 +36,7 @@ namespace
} }
} // namespace } // namespace
namespace PLATFORM_NAMESPACE_LINUX namespace ui
{ {
void InstallAssetHandler(webview::webview& wv) void InstallAssetHandler(webview::webview& wv)
{ {
@@ -49,6 +48,6 @@ namespace PLATFORM_NAMESPACE_LINUX
webkit_web_context_register_uri_scheme(context, "modman", ModManUriSchemeRequestCb, NULL, nullptr); webkit_web_context_register_uri_scheme(context, "modman", ModManUriSchemeRequestCb, NULL, nullptr);
} }
} // namespace PLATFORM_NAMESPACE_LINUX } // namespace ui
#endif #endif

View File

@@ -1,18 +0,0 @@
#pragma once
#include "Web/Platform/Platform.h"
#include <webview/macros.h>
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
#include "Web/WebViewLib.h"
namespace PLATFORM_NAMESPACE_LINUX
{
constexpr auto URL_PREFIX = "modman://localhost/";
void InstallAssetHandler(webview::webview& wv);
} // namespace PLATFORM_NAMESPACE_LINUX
#endif

View File

@@ -1,6 +1,7 @@
#include "Web/Platform/DialogHandler.h" #include "Web/Platform/DialogHandler.h"
#include "Web/Platform/Platform.h"
#ifdef __linux__ #ifdef PLATFORM_LINUX
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>

View File

@@ -1,6 +1,7 @@
#include "AssetHandlerLinux.h" #include "Web/Platform/FaviconHandler.h"
#include "Web/Platform/Platform.h"
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK) #ifdef PLATFORM_LINUX
#include "Web/UiAssets.h" #include "Web/UiAssets.h"
@@ -8,15 +9,14 @@
#include <iostream> #include <iostream>
#include <unordered_map> #include <unordered_map>
using namespace PLATFORM_NAMESPACE_LINUX; namespace ui
namespace
{ {
} // namespace void InstallFaviconHandler(webview::webview& wv)
{
namespace PLATFORM_NAMESPACE_LINUX // The icon system on Linux works a bit different than on Windows
{ // and doesn't really support this kind of dynamic icon setting
void InstallFaviconHandler(webview::webview& wv) {} // we skip it for now
} // namespace PLATFORM_NAMESPACE_LINUX }
} // namespace ui
#endif #endif

View File

@@ -1,16 +0,0 @@
#pragma once
#include "Web/Platform/Platform.h"
#include <webview/macros.h>
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
#include "Web/WebViewLib.h"
namespace PLATFORM_NAMESPACE_LINUX
{
void InstallFaviconHandler(webview::webview& wv);
} // namespace PLATFORM_NAMESPACE_LINUX
#endif

View File

@@ -1,6 +1,7 @@
#include "AssetHandlerLinux.h" #include "Web/Platform/Platform.h"
#include "Web/Platform/TitleHandler.h"
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK) #ifdef PLATFORM_LINUX
#include "Web/UiAssets.h" #include "Web/UiAssets.h"
@@ -8,9 +9,7 @@
#include <iostream> #include <iostream>
#include <unordered_map> #include <unordered_map>
using namespace PLATFORM_NAMESPACE_LINUX; namespace ui
namespace PLATFORM_NAMESPACE_LINUX
{ {
void InstallTitleHandler(webview::webview& wv) void InstallTitleHandler(webview::webview& wv)
{ {
@@ -26,6 +25,6 @@ namespace PLATFORM_NAMESPACE_LINUX
g_signal_connect(G_OBJECT(webView), "notify::title", G_CALLBACK(on_title_changed), (gpointer)window); g_signal_connect(G_OBJECT(webView), "notify::title", G_CALLBACK(on_title_changed), (gpointer)window);
} }
} // namespace PLATFORM_NAMESPACE_LINUX } // namespace ui
#endif #endif

View File

@@ -1,16 +0,0 @@
#pragma once
#include "Web/Platform/Platform.h"
#include <webview/macros.h>
#if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
#include "Web/WebViewLib.h"
namespace PLATFORM_NAMESPACE_LINUX
{
void InstallTitleHandler(webview::webview& wv);
} // namespace PLATFORM_NAMESPACE_LINUX
#endif

View File

@@ -1,10 +1,7 @@
#pragma once #pragma once
#define PLATFORM_NAMESPACE_WINDOWS windows
#define PLATFORM_NAMESPACE_LINUX linux
#ifdef _WIN32 #ifdef _WIN32
#define PLATFORM_NAMESPACE PLATFORM_NAMESPACE_WINDOWS #define PLATFORM_WINDOWS
#elif defined(__linux__) #elif defined(__linux__)
#define PLATFORM_NAMESPACE PLATFORM_NAMESPACE_LINUX #define PLATFORM_LINUX
#endif #endif

View File

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

View File

@@ -1,6 +1,7 @@
#include "AssetHandlerWindows.h" #include "Web/Platform/AssetHandler.h"
#include "Web/Platform/Platform.h"
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE) #ifdef PLATFORM_WINDOWS
#include "PlatformUtilsWindows.h" #include "PlatformUtilsWindows.h"
#include "Web/UiAssets.h" #include "Web/UiAssets.h"
@@ -12,8 +13,6 @@
#include <webview/detail/backends/win32_edge.hh> #include <webview/detail/backends/win32_edge.hh>
#include <wrl/event.h> #include <wrl/event.h>
using namespace PLATFORM_NAMESPACE_WINDOWS;
namespace namespace
{ {
constexpr auto LOCALHOST_PREFIX = "http://localhost:"; constexpr auto LOCALHOST_PREFIX = "http://localhost:";
@@ -25,7 +24,7 @@ namespace
std::wstringstream wss; std::wstringstream wss;
wss << std::format(L"Content-Length: {}\n", contentLength); wss << std::format(L"Content-Length: {}\n", contentLength);
wss << L"Content-Type: " << StringToWideString(ui::GetMimeTypeForFileName(assetName)); wss << L"Content-Type: " << utils::StringToWideString(ui::GetMimeTypeForFileName(assetName));
return wss.str(); return wss.str();
} }
@@ -65,7 +64,7 @@ namespace
Microsoft::WRL::ComPtr<ICoreWebView2WebResourceResponse> response; Microsoft::WRL::ComPtr<ICoreWebView2WebResourceResponse> response;
const auto uri = WideStringToString(wUri); const auto uri = utils::WideStringToString(wUri);
bool fileFound = false; bool fileFound = false;
#ifdef _DEBUG #ifdef _DEBUG
@@ -74,9 +73,9 @@ namespace
return S_OK; return S_OK;
#endif #endif
if (uri.starts_with(URL_PREFIX)) if (uri.starts_with(ui::URL_PREFIX))
{ {
const auto asset = uri.substr(std::char_traits<char>::length(URL_PREFIX) - 1); const auto asset = uri.substr(std::char_traits<char>::length(ui::URL_PREFIX) - 1);
const auto foundUiFile = assetLookup.find(asset); const auto foundUiFile = assetLookup.find(asset);
if (foundUiFile != assetLookup.end()) if (foundUiFile != assetLookup.end())
@@ -117,7 +116,7 @@ namespace
} }
} // namespace } // namespace
namespace PLATFORM_NAMESPACE_WINDOWS namespace ui
{ {
void InstallAssetHandler(webview::webview& wv) void InstallAssetHandler(webview::webview& wv)
{ {
@@ -157,6 +156,6 @@ namespace PLATFORM_NAMESPACE_WINDOWS
std::cerr << "Failed to add resource requested filter\n"; std::cerr << "Failed to add resource requested filter\n";
} }
} }
} // namespace PLATFORM_NAMESPACE_WINDOWS } // namespace ui
#endif #endif

View File

@@ -1,18 +0,0 @@
#pragma once
#include "Web/Platform/Platform.h"
#include <webview/macros.h>
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE)
#include "Web/WebViewLib.h"
namespace PLATFORM_NAMESPACE_WINDOWS
{
constexpr auto URL_PREFIX = "http://modman.local/";
void InstallAssetHandler(webview::webview& wv);
} // namespace PLATFORM_NAMESPACE_WINDOWS
#endif

View File

@@ -1,6 +1,7 @@
#include "Web/Platform/DialogHandler.h" #include "Web/Platform/DialogHandler.h"
#include "Web/Platform/Platform.h"
#ifdef _WIN32 #ifdef PLATFORM_WINDOWS
#include "PlatformUtilsWindows.h" #include "PlatformUtilsWindows.h"
@@ -8,8 +9,6 @@
#include <thread> #include <thread>
#include <windows.h> #include <windows.h>
using namespace PLATFORM_NAMESPACE_WINDOWS;
namespace namespace
{ {
bool SetFilters(IFileDialog* pFileOpen, const std::vector<ui::FileDialogFilter>& filters) bool SetFilters(IFileDialog* pFileOpen, const std::vector<ui::FileDialogFilter>& filters)
@@ -29,8 +28,8 @@ namespace
const auto& filter = filters[i]; const auto& filter = filters[i];
COMDLG_FILTERSPEC filterSpec; COMDLG_FILTERSPEC filterSpec;
const auto& wName = filterStrings.emplace_back(StringToWideString(filter.m_name)); const auto& wName = filterStrings.emplace_back(utils::StringToWideString(filter.m_name));
const auto& wSpec = filterStrings.emplace_back(StringToWideString(filter.m_filter)); const auto& wSpec = filterStrings.emplace_back(utils::StringToWideString(filter.m_filter));
filterSpec.pszName = wName.c_str(); filterSpec.pszName = wName.c_str();
filterSpec.pszSpec = wSpec.c_str(); filterSpec.pszSpec = wSpec.c_str();
@@ -65,7 +64,7 @@ namespace
// Display the file name to the user. // Display the file name to the user.
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
result = WideStringToString(pszFilePath); result = utils::WideStringToString(pszFilePath);
CoTaskMemFree(pszFilePath); CoTaskMemFree(pszFilePath);
resultType = ui::DialogCallbackResultType::SUCCESSFUL; resultType = ui::DialogCallbackResultType::SUCCESSFUL;

View File

@@ -1,6 +1,7 @@
#include "AssetHandlerWindows.h" #include "Web/Platform/FaviconHandler.h"
#include "Web/Platform/Platform.h"
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE) #ifdef PLATFORM_WINDOWS
#include "PlatformUtilsWindows.h" #include "PlatformUtilsWindows.h"
#include "Web/UiAssets.h" #include "Web/UiAssets.h"
@@ -13,8 +14,6 @@
#include <webview/detail/backends/win32_edge.hh> #include <webview/detail/backends/win32_edge.hh>
#include <wrl/event.h> #include <wrl/event.h>
using namespace PLATFORM_NAMESPACE_WINDOWS;
namespace namespace
{ {
class UniqueHIcon class UniqueHIcon
@@ -111,7 +110,7 @@ namespace
} }
} // namespace } // namespace
namespace PLATFORM_NAMESPACE_WINDOWS namespace ui
{ {
void InstallFaviconHandler(webview::webview& wv) void InstallFaviconHandler(webview::webview& wv)
{ {
@@ -146,6 +145,6 @@ namespace PLATFORM_NAMESPACE_WINDOWS
std::cerr << "Failed to add favicon handler\n"; std::cerr << "Failed to add favicon handler\n";
} }
} }
} // namespace PLATFORM_NAMESPACE_WINDOWS } // namespace ui
#endif #endif

View File

@@ -1,16 +0,0 @@
#pragma once
#include "Web/Platform/Platform.h"
#include <webview/macros.h>
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE)
#include "Web/WebViewLib.h"
namespace PLATFORM_NAMESPACE_WINDOWS
{
void InstallFaviconHandler(webview::webview& wv);
} // namespace PLATFORM_NAMESPACE_WINDOWS
#endif

View File

@@ -1,12 +1,12 @@
#include "PlatformUtilsWindows.h" #include "PlatformUtilsWindows.h"
#ifdef _WIN32 #ifdef PLATFORM_WINDOWS
#include <Windows.h> #include <Windows.h>
#include <exception> #include <exception>
#include <format> #include <format>
namespace PLATFORM_NAMESPACE_WINDOWS namespace utils
{ {
std::string WideStringToString(const std::wstring& wideString) std::string WideStringToString(const std::wstring& wideString)
{ {
@@ -35,6 +35,6 @@ namespace PLATFORM_NAMESPACE_WINDOWS
MultiByteToWideChar(CP_UTF8, 0, string.data(), static_cast<int>(string.size()), result.data(), sizeNeeded); MultiByteToWideChar(CP_UTF8, 0, string.data(), static_cast<int>(string.size()), result.data(), sizeNeeded);
return result; return result;
} }
} // namespace PLATFORM_NAMESPACE_WINDOWS } // namespace utils
#endif #endif

View File

@@ -1,16 +1,15 @@
#pragma once #pragma once
#ifdef _WIN32
#include "Web/Platform/Platform.h" #include "Web/Platform/Platform.h"
#include <optional> #ifdef PLATFORM_WINDOWS
#include <string> #include <string>
namespace PLATFORM_NAMESPACE_WINDOWS namespace utils
{ {
std::string WideStringToString(const std::wstring& wideString); std::string WideStringToString(const std::wstring& wideString);
std::wstring StringToWideString(const std::string& string); std::wstring StringToWideString(const std::string& string);
} // namespace PLATFORM_NAMESPACE_WINDOWS } // namespace utils
#endif #endif

View File

@@ -1,6 +1,7 @@
#include "AssetHandlerWindows.h" #include "Web/Platform/Platform.h"
#include "Web/Platform/TitleHandler.h"
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE) #ifdef PLATFORM_WINDOWS
#include "PlatformUtilsWindows.h" #include "PlatformUtilsWindows.h"
#include "Web/UiAssets.h" #include "Web/UiAssets.h"
@@ -12,8 +13,6 @@
#include <webview/detail/backends/win32_edge.hh> #include <webview/detail/backends/win32_edge.hh>
#include <wrl/event.h> #include <wrl/event.h>
using namespace PLATFORM_NAMESPACE_WINDOWS;
namespace namespace
{ {
HRESULT HandleTitleChanged(ICoreWebView2* core, HWND window) HRESULT HandleTitleChanged(ICoreWebView2* core, HWND window)
@@ -33,7 +32,7 @@ namespace
} }
} // namespace } // namespace
namespace PLATFORM_NAMESPACE_WINDOWS namespace ui
{ {
void InstallTitleHandler(webview::webview& wv) void InstallTitleHandler(webview::webview& wv)
{ {
@@ -58,6 +57,6 @@ namespace PLATFORM_NAMESPACE_WINDOWS
std::cerr << "Failed to add title handler\n"; std::cerr << "Failed to add title handler\n";
} }
} }
} // namespace PLATFORM_NAMESPACE_WINDOWS } // namespace ui
#endif #endif

View File

@@ -1,16 +0,0 @@
#pragma once
#include "Web/Platform/Platform.h"
#include <webview/macros.h>
#if defined(WEBVIEW_PLATFORM_WINDOWS) && defined(WEBVIEW_EDGE)
#include "Web/WebViewLib.h"
namespace PLATFORM_NAMESPACE_WINDOWS
{
void InstallTitleHandler(webview::webview& wv);
} // namespace PLATFORM_NAMESPACE_WINDOWS
#endif

View File

@@ -3,10 +3,8 @@
#include "ModManArgs.h" #include "ModManArgs.h"
#include "Web/Binds/Binds.h" #include "Web/Binds/Binds.h"
#include "Web/Platform/AssetHandler.h" #include "Web/Platform/AssetHandler.h"
#include "Web/Platform/Linux/FaviconHandlerLinux.h" #include "Web/Platform/FaviconHandler.h"
#include "Web/Platform/Linux/TitleHandlerLinux.h" #include "Web/Platform/TitleHandler.h"
#include "Web/Platform/Windows/FaviconHandlerWindows.h"
#include "Web/Platform/Windows/TitleHandlerWindows.h"
#include "Web/UiCommunication.h" #include "Web/UiCommunication.h"
#include "Web/ViteAssets.h" #include "Web/ViteAssets.h"
#include "Web/WebViewLib.h" #include "Web/WebViewLib.h"
@@ -21,7 +19,6 @@
#endif #endif
using namespace std::string_literals; using namespace std::string_literals;
using namespace PLATFORM_NAMESPACE;
namespace namespace
{ {
@@ -36,8 +33,8 @@ namespace
{ {
context.m_dev_tools_webview = std::make_unique<webview::webview>(false, nullptr); context.m_dev_tools_webview = std::make_unique<webview::webview>(false, nullptr);
auto& newWindow = *context.m_dev_tools_webview; auto& newWindow = *context.m_dev_tools_webview;
InstallFaviconHandler(newWindow); ui::InstallFaviconHandler(newWindow);
InstallTitleHandler(newWindow); ui::InstallTitleHandler(newWindow);
newWindow.set_title("Devtools"); newWindow.set_title("Devtools");
newWindow.set_size(640, 480, WEBVIEW_HINT_NONE); newWindow.set_size(640, 480, WEBVIEW_HINT_NONE);
@@ -71,13 +68,13 @@ namespace
newWindow.set_size(1280, 640, WEBVIEW_HINT_NONE); newWindow.set_size(1280, 640, WEBVIEW_HINT_NONE);
newWindow.set_size(640, 480, WEBVIEW_HINT_MIN); newWindow.set_size(640, 480, WEBVIEW_HINT_MIN);
InstallAssetHandler(newWindow); ui::InstallAssetHandler(newWindow);
InstallFaviconHandler(newWindow); ui::InstallFaviconHandler(newWindow);
InstallTitleHandler(newWindow); ui::InstallTitleHandler(newWindow);
ui::RegisterAllBinds(newWindow); ui::RegisterAllBinds(newWindow);
#ifdef _DEBUG #ifdef _DEBUG
newWindow.navigate(VITE_DEV_SERVER ? std::format("http://localhost:{}", VITE_DEV_SERVER_PORT) : std::format("{}index.html", URL_PREFIX)); newWindow.navigate(VITE_DEV_SERVER ? std::format("http://localhost:{}", VITE_DEV_SERVER_PORT) : std::format("{}index.html", ui::URL_PREFIX));
if (VITE_DEV_SERVER) if (VITE_DEV_SERVER)
{ {
@@ -88,7 +85,7 @@ namespace
}); });
} }
#else #else
newWindow.navigate(std::format("{}index.html", URL_PREFIX)); newWindow.navigate(std::format("{}index.html", ui::URL_PREFIX));
#endif #endif
newWindow.run(); newWindow.run();
} }