Implement GTK+3/4 compatibility

This commit is contained in:
Benjamin Drieu 2022-05-30 11:27:11 +02:00 committed by Benjamin Drieu
parent af32fbefd2
commit 4e21919d2e
1 changed files with 50 additions and 11 deletions

View File

@ -83,6 +83,9 @@ const prefs = [
function init() { function init() {
monitoLog('initializing ${Me.metadata.name} Preferences'); monitoLog('initializing ${Me.metadata.name} Preferences');
this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA); this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA);
this.gtkVersion = Gtk.get_major_version();
} }
@ -98,12 +101,14 @@ function buildPrefsWidget() {
let mainWidget = new Gtk.Notebook( { } ); let mainWidget = new Gtk.Notebook( { } );
let prefsWidget = new Gtk.Grid({ let prefsWidget = new Gtk.Grid({
// margin: 18,
column_spacing: 12, column_spacing: 12,
row_spacing: 12, row_spacing: 12,
column_homogeneous: false, column_homogeneous: false,
}); });
if ( this.gtkVersion < 4 )
prefsWidget.margin = 18;
this.prefWidgets = { }; this.prefWidgets = { };
// Add a simple title and add it to the prefsWidget // Add a simple title and add it to the prefsWidget
@ -165,12 +170,18 @@ function buildPrefsWidget() {
homogeneous: false, } ); homogeneous: false, } );
monitoLog ( 'Box ' + this._accountsWidgetContainer ); monitoLog ( 'Box ' + this._accountsWidgetContainer );
monitoLog ( 'Func ' + this._accountsWidgetContainer.pack_start ); monitoLog ( 'Func ' + this._accountsWidgetContainer.pack_start );
this._accountsWidgetContainer.append(accountsChooserContainer, true, true, 0); if ( this.gtkVersion == 4 )
this._accountsWidgetContainer.append(accountsChooserContainer, true, true, 0);
else
this._accountsWidgetContainer.add(accountsChooserContainer, true, true, 0);
this.accountsChooser = new Gtk.ListBox ( { valign: Gtk.Align.FILL, this.accountsChooser = new Gtk.ListBox ( { valign: Gtk.Align.FILL,
hexpand: true, hexpand: true,
vexpand: true } ); vexpand: true } );
accountsChooserContainer.append(this.accountsChooser, true, true, 0); if ( this.gtkVersion == 4 )
accountsChooserContainer.append(this.accountsChooser, true, true, 0);
else
accountsChooserContainer.add(this.accountsChooser, true, true, 0);
// Account list // Account list
for ( var server_id of this.getServersList ( ) ) for ( var server_id of this.getServersList ( ) )
@ -179,7 +190,10 @@ function buildPrefsWidget() {
// Action Bar // Action Bar
let accountsChooserActionBar = new Gtk.ActionBar ( { valign: Gtk.Align.END } ); let accountsChooserActionBar = new Gtk.ActionBar ( { valign: Gtk.Align.END } );
accountsChooserContainer.append(accountsChooserActionBar, true, true, 0); if ( this.gtkVersion == 4 )
accountsChooserContainer.append(accountsChooserActionBar, true, true, 0);
else
accountsChooserContainer.add(accountsChooserActionBar, true, true, 0);
let accountCreateButton = Gtk.Button.new_from_icon_name ( 'list-add', let accountCreateButton = Gtk.Button.new_from_icon_name ( 'list-add',
Gtk.IconSize.BUTTON ); Gtk.IconSize.BUTTON );
@ -193,7 +207,10 @@ function buildPrefsWidget() {
this.createAccountWidgets ( false ) ; this.createAccountWidgets ( false ) ;
mainVbox.append(mainWidget, true, true, 0); if ( this.gtkVersion == 4 )
mainVbox.append(mainWidget, true, true, 0);
else
mainVbox.add(mainWidget);
mainVbox.show_all(); mainVbox.show_all();
return mainVbox; return mainVbox;
@ -245,7 +262,10 @@ function createAccountWidgets ( isActive )
this.prefWidgets = { }; this.prefWidgets = { };
this._accountsWidget = new Gtk.Notebook( { } ); this._accountsWidget = new Gtk.Notebook( { } );
this._accountsWidgetContainer.append(this._accountsWidget, true, true, 0); if ( this.gtkVersion == 4 )
this._accountsWidgetContainer.append(this._accountsWidget, true, true, 0);
else
this._accountsWidgetContainer.add(this._accountsWidget);
for ( var _tab of [ 'Settings', 'Columns', 'Colors', 'Filters', 'Replacements' ] ) for ( var _tab of [ 'Settings', 'Columns', 'Colors', 'Filters', 'Replacements' ] )
{ {
@ -276,6 +296,8 @@ function createPrefWidgets ( noteBook, type, isActive )
visible: true, visible: true,
column_homogeneous: false, column_homogeneous: false,
}); });
if ( this.gtkVersion < 4 )
grid.margin = 18;
noteBook.append_page ( grid, new Gtk.Label ( { label: _(type), } ) ); noteBook.append_page ( grid, new Gtk.Label ( { label: _(type), } ) );
let y = 0; let y = 0;
@ -340,6 +362,8 @@ function createColumnsPrefTab ( noteBook, type, isActive )
visible: true, visible: true,
column_homogeneous: false, column_homogeneous: false,
}); });
if ( this.gtkVersion < 4 )
grid.margin = 18;
noteBook.append_page ( grid, new Gtk.Label ( { label: _(type), } ) ); noteBook.append_page ( grid, new Gtk.Label ( { label: _(type), } ) );
let _store = new Gtk.TreeStore(); let _store = new Gtk.TreeStore();
@ -354,13 +378,19 @@ function createColumnsPrefTab ( noteBook, type, isActive )
let columnNumbers = new Gtk.TreeViewColumn ( { title: _("Column") } ); let columnNumbers = new Gtk.TreeViewColumn ( { title: _("Column") } );
let rendererNumbers = new Gtk.CellRendererText ( { editable: true } ); let rendererNumbers = new Gtk.CellRendererText ( { editable: true } );
columnNumbers.append(rendererNumbers, true); if ( this.gtkVersion == 4 )
columnNumbers.append(rendererNumbers, true);
else
columnNumbers.pack_start(rendererNumbers, true);
columnNumbers.add_attribute(rendererNumbers, 'text', 0); columnNumbers.add_attribute(rendererNumbers, 'text', 0);
_treeView.append_column(columnNumbers); _treeView.append_column(columnNumbers);
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.CellRendererText ( { editable: true } );
_colSize.append(_colRenderer, true); if ( this.gtkVersion == 4 )
_colSize.append(_colRenderer, true);
else
_colSize.pack_start(_colRenderer, true);
_colSize.add_attribute(_colRenderer, 'text', 1); _colSize.add_attribute(_colRenderer, 'text', 1);
_treeView.append_column(_colSize); _treeView.append_column(_colSize);
@ -446,9 +476,18 @@ function addAccountLine ( server_id )
hexpand: true, hexpand: true,
// margin: 5, // margin: 5,
halign: Gtk.Align.START } ); halign: Gtk.Align.START } );
row.set_child ( _label ); if ( this.gtkVersion == 4 )
this.accountsChooser.append ( row ); {
this.accountsChooser.show_all(); // XXX Does not work in gtk4 ? row.set_child ( _label );
this.accountsChooser.append ( row );
}
else
{
_label.margin = 5;
row.add ( _label );
this.accountsChooser.add ( row );
this.accountsChooser.show_all();
}
} }