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 case MouseCursorPositioning:
290 return true;
291 default:
292 return QPlatformIntegration::hasCapability(cap);
293 }
294 return false;
295}
296
297QPlatformWindow *QWindowsIntegration::createPlatformWindow(QWindow *window) const
298{
299 QWindowsWindowData requested;
300 requested.flags = window->flags();
301 requested.geometry = window->isTopLevel()
302 ? QHighDpi::toNativePixels(window->geometry(), window)
303 : QHighDpi::toNativeLocalPosition(window->geometry(), window);
304 if (!(requested.flags & Qt::FramelessWindowHint)) {
305 // Apply custom margins (see QWindowsWindow::setCustomMargins())).
306 const QVariant customMarginsV = window->property("_q_windowsCustomMargins");
307 if (customMarginsV.isValid())
308 requested.customMargins = qvariant_cast<QMargins>(customMarginsV);
309 }
310
311 QWindowsWindowData obtained =
312 QWindowsWindowData::create(window, requested,
313 QWindowsWindow::formatWindowTitle(window->title()));
314 qCDebug(lcQpaWindow).nospace()
315 << __FUNCTION__ << ' ' << window
316 << "\n Requested: " << requested.geometry << " frame incl.="
317 << QWindowsGeometryHint::positionIncludesFrame(window)
318 << ' ' << requested.flags
319 << "\n Obtained : " << obtained.geometry << " margins=" << obtained.fullFrameMargins
320 << " handle=" << obtained.hwnd << ' ' << obtained.flags << '\n';
321
322 if (Q_UNLIKELY(!obtained.hwnd))
323 return nullptr;
324
325 QWindowsWindow *result = createPlatformWindowHelper(window, obtained);
326 Q_ASSERT(result);
327
328 if (window->isTopLevel() && !QWindowsContext::shouldHaveNonClientDpiScaling(window))
330
331 if (QWindowsMenuBar *menuBarToBeInstalled = QWindowsMenuBar::menuBarOf(window))
332 menuBarToBeInstalled->install(result);
333
334 return result;
335}
336
337QPlatformWindow *QWindowsIntegration::createForeignWindow(QWindow *window, WId nativeHandle) const
338{
339 const HWND hwnd = reinterpret_cast<HWND>(nativeHandle);
340 if (!IsWindow(hwnd)) {
341 qWarning("Windows QPA: Invalid foreign window ID %p.", hwnd);
342 return nullptr;
343 }
344 auto *result = new QWindowsForeignWindow(window, hwnd);
345 const QRect obtainedGeometry = result->geometry();
346 QScreen *screen = nullptr;
347 if (const QPlatformScreen *pScreen = result->screenForGeometry(obtainedGeometry))
348 screen = pScreen->screen();
349 if (screen && screen != window->screen())
350 window->setScreen(screen);
351 qCDebug(lcQpaWindow) << "Foreign window:" << window << Qt::showbase << Qt::hex
352 << result->winId() << Qt::noshowbase << Qt::dec << obtainedGeometry << screen;
353 return result;
354}
355
356// Overridden to return a QWindowsDirect2DWindow in Direct2D plugin.
357QWindowsWindow *QWindowsIntegration::createPlatformWindowHelper(QWindow *window, const QWindowsWindowData &data) const
358{
359 return new QWindowsWindow(window, data);
360}
361
362#ifndef QT_NO_OPENGL
363
365{
366#if defined(QT_OPENGL_DYNAMIC)
367 QWindowsOpenGLTester::Renderer requestedRenderer = QWindowsOpenGLTester::requestedRenderer();
368 switch (requestedRenderer) {
369 case QWindowsOpenGLTester::DesktopGl:
370 if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
371 if ((QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DisableRotationFlag)
372 && !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
373 qCWarning(lcQpaGl, "Unable to disable rotation.");
374 }
375 return glCtx;
376 }
377 qCWarning(lcQpaGl, "System OpenGL failed. Falling back to Software OpenGL.");
378 return QOpenGLStaticContext::create(true);
379 case QWindowsOpenGLTester::SoftwareRasterizer:
380 if (QWindowsStaticOpenGLContext *swCtx = QOpenGLStaticContext::create(true))
381 return swCtx;
382 qCWarning(lcQpaGl, "Software OpenGL failed. Falling back to system OpenGL.");
383 if (QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DesktopGl)
384 return QOpenGLStaticContext::create();
385 return nullptr;
386 default:
387 break;
388 }
389
390 const QWindowsOpenGLTester::Renderers supportedRenderers = QWindowsOpenGLTester::supportedRenderers(requestedRenderer);
391 if (supportedRenderers.testFlag(QWindowsOpenGLTester::DisableProgramCacheFlag)
392 && !QCoreApplication::testAttribute(Qt::AA_DisableShaderDiskCache)) {
393 QCoreApplication::setAttribute(Qt::AA_DisableShaderDiskCache);
394 }
395 if (supportedRenderers & QWindowsOpenGLTester::DesktopGl) {
396 if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
397 if ((supportedRenderers & QWindowsOpenGLTester::DisableRotationFlag)
398 && !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
399 qCWarning(lcQpaGl, "Unable to disable rotation.");
400 }
401 return glCtx;
402 }
403 }
404 return QOpenGLStaticContext::create(true);
405#else
407#endif
408}
409
414
416{
417 qCDebug(lcQpaGl) << __FUNCTION__ << context->format();
419 std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(context));
420 if (result->isValid())
421 return result.release();
422 }
423 return nullptr;
424}
425
427{
428#if !defined(QT_OPENGL_DYNAMIC)
429 return QOpenGLContext::LibGL;
430#else
431 if (const QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext())
432 return staticOpenGLContext->moduleType();
433 return QOpenGLContext::LibGL;
434#endif
435}
436
438{
440 return static_cast<HMODULE>(staticOpenGLContext->moduleHandle());
441
442 return nullptr;
443}
444
445QOpenGLContext *QWindowsIntegration::createOpenGLContext(HGLRC ctx, HWND window, QOpenGLContext *shareContext) const
446{
447 if (!ctx || !window)
448 return nullptr;
449
451 std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(ctx, window));
452 if (result->isValid()) {
453 auto *context = new QOpenGLContext;
454 context->setShareContext(shareContext);
455 auto *contextPrivate = QOpenGLContextPrivate::get(context);
456 contextPrivate->adopt(result.release());
457 return context;
458 }
459 }
460
461 return nullptr;
462}
463
465{
467 if (!integration)
468 return nullptr;
469 QWindowsIntegrationPrivate *d = integration->d.data();
470 QMutexLocker lock(&d->m_staticContextLock);
471 if (d->m_staticOpenGLContext.isNull())
472 d->m_staticOpenGLContext.reset(QWindowsStaticOpenGLContext::create());
473 return d->m_staticOpenGLContext.data();
474}
475#endif // !QT_NO_OPENGL
476
478{
479 if (!d->m_fontDatabase) {
480#ifndef QT_NO_FREETYPE
481 if (d->m_options & QWindowsIntegration::FontDatabaseFreeType)
482 d->m_fontDatabase = new QWindowsFontDatabaseFT;
483 else
484#endif // QT_NO_FREETYPE
485#if QT_CONFIG(directwrite3)
486 if (!(d->m_options & (QWindowsIntegration::FontDatabaseGDI | QWindowsIntegration::DontUseDirectWriteFonts)))
487 d->m_fontDatabase = new QWindowsDirectWriteFontDatabase;
488 else
489#endif
490 d->m_fontDatabase = new QWindowsFontDatabase;
491 }
492 return d->m_fontDatabase;
493}
494
495#ifdef SPI_GETKEYBOARDSPEED
496static inline int keyBoardAutoRepeatRateMS()
497{
498 DWORD time = 0;
499 if (SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, &time, 0))
500 return time ? 1000 / static_cast<int>(time) : 500;
501 return 30;
502}
503#endif
504
505QVariant QWindowsIntegration::styleHint(QPlatformIntegration::StyleHint hint) const
506{
507 switch (hint) {
508 case QPlatformIntegration::CursorFlashTime:
509 if (const unsigned timeMS = GetCaretBlinkTime())
510 return QVariant(timeMS != INFINITE ? int(timeMS) * 2 : 0);
511 break;
512#ifdef SPI_GETKEYBOARDSPEED
513 case KeyboardAutoRepeatRate:
514 return QVariant(keyBoardAutoRepeatRateMS());
515#endif
516 case QPlatformIntegration::ShowIsMaximized:
517 case QPlatformIntegration::StartDragTime:
518 case QPlatformIntegration::StartDragDistance:
519 case QPlatformIntegration::KeyboardInputInterval:
520 case QPlatformIntegration::ShowIsFullScreen:
521 case QPlatformIntegration::PasswordMaskDelay:
522 case QPlatformIntegration::StartDragVelocity:
523 break; // Not implemented
524 case QPlatformIntegration::FontSmoothingGamma:
525 return QVariant(QWindowsFontDatabase::fontSmoothingGamma());
526 case QPlatformIntegration::MouseDoubleClickInterval:
527 if (const UINT ms = GetDoubleClickTime())
528 return QVariant(int(ms));
529 break;
530 case QPlatformIntegration::UseRtlExtensions:
531 return QVariant(d->m_context.useRTLExtensions());
532 default:
533 break;
534 }
535 return QPlatformIntegration::styleHint(hint);
536}
537
538QPlatformKeyMapper *QWindowsIntegration::keyMapper() const
539{
540 return d->m_context.keyMapper();
541}
542
543#if QT_CONFIG(clipboard)
544QPlatformClipboard * QWindowsIntegration::clipboard() const
545{
546 return &d->m_clipboard;
547}
548# if QT_CONFIG(draganddrop)
549QPlatformDrag *QWindowsIntegration::drag() const
550{
551 return &d->m_drag;
552}
553# endif // QT_CONFIG(draganddrop)
554#endif // !QT_NO_CLIPBOARD
555
557{
558 return d->m_inputContext.data();
559}
560
561#if QT_CONFIG(accessibility)
562QPlatformAccessibility *QWindowsIntegration::accessibility() const
563{
564 return &d->m_accessibility;
565}
566#endif
567
569{
570 return d->m_options;
571}
572
573#if QT_CONFIG(sessionmanager)
574QPlatformSessionManager *QWindowsIntegration::createPlatformSessionManager(const QString &id, const QString &key) const
575{
576 return new QWindowsSessionManager(id, key);
577}
578#endif
579
581{
582 return new QWindowsGuiEventDispatcher;
583}
584
586{
587 return QStringList(QLatin1StringView(QWindowsTheme::name));
588}
589
591{
592 if (name == QLatin1StringView(QWindowsTheme::name))
593 return new QWindowsTheme;
594 return QPlatformIntegration::createPlatformTheme(name);
595}
596
598{
599 if (d->m_services.isNull())
600 d->m_services.reset(new QWindowsServices);
601
602 return d->m_services.data();
603}
604
606{
607 MessageBeep(MB_OK); // For QApplication
608}
609
611{
612 // Clamp to positive numbers, as the Windows API doesn't support negative numbers
613 number = qMax(0, number);
614
615 // Persist, so we can re-apply it on setting changes and Explorer restart
616 m_applicationBadgeNumber = number;
617
618 static const bool isWindows11 = QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows11;
619
620#if QT_CONFIG(cpp_winrt)
621 // We prefer the native BadgeUpdater API, that allows us to set a number directly,
622 // but it requires that the application has a package identity, and also doesn't
623 // seem to work in all cases on < Windows 11.
624 QT_TRY {
625 if (isWindows11 && qt_win_hasPackageIdentity()) {
626 using namespace winrt::Windows::UI::Notifications;
627 auto badgeXml = BadgeUpdateManager::GetTemplateContent(BadgeTemplateType::BadgeNumber);
628 badgeXml.SelectSingleNode(L"//badge/@value").NodeValue(winrt::box_value(winrt::to_hstring(number)));
629 BadgeUpdateManager::CreateBadgeUpdaterForApplication().Update(BadgeNotification(badgeXml));
630 return;
631 }
632 } QT_CATCH(...) {
633 // fall back to win32 implementation
634 }
635#endif
636
637 // Fallback for non-packaged apps, Windows 10, or Qt builds without WinRT/C++ support
638
639 if (!number) {
640 // Clear badge
641 setApplicationBadge(QImage());
642 return;
643 }
644
645 const bool isDarkMode = QWindowsTheme::instance()->colorScheme()
646 == Qt::ColorScheme::Dark;
647
648 QColor badgeColor;
649 QColor textColor;
650
651#if QT_CONFIG(cpp_winrt)
652 if (isWindows11) {
653 // Match colors used by BadgeUpdater
654 static const auto fromUIColor = [](winrt::Windows::UI::Color &&color) {
655 return QColor(color.R, color.G, color.B, color.A);
656 };
657 using namespace winrt::Windows::UI::ViewManagement;
658 const auto settings = UISettings();
659 badgeColor = fromUIColor(settings.GetColorValue(isDarkMode ?
660 UIColorType::AccentLight2 : UIColorType::Accent));
661 textColor = fromUIColor(settings.GetColorValue(UIColorType::Background));
662 }
663#endif
664
665 if (!badgeColor.isValid()) {
666 // Fall back to basic badge colors, based on Windows 10 look
667 badgeColor = isDarkMode ? Qt::black : QColor(220, 220, 220);
668 badgeColor.setAlphaF(0.5f);
669 textColor = isDarkMode ? Qt::white : Qt::black;
670 }
671
672 const auto devicePixelRatio = qApp->devicePixelRatio();
673
674 static const QSize iconBaseSize(16, 16);
675 QImage image(iconBaseSize * devicePixelRatio,
676 QImage::Format_ARGB32_Premultiplied);
677 image.fill(Qt::transparent);
678
679 QPainter painter(&image);
680
681 QRect badgeRect = image.rect();
682 QPen badgeBorderPen = Qt::NoPen;
683 if (!isWindows11) {
684 QColor badgeBorderColor = textColor;
685 badgeBorderColor.setAlphaF(0.5f);
686 badgeBorderPen = badgeBorderColor;
687 badgeRect.adjust(1, 1, -1, -1);
688 }
689 painter.setBrush(badgeColor);
690 painter.setPen(badgeBorderPen);
691 painter.setRenderHint(QPainter::Antialiasing);
692 painter.drawEllipse(badgeRect);
693
694 auto pixelSize = qCeil(10.5 * devicePixelRatio);
695 // Unlike the BadgeUpdater API we're limited by a square
696 // badge, so adjust the font size when above two digits.
697 const bool textOverflow = number > 99;
698 if (textOverflow)
699 pixelSize *= 0.8;
700
701 QFont font = painter.font();
702 font.setPixelSize(pixelSize);
703 font.setWeight(isWindows11 ? QFont::Medium : QFont::DemiBold);
704 painter.setFont(font);
705
706 painter.setRenderHint(QPainter::TextAntialiasing, devicePixelRatio > 1);
707 painter.setPen(textColor);
708
709 auto text = textOverflow ? u"99+"_s : QString::number(number);
710 painter.translate(textOverflow ? 1 : 0, textOverflow ? 0 : -1);
711 painter.drawText(image.rect(), Qt::AlignCenter, text);
712
713 painter.end();
714
715 setApplicationBadge(image);
716}
717
718void QWindowsIntegration::setApplicationBadge(const QImage &image)
719{
720 QComHelper comHelper;
721
722 ComPtr<ITaskbarList3> taskbarList;
723 CoCreateInstance(CLSID_TaskbarList, nullptr,
724 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&taskbarList));
725 if (!taskbarList) {
726 // There may not be any windows with a task bar button yet,
727 // in which case we'll apply the badge once a window with
728 // a button has been created.
729 return;
730 }
731
732 const auto hIcon = image.toHICON();
733
734 // Apply the icon to all top level windows, since the badge is
735 // set on an application level. If one of the windows go away
736 // the other windows will take over in showing the badge.
737 const auto topLevelWindows = QGuiApplication::topLevelWindows();
738 for (auto *topLevelWindow : topLevelWindows) {
739 if (!topLevelWindow->handle())
740 continue;
741 auto hwnd = reinterpret_cast<HWND>(topLevelWindow->winId());
742 taskbarList->SetOverlayIcon(hwnd, hIcon, L"");
743 }
744
745 DestroyIcon(hIcon);
746
747 // FIXME: Update icon when the application scale factor changes.
748 // Doing so in response to screen DPI changes is too soon, as the
749 // task bar is not yet ready for an updated icon, and will just
750 // result in a blurred icon even if our icon is high-DPI.
751}
752
754{
755 // The system color settings have changed, or we are reacting
756 // to a task bar button being created for the fist time or after
757 // Explorer had crashed and re-started. In any case, re-apply the
758 // badge so that everything is up to date.
759 if (m_applicationBadgeNumber)
760 setApplicationBadge(m_applicationBadgeNumber);
761}
762
763#if QT_CONFIG(vulkan)
764QPlatformVulkanInstance *QWindowsIntegration::createPlatformVulkanInstance(QVulkanInstance *instance) const
765{
766 return new QWindowsVulkanInstance(instance);
767}
768#endif
769
770QT_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