From e7eab279bbe2cfaeb59a7068808166ac7c5c046d Mon Sep 17 00:00:00 2001 From: Qwreey Date: Sat, 12 Nov 2022 21:15:56 +0900 Subject: [PATCH] Better extension loading system --- extension.js | 87 ++++++++++++++++++++------------------ features/buttonRemover.js | 4 +- features/dateMenu.js | 4 -- features/dndQuickToggle.js | 4 -- features/inputOutput.js | 4 -- features/notifications.js | 4 -- features/volumeMixer.js | 4 -- 7 files changed, 46 insertions(+), 65 deletions(-) diff --git a/extension.js b/extension.js index ba2dcef..b8d8ed1 100644 --- a/extension.js +++ b/extension.js @@ -3,51 +3,54 @@ const Me = ExtensionUtils.getCurrentExtension() const Features = Me.imports.features const { logger } = Me.imports.libs.utility const { GLib } = imports.gi -var loaded -var timeout -// handling extension -function enable() { - logger("Loading ...") - - let settings = ExtensionUtils.getSettings(Me.metadata['settings-schema']) - ExtensionUtils.initTranslations() - - // load features - loaded = [ - new Features.dndQuickToggle.dndQuickToggleFeature(settings), - new Features.notifications.notificationsFeature(settings), - new Features.volumeMixer.volumeMixerFeature(settings), - new Features.dateMenu.dateMenuFeature(settings), - new Features.buttonRemover.buttonRemoverFeature(settings), - new Features.inputOutput.inputOutputFeature(settings) - ] - - // Add timeout for waitting other extensions such as GSConnect - // This is necessary behavior due to ordering qs panel - timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 400, () => { - for (const feature of loaded) { - logger(`Loading feature '${feature.constructor.name}'`) - feature.load() +class Extension { + constructor() { + logger("Init") + this.features = [ + new Features.dndQuickToggle.dndQuickToggleFeature(), + new Features.notifications.notificationsFeature(), + new Features.volumeMixer.volumeMixerFeature(), + new Features.dateMenu.dateMenuFeature(), + new Features.buttonRemover.buttonRemoverFeature(), + new Features.inputOutput.inputOutputFeature() + ] + } + disable() { + logger("Unloading ...") + + if (this.timeout) { + GLib.Source.remove(this.timeout) + this.timeout = null } - logger("Loaded") - return GLib.SOURCE_REMOVE; - }); -} - -function disable() { - logger("Unloading ...") - - if (timeout) { - GLib.Source.remove(timeout) - timeout = null + for (const feature of this.features) { + logger(`Unload feature '${feature.constructor.name}'`) + feature.unload() + feature.settings = null + } + + logger("Diabled") } - if (!loaded) return - for (const feature of loaded) { - logger(`Unload feature '${feature.constructor.name}'`) - feature.unload() + enable() { + logger("Loading ...") + + let settings = ExtensionUtils.getSettings(Me.metadata['settings-schema']) + ExtensionUtils.initTranslations(Me.metadata['gettext-domain']) + + // Add timeout for waitting other extensions such as GSConnect + // This is necessary behavior due to ordering qs panel + this.timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 400, () => { + for (const feature of this.features) { + logger(`Loading feature '${feature.constructor.name}'`) + feature.settings = settings + feature.load() + } + logger("Loaded") + return GLib.SOURCE_REMOVE + }) } - loaded = null +} - logger("Diabled") +function init(meta) { + return new Extension() } diff --git a/features/buttonRemover.js b/features/buttonRemover.js index 2f8a7c5..b434b7d 100644 --- a/features/buttonRemover.js +++ b/features/buttonRemover.js @@ -9,8 +9,7 @@ const featureReloader = Me.imports.libs.featureReloader const { QuickSettingsGrid } = Me.imports.libs.gnome var buttonRemoverFeature = class { - constructor(settings) { - this.settings = settings + constructor() { this.removedItems = [] this.visibleListeners = [] } @@ -70,6 +69,5 @@ var buttonRemoverFeature = class { unload() { this._unapply() this.settings.disconnect(this._removedItemsConnection) - this.settings = null } } diff --git a/features/dateMenu.js b/features/dateMenu.js index 585079a..3584110 100644 --- a/features/dateMenu.js +++ b/features/dateMenu.js @@ -12,10 +12,6 @@ const { const { Indicator } = Me.imports.libs.dndQuickToogleHandler var dateMenuFeature = class { - constructor(settings) { - this.settings = settings - } - load() { // setup reloader featureReloader.enableWithSettingKeys(this,[ diff --git a/features/dndQuickToggle.js b/features/dndQuickToggle.js index cab2a66..d975fbd 100644 --- a/features/dndQuickToggle.js +++ b/features/dndQuickToggle.js @@ -6,10 +6,6 @@ const { QuickSettings } = Me.imports.libs.gnome const { Indicator } = Me.imports.libs.dndQuickToogleHandler var dndQuickToggleFeature = class { - constructor(settings) { - this.settings = settings - } - load() { // setup reloader featureReloader.enableWithSettingKeys(this,[ diff --git a/features/inputOutput.js b/features/inputOutput.js index a3102f9..d8115b2 100644 --- a/features/inputOutput.js +++ b/features/inputOutput.js @@ -9,10 +9,6 @@ const Volume = imports.ui.status.volume const PopupMenu = imports.ui.popupMenu; var inputOutputFeature = class { - constructor(settings) { - this.settings = settings - } - load() { // setup reloader featureReloader.enableWithSettingKeys(this,[ diff --git a/features/notifications.js b/features/notifications.js index 1c8cb1e..c366e0a 100644 --- a/features/notifications.js +++ b/features/notifications.js @@ -12,10 +12,6 @@ const { } = Me.imports.libs.gnome var notificationsFeature = class { - constructor(settings) { - this.settings = settings - } - load() { let settings = this.settings diff --git a/features/volumeMixer.js b/features/volumeMixer.js index da81127..d2ebac4 100644 --- a/features/volumeMixer.js +++ b/features/volumeMixer.js @@ -7,10 +7,6 @@ const { QuickSettingsGrid } = Me.imports.libs.gnome const { addChildWithIndex } = Me.imports.libs.utility var volumeMixerFeature = class { - constructor(settings) { - this.settings = settings - } - load() { let settings = this.settings