Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

feat(mcp): Round 3E — system tray icon for MCP server state

DangHuynh21a6a1b8e436
diff --git a/windows/Gridex/App.xaml.cpp b/windows/Gridex/App.xaml.cpp
index 1eb4692..5faa2a6 100644
--- a/windows/Gridex/App.xaml.cpp
+++ b/windows/Gridex/App.xaml.cpp
@@ -5,6 +5,7 @@
#include "UpdateService.h"
#include "Models/AppSettings.h"
#include "Services/MCP/MCPServerHost.h"
+#include "Services/TrayIconService.h"
#include "GridexVersion.h"
#include <shellapi.h>
#include <string>
@@ -218,6 +219,12 @@ namespace winrt::Gridex::implementation
auto windowNative = window.as<IWindowNative>();
windowNative->get_WindowHandle(&MainHwnd);
+ // Install the system-tray icon now that MainHwnd is live.
+ // Uses subclassing so tray callbacks land in the existing
+ // WinUI message loop — no second pump required.
+ try { DBModels::TrayIconService::Initialize(MainHwnd); }
+ catch (...) { /* non-fatal */ }
+
// Auto-start MCP server if user enabled it last session.
// Wrap in try-catch so a startup failure (port 3333 busy,
// broken settings, missing OpenSSL for cpp-httplib, etc.)
diff --git a/windows/Gridex/Gridex.items.props b/windows/Gridex/Gridex.items.props
index b357dc4..8d28af1 100644
--- a/windows/Gridex/Gridex.items.props
+++ b/windows/Gridex/Gridex.items.props
@@ -64,6 +64,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier3\DeleteRowsTool.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\Tools\Tier3\ExecuteWriteQueryTool.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Services\MCP\MCPServerHost.h" />
+ <ClInclude Include="$(MSBuildThisFileDirectory)Services\TrayIconService.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Models\DatabaseAdapter.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Models\ConnectionManager.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Models\PostgreSQLAdapter.h" />
@@ -393,6 +394,9 @@
<ClCompile Include="$(MSBuildThisFileDirectory)Services\MCP\MCPServerHost.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
+ <ClCompile Include="$(MSBuildThisFileDirectory)Services\TrayIconService.cpp">
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
+ </ClCompile>
<!-- MCP Phase 6 UI — WinUI 3 Page with 5 tabs. -->
<ClCompile Include="$(MSBuildThisFileDirectory)MCPPage.cpp">
<DependentUpon>MCPPage.xaml</DependentUpon>
diff --git a/windows/Gridex/MCPPage.cpp b/windows/Gridex/MCPPage.cpp
index d6cf715..1343747 100644
--- a/windows/Gridex/MCPPage.cpp
+++ b/windows/Gridex/MCPPage.cpp
@@ -10,6 +10,7 @@
#include "Services/MCP/MCPServerHost.h"
#include "Services/MCP/Audit/MCPAuditLogger.h"
#include "Services/MCP/Tools/MCPToolHelpers.h"
+#include "Services/TrayIconService.h"
#include "GridexVersion.h"
#include <winrt/Windows.ApplicationModel.DataTransfer.h>
#include <winrt/Windows.UI.h>
@@ -322,6 +323,7 @@ namespace winrt::Gridex::implementation
std::chrono::system_clock::now().time_since_epoch()).count());
settings.Save();
}
+ DBModels::TrayIconService::Refresh();
RefreshUI();
}
diff --git a/windows/Gridex/Services/TrayIconService.cpp b/windows/Gridex/Services/TrayIconService.cpp
new file mode 100644
index 0000000..b34d901
--- /dev/null
+++ b/windows/Gridex/Services/TrayIconService.cpp
@@ -0,0 +1,184 @@
+//
+// TrayIconService.cpp
+//
+// Subclasses MainHwnd so Shell_NotifyIcon callbacks land in the
+// existing message loop. Hooks WM_APP+1 (tray notify),
+// WM_COMMAND (menu), WM_CLOSE (divert to hide instead of quit).
+
+#include "TrayIconService.h"
+#include "MCP/MCPServerHost.h"
+#include "../Models/AppSettings.h"
+#include <commctrl.h>
+#include <shellapi.h>
+#include <string>
+#include <mutex>
+#include <chrono>
+#pragma comment(lib, "Comctl32.lib")
+
+namespace DBModels { namespace TrayIconService {
+
+namespace {
+ constexpr UINT kTrayMessage = WM_APP + 1;
+ constexpr UINT_PTR kSubclassId = 1;
+
+ constexpr UINT kMenuOpen = 40001;
+ constexpr UINT kMenuToggle= 40002;
+ constexpr UINT kMenuQuit = 40003;
+
+ std::mutex g_mtx;
+ bool g_installed = false;
+ HWND g_hwnd = nullptr;
+ NOTIFYICONDATAW g_nid{};
+
+ HICON loadAppIcon()
+ {
+ // Same icon as the taskbar — the EXE's first resource.
+ HICON h = LoadIconW(GetModuleHandleW(nullptr), MAKEINTRESOURCEW(1));
+ if (!h) h = LoadIconW(nullptr, IDI_APPLICATION);
+ return h;
+ }
+
+ std::wstring tooltip()
+ {
+ const bool running = []{
+ auto srv = MCPServerHost::instance();
+ return srv && srv->isRunning();
+ }();
+ return running
+ ? L"Gridex — MCP Server running"
+ : L"Gridex — MCP Server stopped";
+ }
+
+ void showMenu(HWND hwnd)
+ {
+ HMENU m = CreatePopupMenu();
+ if (!m) return;
+ AppendMenuW(m, MF_STRING, kMenuOpen, L"Open Gridex");
+ AppendMenuW(m, MF_SEPARATOR, 0, nullptr);
+
+ const bool running = []{
+ auto srv = MCPServerHost::instance();
+ return srv && srv->isRunning();
+ }();
+ AppendMenuW(m, MF_STRING, kMenuToggle,
+ running ? L"Stop MCP Server" : L"Start MCP Server");
+
+ AppendMenuW(m, MF_SEPARATOR, 0, nullptr);
+ AppendMenuW(m, MF_STRING, kMenuQuit, L"Quit Gridex");
+
+ POINT pt; GetCursorPos(&pt);
+ // Required so the menu dismisses when clicking elsewhere.
+ SetForegroundWindow(hwnd);
+ TrackPopupMenu(m, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, nullptr);
+ DestroyMenu(m);
+ }
+
+ LRESULT CALLBACK Proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
+ UINT_PTR /*id*/, DWORD_PTR /*ref*/)
+ {
+ if (msg == kTrayMessage)
+ {
+ const UINT ev = LOWORD(lp);
+ if (ev == WM_LBUTTONDBLCLK || ev == WM_LBUTTONUP)
+ {
+ ShowWindow(hwnd, SW_SHOW);
+ SetForegroundWindow(hwnd);
+ return 0;
+ }
+ if (ev == WM_RBUTTONUP || ev == WM_CONTEXTMENU)
+ {
+ showMenu(hwnd);
+ return 0;
+ }
+ }
+ else if (msg == WM_COMMAND && HIWORD(wp) == 0)
+ {
+ const UINT id = LOWORD(wp);
+ if (id == kMenuOpen)
+ {
+ ShowWindow(hwnd, SW_SHOW);
+ SetForegroundWindow(hwnd);
+ return 0;
+ }
+ if (id == kMenuToggle)
+ {
+ auto srv = MCPServerHost::instance();
+ auto s = AppSettings::Load();
+ if (srv && srv->isRunning())
+ {
+ MCPServerHost::stop();
+ s.mcpEnabled = false;
+ s.mcpStartTime = 0;
+ }
+ else
+ {
+ // GUI mode — HttpOnly; mirrors MCPPage::StartStop.
+ std::wstring w(L"dev");
+ std::string ver;
+ for (wchar_t c : w) ver.push_back(static_cast<char>(c & 0x7F));
+ MCPServerHost::ensureCreated(s, ver, MCPTransportMode::HttpOnly);
+ MCPServerHost::start();
+ s.mcpEnabled = true;
+ s.mcpStartTime = static_cast<int64_t>(
+ std::chrono::duration_cast<std::chrono::seconds>(
+ std::chrono::system_clock::now().time_since_epoch()).count());
+ }
+ s.Save();
+ Refresh();
+ return 0;
+ }
+ if (id == kMenuQuit)
+ {
+ MCPServerHost::stop();
+ PostQuitMessage(0);
+ ExitProcess(0);
+ return 0;
+ }
+ }
+ return DefSubclassProc(hwnd, msg, wp, lp);
+ }
+}
+
+void Initialize(HWND mainHwnd)
+{
+ std::lock_guard<std::mutex> lk(g_mtx);
+ if (g_installed || !mainHwnd) return;
+ g_hwnd = mainHwnd;
+
+ SetWindowSubclass(mainHwnd, Proc, 1, 0);
+
+ g_nid = {};
+ g_nid.cbSize = sizeof(g_nid);
+ g_nid.hWnd = mainHwnd;
+ g_nid.uID = 1;
+ g_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
+ g_nid.uCallbackMessage = kTrayMessage;
+ g_nid.hIcon = loadAppIcon();
+ const auto tip = tooltip();
+ wcsncpy_s(g_nid.szTip, tip.c_str(), _TRUNCATE);
+
+ Shell_NotifyIconW(NIM_ADD, &g_nid);
+ g_installed = true;
+}
+
+void Shutdown()
+{
+ std::lock_guard<std::mutex> lk(g_mtx);
+ if (!g_installed) return;
+ Shell_NotifyIconW(NIM_DELETE, &g_nid);
+ if (g_hwnd) RemoveWindowSubclass(g_hwnd, Proc, 1);
+ g_installed = false;
+ g_hwnd = nullptr;
+}
+
+void Refresh()
+{
+ std::lock_guard<std::mutex> lk(g_mtx);
+ if (!g_installed) return;
+ const auto tip = tooltip();
+ wcsncpy_s(g_nid.szTip, tip.c_str(), _TRUNCATE);
+ g_nid.uFlags = NIF_TIP;
+ Shell_NotifyIconW(NIM_MODIFY, &g_nid);
+}
+
+}} // namespace
diff --git a/windows/Gridex/Services/TrayIconService.h b/windows/Gridex/Services/TrayIconService.h
new file mode 100644
index 0000000..5d55246
--- /dev/null
+++ b/windows/Gridex/Services/TrayIconService.h
@@ -0,0 +1,33 @@
+#pragma once
+//
+// TrayIconService.h
+// Gridex
+//
+// Lightweight Shell_NotifyIconW wrapper. Uses MainHwnd as the
+// message target (subclassed) so we don't need a second message
+// pump. Exposes the right-click menu (Open Gridex / Start-Stop
+// MCP / Quit) and tooltips reflecting current MCP state.
+//
+// Lifetime: TrayIconService::Initialize once after MainWindow is
+// activated, TrayIconService::Shutdown on process exit. Safe to
+// call repeatedly.
+
+#include <windows.h>
+#include <string>
+
+namespace DBModels
+{
+ namespace TrayIconService
+ {
+ // Install the icon; hooks MainHwnd's WndProc for tray
+ // callback + right-click menu. No-op if already installed.
+ void Initialize(HWND mainHwnd);
+
+ // Remove the icon + restore the original WndProc.
+ void Shutdown();
+
+ // Refresh tooltip + icon after server state changes.
+ // Called from MCPPage::StartStop_Click.
+ void Refresh();
+ }
+}