tray_linux.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "platform/linux/tray_linux.h"
  8. #include "base/invoke_queued.h"
  9. #include "base/qt_signal_producer.h"
  10. #include "base/platform/linux/base_linux_dbus_utilities.h"
  11. #include "core/application.h"
  12. #include "core/sandbox.h"
  13. #include "platform/platform_specific.h"
  14. #include "ui/ui_utility.h"
  15. #include "ui/widgets/popup_menu.h"
  16. #include "window/window_controller.h"
  17. #include "styles/style_window.h"
  18. #include <QtCore/QCoreApplication>
  19. #include <QtWidgets/QMenu>
  20. #include <QtWidgets/QSystemTrayIcon>
  21. #include <gio/gio.hpp>
  22. namespace Platform {
  23. namespace {
  24. using namespace gi::repository;
  25. [[nodiscard]] QString PanelIconName(int counter, bool muted) {
  26. return ApplicationIconName() + ((counter > 0)
  27. ? (muted
  28. ? u"-mute"_q
  29. : u"-attention"_q)
  30. : QString()) + u"-symbolic"_q;
  31. }
  32. } // namespace
  33. class IconGraphic final {
  34. public:
  35. explicit IconGraphic();
  36. ~IconGraphic();
  37. void updateState();
  38. [[nodiscard]] bool isRefreshNeeded() const;
  39. [[nodiscard]] QIcon trayIcon();
  40. private:
  41. struct State {
  42. QIcon systemIcon;
  43. QString iconThemeName;
  44. bool monochrome = false;
  45. int32 counter = 0;
  46. bool muted = false;
  47. };
  48. [[nodiscard]] QIcon systemIcon() const;
  49. [[nodiscard]] bool isCounterNeeded(const State &state) const;
  50. [[nodiscard]] int counterSlice(int counter) const;
  51. [[nodiscard]] QSize dprSize(const QImage &image) const;
  52. const int _iconSizes[7];
  53. base::flat_map<int, QImage> _imageBack;
  54. QIcon _trayIcon;
  55. State _current;
  56. State _new;
  57. };
  58. IconGraphic::IconGraphic()
  59. : _iconSizes{ 16, 22, 32, 48, 64, 128, 256 } {
  60. updateState();
  61. }
  62. IconGraphic::~IconGraphic() = default;
  63. QIcon IconGraphic::systemIcon() const {
  64. if (_new.iconThemeName == _current.iconThemeName
  65. && _new.monochrome == _current.monochrome
  66. && (_new.counter > 0) == (_current.counter > 0)
  67. && _new.muted == _current.muted) {
  68. return _current.systemIcon;
  69. }
  70. const auto candidates = {
  71. _new.monochrome ? PanelIconName(_new.counter, _new.muted) : QString(),
  72. ApplicationIconName(),
  73. };
  74. for (const auto &candidate : candidates) {
  75. if (candidate.isEmpty()) {
  76. continue;
  77. }
  78. const auto icon = QIcon::fromTheme(candidate);
  79. if (icon.name() == candidate) {
  80. return icon;
  81. }
  82. }
  83. return QIcon();
  84. }
  85. bool IconGraphic::isCounterNeeded(const State &state) const {
  86. return state.systemIcon.name() != PanelIconName(
  87. state.counter,
  88. state.muted);
  89. }
  90. int IconGraphic::counterSlice(int counter) const {
  91. return (counter >= 100)
  92. ? (100 + (counter % 10))
  93. : counter;
  94. }
  95. QSize IconGraphic::dprSize(const QImage &image) const {
  96. return image.size() / image.devicePixelRatio();
  97. }
  98. void IconGraphic::updateState() {
  99. _new.iconThemeName = QIcon::themeName();
  100. _new.monochrome = Core::App().settings().trayIconMonochrome();
  101. _new.counter = Core::App().unreadBadge();
  102. _new.muted = Core::App().unreadBadgeMuted();
  103. _new.systemIcon = systemIcon();
  104. }
  105. bool IconGraphic::isRefreshNeeded() const {
  106. return _trayIcon.isNull()
  107. || _new.iconThemeName != _current.iconThemeName
  108. || _new.systemIcon.name() != _current.systemIcon.name()
  109. || (isCounterNeeded(_new)
  110. ? _new.muted != _current.muted
  111. || counterSlice(_new.counter) != counterSlice(
  112. _current.counter)
  113. : false);
  114. }
  115. QIcon IconGraphic::trayIcon() {
  116. if (!isRefreshNeeded()) {
  117. return _trayIcon;
  118. }
  119. const auto guard = gsl::finally([&] {
  120. _current = _new;
  121. });
  122. if (!isCounterNeeded(_new)) {
  123. _trayIcon = _new.systemIcon;
  124. return _trayIcon;
  125. }
  126. QIcon result;
  127. for (const auto iconSize : _iconSizes) {
  128. auto &currentImageBack = _imageBack[iconSize];
  129. const auto desiredSize = QSize(iconSize, iconSize);
  130. if (currentImageBack.isNull()
  131. || _new.iconThemeName != _current.iconThemeName
  132. || _new.systemIcon.name() != _current.systemIcon.name()) {
  133. if (!_new.systemIcon.isNull()) {
  134. // We can't use QIcon::actualSize here
  135. // since it works incorrectly with svg icon themes
  136. currentImageBack = _new.systemIcon
  137. .pixmap(desiredSize)
  138. .toImage();
  139. const auto firstAttemptSize = dprSize(currentImageBack);
  140. // if current icon theme is not a svg one, Qt can return
  141. // a pixmap that less in size even if there are a bigger one
  142. if (firstAttemptSize.width() < desiredSize.width()) {
  143. const auto availableSizes
  144. = _new.systemIcon.availableSizes();
  145. const auto biggestSize = ranges::max_element(
  146. availableSizes,
  147. std::less<>(),
  148. &QSize::width);
  149. if (biggestSize->width() > firstAttemptSize.width()) {
  150. currentImageBack = _new.systemIcon
  151. .pixmap(*biggestSize)
  152. .toImage();
  153. }
  154. }
  155. } else {
  156. currentImageBack = Window::Logo();
  157. }
  158. if (dprSize(currentImageBack) != desiredSize) {
  159. currentImageBack = currentImageBack.scaled(
  160. desiredSize * currentImageBack.devicePixelRatio(),
  161. Qt::IgnoreAspectRatio,
  162. Qt::SmoothTransformation);
  163. }
  164. }
  165. result.addPixmap(Ui::PixmapFromImage(_new.counter > 0
  166. ? Window::WithSmallCounter(std::move(currentImageBack), {
  167. .size = iconSize,
  168. .count = _new.counter,
  169. .bg = _new.muted ? st::trayCounterBgMute : st::trayCounterBg,
  170. .fg = st::trayCounterFg,
  171. }) : std::move(currentImageBack)));
  172. }
  173. _trayIcon = result;
  174. return _trayIcon;
  175. }
  176. class TrayEventFilter final : public QObject {
  177. public:
  178. TrayEventFilter(not_null<QObject*> parent);
  179. [[nodiscard]] rpl::producer<> contextMenuFilters() const;
  180. protected:
  181. bool eventFilter(QObject *watched, QEvent *event) override;
  182. private:
  183. const QString _iconObjectName;
  184. rpl::event_stream<> _contextMenuFilters;
  185. };
  186. TrayEventFilter::TrayEventFilter(not_null<QObject*> parent)
  187. : QObject(parent)
  188. , _iconObjectName("QSystemTrayIconSys") {
  189. parent->installEventFilter(this);
  190. }
  191. bool TrayEventFilter::eventFilter(QObject *obj, QEvent *event) {
  192. if (event->type() == QEvent::MouseButtonPress
  193. && obj->objectName() == _iconObjectName) {
  194. const auto m = static_cast<QMouseEvent*>(event);
  195. if (m->button() == Qt::RightButton) {
  196. Core::Sandbox::Instance().customEnterFromEventLoop([&] {
  197. _contextMenuFilters.fire({});
  198. });
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. rpl::producer<> TrayEventFilter::contextMenuFilters() const {
  205. return _contextMenuFilters.events();
  206. }
  207. Tray::Tray() {
  208. auto connection = Gio::bus_get_sync(Gio::BusType::SESSION_, nullptr);
  209. if (connection) {
  210. _sniWatcher = std::make_unique<base::Platform::DBus::ServiceWatcher>(
  211. connection.gobj_(),
  212. "org.kde.StatusNotifierWatcher",
  213. [=](
  214. const std::string &service,
  215. const std::string &oldOwner,
  216. const std::string &newOwner) {
  217. Core::Sandbox::Instance().customEnterFromEventLoop([&] {
  218. if (hasIcon()) {
  219. destroyIcon();
  220. createIcon();
  221. }
  222. });
  223. });
  224. }
  225. }
  226. void Tray::createIcon() {
  227. if (!_icon) {
  228. LOG(("System tray available: %1").arg(Logs::b(TrayIconSupported())));
  229. if (!_iconGraphic) {
  230. _iconGraphic = std::make_unique<IconGraphic>();
  231. }
  232. const auto showCustom = [=] {
  233. _aboutToShowRequests.fire({});
  234. InvokeQueued(_menuCustom.get(), [=] {
  235. _menuCustom->popup(QCursor::pos());
  236. });
  237. };
  238. _icon = base::make_unique_q<QSystemTrayIcon>(nullptr);
  239. _icon->setIcon(_iconGraphic->trayIcon());
  240. _icon->setToolTip(AppName.utf16());
  241. using Reason = QSystemTrayIcon::ActivationReason;
  242. base::qt_signal_producer(
  243. _icon.get(),
  244. &QSystemTrayIcon::activated
  245. ) | rpl::start_with_next([=](Reason reason) {
  246. if (reason == QSystemTrayIcon::Context) {
  247. showCustom();
  248. } else {
  249. _iconClicks.fire({});
  250. }
  251. }, _lifetime);
  252. _icon->setContextMenu(_menu.get());
  253. if (!_eventFilter) {
  254. _eventFilter = base::make_unique_q<TrayEventFilter>(
  255. QCoreApplication::instance());
  256. _eventFilter->contextMenuFilters(
  257. ) | rpl::start_with_next([=] {
  258. showCustom();
  259. }, _lifetime);
  260. }
  261. }
  262. updateIcon();
  263. _icon->show();
  264. }
  265. void Tray::destroyIcon() {
  266. _icon = nullptr;
  267. }
  268. void Tray::updateIcon() {
  269. if (!_icon || !_iconGraphic) {
  270. return;
  271. }
  272. _iconGraphic->updateState();
  273. if (_iconGraphic->isRefreshNeeded()) {
  274. _icon->setIcon(_iconGraphic->trayIcon());
  275. }
  276. }
  277. void Tray::createMenu() {
  278. if (!_menu) {
  279. _menu = base::make_unique_q<QMenu>(nullptr);
  280. }
  281. if (!_menuCustom) {
  282. _menuCustom = base::make_unique_q<Ui::PopupMenu>(nullptr);
  283. _menuCustom->deleteOnHide(false);
  284. }
  285. }
  286. void Tray::destroyMenu() {
  287. _menuCustom = nullptr;
  288. if (_menu) {
  289. _menu->clear();
  290. }
  291. _actionsLifetime.destroy();
  292. }
  293. void Tray::addAction(rpl::producer<QString> text, Fn<void()> &&callback) {
  294. if (_menuCustom) {
  295. const auto action = _menuCustom->addAction(QString(), callback);
  296. rpl::duplicate(
  297. text
  298. ) | rpl::start_with_next([=](const QString &text) {
  299. action->setText(text);
  300. }, _actionsLifetime);
  301. }
  302. if (_menu) {
  303. const auto action = _menu->addAction(QString(), std::move(callback));
  304. std::move(
  305. text
  306. ) | rpl::start_with_next([=](const QString &text) {
  307. action->setText(text);
  308. }, _actionsLifetime);
  309. }
  310. }
  311. void Tray::showTrayMessage() const {
  312. }
  313. bool Tray::hasTrayMessageSupport() const {
  314. return false;
  315. }
  316. rpl::producer<> Tray::aboutToShowRequests() const {
  317. return rpl::merge(
  318. _aboutToShowRequests.events(),
  319. _menu
  320. ? base::qt_signal_producer(_menu.get(), &QMenu::aboutToShow)
  321. : rpl::never<>() | rpl::type_erased());
  322. }
  323. rpl::producer<> Tray::showFromTrayRequests() const {
  324. return rpl::never<>();
  325. }
  326. rpl::producer<> Tray::hideToTrayRequests() const {
  327. return rpl::never<>();
  328. }
  329. rpl::producer<> Tray::iconClicks() const {
  330. return _iconClicks.events();
  331. }
  332. bool Tray::hasIcon() const {
  333. return _icon;
  334. }
  335. rpl::lifetime &Tray::lifetime() {
  336. return _lifetime;
  337. }
  338. Tray::~Tray() = default;
  339. bool HasMonochromeSetting() {
  340. return QIcon::hasThemeIcon(
  341. PanelIconName(
  342. Core::App().unreadBadge(),
  343. Core::App().unreadBadgeMuted()));
  344. }
  345. } // namespace Platform