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>
This commit is contained in:
Yuhang Zhao 2022-09-13 17:38:10 +08:00
parent 119d10930b
commit 221f1c12aa
5 changed files with 43 additions and 5 deletions

View File

@ -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 normalColor READ normalColor WRITE setNormalColor NOTIFY normalColorChanged FINAL)
Q_PROPERTY(QColor activeForegroundColor READ activeForegroundColor WRITE setActiveForegroundColor NOTIFY activeForegroundColorChanged 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(QColor inactiveForegroundColor READ inactiveForegroundColor WRITE setInactiveForegroundColor NOTIFY inactiveForegroundColorChanged FINAL)
Q_PROPERTY(qreal iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged FINAL)
public: public:
explicit QuickStandardSystemButton(QQuickItem *parent = nullptr); explicit QuickStandardSystemButton(QQuickItem *parent = nullptr);
@ -62,6 +63,7 @@ public:
Q_NODISCARD QColor pressColor() const; Q_NODISCARD QColor pressColor() const;
Q_NODISCARD QColor activeForegroundColor() const; Q_NODISCARD QColor activeForegroundColor() const;
Q_NODISCARD QColor inactiveForegroundColor() const; Q_NODISCARD QColor inactiveForegroundColor() const;
Q_NODISCARD qreal iconSize() const;
public Q_SLOTS: public Q_SLOTS:
void updateColor(); void updateColor();
@ -72,6 +74,7 @@ public Q_SLOTS:
void setPressColor(const QColor &value); void setPressColor(const QColor &value);
void setActiveForegroundColor(const QColor &value); void setActiveForegroundColor(const QColor &value);
void setInactiveForegroundColor(const QColor &value); void setInactiveForegroundColor(const QColor &value);
void setIconSize(const qreal value);
private: private:
void initialize(); void initialize();
@ -84,6 +87,7 @@ Q_SIGNALS:
void pressColorChanged(); void pressColorChanged();
void activeForegroundColorChanged(); void activeForegroundColorChanged();
void inactiveForegroundColorChanged(); void inactiveForegroundColorChanged();
void iconSizeChanged();
private: private:
QScopedPointer<QQuickText> m_contentItem; QScopedPointer<QQuickText> m_contentItem;

View File

@ -98,6 +98,9 @@ void FramelessHelperQt::addWindow(const SystemParameters &params)
} }
window->installEventFilter(data.eventFilter); window->installEventFilter(data.eventFilter);
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
# if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
window->setProperty("_q_mac_wantsLayer", 1);
# endif
Utils::setSystemTitleBarVisible(windowId, false); Utils::setSystemTitleBarVisible(windowId, false);
#endif #endif
FramelessHelper::Core::setApplicationOSThemeAware(); FramelessHelper::Core::setApplicationOSThemeAware();

View File

@ -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}) add_library(${PROJECT_NAME}::${SUB_MOD_NAME} ALIAS ${SUB_PROJ_NAME})
set(__import_base_dir ${PROJECT_BINARY_DIR}/qml) set(__import_base_dir ${PROJECT_BINARY_DIR}/qml)
if(${FRAMELESSHELPER_IMPORT_DIR}) if(DEFINED FRAMELESSHELPER_IMPORT_DIR)
set(__import_base_dir ${FRAMELESSHELPER_IMPORT_DIR}) set(__import_base_dir ${FRAMELESSHELPER_IMPORT_DIR})
endif() endif()
set(__import_uri org/wangwenx190/${PROJECT_NAME}) set(__import_uri org/wangwenx190/${PROJECT_NAME})

View File

@ -90,6 +90,23 @@ QColor QuickStandardSystemButton::inactiveForegroundColor() const
return m_inactiveForegroundColor; 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) void QuickStandardSystemButton::setButtonType(const QuickGlobal::SystemButtonType type)
{ {
Q_ASSERT(type != QuickGlobal::SystemButtonType::Unknown); Q_ASSERT(type != QuickGlobal::SystemButtonType::Unknown);
@ -189,6 +206,21 @@ void QuickStandardSystemButton::setInactiveForegroundColor(const QColor &value)
Q_EMIT inactiveForegroundColorChanged(); 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() void QuickStandardSystemButton::updateColor()
{ {
const bool hover = isHovered(); const bool hover = isHovered();

View File

@ -39,8 +39,6 @@ Q_LOGGING_CATEGORY(lcStandardSystemButton, "wangwenx190.framelesshelper.widgets.
using namespace Global; using namespace Global;
static constexpr const QRect g_buttonRect = {QPoint(0, 0), kDefaultSystemButtonSize};
StandardSystemButtonPrivate::StandardSystemButtonPrivate(StandardSystemButton *q) : QObject(q) StandardSystemButtonPrivate::StandardSystemButtonPrivate(StandardSystemButton *q) : QObject(q)
{ {
Q_ASSERT(q); Q_ASSERT(q);
@ -331,8 +329,9 @@ void StandardSystemButtonPrivate::paintEventHandler(QPaintEvent *event)
} }
return {}; return {};
}(); }();
const QRect buttonRect = {QPoint(0, 0), q->size()};
if (backgroundColor.isValid()) { if (backgroundColor.isValid()) {
painter.fillRect(g_buttonRect, backgroundColor); painter.fillRect(buttonRect, backgroundColor);
} }
if (!m_code.isEmpty()) { if (!m_code.isEmpty()) {
painter.setPen([this]() -> QColor { painter.setPen([this]() -> QColor {
@ -345,7 +344,7 @@ void StandardSystemButtonPrivate::paintEventHandler(QPaintEvent *event)
return kDefaultBlackColor; return kDefaultBlackColor;
}()); }());
painter.setFont(FramelessManagerPrivate::getIconFont()); painter.setFont(FramelessManagerPrivate::getIconFont());
painter.drawText(g_buttonRect, Qt::AlignCenter, m_code); painter.drawText(buttonRect, Qt::AlignCenter, m_code);
} }
painter.restore(); painter.restore();
} }