Skip to content
AylurPublic

About

Library that brings JSX, reactivity and type-safety to GJS

Topics

Resources

Stars

96 stars

Watchers

1 watching

Forks

Repository files navigation

Gnim

Library that brings JSX, reactivity and type-safety to GNOME JavaScript.

Get started

pnpm create gnim

JSX and reactivity

Build reactive GTK interfaces with familiar JSX syntax. Create signals, derive computed values, and let the UI update automatically when state changes.

function Counter() {
  const [count, setCount] = createState(0)

  function increment() {
    setCount((v) => v + 1)
  }

  effect(() => {
    console.log("count is", count())
  })

  return (
    <Gtk.Box spacing={8}>
      <Gtk.Label label={count.as((c) => c.toString())} />
      <Gtk.Button onClicked={increment}>Increment</Gtk.Button>
    </Gtk.Box>
  )
}

let win: Gtk.Window
render(Counter, win)

GObject decorators

Define GObject classes with clean, declarative TypeScript decorators.

import GObject from "gi://GObject?version=2.0"
import { register, property, signal } from "gnim/gobject"

@register
class MyObj extends GObject.Object {
  @property myProp: string = ""

  @signal mySignal(a: string, b: number): void {
    print(a, b)
  }
}

DBus

Create DBus services and proxies with ease in a fully typed manner.

import { createDBusInterface, property, method, signal } from "gnim/dbus"

const MyInterface = createDBusInterface("example.gjs.MyInterface", {
  MyProperty: property("s"),
  MyMethod: method(["s"], ["s"]),
  MySignal: signal("s"),
})

// serve it
const service = await MyInterface.serve({
  name: "example.gjs.MyInterface",
  objectPath: "/example/gjs/MyInterface",
  implementation: (emitter) => ({
    MyProperty: "initial value",
    MyMethod(str): [string] {
      emitter.MySignal(str)
      return [str]
    },
  }),
})

// or proxy it
const proxy = await MyInterface.proxy({
  name: "example.gjs.MyInterface",
  objectPath: "/example/gjs/MyInterface",
})

Gio Settings

Define your app's settings schema in TypeScript and get reactive, type-safe access to GSettings. Schema XML can be generated at build time that integrates into existing tooling.

import GLib from "gi://GLib?version=2.0"
import { defineSchemaList, Schema, Enum, Flags } from "gnim/schema"

const myFlags = new Flags("my.flags", ["one", "two"])
const myEnum = new Enum("my.enum", ["one", "two"])

export const schema = new Schema({
  id: "com.example.MyApp",
  path: "/com/example/myapp/",
})
  .key("my-key", "s", {
    default: "",
    summary: "Simple string key",
  })
  .key("complex-key", "a{sv}", {
    default: {
      key: GLib.Variant.new("s", "value"),
    },
    summary: "Variant dict key",
  })
  .key("enum-key", myEnum, {
    default: "one",
  })
  .key("flags-key", myFlags, {
    default: ["one", "two"],
  })

export default defineSchemaList([schema])
import { schema } from "./com.example.MyApp.gschema"
import { createSettings } from "gnim/schema"

const settings = createSettings(schema)

effect(() => {
  print(settings.myKey())
})

settings.setMyKey("hello")

Text formatting

Type-safe text formatting using the ICU Message Format.

import { createDomain, fmt } from "gnim/intl"

const { gettext: t } = createDomain("com.example.MyApp")

function App() {
  const [count, setCount] = createState(0)

  return (
    <Gtk.Button onClicked={() => setCount((c) => c + 1)}>
      <Gtk.Label label={t("Click Me!")} />
      <Gtk.Label
        label={count.as((count) =>
          fmt(
            t(`{count, plural,
              =0 {Not yet clicked}
              =1 {Clicked once}
              other {Clicked # times}
            }`),
            { count },
          ),
        )}
      />
    </Gtk.Button>
  )
}

Asset handling

Import assets that are automatically bundled with zero setup.

import image from "./assets/image?file"

return <Gtk.Picture file={image} />

About

Library that brings JSX, reactivity and type-safety to GJS

Topics

Resources

Stars

96 stars

Watchers

1 watching

Forks

Sponsor this project

Used by

Contributors

Languages