---
title: "Settings"
description: "Store preferences with GSettings: compile a schema, provide its settings instance, and read, write and bind its keys."
---

# Settings

The [GSettings reference](https://docs.gtk.org/gio/class.Settings.html) covers schemas and their native storage; GTKX compiles your schema, types its keys, and provides hooks to read, write and bind them.

## Your app's schema

Write the schema in a `.gschema.xml` file, usually `data/<application ID>.gschema.xml` with the application ID as the schema ID:

```xml [data/com.example.Notes.gschema.xml]
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
  <schema id="com.example.Notes" path="/com/example/Notes/">
    <key name="color-scheme" type="s">
      <choices>
        <choice value="default"/>
        <choice value="light"/>
        <choice value="dark"/>
      </choices>
      <default>'default'</default>
      <summary>Color scheme</summary>
    </key>
    <key name="window-width" type="i">
      <default>800</default>
      <summary>Window width</summary>
    </key>
  </schema>
</schemalist>
```

Import the file without a query wherever a hook needs it. GTKX compiles and bundles the schema, and codegen writes its types beside it as `com.example.Notes.gschema.d.xml.ts`, which TypeScript reads with `allowArbitraryExtensions`; the scaffold's `tsconfig.json` enables it, and its `.gitignore` leaves out `*.d.xml.ts`. The import's default export describes the file's first schema, and every schema in the file is also a named export (`com_example_Notes` here). The hooks use that description to check key names and value types:

```ts
import schema from "../data/com.example.Notes.gschema.xml";
```

GTKX installs schemas by file name, so imported schema files that share a name must be identical, including [copies in workspace packages](/guide/workspaces#workspace-sources); otherwise GTKX stops with an error naming both.

A schema without a `path` attribute is relocatable: its import has an `at(path)` method, and each path keeps its own values. Pass what `at` returns wherever a schema is expected.

[Translate settings schemas](/guide/internationalization#translate-settings-schemas) covers the schema text, and [Test data and settings](/guide/testing#test-data-and-settings) how tests start from the defaults.

## The settings instance

`SettingsProvider` creates the `Gio.Settings` instance of a schema and owns it. Wrap the windows inside the application element in it:

```tsx [src/app.tsx]
import { AdwApplication } from "@gtkx/jsx/adw";
import { SettingsProvider } from "@gtkx/react";
import schema from "../data/com.example.Notes.gschema.xml";
import { Window } from "./components/window.js";

export const App = () => (
    <AdwApplication applicationId="com.example.Notes">
        <SettingsProvider schema={schema}>
            <Window />
        </SettingsProvider>
    </AdwApplication>
);
```

`useSetting` and `useBindSetting` find the instance by the schema they are given, and throw when no provider for that schema is rendered above them. An app that reads several schemas, or several paths of a relocatable one, nests a provider for each.

The provider's `ref` receives the instance, for calls the hooks do not cover, such as `reset` and `createAction`. Changing `schema` to another id or path creates another instance and remounts the children.

## Reading and writing a key

`useSetting(schema, key)` returns the key's value and a setter, and re-renders when the value changes, including from another instance or `gsettings set`:

```tsx [src/components/dark-style-row.tsx]
import { AdwSwitchRow } from "@gtkx/jsx/adw";
import { useSetting } from "@gtkx/react";
import schema from "../../data/com.example.Notes.gschema.xml";

export const DarkStyleRow = () => {
    const [scheme, setScheme] = useSetting(schema, "color-scheme");

    return (
        <AdwSwitchRow
            title="Dark Style"
            active={scheme === "dark"}
            onNotifyActive={(active) => setScheme(active ? "dark" : "default")}
        />
    );
};
```

`scheme` is `"default" | "light" | "dark"`, from the key's `<choices>`, which the schema's [`values`](/reference/@gtkx/react/index/type-aliases/SettingsSchema) also lists at runtime. Other keys read as their unpacked GVariant type, such as a `number` for `i` and a `bigint` for `x`, and a key declared with `enum` or `flags` reads as a number. A key the schema does not declare fails `tsc`, and so does a value of the wrong type.

## Binding a key to a property

`useBindSetting` keeps a key and an object's property in step, both ways by default, with no React state between them. Use it for values the widget owns, such as a window's size:

```tsx
import type * as Adw from "@gtkx/gi/adw";
import { AdwApplicationWindow } from "@gtkx/jsx/adw";
import { quit, useBindSetting } from "@gtkx/react";
import { useState } from "react";
import schema from "../../data/com.example.Notes.gschema.xml";

export const Window = () => {
    const [window, setWindow] = useState<Adw.ApplicationWindow | null>(null);
    useBindSetting({
        schema,
        key: "window-width",
        object: window,
        property: "defaultWidth",
    });

    return (
        <AdwApplicationWindow
            ref={setWindow}
            title="Notes"
            onCloseRequest={quit}
        />
    );
};
```

`flags` takes [`Gio.SettingsBindFlags`](https://docs.gtk.org/gio/flags.SettingsBindFlags.html), such as `GET` to only read the key.

## System schemas

Schema imports type only the schemas the project ships. To read a schema installed on the system, such as `org.gnome.desktop.interface` for the desktop's fonts and color scheme, describe the keys the app uses in an object that satisfies `SettingsSchema`:

```tsx [src/components/system-appearance.tsx]
import { AdwActionRow } from "@gtkx/jsx/adw";
import { SettingsProvider, type SettingsSchema, useSetting } from "@gtkx/react";

const desktopInterface = {
    id: "org.gnome.desktop.interface",
    path: null,
    keys: { "monospace-font-name": "s", "color-scheme": "enum" },
    values: {
        "color-scheme": { default: 0, "prefer-dark": 1, "prefer-light": 2 },
    },
} as const satisfies SettingsSchema;

const AppearanceRows = () => {
    const [font] = useSetting(desktopInterface, "monospace-font-name");
    const [scheme] = useSetting(desktopInterface, "color-scheme");
    const prefersDark =
        scheme === desktopInterface.values["color-scheme"]["prefer-dark"];

    return (
        <>
            <AdwActionRow title="Monospace font" subtitle={font} />
            <AdwActionRow
                title="Dark style"
                subtitle={prefersDark ? "Preferred" : "Not preferred"}
            />
        </>
    );
};

export const SystemAppearance = () => (
    <SettingsProvider schema={desktopInterface}>
        <AppearanceRows />
    </SettingsProvider>
);
```

<figure class="result">
    <img class="result-light" src="/figures/guide-native-state-system-appearance-light.png" width="420" height="260" loading="lazy" alt="A window titled Appearance with a System group of two rows: Monospace font, showing the desktop's font name, and Dark style, showing whether the desktop prefers it." />
    <img class="result-dark" src="/figures/guide-native-state-system-appearance-dark.png" width="420" height="260" loading="lazy" alt="A window titled Appearance with a System group of two rows: Monospace font, showing the desktop's font name, and Dark style, showing whether the desktop prefers it." />
    <figcaption>Captured from the sample above, with its rows inside an <code>AdwPreferencesGroup</code>. The subtitles are the values of the two desktop keys.</figcaption>
</figure>

`keys` maps each key to its GVariant type string, or to `enum` or `flags` for a key declared with those attributes; `values` gives the number of each enum nick, or the allowed strings of a key with choices. Copy the key types from the installed `.gschema.xml` under `/usr/share/glib-2.0/schemas`, and the enum numbers from the `.enums.xml` file that declares the enum, here `org.gnome.desktop.enums.xml`. Keep `as const satisfies SettingsSchema` so the hooks see literal types: `font` is a `string`, `scheme` is `0 | 1 | 2`, and a key the object does not list fails `tsc`.

GTKX does not bundle or compile a system schema; the app reads the copy installed on the user's machine. TypeScript cannot check the object against that copy. As in GJS, creating the instance throws when the schema is not installed, and reading a key the installed schema does not declare throws, so an [error boundary](/guide/error-handling#react-error-boundaries) can handle a missing package or a misspelled name.
