Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwindowsintegration.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2013 Samuel Gaist <samuel.gaist@edeltech.ch>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
10#include "qwindowsmenu.h"
12
13#include "qwindowsscreen.h"
14#include "qwindowstheme.h"
16#include <QtGui/private/qtgui-config_p.h>
17#if QT_CONFIG(directwrite3)
18#include <QtGui/private/qwindowsdirectwritefontdatabase_p.h>
19#endif
20#ifndef QT_NO_FREETYPE
21# include <QtGui/private/qwindowsfontdatabase_ft_p.h>
22#endif
23#include <QtGui/private/qwindowsfontdatabase_p.h>
24#if QT_CONFIG(clipboard)
25# include "qwindowsclipboard.h"
26# if QT_CONFIG(draganddrop)
27# include "qwindowsdrag.h"
28# endif
29#endif
32#if QT_CONFIG(accessibility)
33# include "uiautomation/qwindowsuiaaccessibility.h"
34#endif
35
36#include <qpa/qplatformnativeinterface.h>
37#include <qpa/qwindowsysteminterface.h>
38#if QT_CONFIG(sessionmanager)
39# include "qwindowssessionmanager.h"
40#endif
41#include <QtGui/qpointingdevice.h>
42#include <QtGui/private/qguiapplication_p.h>
43#include <QtGui/private/qhighdpiscaling_p.h>
44#include <QtGui/qpa/qplatforminputcontextfactory_p.h>
45#include <QtGui/qpa/qplatformcursor.h>
46
47#include <QtGui/private/qwindowsguieventdispatcher_p.h>
48
49#include <QtCore/qdebug.h>
50#include <QtCore/qvariant.h>
51
52#include <QtCore/qoperatingsystemversion.h>
53#include <QtCore/private/qfunctions_win_p.h>
54#include <QtCore/private/qcomptr_p.h>
55
56#include <limits.h>
57
58#if !defined(QT_NO_OPENGL)
60#endif
61
63
64#if QT_CONFIG(cpp_winrt)
65# include <QtCore/private/qt_winrtbase_p.h>
66# include <winrt/Windows.UI.Notifications.h>
67# include <winrt/Windows.Data.Xml.Dom.h>
68# include <winrt/Windows.Foundation.h>
69# include <winrt/Windows.UI.ViewManagement.h>
70#endif
71
72#include <memory>
73
74static inline void initOpenGlBlacklistResources()
75{
76 Q_INIT_RESOURCE(openglblacklists);
77}
78
79QT_BEGIN_NAMESPACE
80
81using namespace Qt::StringLiterals;
82
110
111template <typename IntType>
112bool parseIntOption(const QString &parameter,const QLatin1StringView &option,
113 IntType minimumValue, IntType maximumValue, IntType *target)
114{
115 const int valueLength = parameter.size() - option.size() - 1;
116 if (valueLength < 1 || !parameter.startsWith(option) || parameter.at(option.size()) != u'=')
117 return false;
118 bool ok;
119 const auto valueRef = QStringView{parameter}.right(valueLength);
120 const int value = valueRef.toInt(&ok);
121 if (ok) {
122 if (value >= int(minimumValue) && value <= int(maximumValue))
123 *target = static_cast<IntType>(value);
124 else {
125 qWarning() << "Value" << value << "for option" << option << "out of range"
126 << minimumValue << ".." << maximumValue;
127 }
128 } else {
129 qWarning() << "Invalid value" << valueRef << "for option" << option;
130 }
131 return true;
132}
133
134using DarkModeHandlingFlag = QNativeInterface::Private::QWindowsApplication::DarkModeHandlingFlag;
135using DarkModeHandling = QNativeInterface::Private::QWindowsApplication::DarkModeHandling;
136
137static inline unsigned parseOptions(const QStringList &paramList,
138 int *tabletAbsoluteRange,
139 QtWindows::DpiAwareness *dpiAwareness,
140 DarkModeHandling *darkModeHandling)
141{
142 unsigned options = 0;
143 for (const QString &param : paramList) {
144 if (param.startsWith(u"fontengine=")) {
145 if (param.endsWith(u"gdi")) {
146 options |= QWindowsIntegration::FontDatabaseGDI;
147 } else if (param.endsWith(u"freetype")) {
148 options |= QWindowsIntegration::FontDatabaseFreeType;
149 } else if (param.endsWith(u"native")) {
150 options |= QWindowsIntegration::FontDatabaseNative;
151 }
152 } else if (param.startsWith(u"dialogs=")) {
153 if (param.endsWith(u"xp")) {
154 options |= QWindowsIntegration::XpNativeDialogs;
155 } else if (param.endsWith(u"none")) {
156 options |= QWindowsIntegration::NoNativeDialogs;
157 }
158 } else if (param == u"altgr") {
159 options |= QWindowsIntegration::DetectAltGrModifier;
160 } else if (param == u"gl=gdi") {
161 options |= QWindowsIntegration::DisableArb;
162 } else if (param == u"nodirectwrite") {
163 options |= QWindowsIntegration::DontUseDirectWriteFonts;
164 } else if (param == u"nocolorfonts") {
165 options |= QWindowsIntegration::DontUseColorFonts;
166 } else if (param == u"nomousefromtouch") {
167 options |= QWindowsIntegration::DontPassOsMouseEventsSynthesizedFromTouch;
168 } else if (parseIntOption(param, "verbose"_L1, 0, INT_MAX, &QWindowsContext::verbose)
169 || parseIntOption(param, "tabletabsoluterange"_L1, 0, INT_MAX, tabletAbsoluteRange)
170 || parseIntOption(param, "dpiawareness"_L1, QtWindows::DpiAwareness::Invalid,
171 QtWindows::DpiAwareness::PerMonitorVersion2, dpiAwareness)) {
172 } else if (param == u"menus=native") {
173 options |= QWindowsIntegration::AlwaysUseNativeMenus;
174 } else if (param == u"menus=none") {
175 options |= QWindowsIntegration::NoNativeMenus;
176 } else if (param == u"reverse") {
177 options |= QWindowsIntegration::RtlEnabled;
178 } else if (param == u"darkmode=0") {
179 *darkModeHandling = {};
180 } else if (param == u"darkmode=1") {
181 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeWindowFrames);
182 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeStyle, false);
183 } else if (param == u"darkmode=2") {
184 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeWindowFrames);
185 darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeStyle);
186 } else {
187 qWarning() << "Unknown option" << param;
188 }
189 }
190 return options;
191}
192
193void QWindowsIntegrationPrivate::parseOptions(QWindowsIntegration *q, const QStringList &paramList)
194{
196
197 static bool dpiAwarenessSet = false;
198 // Default to per-monitor-v2 awareness (if available)
199 QtWindows::DpiAwareness dpiAwareness = QtWindows::DpiAwareness::PerMonitorVersion2;
200
201 int tabletAbsoluteRange = -1;
202 DarkModeHandling darkModeHandling = DarkModeHandlingFlag::DarkModeWindowFrames
203 | DarkModeHandlingFlag::DarkModeStyle;
204 m_options = ::parseOptions(paramList, &tabletAbsoluteRange, &dpiAwareness, &darkModeHandling);
205 q->setDarkModeHandling(darkModeHandling);
206 QWindowsFontDatabase::setFontOptions(m_options);
207 if (tabletAbsoluteRange >= 0)
209
210 QCoreApplication::setAttribute(Qt::AA_CompressHighFrequencyEvents);
211 QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(false);
212
213 if (!dpiAwarenessSet) { // Set only once in case of repeated instantiations of QGuiApplication.
214 if (!QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
215 m_context.setProcessDpiAwareness(dpiAwareness);
216 qCDebug(lcQpaWindow) << "DpiAwareness=" << dpiAwareness
217 << "effective process DPI awareness=" << QWindowsContext::processDpiAwareness();
218 }
219 dpiAwarenessSet = true;
220 }
221
222 m_context.initTouch(m_options);
223 QPlatformCursor::setCapability(QPlatformCursor::OverrideCursor);
224
225 m_context.initPowerNotificationHandler();
226}
227
229{
230 delete m_fontDatabase;
231}
232
233QWindowsIntegration *QWindowsIntegration::m_instance = nullptr;
234
235QWindowsIntegration::QWindowsIntegration(const QStringList &paramList) :
237{
238 m_instance = this;
239 d->parseOptions(this, paramList);
240#if QT_CONFIG(clipboard)
241 d->m_clipboard.registerViewer();
242#endif
243 d->m_context.screenManager().initialize();
244 d->m_context.setDetectAltGrModifier((d->m_options & DetectAltGrModifier) != 0);
245}
246
248{
249 m_instance = nullptr;
250}
251
253{
254 auto icStrs = QPlatformInputContextFactory::requested();
255 icStrs.isEmpty() ? d->m_inputContext.reset(new QWindowsInputContext)
256 : d->m_inputContext.reset(QPlatformInputContextFactory::create(icStrs));
257}
258
259bool QWindowsIntegration::hasCapability(QPlatformIntegration::Capability cap) const
260{
261 switch (cap) {
262 case ThreadedPixmaps:
263 return true;
264#ifndef QT_NO_OPENGL
265 case OpenGL:
266#if !QT_CONFIG(run_opengl_tests)
267 // Workaround for build configs on WoA that don't have OpenGL installed
268 // FIXME: Detect at runtime
269 return false;
270#endif
271 return true;
272 case ThreadedOpenGL:
274 return glContext->supportsThreadedOpenGL();
275 return false;
276#endif // !QT_NO_OPENGL
277 case WindowMasks:
278 return true;
279 case MultipleWindows:
280 return true;
281 case ForeignWindows:
282 return true;
283 case AllGLFunctionsQueryable:
284 return true;
285 case SwitchableWidgetComposition:
286 return false; // QTBUG-68329 QTBUG-53515 QTBUG-54734
287 case BackingStoreStaticContents:
288 return true;
289 default:
290 return QPlatformIntegration::hasCapability(cap);
291 }
292 return false;
293}
294
295QPlatformWindow *QWindowsIntegration::createPlatformWindow(QWindow *window) const
296{
297 QWindowsWindowData requested;
298 requested.flags = window->flags();
299 requested.geometry = window->isTopLevel()
300 ? QHighDpi::toNativePixels(window->geometry(), window)
301 : QHighDpi::toNativeLocalPosition(window->geometry(), window);
302 if (!(requested.flags & Qt::FramelessWindowHint)) {
303 // Apply custom margins (see QWindowsWindow::setCustomMargins())).
304 const QVariant customMarginsV = window->property("_q_windowsCustomMargins");
305 if (customMarginsV.isValid())
306 requested.customMargins = qvariant_cast<QMargins>(customMarginsV);
307 }
308
309 QWindowsWindowData obtained =
310 QWindowsWindowData::create(window, requested,
311 QWindowsWindow::formatWindowTitle(window->title()));
312 qCDebug(lcQpaWindow).nospace()
313 << __FUNCTION__ << ' ' << window
314 << "\n Requested: " << requested.geometry << " frame incl.="
315 << QWindowsGeometryHint::positionIncludesFrame(window)
316 << ' ' << requested.flags
317 << "\n Obtained : " << obtained.geometry << " margins=" << obtained.fullFrameMargins
318 << " handle=" << obtained.hwnd << ' ' << obtained.flags << '\n';
319
320 if (Q_UNLIKELY(!obtained.hwnd))
321 return nullptr;
322
323 QWindowsWindow *result = createPlatformWindowHelper(window, obtained);
324 Q_ASSERT(result);
325
326 if (window->isTopLevel() && !QWindowsContext::shouldHaveNonClientDpiScaling(window))
328
329 if (QWindowsMenuBar *menuBarToBeInstalled = QWindowsMenuBar::menuBarOf(window))
330 menuBarToBeInstalled->install(result);
331
332 return result;
333}
334
335QPlatformWindow *QWindowsIntegration::createForeignWindow(QWindow *window, WId nativeHandle) const
336{
337 const HWND hwnd = reinterpret_cast<HWND>(nativeHandle);
338 if (!IsWindow(hwnd)) {
339 qWarning("Windows QPA: Invalid foreign window ID %p.", hwnd);
340 return nullptr;
341 }
342 auto *result = new QWindowsForeignWindow(window, hwnd);
343 const QRect obtainedGeometry = result->geometry();
344 QScreen *screen = nullptr;
345 if (const QPlatformScreen *pScreen = result->screenForGeometry(obtainedGeometry))
346 screen = pScreen->screen();
347 if (screen && screen != window->screen())
348 window->setScreen(screen);
349 qCDebug(lcQpaWindow) << "Foreign window:" << window << Qt::showbase << Qt::hex
350 << result->winId() << Qt::noshowbase << Qt::dec << obtainedGeometry << screen;
351 return result;
352}
353
354// Overridden to return a QWindowsDirect2DWindow in Direct2D plugin.
355QWindowsWindow *QWindowsIntegration::createPlatformWindowHelper(QWindow *window, const QWindowsWindowData &data) const
356{
357 return new QWindowsWindow(window, data);
358}
359
360#ifndef QT_NO_OPENGL
361
363{
364#if defined(QT_OPENGL_DYNAMIC)
365 QWindowsOpenGLTester::Renderer requestedRenderer = QWindowsOpenGLTester::requestedRenderer();
366 switch (requestedRenderer) {
367 case QWindowsOpenGLTester::DesktopGl:
368 if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
369 if ((QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DisableRotationFlag)
370 && !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
371 qCWarning(lcQpaGl, "Unable to disable rotation.");
372 }
373 return glCtx;
374 }
375 qCWarning(lcQpaGl, "System OpenGL failed. Falling back to Software OpenGL.");
376 return QOpenGLStaticContext::create(true);
377 case QWindowsOpenGLTester::SoftwareRasterizer:
378 if (QWindowsStaticOpenGLContext *swCtx = QOpenGLStaticContext::create(true))
379 return swCtx;
380 qCWarning(lcQpaGl, "Software OpenGL failed. Falling back to system OpenGL.");
381 if (QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DesktopGl)
382 return QOpenGLStaticContext::create();
383 return nullptr;
384 default:
385 break;
386 }
387
388 const QWindowsOpenGLTester::Renderers supportedRenderers = QWindowsOpenGLTester::supportedRenderers(requestedRenderer);
389 if (supportedRenderers.testFlag(QWindowsOpenGLTester::DisableProgramCacheFlag)
390 && !QCoreApplication::testAttribute(Qt::AA_DisableShaderDiskCache)) {
391 QCoreApplication::setAttribute(Qt::AA_DisableShaderDiskCache);
392 }
393 if (supportedRenderers & QWindowsOpenGLTester::DesktopGl) {
394 if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
395 if ((supportedRenderers & QWindowsOpenGLTester::DisableRotationFlag)
396 && !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
397 qCWarning(lcQpaGl, "Unable to disable rotation.");
398 }
399 return glCtx;
400 }
401 }
402 return QOpenGLStaticContext::create(true);
403#else
405#endif
406}
407
412
414{
415 qCDebug(lcQpaGl) << __FUNCTION__ << context->format();
417 std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(context));
418 if (result->isValid())
419 return result.release();
420 }
421 return nullptr;
422}
423
425{
426#if !defined(QT_OPENGL_DYNAMIC)
427 return QOpenGLContext::LibGL;
428#else
429 if (const QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext())
430 return staticOpenGLContext->moduleType();
431 return QOpenGLContext::LibGL;
432#endif
433}
434
436{
438 return static_cast<HMODULE>(staticOpenGLContext->moduleHandle());
439
440 return nullptr;
441}
442
443QOpenGLContext *QWindowsIntegration::createOpenGLContext(HGLRC ctx, HWND window, QOpenGLContext *shareContext) const
444{
445 if (!ctx || !window)
446 return nullptr;
447
449 std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(ctx, window));
450 if (result->isValid()) {
451 auto *context = new QOpenGLContext;
452 context->setShareContext(shareContext);
453 auto *contextPrivate = QOpenGLContextPrivate::get(context);
454 contextPrivate->adopt(result.release());
455 return context;
456 }
457 }
458
459 return nullptr;
460}
461
463{
465 if (!integration)
466 return nullptr;
467 QWindowsIntegrationPrivate *d = integration->d.data();
468 QMutexLocker lock(&d->m_staticContextLock);
469 if (d->m_staticOpenGLContext.isNull())
470 d->m_staticOpenGLContext.reset(QWindowsStaticOpenGLContext::create());
471 return d->m_staticOpenGLContext.data();
472}
473#endif // !QT_NO_OPENGL
474
476{
477 if (!d->m_fontDatabase) {
478#ifndef QT_NO_FREETYPE
479 if (d->m_options & QWindowsIntegration::FontDatabaseFreeType)
480 d->m_fontDatabase = new QWindowsFontDatabaseFT;
481 else
482#endif // QT_NO_FREETYPE
483#if QT_CONFIG(directwrite3)
484 if (!(d->m_options & (QWindowsIntegration::FontDatabaseGDI | QWindowsIntegration::DontUseDirectWriteFonts)))
485 d->m_fontDatabase = new QWindowsDirectWriteFontDatabase;
486 else
487#endif
488 d->m_fontDatabase = new QWindowsFontDatabase;
489 }
490 return d->m_fontDatabase;
491}
492
493#ifdef SPI_GETKEYBOARDSPEED
494static inline int keyBoardAutoRepeatRateMS()
495{
496 DWORD time = 0;
497 if (SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, &time, 0))
498 return time ? 1000 / static_cast<int>(time) : 500;
499 return 30;
500}
501#endif
502
503QVariant QWindowsIntegration::styleHint(QPlatformIntegration::StyleHint hint) const
504{
505 switch (hint) {
506 case QPlatformIntegration::CursorFlashTime:
507 if (const unsigned timeMS = GetCaretBlinkTime())
508 return QVariant(timeMS != INFINITE ? int(timeMS) * 2 : 0);
509 break;
510#ifdef SPI_GETKEYBOARDSPEED
511 case KeyboardAutoRepeatRate:
512 return QVariant(keyBoardAutoRepeatRateMS());
513#endif
514 case QPlatformIntegration::ShowIsMaximized:
515 case QPlatformIntegration::StartDragTime:
516 case QPlatformIntegration::StartDragDistance:
517 case QPlatformIntegration::KeyboardInputInterval:
518 case QPlatformIntegration::ShowIsFullScreen:
519 case QPlatformIntegration::PasswordMaskDelay:
520 case QPlatformIntegration::StartDragVelocity:
521 break; // Not implemented
522 case QPlatformIntegration::FontSmoothingGamma:
523 return QVariant(QWindowsFontDatabase::fontSmoothingGamma());
524 case QPlatformIntegration::MouseDoubleClickInterval:
525 if (const UINT ms = GetDoubleClickTime())
526 return QVariant(int(ms));
527 break;
528 case QPlatformIntegration::UseRtlExtensions:
529 return QVariant(d->m_context.useRTLExtensions());
530 default:
531 break;
532 }
533 return QPlatformIntegration::styleHint(hint);
534}
535
536QPlatformKeyMapper *QWindowsIntegration::keyMapper() const
537{
538 return d->m_context.keyMapper();
539}
540
541#if QT_CONFIG(clipboard)
542QPlatformClipboard * QWindowsIntegration::clipboard() const
543{
544 return &d->m_clipboard;
545}
546# if QT_CONFIG(draganddrop)
547QPlatformDrag *QWindowsIntegration::drag() const
548{
549 return &d->m_drag;
550}
551# endif // QT_CONFIG(draganddrop)
552#endif // !QT_NO_CLIPBOARD
553
555{
556 return d->m_inputContext.data();
557}
558
559#if QT_CONFIG(accessibility)
560QPlatformAccessibility *QWindowsIntegration::accessibility() const
561{
562 return &d->m_accessibility;
563}
564#endif
565
567{
568 return d->m_options;
569}
570
571#if QT_CONFIG(sessionmanager)
572QPlatformSessionManager *QWindowsIntegration::createPlatformSessionManager(const QString &id, const QString &key) const
573{
574 return new QWindowsSessionManager(id, key);
575}
576#endif
577
579{
580 return new QWindowsGuiEventDispatcher;
581}
582
584{
585 return QStringList(QLatin1StringView(QWindowsTheme::name));
586}
587
589{
590 if (name == QLatin1StringView(QWindowsTheme::name))
591 return new QWindowsTheme;
592 return QPlatformIntegration::createPlatformTheme(name);
593}
594
596{
597 if (d->m_services.isNull())
598 d->m_services.reset(new QWindowsServices);
599
600 return d->m_services.data();
601}
602
604{
605 MessageBeep(MB_OK); // For QApplication
606}
607
609{
610 // Clamp to positive numbers, as the Windows API doesn't support negative numbers
611 number = qMax(0, number);
612
613 // Persist, so we can re-apply it on setting changes and Explorer restart
614 m_applicationBadgeNumber = number;
615
616 static const bool isWindows11 = QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows11;
617
618#if QT_CONFIG(cpp_winrt)
619 // We prefer the native BadgeUpdater API, that allows us to set a number directly,
620 // but it requires that the application has a package identity, and also doesn't
621 // seem to work in all cases on < Windows 11.
622 QT_TRY {
623 if (isWindows11 && qt_win_hasPackageIdentity()) {
624 using namespace winrt::Windows::UI::Notifications;
625 auto badgeXml = BadgeUpdateManager::GetTemplateContent(BadgeTemplateType::BadgeNumber);
626 badgeXml.SelectSingleNode(L"//badge/@value").NodeValue(winrt::box_value(winrt::to_hstring(number)));
627 BadgeUpdateManager::CreateBadgeUpdaterForApplication().Update(BadgeNotification(badgeXml));
628 return;
629 }
630 } QT_CATCH(...) {
631 // fall back to win32 implementation
632 }
633#endif
634
635 // Fallback for non-packaged apps, Windows 10, or Qt builds without WinRT/C++ support
636
637 if (!number) {
638 // Clear badge
639 setApplicationBadge(QImage());
640 return;
641 }
642
643 const bool isDarkMode = QWindowsTheme::instance()->colorScheme()
644 == Qt::ColorScheme::Dark;
645
646 QColor badgeColor;
647 QColor textColor;
648
649#if QT_CONFIG(cpp_winrt)
650 if (isWindows11) {
651 // Match colors used by BadgeUpdater
652 static const auto fromUIColor = [](winrt::Windows::UI::Color &&color) {
653 return QColor(color.R, color.G, color.B, color.A);
654 };
655 using namespace winrt::Windows::UI::ViewManagement;
656 const auto settings = UISettings();
657 badgeColor = fromUIColor(settings.GetColorValue(isDarkMode ?
658 UIColorType::AccentLight2 : UIColorType::Accent));
659 textColor = fromUIColor(settings.GetColorValue(UIColorType::Background));
660 }
661#endif
662
663 if (!badgeColor.isValid()) {
664 // Fall back to basic badge colors, based on Windows 10 look
665 badgeColor = isDarkMode ? Qt::black : QColor(220, 220, 220);
666 badgeColor.setAlphaF(0.5f);
667 textColor = isDarkMode ? Qt::white : Qt::black;
668 }
669
670 const auto devicePixelRatio = qApp->devicePixelRatio();
671
672 static const QSize iconBaseSize(16, 16);
673 QImage image(iconBaseSize * devicePixelRatio,
674 QImage::Format_ARGB32_Premultiplied);
675 image.fill(Qt::transparent);
676
677 QPainter painter(&image);
678
679 QRect badgeRect = image.rect();
680 QPen badgeBorderPen = Qt::NoPen;
681 if (!isWindows11) {
682 QColor badgeBorderColor = textColor;
683 badgeBorderColor.setAlphaF(0.5f);
684 badgeBorderPen = badgeBorderColor;
685 badgeRect.adjust(1, 1, -1, -1);
686 }
687 painter.setBrush(badgeColor);
688 painter.setPen(badgeBorderPen);
689 painter.setRenderHint(QPainter::Antialiasing);
690 painter.drawEllipse(badgeRect);
691
692 auto pixelSize = qCeil(10.5 * devicePixelRatio);
693 // Unlike the BadgeUpdater API we're limited by a square
694 // badge, so adjust the font size when above two digits.
695 const bool textOverflow = number > 99;
696 if (textOverflow)
697 pixelSize *= 0.8;
698
699 QFont font = painter.font();
700 font.setPixelSize(pixelSize);
701 font.setWeight(isWindows11 ? QFont::Medium : QFont::DemiBold);
702 painter.setFont(font);
703
704 painter.setRenderHint(QPainter::TextAntialiasing, devicePixelRatio > 1);
705 painter.setPen(textColor);
706
707 auto text = textOverflow ? u"99+"_s : QString::number(number);
708 painter.translate(textOverflow ? 1 : 0, textOverflow ? 0 : -1);
709 painter.drawText(image.rect(), Qt::AlignCenter, text);
710
711 painter.end();
712
713 setApplicationBadge(image);
714}
715
716void QWindowsIntegration::setApplicationBadge(const QImage &image)
717{
718 QComHelper comHelper;
719
720 ComPtr<ITaskbarList3> taskbarList;
721 CoCreateInstance(CLSID_TaskbarList, nullptr,
722 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&taskbarList));
723 if (!taskbarList) {
724 // There may not be any windows with a task bar button yet,
725 // in which case we'll apply the badge once a window with
726 // a button has been created.
727 return;
728 }
729
730 const auto hIcon = image.toHICON();
731
732 // Apply the icon to all top level windows, since the badge is
733 // set on an application level. If one of the windows go away
734 // the other windows will take over in showing the badge.
735 const auto topLevelWindows = QGuiApplication::topLevelWindows();
736 for (auto *topLevelWindow : topLevelWindows) {
737 if (!topLevelWindow->handle())
738 continue;
739 auto hwnd = reinterpret_cast<HWND>(topLevelWindow->winId());
740 taskbarList->SetOverlayIcon(hwnd, hIcon, L"");
741 }
742
743 DestroyIcon(hIcon);
744
745 // FIXME: Update icon when the application scale factor changes.
746 // Doing so in response to screen DPI changes is too soon, as the
747 // task bar is not yet ready for an updated icon, and will just
748 // result in a blurred icon even if our icon is high-DPI.
749}
750
752{
753 // The system color settings have changed, or we are reacting
754 // to a task bar button being created for the fist time or after
755 // Explorer had crashed and re-started. In any case, re-apply the
756 // badge so that everything is up to date.
757 if (m_applicationBadgeNumber)
758 setApplicationBadge(m_applicationBadgeNumber);
759}
760
761#if QT_CONFIG(vulkan)
762QPlatformVulkanInstance *QWindowsIntegration::createPlatformVulkanInstance(QVulkanInstance *instance) const
763{
764 return new QWindowsVulkanInstance(instance);
765}
766#endif
767
768QT_END_NAMESPACE
Static Open GL context containing version information, extension function pointers,...
static QOpenGLStaticContext * create(bool softwareRendering=false)
Singleton container for all relevant information.
static void setTabletAbsoluteRange(int a)
Window wrapping a foreign native window.
QStringList themeNames() const override
QPlatformKeyMapper * keyMapper() const override
Accessor for the platform integration's key mapper.
static QWindowsStaticOpenGLContext * staticOpenGLContext()
void setApplicationBadge(const QImage &image)
QOpenGLContext * createOpenGLContext(HGLRC context, HWND window, QOpenGLContext *shareContext) const override
QWindowsIntegration(const QStringList &paramList)
QAbstractEventDispatcher * createEventDispatcher() const override
Factory function for the GUI event dispatcher.
QPlatformTheme * createPlatformTheme(const QString &name) const override
bool hasCapability(QPlatformIntegration::Capability cap) const override
QOpenGLContext::OpenGLModuleType openGLModuleType() override
Platform integration function for querying the OpenGL implementation type.
QPlatformFontDatabase * fontDatabase() const override
Accessor for the platform integration's fontdatabase.
void initialize() override
Performs initialization steps that depend on having an event dispatcher available.
void setApplicationBadge(qint64 number) override
static QWindowsIntegration * instance()
HMODULE openGLModuleHandle() const override
void beep() const override
QPlatformInputContext * inputContext() const override
Returns the platforms input context.
QPlatformServices * services() const override
QVariant styleHint(StyleHint hint) const override
QPlatformOpenGLContext * createPlatformOpenGLContext(QOpenGLContext *context) const override
Factory function for QPlatformOpenGLContext.
Windows native menu bar.
void install(QWindowsWindow *window)
virtual void * moduleHandle() const =0
virtual bool supportsThreadedOpenGL() const
static const char * name
Raster or OpenGL Window.
void setFlag(unsigned f) const
static void initOpenGlBlacklistResources()
bool parseIntOption(const QString &parameter, const QLatin1StringView &option, IntType minimumValue, IntType maximumValue, IntType *target)
static unsigned parseOptions(const QStringList &paramList, int *tabletAbsoluteRange, QtWindows::DpiAwareness *dpiAwareness, DarkModeHandling *darkModeHandling)
QScopedPointer< QWindowsStaticOpenGLContext > m_staticOpenGLContext
void parseOptions(QWindowsIntegration *q, const QStringList &paramList)
QScopedPointer< QWindowsServices > m_services
QPlatformFontDatabase * m_fontDatabase
QScopedPointer< QPlatformInputContext > m_inputContext