Fix macOS tray icon on Big Sur & Qt 5.15.1.
This commit is contained in:
		
							parent
							
								
									117de5a1f9
								
							
						
					
					
						commit
						b1e2beba2c
					
				
					 2 changed files with 39 additions and 29 deletions
				
			
		| 
						 | 
					@ -65,7 +65,6 @@ protected:
 | 
				
			||||||
	void titleVisibilityChangedHook() override;
 | 
						void titleVisibilityChangedHook() override;
 | 
				
			||||||
	void unreadCounterChangedHook() override;
 | 
						void unreadCounterChangedHook() override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QImage psTrayIcon(bool selected = false) const;
 | 
					 | 
				
			||||||
	bool hasTrayIcon() const override {
 | 
						bool hasTrayIcon() const override {
 | 
				
			||||||
		return trayIcon;
 | 
							return trayIcon;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -77,8 +76,6 @@ protected:
 | 
				
			||||||
	QSystemTrayIcon *trayIcon = nullptr;
 | 
						QSystemTrayIcon *trayIcon = nullptr;
 | 
				
			||||||
	QMenu *trayIconMenu = nullptr;
 | 
						QMenu *trayIconMenu = nullptr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QImage trayImg, trayImgSel;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	void psTrayMenuUpdated();
 | 
						void psTrayMenuUpdated();
 | 
				
			||||||
	void psSetupTrayIcon();
 | 
						void psSetupTrayIcon();
 | 
				
			||||||
	virtual void placeSmallCounter(QImage &img, int size, int count, style::color bg, const QPoint &shift, style::color color) = 0;
 | 
						virtual void placeSmallCounter(QImage &img, int size, int count, style::color bg, const QPoint &shift, style::color color) = 0;
 | 
				
			||||||
| 
						 | 
					@ -95,6 +92,7 @@ private:
 | 
				
			||||||
	void hideAndDeactivate();
 | 
						void hideAndDeactivate();
 | 
				
			||||||
	void updateTitleCounter();
 | 
						void updateTitleCounter();
 | 
				
			||||||
	void updateIconCounters();
 | 
						void updateIconCounters();
 | 
				
			||||||
 | 
						[[nodiscard]] QIcon generateIconForTray(int counter, bool muted) const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	std::unique_ptr<Private> _private;
 | 
						std::unique_ptr<Private> _private;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -124,6 +124,17 @@ private:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[[nodiscard]] QImage TrayIconBack(bool darkMode) {
 | 
				
			||||||
 | 
						static const auto WithColor = [](QColor color) {
 | 
				
			||||||
 | 
							return st::macTrayIcon.instance(color, 100);
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
						static const auto DarkModeResult = WithColor({ 255, 255, 255 });
 | 
				
			||||||
 | 
						static const auto LightModeResult = WithColor({ 0, 0, 0, 180 });
 | 
				
			||||||
 | 
						auto result = darkMode ? DarkModeResult : LightModeResult;
 | 
				
			||||||
 | 
						result.detach();
 | 
				
			||||||
 | 
						return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
} // namespace
 | 
					} // namespace
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MainWindow::Private {
 | 
					class MainWindow::Private {
 | 
				
			||||||
| 
						 | 
					@ -200,7 +211,6 @@ private:
 | 
				
			||||||
- (void) darkModeChanged:(NSNotification *)aNotification {
 | 
					- (void) darkModeChanged:(NSNotification *)aNotification {
 | 
				
			||||||
	Core::Sandbox::Instance().customEnterFromEventLoop([&] {
 | 
						Core::Sandbox::Instance().customEnterFromEventLoop([&] {
 | 
				
			||||||
		Core::App().settings().setSystemDarkMode(Platform::IsDarkMode());
 | 
							Core::App().settings().setSystemDarkMode(Platform::IsDarkMode());
 | 
				
			||||||
		Core::App().domain().notifyUnreadBadgeChanged();
 | 
					 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -485,9 +495,6 @@ MainWindow::MainWindow(not_null<Window::Controller*> controller)
 | 
				
			||||||
	auto forceOpenGL = std::make_unique<QOpenGLWidget>(this);
 | 
						auto forceOpenGL = std::make_unique<QOpenGLWidget>(this);
 | 
				
			||||||
#endif // !OS_MAC_OLD
 | 
					#endif // !OS_MAC_OLD
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	trayImg = st::macTrayIcon.instance(QColor(0, 0, 0, 180), 100);
 | 
					 | 
				
			||||||
	trayImgSel = st::macTrayIcon.instance(QColor(255, 255, 255), 100);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	_hideAfterFullScreenTimer.setCallback([this] { hideAndDeactivate(); });
 | 
						_hideAfterFullScreenTimer.setCallback([this] { hideAndDeactivate(); });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	subscribe(Window::Theme::Background(), [this](const Window::Theme::BackgroundUpdate &data) {
 | 
						subscribe(Window::Theme::Background(), [this](const Window::Theme::BackgroundUpdate &data) {
 | 
				
			||||||
| 
						 | 
					@ -543,10 +550,6 @@ void MainWindow::hideAndDeactivate() {
 | 
				
			||||||
	hide();
 | 
						hide();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
QImage MainWindow::psTrayIcon(bool selected) const {
 | 
					 | 
				
			||||||
	return selected ? trayImgSel : trayImg;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void MainWindow::psShowTrayMenu() {
 | 
					void MainWindow::psShowTrayMenu() {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -566,14 +569,13 @@ void MainWindow::psTrayMenuUpdated() {
 | 
				
			||||||
void MainWindow::psSetupTrayIcon() {
 | 
					void MainWindow::psSetupTrayIcon() {
 | 
				
			||||||
	if (!trayIcon) {
 | 
						if (!trayIcon) {
 | 
				
			||||||
		trayIcon = new QSystemTrayIcon(this);
 | 
							trayIcon = new QSystemTrayIcon(this);
 | 
				
			||||||
 | 
							trayIcon->setIcon(generateIconForTray(
 | 
				
			||||||
		QIcon icon(QPixmap::fromImage(psTrayIcon(), Qt::ColorOnly));
 | 
								Core::App().unreadBadge(),
 | 
				
			||||||
		icon.addPixmap(QPixmap::fromImage(psTrayIcon(true), Qt::ColorOnly), QIcon::Selected);
 | 
								Core::App().unreadBadgeMuted()));
 | 
				
			||||||
 | 
					 | 
				
			||||||
		trayIcon->setIcon(icon);
 | 
					 | 
				
			||||||
		attachToTrayIcon(trayIcon);
 | 
							attachToTrayIcon(trayIcon);
 | 
				
			||||||
	}
 | 
						} else {
 | 
				
			||||||
		updateIconCounters();
 | 
							updateIconCounters();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	trayIcon->show();
 | 
						trayIcon->show();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -651,21 +653,31 @@ void MainWindow::updateIconCounters() {
 | 
				
			||||||
	_private->setWindowBadge(string);
 | 
						_private->setWindowBadge(string);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (trayIcon) {
 | 
						if (trayIcon) {
 | 
				
			||||||
		bool dm = Platform::IsDarkMenuBar();
 | 
							trayIcon->setIcon(generateIconForTray(counter, muted));
 | 
				
			||||||
		auto &bg = (muted ? st::trayCounterBgMute : st::trayCounterBg);
 | 
					 | 
				
			||||||
		QIcon icon;
 | 
					 | 
				
			||||||
		QImage img(psTrayIcon(dm)), imgsel(psTrayIcon(true));
 | 
					 | 
				
			||||||
		img.detach();
 | 
					 | 
				
			||||||
		imgsel.detach();
 | 
					 | 
				
			||||||
		int32 size = 22 * cIntRetinaFactor();
 | 
					 | 
				
			||||||
		_placeCounter(img, size, counter, bg, (dm && muted) ? st::trayCounterFgMacInvert : st::trayCounterFg);
 | 
					 | 
				
			||||||
		_placeCounter(imgsel, size, counter, st::trayCounterBgMacInvert, st::trayCounterFgMacInvert);
 | 
					 | 
				
			||||||
		icon.addPixmap(App::pixmapFromImageInPlace(std::move(img)));
 | 
					 | 
				
			||||||
		icon.addPixmap(App::pixmapFromImageInPlace(std::move(imgsel)), QIcon::Selected);
 | 
					 | 
				
			||||||
		trayIcon->setIcon(icon);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					QIcon MainWindow::generateIconForTray(int counter, bool muted) const {
 | 
				
			||||||
 | 
						auto result = QIcon();
 | 
				
			||||||
 | 
						auto lightMode = TrayIconBack(false);
 | 
				
			||||||
 | 
						auto darkMode = TrayIconBack(true);
 | 
				
			||||||
 | 
						auto lightModeActive = darkMode;
 | 
				
			||||||
 | 
						auto darkModeActive = darkMode;
 | 
				
			||||||
 | 
						lightModeActive.detach();
 | 
				
			||||||
 | 
						darkModeActive.detach();
 | 
				
			||||||
 | 
						const auto size = 22 * cIntRetinaFactor();
 | 
				
			||||||
 | 
						const auto &bg = (muted ? st::trayCounterBgMute : st::trayCounterBg);
 | 
				
			||||||
 | 
						_placeCounter(lightMode, size, counter, bg, st::trayCounterFg);
 | 
				
			||||||
 | 
						_placeCounter(darkMode, size, counter, bg, muted ? st::trayCounterFgMacInvert : st::trayCounterFg);
 | 
				
			||||||
 | 
						_placeCounter(lightModeActive, size, counter, st::trayCounterBgMacInvert, st::trayCounterFgMacInvert);
 | 
				
			||||||
 | 
						_placeCounter(darkModeActive, size, counter, st::trayCounterBgMacInvert, st::trayCounterFgMacInvert);
 | 
				
			||||||
 | 
						result.addPixmap(App::pixmapFromImageInPlace(std::move(lightMode)), QIcon::Normal, QIcon::Off);
 | 
				
			||||||
 | 
						result.addPixmap(App::pixmapFromImageInPlace(std::move(darkMode)), QIcon::Normal, QIcon::On);
 | 
				
			||||||
 | 
						result.addPixmap(App::pixmapFromImageInPlace(std::move(lightModeActive)), QIcon::Active, QIcon::Off);
 | 
				
			||||||
 | 
						result.addPixmap(App::pixmapFromImageInPlace(std::move(darkModeActive)), QIcon::Active, QIcon::On);
 | 
				
			||||||
 | 
						return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void MainWindow::initShadows() {
 | 
					void MainWindow::initShadows() {
 | 
				
			||||||
	_private->enableShadow(winId());
 | 
						_private->enableShadow(winId());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue