Refactor HWND management and modularize initialization

Centralized HWND handling into a dedicated `hwnd_module` for improved clarity and modularity. Updated `toolboxWindow` and `optionWindow` to use the new module abstraction. Adjusted related initialization code for better structure and maintainability.
This commit is contained in:
2025-01-19 19:17:35 +01:00
parent c93758f732
commit 9e3c9f4d0e
9 changed files with 52 additions and 26 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ add_subdirectory(lib/cppcodec)
add_subdirectory(lib/saucer) add_subdirectory(lib/saucer)
add_subdirectory(lib/lua) add_subdirectory(lib/lua)
add_subdirectory(lib/sol2) add_subdirectory(lib/sol2)
add_subdirectory(lib/saucer4lua) #add_subdirectory(lib/saucer4lua)
include_directories(inc) include_directories(inc)
include_directories(lib/json/include/nlohmann) include_directories(lib/json/include/nlohmann)
+23 -14
View File
@@ -6,25 +6,34 @@
#define HWND_MODULE_H #define HWND_MODULE_H
#include <execution>
#include <saucer/modules/native/webview2.hpp> // Für Windows mit WebView2 #include <saucer/smartview.hpp>
#include <saucer/modules/stable/webview2.hpp> // Für Windows mit WebView2
class hwnd_module { class hwnd_module {
saucer::natives m_natives; saucer::webview *m_parent;
saucer::smartview_core *m_smartview;
public:
explicit hwnd_module(saucer::webview *parent)
: m_parent(parent) {}
HWND get_hwnd() const
{
if (auto [window, webview] = m_parent->native<>(); !webview) {
throw std::runtime_error("WebView instance is null.");
}
HWND parent = nullptr;
if (FAILED(m_parent->native<>().controller->get_ParentWindow(&parent))) {
throw std::runtime_error("Failed to retrieve HWND from WebView.");
}
return parent;
}
public:
hwnd_module(saucer::smartview_core *smartview, saucer::natives natives)
: m_natives(natives), m_smartview(smartview) {}
HWND get_hwnd() {
auto [window, webview] = m_natives;
return window->hwnd.get();
}
}; };
static_assert(saucer::Module<hwnd_module>); static_assert(saucer::Module<hwnd_module, saucer::webview>);
#endif //HWND_MODULE_H #endif //HWND_MODULE_H
+3 -1
View File
@@ -17,10 +17,12 @@ public:
// Für die Fensterinitialisierung // Für die Fensterinitialisierung
void initialize(); void initialize();
void setHWND(HWND hwnd);
private: private:
std::shared_ptr<saucer::application> m_app; std::shared_ptr<saucer::application> m_app;
saucer::smartview<saucer::default_serializer, hwnd_module> m_webview; saucer::smartview<saucer::default_serializer> m_webview;
HWND m_hwnd;
void configureWindow(); void configureWindow();
void loadResources(); void loadResources();
+3 -1
View File
@@ -40,9 +40,11 @@ public:
std::vector<ToolboxItem> m_toolboxItems; std::vector<ToolboxItem> m_toolboxItems;
saucer::smartview<saucer::default_serializer, hwnd_module> m_webview; saucer::smartview<saucer::default_serializer> m_webview;
private: private:
hwnd_module* m_hwnd_module;
HWND m_hwnd;
std::shared_ptr<saucer::application> m_app; std::shared_ptr<saucer::application> m_app;
std::thread m_threadMonitor; std::thread m_threadMonitor;
std::atomic<bool> m_runningMonitor = true; std::atomic<bool> m_runningMonitor = true;
+1
Submodule lib/saucer4lua added at 5029c0dd6d
+1 -1
View File
@@ -23,7 +23,7 @@ void on_click(struct tray_menu *item = nullptr) {
int start(){ int start(){
// Neue Anwendung erstellen // Neue Anwendung erstellen
auto app = saucer::application::acquire({ auto app = saucer::application::init({
.id = "toolbox", .id = "toolbox",
}); });
+11 -2
View File
@@ -3,13 +3,21 @@
#include <windows.h> #include <windows.h>
optionWindow::optionWindow(std::shared_ptr<saucer::application>& app) optionWindow::optionWindow(std::shared_ptr<saucer::application>& app)
: m_app(app), m_webview({.application = app}) {} : m_app(app), m_webview({.application = app}), m_hwnd()
{
}
void optionWindow::initialize() { void optionWindow::initialize() {
// Modul hinzufügen
configureWindow(); configureWindow();
loadResources(); loadResources();
} }
void optionWindow::setHWND(HWND hwnd)
{
m_hwnd = hwnd;
}
void optionWindow::configureWindow() { void optionWindow::configureWindow() {
int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN);
@@ -23,7 +31,8 @@ void optionWindow::configureWindow() {
int xPos = screenWidth - windowWidth; int xPos = screenWidth - windowWidth;
int yPos = screenHeight - 42 - windowHeight; int yPos = screenHeight - 42 - windowHeight;
if (HWND hwnd = m_webview.module<hwnd_module>().get_hwnd()) { // HWND über das Modul abrufen
if (HWND hwnd = m_hwnd) {
SetWindowPos(hwnd, nullptr, xPos, yPos, 0, 0, SWP_NOZORDER | SWP_NOSIZE); SetWindowPos(hwnd, nullptr, xPos, yPos, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
} }
} }
+8 -5
View File
@@ -1,14 +1,17 @@
#include "toolboxWindow.h" #include "toolboxWindow.h"
#include "optionWindow.h"
#include <iostream> #include <iostream>
#include <windows.h> #include <windows.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <cppcodec/base64_rfc4648.hpp> #include <cppcodec/base64_rfc4648.hpp>
ToolboxWindow::ToolboxWindow(std::shared_ptr<saucer::application>& app) ToolboxWindow::ToolboxWindow(std::shared_ptr<saucer::application>& app)
: m_webview({.application = app}), m_app(app) {} : m_webview({.application = app}), m_hwnd_module(&m_webview.add_module<hwnd_module>()), m_app(app)
{
}
void ToolboxWindow::initialize() { void ToolboxWindow::initialize() {
m_hwnd_module = &m_webview.add_module<hwnd_module>();
m_hwnd = m_hwnd_module->get_hwnd();
ensureToolboxInUserPath(); ensureToolboxInUserPath();
fillToolboxMenu(); fillToolboxMenu();
configureWindow(); configureWindow();
@@ -62,13 +65,13 @@ void ToolboxWindow::configureWindow() {
const int xPos = screenWidth - windowWidth; const int xPos = screenWidth - windowWidth;
const int yPos = screenHeight - 42 - windowHeight; const int yPos = screenHeight - 42 - windowHeight;
if (const HWND hwnd = m_webview.module<hwnd_module>().get_hwnd()) { if (const HWND hwnd = m_hwnd) {
SetWindowPos(hwnd, nullptr, xPos, yPos, 0, 0, SWP_NOZORDER | SWP_NOSIZE); SetWindowPos(hwnd, nullptr, xPos, yPos, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
} }
} }
void ToolboxWindow::moveWindowTo(int x, int y) { void ToolboxWindow::moveWindowTo(int x, int y) {
if (const HWND hwnd = m_webview.module<hwnd_module>().get_hwnd()) { if (const HWND hwnd = m_hwnd) {
SetWindowPos(hwnd, nullptr, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE); SetWindowPos(hwnd, nullptr, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
} }
} }
@@ -384,7 +387,7 @@ void ToolboxWindow::centerWindow() {
int windowWidth = 0; int windowWidth = 0;
int windowHeight = 0; int windowHeight = 0;
if (const HWND hwnd = m_webview.module<hwnd_module>().get_hwnd()) { if (const HWND hwnd = m_hwnd) {
RECT rect; RECT rect;
if (GetWindowRect(hwnd, &rect)) { if (GetWindowRect(hwnd, &rect)) {
windowWidth = rect.right - rect.left; windowWidth = rect.right - rect.left;