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

Commit

Permalink
minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
wangwenx190 committed Nov 11, 2023
1 parent bb3f8e0 commit 7fdea56
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 35 deletions.
1 change: 1 addition & 0 deletions include/FramelessHelper/Core/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace Utils
[[nodiscard]] FRAMELESSHELPER_CORE_API quint32 defaultScreenDpi();
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowAccelerated(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
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowsVersionOrGreater(const Global::WindowsVersion version);
Expand Down
11 changes: 1 addition & 10 deletions src/core/chromepalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,7 @@ void ChromePalettePrivate::refresh()
titleBarInactiveBackgroundColor_sys = (dark ? kDefaultSystemDarkColor : kDefaultWhiteColor);
titleBarActiveForegroundColor_sys = [this, dark, colorized]() -> QColor {
if (dark || colorized) {
// Calculate the most appropriate foreground color, based on the
// 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 Utils::calculateForegroundColor(titleBarActiveBackgroundColor_sys);
}
return kDefaultBlackColor;
}();
Expand Down
51 changes: 31 additions & 20 deletions src/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,39 +279,28 @@ 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 = [isDark, isClose, isTitleColor]() -> QColor {
auto result = [isDark, isClose, isTitleColor]() -> QColor {
if (isClose) {
return kDefaultSystemCloseButtonBackgroundColor;
}
if (isTitleColor) {
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 calculateForegroundColor(getAccentColor());
}
return isDark ? kDefaultWhiteColor : kDefaultBlackColor;
return (isDark ? kDefaultWhiteColor : kDefaultBlackColor);
}();
if (isClose) {
return (isHovered ? result.lighter(110) : result.lighter(140));
}

return (isHovered ? QColor(result.red(), result.green(), result.blue(), 12) :
QColor(result.red(), result.green(), result.blue(), 6));
if (isHovered) {
result.setAlpha(12);
} else {
result.setAlpha(6);
}
return result;
}

bool Utils::shouldAppsUseDarkMode()
Expand Down Expand Up @@ -654,4 +643,26 @@ bool Utils::isWindowTransparent(const QWindow *window)
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
3 changes: 3 additions & 0 deletions src/quick/quickstandardsystembutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ void QuickStandardSystemButton::updateColor()
if (!hover && !active && m_inactiveForegroundColor.isValid()) {
return m_inactiveForegroundColor;
}
if ((m_buttonType == QuickGlobal::SystemButtonType::Close) && hover) {
return kDefaultWhiteColor;
}
if (m_activeForegroundColor.isValid()) {
return m_activeForegroundColor;
}
Expand Down
11 changes: 6 additions & 5 deletions src/widgets/standardsystembutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,13 @@ void StandardSystemButton::paintEvent(QPaintEvent *event)
painter.save();
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing
| 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.
if (isDown() && d->pressColor.isValid()) {
return d->pressColor;
}
if (underMouse() && d->hoverColor.isValid()) {
if (isHovering && d->hoverColor.isValid()) {
return d->hoverColor;
}
if (d->normalColor.isValid()) {
Expand All @@ -323,11 +324,11 @@ void StandardSystemButton::paintEvent(QPaintEvent *event)
painter.fillRect(buttonRect, backgroundColor);
}
if (!d->glyph.isEmpty()) {
painter.setPen([this, d]() -> QColor {
if (!underMouse() && !d->active && d->inactiveForegroundColor.isValid()) {
painter.setPen([isHovering, d]() -> QColor {
if (!isHovering && !d->active && d->inactiveForegroundColor.isValid()) {
return d->inactiveForegroundColor;
}
if (d->buttonType == SystemButtonType::Close && underMouse()) {
if ((d->buttonType == SystemButtonType::Close) && isHovering) {
return kDefaultWhiteColor;
}
if (d->activeForegroundColor.isValid()) {
Expand Down

0 comments on commit 7fdea56

Please sign in to comment.