Compare commits

..

2 Commits

Author SHA1 Message Date
Zhao Yuhang 7fdea56785 minor tweaks 2023-11-11 16:26:32 +08:00
kirakira bb3f8e0d40
Modified system button colors for hover and press states (#318)
* 窗口关闭按钮鼠标悬浮时的前景色修改为白色

* #243 修改系统按钮的鼠标悬浮背景色和按下时背景色

---------

Co-authored-by: yangzhenghan <yangzhenghan@sinap.ac.cn>
2023-11-11 16:08:19 +08:00
5 changed files with 45 additions and 20 deletions

View File

@ -81,6 +81,7 @@ namespace Utils
[[nodiscard]] FRAMELESSHELPER_CORE_API quint32 defaultScreenDpi(); [[nodiscard]] FRAMELESSHELPER_CORE_API quint32 defaultScreenDpi();
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowAccelerated(const QWindow *window); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowAccelerated(const QWindow *window);
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowTransparent(const QWindow *window); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowTransparent(const QWindow *window);
[[nodiscard]] FRAMELESSHELPER_CORE_API QColor calculateForegroundColor(const QColor &backgroundColor);
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowsVersionOrGreater(const Global::WindowsVersion version); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowsVersionOrGreater(const Global::WindowsVersion version);

View File

@ -93,16 +93,7 @@ void ChromePalettePrivate::refresh()
titleBarInactiveBackgroundColor_sys = (dark ? kDefaultSystemDarkColor : kDefaultWhiteColor); titleBarInactiveBackgroundColor_sys = (dark ? kDefaultSystemDarkColor : kDefaultWhiteColor);
titleBarActiveForegroundColor_sys = [this, dark, colorized]() -> QColor { titleBarActiveForegroundColor_sys = [this, dark, colorized]() -> QColor {
if (dark || colorized) { if (dark || colorized) {
// Calculate the most appropriate foreground color, based on the return Utils::calculateForegroundColor(titleBarActiveBackgroundColor_sys);
// current background color.
const qreal grayF = (
(qreal(0.299) * titleBarActiveBackgroundColor_sys.redF()) +
(qreal(0.587) * titleBarActiveBackgroundColor_sys.greenF()) +
(qreal(0.114) * titleBarActiveBackgroundColor_sys.blueF()));
static constexpr const auto kFlag = qreal(0.5);
if ((grayF < kFlag) || qFuzzyCompare(grayF, kFlag)) {
return kDefaultWhiteColor;
}
} }
return kDefaultBlackColor; return kDefaultBlackColor;
}(); }();

View File

@ -25,6 +25,7 @@
#include "utils.h" #include "utils.h"
#include "framelesshelpercore_global_p.h" #include "framelesshelpercore_global_p.h"
#include "framelessmanager_p.h" #include "framelessmanager_p.h"
#include "framelessmanager.h"
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
# include "winverhelper_p.h" # include "winverhelper_p.h"
#endif // Q_OS_WINDOWS #endif // Q_OS_WINDOWS
@ -278,25 +279,28 @@ QColor Utils::calculateSystemButtonBackgroundColor(const SystemButtonType button
if (state == ButtonState::Normal) { if (state == ButtonState::Normal) {
return kDefaultTransparentColor; return kDefaultTransparentColor;
} }
const bool isDark = (FramelessManager::instance()->systemTheme() == SystemTheme::Dark);
const bool isClose = (button == SystemButtonType::Close); const bool isClose = (button == SystemButtonType::Close);
const bool isTitleColor = isTitleBarColorized(); const bool isTitleColor = isTitleBarColorized();
const bool isHovered = (state == ButtonState::Hovered); const bool isHovered = (state == ButtonState::Hovered);
const auto result = [isClose, isTitleColor]() -> QColor { auto result = [isDark, isClose, isTitleColor]() -> QColor {
if (isClose) { if (isClose) {
return kDefaultSystemCloseButtonBackgroundColor; return kDefaultSystemCloseButtonBackgroundColor;
} }
if (isTitleColor) { if (isTitleColor) {
return getAccentColor(); return calculateForegroundColor(getAccentColor());
} }
return kDefaultSystemButtonBackgroundColor; return (isDark ? kDefaultWhiteColor : kDefaultBlackColor);
}(); }();
if (isClose) { if (isClose) {
return (isHovered ? result.lighter(110) : result.lighter(140)); return (isHovered ? result.lighter(110) : result.lighter(140));
} }
if (!isTitleColor) { if (isHovered) {
return (isHovered ? result.lighter(110) : result); result.setAlpha(12);
} else {
result.setAlpha(6);
} }
return (isHovered ? result.lighter(150) : result.lighter(120)); return result;
} }
bool Utils::shouldAppsUseDarkMode() bool Utils::shouldAppsUseDarkMode()
@ -639,4 +643,26 @@ bool Utils::isWindowTransparent(const QWindow *window)
return window->format().hasAlpha(); return window->format().hasAlpha();
} }
QColor Utils::calculateForegroundColor(const QColor &backgroundColor)
{
Q_ASSERT(backgroundColor.isValid());
if (!backgroundColor.isValid()) {
return kDefaultBlackColor;
}
static constexpr const auto kFlag = qreal(0.5);
if (backgroundColor.alphaF() < kFlag) {
return kDefaultBlackColor;
}
// Calculate the most appropriate foreground color, based on the
// current background color.
const qreal grayF = (
(qreal(0.299) * backgroundColor.redF()) +
(qreal(0.587) * backgroundColor.greenF()) +
(qreal(0.114) * backgroundColor.blueF()));
if ((grayF < kFlag) || qFuzzyCompare(grayF, kFlag)) {
return kDefaultWhiteColor;
}
return kDefaultBlackColor;
}
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -240,6 +240,9 @@ void QuickStandardSystemButton::updateColor()
if (!hover && !active && m_inactiveForegroundColor.isValid()) { if (!hover && !active && m_inactiveForegroundColor.isValid()) {
return m_inactiveForegroundColor; return m_inactiveForegroundColor;
} }
if ((m_buttonType == QuickGlobal::SystemButtonType::Close) && hover) {
return kDefaultWhiteColor;
}
if (m_activeForegroundColor.isValid()) { if (m_activeForegroundColor.isValid()) {
return m_activeForegroundColor; return m_activeForegroundColor;
} }

View File

@ -305,12 +305,13 @@ void StandardSystemButton::paintEvent(QPaintEvent *event)
painter.save(); painter.save();
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing
| QPainter::SmoothPixmapTransform); | QPainter::SmoothPixmapTransform);
const auto backgroundColor = [this, d]() -> QColor { const bool isHovering = underMouse();
const auto backgroundColor = [isHovering, d, this]() -> QColor {
// The pressed state has higher priority than the hovered state. // The pressed state has higher priority than the hovered state.
if (isDown() && d->pressColor.isValid()) { if (isDown() && d->pressColor.isValid()) {
return d->pressColor; return d->pressColor;
} }
if (underMouse() && d->hoverColor.isValid()) { if (isHovering && d->hoverColor.isValid()) {
return d->hoverColor; return d->hoverColor;
} }
if (d->normalColor.isValid()) { if (d->normalColor.isValid()) {
@ -323,10 +324,13 @@ void StandardSystemButton::paintEvent(QPaintEvent *event)
painter.fillRect(buttonRect, backgroundColor); painter.fillRect(buttonRect, backgroundColor);
} }
if (!d->glyph.isEmpty()) { if (!d->glyph.isEmpty()) {
painter.setPen([this, d]() -> QColor { painter.setPen([isHovering, d]() -> QColor {
if (!underMouse() && !d->active && d->inactiveForegroundColor.isValid()) { if (!isHovering && !d->active && d->inactiveForegroundColor.isValid()) {
return d->inactiveForegroundColor; return d->inactiveForegroundColor;
} }
if ((d->buttonType == SystemButtonType::Close) && isHovering) {
return kDefaultWhiteColor;
}
if (d->activeForegroundColor.isValid()) { if (d->activeForegroundColor.isValid()) {
return d->activeForegroundColor; return d->activeForegroundColor;
} }