1- // Copyright (c) 2025 The Bitcoin Core developers
1+ // Copyright (c) 2025 The Bitcoin Knots developers
22// Distributed under the MIT software license, see the accompanying
33// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
55#include < qt/wintaskbarprogress.h>
66
77#if defined(Q_OS_WIN)
88
9- #include < QWidget>
109#include < QWindow>
1110
1211#include < windows.h>
1312#include < shobjidl.h>
1413
1514WinTaskbarProgress::WinTaskbarProgress ()
1615{
17- // COM will be initialized by Qt or the application
1816}
1917
2018WinTaskbarProgress::~WinTaskbarProgress ()
2119{
2220 releaseTaskbar ();
2321}
2422
25- void WinTaskbarProgress::setWindow (QWidget * window)
23+ void WinTaskbarProgress::setWindow (QWindow * window)
2624{
27- if (!window) {
28- return ;
29- }
30-
31- // Get the native window handle
32- QWindow* qwindow = window->windowHandle ();
33- if (qwindow) {
34- m_winId = reinterpret_cast <HWND>(qwindow->winId ());
35- initializeTaskbar ();
36- }
25+ m_window = window;
3726}
3827
3928void WinTaskbarProgress::initializeTaskbar ()
4029{
41- if (m_initialized || !m_winId) {
30+ if (m_initialized || !m_window) {
31+ return ;
32+ }
33+
34+ HWND winId = reinterpret_cast <HWND>(m_window->winId ());
35+ if (!winId) {
4236 return ;
4337 }
4438
45- // Create ITaskbarList3 instance
4639 HRESULT hr = CoCreateInstance (
4740 CLSID_TaskbarList,
4841 nullptr ,
@@ -66,41 +59,76 @@ void WinTaskbarProgress::releaseTaskbar()
6659{
6760 if (m_taskbarList) {
6861 // Clear progress state before releasing
69- if (m_winId) {
70- m_taskbarList->SetProgressState (m_winId, TBPF_NOPROGRESS);
62+ if (m_window) {
63+ HWND winId = reinterpret_cast <HWND>(m_window->winId ());
64+ if (winId) {
65+ m_taskbarList->SetProgressState (winId, TBPF_NOPROGRESS);
66+ }
7167 }
7268 m_taskbarList->Release ();
7369 m_taskbarList = nullptr ;
7470 }
7571 m_initialized = false ;
72+ m_visible = false ;
7673}
7774
7875void WinTaskbarProgress::setValue (int value)
7976{
80- if (!m_initialized || !m_taskbarList || !m_winId) {
77+ // Lazy initialization on first use
78+ if (!m_initialized) {
79+ initializeTaskbar ();
80+ }
81+
82+ if (!m_initialized || !m_taskbarList || !m_window) {
83+ return ;
84+ }
85+
86+ HWND winId = reinterpret_cast <HWND>(m_window->winId ());
87+ if (!winId) {
8188 return ;
8289 }
8390
8491 // Clamp value to 0-100
8592 if (value < 0 ) value = 0 ;
8693 if (value > 100 ) value = 100 ;
8794
95+ // Use indeterminate mode for very low progress (< 1%) to show activity
96+ // Otherwise progress of 0 is invisible on the taskbar
97+ if (value < 1 ) {
98+ m_taskbarList->SetProgressState (winId, TBPF_INDETERMINATE);
99+ m_visible = true ;
100+ return ;
101+ }
102+
103+ // Switch to normal mode if not already visible
104+ if (!m_visible) {
105+ m_taskbarList->SetProgressState (winId, TBPF_NORMAL);
106+ m_visible = true ;
107+ }
108+
88109 // Set progress value (current, maximum)
89- m_taskbarList->SetProgressValue (m_winId , value, 100 );
110+ m_taskbarList->SetProgressValue (winId , value, 100 );
90111}
91112
92113void WinTaskbarProgress::setVisible (bool visible)
93114{
94- if (!m_initialized || !m_taskbarList || !m_winId) {
115+ if (!m_initialized || !m_taskbarList || !m_window) {
116+ return ;
117+ }
118+
119+ HWND winId = reinterpret_cast <HWND>(m_window->winId ());
120+ if (!winId) {
95121 return ;
96122 }
97123
98- if (visible) {
124+ if (visible && !m_visible ) {
99125 // Show normal progress
100- m_taskbarList->SetProgressState (m_winId, TBPF_NORMAL);
101- } else {
126+ m_taskbarList->SetProgressState (winId, TBPF_NORMAL);
127+ m_visible = true ;
128+ } else if (!visible && m_visible) {
102129 // Hide progress
103- m_taskbarList->SetProgressState (m_winId, TBPF_NOPROGRESS);
130+ m_taskbarList->SetProgressState (winId, TBPF_NOPROGRESS);
131+ m_visible = false ;
104132 }
105133}
106134
0 commit comments