Actually let user change columns

This commit is contained in:
Benjamin Drieu 2022-09-09 19:22:46 +02:00
parent 6b7ba7c0f1
commit 348f2a593d
3 changed files with 118 additions and 45 deletions

View File

@ -276,7 +276,7 @@ class Indicator extends PanelMenu.Button {
}); });
} }
createHeaderBin ( colName ) { createHeaderBin ( colName, colSize = 50 ) {
let col = column_definitions [ colName ]; let col = column_definitions [ colName ];
let _box = new St.BoxLayout ( { vertical: false, let _box = new St.BoxLayout ( { vertical: false,
@ -305,7 +305,7 @@ class Indicator extends PanelMenu.Button {
let _button = new St.Button ( { let _button = new St.Button ( {
x_align: Clutter.ActorAlign.FILL, x_align: Clutter.ActorAlign.FILL,
y_align: Clutter.ActorAlign.CENTER, y_align: Clutter.ActorAlign.CENTER,
width: col.width, width: colSize,
reactive: true, reactive: true,
can_focus: true, can_focus: true,
track_hover: true, track_hover: true,
@ -319,7 +319,7 @@ class Indicator extends PanelMenu.Button {
let _bin = new St.Bin({ let _bin = new St.Bin({
style_class: 'monito-service', style_class: 'monito-service',
width: col.width, width: colSize,
x_expand: col.expand, x_expand: col.expand,
child: _button, child: _button,
}); });
@ -327,7 +327,7 @@ class Indicator extends PanelMenu.Button {
return _bin; return _bin;
} }
createBin ( status, text, col ) { createBin ( status, text, colSize = 50, col ) {
let _child; let _child;
if ( text === undefined ) if ( text === undefined )
@ -339,7 +339,7 @@ class Indicator extends PanelMenu.Button {
reactive: true, reactive: true,
can_focus: true, can_focus: true,
track_hover: true, track_hover: true,
width: col.width, width: colSize,
text: text.toString().replace(/\n.*/s, ''), text: text.toString().replace(/\n.*/s, ''),
x_align: ( col.align ? col.align : Clutter.ActorAlign.START ), x_align: ( col.align ? col.align : Clutter.ActorAlign.START ),
style: ( col.style ? col.style : '' ) } ); style: ( col.style ? col.style : '' ) } );
@ -351,7 +351,7 @@ class Indicator extends PanelMenu.Button {
{ {
_child = new St.BoxLayout ( { x_expand: true, _child = new St.BoxLayout ( { x_expand: true,
vertical: false, vertical: false,
width: col.width, } ); width: colSize, } );
if ( this.serverLogic.canRecheck ) if ( this.serverLogic.canRecheck )
{ {
@ -363,7 +363,7 @@ class Indicator extends PanelMenu.Button {
let _bin = new St.Bin({ let _bin = new St.Bin({
track_hover: true, track_hover: true,
width: col.width, width: colSize,
x_expand: col.expand, x_expand: col.expand,
child: _child, child: _child,
}); });
@ -421,10 +421,8 @@ class Indicator extends PanelMenu.Button {
}); });
this._box.add_child(headerBox); this._box.add_child(headerBox);
let _columns = Preferences.getColumns ( this.server ); for ( let _col of Preferences.getColumns ( this.server ) )
for ( let _col of _columns ) headerBox.add_child ( this.createHeaderBin ( _col.name, _col.size ) );
headerBox.add_child ( this.createHeaderBin ( _col ) );
headerBox.add_child ( this.createHeaderBin ( 'actions' ) );
let scrollBox = new St.ScrollView ( { hscrollbar_policy: St.PolicyType.NEVER, let scrollBox = new St.ScrollView ( { hscrollbar_policy: St.PolicyType.NEVER,
enable_mouse_scrolling: true, } ); enable_mouse_scrolling: true, } );
@ -465,30 +463,28 @@ class Indicator extends PanelMenu.Button {
}); });
tableBox.add_child(infoBox); tableBox.add_child(infoBox);
let _columns = Preferences.getColumns ( this.server ); for ( let _col of Preferences.getColumns ( this.server ) )
for ( let _col of _columns )
{ {
entry [ 'real_' + _col ] = entry [ _col ]; entry [ 'real_' + _col.name ] = entry [ _col.name ];
if ( _col == 'host_name' && this.account_settings.get_string ( 'host-match' ) ) if ( _col.name == 'host_name' && this.account_settings.get_string ( 'host-match' ) )
entry [ _col ] = entry [ _col ] . replace ( new RegExp ( this.account_settings.get_string ( 'host-match' ), 'i' ), entry [ _col.name ] = entry [ _col.name ] . replace ( new RegExp ( this.account_settings.get_string ( 'host-match' ), 'i' ),
this.account_settings.get_string ( 'host-replace' ) ); this.account_settings.get_string ( 'host-replace' ) );
else if ( _col == 'service_display_name' && this.account_settings.get_string ( 'service-match' ) ) else if ( _col.name == 'service_display_name' && this.account_settings.get_string ( 'service-match' ) )
entry [ _col ] = entry [ _col ] . replace ( new RegExp ( this.account_settings.get_string ( 'service-match' ), 'i' ), entry [ _col.name ] = entry [ _col.name ] . replace ( new RegExp ( this.account_settings.get_string ( 'service-match' ), 'i' ),
this.account_settings.get_string ( 'service-replace' ) ); this.account_settings.get_string ( 'service-replace' ) );
else if ( _col == 'status_information' && this.account_settings.get_string ( 'status-info-match' ) ) else if ( _col.name == 'status_information' && this.account_settings.get_string ( 'status-info-match' ) )
entry [ _col ] = entry [ _col ] . replace ( new RegExp ( this.account_settings.get_string ( 'status-info-match' ), 'i' ), entry [ _col.name ] = entry [ _col.name ] . replace ( new RegExp ( this.account_settings.get_string ( 'status-info-match' ), 'i' ),
this.account_settings.get_string ( 'status-info-replace' ) ) . replace ( new RegExp ( "\\n.*" ), "" ); this.account_settings.get_string ( 'status-info-replace' ) ) . replace ( new RegExp ( "\\n.*" ), "" );
else if ( _col == 'has_been_acknowledged' ) else if ( _col.name == 'has_been_acknowledged' )
{ {
if ( entry [ _col ] ) if ( entry [ _col.name ] )
entry [ _col ] = '✔'; entry [ _col.name ] = '✔';
else else
entry [ _col ] = ''; entry [ _col.name ] = '';
} }
infoBox.add_child ( this.createBin ( entry.status, entry [ _col ], column_definitions [ _col ] ) ); infoBox.add_child ( this.createBin ( entry.status, entry [ _col.name ], _col.size, column_definitions [ _col.name ] ) );
} }
infoBox.add_child ( this.createBin ( entry.status, entry, column_definitions [ 'actions' ] ) );
_row ++; _row ++;
} }

109
prefs.js
View File

@ -44,6 +44,18 @@ const SETTINGS_SCHEMA_ACCOUNT = "org.gnome.shell.extensions.monito.account";
const SETTINGS_SCHEMA_ACCOUNT_PATH = "/org/gnome/shell/extensions/monito/account"; const SETTINGS_SCHEMA_ACCOUNT_PATH = "/org/gnome/shell/extensions/monito/account";
const column_definitions = {
status: { label: _('Status'), width: 50, expand: false, },
host_name: { label: _('Host name'), width: 300, expand: false, },
service_display_name: { label: _('Service'), width: 300, expand: false, },
has_been_acknowledged: { label: _('Ack'), width: 50, expand: false },
last_check: { label: _('Last check'), width: 200, expand: false, type: 'date' },
attempts: { label: _('Attempts'), width: 50, expand: false, },
status_information: { label: _('Information'), width: 600, expand: false, },
actions: { label: 'Actions', width: 50, expand: false, special: 'actions' },
};
const prefs = [ const prefs = [
{ type: Gtk.Switch, category: 'Settings', label: _('Active (restart for effect)'), key: 'active', align: Gtk.Align.START }, { type: Gtk.Switch, category: 'Settings', label: _('Active (restart for effect)'), key: 'active', align: Gtk.Align.START },
{ type: Gtk.Entry, category: 'Settings', label: _('Name'), key: 'name' }, { type: Gtk.Entry, category: 'Settings', label: _('Name'), key: 'name' },
@ -246,7 +258,11 @@ function setSortOrder ( server, sort_order )
function getColumns ( server ) function getColumns ( server )
{ {
return this.getAccountSettings ( server ) . get_strv ( 'columns' ); let _columns = this.getAccountSettings ( server ) . get_strv ( 'columns' );
let _columnsSizes = this.getAccountSettings ( server ) . get_strv ( 'columns-size' );
const zip = (a1, a2) => a1.map((x, i) => { return { name: x, size: a2[i] } } );
let _result = zip ( _columns, _columnsSizes );
return _result;
} }
@ -371,18 +387,21 @@ function createColumnsPrefTab ( noteBook, type, isActive )
vexpand: true }); vexpand: true });
let columnNumbers = new Gtk.TreeViewColumn ( { title: _("Column") } ); let columnNumbers = new Gtk.TreeViewColumn ( { title: _("Column") } );
let rendererNumbers = new Gtk.CellRendererCombo ( { editable: true } ); this.ColumnNameRenderer = new Gtk.CellRendererCombo ( { editable: true,
rendererNumbers.col = 0; has_entry: false,
text_column: 0 } );
this.ColumnNameRenderer.col = 0;
if ( this.gtkVersion == 4 ) if ( this.gtkVersion == 4 )
columnNumbers.append(rendererNumbers, true); columnNumbers.append(this.ColumnNameRenderer, true);
else else
columnNumbers.pack_start(rendererNumbers, true); columnNumbers.pack_start(this.ColumnNameRenderer, true);
columnNumbers.add_attribute(rendererNumbers, 'text', 0); columnNumbers.add_attribute(this.ColumnNameRenderer, 'text', 0);
this.treeView.append_column(columnNumbers); this.treeView.append_column(columnNumbers);
rendererNumbers.connect('edited', Lang.bind ( this, this.editColumn ) ); this.ColumnNameRenderer.connect('edited', Lang.bind ( this, this.editColumn ) );
let _colSize = new Gtk.TreeViewColumn ( { title: _("Size") } ); let _colSize = new Gtk.TreeViewColumn ( { title: _("Size") } );
let _colRenderer = new Gtk.CellRendererText ( { editable: true } ); let _colRenderer = new Gtk.CellRendererSpin ( { adjustment: new Gtk.Adjustment ( { lower: 0, upper: 999, step_increment: 1, page_increment: 10 } ),
editable: true } );
if ( this.gtkVersion == 4 ) if ( this.gtkVersion == 4 )
_colSize.append(_colRenderer, true); _colSize.append(_colRenderer, true);
else else
@ -426,12 +445,30 @@ function activateAccountRow ( ) {
this.treeView.model = this.store; this.treeView.model = this.store;
let _row = this.accountsChooser.get_selected_row(); let _row = this.accountsChooser.get_selected_row();
monitoLog('Active:' + _row.server);
let _account_settings = getAccountSettings ( _row.server ); let _account_settings = getAccountSettings ( _row.server );
this.columnsModel = new Gtk.ListStore ( );
this.columnsModel.set_column_types([GObject.TYPE_STRING,GObject.TYPE_STRING]);
for ( let [ _colName, _colDef ] of Object.entries(column_definitions) )
{
let _iter = this.columnsModel.append();
this.columnsModel.set_value(_iter, 0, _colName );
this.columnsModel.set_value(_iter, 1, _colDef.label );
}
this.ColumnNameRenderer.model = this.columnsModel;
if ( ! _account_settings ) if ( ! _account_settings )
return; return;
let _columns = _account_settings . get_strv ( 'columns' );
let _columnsSizes = _account_settings . get_strv ( 'columns-size' );
for ( var i in _columns )
{
let _iter = this.store.append();
this.store.set_value(_iter, 0, _columns [ i ] );
this.store.set_value(_iter, 1, parseInt(_columnsSizes [ i ]) );
}
for ( var prefEntry of prefs ) for ( var prefEntry of prefs )
{ {
if ( prefEntry.type == Gtk.Entry ) if ( prefEntry.type == Gtk.Entry )
@ -464,8 +501,6 @@ function activateAccountRow ( ) {
{ {
this.prefWidgets[prefEntry.key].set_active(_account_settings.get_enum('type')); this.prefWidgets[prefEntry.key].set_active(_account_settings.get_enum('type'));
this.prefWidgets[prefEntry.key].connect('changed', Lang.bind(this, function (e) { this.prefWidgets[prefEntry.key].connect('changed', Lang.bind(this, function (e) {
monitoLog ( e ) ;
monitoLog('Active:' + this.prefWidgets['type'].get_active());
let _account_settings = getAccountSettings ( _row.server ); let _account_settings = getAccountSettings ( _row.server );
_account_settings.set_enum('type', this.prefWidgets['type'].get_active()); _account_settings.set_enum('type', this.prefWidgets['type'].get_active());
})); }));
@ -546,30 +581,68 @@ function setColor ( color )
function createColumnRow ( ) { function createColumnRow ( ) {
monitoLog ( '> Create column row' ); let _item = this.store.append();
this.store.set_value(_item, 0, 'status' );
this.store.set_value(_item, 1, 300 );
let _item = this.store.append(null); let _row = this.accountsChooser.get_selected_row();
this.store.set_value(_item, 0, 'bar' ); let _account_settings = getAccountSettings ( _row.server );
this.store.set_value(_item, 1, 1 ); let prefKey;
let _defs = _account_settings . get_strv ( 'columns' );
_defs.push ( 'status' );
_account_settings . set_strv ( 'columns', _defs );
_defs = _account_settings . get_strv ( 'columns-size' );
_defs.push ( '300' );
_account_settings . set_strv ( 'columns-size', _defs );
} }
function removeColumnRow ( widget ) { function removeColumnRow ( widget ) {
monitoLog ( '> Remove column row' );
let [any, model, _iter] = this.treeView.get_selection().get_selected ( ); let [any, model, _iter] = this.treeView.get_selection().get_selected ( );
let _row = this.accountsChooser.get_selected_row();
let _account_settings = getAccountSettings ( _row.server );
let prefKey;
for ( var foo of this.treeView.get_selection().get_selected_rows()[0] )
{
let _defs = _account_settings . get_strv ( 'columns' );
_defs.splice ( foo.get_indices ( ), 1 );
_account_settings . set_strv ( 'columns', _defs );
_defs = _account_settings . get_strv ( 'columns-size' );
_defs.splice ( foo.get_indices ( ), 1 );
_account_settings . set_strv ( 'columns-size', _defs );
}
this.store.remove ( _iter ); this.store.remove ( _iter );
} }
function editColumn ( widget, path, text ) function editColumn ( widget, path, text )
{ {
monitoLog ( '> Edit column row ' + widget.col + ', ' + path + ', ' + text ) let _row = this.accountsChooser.get_selected_row();
let _account_settings = getAccountSettings ( _row.server );
let prefKey;
let _iter = this.store.get_iter ( Gtk.TreePath.new_from_string ( path ) ); let _iter = this.store.get_iter ( Gtk.TreePath.new_from_string ( path ) );
monitoLog ( '> Iter ' + _iter[1] );
if ( widget.col == 1 ) if ( widget.col == 1 )
{
this.store.set_value(_iter[1], widget.col, parseInt(text) ); this.store.set_value(_iter[1], widget.col, parseInt(text) );
prefKey = 'columns-size';
}
else else
{
this.store.set_value(_iter[1], widget.col, text ); this.store.set_value(_iter[1], widget.col, text );
prefKey = 'columns';
}
let _defs = _account_settings . get_strv ( prefKey );
_defs [ parseInt(path) ] = text;
_account_settings . set_strv ( prefKey, _defs );
return true; return true;
} }

View File

@ -134,7 +134,11 @@
</key> </key>
<key name="columns" type="as"> <key name="columns" type="as">
<default>['status','host_name','service_display_name','has_been_acknowledged','last_check','attempts','status_information']</default> <default>['status','host_name','service_display_name','has_been_acknowledged','last_check','attempts','status_information','actions']</default>
</key>
<key name="columns-size" type="as">
<default>[ '50', '300', '300', '50', '200', '50', '600', '50' ]</default>
</key> </key>
<key name="columns-order" type="as"> <key name="columns-order" type="as">