From 1e9bebafc8c3805364f3c3b67c1300421db1bde3 Mon Sep 17 00:00:00 2001 From: StillMisty <1772797842@qq.com> Date: Fri, 4 Dec 2026 20:02:29 +0800 Subject: [PATCH] fix(windows): update thumbnail toolbar instead of re-adding it ThumbBarAddButtons is not a replace call once the toolbar exists. Windows 11 build 26300 returns S_OK from a second ThumbBarAddButtons while leaving the toolbar untouched, so calling it first on every sync meant ThumbBarUpdateButtons never ran: button icons / enabled flags stopped following after the first add (clicks and the progress indicator still worked). Update first once a toolbar has been created for the current shell taskbar item, and fall back to ThumbBarAddButtons on E_INVALIDARG (toolbar gone after the shell recreated the taskbar item). ReplayAllOnWindowThread still resets the flag on TaskbarButtonCreated. Verified on Windows 11 build 26300: icons, enabled state and tooltips follow immediately; hide-to-tray/restore and Explorer restart still recreate the toolbar. Refs libnativeapi/nativeapi#90 --- .../windows/window_taskbar_windows.cpp | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/platform/windows/window_taskbar_windows.cpp b/src/platform/windows/window_taskbar_windows.cpp index 3dc9e8f..a411eb2 100644 --- 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 { } } - // 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(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(kMaxThumbnailButtons), native_buttons.data()); + if (hr == E_INVALIDARG) { + hr = list->ThumbBarAddButtons(hwnd_, static_cast(kMaxThumbnailButtons), + native_buttons.data()); + } + } else { + hr = list->ThumbBarAddButtons(hwnd_, static_cast(kMaxThumbnailButtons), + native_buttons.data()); } if (SUCCEEDED(hr)) { - thumbbar_initialized_ = !snapshots.empty(); + thumbbar_initialized_ = true; } std::array old_icons{};