From f76796c7ea1c4fee15bc6d2841579da8aa29c1db Mon Sep 17 00:00:00 2001 From: Anatolii Hrynchuk Date: Tue, 1 Dec 2026 22:30:12 +0200 Subject: [PATCH] Show a tray warning when held input blocks LAN switching Refs #227 --- LAN-PREVIEW.md | 10 ++++----- src/app.cpp | 39 ++++++++++++++++++++++++++--------- src/app.h | 1 + tests/input.cpp | 35 ++++++++++++++++++++++++++++++++ tests/input_pause.cpp | 47 +++++++++++++++++++++++++++++++++---------- tests/input_pause.h | 2 ++ 6 files changed, 109 insertions(+), 25 deletions(-) diff --git a/LAN-PREVIEW.md b/LAN-PREVIEW.md index 260834b..76b4c67 100644 --- a/LAN-PREVIEW.md +++ b/LAN-PREVIEW.md @@ -140,11 +140,11 @@ worker stops. Hover over the icon to see the connection status and the connected count when only some paired PCs are connected. If a held key or button blocks a handoff, the tray and **Settings → Computers** -identify the PC and name the input when available. The hint clears when the -input is released or a handoff succeeds. If Windows still sees an input as held, -choose **Reset held keys and mouse buttons** in the tray menu on the PC named -in the hint. Both computers need v0.8.12 or later for the source PC to display -which input is held on the destination. +identify the PC and name the input when available. The tray icon shows a steady +amber exclamation mark until the input is released or a handoff succeeds. +If Windows still sees an input as held, choose **Reset held keys and mouse buttons** +in the tray menu on the PC named in the hint. Both computers need v0.8.12 or +later for the source PC to display which input is held on the destination. The source cursor is parked and hidden beneath a tiny no-activation window. A small banner identifies remote control; no full-screen layered bitmap is retained. diff --git a/src/app.cpp b/src/app.cpp index bd11fa7..b0d82a5 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -1079,23 +1079,37 @@ HBITMAP trayIconBitmap(HICON base, int width, int height, void **data = nullptr) return nullptr; } -HICON trayInputIcon(HICON base, int width, int height, uint32_t color) +HICON trayInputIcon(HICON base, int width, int height, uint32_t color, bool blocked = false) { if (width < 8 || height < 8) return nullptr; void *bits{}; const auto bitmap = trayIconBitmap(base, width, height, &bits); if (!bitmap) return nullptr; auto pixels = static_cast(bits); - const int radius = std::max(3, MulDiv(std::min(width, height), 3, 16)); + const int minimumRadius = blocked ? 4 : 3; + const int radius = std::min((std::min(width, height) - 1) / 2, + std::max(minimumRadius, MulDiv(std::min(width, height), minimumRadius, 16))); const int cx = width - 1 - radius, cy = height - 1 - radius; + constexpr uint32_t ink = 0xff242424; for (int y = -radius; y <= radius; ++y) for (int x = -radius; x <= radius; ++x) { const int distance = x * x + y * y; if (distance <= radius * radius) pixels[(cy + y) * width + cx + x] = distance <= (radius - 1) * (radius - 1) - ? color : 0xff242424; + ? color : ink; } + if (blocked) + { + const int stroke = std::max(1, radius / 4); + const int left = cx - (stroke - 1) / 2; + const int dot = cy + std::max(2, radius / 2); + for (int x = left; x < left + stroke; ++x) + { + for (int y = cy - radius / 2; y <= cy; ++y) pixels[y * width + x] = ink; + for (int y = dot; y < dot + stroke; ++y) pixels[y * width + x] = ink; + } + } // The 32-bit DIB supplies the transparency. The fallback mask is initialized as well. std::vector maskBits(size_t((width + 15) / 16) * 2 * height); HICON icon{}; @@ -1116,6 +1130,16 @@ HICON App::trayBaseIcon(int width, int height) const return static_cast(LoadImageW(instance, MAKEINTRESOURCEW(resource), IMAGE_ICON, width, height, LR_SHARED)); } +HICON App::trayInputBadge(HICON base, int width, int height) const +{ + if (inputState != InputState::Active) return nullptr; + if (!trayInputBlock.empty()) return trayInputIcon(base, width, height, 0xffffc457, true); + if (!trayInput.paired) return nullptr; + const uint32_t color = trayInput.stopped ? 0xfff16b66 : !trayInput.online ? 0xffa0a4ab : + !trayInput.reconnecting() ? 0xff45d58c : trayInputPulse ? 0xffffc457 : 0xffac7830; + return trayInputIcon(base, width, height, color); +} + bool App::updateTray(DWORD action) { NOTIFYICONDATAW icon{sizeof(icon)}; @@ -1126,7 +1150,8 @@ bool App::updateTray(DWORD action) icon.uCallbackMessage = TrayMessage; const int width = GetSystemMetrics(SM_CXSMICON), height = GetSystemMetrics(SM_CYSMICON); icon.hIcon = trayBaseIcon(width, height); - HICON badge{}; + const auto badge = trayInputBadge(icon.hIcon, width, height); + if (badge) icon.hIcon = badge; auto tip = localBuildTitle(L"KSwitcher"); const auto state = inputState.load(); tip += state == InputState::Active ? L" — Input processing active" : @@ -1134,10 +1159,6 @@ bool App::updateTray(DWORD action) if (state == InputState::Active && trayInput.paired) { const bool reconnecting = trayInput.reconnecting(); - const uint32_t color = trayInput.stopped ? 0xfff16b66 : !trayInput.online ? 0xffa0a4ab : - !reconnecting ? 0xff45d58c : trayInputPulse ? 0xffffc457 : 0xffac7830; - badge = trayInputIcon(icon.hIcon, width, height, color); - if (badge) icon.hIcon = badge; tip += L"\nMouse & keyboard: "; if (trayInput.stopped) tip += L"connection stopped"; else if (!trayInput.online) tip += L"all paired PCs offline. Waiting for LAN discovery."; @@ -3980,7 +4001,7 @@ LRESULT CALLBACK App::windowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) if (app->trayInput.reconnecting()) { app->trayInputPulse = !app->trayInputPulse; - app->updateTray(NIM_MODIFY); + if (app->trayInputBlock.empty()) app->updateTray(NIM_MODIFY); } return 0; } diff --git a/src/app.h b/src/app.h index 86a876b..04389e4 100644 --- a/src/app.h +++ b/src/app.h @@ -185,6 +185,7 @@ class App ULONGLONG nextInputBlockNotice{}; bool inputBlockNoticeVisible{}; HICON trayBaseIcon(int width, int height) const; + HICON trayInputBadge(HICON base, int width, int height) const; bool updateTray(DWORD action); void refreshTrayInput(); void refreshTrayInputBlock(); diff --git a/tests/input.cpp b/tests/input.cpp index 55b5a15..c752e8e 100644 --- a/tests/input.cpp +++ b/tests/input.cpp @@ -43,6 +43,40 @@ struct InputRegressionAccess { } for (const auto &job : jobs) job(); } + static bool probeTrayBlock(App &app) + { + const auto savedConnections = app.trayInput; + const auto savedBlock = app.trayInputBlock; + const auto savedPulse = app.trayInputPulse; + const auto checkBadge = [&](bool blocked) { + for (int size : {16, 32}) + { + const auto icon = app.trayInputBadge(app.trayBaseIcon(size, size), size, size); + const bool matches = app.inputState == App::InputState::Active ? + trayIconMatchesLanBadge(icon, size, blocked) : !icon; + if (icon) DestroyIcon(icon); + if (!matches) return false; + } + return true; + }; + app.trayInput = {1, 1, 1, false}; + app.trayInputBlock.clear(); + const bool ready = checkBadge(false); + app.trayInputBlock = L"Cursor handoff blocked on this PC: Tab is held."; + const bool blocked = checkBadge(true); + app.trayInput = {1, 2, 2, false}; + app.trayInputPulse = false; + const bool steadyDim = checkBadge(true); + app.trayInputPulse = true; + const bool steadyBright = checkBadge(true); + app.trayInput = {1, 1, 1, false}; + app.trayInputBlock.clear(); + const bool restored = checkBadge(false); + app.trayInput = savedConnections; + app.trayInputBlock = savedBlock; + app.trayInputPulse = savedPulse; + return ready && blocked && steadyDim && steadyBright && restored; + } static LRESULT CALLBACK probeInput(HWND w, UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR context) { auto &app = *reinterpret_cast(context); static bool queuedSnippet = false; @@ -50,6 +84,7 @@ struct InputRegressionAccess { if (msg == WM_EXITMENULOOP && app.inputState == App::InputState::Active) queuedSnippet = app.queueSnippet(w, 0, 2, w); if (msg == ProbeTrayIcon) return trayIconMatchesInput(app.trayBaseIcon(16, 16), wp != 0); + if (msg == ProbeTrayBlock) return probeTrayBlock(app); if (msg != ProbeInputState) return DefSubclassProc(w, msg, wp, lp); // Also exercise settings/layout updates that would normally re-register shortcuts. if (wp && app.inputState == App::InputState::Paused) { diff --git a/tests/input_pause.cpp b/tests/input_pause.cpp index b38e7ea..3abc2bc 100644 --- a/tests/input_pause.cpp +++ b/tests/input_pause.cpp @@ -80,28 +80,51 @@ void chooseInputAction(HWND resident, DWORD pid, const wchar_t *expected) } } -bool trayIconMatchesInput(HICON icon, bool active) +namespace { +template bool renderedTrayIconMatches(HICON icon, int size, Check check) { HDC dc = CreateCompatibleDC(nullptr); - HBITMAP bitmap = CreateBitmap(16, 16, 1, 32, nullptr); + HBITMAP bitmap = CreateBitmap(size, size, 1, 32, nullptr); bool matches = false; if (dc && bitmap) { const auto previous = SelectObject(dc, bitmap); - if (icon && DrawIconEx(dc, 0, 0, icon, 16, 16, 0, nullptr, DI_NORMAL)) { - const auto tile = GetPixel(dc, 2, 8); - const auto white = RGB(255, 255, 255); - const auto gap = GetPixel(dc, 8, 8); - // The active keycap is purple. The pause glyph has two white strokes with a gap. - matches = active ? GetBValue(tile) > GetRValue(tile) + 70 : - GetPixel(dc, 5, 8) == white && GetPixel(dc, 10, 8) == white && - GetRValue(gap) < 160 && std::abs(GetBValue(gap) - GetRValue(gap)) < 32; - } + if (icon && DrawIconEx(dc, 0, 0, icon, size, size, 0, nullptr, DI_NORMAL)) matches = check(dc); SelectObject(dc, previous); } if (bitmap) DeleteObject(bitmap); if (dc) DeleteDC(dc); return matches; } +} + +bool trayIconMatchesInput(HICON icon, bool active) +{ + return renderedTrayIconMatches(icon, 16, [active](HDC dc) { + const auto tile = GetPixel(dc, 2, 8); + const auto white = RGB(255, 255, 255); + const auto gap = GetPixel(dc, 8, 8); + // The active keycap is purple. The pause glyph has two white strokes with a gap. + return active ? GetBValue(tile) > GetRValue(tile) + 70 : + GetPixel(dc, 5, 8) == white && GetPixel(dc, 10, 8) == white && + GetRValue(gap) < 160 && std::abs(GetBValue(gap) - GetRValue(gap)) < 32; + }); +} + +bool trayIconMatchesLanBadge(HICON icon, int size, bool blocked) +{ + return renderedTrayIconMatches(icon, size, [=](HDC dc) { + if (!blocked) + { + const int center = size - 1 - 3 * size / 16; + return GetPixel(dc, center, center) == RGB(69, 213, 140); + } + const int center = size - 1 - size / 4; + const auto amber = RGB(255, 196, 87), ink = RGB(36, 36, 36); + return GetPixel(dc, center - size / 8, center) == amber && + GetPixel(dc, center, center) == ink && GetPixel(dc, center, center + 1) == amber && + GetPixel(dc, center, center + size / 8) == ink; + }); +} int testInputPause() { @@ -129,6 +152,7 @@ int testInputPause() return resident && query(resident, ProbeInputState) == 1; }, "resident did not start with both hooks and shortcuts"); require(query(resident, ProbeTrayIcon, TRUE), "active tray icon missing at startup"); + require(query(resident, ProbeTrayBlock), "tray shows a distinct warning during a LAN handoff block and restores green afterward"); constexpr UINT mods = MOD_CONTROL | MOD_SHIFT; require(!RegisterHotKey(nullptr, 1, mods, VK_F23) && GetLastError() == ERROR_HOTKEY_ALREADY_REGISTERED, "Files shortcut should be owned by resident before pause"); @@ -136,6 +160,7 @@ int testInputPause() until([&] { return query(resident, ProbeInputState) == 2; }, "pause did not release both hooks and bindings and cancel the queued snippet"); require(query(resident, ProbeTrayIcon), "pause bars missing after pause"); + require(query(resident, ProbeTrayBlock), "the handoff warning does not cover the paused tray icon"); require(RegisterHotKey(nullptr, 1, mods, VK_F23), "another process could not acquire Files shortcut"); require(query(resident, ProbeInputState, 1) == 2, "settings, layout reload or IPC restarted input"); chooseInputAction(resident, child.dwProcessId, L"Resume &input processing"); diff --git a/tests/input_pause.h b/tests/input_pause.h index 86f288b..19a5f04 100644 --- a/tests/input_pause.h +++ b/tests/input_pause.h @@ -3,5 +3,7 @@ inline constexpr UINT ProbeInputState = WM_APP + 200; inline constexpr UINT ProbeTrayIcon = WM_APP + 201; +inline constexpr UINT ProbeTrayBlock = WM_APP + 202; bool trayIconMatchesInput(HICON icon, bool active); +bool trayIconMatchesLanBadge(HICON icon, int size, bool blocked); int testInputPause();