From b208ef99f2f0916c6b85d52176fcf5e466943521 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 24 Nov 2026 23:28:43 +0800 Subject: [PATCH] fix(windows): closing the window without a tray icon minimizes it With the tray on, closing the window hid it, whether or not the icon was up: where it could not go up the app was left running with no way back but launching it again (#1107). Closing now tries to put the icon up first, and without one the window is minimized, its taskbar button left. The icon failing to go up is written to events.log (Shell_NotifyIcon's error), once until it does, to find out why. Refs #1107 Co-Authored-By: BaoCode --- lib/notifications/attention_host.dart | 9 ++++++++- windows/runner/attention.cpp | 19 ++++++++++++++++++- windows/runner/attention.h | 3 +++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/notifications/attention_host.dart b/lib/notifications/attention_host.dart index 28bd1487f..0b4fe5cbf 100644 --- a/lib/notifications/attention_host.dart +++ b/lib/notifications/attention_host.dart @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import '../platform/app_platform.dart'; +import '../platform/event_log.dart'; import '../workspace/window_controls.dart'; import 'system_sounds_stub.dart' if (dart.library.io) 'system_sounds_io.dart' @@ -110,7 +111,13 @@ class ChannelAttentionHost implements AttentionHost { set onOpen(void Function(String? id)? handler) => _onOpen = handler; Future _handle(MethodCall call) async { - if (call.method == 'open') _onOpen?.call(call.arguments as String?); + switch (call.method) { + case 'open': + _onOpen?.call(call.arguments as String?); + // Windows: the tray icon could not go up (Shell_NotifyIcon's error). + case 'trayFailed': + appEvent('tray', 'icon not added: error ${call.arguments}'); + } return null; } diff --git a/windows/runner/attention.cpp b/windows/runner/attention.cpp index 38fd8d7dc..fe24ed0e7 100644 --- a/windows/runner/attention.cpp +++ b/windows/runner/attention.cpp @@ -426,7 +426,13 @@ std::optional Attention::HandleMessage(HWND window, UINT message, return std::nullopt; } if (tray_) { - ::ShowWindow(window, SW_HIDE); + // Hidden only with the icon up to bring it back: with none (it could + // not go up), minimized, its taskbar button left (#1107). + if (!icon_added_ || !IconThere()) { + icon_added_ = false; + ShowIcon(); + } + ::ShowWindow(window, icon_added_ ? SW_HIDE : SW_MINIMIZE); return 0; } return std::nullopt; @@ -557,9 +563,20 @@ void Attention::ShowIcon() { data.uVersion = NOTIFYICON_VERSION_4; ::Shell_NotifyIconW(NIM_SETVERSION, &data); } else { + const DWORD error = ::GetLastError(); shown = ::Shell_NotifyIconW(NIM_MODIFY, &data) != FALSE; + if (!shown && tray_ && !told_tray_failed_) { + // In events.log, to find why (#1107). + told_tray_failed_ = true; + channel_->InvokeMethod( + "trayFailed", std::make_unique( + static_cast(error))); + } } } + if (shown) { + told_tray_failed_ = false; + } icon_added_ = shown; if (tray_) { ::SetTimer(window_, kTrayTimer, kTrayCheckMilliseconds, nullptr); diff --git a/windows/runner/attention.h b/windows/runner/attention.h index eb012bfcf..417bbb739 100644 --- a/windows/runner/attention.h +++ b/windows/runner/attention.h @@ -122,6 +122,9 @@ class Attention { // Whether the tray icon is up, and whether Flutter asked for it (it is up // without, for a notification's sake, until the notification goes). bool icon_added_ = false; + // Whether Flutter was told the icon could not go up: once, until it does + // (the 15-second check tries again). + bool told_tray_failed_ = false; std::optional tray_; HICON icon_ = nullptr;