How to enter data in to gschema.xml file for ARRAYS and get them into extension.js?

I am writing an Extension. this needs to set default ARRAY in gschema.xml file and later on read it in extension.js file.

//Usecase in extension.js file

const order = [ 8, 7, 6, 5, 4, 3, 2, 1]
//const orderFromGSettings = this._settings.get_???('buttons-order');

//gschema.xml file

<key name="buttons-order" type="as">
<default>[ '1', '2', '3', '4', '5', '6', '7', '8' ]</default>
<summary>Default Order.</summary>
<description>Default Order</description>
</key>

need help for how to write data into xml file and export it in extension.js file via this._settings.get_???('buttons-order')

settings.get_strv('key')?

1 Like

Perfect. It worked. Thank you @rmnvgr

<key name="buttons-order" type="ai">

is more appropriate for storing numbers.

You can read it with

const order = this._settings.get_value().deepUnpack();
1 Like

Thank You very much. You always give good explanation @fmuellner.

This really make sense. when Array Integer (ai) is used, no need of quotations.
String Array (as) looks like this [ ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’ ]
where as ArrayInteger (ai) is simple [ 1, 2, 3, 4, 5, 6, 7, 8 ]

It worked in the extension. thank you once again.

I am sorry I tried on web how to set this value in prefs.js file but could not get it.
How to set the Array Integer?

<key name="buttons-order" type="ai">
const order = this._settings.get_value().deepUnpack();

How to properly set below?
this._settings.set_value('buttons-order', [ 1, 2, 3, 4, 5, 6, 7, 8 ] )

You have to create a GVariant of the expected type:

    const value = new GLib.Variant('ai', [1, 2, 3, 4, 5, 6, 7, 8]);
    this._settings.set_value('buttons-order', value);

This is actually the case for all GSettings values, methods like set_string() or get_boolean() are all convenience wrappers around get_value() and set_value().

1 Like

@fmuellner I am having difficulty to get the ‘ai’ from prefs.js file to extension.js file
//prefs.js

let orderEntry = new Gtk.Entry({ hexpand: true, margin_start: 20 });

orderEntry.set_placeholder_text("Example: '[ 8, 7, 6, 5, 1, 2, 3, 4 ]'" );
orderEntry.connect('changed', (entry) => { this._settings.set_value('buttons-order', new GLib.Variant('ai', entry.get_text()) )});

This is because the text in the entry is a string, with this code you’re passing the string '[1, 2, 3,…]' instead of the array of integers [1, 2, 3,…] to the settings, and I think the console would have told you about that.

You have to convert the string to the array of integers, GLib.Variant.parse() can help you with that:

const s = '[1, 2, 3, 4]';
const variantType = new GLib.VariantType('ai');
const variant = GLib.Variant.parse(variantType, s, null, null);

This will give you a new GVariant with the parsed array.

1 Like

I have in prefs.js like this

let orderEntry = new Gtk.Entry();
orderEntry.connect('changed', (entry) => {
				let string = entry.get_text();
				const varType = new GLib.VariantType('ai');
				let value = GLib.Variant.parse(variantType, string, null, null);
				this._settings.set_value('buttons-order', value); });

and in extension.js like this

let BUTTONS_ORDER = this._settings.get_value('buttons-order').deepUnpack();

this BUTTONS_ORDER works initially with the schema’s set in xml file. But when I change in the Gtk.Entry in extension settings, the value is not chaning.

xml file.

<key name="buttons-order" type="ai">
<default>[ 1, 2, 3, 4, 5, 6, 7, 8 ]</default>
<summary>Default Order</summary>
<description>Default Order</description>
</key>

Also I am able to change the order with gsettings command for example:

gsettings --schemadir . set org.gnome.shell.extensions.round-system-menu-buttons buttons-order '[8, 7, 6, 5, 1, 2, 3, 4]'

Have you connected to the changed signal of the settings to react to the changes in your extension? Is there anything in the gnome-shell and gjs logs?

Signals are connected. I think something wrong here

Error: Expected type string for argument ‘text’ but got type GObject_Boxed

Stack trace:
_arrangeButtonOrder@/home/admin/.local/share/gnome-shell/extensions/roundedSystemMenuButtons@pratap.fastmail.fm/prefs.js:107:13

let orderEntry = new Gtk.Entry();
orderEntry.set_placeholder_text(Example: ‘[ 8, 7, 6, 5, 1, 2, 3, 4 ]’ );
const value = new GLib.Variant(‘ai’, this._settings.get_value(‘buttons-order’));
orderEntry.set_text(value);

set_text(value) is the line 107

Well, there are several wrong things here:

  • Gio.Settings.get_value() already gives you a GVariant, you don’t have to place it inside another GVariant
  • Gtk.Editable.set_text() (the interface implemented by GtkEntry) expects a String, but you’re giving it a GVariant, so it complains. Try passing it value.deepUnpack().toString()? You’ll have to manually add the square brackets as they are not included in the Array.toString() method.
1 Like

@rmnvgr thank you for your continuous support on this. I am trying your way. will get back to you soon.

let orderEntry = new Gtk.Entry();
orderEntry.set_placeholder_text("Example: '[ 8, 7, 6, 5, 1, 2, 3, 4 ]' ");
const value = this._settings.get_value('buttons-order').deepUnpack().toString();
orderEntry.set_text('[' + value + ']');
    
orderEntry.connect('changed', (entry) => {
				let string = entry.get_text();
				const varType = new GLib.VariantType('ai');
				let value2 = GLib.Variant.parse(variantType, string, null, null);
				this._settings.set_value('buttons-order', value2); });

orderEntry.set_text () is working.
orderEntry.connect is not working.

Look at your logs, I’m sure they will tell you that variantType is undefined, because you declared the GVariantType with another variable name.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.