From 1438046dd46d527f0cd04288bfb4958b4ba6b0fa Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 19 Nov 2024 10:37:47 +0400 Subject: [PATCH] Improve bottom label color in mini apps. --- .../ui/chat/attach/attach_bot_webview.cpp | 46 +++++++++++++++++-- .../ui/chat/attach/attach_bot_webview.h | 3 ++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp index 6fa4bb997..1ede6deb9 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp @@ -783,6 +783,8 @@ void Panel::createWebviewBottom() { _webviewBottom.get(), _bottomText.value(), st::paymentsWebviewBottom); + _webviewBottomLabel = label; + const auto height = padding.top() + label->heightNoMargins() + padding.bottom(); @@ -840,6 +842,7 @@ bool Panel::createWebview(const Webview::ThemeParams ¶ms) { } } if (_webviewBottom.get() == bottom) { + _webviewBottomLabel = nullptr; _webviewBottom = nullptr; _secondaryButton = nullptr; _mainButton = nullptr; @@ -1596,20 +1599,53 @@ void Panel::processHeaderColor(const QJsonObject &args) { } } +void Panel::overrideBodyColor(std::optional color) { + _widget->overrideBodyColor(color); + const auto raw = _webviewBottomLabel.data(); + if (!raw) { + return; + } else if (!color) { + raw->setTextColorOverride(std::nullopt); + return; + } + const auto set = [](const style::color &color, QColor value) { + color.set( + uchar(value.red()), + uchar(value.green()), + uchar(value.blue()), + uchar(value.alpha())); + }; + + const auto contrast = 2.5; + const auto luminance = 0.2126 * color->redF() + + 0.7152 * color->greenF() + + 0.0722 * color->blueF(); + const auto textColor = (luminance > 0.5) + ? QColor(0, 0, 0) + : QColor(255, 255, 255); + const auto textLuminance = (luminance > 0.5) ? 0 : 1; + const auto adaptiveOpacity = (luminance - textLuminance + contrast) + / contrast; + const auto opacity = std::clamp(adaptiveOpacity, 0.5, 0.64); + auto buttonColor = textColor; + buttonColor.setAlphaF(opacity); + raw->setTextColorOverride(buttonColor); +} + void Panel::processBackgroundColor(const QJsonObject &args) { _bodyColorReceived = true; if (const auto color = ParseColor(args["color"].toString())) { - _widget->overrideBodyColor(color); + overrideBodyColor(*color); _bodyColorLifetime.destroy(); } else if (const auto color = LookupNamedColor( args["color_key"].toString())) { - _widget->overrideBodyColor((*color)->c); + overrideBodyColor((*color)->c); _bodyColorLifetime = style::PaletteChanged( ) | rpl::start_with_next([=] { - _widget->overrideBodyColor((*color)->c); + overrideBodyColor((*color)->c); }); } else { - _widget->overrideBodyColor(std::nullopt); + overrideBodyColor(std::nullopt); _bodyColorLifetime.destroy(); } if (const auto raw = _bottomButtonsBg.get()) { @@ -1923,7 +1959,7 @@ void Panel::updateColorOverrides(const Webview::ThemeParams ¶ms) { _widget->overrideTitleColor(params.titleBg); } if (!_bodyColorReceived && params.bodyBg.alpha() == 255) { - _widget->overrideBodyColor(params.bodyBg); + overrideBodyColor(params.bodyBg); } } diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h index e5a822474..b17a648a4 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h @@ -19,6 +19,7 @@ class QJsonObject; class QJsonValue; namespace Ui { +class FlatLabel; class BoxContent; class RpWidget; class SeparatePanel; @@ -196,6 +197,7 @@ private: void sendFullScreen(); void updateColorOverrides(const Webview::ThemeParams ¶ms); + void overrideBodyColor(std::optional color); using EventData = std::variant; void postEvent(const QString &event); @@ -216,6 +218,7 @@ private: std::unique_ptr _widget; std::unique_ptr _webview; std::unique_ptr _webviewBottom; + QPointer _webviewBottomLabel; rpl::variable _bottomText; QPointer _webviewParent; std::unique_ptr _bottomButtonsBg;