Skip to content

Commit 09ea822

Browse files
committed
Context menu on tray icon
1 parent c63870b commit 09ea822

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/main.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// TODO:
2-
// - show context menu on tray icon (has 'Show Log', 'Reload' and 'Exit')
32
// - click handlers for blocks
43
// - bug with changing font of block also changing defaultBlock font *sometimes*...?, debug by logging fontrefs on render
54

@@ -23,13 +22,20 @@
2322
INCTXT(wblocksLibMJS, "src/lib.mjs");
2423

2524
#define WBLOCKS_BAR_CLASS "wblocks2_bar"
25+
#define WM_WBLOCKS_TRAY (WM_USER + 1)
26+
27+
#define TRAY_MENU_SHOW_LOG 1
28+
#define TRAY_MENU_RELOAD_SCRIPTS 2
29+
#define TRAY_MENU_EXIT 3
30+
31+
#define WBLOCKS_MAX_LEN 1024
32+
33+
#define WBLOCKS_LOGFILE "wblocks.log"
2634

2735
JSClassID jsBlockClassId;
2836
UINT_PTR createWindowTimer;
2937
HINSTANCE hInst;
3038

31-
#define WBLOCKS_MAX_LEN 1024
32-
3339
typedef struct {
3440
HFONT handle;
3541
size_t refCount;
@@ -225,8 +231,9 @@ void initWnd(HWND wnd)
225231
.cbSize = sizeof(notifData),
226232
.hWnd = wnd,
227233
.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(100)),
228-
.uFlags = NIF_ICON | NIF_TIP,
234+
.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP,
229235
.szTip = "wblocks",
236+
.uCallbackMessage = WM_WBLOCKS_TRAY,
230237
};
231238
Shell_NotifyIcon(NIM_ADD, &notifData);
232239
}
@@ -258,6 +265,28 @@ LRESULT CALLBACK wndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
258265
cleanupWnd();
259266
err("wblocks window died, probably due to explorer.exe crashing");
260267
break;
268+
case WM_WBLOCKS_TRAY:
269+
if (LOWORD(lParam) == WM_LBUTTONUP || LOWORD(lParam) == WM_RBUTTONUP) {
270+
POINT pt;
271+
GetCursorPos(&pt);
272+
HMENU hmenu = CreatePopupMenu();
273+
InsertMenu(hmenu, 0, MF_BYPOSITION | MF_STRING, TRAY_MENU_SHOW_LOG, "Show Log");
274+
// TODO: InsertMenu(hmenu, 1, MF_BYPOSITION | MF_STRING, TRAY_MENU_RELOAD_SCRIPTS, "Reload Scripts");
275+
InsertMenu(hmenu, 2, MF_BYPOSITION | MF_STRING, TRAY_MENU_EXIT, "Exit");
276+
SetForegroundWindow(wnd);
277+
int cmd = TrackPopupMenu(hmenu,
278+
TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_BOTTOMALIGN | TPM_NONOTIFY | TPM_RETURNCMD,
279+
pt.x, pt.y, 0, wnd, NULL);
280+
PostMessage(wnd, WM_NULL, 0, 0);
281+
if (cmd == TRAY_MENU_SHOW_LOG) {
282+
ShellExecute(NULL, NULL, WBLOCKS_LOGFILE, NULL, NULL, SW_SHOWNORMAL);
283+
} else if (cmd == TRAY_MENU_RELOAD_SCRIPTS) {
284+
// TODO
285+
} else if (cmd == TRAY_MENU_EXIT) {
286+
cleanupWnd();
287+
exit(0);
288+
}
289+
}
261290
}
262291

263292
return DefWindowProc(wnd, msg, wParam, lParam);
@@ -530,7 +559,7 @@ int CALLBACK WinMain(HINSTANCE inst, HINSTANCE prevInst, LPSTR cmdLine, int cmdS
530559
assert(freopen("CONOUT$", "w", stderr));
531560
#else
532561
ShowWindow(GetConsoleWindow(), 0);
533-
assert(freopen("wblocks.log", "w", stdout));
562+
assert(freopen(WBLOCKS_LOGFILE, "w", stdout));
534563
assert(freopen("NUL", "w", stderr));
535564
assert(!_dup2(_fileno(stdout), _fileno(stderr)));
536565
#endif

0 commit comments

Comments
 (0)