trayIconsManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // This file is part of the AppIndicator/KStatusNotifierItem GNOME Shell extension
  2. //
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. import Shell from 'gi://Shell';
  17. import * as Main from 'resource:///org/gnome/shell/ui/main.js';
  18. import * as Signals from 'resource:///org/gnome/shell/misc/signals.js';
  19. import * as IndicatorStatusIcon from './indicatorStatusIcon.js';
  20. import * as Util from './util.js';
  21. import * as SettingsManager from './settingsManager.js';
  22. let trayIconsManager;
  23. export class TrayIconsManager extends Signals.EventEmitter {
  24. static initialize() {
  25. if (!trayIconsManager)
  26. trayIconsManager = new TrayIconsManager();
  27. return trayIconsManager;
  28. }
  29. static destroy() {
  30. trayIconsManager.destroy();
  31. }
  32. constructor() {
  33. super();
  34. if (trayIconsManager)
  35. throw new Error('TrayIconsManager is already constructed');
  36. this._changedId = SettingsManager.getDefaultGSettings().connect(
  37. 'changed::legacy-tray-enabled', () => this._toggle());
  38. // On theme changed, need to update the bg color to match style,
  39. // This may not be required anymore on newer shell versions that use
  40. // ARGBA visuals.
  41. this._styleChangedID = Main.panel.connect('style-changed', () => {
  42. const panelBgColor = this._getPanelBgColor();
  43. const {bgColor} = this._tray ?? {bgColor: null};
  44. if (bgColor === panelBgColor || bgColor?.equal(panelBgColor))
  45. return;
  46. this._disable();
  47. this._toggle();
  48. });
  49. this._toggle();
  50. }
  51. _toggle() {
  52. if (SettingsManager.getDefaultGSettings().get_boolean('legacy-tray-enabled'))
  53. this._enable();
  54. else
  55. this._disable();
  56. }
  57. _getPanelBgColor() {
  58. return Main.panel?.get_parent()
  59. ? Main.panel.get_theme_node()?.get_background_color() : null;
  60. }
  61. _enable() {
  62. if (this._tray)
  63. return;
  64. this._tray = new Shell.TrayManager({bgColor: this._getPanelBgColor()});
  65. Util.connectSmart(this._tray, 'tray-icon-added', this, this.onTrayIconAdded);
  66. Util.connectSmart(this._tray, 'tray-icon-removed', this, this.onTrayIconRemoved);
  67. this._tray.manage_screen(Main.panel);
  68. }
  69. _disable() {
  70. if (!this._tray)
  71. return;
  72. IndicatorStatusIcon.getTrayIcons().forEach(i => i.destroy());
  73. this._tray.unmanage_screen();
  74. this._tray = null;
  75. }
  76. onTrayIconAdded(_tray, icon) {
  77. const trayIcon = new IndicatorStatusIcon.IndicatorStatusTrayIcon(icon);
  78. IndicatorStatusIcon.addIconToPanel(trayIcon);
  79. }
  80. onTrayIconRemoved(_tray, icon) {
  81. try {
  82. const [trayIcon] = IndicatorStatusIcon.getTrayIcons().filter(i => i.icon === icon);
  83. trayIcon.destroy();
  84. } catch (e) {
  85. Util.Logger.warning(`No icon container found for ${icon.title} (${icon})`);
  86. }
  87. }
  88. destroy() {
  89. this.emit('destroy');
  90. SettingsManager.getDefaultGSettings().disconnect(this._changedId);
  91. Main.panel.disconnect(this._styleChangedID);
  92. this._disable();
  93. trayIconsManager = null;
  94. }
  95. }