Optimize icon regeneration

This commit is contained in:
Ilya Fedin 2020-03-05 23:46:46 +04:00 committed by John Preston
parent 7202ffca76
commit 3fb6bbeae4
2 changed files with 142 additions and 110 deletions

View File

@ -71,32 +71,57 @@ QString GetTrayIconName(int counter, bool muted) {
return QString(); return QString();
} }
QIcon TrayIconGen(int counter, bool muted) { int GetCounterSlice(int counter) {
const auto iconThemeName = QIcon::themeName(); return (counter >= 1000)
const auto iconName = GetTrayIconName(counter, muted); ? (1000 + (counter % 100))
: counter;
}
if (qEnvironmentVariableIsSet(kDisableTrayCounter.utf8()) bool IsIconRegenerationNeeded(
&& !iconName.isEmpty()) { int counter,
if (TrayIcon.isNull() bool muted,
const QString &iconThemeName = QIcon::themeName()) {
const auto iconName = GetTrayIconName(counter, muted);
const auto counterSlice = GetCounterSlice(counter);
return TrayIcon.isNull()
|| iconThemeName != TrayIconThemeName || iconThemeName != TrayIconThemeName
|| iconName != TrayIconName) { || iconName != TrayIconName
TrayIcon = QIcon::fromTheme(iconName); || muted != TrayIconMuted
|| counterSlice != TrayIconCount;
}
void UpdateIconRegenerationNeeded(
const QIcon &icon,
int counter,
bool muted,
const QString &iconThemeName) {
const auto iconName = GetTrayIconName(counter, muted);
const auto counterSlice = GetCounterSlice(counter);
TrayIcon = icon;
TrayIconMuted = muted;
TrayIconCount = counterSlice;
TrayIconThemeName = iconThemeName; TrayIconThemeName = iconThemeName;
TrayIconName = iconName; TrayIconName = iconName;
} }
QIcon TrayIconGen(int counter, bool muted) {
const auto iconThemeName = QIcon::themeName();
if (!IsIconRegenerationNeeded(counter, muted, iconThemeName)) {
return TrayIcon; return TrayIcon;
} }
const auto counterSlice = (counter >= 1000) const auto iconName = GetTrayIconName(counter, muted);
? (1000 + (counter % 100))
: counter; if (qEnvironmentVariableIsSet(kDisableTrayCounter.utf8())
&& !iconName.isEmpty()) {
const auto result = QIcon::fromTheme(iconName);
UpdateIconRegenerationNeeded(result, counter, muted, iconThemeName);
return result;
}
if (TrayIcon.isNull()
|| iconThemeName != TrayIconThemeName
|| iconName != TrayIconName
|| muted != TrayIconMuted
|| counterSlice != TrayIconCount) {
QIcon result; QIcon result;
QIcon systemIcon; QIcon systemIcon;
@ -151,10 +176,6 @@ QIcon TrayIconGen(int counter, bool muted) {
} }
auto iconImage = currentImageBack; auto iconImage = currentImageBack;
TrayIconMuted = muted;
TrayIconCount = counterSlice;
TrayIconThemeName = iconThemeName;
TrayIconName = iconName;
if (!qEnvironmentVariableIsSet(kDisableTrayCounter.utf8()) if (!qEnvironmentVariableIsSet(kDisableTrayCounter.utf8())
&& counter > 0) { && counter > 0) {
@ -192,10 +213,9 @@ QIcon TrayIconGen(int counter, bool muted) {
std::move(iconImage))); std::move(iconImage)));
} }
TrayIcon = result; UpdateIconRegenerationNeeded(result, counter, muted, iconThemeName);
}
return TrayIcon; return result;
} }
#ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION #ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION
@ -332,14 +352,22 @@ void MainWindow::psTrayMenuUpdated() {
} }
#ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION #ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION
void MainWindow::setSNITrayIcon(int counter, bool muted) { void MainWindow::setSNITrayIcon(int counter, bool muted, bool firstShow) {
const auto iconName = GetTrayIconName(counter, muted); const auto iconName = GetTrayIconName(counter, muted);
if (qEnvironmentVariableIsSet(kDisableTrayCounter.utf8()) if (qEnvironmentVariableIsSet(kDisableTrayCounter.utf8())
&& !iconName.isEmpty()) { && !iconName.isEmpty()) {
if (_sniTrayIcon->iconName() == iconName) {
return;
}
_sniTrayIcon->setIconByName(iconName); _sniTrayIcon->setIconByName(iconName);
_sniTrayIcon->setToolTipIconByName(iconName); _sniTrayIcon->setToolTipIconByName(iconName);
} else if (IsIndicatorApplication()) { } else if (IsIndicatorApplication()) {
if(!IsIconRegenerationNeeded(counter, muted) && !firstShow) {
return;
}
const auto icon = TrayIconGen(counter, muted); const auto icon = TrayIconGen(counter, muted);
_trayIconFile = TrayIconFile(icon, 22, this); _trayIconFile = TrayIconFile(icon, 22, this);
@ -348,6 +376,10 @@ void MainWindow::setSNITrayIcon(int counter, bool muted) {
_sniTrayIcon->setIconByName(_trayIconFile->fileName()); _sniTrayIcon->setIconByName(_trayIconFile->fileName());
} }
} else { } else {
if(!IsIconRegenerationNeeded(counter, muted) && !firstShow) {
return;
}
const auto icon = TrayIconGen(counter, muted); const auto icon = TrayIconGen(counter, muted);
_sniTrayIcon->setIconByPixmap(icon); _sniTrayIcon->setIconByPixmap(icon);
_sniTrayIcon->setToolTipIconByPixmap(icon); _sniTrayIcon->setToolTipIconByPixmap(icon);
@ -389,7 +421,7 @@ void MainWindow::psSetupTrayIcon() {
this); this);
_sniTrayIcon->setTitle(AppName.utf16()); _sniTrayIcon->setTitle(AppName.utf16());
setSNITrayIcon(counter, muted); setSNITrayIcon(counter, muted, true);
attachToSNITrayIcon(); attachToSNITrayIcon();
} }
@ -476,7 +508,7 @@ void MainWindow::updateIconCounters() {
setSNITrayIcon(counter, muted); setSNITrayIcon(counter, muted);
} }
#endif // !TDESKTOP_DISABLE_DBUS_INTEGRATION #endif // !TDESKTOP_DISABLE_DBUS_INTEGRATION
} else if (trayIcon) { } else if (trayIcon && IsIconRegenerationNeeded(counter, muted)) {
trayIcon->setIcon(TrayIconGen(counter, muted)); trayIcon->setIcon(TrayIconGen(counter, muted));
} }
} }

View File

@ -70,7 +70,7 @@ private:
StatusNotifierItem *_sniTrayIcon = nullptr; StatusNotifierItem *_sniTrayIcon = nullptr;
std::unique_ptr<QTemporaryFile> _trayIconFile = nullptr; std::unique_ptr<QTemporaryFile> _trayIconFile = nullptr;
void setSNITrayIcon(int counter, bool muted); void setSNITrayIcon(int counter, bool muted, bool firstShow = false);
void attachToSNITrayIcon(); void attachToSNITrayIcon();
#endif // !TDESKTOP_DISABLE_DBUS_INTEGRATION #endif // !TDESKTOP_DISABLE_DBUS_INTEGRATION