Implement window hiding and tray icon functionality

Added methods to hide and show the Toolbox window and integrated a system tray icon with menu options, including window toggle and application quit functionality. Moved Lua script execution to occur after successful script loading. Introduced resource inclusion in the project files to support icon loading.
This commit is contained in:
2024-12-07 23:07:55 +01:00
parent 486094b540
commit 4fa4c6df6c
7 changed files with 116 additions and 16 deletions

6
inc/resource.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef RESOURCE_H
#define RESOURCE_H
#define IDI_ICON1 101 // ID für das Icon
#endif // RESOURCE_H

13
inc/toolboxTray.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#include "tray.h"
#include "toolboxWindow.h"
class toolboxTray {
public:
private:
};

View File

@@ -10,10 +10,13 @@ class ToolboxWindow {
public:
// Konstruktor erhält das Application-Objekt als Parameter
explicit ToolboxWindow(std::shared_ptr<saucer::application>& app);
~ToolboxWindow();
bool m_bShow = false;
// Methode zum Anzeigen des Fensters
void show();
void hide();
// Für die Fensterinitialisierung
void initialize();
@@ -35,9 +38,13 @@ public:
std::vector<ToolboxItem> m_toolboxItems;
saucer::smartview<saucer::default_serializer, hwnd_module> m_webview;
private:
std::shared_ptr<saucer::application> m_app;
saucer::smartview<saucer::default_serializer, hwnd_module> m_webview;
std::thread m_threadMonitor;
std::atomic<bool> m_runningMonitor = true;
void configureWindow();
void loadResources();
void runLuaScript();
@@ -51,4 +58,6 @@ private:
void debug_print();
void run_lua(int id);
void monitor_focus();
};

View File

@@ -1,21 +1,67 @@
#include "toolboxWindow.h"
#include <saucer/app.hpp>
#include "toolboxWindow.h"
#include "tray.h"
#include "resource.h"
ToolboxWindow* g_toolboxWindow = nullptr;
void on_toggle(struct tray_menu *item) {
item->checked = !item->checked;
}
void on_quit(struct tray_menu *item) {
tray_exit();
}
void on_click(struct tray_menu *item) {
if (g_toolboxWindow->m_bShow)
g_toolboxWindow->hide();
else
g_toolboxWindow->show();
}
int main(int argc, char** argv) {
// Initialisiere die Saucer-Anwendung
auto app = saucer::application::acquire({
.id = "toolbox",
});
// Erstelle und initialisiere das Toolbox-Fenster
// Neue Anwendung erstellen
auto app = saucer::application::acquire({
.id = "toolbox",
});
// Toolbox-Fenster erstellen und initialisieren
ToolboxWindow toolboxWindow(app);
toolboxWindow.initialize();
// Zeige das Fenster an
g_toolboxWindow = &toolboxWindow;
toolboxWindow.show();
// Starte die Saucer-Anwendung
HICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
struct tray_menu tray_menu_items[] = {
{"<EFBFBD>ffnen", 0, 0, NULL, on_click, 0},
{"Option umschalten", 0, 0, NULL, on_toggle, 0},
{"-", 0, 0, NULL, nullptr, 0},
{"Beenden", 0, 0, NULL, on_quit, 0},
{nullptr, 0, 0, NULL, nullptr, 0}
};
struct tray tray = {
.icon = (char*)"toolbox.ico",
.menu = tray_menu_items
};
if (tray_init(&tray) != 0) {
fprintf(stderr, "Fehler beim Initialisieren des Tray-Icons\n");
return 1;
}
//toolboxWindow.show();
/* while (tray_loop(1) == 0) {
}*/
// Anwendung ausf<73>hren
app->run();
return 0;
}
}

1
src/toolboxTray.cpp Normal file
View File

@@ -0,0 +1 @@
#include "toolboxTray.h"

View File

@@ -15,6 +15,14 @@ void ToolboxWindow::initialize() {
loadResources();
ExposeToJs();
m_webview.set_dev_tools(true);
//m_webview.set_decorations(false);
//m_webview.set_always_on_top(true);
m_threadMonitor = std::thread([&]() {
monitor_focus();
});
m_threadMonitor.detach();
}
void ToolboxWindow::configureWindow() {
@@ -71,9 +79,13 @@ void ToolboxWindow::runLuaScript() {
void ToolboxWindow::show() {
m_webview.show();
runLuaScript();
m_bShow = true;
}
void ToolboxWindow::hide() {
m_webview.hide();
m_bShow = false;
}
void ToolboxWindow::ExposeToJs() {
// Konvertierung des Vektors in JSON
@@ -228,9 +240,6 @@ void ToolboxWindow::fillToolboxMenu() {
void ToolboxWindow::run_lua(int id) {
m_webview.execute("enableButtonByToolId({})",saucer::make_args(id));
m_webview.reload();
// Lambda-Funktion, um das Lua-Skript auszuführen
auto lua_task = [this, id]() {
sol::state lua;
@@ -239,6 +248,8 @@ void ToolboxWindow::run_lua(int id) {
try {
std::string scriptPath = m_toolboxItems[id].lua_script;
lua.script_file(scriptPath);
m_webview.execute("enableButtonByToolId({})",saucer::make_args(id));
m_webview.reload();
} catch (const sol::error& e) {
std::cerr << "Lua error: " << e.what() << std::endl;
}
@@ -256,3 +267,15 @@ void ToolboxWindow::run_lua(int id) {
void ToolboxWindow::debug_print() {
std::cout << "ToolboxWindow::debug_print()" << std::endl;
}
ToolboxWindow::~ToolboxWindow() {
m_runningMonitor = false;
}
void ToolboxWindow::monitor_focus() {
while(m_runningMonitor){
if(m_bShow && !m_webview.focused()) {
this->hide();
}
}
}

View File

@@ -1 +1,3 @@
IDI_ICON1 ICON "res/ico/toolbox.ico"
#include "resource.h"
IDI_ICON1 ICON "res/ico/toolbox.ico"