1313
1414WinTaskbarProgress::WinTaskbarProgress ()
1515{
16- // COM will be initialized by Qt or the application
17- // Register for TaskbarButtonCreated message
18- m_taskbarButtonCreatedMsg = RegisterWindowMessageW (L" TaskbarButtonCreated" );
1916}
2017
2118WinTaskbarProgress::~WinTaskbarProgress ()
@@ -25,26 +22,20 @@ WinTaskbarProgress::~WinTaskbarProgress()
2522
2623void WinTaskbarProgress::setWindow (QWindow* window)
2724{
28- if (!window) {
29- return ;
30- }
31-
32- // Get the native window handle
33- HWND newWinId = reinterpret_cast <HWND>(window->winId ());
34-
35- // Store window handle but DON'T initialize yet
36- // We must wait for TaskbarButtonCreated message
37- m_winId = newWinId;
25+ m_window = window;
3826}
3927
4028void WinTaskbarProgress::initializeTaskbar ()
4129{
42- 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) {
4336 return ;
4437 }
4538
46- // Create ITaskbarList3 instance
47- // This should ONLY be called after TaskbarButtonCreated message
4839 HRESULT hr = CoCreateInstance (
4940 CLSID_TaskbarList,
5041 nullptr ,
@@ -64,27 +55,15 @@ void WinTaskbarProgress::initializeTaskbar()
6455 }
6556}
6657
67- bool WinTaskbarProgress::nativeEventFilter (void * message)
68- {
69- MSG* msg = static_cast <MSG*>(message);
70-
71- // Wait for TaskbarButtonCreated before using taskbar features
72- if (msg->message == m_taskbarButtonCreatedMsg) {
73- if (!m_initialized && m_winId) {
74- initializeTaskbar ();
75- }
76- return false ;
77- }
78-
79- return false ;
80- }
81-
8258void WinTaskbarProgress::releaseTaskbar ()
8359{
8460 if (m_taskbarList) {
8561 // Clear progress state before releasing
86- if (m_winId) {
87- 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+ }
8867 }
8968 m_taskbarList->Release ();
9069 m_taskbarList = nullptr ;
@@ -95,37 +74,60 @@ void WinTaskbarProgress::releaseTaskbar()
9574
9675void WinTaskbarProgress::setValue (int value)
9776{
98- 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) {
9988 return ;
10089 }
10190
10291 // Clamp value to 0-100
10392 if (value < 0 ) value = 0 ;
10493 if (value > 100 ) value = 100 ;
10594
106- // If not visible yet, make it visible first
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
107104 if (!m_visible) {
108- m_taskbarList->SetProgressState (m_winId , TBPF_NORMAL);
105+ m_taskbarList->SetProgressState (winId , TBPF_NORMAL);
109106 m_visible = true ;
110107 }
111108
112109 // Set progress value (current, maximum)
113- m_taskbarList->SetProgressValue (m_winId , value, 100 );
110+ m_taskbarList->SetProgressValue (winId , value, 100 );
114111}
115112
116113void WinTaskbarProgress::setVisible (bool visible)
117114{
118- 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) {
119121 return ;
120122 }
121123
122124 if (visible && !m_visible) {
123125 // Show normal progress
124- m_taskbarList->SetProgressState (m_winId , TBPF_NORMAL);
126+ m_taskbarList->SetProgressState (winId , TBPF_NORMAL);
125127 m_visible = true ;
126128 } else if (!visible && m_visible) {
127129 // Hide progress
128- m_taskbarList->SetProgressState (m_winId , TBPF_NOPROGRESS);
130+ m_taskbarList->SetProgressState (winId , TBPF_NOPROGRESS);
129131 m_visible = false ;
130132 }
131133}
0 commit comments