commit 1178d04d370abae0c09079e9a013ff1b443fc526 Author: Pascal Thuma Date: Tue Mar 24 18:58:33 2026 +0100 Initial commit diff --git a/app.ts b/app.ts new file mode 100644 index 0000000..51d536f --- /dev/null +++ b/app.ts @@ -0,0 +1,10 @@ +import app from "ags/gtk4/app" +import style from "./style.scss" +import Bar from "./widget/Bar" + +app.start({ + css: style, + main() { + app.get_monitors().map(Bar) + }, +}) diff --git a/docs/.gitignore b/docs/.gitignore new file mode 100644 index 0000000..8901a5d --- /dev/null +++ b/docs/.gitignore @@ -0,0 +1,5 @@ +dist/ +result/ +.vitepress/cache/ +node_modules/ +npm-debug.log* diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts new file mode 100644 index 0000000..3bfd450 --- /dev/null +++ b/docs/.vitepress/config.ts @@ -0,0 +1 @@ +export { default } from "../vitepress.config" diff --git a/docs/.vitepress/theme/index.ts b/docs/.vitepress/theme/index.ts new file mode 100644 index 0000000..a2ca770 --- /dev/null +++ b/docs/.vitepress/theme/index.ts @@ -0,0 +1,5 @@ +import DefaultTheme from "vitepress/theme" +import "../../vitepress.theme.css" +import "devicon/devicon.min.css" + +export default DefaultTheme diff --git a/docs/guide/app-cli.md b/docs/guide/app-cli.md new file mode 100644 index 0000000..95ae522 --- /dev/null +++ b/docs/guide/app-cli.md @@ -0,0 +1,226 @@ +# App and CLI + +`app` is a singleton **instance** of an +[Gtk.Application](https://docs.gtk.org/gtk4/class.Application.html). + +Depending on Gtk version import paths will differ + +```ts +import app from "astal/gtk3/app" +import app from "astal/gtk4/app" +``` + +> [!TIP] +> +> The `app` instance's DBus name is prefixed with `io.Astal`. If you are writing +> a shell which is meant to be distributed you might want to avoid using `app` +> and instead create a subclass of `Gtk.Application` or `Adw.Application` while +> also following the +> [packaging conventions](https://gjs.guide/guides/gtk/application-packaging.html). + +::: details Example App implementation + +```tsx +import Astal from "gi://Astal?version=4.0" +import Gio from "gi://Gio?version=2.0" +import GObject from "gi://GObject?version=2.0" +import Gtk from "gi://Gtk?version=4.0" +import { programInvocationName, programArgs } from "system" +import { createRoot } from "gnim" + +class App extends Gtk.Application { + static { + GObject.registerClass(this) + } + + constructor() { + super({ + applicationId: "my.awesome.app", + flags: Gio.ApplicationFlags.HANDLES_COMMAND_LINE, + }) + } + + vfunc_command_line(cmd: Gio.ApplicationCommandLine): number { + const args: string[] = cmd.get_arguments() + + if (cmd.isRemote) { + console.log("invoked from remote instance") + cmd.print_literal("hello from primary instance") + cmd.done() + } else { + this.main(args) + } + + return 0 + } + + private main(args: string[]) { + createRoot((dispose) => { + this.connect("shutdown", dispose) + + return ( + + + + + + ) + }) + } +} + +const app = new App() +app.runAsync([programInvocationName, ...programArgs]) +``` + +::: + +## Entry point + +You should generally avoid creating resources in the top level of modules and +instead create everything in the scope of the `main` function. This is due to +the possibility of running as a [client](./app-cli#clients) process. + +:::code-group + +```tsx [app.tsx] +const globalInstance = SomeLibrary.get_default() // [!code --] + +function Bar() { + const globalInstance = SomeLibrary.get_default() // [!code ++] + + return <> +} + +app.start({ + main() { + Bar() + }, +}) +``` + +::: + +## Instance identifier + +You can run multiple instances by defining a unique instance name. + +```ts +app.start({ + instanceName: "my-instance", // defaults to "ags" + main() {}, +}) +``` + +## Messaging from CLI + +If you want to interact with an instance from the CLI, you can do so by sending +a request. A request is an argument array. + +```ts +app.start({ + requestHandler(argv: string[], response: (response: string) => void) { + const [cmd, arg, ...rest] = argv + if (cmd == "say") { + return response(arg) + } + response("unknown command") + }, + main() {}, +}) +``` + +The `response` function can be called once per request. `ags request` command +will wait until a response is given at which point it will print it and exit. + +```sh +ags request say hi +# hi +``` + +A request handler can also be defined by connecting to the `request` signal. + +```ts +app.connect("reqeust", (app, [cmd, arg, ...rest], response) => { + if (cmd === "say") { + response(arg) + } +}) +``` + +## Toggling Windows by their name + +In order for the application to know about your windows, you have to register +them. You can do this by specifying a **unique** `name` and calling +`app.add_window()` + +```tsx {5} +import app from "astal/gtk4/app" + +function Bar() { + return ( + app.add_window(self)}> + + + ) +} +``` + +You can also invoke `app.add_window()` by simply passing `app` to the +`application` prop. + +```tsx {5} +import app from "astal/gtk4/app" + +function Bar() { + return ( + + + + ) +} +``` + +> [!WARNING] +> +> When assigning the `application` prop make sure `name` comes before. Props are +> set sequentially and if name is applied after application it won't work. + +Toggle the visibility of windows using the `ags` CLI. + +```sh +ags toggle Bar +``` + +> [!TIP] +> +> In JavaScript you can get a window instance and toggle it using +> `app.get_window()` +> +> ```ts +> const bar = app.get_window("Bar") +> if (bar) bar.visible = true +> ``` + +## Clients + +The first time you invoke `app.start()` (for example with `ags run`) the `main` +block gets executed. While that instance is running any subsequent execution of +the app will simply invoke a [request](#messaging-from-cli). For example running +`ags run` again will be the equivalent of running `ags request` + +:::code-group + +```ts [app.ts] +app.start({ + requestHandler(argv, response) { + console.log("request", ...argv) + response("hello from main instance") + }, + main(...argv: string[]) { + console.log(...argv) + }, +}) +``` + +::: diff --git a/docs/guide/examples.md b/docs/guide/examples.md new file mode 100644 index 0000000..09eafbf --- /dev/null +++ b/docs/guide/examples.md @@ -0,0 +1,15 @@ +# Examples + +## Gtk4 + +### [Simple Bar](https://github.com/Aylur/ags/tree/main/examples/gtk4/simple-bar) + +![simple bar](https://github-production-user-asset-6210df.s3.amazonaws.com/104676705/365398793-a306c864-56b7-44c4-8820-81f424f32b9b.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20251022%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251022T002056Z&X-Amz-Expires=300&X-Amz-Signature=77da46a033d21d26ba8ec7713c0a1630fa36174699bfcce2b8f10acb6663f9f4&X-Amz-SignedHeaders=host) + +### [Applauncher](https://github.com/Aylur/ags/tree/main/examples/gtk4/applauncher) + +![Applauncher](https://github-production-user-asset-6210df.s3.amazonaws.com/104676705/383792578-2695e3bb-dff4-478a-b392-279fe638bfd3.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20251022%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251022T002056Z&X-Amz-Expires=300&X-Amz-Signature=9c222efd9255ec0e8874aca1d639b07e1e9b3d38dc2a0249af895f3617eec154&X-Amz-SignedHeaders=host) + +### [Notifications](https://github.com/Aylur/ags/tree/main/examples/gtk4/notifications) + +![Notifications](https://github-production-user-asset-6210df.s3.amazonaws.com/104676705/383769153-0df0eddc-5c74-4af0-a694-48dc8ec6bb44.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20251022%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251022T002056Z&X-Amz-Expires=300&X-Amz-Signature=4751326ceeb806194361258173dede15b5cff48461382e2013a7cf18405ba47f&X-Amz-SignedHeaders=host) diff --git a/docs/guide/faq.md b/docs/guide/faq.md new file mode 100644 index 0000000..a82644d --- /dev/null +++ b/docs/guide/faq.md @@ -0,0 +1,327 @@ +# Frequently Asked Questions + +## Standard Library + +GJS does not include Node.js APIs you might be used to. You can find the +alternative for most APIs in [GLib](https://docs.gtk.org/glib/) and +[Gio](https://docs.gtk.org/gio/). + +## Avoiding JSX + +JSX syntax is entirely optional. Under the hood it is just syntactic sugar for +[function composition](https://aylur.github.io/gnim/jsx#jsx-expressions-and-jsx-function). + +```tsx +function Bar() { + // [!code --:5] + return ( + + + + ) + // [!code ++:4] + return jsx(Astal.Window, { + name: "Bar", + children: [jsx(Gtk.Box, {})], + }) +} +``` + +> [!TIP] +> +> You can wrap Gtk/Astal widgets in a simple function to achieve the syntax used +> in AGSv1: +> +> ```ts +> import { CCProps, jsx } from "ags" +> import { Gtk, Astal } from "ags/gtk4" +> +> const Box = (props: Partial>) => +> jsx(Gtk.Box, props) +> +> const Window = ( +> props: Partial>, +> ) => jsx(Astal.Window, props) +> +> function Bar() { +> return Window({ +> name: "Bar", +> children: [Box({ children: ["Hello There"] })], +> }) +> } +> ``` + +## Monitor id does not match compositor + +The monitor id property that windows expect is mapped by Gdk, which is not +always the same as the compositor. Instead use the `gdkmonitor` property which +expects a `Gdk.Monitor` object. + +```tsx +function Bar(gdkmonitor) { + return +} + +function main() { + for (const monitor of app.get_monitors()) { + if (monitor.model == "your-desired-model") { + Bar(monitor) + } + } +} +``` + +## Environment variables + +JavaScript is **not** bash or other shell environments. + +```ts +const HOME = exec("echo $HOME") // does not work as you'd expect +``` + +`exec` and `execAsync` runs the passed program as is, its **not** run in a shell +environment, so the above example just passes `$HOME` as a string literal to the +`echo` program. + +:::danger Please don't do this + +```ts +const HOME = exec("bash -c 'echo $HOME'") +``` + +::: + +You can read environment variables using +[GLib.getenv](https://gjs-docs.gnome.org/glib20~2.0/glib.getenv). + +```ts +import GLib from "gi://GLib" + +const HOME = GLib.getenv("HOME") +``` + +## Custom SVG symbolic icons + +Put the svgs in a directory following the freedesktop spec, name them +`-symbolic.svg` and use `app.add_icons()` or `icons` parameter in +`app.start()` + +```txt +. +├── icons +│ └── hicolor +│ └── scalable +│ └── actions +│ └── custom-symbolic.svg +└── app.ts +``` + +:::code-group + +```ts [app.ts] +app.start({ + icons: `${SRC}/icons`, // SRC will point to the root + main() { + new Gtk.Image({ + iconName: "custom-symbolic", + }) + }, +}) +``` + +::: + +> [!INFO] +> +> If there is a name clash with an icon from your current icon pack the icon +> pack will take precedence + +## Logging + +The `console` API in GJS uses glib logging functions. If you just want to print +some text as is to stdout use the globally available `print` function or +`printerr` for stderr. + +```ts +print("print this line to stdout") +printerr("print this line to stderr") +``` + +## Auto create Window for each Monitor + +You can use the `` component to auto create/destroy a top-level widget +automatically for each monitor. + +```tsx [app.ts] +import Gtk from "gi://Gtk" +import Bar from "./Bar" +import { For, This, createBinding } from "ags" + +function main() { + const monitors = createBinding(app, "monitors") + + return ( + + {(monitor) => ( + + onCleanup(() => self.destroy())} + /> + {otherWindows} + + )} + + ) +} + +app.start({ main }) +``` + +## Error: Can't convert non-null pointer to JS value + +These happen when accessing list type properties. Gjs fails to correctly bind +`List` and other array like types of Vala as a property. + +```ts +import Notifd from "gi://AstalNotifd" +const notifd = Notifd.get_default() + +notifd.notifications // [!code --] +notifd.get_notifications() // [!code ++] +``` + +> [!TIP] +> +> Open up an issue/PR to add an +> [override](https://github.com/Aylur/ags/blob/main/lib/src/overrides.ts). + +## How to create regular floating windows + +Use `Gtk.Window`. + +By default `Gtk.Window` is destroyed on close. To prevent this add a handler for +`delete-event`. + +```tsx {3-6} +return ( + { + self.hide() + return true + }} + > + {child} + +) +``` + +## Is there a way to limit the width/height of a widget? + +- Gtk3: Unfortunately not. You can set a minimum size with `min-width` and + `min-heigth` css attributes, but you can not set max size. + +- Gtk4: Yes, using custom layout managers. As a shortcut you can use + [Adw.Clamp](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/1.7/class.Clamp.html) + +## How do I register keybindings? + +Only **focused** windows can capture events. To make a window focusable set its +keymode. + +::: code-group + +```tsx [gtk3] + { + if (event.get_keyval()[1] === Gdk.KEY_Escape) { + self.hide() + } + }} +/> +``` + +```tsx [gtk4] + + { + if (keyval === Gdk.KEY_Escape) { + widget.hide() + } + }} + /> + +``` + +::: + +> [!TIP] +> +> If you want global keybindings you have to use your compositor keybinding +> settings. You can define a [request handler](./app-cli#messaging-from-cli) and +> invoke it using a compositor keybinding. + +## How to create a Popup + +- Gtk4: simply use Gtk's builtin + [Popover](https://docs.gtk.org/gtk4/class.Popover.html). + +- Gtk3: you can create an + [Astal.Window](https://aylur.github.io/libastal/astal3/class.Window.html) + instance, position it and handle click events. Checkout + [examples/gtk3/popover](https://github.com/Aylur/ags/tree/main/examples/gtk3/popover) + +## Nested reactive values + +You might want to access deeply nested objects reactively which you can do using +the [``](https://aylur.github.io/gnim/jsx#dynamic-rendering) component or +by using [`createBinding`](https://aylur.github.io/gnim/jsx#createbinding). + +```ts +interface Nested extends GObject.Object { + value: string +} + +interface MyObject extends GObject.Object { + nested: null | Nested +} +``` + +Using the `` component you can conditionally access and bind nested +objects. + +```tsx +function Component() { + const nested: Accessor = createBinding(object, "nested") + + return ( + + {(nested) => + nested && ( + + + ) + } + + ) +} +``` + +Using `createBinding` you can specify multiple properties which will be have as +a "path" to the target property. Note that if any property in the path is +nullable the result will be nullable. + +```tsx +function Component() { + const value: Accessor = createBinding( + object, + "nested", + "value", + ) + + return