mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Moved out static job for Linux tray icon to separated class.
This commit is contained in:
parent
27c5c4b8f2
commit
aee1ef78da
2 changed files with 109 additions and 74 deletions
|
@ -25,30 +25,73 @@ namespace Platform {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
constexpr auto kPanelTrayIconName = "telegram-panel"_cs;
|
[[nodiscard]] QWidget *Parent() {
|
||||||
constexpr auto kMutePanelTrayIconName = "telegram-mute-panel"_cs;
|
Expects(Core::App().primaryWindow() != nullptr);
|
||||||
constexpr auto kAttentionPanelTrayIconName = "telegram-attention-panel"_cs;
|
return Core::App().primaryWindow()->widget();
|
||||||
|
|
||||||
bool TrayIconMuted = true;
|
|
||||||
int32 TrayIconCount = 0;
|
|
||||||
base::flat_map<int, QImage> TrayIconImageBack;
|
|
||||||
QIcon TrayIcon;
|
|
||||||
QString TrayIconThemeName, TrayIconName;
|
|
||||||
|
|
||||||
[[nodiscard]] QString GetPanelIconName(int counter, bool muted) {
|
|
||||||
return (counter > 0)
|
|
||||||
? (muted
|
|
||||||
? kMutePanelTrayIconName.utf16()
|
|
||||||
: kAttentionPanelTrayIconName.utf16())
|
|
||||||
: kPanelTrayIconName.utf16();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] QString GetTrayIconName(int counter, bool muted) {
|
} // namespace
|
||||||
const auto iconName = GetIconName();
|
|
||||||
const auto panelIconName = GetPanelIconName(counter, muted);
|
|
||||||
|
|
||||||
if (QIcon::hasThemeIcon(panelIconName)) {
|
class IconGraphic final {
|
||||||
return panelIconName;
|
public:
|
||||||
|
explicit IconGraphic();
|
||||||
|
~IconGraphic();
|
||||||
|
|
||||||
|
[[nodiscard]] bool isRefreshNeeded(
|
||||||
|
int counter,
|
||||||
|
bool muted,
|
||||||
|
const QString &iconThemeName) const;
|
||||||
|
[[nodiscard]] QIcon trayIcon(int counter, bool muted);
|
||||||
|
|
||||||
|
private:
|
||||||
|
[[nodiscard]] QString panelIconName(int counter, bool muted) const;
|
||||||
|
[[nodiscard]] QString trayIconName(int counter, bool muted) const;
|
||||||
|
[[nodiscard]] int counterSlice(int counter) const;
|
||||||
|
void updateIconRegenerationNeeded(
|
||||||
|
const QIcon &icon,
|
||||||
|
int counter,
|
||||||
|
bool muted,
|
||||||
|
const QString &iconThemeName);
|
||||||
|
[[nodiscard]] QSize dprSize(const QImage &image) const;
|
||||||
|
|
||||||
|
const QString _panelTrayIconName;
|
||||||
|
const QString _mutePanelTrayIconName;
|
||||||
|
const QString _attentionPanelTrayIconName;
|
||||||
|
|
||||||
|
const int _iconSizes[5];
|
||||||
|
|
||||||
|
bool _muted = true;
|
||||||
|
int32 _count = 0;
|
||||||
|
base::flat_map<int, QImage> _imageBack;
|
||||||
|
QIcon _trayIcon;
|
||||||
|
QString _themeName;
|
||||||
|
QString _name;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
IconGraphic::IconGraphic()
|
||||||
|
: _panelTrayIconName("telegram-panel")
|
||||||
|
, _mutePanelTrayIconName("telegram-mute-panel")
|
||||||
|
, _attentionPanelTrayIconName("telegram-attention-panel")
|
||||||
|
, _iconSizes{ 16, 22, 24, 32, 48 } {
|
||||||
|
}
|
||||||
|
|
||||||
|
IconGraphic::~IconGraphic() = default;
|
||||||
|
|
||||||
|
QString IconGraphic::panelIconName(int counter, bool muted) const {
|
||||||
|
return (counter > 0)
|
||||||
|
? (muted
|
||||||
|
? _mutePanelTrayIconName
|
||||||
|
: _attentionPanelTrayIconName)
|
||||||
|
: _panelTrayIconName;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString IconGraphic::trayIconName(int counter, bool muted) const {
|
||||||
|
const auto iconName = GetIconName();
|
||||||
|
const auto panelName = panelIconName(counter, muted);
|
||||||
|
|
||||||
|
if (QIcon::hasThemeIcon(panelName)) {
|
||||||
|
return panelName;
|
||||||
} else if (QIcon::hasThemeIcon(iconName)) {
|
} else if (QIcon::hasThemeIcon(iconName)) {
|
||||||
return iconName;
|
return iconName;
|
||||||
}
|
}
|
||||||
|
@ -57,79 +100,68 @@ QString TrayIconThemeName, TrayIconName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[[nodiscard]] int GetCounterSlice(int counter) {
|
int IconGraphic::counterSlice(int counter) const {
|
||||||
return (counter >= 1000)
|
return (counter >= 1000)
|
||||||
? (1000 + (counter % 100))
|
? (1000 + (counter % 100))
|
||||||
: counter;
|
: counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] bool IsIconRegenerationNeeded(
|
bool IconGraphic::isRefreshNeeded(
|
||||||
int counter,
|
int counter,
|
||||||
bool muted,
|
bool muted,
|
||||||
const QString &iconThemeName = QIcon::themeName()) {
|
const QString &iconThemeName) const {
|
||||||
const auto iconName = GetTrayIconName(counter, muted);
|
const auto iconName = trayIconName(counter, muted);
|
||||||
const auto counterSlice = GetCounterSlice(counter);
|
|
||||||
|
|
||||||
return TrayIcon.isNull()
|
return _trayIcon.isNull()
|
||||||
|| iconThemeName != TrayIconThemeName
|
|| iconThemeName != _themeName
|
||||||
|| iconName != TrayIconName
|
|| iconName != _name
|
||||||
|| muted != TrayIconMuted
|
|| muted != _muted
|
||||||
|| counterSlice != TrayIconCount;
|
|| counterSlice(counter) != _count;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateIconRegenerationNeeded(
|
void IconGraphic::updateIconRegenerationNeeded(
|
||||||
const QIcon &icon,
|
const QIcon &icon,
|
||||||
int counter,
|
int counter,
|
||||||
bool muted,
|
bool muted,
|
||||||
const QString &iconThemeName) {
|
const QString &iconThemeName) {
|
||||||
const auto iconName = GetTrayIconName(counter, muted);
|
const auto iconName = trayIconName(counter, muted);
|
||||||
const auto counterSlice = GetCounterSlice(counter);
|
|
||||||
|
|
||||||
TrayIcon = icon;
|
_trayIcon = icon;
|
||||||
TrayIconMuted = muted;
|
_muted = muted;
|
||||||
TrayIconCount = counterSlice;
|
_count = counterSlice(counter);
|
||||||
TrayIconThemeName = iconThemeName;
|
_themeName = iconThemeName;
|
||||||
TrayIconName = iconName;
|
_name = iconName;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] QIcon TrayIconGen(int counter, bool muted) {
|
QSize IconGraphic::dprSize(const QImage &image) const {
|
||||||
|
return image.size() / image.devicePixelRatio();
|
||||||
|
}
|
||||||
|
|
||||||
|
QIcon IconGraphic::trayIcon(int counter, bool muted) {
|
||||||
const auto iconThemeName = QIcon::themeName();
|
const auto iconThemeName = QIcon::themeName();
|
||||||
|
|
||||||
if (!IsIconRegenerationNeeded(counter, muted, iconThemeName)) {
|
if (!isRefreshNeeded(counter, muted, iconThemeName)) {
|
||||||
return TrayIcon;
|
return _trayIcon;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto iconName = GetTrayIconName(counter, muted);
|
const auto iconName = trayIconName(counter, muted);
|
||||||
const auto panelIconName = GetPanelIconName(counter, muted);
|
|
||||||
|
|
||||||
if (iconName == panelIconName) {
|
if (iconName == panelIconName(counter, muted)) {
|
||||||
const auto result = QIcon::fromTheme(iconName);
|
const auto result = QIcon::fromTheme(iconName);
|
||||||
UpdateIconRegenerationNeeded(result, counter, muted, iconThemeName);
|
updateIconRegenerationNeeded(result, counter, muted, iconThemeName);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QIcon result;
|
QIcon result;
|
||||||
QIcon systemIcon;
|
QIcon systemIcon;
|
||||||
|
|
||||||
static const auto iconSizes = {
|
for (const auto iconSize : _iconSizes) {
|
||||||
16,
|
auto ¤tImageBack = _imageBack[iconSize];
|
||||||
22,
|
|
||||||
24,
|
|
||||||
32,
|
|
||||||
48,
|
|
||||||
};
|
|
||||||
|
|
||||||
static const auto dprSize = [](const QImage &image) {
|
|
||||||
return image.size() / image.devicePixelRatio();
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const auto iconSize : iconSizes) {
|
|
||||||
auto ¤tImageBack = TrayIconImageBack[iconSize];
|
|
||||||
const auto desiredSize = QSize(iconSize, iconSize);
|
const auto desiredSize = QSize(iconSize, iconSize);
|
||||||
|
|
||||||
if (currentImageBack.isNull()
|
if (currentImageBack.isNull()
|
||||||
|| iconThemeName != TrayIconThemeName
|
|| iconThemeName != _themeName
|
||||||
|| iconName != TrayIconName) {
|
|| iconName != _name) {
|
||||||
if (!iconName.isEmpty()) {
|
if (!iconName.isEmpty()) {
|
||||||
if (systemIcon.isNull()) {
|
if (systemIcon.isNull()) {
|
||||||
systemIcon = QIcon::fromTheme(iconName);
|
systemIcon = QIcon::fromTheme(iconName);
|
||||||
|
@ -211,18 +243,11 @@ void UpdateIconRegenerationNeeded(
|
||||||
result.addPixmap(Ui::PixmapFromImage(std::move(iconImage)));
|
result.addPixmap(Ui::PixmapFromImage(std::move(iconImage)));
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateIconRegenerationNeeded(result, counter, muted, iconThemeName);
|
updateIconRegenerationNeeded(result, counter, muted, iconThemeName);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] QWidget *Parent() {
|
|
||||||
Expects(Core::App().primaryWindow() != nullptr);
|
|
||||||
return Core::App().primaryWindow()->widget();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
class TrayEventFilter final : public QObject {
|
class TrayEventFilter final : public QObject {
|
||||||
public:
|
public:
|
||||||
TrayEventFilter(not_null<QObject*> parent);
|
TrayEventFilter(not_null<QObject*> parent);
|
||||||
|
@ -267,6 +292,10 @@ Tray::Tray() {
|
||||||
|
|
||||||
void Tray::createIcon() {
|
void Tray::createIcon() {
|
||||||
if (!_icon) {
|
if (!_icon) {
|
||||||
|
if (!_iconGraphic) {
|
||||||
|
_iconGraphic = std::make_unique<IconGraphic>();
|
||||||
|
}
|
||||||
|
|
||||||
const auto showXEmbed = [=] {
|
const auto showXEmbed = [=] {
|
||||||
_aboutToShowRequests.fire({});
|
_aboutToShowRequests.fire({});
|
||||||
InvokeQueued(_menuXEmbed.get(), [=] {
|
InvokeQueued(_menuXEmbed.get(), [=] {
|
||||||
|
@ -275,7 +304,7 @@ void Tray::createIcon() {
|
||||||
};
|
};
|
||||||
|
|
||||||
_icon = base::make_unique_q<QSystemTrayIcon>(Parent());
|
_icon = base::make_unique_q<QSystemTrayIcon>(Parent());
|
||||||
_icon->setIcon(TrayIconGen(
|
_icon->setIcon(_iconGraphic->trayIcon(
|
||||||
Core::App().unreadBadge(),
|
Core::App().unreadBadge(),
|
||||||
Core::App().unreadBadgeMuted()));
|
Core::App().unreadBadgeMuted()));
|
||||||
_icon->setToolTip(AppName.utf16());
|
_icon->setToolTip(AppName.utf16());
|
||||||
|
@ -313,14 +342,14 @@ void Tray::destroyIcon() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tray::updateIcon() {
|
void Tray::updateIcon() {
|
||||||
if (!_icon) {
|
if (!_icon || !_iconGraphic) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto counter = Core::App().unreadBadge();
|
const auto counter = Core::App().unreadBadge();
|
||||||
const auto muted = Core::App().unreadBadgeMuted();
|
const auto muted = Core::App().unreadBadgeMuted();
|
||||||
|
|
||||||
if (IsIconRegenerationNeeded(counter, muted, QIcon::themeName())) {
|
if (_iconGraphic->isRefreshNeeded(counter, muted, QIcon::themeName())) {
|
||||||
_icon->setIcon(TrayIconGen(counter, muted));
|
_icon->setIcon(_iconGraphic->trayIcon(counter, muted));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -393,4 +422,6 @@ rpl::lifetime &Tray::lifetime() {
|
||||||
return _lifetime;
|
return _lifetime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Tray::~Tray() = default;
|
||||||
|
|
||||||
} // namespace Platform
|
} // namespace Platform
|
||||||
|
|
|
@ -20,11 +20,13 @@ class QSystemTrayIcon;
|
||||||
|
|
||||||
namespace Platform {
|
namespace Platform {
|
||||||
|
|
||||||
|
class IconGraphic;
|
||||||
class TrayEventFilter;
|
class TrayEventFilter;
|
||||||
|
|
||||||
class Tray final {
|
class Tray final {
|
||||||
public:
|
public:
|
||||||
Tray();
|
Tray();
|
||||||
|
~Tray();
|
||||||
|
|
||||||
[[nodiscard]] rpl::producer<> aboutToShowRequests() const;
|
[[nodiscard]] rpl::producer<> aboutToShowRequests() const;
|
||||||
[[nodiscard]] rpl::producer<> showFromTrayRequests() const;
|
[[nodiscard]] rpl::producer<> showFromTrayRequests() const;
|
||||||
|
@ -47,6 +49,8 @@ public:
|
||||||
[[nodiscard]] rpl::lifetime &lifetime();
|
[[nodiscard]] rpl::lifetime &lifetime();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::unique_ptr<IconGraphic> _iconGraphic;
|
||||||
|
|
||||||
base::unique_qptr<QSystemTrayIcon> _icon;
|
base::unique_qptr<QSystemTrayIcon> _icon;
|
||||||
base::unique_qptr<QMenu> _menu;
|
base::unique_qptr<QMenu> _menu;
|
||||||
base::unique_qptr<Ui::PopupMenu> _menuXEmbed;
|
base::unique_qptr<Ui::PopupMenu> _menuXEmbed;
|
||||||
|
|
Loading…
Add table
Reference in a new issue