|
1 | 1 | # Examples
|
| 2 | + |
| 3 | +You can find some full examples on the |
| 4 | +[repository](https://github.com/Aylur/ags/tree/main/examples). |
| 5 | + |
| 6 | +## Monitor id does not match compositor |
| 7 | + |
| 8 | +The monitor id property that windows expect is mapped by Gdk, which is not always |
| 9 | +the same as the compositor. Instead use the `gdkmonitor` property which expects |
| 10 | +a `Gdk.Monitor` object. |
| 11 | + |
| 12 | +```tsx |
| 13 | +function Bar(gdkmonitor) { |
| 14 | + return <window gdkmonitor={gdkmonitor} /> |
| 15 | +} |
| 16 | + |
| 17 | +function main() { |
| 18 | + for (const monitor of app.get_monitors()) { |
| 19 | + if (monitor.model == "your-desired-model") { |
| 20 | + Bar(monitor) |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +## Environment variables |
| 27 | + |
| 28 | +JavaScript is **not** bash or other shell environments. |
| 29 | + |
| 30 | +```ts |
| 31 | +const HOME = exec("echo $HOME") // does not work |
| 32 | +``` |
| 33 | + |
| 34 | +`exec` and `execAsync` runs the passed program as is, its **not** run in a |
| 35 | +shell environment, so the above example just passes `$HOME` as a string literal |
| 36 | +to the `echo` program. |
| 37 | + |
| 38 | +:::danger Please don't do this |
| 39 | + |
| 40 | +```ts |
| 41 | +const HOME = exec("bash -c 'echo $HOME'") |
| 42 | +``` |
| 43 | + |
| 44 | +::: |
| 45 | + |
| 46 | +You can read environment variables using |
| 47 | +[GLib.getenv](https://gjs-docs.gnome.org/glib20~2.0/glib.getenv). |
| 48 | + |
| 49 | +```ts |
| 50 | +import GLib from "gi://GLib" |
| 51 | + |
| 52 | +const HOME = GLib.getenv("HOME") |
| 53 | +``` |
| 54 | + |
| 55 | +## Custom SVG symbolic icons |
| 56 | + |
| 57 | +Put the svgs in a directory, name them `<icon-name>-symbolic.svg` |
| 58 | +and use `app.add_icons()` or `icons` parameter in `app.start()` |
| 59 | + |
| 60 | +:::code-group |
| 61 | + |
| 62 | +```ts [app.ts] |
| 63 | +app.start({ |
| 64 | + icons: `/path/to/icons`, // this dir should include custom-symbolic.svg |
| 65 | + main() { |
| 66 | + Widget.Icon({ |
| 67 | + icon: "custom-symbolic", // custom-symbolic.svg |
| 68 | + css: "color: green;", // can be colored, like other named icons |
| 69 | + }) |
| 70 | + }, |
| 71 | +}) |
| 72 | +``` |
| 73 | + |
| 74 | +::: |
| 75 | + |
| 76 | +:::info |
| 77 | +If there is a name clash with an icon from your current icon pack |
| 78 | +the icon pack will take precedence |
| 79 | +::: |
| 80 | + |
| 81 | +## Logging |
| 82 | + |
| 83 | +The `console` API in GJS uses glib logging functions. |
| 84 | +If you just want to print some text as is to stdout |
| 85 | +use the globally available `print` function or `printerr` for stderr. |
| 86 | + |
| 87 | +```ts |
| 88 | +print("print this line to stdout") |
| 89 | +printerr("print this line to stderr") |
| 90 | +``` |
| 91 | + |
| 92 | +## Auto create Window for each Monitor |
| 93 | + |
| 94 | +To have Window widgets appear on a monitor when its plugged in, |
| 95 | +listen to `app.monitor-added`. |
| 96 | + |
| 97 | +:::code-group |
| 98 | + |
| 99 | +```tsx [Bar.tsx] |
| 100 | +export default function Bar(gdkmonitor: Gdk.Monitor) { |
| 101 | + return <window gdkmonitor={gdkmonitor} /> |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +::: |
| 106 | + |
| 107 | +:::code-group |
| 108 | + |
| 109 | +```ts [app.ts] |
| 110 | +import Gtk from "gi://Gtk" |
| 111 | +import Gdk from "gi://Gdk" |
| 112 | +import Bar from "./Bar" |
| 113 | + |
| 114 | +function main() { |
| 115 | + const bars = new Map<Gdk.Monitor, Gtk.Widget>() |
| 116 | + |
| 117 | + // initialize |
| 118 | + for (const gdkmonitor of app.get_monitors()) { |
| 119 | + bars.set(gdkmonitor, Bar(gdkmonitor)) |
| 120 | + } |
| 121 | + |
| 122 | + app.connect("monitor-added", (_, gdkmonitor) => { |
| 123 | + bars.set(gdkmonitor, Bar(gdkmonitor)) |
| 124 | + }) |
| 125 | + |
| 126 | + app.connect("monitor-removed", (_, gdkmonitor) => { |
| 127 | + bars.get(gdkmonitor)?.destroy() |
| 128 | + bars.delete(gdkmonitor) |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +app.start({ main }) |
| 133 | +``` |
| 134 | + |
| 135 | +::: |
| 136 | + |
| 137 | +## Error: Can't convert non-null pointer to JS value |
| 138 | + |
| 139 | +These happen when accessing list type properties. Gjs fails to correctly bind |
| 140 | +`List` and other array like types of Vala as a property. |
| 141 | + |
| 142 | +```ts |
| 143 | +import Notifd from "gi://AstalNotifd" |
| 144 | +const notifd = Notifd.get_default() |
| 145 | + |
| 146 | +notifd.notifications // [!code --] |
| 147 | +notifd.get_notifications() // [!code ++] |
| 148 | +``` |
| 149 | + |
| 150 | +:::tip |
| 151 | +Open up an issue/PR to add a [workaround](https://github.com/Aylur/ags/blob/main/lib/src/overrides.ts). |
| 152 | +::: |
| 153 | + |
| 154 | +## How to create regular floating windows |
| 155 | + |
| 156 | +Use `Gtk.Window`. |
| 157 | + |
| 158 | +By default `Gtk.Window` is destroyed on close. |
| 159 | +To prevent this add a handler for `delete-event`. |
| 160 | + |
| 161 | +```tsx {4-7} |
| 162 | +return ( |
| 163 | + <Gtk.Window |
| 164 | + $delete-event={(self) => { |
| 165 | + self.hide() |
| 166 | + return true |
| 167 | + }} |
| 168 | + > |
| 169 | + {child} |
| 170 | + </Gtk.Window> |
| 171 | +) |
| 172 | +``` |
| 173 | + |
| 174 | +## Is there a way to limit the width/height of a widget? |
| 175 | + |
| 176 | +- Gtk3: Unfortunately not. You can set a minimum size with `min-width` and |
| 177 | + `min-heigth` css attributes, but you can not set max size. |
| 178 | + |
| 179 | +- Gtk4: Yes, using custom layout managers. As a shortcut you can use |
| 180 | + [Adw.Clamp](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/1.7/class.Clamp.html) |
| 181 | + |
| 182 | +## How do I register keybindings? |
| 183 | + |
| 184 | +If you want global keybindings use your compositor. |
| 185 | +Only **focused** windows can capture events. To make a window |
| 186 | +focusable set its keymode. |
| 187 | + |
| 188 | +::: code-group |
| 189 | + |
| 190 | +```tsx [gtk3] |
| 191 | +<window |
| 192 | + keymode={Astal.Keymode.ON_DEMAND} |
| 193 | + $key-press-event={(self, event: Gdk.Event) => { |
| 194 | + if (event.get_keyval()[1] === Gdk.KEY_Escape) { |
| 195 | + self.hide() |
| 196 | + } |
| 197 | + }} |
| 198 | +/> |
| 199 | +``` |
| 200 | + |
| 201 | +```tsx [gtk4] |
| 202 | +<window keymode={Astal.Keymode.ON_DEMAND}> |
| 203 | + <Gtk.EventControllerKey |
| 204 | + $key-pressed={({ widget }, keyval: number) => { |
| 205 | + if (keyval === Gdk.KEY_Escape) { |
| 206 | + widget.hide() |
| 207 | + } |
| 208 | + }} |
| 209 | + /> |
| 210 | +</window> |
| 211 | +``` |
| 212 | + |
| 213 | +::: |
| 214 | + |
| 215 | +## How to create a Popup |
| 216 | + |
| 217 | +- Gtk4: simply use Gtk's builtin |
| 218 | + [Popover](https://docs.gtk.org/gtk4/class.Popover.html). |
| 219 | + |
| 220 | +- Gtk3: you can create an |
| 221 | + [Astal.Window](https://aylur.github.io/libastal/astal3/class.Window.html) |
| 222 | + instance, position it and handle click events. |
| 223 | + Checkout [examples/gtk3/popover](https://github.com/Aylur/ags/tree/main/examples/gtk3/popover) |
0 commit comments