From bb3f8e0d409f2b2adde5034840cfe842258a1531 Mon Sep 17 00:00:00 2001 From: kirakira <847320916@QQ.com> Date: Sat, 11 Nov 2023 16:08:19 +0800 Subject: [PATCH] Modified system button colors for hover and press states (#318) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 窗口关闭按钮鼠标悬浮时的前景色修改为白色 * #243 修改系统按钮的鼠标悬浮背景色和按下时背景色 --------- Co-authored-by: yangzhenghan --- src/core/utils.cpp | 29 +++++++++++++++++++++------- src/widgets/standardsystembutton.cpp | 3 +++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/core/utils.cpp b/src/core/utils.cpp index 3cf8500a..d5688e1f 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -25,6 +25,7 @@ #include "utils.h" #include "framelesshelpercore_global_p.h" #include "framelessmanager_p.h" +#include "framelessmanager.h" #ifdef Q_OS_WINDOWS # include "winverhelper_p.h" #endif // Q_OS_WINDOWS @@ -278,25 +279,39 @@ QColor Utils::calculateSystemButtonBackgroundColor(const SystemButtonType button if (state == ButtonState::Normal) { return kDefaultTransparentColor; } + + const bool isDark = (FramelessManager::instance()->systemTheme() == SystemTheme::Dark); const bool isClose = (button == SystemButtonType::Close); const bool isTitleColor = isTitleBarColorized(); const bool isHovered = (state == ButtonState::Hovered); - const auto result = [isClose, isTitleColor]() -> QColor { + const auto result = [isDark, isClose, isTitleColor]() -> QColor { if (isClose) { return kDefaultSystemCloseButtonBackgroundColor; } if (isTitleColor) { - return getAccentColor(); + const auto accent = getAccentColor(); + return [](const QColor &color) -> QColor { + // Calculate the most appropriate foreground color, based on the + // current background color. + const qreal grayF = ( + (qreal(0.299) * color.redF()) + + (qreal(0.587) * color.greenF()) + + (qreal(0.114) * color.blueF())); + static constexpr const auto kFlag = qreal(0.5); + if ((grayF < kFlag) || qFuzzyCompare(grayF, kFlag)) { + return kDefaultWhiteColor; + } + return kDefaultBlackColor; + }(accent); } - return kDefaultSystemButtonBackgroundColor; + return isDark ? kDefaultWhiteColor : kDefaultBlackColor; }(); if (isClose) { return (isHovered ? result.lighter(110) : result.lighter(140)); } - if (!isTitleColor) { - return (isHovered ? result.lighter(110) : result); - } - return (isHovered ? result.lighter(150) : result.lighter(120)); + + return (isHovered ? QColor(result.red(), result.green(), result.blue(), 12) : + QColor(result.red(), result.green(), result.blue(), 6)); } bool Utils::shouldAppsUseDarkMode() diff --git a/src/widgets/standardsystembutton.cpp b/src/widgets/standardsystembutton.cpp index 73e8205f..fd0d5fd3 100644 --- a/src/widgets/standardsystembutton.cpp +++ b/src/widgets/standardsystembutton.cpp @@ -327,6 +327,9 @@ void StandardSystemButton::paintEvent(QPaintEvent *event) if (!underMouse() && !d->active && d->inactiveForegroundColor.isValid()) { return d->inactiveForegroundColor; } + if (d->buttonType == SystemButtonType::Close && underMouse()) { + return kDefaultWhiteColor; + } if (d->activeForegroundColor.isValid()) { return d->activeForegroundColor; }