From 439f8d0914f9cb9f5f092675b9f01a9ee42a645b Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Sat, 30 Mar 2024 04:52:52 +0400 Subject: [PATCH] Implement monochrome icon setting on Linux --- .../SourceFiles/platform/linux/tray_linux.cpp | 61 +++++++++++++------ .../SourceFiles/platform/linux/tray_linux.h | 4 -- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/tray_linux.cpp b/Telegram/SourceFiles/platform/linux/tray_linux.cpp index a9e1de383..9753556c4 100644 --- a/Telegram/SourceFiles/platform/linux/tray_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/tray_linux.cpp @@ -22,6 +22,17 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include namespace Platform { +namespace { + +[[nodiscard]] QString PanelIconName(int counter, bool muted) { + return (counter > 0) + ? (muted + ? u"telegram-mute-panel"_q + : u"telegram-attention-panel"_q) + : u"telegram-panel"_q; +} + +} // namespace class IconGraphic final { public: @@ -35,29 +46,27 @@ public: bool muted) const; [[nodiscard]] QIcon systemIcon( const QString &iconThemeName, + bool monochrome, int counter, bool muted) const; [[nodiscard]] QIcon trayIcon( const QIcon &systemIcon, const QString &iconThemeName, + bool monochrome, int counter, bool muted); private: - [[nodiscard]] QString panelIconName(int counter, bool muted) const; [[nodiscard]] int counterSlice(int counter) const; void updateIconRegenerationNeeded( const QIcon &icon, const QIcon &systemIcon, const QString &iconThemeName, + bool monochrome, int counter, bool muted); [[nodiscard]] QSize dprSize(const QImage &image) const; - const QString _panelTrayIconName; - const QString _mutePanelTrayIconName; - const QString _attentionPanelTrayIconName; - const int _iconSizes[7]; bool _muted = true; @@ -66,42 +75,37 @@ private: QIcon _trayIcon; QIcon _systemIcon; QString _themeName; + bool _monochrome; }; IconGraphic::IconGraphic() -: _panelTrayIconName("telegram-panel") -, _mutePanelTrayIconName("telegram-mute-panel") -, _attentionPanelTrayIconName("telegram-attention-panel") -, _iconSizes{ 16, 22, 32, 48, 64, 128, 256 } { +: _iconSizes{ 16, 22, 32, 48, 64, 128, 256 } { } IconGraphic::~IconGraphic() = default; -QString IconGraphic::panelIconName(int counter, bool muted) const { - return (counter > 0) - ? (muted - ? _mutePanelTrayIconName - : _attentionPanelTrayIconName) - : _panelTrayIconName; -} - QIcon IconGraphic::systemIcon( const QString &iconThemeName, + bool monochrome, int counter, bool muted) const { if (iconThemeName == _themeName + && monochrome == _monochrome && (counter > 0) == (_count > 0) && muted == _muted) { return _systemIcon; } const auto candidates = { - panelIconName(counter, muted), + monochrome ? PanelIconName(counter, muted) : QString(), base::IconName(), }; for (const auto &candidate : candidates) { + if (candidate.isEmpty()) { + continue; + } const auto icon = QIcon::fromTheme(candidate); if (icon.name() == candidate) { return icon; @@ -134,11 +138,13 @@ void IconGraphic::updateIconRegenerationNeeded( const QIcon &icon, const QIcon &systemIcon, const QString &iconThemeName, + bool monochrome, int counter, bool muted) { _trayIcon = icon; _systemIcon = systemIcon; _themeName = iconThemeName; + _monochrome = monochrome; _count = counterSlice(counter); _muted = muted; } @@ -150,18 +156,19 @@ QSize IconGraphic::dprSize(const QImage &image) const { QIcon IconGraphic::trayIcon( const QIcon &systemIcon, const QString &iconThemeName, + bool monochrome, int counter, bool muted) { if (!isRefreshNeeded(systemIcon, iconThemeName, counter, muted)) { return _trayIcon; } - - if (systemIcon.name() == panelIconName(counter, muted)) { + if (systemIcon.name() == PanelIconName(counter, muted)) { updateIconRegenerationNeeded( systemIcon, systemIcon, iconThemeName, + monochrome, counter, muted); @@ -227,6 +234,7 @@ QIcon IconGraphic::trayIcon( result, systemIcon, iconThemeName, + monochrome, counter, muted); @@ -290,6 +298,7 @@ void Tray::createIcon() { }; const auto iconThemeName = QIcon::themeName(); + const auto monochrome = Core::App().settings().trayIconMonochrome(); const auto counter = Core::App().unreadBadge(); const auto muted = Core::App().unreadBadgeMuted(); @@ -297,9 +306,11 @@ void Tray::createIcon() { _icon->setIcon(_iconGraphic->trayIcon( _iconGraphic->systemIcon( iconThemeName, + monochrome, counter, muted), iconThemeName, + monochrome, counter, muted)); _icon->setToolTip(AppName.utf16()); @@ -342,9 +353,11 @@ void Tray::updateIcon() { } const auto counter = Core::App().unreadBadge(); const auto muted = Core::App().unreadBadgeMuted(); + const auto monochrome = Core::App().settings().trayIconMonochrome(); const auto iconThemeName = QIcon::themeName(); const auto systemIcon = _iconGraphic->systemIcon( iconThemeName, + monochrome, counter, muted); @@ -356,6 +369,7 @@ void Tray::updateIcon() { _icon->setIcon(_iconGraphic->trayIcon( systemIcon, iconThemeName, + monochrome, counter, muted)); } @@ -436,4 +450,11 @@ rpl::lifetime &Tray::lifetime() { Tray::~Tray() = default; +bool HasMonochromeSetting() { + return QIcon::hasThemeIcon( + PanelIconName( + Core::App().unreadBadge(), + Core::App().unreadBadgeMuted())); +} + } // namespace Platform diff --git a/Telegram/SourceFiles/platform/linux/tray_linux.h b/Telegram/SourceFiles/platform/linux/tray_linux.h index 769fe4409..dd0175a2c 100644 --- a/Telegram/SourceFiles/platform/linux/tray_linux.h +++ b/Telegram/SourceFiles/platform/linux/tray_linux.h @@ -67,8 +67,4 @@ private: }; -inline bool HasMonochromeSetting() { - return false; -} - } // namespace Platform