Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
standardsystembutton: use icon font instead of svg images
Browse files Browse the repository at this point in the history
Signed-off-by: Yuhang Zhao <[email protected]>
  • Loading branch information
wangwenx190 committed Jun 24, 2022
1 parent bfc7b27 commit 888f9d0
Show file tree
Hide file tree
Showing 29 changed files with 268 additions and 789 deletions.
9 changes: 6 additions & 3 deletions include/FramelessHelper/Core/framelesshelpercore_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Q_NAMESPACE_EXPORT(FRAMELESSHELPER_CORE_API)
[[maybe_unused]] static constexpr const int kDefaultTitleBarFontPointSize = 11;
[[maybe_unused]] static constexpr const int kDefaultTitleBarContentsMargin = 10;
[[maybe_unused]] static constexpr const int kDefaultWindowIconSize = 16;
[[maybe_unused]] static constexpr const QSize kDefaultSystemButtonSize = {int(qRound(qreal(kDefaultTitleBarHeight) * 1.5)), kDefaultTitleBarHeight};
[[maybe_unused]] static constexpr const QSize kDefaultSystemButtonSize = {qRound(qreal(kDefaultTitleBarHeight) * 1.5), kDefaultTitleBarHeight};
[[maybe_unused]] static constexpr const QSize kDefaultSystemButtonIconSize = {kDefaultWindowIconSize, kDefaultWindowIconSize};
[[maybe_unused]] static constexpr const QSize kDefaultWindowSize = {160, 160}; // Value taken from QPA.

Expand Down Expand Up @@ -306,7 +306,8 @@ enum class WindowsVersion
_10_20H2 = 21,
_10_21H1 = 22,
_10_21H2 = 23,
_11_21H2 = 24
_11_21H2 = 24,
_11_22H2 = 25
};
Q_ENUM_NS(WindowsVersion)

Expand Down Expand Up @@ -490,14 +491,16 @@ struct SystemParameters
{10, 0, 19043}, // Windows 10 Version 21H1 (May 2021 Update) (21H1)
{10, 0, 19044}, // Windows 10 Version 21H2 (November 2021 Update) (21H2)
{10, 0, 22000}, // Windows 11 Version 21H2 (21H2)
{10, 0, 22621}, // Windows 11 Version 22H2 (22H2)
};
static_assert(std::size(WindowsVersions) == (static_cast<int>(WindowsVersion::_11_21H2) + 1));
static_assert(std::size(WindowsVersions) == (static_cast<int>(WindowsVersion::_11_22H2) + 1));

} // namespace Global

namespace FramelessHelper::Core
{
FRAMELESSHELPER_CORE_API void initialize();
FRAMELESSHELPER_CORE_API void uninitialize();
[[nodiscard]] FRAMELESSHELPER_CORE_API int version();
} // namespace FramelessHelper::Core

Expand Down
3 changes: 3 additions & 0 deletions include/FramelessHelper/Core/private/framelessmanager_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class FRAMELESSHELPER_CORE_API FramelessManagerPrivate : public QObject
Q_NODISCARD static FramelessManagerPrivate *get(FramelessManager *pub);
Q_NODISCARD static const FramelessManagerPrivate *get(const FramelessManager *pub);

static void initializeIconFont();
Q_NODISCARD static QFont getIconFont();

Q_NODISCARD Global::SystemTheme systemTheme() const;
Q_NODISCARD QColor systemAccentColor() const;

Expand Down
5 changes: 1 addition & 4 deletions include/FramelessHelper/Core/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ namespace Utils
const QPoint &pos);
FRAMELESSHELPER_CORE_API void startSystemMove(QWindow *window, const QPoint &globalPos);
FRAMELESSHELPER_CORE_API void startSystemResize(QWindow *window, const Qt::Edges edges, const QPoint &globalPos);
[[nodiscard]] FRAMELESSHELPER_CORE_API QVariant
getSystemButtonIconResource(const Global::SystemButtonType button,
const Global::SystemTheme theme,
const Global::ResourceType type);
[[nodiscard]] FRAMELESSHELPER_CORE_API QString getSystemButtonIconCode(const Global::SystemButtonType button);
[[nodiscard]] FRAMELESSHELPER_CORE_API QWindow *findWindow(const WId windowId);
FRAMELESSHELPER_CORE_API void moveWindowToDesktopCenter(
const Global::GetWindowScreenCallback &getWindowScreen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <QtQuickTemplates2/private/qquickbutton_p.h>

QT_BEGIN_NAMESPACE
class QQuickImage;
class QQuickText;
class QQuickRectangle;
QT_END_NAMESPACE

Expand All @@ -51,20 +51,19 @@ class FRAMELESSHELPER_QUICK_API QuickStandardSystemButton : public QQuickButton
public Q_SLOTS:
void updateForeground();
void updateBackground();
void setInactive(const bool value);
void setButtonType(const QuickGlobal::SystemButtonType type);

protected:
void itemChange(const ItemChange change, const ItemChangeData &value) override;

private:
void initialize();
void checkInactive();

private:
QScopedPointer<QQuickImage> m_contentItem;
QScopedPointer<QQuickText> m_contentItem;
QScopedPointer<QQuickRectangle> m_backgroundItem;
bool m_forceLightTheme = false;
bool m_shouldCheck = false;
bool m_checkFlag = false;
QuickGlobal::SystemButtonType m_buttonType = QuickGlobal::SystemButtonType::Unknown;
QMetaObject::Connection m_windowActiveConnection = {};
};

FRAMELESSHELPER_END_NAMESPACE
Expand Down
26 changes: 10 additions & 16 deletions include/FramelessHelper/Widgets/private/standardsystembutton_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#pragma once

#include <QtCore/qobject.h>
#include <QtGui/qpixmap.h>
#include <QtCore/qpointer.h>
#include "framelesshelperwidgets_global.h"

QT_BEGIN_NAMESPACE
Expand All @@ -50,50 +50,44 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButtonPrivate : public QObject
Q_NODISCARD static StandardSystemButtonPrivate *get(StandardSystemButton *pub);
Q_NODISCARD static const StandardSystemButtonPrivate *get(const StandardSystemButton *pub);

void refreshButtonTheme(const bool force);
Q_NODISCARD QString getIconCode() const;
void setIconCode(const QString &value);

Q_NODISCARD Global::SystemButtonType getButtonType() const;
void setButtonType(const Global::SystemButtonType type);

void setIcon(const QIcon &value, const bool reverse);
void setPixmap(const QPixmap &value, const bool reverse);
void setImage(const QImage &value, const bool reverse);

Q_NODISCARD QSize getRecommendedButtonSize() const;

Q_NODISCARD bool isHovered() const;
Q_NODISCARD bool isPressed() const;
Q_NODISCARD QColor getHoverColor() const;
Q_NODISCARD QColor getPressColor() const;
Q_NODISCARD QColor getIconColor() const;

void setHovered(const bool value);
void setPressed(const bool value);
void setHoverColor(const QColor &value);
void setPressColor(const QColor &value);
void setIconColor(const QColor &value);

void updateBackgroundColor();

void enterEventHandler(QT_ENTER_EVENT_TYPE *event);
void leaveEventHandler(QEvent *event);
void paintEventHandler(QPaintEvent *event);

void setInactive(const bool value);

private:
void initialize();
void checkInactive();

private:
StandardSystemButton *q_ptr = nullptr;
Global::SystemTheme m_buttonTheme = Global::SystemTheme::Unknown;
QPointer<StandardSystemButton> q_ptr = nullptr;
Global::SystemButtonType m_buttonType = Global::SystemButtonType::Unknown;
QPixmap m_icon = {};
QPixmap m_reversedIcon = {};
QString m_iconCode = {};
QColor m_hoverColor = {};
QColor m_pressColor = {};
QColor m_iconColor = {};
bool m_hovered = false;
bool m_pressed = false;
bool m_forceLightTheme = false;
bool m_shouldCheck = false;
bool m_checkFlag = false;
};

FRAMELESSHELPER_END_NAMESPACE
9 changes: 8 additions & 1 deletion include/FramelessHelper/Widgets/standardsystembutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButton : public QAbstractButton
Q_DECLARE_PRIVATE(StandardSystemButton)
Q_DISABLE_COPY_MOVE(StandardSystemButton)
Q_PROPERTY(Global::SystemButtonType buttonType READ buttonType WRITE setButtonType NOTIFY buttonTypeChanged FINAL)
Q_PROPERTY(QString iconCode READ iconCode WRITE setIconCode NOTIFY iconCodeChanged FINAL)
Q_PROPERTY(bool hovered READ isHovered WRITE setHovered NOTIFY hoveredChanged FINAL)
Q_PROPERTY(bool pressed READ isPressed WRITE setPressed NOTIFY pressedChanged FINAL)
Q_PROPERTY(QColor hoverColor READ hoverColor WRITE setHoverColor NOTIFY hoverColorChanged FINAL)
Q_PROPERTY(QColor pressColor READ pressColor WRITE setPressColor NOTIFY pressColorChanged FINAL)
Q_PROPERTY(QColor iconColor READ iconColor WRITE setIconColor NOTIFY iconColorChanged FINAL)

public:
explicit StandardSystemButton(QWidget *parent = nullptr);
Expand All @@ -49,18 +51,21 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButton : public QAbstractButton

Q_NODISCARD QSize sizeHint() const override;
Q_NODISCARD Global::SystemButtonType buttonType();
Q_NODISCARD QString iconCode() const;
Q_NODISCARD bool isHovered() const;
Q_NODISCARD bool isPressed() const;
Q_NODISCARD QColor hoverColor() const;
Q_NODISCARD QColor pressColor() const;
Q_NODISCARD QColor iconColor() const;

public Q_SLOTS:
void setIcon(const QIcon &icon);
void setButtonType(const Global::SystemButtonType value);
void setIconCode(const QString &code);
void setHovered(const bool value);
void setPressed(const bool value);
void setHoverColor(const QColor &value);
void setPressColor(const QColor &value);
void setIconColor(const QColor &value);

protected:
void enterEvent(QT_ENTER_EVENT_TYPE *event) override;
Expand All @@ -69,10 +74,12 @@ public Q_SLOTS:

Q_SIGNALS:
void buttonTypeChanged();
void iconCodeChanged();
void hoveredChanged();
void pressedChanged();
void hoverColorChanged();
void pressColorChanged();
void iconColorChanged();

private:
QScopedPointer<StandardSystemButtonPrivate> d_ptr;
Expand Down
2 changes: 1 addition & 1 deletion src/core/framelesshelper_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
// Make the border a little wider to let the user easy to resize on corners.
const qreal scaleFactor = ((isTop || isBottom) ? 2.0 : 1.0);
const int frameSizeX = Utils::getResizeBorderThickness(windowId, true, true);
const auto scaledFrameSizeX = static_cast<int>(qRound(qreal(frameSizeX) * scaleFactor));
const int scaledFrameSizeX = qRound(qreal(frameSizeX) * scaleFactor);
const bool isLeft = (nativeLocalPos.x < scaledFrameSizeX);
const bool isRight = (nativeLocalPos.x >= (width - scaledFrameSizeX));
if (isTop) {
Expand Down
9 changes: 1 addition & 8 deletions src/core/framelesshelpercore.qrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<RCC>
<qresource prefix="/org.wangwenx190.FramelessHelper">
<file>images/dark/chrome-close.svg</file>
<file>images/dark/chrome-maximize.svg</file>
<file>images/dark/chrome-minimize.svg</file>
<file>images/dark/chrome-restore.svg</file>
<file>images/light/chrome-close.svg</file>
<file>images/light/chrome-maximize.svg</file>
<file>images/light/chrome-minimize.svg</file>
<file>images/light/chrome-restore.svg</file>
<file>resources/fonts/Segoe Fluent Icons.ttf</file>
</qresource>
</RCC>
45 changes: 44 additions & 1 deletion src/core/framelessmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@
#include <QtGui/qguiapplication.h>
#include <QtGui/qscreen.h>
#include <QtGui/qwindow.h>
#include <QtGui/qfontdatabase.h>
#include "framelesshelper_qt.h"
#include "framelessconfig_p.h"
#include "utils.h"
#ifdef Q_OS_WINDOWS
# include "framelesshelper_win.h"
#endif

// The "Q_INIT_RESOURCE()" macro can't be used within a namespace,
// so we wrap it into a separate function outside of the namespace and
// then call it instead inside the namespace, that's also the recommended
// workaround provided by Qt's official documentation.
static inline void initResource()
{
Q_INIT_RESOURCE(framelesshelpercore);
}

FRAMELESSHELPER_BEGIN_NAMESPACE

using namespace Global;
Expand All @@ -59,6 +69,10 @@ static constexpr const char MAC_LAYER_ENV_VAR[] = "QT_MAC_WANTS_LAYER";
FRAMELESSHELPER_BYTEARRAY_CONSTANT2(ValueOne, "1")
#endif

FRAMELESSHELPER_STRING_CONSTANT2(IconFontFilePath, ":/org.wangwenx190.FramelessHelper/resources/fonts/Segoe Fluent Icons.ttf")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontName, "Segoe Fluent Icons")
static constexpr const int kIconFontPointSize = 7;

FramelessManagerPrivate::FramelessManagerPrivate(FramelessManager *q) : QObject(q)
{
Q_ASSERT(q);
Expand Down Expand Up @@ -89,6 +103,30 @@ const FramelessManagerPrivate *FramelessManagerPrivate::get(const FramelessManag
return pub->d_func();
}

void FramelessManagerPrivate::initializeIconFont()
{
static bool inited = false;
if (inited) {
return;
}
inited = true;
initResource();
if (QFontDatabase::addApplicationFont(kIconFontFilePath) < 0) {
qWarning() << "Failed to load icon font:" << kIconFontFilePath;
}
}

QFont FramelessManagerPrivate::getIconFont()
{
static const QFont font = []() -> QFont {
QFont f = {};
f.setFamily(kIconFontName);
f.setPointSize(kIconFontPointSize);
return f;
}();
return font;
}

SystemTheme FramelessManagerPrivate::systemTheme() const
{
return m_systemTheme;
Expand Down Expand Up @@ -271,7 +309,7 @@ void FramelessHelper::Core::initialize()
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
// Non-integer scale factors will cause Qt have some painting defects
// for both Qt Widgets and Qt Quick applications, and it's still not
// totally fixed till now (Qt 6.4), so we round the scale factors to
// totally fixed till now (Qt 6.5), so we round the scale factors to
// get a better looking. Non-integer scale factors will also cause
// flicker and jitter during window resizing.
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round);
Expand All @@ -289,6 +327,11 @@ void FramelessHelper::Core::initialize()
qRegisterMetaType<SystemParameters>();
}

void FramelessHelper::Core::uninitialize()
{
// Currently nothing to do here.
}

int FramelessHelper::Core::version()
{
return FRAMELESSHELPER_VERSION;
Expand Down
Loading

0 comments on commit 888f9d0

Please sign in to comment.