support use different icon font based on current platform

And some other minor tweaks

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-07-11 10:55:05 +08:00
parent 4f8106a009
commit b8fee2e732
6 changed files with 71 additions and 59 deletions

View File

@ -1,5 +1,7 @@
<RCC>
<qresource prefix="/org.wangwenx190.FramelessHelper">
<file>resources/fonts/Segoe Fluent Icons.ttf</file>
<file>resources/fonts/Segoe MDL2 Assets.ttf</file>
<file>resources/fonts/Micon.ttf</file>
</qresource>
</RCC>

View File

@ -24,8 +24,8 @@
#include "framelessmanager.h"
#include "framelessmanager_p.h"
#include <QtCore/qmutex.h>
#include <QtCore/qdebug.h>
#include <QtCore/qmutex.h>
#include <QtGui/qscreen.h>
#include <QtGui/qwindow.h>
#include <QtGui/qfontdatabase.h>
@ -59,10 +59,41 @@ Q_GLOBAL_STATIC(FramelessManagerHelper, g_helper)
Q_GLOBAL_STATIC(FramelessManager, g_manager)
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFilePath, ":/org.wangwenx190.FramelessHelper/resources/fonts/Segoe Fluent Icons.ttf")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontName, "Segoe Fluent Icons")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontResourcePrefix, ":/org.wangwenx190.FramelessHelper/resources/fonts/")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFileName_win11, "Segoe Fluent Icons.ttf")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFileName_win, "Segoe MDL2 Assets.ttf")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFileName_unix, "Micon.ttf")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFamilyName_win11, "Segoe Fluent Icons")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFamilyName_win, "Segoe MDL2 Assets")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFamilyName_unix, "micon_nb")
static constexpr const int kIconFontPointSize = 8;
[[nodiscard]] static inline QString iconFontFilePath()
{
static const QString result = []() -> QString {
#ifdef Q_OS_WINDOWS
static const bool isWin11OrGreater = Utils::isWindowsVersionOrGreater(WindowsVersion::_11_21H2);
return (kIconFontResourcePrefix + (isWin11OrGreater ? kIconFontFileName_win11 : kIconFontFileName_win));
#else
return (kIconFontResourcePrefix + kIconFontFileName_unix);
#endif
}();
return result;
}
[[nodiscard]] static inline QString iconFontFamilyName()
{
static const QString result = []() -> QString {
#ifdef Q_OS_WINDOWS
static const bool isWin11OrGreater = Utils::isWindowsVersionOrGreater(WindowsVersion::_11_21H2);
return (isWin11OrGreater ? kIconFontFamilyName_win11 : kIconFontFamilyName_win);
#else
return kIconFontFamilyName_unix;
#endif
}();
return result;
}
FramelessManagerPrivate::FramelessManagerPrivate(FramelessManager *q) : QObject(q)
{
Q_ASSERT(q);
@ -101,8 +132,11 @@ void FramelessManagerPrivate::initializeIconFont()
}
inited = true;
initResource();
if (QFontDatabase::addApplicationFont(kIconFontFilePath) < 0) {
qWarning() << "Failed to load icon font:" << kIconFontFilePath;
const int id = QFontDatabase::addApplicationFont(iconFontFilePath());
if (id < 0) {
qWarning() << "Failed to load icon font:" << iconFontFilePath();
} else {
qDebug() << "Successfully registered icon font:" << QFontDatabase::applicationFontFamilies(id);
}
}
@ -110,7 +144,7 @@ QFont FramelessManagerPrivate::getIconFont()
{
static const QFont font = []() -> QFont {
QFont f = {};
f.setFamily(kIconFontName);
f.setFamily(iconFontFamilyName());
f.setPointSize(kIconFontPointSize);
return f;
}();

Binary file not shown.

Binary file not shown.

View File

@ -23,6 +23,7 @@
*/
#include "utils.h"
#include <QtCore/qdebug.h>
#include <QtGui/qwindow.h>
#include <QtGui/qscreen.h>
#include <QtGui/qguiapplication.h>
@ -35,12 +36,21 @@ FRAMELESSHELPER_BEGIN_NAMESPACE
using namespace Global;
FRAMELESSHELPER_STRING_CONSTANT2(ChromeWindowIcon, "\ue756")
FRAMELESSHELPER_STRING_CONSTANT2(ChromeHelpIcon, "\ue897")
FRAMELESSHELPER_STRING_CONSTANT2(ChromeMinimizeIcon, "\ue921")
FRAMELESSHELPER_STRING_CONSTANT2(ChromeMaximizeIcon, "\ue922")
FRAMELESSHELPER_STRING_CONSTANT2(ChromeRestoreIcon, "\ue923")
FRAMELESSHELPER_STRING_CONSTANT2(ChromeCloseIcon, "\ue8bb")
struct FONT_ICON
{
quint32 win = 0;
quint32 unix = 0;
};
static const QHash<int, FONT_ICON> g_fontIconsTable = {
{static_cast<int>(SystemButtonType::Unknown), {0x0000, 0x0000}},
{static_cast<int>(SystemButtonType::WindowIcon), {0xE756, 0xEB06}},
{static_cast<int>(SystemButtonType::Help), {0xE897, 0xEC04}},
{static_cast<int>(SystemButtonType::Minimize), {0xE921, 0xEAE0}},
{static_cast<int>(SystemButtonType::Maximize), {0xE922, 0xEADE}},
{static_cast<int>(SystemButtonType::Restore), {0xE923, 0xEAE2}},
{static_cast<int>(SystemButtonType::Close), {0xE8BB, 0xEADA}}
};
Qt::CursorShape Utils::calculateCursorShape(const QWindow *window, const QPoint &pos)
{
@ -113,24 +123,16 @@ Qt::Edges Utils::calculateWindowEdges(const QWindow *window, const QPoint &pos)
QString Utils::getSystemButtonIconCode(const SystemButtonType button)
{
switch (button) {
case SystemButtonType::Unknown:
Q_ASSERT(false);
break;
case SystemButtonType::WindowIcon:
return kChromeWindowIcon;
case SystemButtonType::Help:
return kChromeHelpIcon;
case SystemButtonType::Minimize:
return kChromeMinimizeIcon;
case SystemButtonType::Maximize:
return kChromeMaximizeIcon;
case SystemButtonType::Restore:
return kChromeRestoreIcon;
case SystemButtonType::Close:
return kChromeCloseIcon;
if (!g_fontIconsTable.contains(static_cast<int>(button))) {
qWarning() << "FIXME: Add FONT_ICON value for button" << button;
return {};
}
return {};
const FONT_ICON icon = g_fontIconsTable.value(static_cast<int>(button));
#ifdef Q_OS_WINDOWS
return QChar(icon.win);
#else
return QChar(icon.unix);
#endif
}
QWindow *Utils::findWindow(const WId windowId)

View File

@ -905,15 +905,16 @@ bool Utils::isHighContrastModeEnabled()
quint32 Utils::getPrimaryScreenDpi(const bool horizontal)
{
const HMONITOR monitor = []() -> HMONITOR {
const auto getPrimaryMonitor = []() -> HMONITOR {
const HWND window = ensureDummyWindow();
if (window) {
return MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY);
}
static constexpr const int kTaskBarSize = 100;
return MonitorFromPoint(POINT{kTaskBarSize, kTaskBarSize}, MONITOR_DEFAULTTOPRIMARY);
}();
};
if (API_SHCORE_AVAILABLE(GetDpiForMonitor)) {
const HMONITOR monitor = getPrimaryMonitor();
if (monitor) {
UINT dpiX = 0, dpiY = 0;
const HRESULT hr = API_CALL_FUNCTION(GetDpiForMonitor, monitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
@ -927,6 +928,7 @@ quint32 Utils::getPrimaryScreenDpi(const bool horizontal)
}
}
if (API_SHCORE_AVAILABLE(GetScaleFactorForMonitor)) {
const HMONITOR monitor = getPrimaryMonitor();
if (monitor) {
DEVICE_SCALE_FACTOR factor = DEVICE_SCALE_FACTOR_INVALID;
const HRESULT hr = API_CALL_FUNCTION(GetScaleFactorForMonitor, monitor, &factor);
@ -1020,34 +1022,6 @@ quint32 Utils::getWindowDpi(const WId windowId, const bool horizontal)
qWarning() << getSystemErrorMessage(kGetDpiForSystem);
}
}
if (API_SHCORE_AVAILABLE(GetDpiForMonitor)) {
const HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if (monitor) {
UINT dpiX = 0, dpiY = 0;
const HRESULT hr = API_CALL_FUNCTION(GetDpiForMonitor, monitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
if (SUCCEEDED(hr) && (dpiX > 0) && (dpiY > 0)) {
return (horizontal ? dpiX : dpiY);
} else {
qWarning() << __getSystemErrorMessage(kGetDpiForMonitor, hr);
}
} else {
qWarning() << getSystemErrorMessage(kMonitorFromWindow);
}
}
if (API_SHCORE_AVAILABLE(GetScaleFactorForMonitor)) {
const HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if (monitor) {
DEVICE_SCALE_FACTOR factor = DEVICE_SCALE_FACTOR_INVALID;
const HRESULT hr = API_CALL_FUNCTION(GetScaleFactorForMonitor, monitor, &factor);
if (SUCCEEDED(hr) && (factor != DEVICE_SCALE_FACTOR_INVALID)) {
return quint32(qRound(qreal(USER_DEFAULT_SCREEN_DPI) * qreal(factor) / qreal(100)));
} else {
qWarning() << __getSystemErrorMessage(kGetScaleFactorForMonitor, hr);
}
} else {
qWarning() << getSystemErrorMessage(kMonitorFromWindow);
}
}
return getPrimaryScreenDpi(horizontal);
}