From 10242f32a55e87a6abb216ddf484f0a620b4911e Mon Sep 17 00:00:00 2001 From: Benjamin Drieu Date: Wed, 10 Nov 2021 15:52:40 +0100 Subject: [PATCH] Working prefs --- extension.js | 11 +-- prefs.js | 198 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 137 insertions(+), 72 deletions(-) diff --git a/extension.js b/extension.js index 8b54af1..880c9ab 100644 --- a/extension.js +++ b/extension.js @@ -101,7 +101,6 @@ class Indicator extends PanelMenu.Button { this._reloadButton = this._createButton ( 'view-refresh-symbolic', 'Reload', this.updateStatus ); this._buttonMenu.actor.add_child (this._reloadButton ); - let _intermediate = new PopupMenu.PopupBaseMenuItem ( { style_class: 'monito-services', reactive: false @@ -115,14 +114,6 @@ class Indicator extends PanelMenu.Button { }); _intermediate.actor.add_actor(this._box); -// let _bin = new St.Bin(); -// _intermediate.actor.add_actor(_bin); -// _bin.set_child(this._services_table); - - -// this._list = new FlatList() -// _bin.set_child(this._list); - this.updateStatus ( ); } @@ -136,7 +127,7 @@ class Indicator extends PanelMenu.Button { updateStatus ( ) { const SETTINGS_SCHEMA_ACCOUNT = "org.gnome.shell.extensions.monito@drieu.org.account"; const Convenience = Me.imports.convenience; - let account_settings = Convenience.getSettings(SETTINGS_SCHEMA_ACCOUNT, '/org/gnome/shell/extensions/monito@drieu/org/account/0'); + let account_settings = Convenience.getSettings(SETTINGS_SCHEMA_ACCOUNT, '/org/gnome/shell/extensions/monito@drieu.org/account/0/'); let username = account_settings.get_string("username"); let password = account_settings.get_string("password"); diff --git a/prefs.js b/prefs.js index 8b4d303..4294834 100644 --- a/prefs.js +++ b/prefs.js @@ -48,6 +48,7 @@ const prefs = [ { type: Gtk.Entry, label: _('Name'), key: 'name' }, { type: Gtk.Entry, label: _('URL CGI'), key: 'urlcgi' }, ]; + // Like 'extension.js' this is used for any one-time setup like translations. function init() { // log('initializing ${Me.metadata.name} Preferences'); @@ -88,72 +89,58 @@ function buildPrefsWidget() { // Accounts - let accountsWidgetContainer = new Gtk.Box( { orientation: Gtk.Orientation.HORIZONTAL, + this._accountsWidgetContainer = new Gtk.Box( { orientation: Gtk.Orientation.HORIZONTAL, homogeneous: false, } ); - mainWidget.append_page ( accountsWidgetContainer, + mainWidget.append_page ( this._accountsWidgetContainer, new Gtk.Label ( { label: 'Servers', } ) ); let accountsChooserContainer = new Gtk.Box( { orientation: Gtk.Orientation.VERTICAL, homogeneous: false, } ); - accountsWidgetContainer.pack_start(accountsChooserContainer, true, true, 0); + this._accountsWidgetContainer.pack_start(accountsChooserContainer, true, true, 0); - let accountsChooser = new Gtk.ListBox ( { valign: Gtk.Align.FILL, + this.accountsChooser = new Gtk.ListBox ( { valign: Gtk.Align.FILL, hexpand: true, vexpand: true } ); - accountsChooserContainer.pack_start(accountsChooser, true, true, 0); + accountsChooserContainer.pack_start(this.accountsChooser, true, true, 0); // Account list - for ( var server of this.settings.get_string ( 'servers' ) . split ( ',' ) ) - { - let _account_settings = getAccountSettings ( server ); - let row = new Gtk.ListBoxRow ( { hexpand: true, - halign: Gtk.Align.FILL } ); - row.server= server - let _label = new Gtk.Label ( { label: _account_settings.get_string('name'), - hexpand: true, - margin: 5, - halign: Gtk.Align.START, - expand: true } ); - row.add ( _label ); - _label.set_data ( 'server', GLib.strdup(server) ); - accountsChooser.add ( row ); - } - accountsChooser.connect('row-activated', Lang.bind(this, function () { - let _row = accountsChooser.get_selected_row(); - log('Active:' + _row.server); - let _account_settings = getAccountSettings ( _row.server ); + for ( var server_id of this.settings.get_string ( 'servers' ) . split ( ',' ) ) + this.addAccountLine ( server_id ); + this.accountsChooser.connect ( 'row-activated', Lang.bind ( this, this.activateAccountRow ) ); - for ( var prefEntry of prefs ) - { - if ( prefEntry.type == Gtk.Entry ) - { - // How to unbind previous one? - _account_settings.bind ( - prefEntry.key, - this.prefWidgets[prefEntry.key], - 'text', - Gio.SettingsBindFlags.DEFAULT - ); - } - else if ( prefEntry.type == Gtk.ComboBoxText ) - { - this.prefWidgets[prefEntry.key].set_active(_account_settings.get_enum('type')); - this.prefWidgets[prefEntry.key].connect('changed', Lang.bind(this, function () { - log('Active:' + this.prefWidgets[prefEntry.key].get_active()); - _account_settings.set_enum('type', this.prefWidgets[prefEntry.key].get_active()); - })); - } - } - } ) ); - - - - let accountsChooserActionBar = new Gtk.ActionBar ( { visible: true, - valign: Gtk.Align.END, - vexpand: false} ); + // Action Bar + let accountsChooserActionBar = new Gtk.ActionBar ( { valign: Gtk.Align.END } ); accountsChooserContainer.pack_start(accountsChooserActionBar, true, true, 0); - let accountsWidget = new Gtk.Grid ( { + let accountCreateButton = Gtk.Button.new_from_icon_name ( 'list-add', + Gtk.IconSize.BUTTON ); + accountsChooserActionBar.add ( accountCreateButton ); + accountCreateButton.connect ( 'clicked', Lang.bind ( this, this.createAccount ) ); + + let accountRemoveButton = Gtk.Button.new_from_icon_name ( 'list-remove', + Gtk.IconSize.BUTTON ); + accountsChooserActionBar.add ( accountRemoveButton ); + accountRemoveButton.connect ( 'clicked', Lang.bind ( this, this.removeAccount ) ); + + this.createAccountWidgets ( false ) ; + + mainVbox.pack_start(mainWidget, true, true, 0); + mainVbox.show_all(); + + return mainVbox; +} + +function getAccountSettings ( id ) +{ + let _path = SETTINGS_SCHEMA_ACCOUNT_PATH + '/' + id + '/'; + return Convenience.getSettings(SETTINGS_SCHEMA_ACCOUNT, _path); +} + + +function createAccountWidgets ( isActive ) +{ + // Accounts + this._accountsWidget = new Gtk.Grid ( { halign: Gtk.Align.FILL, margin: 18, column_spacing: 12, @@ -161,7 +148,9 @@ function buildPrefsWidget() { visible: true, column_homogeneous: false, }); - accountsWidgetContainer.pack_start(accountsWidget, true, true, 0); + this._accountsWidgetContainer.pack_start(this._accountsWidget, true, true, 0); + + this.prefWidgets = { }; let y = 1; for ( var prefEntry of prefs ) @@ -171,12 +160,14 @@ function buildPrefsWidget() { visible: true, halign: Gtk.Align.START, }); - accountsWidget.attach ( _label, 0, y, 1, 1 ); + this._accountsWidget.attach ( _label, 0, y, 1, 1 ); this.prefWidgets[prefEntry.key] = new prefEntry.type({ halign: Gtk.Align.FILL, visible: true, hexpand: true, + editable: isActive, + can_focus: isActive, }); let boundValue = 'text'; @@ -192,7 +183,7 @@ function buildPrefsWidget() { } ); } - accountsWidget.attach(this.prefWidgets[prefEntry.key], 1, y, 1, 1); + this._accountsWidget.attach(this.prefWidgets[prefEntry.key], 1, y, 1, 1); if ( prefEntry.type != Gtk.ComboBoxText ) { @@ -206,15 +197,98 @@ function buildPrefsWidget() { y++; } - mainVbox.pack_start(mainWidget, true, true, 0); - mainVbox.show_all(); + this.prefWidgets['name'].connect('changed', Lang.bind(this, function () { + let _row = this.accountsChooser.get_selected_row(); + _row.get_child().label = this.prefWidgets['name'].text; - return mainVbox; + } ) ); } -function getAccountSettings ( id ) +function activateAccountRow ( ) { + + if ( this._accountsWidget ) + this._accountsWidget.destroy ( ); + this.createAccountWidgets ( true ); + + let _row = this.accountsChooser.get_selected_row(); + log('Active:' + _row.server); + let _account_settings = getAccountSettings ( _row.server ); + + if ( ! _account_settings ) + return; + + for ( var prefEntry of prefs ) + { + if ( prefEntry.type == Gtk.Entry ) + { + _account_settings.bind ( + prefEntry.key, + this.prefWidgets[prefEntry.key], + 'text', + Gio.SettingsBindFlags.DEFAULT + ); + } + else if ( prefEntry.type == Gtk.ComboBoxText ) + { + this.prefWidgets[prefEntry.key].set_active(_account_settings.get_enum('type')); + this.prefWidgets[prefEntry.key].connect('changed', Lang.bind(this, function (e) { + log ( e ) ; + log('Active:' + this.prefWidgets['type'].get_active()); + let _account_settings = getAccountSettings ( _row.server ); + _account_settings.set_enum('type', this.prefWidgets['type'].get_active()); + })); + } + } +} + + +function addAccountLine ( server_id ) { - let _path = SETTINGS_SCHEMA_ACCOUNT_PATH + '/' + id; - return Convenience.getSettings(SETTINGS_SCHEMA_ACCOUNT, _path); + log ( '> Add line ' + server_id ); + let _account_settings = getAccountSettings ( server_id ); + let row = new Gtk.ListBoxRow ( { hexpand: true, + halign: Gtk.Align.FILL } ); + row.server = server_id; + + let _label = new Gtk.Label ( { label: _account_settings.get_string('name'), + hexpand: true, + margin: 5, + halign: Gtk.Align.START, + expand: true } ); + row.add ( _label ); + this.accountsChooser.add ( row ); + this.accountsChooser.show_all(); } + +function createAccount ( ) { + log ( '> Create Account' ); + + let _servers = this.settings.get_string ( 'servers' ) . split ( ',' ); + let _max = Math.max ( ..._servers ); + let _server_id = _max + 1; + _servers.push ( _server_id ); + + let _account_settings = getAccountSettings ( _server_id ); + _account_settings.set_string ( 'name', _('New server #') + _server_id ); + + this.addAccountLine ( _max + 1 ); + this.settings.set_string ( 'servers', _servers.join(',') ); +} + + + +function removeAccount ( ) { + let _row = this.accountsChooser.get_selected_row(); + log('Active:' + _row.server); + + let _servers = this.settings.get_string ( 'servers' ) . split ( ',' ); + _servers.splice ( _servers.indexOf ( _row.server ), 1 ); + log ( _servers ); + + if ( ! _row ) + return; + + _row.destroy(); + this.settings.set_string ( 'servers', _servers . join ( ',' ) ); +}