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:
parent
4f8106a009
commit
b8fee2e732
|
@ -1,5 +1,7 @@
|
||||||
<RCC>
|
<RCC>
|
||||||
<qresource prefix="/org.wangwenx190.FramelessHelper">
|
<qresource prefix="/org.wangwenx190.FramelessHelper">
|
||||||
<file>resources/fonts/Segoe Fluent Icons.ttf</file>
|
<file>resources/fonts/Segoe Fluent Icons.ttf</file>
|
||||||
|
<file>resources/fonts/Segoe MDL2 Assets.ttf</file>
|
||||||
|
<file>resources/fonts/Micon.ttf</file>
|
||||||
</qresource>
|
</qresource>
|
||||||
</RCC>
|
</RCC>
|
||||||
|
|
|
@ -24,8 +24,8 @@
|
||||||
|
|
||||||
#include "framelessmanager.h"
|
#include "framelessmanager.h"
|
||||||
#include "framelessmanager_p.h"
|
#include "framelessmanager_p.h"
|
||||||
#include <QtCore/qmutex.h>
|
|
||||||
#include <QtCore/qdebug.h>
|
#include <QtCore/qdebug.h>
|
||||||
|
#include <QtCore/qmutex.h>
|
||||||
#include <QtGui/qscreen.h>
|
#include <QtGui/qscreen.h>
|
||||||
#include <QtGui/qwindow.h>
|
#include <QtGui/qwindow.h>
|
||||||
#include <QtGui/qfontdatabase.h>
|
#include <QtGui/qfontdatabase.h>
|
||||||
|
@ -59,10 +59,41 @@ Q_GLOBAL_STATIC(FramelessManagerHelper, g_helper)
|
||||||
|
|
||||||
Q_GLOBAL_STATIC(FramelessManager, g_manager)
|
Q_GLOBAL_STATIC(FramelessManager, g_manager)
|
||||||
|
|
||||||
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFilePath, ":/org.wangwenx190.FramelessHelper/resources/fonts/Segoe Fluent Icons.ttf")
|
FRAMELESSHELPER_STRING_CONSTANT2(IconFontResourcePrefix, ":/org.wangwenx190.FramelessHelper/resources/fonts/")
|
||||||
FRAMELESSHELPER_STRING_CONSTANT2(IconFontName, "Segoe Fluent Icons")
|
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;
|
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)
|
FramelessManagerPrivate::FramelessManagerPrivate(FramelessManager *q) : QObject(q)
|
||||||
{
|
{
|
||||||
Q_ASSERT(q);
|
Q_ASSERT(q);
|
||||||
|
@ -101,8 +132,11 @@ void FramelessManagerPrivate::initializeIconFont()
|
||||||
}
|
}
|
||||||
inited = true;
|
inited = true;
|
||||||
initResource();
|
initResource();
|
||||||
if (QFontDatabase::addApplicationFont(kIconFontFilePath) < 0) {
|
const int id = QFontDatabase::addApplicationFont(iconFontFilePath());
|
||||||
qWarning() << "Failed to load icon font:" << kIconFontFilePath;
|
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 {
|
static const QFont font = []() -> QFont {
|
||||||
QFont f = {};
|
QFont f = {};
|
||||||
f.setFamily(kIconFontName);
|
f.setFamily(iconFontFamilyName());
|
||||||
f.setPointSize(kIconFontPointSize);
|
f.setPointSize(kIconFontPointSize);
|
||||||
return f;
|
return f;
|
||||||
}();
|
}();
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -23,6 +23,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include <QtCore/qdebug.h>
|
||||||
#include <QtGui/qwindow.h>
|
#include <QtGui/qwindow.h>
|
||||||
#include <QtGui/qscreen.h>
|
#include <QtGui/qscreen.h>
|
||||||
#include <QtGui/qguiapplication.h>
|
#include <QtGui/qguiapplication.h>
|
||||||
|
@ -35,12 +36,21 @@ FRAMELESSHELPER_BEGIN_NAMESPACE
|
||||||
|
|
||||||
using namespace Global;
|
using namespace Global;
|
||||||
|
|
||||||
FRAMELESSHELPER_STRING_CONSTANT2(ChromeWindowIcon, "\ue756")
|
struct FONT_ICON
|
||||||
FRAMELESSHELPER_STRING_CONSTANT2(ChromeHelpIcon, "\ue897")
|
{
|
||||||
FRAMELESSHELPER_STRING_CONSTANT2(ChromeMinimizeIcon, "\ue921")
|
quint32 win = 0;
|
||||||
FRAMELESSHELPER_STRING_CONSTANT2(ChromeMaximizeIcon, "\ue922")
|
quint32 unix = 0;
|
||||||
FRAMELESSHELPER_STRING_CONSTANT2(ChromeRestoreIcon, "\ue923")
|
};
|
||||||
FRAMELESSHELPER_STRING_CONSTANT2(ChromeCloseIcon, "\ue8bb")
|
|
||||||
|
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)
|
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)
|
QString Utils::getSystemButtonIconCode(const SystemButtonType button)
|
||||||
{
|
{
|
||||||
switch (button) {
|
if (!g_fontIconsTable.contains(static_cast<int>(button))) {
|
||||||
case SystemButtonType::Unknown:
|
qWarning() << "FIXME: Add FONT_ICON value for button" << button;
|
||||||
Q_ASSERT(false);
|
return {};
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
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)
|
QWindow *Utils::findWindow(const WId windowId)
|
||||||
|
|
|
@ -905,15 +905,16 @@ bool Utils::isHighContrastModeEnabled()
|
||||||
|
|
||||||
quint32 Utils::getPrimaryScreenDpi(const bool horizontal)
|
quint32 Utils::getPrimaryScreenDpi(const bool horizontal)
|
||||||
{
|
{
|
||||||
const HMONITOR monitor = []() -> HMONITOR {
|
const auto getPrimaryMonitor = []() -> HMONITOR {
|
||||||
const HWND window = ensureDummyWindow();
|
const HWND window = ensureDummyWindow();
|
||||||
if (window) {
|
if (window) {
|
||||||
return MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY);
|
return MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY);
|
||||||
}
|
}
|
||||||
static constexpr const int kTaskBarSize = 100;
|
static constexpr const int kTaskBarSize = 100;
|
||||||
return MonitorFromPoint(POINT{kTaskBarSize, kTaskBarSize}, MONITOR_DEFAULTTOPRIMARY);
|
return MonitorFromPoint(POINT{kTaskBarSize, kTaskBarSize}, MONITOR_DEFAULTTOPRIMARY);
|
||||||
}();
|
};
|
||||||
if (API_SHCORE_AVAILABLE(GetDpiForMonitor)) {
|
if (API_SHCORE_AVAILABLE(GetDpiForMonitor)) {
|
||||||
|
const HMONITOR monitor = getPrimaryMonitor();
|
||||||
if (monitor) {
|
if (monitor) {
|
||||||
UINT dpiX = 0, dpiY = 0;
|
UINT dpiX = 0, dpiY = 0;
|
||||||
const HRESULT hr = API_CALL_FUNCTION(GetDpiForMonitor, monitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
|
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)) {
|
if (API_SHCORE_AVAILABLE(GetScaleFactorForMonitor)) {
|
||||||
|
const HMONITOR monitor = getPrimaryMonitor();
|
||||||
if (monitor) {
|
if (monitor) {
|
||||||
DEVICE_SCALE_FACTOR factor = DEVICE_SCALE_FACTOR_INVALID;
|
DEVICE_SCALE_FACTOR factor = DEVICE_SCALE_FACTOR_INVALID;
|
||||||
const HRESULT hr = API_CALL_FUNCTION(GetScaleFactorForMonitor, monitor, &factor);
|
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);
|
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);
|
return getPrimaryScreenDpi(horizontal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue