Embed a settings.gschema.xml with compile_resources and gio::resources_register_include!

My question is whether this is possible. Create a settings.gschema.xml:

<?xml version="1.0" encoding="utf-8"?>
<schemalist>
  <schema id="org.domain.myapp" path="/org/domain/myapp/">
    <key name="is-switch-enabled" type="b">
      <default>false</default>
      <summary>Default switch state</summary>
    </key>
  </schema>
</schemalist>

Which is then put into a resource file such as this:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/org/domain/myapp/">
    <file>settings.gschema.xml</file>
  </gresource>
</gresources>

Then include it with compile_resources, load it with gio::resources_register_include!("my.gresource").expect("Failed to register resources."); and finally access it in some way similar to let settings = gio::Settings::new("org.domain.myapp");.

I have tried several different ways and all of them fail with: Settings schema ‘org.domain.myapp’ is not installed :cry:

I don’t think this would work out of the box; no such behaviour is documented for GSettings.

If you wish to load an embedded schema, you’d likely need to embed the compiled settings as a resource, copy it from resources to a temporary directory on startup, create a schema source for that directory (see SettingsSchemaSource.new_from_directory, and then create your GSettings object with your schema explicitly looked up from that source.

That’d be rather… unusual behaviour for a Gtk application, tho. Why do you wish to include the settings schema as a resource?

It would be to make the application more portable and not have to be installing things here and there. Especially for Windows which is a bit hellish.

In any case, I think it would be interesting if it were supported in the same way that composite templates or CSS are supported.

It is not: allow creating schema source from resource (#499) · Issues · GNOME / GLib · GitLab

It does not matter if you do it for your own settings: GTK and other libraries have their own settings as well, which means they would need to switch to embedded schemas first.

Wow, I’ve hit the 13 year “bug”!

Well, mystery solved. It is true that it would be interesting if it could be done, but I understand the difficulties of why it is not done.

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