trayicon: added allow client server restart/shutdown options to popupmenu

Dieser Commit ist enthalten in:
Reinder Feenstra 2022-05-26 09:59:09 +02:00
Ursprung 0f3597bdcc
Commit 1a8c17fb50
2 geänderte Dateien mit 81 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -27,6 +27,7 @@
#include "consolewindow.hpp"
#include "../../core/eventloop.hpp"
#include "../../traintastic/traintastic.hpp"
#include "../../utils/setthreadname.hpp"
namespace Windows {
@ -50,6 +51,8 @@ void TrayIcon::remove()
void TrayIcon::run()
{
setThreadName("trayicon");
const LPCSTR windowClassName = "TraintasticServerTrayIcon";
// register window class:
@ -73,6 +76,9 @@ void TrayIcon::run()
s_menu = CreatePopupMenu();
menuAddItem(MenuItem::ShowHideConsole, "Show/hide console", hasConsoleWindow());
menuAddSeperator();
menuAddItem(MenuItem::AllowClientServerRestart, "Allow client to restart server");
menuAddItem(MenuItem::AllowClientServerShutdown, "Allow client to shutdown server");
menuAddSeperator();
menuAddItem(MenuItem::Restart, "Restart");
menuAddItem(MenuItem::Shutdown, "Shutdown");
@ -97,6 +103,9 @@ void TrayIcon::run()
Shell_NotifyIcon(NIM_ADD, &notifyIconData);
// Get settings:
EventLoop::call(getSettings);
// message loop:
while(true)
{
@ -166,8 +175,47 @@ LRESULT CALLBACK TrayIcon::windowProc(_In_ HWND hWnd, _In_ UINT uMsg, _In_ WPARA
case MenuItem::ShowHideConsole:
setConsoleWindowVisible(!isConsoleWindowVisible());
return 0;
case MenuItem::AllowClientServerRestart:
{
bool value;
{
std::lock_guard lock{s_settings.mutex};
value = !s_settings.allowClientServerRestart;
}
EventLoop::call(
[value]()
{
Traintastic::instance->settings->allowClientServerRestart = value;
getSettings();
});
break;
}
case MenuItem::AllowClientServerShutdown:
{
bool value;
{
std::lock_guard lock{s_settings.mutex};
value = !s_settings.allowClientServerShutdown;
}
EventLoop::call(
[value]()
{
Traintastic::instance->settings->allowClientServerShutdown = value;
getSettings();
});
break;
}
}
break;
case WM_TRAINTASTIC_SETTINGS:
{
std::lock_guard lock{s_settings.mutex};
menuSetItemChecked(MenuItem::AllowClientServerRestart, s_settings.allowClientServerRestart);
menuSetItemChecked(MenuItem::AllowClientServerShutdown, s_settings.allowClientServerShutdown);
break;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
@ -197,4 +245,22 @@ void TrayIcon::menuAddSeperator()
InsertMenuItem(s_menu, GetMenuItemCount(s_menu), TRUE, &item);
}
void TrayIcon::menuSetItemChecked(MenuItem id, bool checked)
{
assert(s_menu);
CheckMenuItem(s_menu, static_cast<UINT>(id), checked ? MF_CHECKED : MF_UNCHECKED);
}
void TrayIcon::getSettings()
{
assert(isEventLoopThread());
const auto& settings = *Traintastic::instance->settings.value();
{
std::lock_guard lock{s_settings.mutex};
s_settings.allowClientServerRestart = settings.allowClientServerRestart;
s_settings.allowClientServerShutdown = settings.allowClientServerShutdown;
}
PostMessage(s_window, WM_TRAINTASTIC_SETTINGS, 0, 0);
}
}

Datei anzeigen

@ -25,6 +25,7 @@
#include <memory>
#include <thread>
#include <mutex>
#include <string>
#define _WINSOCKAPI_ // prevent windows.h including winsock.h
#include <windows.h>
@ -43,19 +44,33 @@ class TrayIcon
Shutdown = 1,
Restart = 2,
ShowHideConsole = 3,
AllowClientServerRestart = 4,
AllowClientServerShutdown = 5,
};
struct TraintasticSettings
{
std::mutex mutex;
bool allowClientServerRestart;
bool allowClientServerShutdown;
};
static constexpr UINT WM_NOTIFYICON_CALLBACK = WM_USER + 1;
static constexpr UINT WM_TRAINTASTIC_SETTINGS = WM_USER + 2;
static std::unique_ptr<std::thread> s_thread;
static HWND s_window;
static HMENU s_menu;
inline static TraintasticSettings s_settings = {{}, false, false};
static void run();
static LRESULT CALLBACK windowProc(_In_ HWND hWnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam);
static void menuAddItem(MenuItem id, const LPSTR text, bool enabled = true);
static void menuAddSeperator();
static void menuSetItemChecked(MenuItem id, bool checked);
static void getSettings();
public:
static void add();