2021-07-29 22:06:36 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2023-05-25 18:31:22 +00:00
|
|
|
import Adw from 'gi://Adw?version=1';
|
|
|
|
import Gdk from 'gi://Gdk?version=4.0';
|
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GLib from 'gi://GLib';
|
|
|
|
import GObject from 'gi://GObject';
|
|
|
|
import Gtk from 'gi://Gtk?version=4.0';
|
2021-07-29 22:06:36 +00:00
|
|
|
|
2023-05-25 18:31:22 +00:00
|
|
|
export const ExtensionPrefsDialog = GObject.registerClass({
|
2021-07-29 22:06:36 +00:00
|
|
|
GTypeName: 'ExtensionPrefsDialog',
|
2021-07-30 00:33:54 +00:00
|
|
|
}, class ExtensionPrefsDialog extends Adw.PreferencesWindow {
|
2021-07-29 22:26:05 +00:00
|
|
|
_init(extension) {
|
|
|
|
super._init({
|
|
|
|
title: extension.metadata.name,
|
2021-07-30 00:33:54 +00:00
|
|
|
search_enabled: false,
|
2021-07-29 22:26:05 +00:00
|
|
|
});
|
|
|
|
|
2023-07-09 10:47:05 +00:00
|
|
|
this._extension = extension;
|
|
|
|
|
|
|
|
this._loadPrefs().catch(e => {
|
2023-06-02 02:12:34 +00:00
|
|
|
this._showErrorPage(e);
|
|
|
|
logError(e, 'Failed to open preferences');
|
|
|
|
});
|
|
|
|
}
|
2021-07-29 22:26:05 +00:00
|
|
|
|
2023-07-09 10:47:05 +00:00
|
|
|
async _loadPrefs() {
|
2023-07-16 13:15:07 +00:00
|
|
|
const {dir, path, metadata} = this._extension;
|
2021-07-29 22:26:05 +00:00
|
|
|
|
2023-07-09 10:47:05 +00:00
|
|
|
const prefsJs = dir.get_child('prefs.js');
|
2023-06-02 02:12:34 +00:00
|
|
|
const prefsModule = await import(prefsJs.get_uri());
|
2021-07-29 22:26:05 +00:00
|
|
|
|
2023-07-16 13:15:07 +00:00
|
|
|
const prefsObj = new prefsModule.default({...metadata, dir, path});
|
2023-07-09 00:54:58 +00:00
|
|
|
this._extension.stateObj = prefsObj;
|
|
|
|
|
2023-06-02 02:12:34 +00:00
|
|
|
prefsObj.fillPreferencesWindow(this);
|
2021-07-30 08:12:11 +00:00
|
|
|
|
2023-06-02 02:12:34 +00:00
|
|
|
if (!this.visible_page)
|
|
|
|
throw new Error('Extension did not provide any UI');
|
2021-07-29 22:26:05 +00:00
|
|
|
}
|
2021-07-30 00:33:54 +00:00
|
|
|
|
|
|
|
set titlebar(w) {
|
|
|
|
this.set_titlebar(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
set_titlebar() {
|
|
|
|
// intercept fatal libadwaita error, show error page instead
|
|
|
|
GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
|
|
this._showErrorPage(
|
|
|
|
new Error('set_titlebar() is not supported for Adw.Window'));
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_showErrorPage(e) {
|
|
|
|
while (this.visible_page)
|
|
|
|
this.remove(this.visible_page);
|
|
|
|
|
2023-07-09 10:47:05 +00:00
|
|
|
this.add(new ExtensionPrefsErrorPage(this._extension, e));
|
2021-07-30 00:33:54 +00:00
|
|
|
}
|
2021-07-29 22:26:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const ExtensionPrefsErrorPage = GObject.registerClass({
|
|
|
|
GTypeName: 'ExtensionPrefsErrorPage',
|
|
|
|
Template: 'resource:///org/gnome/Shell/Extensions/ui/extension-error-page.ui',
|
2021-07-29 22:06:36 +00:00
|
|
|
InternalChildren: [
|
|
|
|
'expander',
|
|
|
|
'expanderArrow',
|
|
|
|
'revealer',
|
|
|
|
'errorView',
|
|
|
|
],
|
2021-07-30 00:33:54 +00:00
|
|
|
}, class ExtensionPrefsErrorPage extends Adw.PreferencesPage {
|
2021-07-29 22:58:18 +00:00
|
|
|
static _classInit(klass) {
|
|
|
|
super._classInit(klass);
|
|
|
|
|
|
|
|
klass.install_action('page.copy-error',
|
|
|
|
null,
|
|
|
|
self => {
|
|
|
|
const clipboard = self.get_display().get_clipboard();
|
|
|
|
clipboard.set(self._errorMarkdown);
|
|
|
|
});
|
|
|
|
klass.install_action('page.show-url',
|
|
|
|
null,
|
|
|
|
self => Gtk.show_uri(self.get_root(), self._url, Gdk.CURRENT_TIME));
|
|
|
|
|
|
|
|
return klass;
|
|
|
|
}
|
|
|
|
|
2021-07-29 22:26:05 +00:00
|
|
|
_init(extension, error) {
|
2021-07-30 00:33:54 +00:00
|
|
|
super._init();
|
|
|
|
|
2021-07-29 22:58:18 +00:00
|
|
|
this._addCustomStylesheet();
|
2021-07-29 22:06:36 +00:00
|
|
|
|
|
|
|
this._uuid = extension.uuid;
|
|
|
|
this._url = extension.metadata.url || '';
|
|
|
|
|
2021-07-29 22:58:18 +00:00
|
|
|
this.action_set_enabled('page.show-url', this._url !== '');
|
2021-07-29 22:06:36 +00:00
|
|
|
|
|
|
|
this._gesture = new Gtk.GestureClick({
|
|
|
|
button: 0,
|
|
|
|
exclusive: true,
|
|
|
|
});
|
|
|
|
this._expander.add_controller(this._gesture);
|
|
|
|
|
|
|
|
this._gesture.connect('released', (gesture, nPress) => {
|
|
|
|
if (nPress === 1)
|
|
|
|
this._revealer.reveal_child = !this._revealer.reveal_child;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._revealer.connect('notify::reveal-child', () => {
|
|
|
|
this._expanderArrow.icon_name = this._revealer.reveal_child
|
|
|
|
? 'pan-down-symbolic'
|
|
|
|
: 'pan-end-symbolic';
|
|
|
|
this._syncExpandedStyle();
|
|
|
|
});
|
|
|
|
this._revealer.connect('notify::child-revealed',
|
|
|
|
() => this._syncExpandedStyle());
|
|
|
|
|
2021-07-29 22:26:05 +00:00
|
|
|
this._errorView.buffer.text = `${error}\n\nStack trace:\n`;
|
2021-07-29 22:06:36 +00:00
|
|
|
// Indent stack trace.
|
|
|
|
this._errorView.buffer.text +=
|
2021-07-29 22:26:05 +00:00
|
|
|
error.stack.split('\n').map(line => ` ${line}`).join('\n');
|
2021-07-29 22:06:36 +00:00
|
|
|
|
|
|
|
// markdown for pasting in gitlab issues
|
|
|
|
let lines = [
|
|
|
|
`The settings of extension ${this._uuid} had an error:`,
|
|
|
|
'```',
|
2021-07-29 22:26:05 +00:00
|
|
|
`${error}`,
|
2021-07-29 22:06:36 +00:00
|
|
|
'```',
|
|
|
|
'',
|
|
|
|
'Stack trace:',
|
|
|
|
'```',
|
2021-07-29 22:26:05 +00:00
|
|
|
error.stack.replace(/\n$/, ''), // stack without trailing newline
|
2021-07-29 22:06:36 +00:00
|
|
|
'```',
|
|
|
|
'',
|
|
|
|
];
|
|
|
|
this._errorMarkdown = lines.join('\n');
|
2021-07-29 22:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_syncExpandedStyle() {
|
|
|
|
if (this._revealer.reveal_child)
|
|
|
|
this._expander.add_css_class('expanded');
|
|
|
|
else if (!this._revealer.child_revealed)
|
|
|
|
this._expander.remove_css_class('expanded');
|
2021-07-29 22:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_addCustomStylesheet() {
|
|
|
|
let provider = new Gtk.CssProvider();
|
|
|
|
let uri = 'resource:///org/gnome/Shell/Extensions/css/application.css';
|
|
|
|
try {
|
|
|
|
provider.load_from_file(Gio.File.new_for_uri(uri));
|
|
|
|
} catch (e) {
|
|
|
|
logError(e, 'Failed to add application style');
|
|
|
|
}
|
|
|
|
Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(),
|
|
|
|
provider,
|
|
|
|
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
|
|
}
|
|
|
|
});
|