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

View File

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