From a27e9ad73491113eabd442b6264e1b47e174a511 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 7 Oct 2018 14:45:46 +0300 Subject: [PATCH] Use black icon for support mode (support). --- .../SourceFiles/core/crash_report_window.cpp | 3 +- Telegram/SourceFiles/mainwindow.cpp | 3 + Telegram/SourceFiles/messenger.cpp | 5 +- Telegram/SourceFiles/window/main_window.cpp | 89 +++++++++++++++++-- Telegram/SourceFiles/window/main_window.h | 2 + 5 files changed, 91 insertions(+), 11 deletions(-) diff --git a/Telegram/SourceFiles/core/crash_report_window.cpp b/Telegram/SourceFiles/core/crash_report_window.cpp index 3015f308a..d13a164a9 100644 --- a/Telegram/SourceFiles/core/crash_report_window.cpp +++ b/Telegram/SourceFiles/core/crash_report_window.cpp @@ -19,8 +19,7 @@ PreLaunchWindow *PreLaunchWindowInstance = nullptr; PreLaunchWindow::PreLaunchWindow(QString title) { Fonts::Start(); - auto icon = Window::CreateIcon(); - setWindowIcon(icon); + setWindowIcon(Window::CreateIcon()); setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint); setWindowTitle(title.isEmpty() ? qsl("Telegram") : title); diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 5e172ed95..b1efaba00 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -874,6 +874,9 @@ QImage MainWindow::iconWithCounter(int size, int count, style::color bg, style:: } QImage img(smallIcon ? ((size == 16) ? iconbig16 : (size == 32 ? iconbig32 : iconbig64)) : ((size == 16) ? icon16 : (size == 32 ? icon32 : icon64))); + if (AuthSession::Exists() && Auth().supportMode()) { + Window::ConvertIconToBlack(img); + } if (!count) return img; if (smallIcon) { diff --git a/Telegram/SourceFiles/messenger.cpp b/Telegram/SourceFiles/messenger.cpp index fd142279e..37076742e 100644 --- a/Telegram/SourceFiles/messenger.cpp +++ b/Telegram/SourceFiles/messenger.cpp @@ -573,8 +573,8 @@ void Messenger::startLocalStorage() { } } }); - subscribe(authSessionChanged(), [this] { - InvokeQueued(this, [this] { + subscribe(authSessionChanged(), [=] { + InvokeQueued(this, [=] { const auto phone = AuthSession::Exists() ? Auth().user()->phone() : QString(); @@ -588,6 +588,7 @@ void Messenger::startLocalStorage() { if (_mtproto) { _mtproto->requestConfig(); } + qApp->setWindowIcon(Window::CreateIcon()); }); }); } diff --git a/Telegram/SourceFiles/window/main_window.cpp b/Telegram/SourceFiles/window/main_window.cpp index e423a690f..6b0d8cfbc 100644 --- a/Telegram/SourceFiles/window/main_window.cpp +++ b/Telegram/SourceFiles/window/main_window.cpp @@ -24,6 +24,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_window.h" #include "styles/style_boxes.h" +#ifdef small +#undef small +#endif // small + namespace Window { constexpr auto kInactivePressTimeout = TimeMs(200); @@ -37,12 +41,70 @@ QImage LoadLogoNoMargin() { return QImage(qsl(":/gui/art/logo_256_no_margin.png")); } +void ConvertIconToBlack(QImage &image) { + if (image.format() != QImage::Format_ARGB32_Premultiplied) { + image = std::move(image).convertToFormat( + QImage::Format_ARGB32_Premultiplied); + } + //const auto gray = red * 0.299 + green * 0.587 + blue * 0.114; + //const auto result = (gray - 100 < 0) ? 0 : (gray - 100) * 255 / 155; + constexpr auto scale = 255 / 155.; + constexpr auto red = 0.299; + constexpr auto green = 0.587; + constexpr auto blue = 0.114; + static constexpr auto shift = (1 << 24); + auto shifter = [](double value) { + return uint32(value * shift); + }; + constexpr auto iscale = shifter(scale); + constexpr auto ired = shifter(red); + constexpr auto igreen = shifter(green); + constexpr auto iblue = shifter(blue); + constexpr auto threshold = 100; + constexpr auto ithreshold = shifter(threshold); + + const auto width = image.width(); + const auto height = image.height(); + const auto data = reinterpret_cast(image.bits()); + const auto intsPerLine = image.bytesPerLine() / 4; + const auto intsPerLineAdded = intsPerLine - width; + + auto pixel = data; + for (auto j = 0; j != height; ++j) { + for (auto i = 0; i != width; ++i) { + const auto value = *pixel; + const auto gray = (((value >> 16) & 0xFF) * ired + + ((value >> 8) & 0xFF) * igreen + + (value & 0xFF) * iblue) >> 24; + const auto small = gray - threshold; + const auto test = ~small; + const auto result = (test >> 31) * small * iscale; + const auto component = (result >> 24) & 0xFF; + *pixel++ = (value & 0xFF000000U) + | (component << 16) + | (component << 8) + | component; + } + pixel += intsPerLineAdded; + } +} + QIcon CreateOfficialIcon() { auto useNoMarginLogo = (cPlatform() == dbipMac); - if (auto messenger = Messenger::InstancePointer()) { - return QIcon(App::pixmapFromImageInPlace(useNoMarginLogo ? messenger->logoNoMargin() : messenger->logo())); + auto image = [&] { + if (const auto messenger = Messenger::InstancePointer()) { + return useNoMarginLogo + ? messenger->logoNoMargin() + : messenger->logo(); + } + return useNoMarginLogo + ? LoadLogoNoMargin() + : LoadLogo(); + }(); + if (AuthSession::Exists() && Auth().supportMode()) { + ConvertIconToBlack(image); } - return QIcon(App::pixmapFromImageInPlace(useNoMarginLogo ? LoadLogoNoMargin() : LoadLogo())); + return QIcon(App::pixmapFromImageInPlace(std::move(image))); } QIcon CreateIcon() { @@ -58,14 +120,22 @@ MainWindow::MainWindow() , _body(this) , _icon(CreateIcon()) , _titleText(qsl("Telegram")) { - subscribe(Theme::Background(), [this](const Theme::BackgroundUpdate &data) { + subscribe(Theme::Background(), [=]( + const Theme::BackgroundUpdate &data) { if (data.paletteChanged()) { updatePalette(); } }); - subscribe(Global::RefUnreadCounterUpdate(), [this] { updateUnreadCounter(); }); - subscribe(Global::RefWorkMode(), [this](DBIWorkMode mode) { workmodeUpdated(mode); }); - subscribe(Messenger::Instance().authSessionChanged(), [this] { checkAuthSession(); }); + subscribe(Global::RefUnreadCounterUpdate(), [=] { + updateUnreadCounter(); + }); + subscribe(Global::RefWorkMode(), [=](DBIWorkMode mode) { + workmodeUpdated(mode); + }); + subscribe(Messenger::Instance().authSessionChanged(), [=] { + checkAuthSession(); + updateWindowIcon(); + }); checkAuthSession(); Messenger::Instance().termsLockValue( @@ -194,6 +264,11 @@ bool MainWindow::computeIsActive() const { } void MainWindow::updateWindowIcon() { + const auto supportIcon = AuthSession::Exists() && Auth().supportMode(); + if (supportIcon != _usingSupportIcon) { + _icon = CreateIcon(); + _usingSupportIcon = supportIcon; + } setWindowIcon(_icon); } diff --git a/Telegram/SourceFiles/window/main_window.h b/Telegram/SourceFiles/window/main_window.h index 1c8834840..9a8636468 100644 --- a/Telegram/SourceFiles/window/main_window.h +++ b/Telegram/SourceFiles/window/main_window.h @@ -23,6 +23,7 @@ struct TermsLock; QImage LoadLogo(); QImage LoadLogoNoMargin(); QIcon CreateIcon(); +void ConvertIconToBlack(QImage &image); class MainWindow : public Ui::RpWidget, protected base::Subscriber { Q_OBJECT @@ -165,6 +166,7 @@ private: QPointer _termsBox; QIcon _icon; + bool _usingSupportIcon = false; QString _titleText; bool _isActive = false;