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

View File

@@ -13,7 +13,7 @@ add_subdirectory(lib/cppcodec)
add_subdirectory(lib/saucer)
add_subdirectory(lib/lua)
add_subdirectory(lib/sol2)
add_subdirectory(lib/saucer4lua)
#add_subdirectory(lib/saucer4lua)
include_directories(inc)
include_directories(lib/json/include/nlohmann)

View File

@@ -6,25 +6,34 @@
#define HWND_MODULE_H
#include <saucer/modules/native/webview2.hpp> // Für Windows mit WebView2
#include <execution>
#include <saucer/smartview.hpp>
#include <saucer/modules/stable/webview2.hpp> // Für Windows mit WebView2
class hwnd_module {
saucer::natives m_natives;
saucer::smartview_core *m_smartview;
saucer::webview *m_parent;
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

View File

@@ -17,10 +17,12 @@ public:
// Für die Fensterinitialisierung
void initialize();
void setHWND(HWND hwnd);
private:
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 loadResources();

View File

@@ -40,9 +40,11 @@ public:
std::vector<ToolboxItem> m_toolboxItems;
saucer::smartview<saucer::default_serializer, hwnd_module> m_webview;
saucer::smartview<saucer::default_serializer> m_webview;
private:
hwnd_module* m_hwnd_module;
HWND m_hwnd;
std::shared_ptr<saucer::application> m_app;
std::thread m_threadMonitor;
std::atomic<bool> m_runningMonitor = true;

1
lib/saucer4lua Submodule

Submodule lib/saucer4lua added at 5029c0dd6d

View File

@@ -23,7 +23,7 @@ void on_click(struct tray_menu *item = nullptr) {
int start(){
// Neue Anwendung erstellen
auto app = saucer::application::acquire({
auto app = saucer::application::init({
.id = "toolbox",
});

View File

@@ -3,13 +3,21 @@
#include <windows.h>
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() {
// Modul hinzufügen
configureWindow();
loadResources();
}
void optionWindow::setHWND(HWND hwnd)
{
m_hwnd = hwnd;
}
void optionWindow::configureWindow() {
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
@@ -23,7 +31,8 @@ void optionWindow::configureWindow() {
int xPos = screenWidth - windowWidth;
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);
}
}

View File

@@ -1,14 +1,17 @@
#include "toolboxWindow.h"
#include "optionWindow.h"
#include <iostream>
#include <windows.h>
#include <nlohmann/json.hpp>
#include <cppcodec/base64_rfc4648.hpp>
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() {
m_hwnd_module = &m_webview.add_module<hwnd_module>();
m_hwnd = m_hwnd_module->get_hwnd();
ensureToolboxInUserPath();
fillToolboxMenu();
configureWindow();
@@ -62,13 +65,13 @@ void ToolboxWindow::configureWindow() {
const int xPos = screenWidth - windowWidth;
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);
}
}
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);
}
}
@@ -384,7 +387,7 @@ void ToolboxWindow::centerWindow() {
int windowWidth = 0;
int windowHeight = 0;
if (const HWND hwnd = m_webview.module<hwnd_module>().get_hwnd()) {
if (const HWND hwnd = m_hwnd) {
RECT rect;
if (GetWindowRect(hwnd, &rect)) {
windowWidth = rect.right - rect.left;