From 51e8d3592acc8bacf326fe3933b5dec13bb518e6 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Fri, 17 Sep 2021 18:18:40 +0200 Subject: [PATCH] Let QLocale::uiLanguages() use WinRT API when possible This patch introduces support for the WinRT UI languages API. We are using the Win32 API to get the list of preferred languages when the system locale is used. However, this API returns an incomplete list. As Qt 6 supports Windows 10 and above, we can make use of the WinRT API, if it's supported by the compiler. This API returns the full list, as reported by the Windows system itself. Note however, that this API can't be used with Clang and MinGW, so we still have to fall back to Win32 API for these compilers. We also do it if WinRT API returns an empty list of languages for some reason. Fixes: QTBUG-94341 Pick-to: 6.2 Change-Id: I1d23c68d2ec298ae7835d0d18718876ff041aede Reviewed-by: Edward Welbourne --- src/corelib/CMakeLists.txt | 7 ++++++- src/corelib/text/qlocale_win.cpp | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index 83d26833fe..0320636c29 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -791,11 +791,16 @@ qt_internal_extend_target(Core CONDITION UNIX AND (NACL OR NOT APPLE) text/qlocale_unix.cpp ) -qt_internal_extend_target(Core CONDITION WIN32 AND (NACL OR NOT APPLE) +qt_internal_extend_target(Core CONDITION WIN32 SOURCES text/qlocale_win.cpp ) +qt_internal_extend_target(Core CONDITION WIN32 AND MSVC + LIBRARIES + runtimeobject +) + qt_internal_extend_target(Core CONDITION QT_FEATURE_icu SOURCES text/qcollator_icu.cpp diff --git a/src/corelib/text/qlocale_win.cpp b/src/corelib/text/qlocale_win.cpp index ef41eb6bd2..d2dd07e549 100644 --- a/src/corelib/text/qlocale_win.cpp +++ b/src/corelib/text/qlocale_win.cpp @@ -51,6 +51,20 @@ # include #endif +#if defined(Q_CC_MSVC) +# include +// Workaround for Windows SDK bug. +// See https://github.com/microsoft/Windows.UI.Composition-Win32-Samples/issues/47 +namespace winrt::impl +{ + template + auto wait_for(Async const& async, Windows::Foundation::TimeSpan const& timeout); +} +# include +# include +# include +#endif // defined(Q_CC_MSVC) + QT_BEGIN_NAMESPACE static QByteArray getWinLocaleName(LCID id = LOCALE_USER_DEFAULT); @@ -615,6 +629,18 @@ QVariant QSystemLocalePrivate::toCurrencyString(const QSystemLocale::CurrencyToS QVariant QSystemLocalePrivate::uiLanguages() { + QStringList result; +#if defined(Q_CC_MSVC) // msvc supports WinRT calls + using namespace winrt; + using namespace Windows::Foundation; + using namespace Windows::System::UserProfile; + auto languages = GlobalizationPreferences::Languages(); + for (const auto &lang : languages) + result << QString::fromStdString(winrt::to_string(lang)); + if (!result.isEmpty()) + return result; // else just fall back to WIN32 API implementation +#endif // defined(Q_CC_MSVC) + // mingw and clang still have to use Win32 API unsigned long cnt = 0; QVarLengthArray buf(64); # if !defined(QT_BOOTSTRAPPED) // Not present in MinGW 4.9/bootstrap builds. @@ -629,7 +655,6 @@ QVariant QSystemLocalePrivate::uiLanguages() } } # endif // !QT_BOOTSTRAPPED - QStringList result; result.reserve(cnt); const wchar_t *str = buf.constData(); for (; cnt > 0; --cnt) {