From 221f1c12aac48bf010e09f9dbfae360f59bf32b5 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Tue, 13 Sep 2022 17:38:10 +0800 Subject: [PATCH] fix button size can't be changed widget: fix the bug quick: add the interface to change size Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- .../private/quickstandardsystembutton_p.h | 4 +++ src/core/framelesshelper_qt.cpp | 3 ++ src/quick/CMakeLists.txt | 2 +- src/quick/quickstandardsystembutton.cpp | 32 +++++++++++++++++++ src/widgets/standardsystembutton.cpp | 7 ++-- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/include/FramelessHelper/Quick/private/quickstandardsystembutton_p.h b/include/FramelessHelper/Quick/private/quickstandardsystembutton_p.h index e4ad27b..deb0e2d 100644 --- a/include/FramelessHelper/Quick/private/quickstandardsystembutton_p.h +++ b/include/FramelessHelper/Quick/private/quickstandardsystembutton_p.h @@ -49,6 +49,7 @@ class FRAMELESSHELPER_QUICK_API QuickStandardSystemButton : public QQuickButton Q_PROPERTY(QColor normalColor READ normalColor WRITE setNormalColor NOTIFY normalColorChanged FINAL) Q_PROPERTY(QColor activeForegroundColor READ activeForegroundColor WRITE setActiveForegroundColor NOTIFY activeForegroundColorChanged FINAL) Q_PROPERTY(QColor inactiveForegroundColor READ inactiveForegroundColor WRITE setInactiveForegroundColor NOTIFY inactiveForegroundColorChanged FINAL) + Q_PROPERTY(qreal iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged FINAL) public: explicit QuickStandardSystemButton(QQuickItem *parent = nullptr); @@ -62,6 +63,7 @@ public: Q_NODISCARD QColor pressColor() const; Q_NODISCARD QColor activeForegroundColor() const; Q_NODISCARD QColor inactiveForegroundColor() const; + Q_NODISCARD qreal iconSize() const; public Q_SLOTS: void updateColor(); @@ -72,6 +74,7 @@ public Q_SLOTS: void setPressColor(const QColor &value); void setActiveForegroundColor(const QColor &value); void setInactiveForegroundColor(const QColor &value); + void setIconSize(const qreal value); private: void initialize(); @@ -84,6 +87,7 @@ Q_SIGNALS: void pressColorChanged(); void activeForegroundColorChanged(); void inactiveForegroundColorChanged(); + void iconSizeChanged(); private: QScopedPointer m_contentItem; diff --git a/src/core/framelesshelper_qt.cpp b/src/core/framelesshelper_qt.cpp index 5885773..f6a5238 100644 --- a/src/core/framelesshelper_qt.cpp +++ b/src/core/framelesshelper_qt.cpp @@ -98,6 +98,9 @@ void FramelessHelperQt::addWindow(const SystemParameters ¶ms) } window->installEventFilter(data.eventFilter); #ifdef Q_OS_MACOS +# if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) + window->setProperty("_q_mac_wantsLayer", 1); +# endif Utils::setSystemTitleBarVisible(windowId, false); #endif FramelessHelper::Core::setApplicationOSThemeAware(); diff --git a/src/quick/CMakeLists.txt b/src/quick/CMakeLists.txt index 06d2115..f09a845 100644 --- a/src/quick/CMakeLists.txt +++ b/src/quick/CMakeLists.txt @@ -95,7 +95,7 @@ add_library(${PROJECT_NAME}::${SUB_PROJ_NAME} ALIAS ${SUB_PROJ_NAME}) add_library(${PROJECT_NAME}::${SUB_MOD_NAME} ALIAS ${SUB_PROJ_NAME}) set(__import_base_dir ${PROJECT_BINARY_DIR}/qml) -if(${FRAMELESSHELPER_IMPORT_DIR}) +if(DEFINED FRAMELESSHELPER_IMPORT_DIR) set(__import_base_dir ${FRAMELESSHELPER_IMPORT_DIR}) endif() set(__import_uri org/wangwenx190/${PROJECT_NAME}) diff --git a/src/quick/quickstandardsystembutton.cpp b/src/quick/quickstandardsystembutton.cpp index d71daff..3752d44 100644 --- a/src/quick/quickstandardsystembutton.cpp +++ b/src/quick/quickstandardsystembutton.cpp @@ -90,6 +90,23 @@ QColor QuickStandardSystemButton::inactiveForegroundColor() const return m_inactiveForegroundColor; } +qreal QuickStandardSystemButton::iconSize() const +{ + if (m_contentItem.isNull()) { + return -1; + } + const QFont font = m_contentItem->font(); + const qreal point = font.pointSizeF(); + if (point > 0) { + return point; + } + const int pixel = font.pixelSize(); + if (pixel > 0) { + return pixel; + } + return -1; +} + void QuickStandardSystemButton::setButtonType(const QuickGlobal::SystemButtonType type) { Q_ASSERT(type != QuickGlobal::SystemButtonType::Unknown); @@ -189,6 +206,21 @@ void QuickStandardSystemButton::setInactiveForegroundColor(const QColor &value) Q_EMIT inactiveForegroundColorChanged(); } +void QuickStandardSystemButton::setIconSize(const qreal value) +{ + Q_ASSERT(value > 0); + if (qFuzzyIsNull(value) || (value < 0)) { + return; + } + if (qFuzzyCompare(iconSize(), value)) { + return; + } + QFont font = m_contentItem->font(); + font.setPointSizeF(value); + m_contentItem->setFont(font); + Q_EMIT iconSizeChanged(); +} + void QuickStandardSystemButton::updateColor() { const bool hover = isHovered(); diff --git a/src/widgets/standardsystembutton.cpp b/src/widgets/standardsystembutton.cpp index 5ef0951..618ed45 100644 --- a/src/widgets/standardsystembutton.cpp +++ b/src/widgets/standardsystembutton.cpp @@ -39,8 +39,6 @@ Q_LOGGING_CATEGORY(lcStandardSystemButton, "wangwenx190.framelesshelper.widgets. using namespace Global; -static constexpr const QRect g_buttonRect = {QPoint(0, 0), kDefaultSystemButtonSize}; - StandardSystemButtonPrivate::StandardSystemButtonPrivate(StandardSystemButton *q) : QObject(q) { Q_ASSERT(q); @@ -331,8 +329,9 @@ void StandardSystemButtonPrivate::paintEventHandler(QPaintEvent *event) } return {}; }(); + const QRect buttonRect = {QPoint(0, 0), q->size()}; if (backgroundColor.isValid()) { - painter.fillRect(g_buttonRect, backgroundColor); + painter.fillRect(buttonRect, backgroundColor); } if (!m_code.isEmpty()) { painter.setPen([this]() -> QColor { @@ -345,7 +344,7 @@ void StandardSystemButtonPrivate::paintEventHandler(QPaintEvent *event) return kDefaultBlackColor; }()); painter.setFont(FramelessManagerPrivate::getIconFont()); - painter.drawText(g_buttonRect, Qt::AlignCenter, m_code); + painter.drawText(buttonRect, Qt::AlignCenter, m_code); } painter.restore(); }