Skip to content

[Windows] Thumbnail toolbar button state stops updating: second ThumbBarAddButtons returns S_OK, so ThumbBarUpdateButtons never runs #89

Duplicate

Description

@StillMisty

[Windows] Thumbnail toolbar button state stops updating: second ThumbBarAddButtons returns S_OK, so ThumbBarUpdateButtons never runs

Environment

  • nativeapi / cnativeapi / nativeapi_flutter 0.6.3 (latest release, 2026-12-01)
  • Flutter 3.50.1 / Dart 3.14.0, MSVC 2022
  • Windows 11 build 26300 (26H2)

Summary

Changing a TaskbarThumbnailButton after it was added updates nativeapi's own state (GetIcon / IsEnabled reflect the new values), but the taskbar thumbnail toolbar keeps rendering the values from the first successful ThumbBarAddButtons. Button clicks still work, and the ITaskbarList3 progress indicator updates normally.

Expected

Adding a button to a WindowTaskbar, then changing its icon / enabled flag / tooltip, updates the visible thumbnail toolbar. specs/window-taskbar.md says exactly this:

按钮独立构造与配置属性,添加到某个 WindowTaskbar 后,修改属性立即同步到原生缩略图工具栏。

Actual

Only properties set before the first successful ThumbBarAddButtons are applied. Every later change is dropped by the shell.

Root cause

SyncThumbnailToolbarOnWindowThread() in src/platform/windows/window_taskbar_windows.cpp (core) calls ThumbBarAddButtons first on every sync, and only falls back to ThumbBarUpdateButtons when the add returns E_INVALIDARG:

HRESULT hr = list->ThumbBarAddButtons(hwnd_, kMaxThumbnailButtons,
                                      native_buttons.data());
if (hr == E_INVALIDARG) {
  hr = list->ThumbBarUpdateButtons(hwnd_, kMaxThumbnailButtons,
                                   native_buttons.data());
}

On Windows 11 build 26300 a second ThumbBarAddButtons for an existing toolbar returns S_OK instead of E_INVALIDARG, and leaves the existing toolbar untouched. ThumbBarUpdateButtons is therefore never called, and the toolbar keeps the state from the first add.

Evidence

Minimal Win32 repro, no Flutter/nativeapi involved: create a window, add 7 buttons with ThumbBarAddButtons, then call it again and log the HRESULT:

call HRESULT
ThumbBarAddButtons (first) 0x00000000 (S_OK)
ThumbBarAddButtons (second) 0x00000000 (S_OK) — expected E_INVALIDARG
ThumbBarUpdateButtons 0x00000000 (S_OK)

The same sequence works on Windows 10 / Windows 11 23H2–24H2 shells, where the second add returns E_INVALIDARG and the fallback runs. The repro program can be attached on request.

Proposed fix

Track whether the toolbar was created for the current shell taskbar item and update first:

HRESULT hr = S_OK;
if (thumbbar_initialized_) {
  hr = list->ThumbBarUpdateButtons(hwnd_, kMaxThumbnailButtons,
                                   native_buttons.data());
  if (hr == E_INVALIDARG) {
    // The shell recreated this window's taskbar item (hide/restore,
    // Explorer restart) and the toolbar is gone: create it again.
    hr = list->ThumbBarAddButtons(hwnd_, kMaxThumbnailButtons,
                                  native_buttons.data());
  }
} else {
  hr = list->ThumbBarAddButtons(hwnd_, kMaxThumbnailButtons,
                                native_buttons.data());
}
if (SUCCEEDED(hr)) {
  thumbbar_initialized_ = true;
}

Notes:

  • ReplayAllOnWindowThread() already resets thumbbar_initialized_ = false when TaskbarButtonCreated arrives (first show, restore after hide, Explorer restart), so the add path still runs after the shell recreates the taskbar item. The E_INVALIDARG fallback covers a missed/early flag reset.
  • thumbbar_initialized_ = true (instead of !snapshots.empty()) keeps the update path after ClearThumbnailButtons: a toolbar cannot be removed from a taskbar item, only its buttons cleared, so the next sync must not add again.

Patch

diff --git a/src/platform/windows/window_taskbar_windows.cpp b/src/platform/windows/window_taskbar_windows.cpp
--- a/src/platform/windows/window_taskbar_windows.cpp
+++ b/src/platform/windows/window_taskbar_windows.cpp
@@ -1089,20 +1089,29 @@ class TaskbarState : public std::enable_shared_from_this<TaskbarState> {
       }
     }

-    // Attempt ThumbBarAddButtons first on every sync: hiding a window destroys
-    // its shell taskbar item, so on restore a cached boolean would call
-    // ThumbBarUpdateButtons (which silently returns S_OK without recreating the
-    // buttons). When buttons already exist on the active taskbar item,
-    // ThumbBarAddButtons returns E_INVALIDARG and we fall back to
-    // ThumbBarUpdateButtons.
-    HRESULT hr = list->ThumbBarAddButtons(hwnd_, static_cast<UINT>(kMaxThumbnailButtons),
-                                          native_buttons.data());
-    if (hr == E_INVALIDARG) {
+    // The toolbar is created once per shell taskbar item; afterwards its
+    // buttons can only be changed through ThumbBarUpdateButtons. Current
+    // Windows 11 shells return S_OK from a second ThumbBarAddButtons while
+    // leaving the existing toolbar untouched, so adding first on every sync
+    // silently drops every later state change (icons / enabled flags).
+    // Update first and fall back to ThumbBarAddButtons only when the toolbar
+    // is gone, reported as E_INVALIDARG after the shell recreates the taskbar
+    // item (restore from hide, Explorer restart); ReplayAllOnWindowThread
+    // also resets the flag on TaskbarButtonCreated.
+    HRESULT hr = S_OK;
+    if (thumbbar_initialized_) {
       hr = list->ThumbBarUpdateButtons(hwnd_, static_cast<UINT>(kMaxThumbnailButtons),
                                        native_buttons.data());
+      if (hr == E_INVALIDARG) {
+        hr = list->ThumbBarAddButtons(hwnd_, static_cast<UINT>(kMaxThumbnailButtons),
+                                      native_buttons.data());
+      }
+    } else {
+      hr = list->ThumbBarAddButtons(hwnd_, static_cast<UINT>(kMaxThumbnailButtons),
+                                    native_buttons.data());
     }
     if (SUCCEEDED(hr)) {
-      thumbbar_initialized_ = !snapshots.empty();
+      thumbbar_initialized_ = true;
     }

Verification

The patch was applied to the local cnativeapi 0.6.3 core, cnativeapi.dll recompiled by the Dart native-assets hook, and a Flutter app using TaskbarThumbnailButton.icon / isEnabled / tooltip setters re-tested on Windows 11 build 26300:

  • play/pause icon and favorite icon now follow the app state immediately;
  • the disabled (no track) state follows;
  • hide-to-tray and restore still recreate the toolbar correctly;
  • ITaskbarList3 progress was unaffected before and after.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions