extension.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This file is part of the Forge GNOME extension
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. // Gnome imports
  19. import * as Main from "resource:///org/gnome/shell/ui/main.js";
  20. import { Extension, gettext as _ } from "resource:///org/gnome/shell/extensions/extension.js";
  21. // Shared state
  22. import { Logger } from "./lib/shared/logger.js";
  23. import { ConfigManager } from "./lib/shared/settings.js";
  24. // Application imports
  25. import { Keybindings } from "./lib/extension/keybindings.js";
  26. import { WindowManager } from "./lib/extension/window.js";
  27. import { FeatureIndicator, FeatureMenuToggle } from "./lib/extension/indicator.js";
  28. import { ExtensionThemeManager } from "./lib/extension/extension-theme-manager.js";
  29. export default class ForgeExtension extends Extension {
  30. enable() {
  31. this.settings = this.getSettings();
  32. this.kbdSettings = this.getSettings("org.gnome.shell.extensions.forge.keybindings");
  33. Logger.init(this.settings);
  34. Logger.info("enable");
  35. this.configMgr = new ConfigManager(this);
  36. this.theme = new ExtensionThemeManager(this);
  37. this.extWm = new WindowManager(this);
  38. this.keybindings = new Keybindings(this);
  39. this._onSessionModeChanged(Main.sessionMode);
  40. this._sessionId = Main.sessionMode.connect("updated", this._onSessionModeChanged.bind(this));
  41. this.theme.patchCss();
  42. this.theme.reloadStylesheet();
  43. this.extWm.enable();
  44. Logger.info(`enable: finalized vars`);
  45. }
  46. disable() {
  47. Logger.info("disable");
  48. // See session mode unlock-dialog explanation on _onSessionModeChanged()
  49. if (this._sessionId) {
  50. Main.sessionMode.disconnect(this._sessionId);
  51. this._sessionId = null;
  52. }
  53. this._removeIndicator();
  54. this.extWm?.disable();
  55. this.keybindings?.disable();
  56. this.keybindings = null;
  57. this.extWm = null;
  58. this.themeWm = null;
  59. this.configMgr = null;
  60. this.settings = null;
  61. this.kbdSettings = null;
  62. }
  63. _onSessionModeChanged(session) {
  64. if (session.currentMode === "user" || session.parentMode === "user") {
  65. Logger.info("user on session change");
  66. this._addIndicator();
  67. this.keybindings?.enable();
  68. } else if (session.currentMode === "unlock-dialog") {
  69. // To the reviewer and maintainer: this extension needs to persist the window data structure in memory so it has to keep running on lock screen.
  70. // This is previous feature but was removed during GNOME 45 update due to the session-mode rule review.
  71. // The argument is that users will keep re-arranging windows when it times out or locks up.
  72. // Intent to serialize/deserialize to disk but that will take a longer time or probably a longer argument during review.
  73. // To keep following, added to only disable keybindings() and re-enable them during user session.
  74. // https://gjs.guide/extensions/review-guidelines/review-guidelines.html#session-modes
  75. Logger.info("lock-screen on session change");
  76. this.keybindings?.disable();
  77. this._removeIndicator();
  78. }
  79. }
  80. _addIndicator() {
  81. this.indicator ??= new FeatureIndicator(this);
  82. this.indicator.quickSettingsItems.push(new FeatureMenuToggle(this));
  83. Main.panel.statusArea.quickSettings.addExternalIndicator(this.indicator);
  84. }
  85. _removeIndicator() {
  86. this.indicator?.quickSettingsItems.forEach((item) => item.destroy());
  87. this.indicator?.destroy();
  88. this.indicator = null;
  89. }
  90. }