Commit Graph

8 Commits

Author SHA1 Message Date
Florian Müllner
797df4f52f extensions: Make ExtensionPreferences more flexible
Extensions must now export a class with a fillPreferencesWindow()
method in their prefs. That is less convenient for extensions
with simple preferences than the old buildPrefsWidget() hook, as
they must wrap their widget in page/group widgets.

Address this by adding a default fillPreferencesWindow() implementation
that calls a getPreferencesWidget() method and wraps it as necessary.

This is flexible enough to support different cases fairly conveniently,
from simple single-widget prefs over tweaking the window to complex
multi-page prefs:

```js
class SimplePreferences extends ExtensionPreferences {
    getPreferencesWidget() {
        return new SimplePrefsWidget();
    }
}

class TinkerPreferences extends ExtensionPreferences {
    getPreferencesWidget() {
        return new SimplePrefsWidget();
    }

    fillPreferencesWindow(window) {
        super.fillPreferencesWindow(window);

        window.set_default_size(123, 456);
    }
}

class FullPreferences extends ExtensionPreferences {
    fillPreferencesWindow(window) {
        const page1 = new GeneralPage();
        window.add(page1);

        const page2 = new FoobarPage();
        window.add(page2);
    }
}
```

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>
2023-07-30 10:29:44 +03:00
Florian Müllner
abdd1346da extensionBase: Stop injecting extensionManager
All extension lookups now happen through the dedicated
Extension/ExtensionPreferences classes, so the shared
code no longer has to access the extensionManager.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>
2023-07-30 10:29:44 +03:00
Florian Müllner
82237a398f extensions: Add static lookupByUUID() method
This allows the Extension/Preferences classes to provide
the UUID -> extension lookup without exposing the underlying
extension manager.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>
2023-07-30 10:29:44 +03:00
Florian Müllner
931ca5e4ab extensions: Replace exported gettext functions
Use the new defineTranslationFunctions() method from the previous
commit to create gettext functions for the module, instead of
re-exporting from the shared module.

It is now up to extension developers to use the more effective

```js
import {Extension} from 'etensions/extension.js';
const {gettext: _} =
    Extension.defineTranslationFunctions(import.meta.url);
```

or the more convenient

```js
import {Extension, gettext} from 'extensions/extension.js';
const _ = gettext;
```

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>
2023-07-30 10:29:44 +03:00
Florian Müllner
f59d523694 extensions: Add static defineTranslationFunctions() method
The method can be used to define a set of gettext functions that
call the corresponding method of an extension.

Those functions are very similar to the gettext functions we are
exporting, except that:

 - looking up the extension is delegated to the
   Extension/Preferences class
 - it is possible to avoid examining the stack
   when called with `import.meta.url`

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>
2023-07-30 10:29:44 +03:00
Florian Müllner
3e4fd4b67a extensions: Add Extension/Preferences base classes
Extensions now must export a class that conforms to a particular
interface both for the actual extension as well as for prefs:

enable()/disable() methods for the former, fillPreferencesWindow()
for the latter.

This is quite similar to the previous method-based entry points,
but it also gives us a more structured way of providing convenience
API in form of base classes.

Do that in form of Extension and ExtensionPreferences classes on
top of a common ExtensionBase base class.

getSettings(), initTranslations() and the gettext wrappers are
now methods of the common base, while openPreferences() moves
to the Extension class.

Based on an original suggestion from Evan Welsh.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>
2023-07-29 15:20:20 +03:00
Florian Müllner
1354d2cf56 extensions: Unify how manager is injected into shared module
We unified most code paths earlier, but the common code will still
import Main locally if no extension manager was injected before.

Now that the old extensionUtils was split between extension and
preferences, each of those modules can simply import the manager
from its corresponding environment, and then inject it into the
shared module.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2837>
2023-07-15 13:16:42 +02:00
Florian Müllner
6a46d338e7 extensions: Provide separate implementations
For the time being this mostly means re-exporting functions
from the shared module. However openPrefs() is now only
available to extensions, and we stop exporting both
getCurrentExtension() and setExtensionManager() to either
extensions or prefs.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2837>
2023-07-15 13:16:42 +02:00