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

Commit

Permalink
general: fix the system button background color in various cases
Browse files Browse the repository at this point in the history
Signed-off-by: Yuhang Zhao <[email protected]>
  • Loading branch information
wangwenx190 committed May 12, 2022
1 parent 8cb24c6 commit 5c3b8b7
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,22 @@ class FRAMELESSHELPER_QUICK_API QuickStandardCloseButton : public QQuickButton
explicit QuickStandardCloseButton(QQuickItem *parent = nullptr);
~QuickStandardCloseButton() override;

private Q_SLOTS:
public Q_SLOTS:
void updateForeground();
void updateBackground();
void setInactive(const bool value);

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

private:
QScopedPointer<QQuickItem> m_contentItem;
QScopedPointer<QQuickImage> m_image;
QScopedPointer<QQuickRectangle> m_backgroundItem;
bool m_forceLightTheme = false;
bool m_shouldCheck = false;
bool m_checkFlag = false;
};

FRAMELESSHELPER_END_NAMESPACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,26 @@ class FRAMELESSHELPER_QUICK_API QuickStandardMaximizeButton : public QQuickButto
Q_NODISCARD bool isMaximized() const;
void setMaximized(const bool max);

private Q_SLOTS:
public Q_SLOTS:
void updateForeground();
void updateBackground();
void setInactive(const bool value);

Q_SIGNALS:
void maximizedChanged();

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

private:
bool m_max = false;
QScopedPointer<QQuickItem> m_contentItem;
QScopedPointer<QQuickImage> m_image;
QScopedPointer<QQuickRectangle> m_backgroundItem;
bool m_forceLightTheme = false;
bool m_shouldCheck = false;
bool m_checkFlag = false;
};

FRAMELESSHELPER_END_NAMESPACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,22 @@ class FRAMELESSHELPER_QUICK_API QuickStandardMinimizeButton : public QQuickButto
explicit QuickStandardMinimizeButton(QQuickItem *parent = nullptr);
~QuickStandardMinimizeButton() override;

private Q_SLOTS:
public Q_SLOTS:
void updateForeground();
void updateBackground();
void setInactive(const bool value);

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

private:
QScopedPointer<QQuickItem> m_contentItem;
QScopedPointer<QQuickImage> m_image;
QScopedPointer<QQuickRectangle> m_backgroundItem;
bool m_forceLightTheme = false;
bool m_shouldCheck = false;
bool m_checkFlag = false;
};

FRAMELESSHELPER_END_NAMESPACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButtonPrivate : public QObject
void leaveEventHandler(QEvent *event);
void paintEventHandler(QPaintEvent *event);

void setInactive(const bool value);

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

private:
StandardSystemButton *q_ptr = nullptr;
Expand All @@ -88,6 +91,9 @@ class FRAMELESSHELPER_WIDGETS_API StandardSystemButtonPrivate : public QObject
QColor m_pressColor = {};
bool m_hovered = false;
bool m_pressed = false;
bool m_forceLightTheme = false;
bool m_shouldCheck = false;
bool m_checkFlag = false;
};

FRAMELESSHELPER_END_NAMESPACE
24 changes: 23 additions & 1 deletion src/quick/quickstandardclosebutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ QuickStandardCloseButton::~QuickStandardCloseButton() = default;

void QuickStandardCloseButton::updateForeground()
{
const bool dark = (Utils::shouldAppsUseDarkMode() || Utils::isTitleBarColorized());
const bool dark = ((Utils::shouldAppsUseDarkMode() || Utils::isTitleBarColorized()) && !m_forceLightTheme);
const auto url = QUrl((dark || isHovered() || isPressed()) ? kDarkUrl : kLightUrl);
initResource();
m_image->setSource(url);
Expand All @@ -65,9 +65,31 @@ void QuickStandardCloseButton::updateBackground()
const bool press = isPressed();
m_backgroundItem->setColor(Utils::calculateSystemButtonBackgroundColor(button, (press ? ButtonState::Pressed : ButtonState::Hovered)));
m_backgroundItem->setVisible(hover || press);
checkInactive();
qobject_cast<QQuickToolTipAttached *>(qmlAttachedPropertiesObject<QQuickToolTip>(this))->setVisible(hover);
}

void QuickStandardCloseButton::setInactive(const bool value)
{
const bool force = (value && Utils::isTitleBarColorized() && !Utils::shouldAppsUseDarkMode());
if (m_forceLightTheme == force) {
return;
}
m_forceLightTheme = force;
m_shouldCheck = m_forceLightTheme;
updateForeground();
}

void QuickStandardCloseButton::checkInactive()
{
if (!m_shouldCheck) {
return;
}
m_forceLightTheme = m_checkFlag;
m_checkFlag = !m_checkFlag;
updateForeground();
}

void QuickStandardCloseButton::initialize()
{
setImplicitWidth(kDefaultSystemButtonSize.width());
Expand Down
24 changes: 23 additions & 1 deletion src/quick/quickstandardmaximizebutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void QuickStandardMaximizeButton::setMaximized(const bool max)

void QuickStandardMaximizeButton::updateForeground()
{
const bool dark = (Utils::shouldAppsUseDarkMode() || Utils::isTitleBarColorized());
const bool dark = ((Utils::shouldAppsUseDarkMode() || Utils::isTitleBarColorized()) && !m_forceLightTheme);
const auto url = QUrl(dark ? (m_max ? kDarkRestoreUrl : kDarkMaxUrl) : (m_max ? kLightRestoreUrl : kLightMaxUrl));
initResource();
m_image->setSource(url);
Expand All @@ -81,9 +81,31 @@ void QuickStandardMaximizeButton::updateBackground()
const bool press = isPressed();
m_backgroundItem->setColor(Utils::calculateSystemButtonBackgroundColor(button, (press ? ButtonState::Pressed : ButtonState::Hovered)));
m_backgroundItem->setVisible(hover || press);
checkInactive();
qobject_cast<QQuickToolTipAttached *>(qmlAttachedPropertiesObject<QQuickToolTip>(this))->setVisible(hover);
}

void QuickStandardMaximizeButton::setInactive(const bool value)
{
const bool force = (value && Utils::isTitleBarColorized() && !Utils::shouldAppsUseDarkMode());
if (m_forceLightTheme == force) {
return;
}
m_forceLightTheme = force;
m_shouldCheck = m_forceLightTheme;
updateForeground();
}

void QuickStandardMaximizeButton::checkInactive()
{
if (!m_shouldCheck) {
return;
}
m_forceLightTheme = m_checkFlag;
m_checkFlag = !m_checkFlag;
updateForeground();
}

void QuickStandardMaximizeButton::initialize()
{
setImplicitWidth(kDefaultSystemButtonSize.width());
Expand Down
24 changes: 23 additions & 1 deletion src/quick/quickstandardminimizebutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ QuickStandardMinimizeButton::~QuickStandardMinimizeButton() = default;

void QuickStandardMinimizeButton::updateForeground()
{
const bool dark = (Utils::shouldAppsUseDarkMode() || Utils::isTitleBarColorized());
const bool dark = ((Utils::shouldAppsUseDarkMode() || Utils::isTitleBarColorized()) && !m_forceLightTheme);
const auto url = QUrl(dark ? kDarkUrl : kLightUrl);
initResource();
m_image->setSource(url);
Expand All @@ -65,9 +65,31 @@ void QuickStandardMinimizeButton::updateBackground()
const bool press = isPressed();
m_backgroundItem->setColor(Utils::calculateSystemButtonBackgroundColor(button, (press ? ButtonState::Pressed : ButtonState::Hovered)));
m_backgroundItem->setVisible(hover || press);
checkInactive();
qobject_cast<QQuickToolTipAttached *>(qmlAttachedPropertiesObject<QQuickToolTip>(this))->setVisible(hover);
}

void QuickStandardMinimizeButton::setInactive(const bool value)
{
const bool force = (value && Utils::isTitleBarColorized() && !Utils::shouldAppsUseDarkMode());
if (m_forceLightTheme == force) {
return;
}
m_forceLightTheme = force;
m_shouldCheck = m_forceLightTheme;
updateForeground();
}

void QuickStandardMinimizeButton::checkInactive()
{
if (!m_shouldCheck) {
return;
}
m_forceLightTheme = m_checkFlag;
m_checkFlag = !m_checkFlag;
updateForeground();
}

void QuickStandardMinimizeButton::initialize()
{
setImplicitWidth(kDefaultSystemButtonSize.width());
Expand Down
6 changes: 5 additions & 1 deletion src/quick/quickstandardtitlebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ void QuickStandardTitleBar::updateTitleBarColor()
if (!w) {
return;
}
const bool active = w->isActive();
QColor backgroundColor = {};
QColor foregroundColor = {};
if (w->isActive()) {
if (active) {
if (Utils::isTitleBarColorized()) {
#ifdef Q_OS_WINDOWS
backgroundColor = Utils::getDwmColorizationColor();
Expand Down Expand Up @@ -188,6 +189,9 @@ void QuickStandardTitleBar::updateTitleBarColor()
}
setColor(backgroundColor);
m_windowTitleLabel->setColor(foregroundColor);
m_minimizeButton->setInactive(!active);
m_maximizeButton->setInactive(!active);
m_closeButton->setInactive(!active);
}

void QuickStandardTitleBar::clickMinimizeButton()
Expand Down
53 changes: 44 additions & 9 deletions src/widgets/standardsystembutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ void StandardSystemButtonPrivate::refreshButtonTheme(const bool force)
if (m_buttonType == SystemButtonType::Unknown) {
return;
}
const SystemTheme systemTheme = []() -> SystemTheme {
const SystemTheme systemTheme = [this]() -> SystemTheme {
if (m_forceLightTheme) {
return SystemTheme::Light;
}
#ifdef Q_OS_WINDOWS
if (Utils::isTitleBarColorized()) {
return SystemTheme::Dark;
Expand Down Expand Up @@ -204,7 +207,16 @@ void StandardSystemButtonPrivate::setHovered(const bool value)
if (m_hovered) {
const QString toolTip = q->toolTip();
if (!toolTip.isEmpty() && !QToolTip::isVisible()) {
QToolTip::showText(q->mapToGlobal(QPoint(0, -(qRound(qreal(q->height()) * 1.3)))), toolTip, q, q->geometry());
const int yPos = [q]() -> int {
const auto h = qreal(q->height());
if (const QWidget * const window = q->window()) {
if (Utils::windowStatesToWindowState(window->windowState()) == Qt::WindowMaximized) {
return int(qRound(h * 0.5));
}
}
return -int(qRound(h * 1.3));
}();
QToolTip::showText(q->mapToGlobal(QPoint(-2, yPos)), toolTip, q, q->geometry());
}
} else {
if (QToolTip::isVisible()) {
Expand Down Expand Up @@ -299,17 +311,40 @@ void StandardSystemButtonPrivate::paintEventHandler(QPaintEvent *event)
painter.fillRect(g_buttonRect, color);
}
if (!m_icon.isNull()) {
painter.drawPixmap(g_buttonIconX,
g_buttonIconY,
((m_buttonType == SystemButtonType::Close)
&& (m_buttonTheme == SystemTheme::Light) && m_hovered
&& !m_reversedIcon.isNull())
? m_reversedIcon
: m_icon);
painter.drawPixmap(g_buttonIconX, g_buttonIconY, [this]() -> QPixmap {
if (m_reversedIcon.isNull()) {
return m_icon;
}
if (m_hovered && m_forceLightTheme) {
return m_reversedIcon;
}
return m_icon;
}());
}
painter.restore();
}

void StandardSystemButtonPrivate::setInactive(const bool value)
{
const bool force = (value && Utils::isTitleBarColorized() && !Utils::shouldAppsUseDarkMode());
if (m_forceLightTheme == force) {
return;
}
m_forceLightTheme = force;
m_shouldCheck = m_forceLightTheme;
refreshButtonTheme(true);
}

void StandardSystemButtonPrivate::checkInactive()
{
if (!m_shouldCheck) {
return;
}
m_forceLightTheme = m_checkFlag;
m_checkFlag = !m_checkFlag;
refreshButtonTheme(true);
}

void StandardSystemButtonPrivate::initialize()
{
Q_Q(StandardSystemButton);
Expand Down
13 changes: 9 additions & 4 deletions src/widgets/standardtitlebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "standardtitlebar.h"
#include "standardtitlebar_p.h"
#include "standardsystembutton.h"
#include "standardsystembutton_p.h"
#include <QtCore/qcoreevent.h>
#include <QtGui/qpainter.h>
#include <QtWidgets/qlabel.h>
Expand Down Expand Up @@ -84,13 +85,13 @@ void StandardTitleBarPrivate::setTitleLabelAlignment(const Qt::Alignment value)
m_labelAlignment = value;
bool needsInvalidate = false;
if (m_labelAlignment & Qt::AlignLeft) {
m_labelLeftStretch->changeSize(0, 0);
m_labelLeftStretch->changeSize(kDefaultTitleBarContentsMargin, 0, QSizePolicy::Fixed);
m_labelRightStretch->changeSize(0, 0, QSizePolicy::Expanding);
needsInvalidate = true;
}
if (m_labelAlignment & Qt::AlignRight) {
m_labelLeftStretch->changeSize(0, 0, QSizePolicy::Expanding);
m_labelRightStretch->changeSize(0, 0);
m_labelRightStretch->changeSize(kDefaultTitleBarContentsMargin, 0, QSizePolicy::Fixed);
needsInvalidate = true;
}
if (m_labelAlignment & Qt::AlignHCenter) {
Expand All @@ -100,6 +101,8 @@ void StandardTitleBarPrivate::setTitleLabelAlignment(const Qt::Alignment value)
}
Q_Q(StandardTitleBar);
if (needsInvalidate) {
// Tell the layout manager that we have changed the layout item's size
// manually to let it refresh the layout immediately.
q->layout()->invalidate();
}
Q_EMIT q->titleLabelAlignmentChanged();
Expand Down Expand Up @@ -162,6 +165,9 @@ void StandardTitleBarPrivate::updateTitleBarStyleSheet()
}();
const QColor windowTitleLabelTextColor = (active ? ((dark || colorizedTitleBar) ? kDefaultWhiteColor : kDefaultBlackColor) : kDefaultDarkGrayColor);
m_windowTitleLabel->setStyleSheet(kStyleSheetColorTemplate.arg(windowTitleLabelTextColor.name()));
StandardSystemButtonPrivate::get(m_minimizeButton.data())->setInactive(!active);
StandardSystemButtonPrivate::get(m_maximizeButton.data())->setInactive(!active);
StandardSystemButtonPrivate::get(m_closeButton.data())->setInactive(!active);
Q_Q(StandardTitleBar);
q->setStyleSheet(kStyleSheetBackgroundColorTemplate.arg(titleBarBackgroundColor.name()));
q->update();
Expand Down Expand Up @@ -253,9 +259,8 @@ void StandardTitleBarPrivate::initialize()
systemButtonsOuterLayout->addLayout(systemButtonsInnerLayout);
systemButtonsOuterLayout->addStretch();
const auto titleBarLayout = new QHBoxLayout(q);
titleBarLayout->setContentsMargins(0, 0, 0, 0);
titleBarLayout->setSpacing(0);
titleBarLayout->addSpacerItem(new QSpacerItem(kDefaultTitleBarContentsMargin, 0, QSizePolicy::Fixed, QSizePolicy::Fixed));
titleBarLayout->setContentsMargins(0, 0, 0, 0);
titleBarLayout->addLayout(titleLabelLayout);
titleBarLayout->addLayout(systemButtonsOuterLayout);
q->setLayout(titleBarLayout);
Expand Down

0 comments on commit 5c3b8b7

Please sign in to comment.