commit 4f70f27a44ee8c6b0387fca028f073b47813fe14 Author: Henry-Hiles Date: Mon Nov 16 18:35:24 2026 -0500 App grid for a phone shell on niri diff --git a/README.md b/README.md new file mode 100644 index 0000000..11c1e2a --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# app-grid + +An app grid for a phone shell on [niri](https://github.com/niri-wm/niri), as a GTK4 layer-shell surface. + +It's meant to sit in niri's overview backdrop, behind the workspaces, so it shows up as the overview opens: + +```kdl +layer-rule { + match namespace="^app-grid$" + place-within-backdrop true + backdrop-input true +} +``` + +`place-within-backdrop` on the bottom layer and `backdrop-input` aren't in upstream niri yet. + +The grid's position assumes `overview { zoom 0.25; workspace-anchor 0.15; }`, see the constants at the top of `grid.js`. + +Launching an app opens a splash window with its icon until the app's window opens, using `niri msg event-stream`. Set `NIRI_BIN` if `niri` isn't on `PATH`. diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..e82b598 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1794537146, + "narHash": "sha256-Wpz/7x/YWnDIBu7yj6ixe8j6fIMsidd9Y+o1PqPF3iM=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "d5cc6833999746cffdfe0598f7bc801a57b887a9", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..70db73a --- /dev/null +++ b/flake.nix @@ -0,0 +1,22 @@ +{ + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + + outputs = + { self, nixpkgs, ... }: + let + forAllSystems = nixpkgs.lib.genAttrs [ + "x86_64-linux" + "aarch64-linux" + ]; + in + { + # Use this to build the grid with your own package set, e.g. when cross-compiling + overlays.default = final: prev: { + app-grid = final.callPackage ./package.nix { }; + }; + + packages = forAllSystems (system: { + default = (nixpkgs.legacyPackages.${system}.extend self.overlays.default).app-grid; + }); + }; +} diff --git a/grid.js b/grid.js new file mode 100644 index 0000000..075a21f --- /dev/null +++ b/grid.js @@ -0,0 +1,268 @@ +import Adw from "gi://Adw?version=1" +import Gdk from "gi://Gdk?version=4.0" +import Gio from "gi://Gio" +import GLib from "gi://GLib" +import Gtk from "gi://Gtk?version=4.0" +import LayerShell from "gi://Gtk4LayerShell" + +// Keep in sync with the overview section in the niri config. +const ZOOM = 0.25 +const WORKSPACE_ANCHOR = 0.15 +const GAP = 24 + +const COLUMNS = 5 +const ROWS = 4 + +// Close the splash anyway if the app never opens a window. +const SPLASH_TIMEOUT_SECONDS = 20 + +const CSS = ` +window.grid { + background: none; +} +.app { + padding: 8px; + border-radius: 16px; + background: none; + color: white; +} +.app:hover { + background: alpha(white, 0.1); +} +` + +const niriBin = GLib.getenv("NIRI_BIN") ?? "niri" + +function closeOverview() { + GLib.spawn_command_line_async(`${niriBin} msg action close-overview`) +} + +const windows = new Map() +const pendingSplashes = new Set() +let appInfoMonitor = null + +function onWindowOpened(window) { + const isNew = !windows.has(window.id) + windows.set(window.id, window) + if (!isNew) return + + for (const pending of pendingSplashes) { + if (pending.matches(window.app_id)) pending.finish() + } +} + +function watchNiriEvents() { + const proc = Gio.Subprocess.new( + [niriBin, "msg", "--json", "event-stream"], + Gio.SubprocessFlags.STDOUT_PIPE, + ) + const stdout = new Gio.DataInputStream({ + base_stream: proc.get_stdout_pipe(), + }) + + const readLine = () => { + stdout.read_line_async(GLib.PRIORITY_DEFAULT, null, (stream, res) => { + const [line] = stream.read_line_finish_utf8(res) + if (line === null) return + + const event = JSON.parse(line) + if (event.WindowsChanged) { + windows.clear() + for (const window of event.WindowsChanged.windows) + windows.set(window.id, window) + } else if (event.WindowOpenedOrChanged) { + onWindowOpened(event.WindowOpenedOrChanged.window) + } else if (event.WindowClosed) { + windows.delete(event.WindowClosed.id) + } + + readLine() + }) + } + readLine() +} + +function appIds(info) { + const ids = [] + const id = info.get_id() + if (id) ids.push(id.replace(/\.desktop$/, "")) + const wmClass = info.get_startup_wm_class?.() + if (wmClass) ids.push(wmClass) + return ids.map((id) => id.toLowerCase()) +} + +function openSplash(app, info) { + const ids = appIds(info) + + if ( + [...windows.values()].some((w) => ids.includes(w.app_id?.toLowerCase())) + ) + return + + const box = new Gtk.Box({ + orientation: Gtk.Orientation.VERTICAL, + spacing: 24, + halign: Gtk.Align.CENTER, + valign: Gtk.Align.CENTER, + }) + box.append(new Gtk.Image({ gicon: info.get_icon(), pixel_size: 128 })) + box.append( + new Gtk.Label({ + label: info.get_display_name(), + css_classes: ["title-1"], + }), + ) + + const splash = new Gtk.ApplicationWindow({ + application: app, + title: info.get_display_name(), + child: box, + }) + + let timeout = 0 + const pending = { + matches: (appId) => ids.includes(appId?.toLowerCase()), + finish: () => { + pendingSplashes.delete(pending) + if (timeout) GLib.source_remove(timeout) + splash.destroy() + }, + } + timeout = GLib.timeout_add_seconds( + GLib.PRIORITY_DEFAULT, + SPLASH_TIMEOUT_SECONDS, + () => { + timeout = 0 + pending.finish() + return GLib.SOURCE_REMOVE + }, + ) + pendingSplashes.add(pending) + + splash.present() +} + +function appButton(app, info) { + const box = new Gtk.Box({ + orientation: Gtk.Orientation.VERTICAL, + spacing: 6, + }) + box.append(new Gtk.Image({ gicon: info.get_icon(), pixel_size: 48 })) + box.append( + new Gtk.Label({ + label: info.get_display_name(), + ellipsize: 3, + max_width_chars: 8, + }), + ) + + const button = new Gtk.Button({ child: box, css_classes: ["app"] }) + button.connect("clicked", () => { + openSplash(app, info) + info.launch([], Gdk.Display.get_default().get_app_launch_context()) + closeOverview() + }) + return button +} + +const app = new Adw.Application({ application_id: "com.henryhiles.AppGrid" }) + +app.connect("activate", () => { + const win = new Gtk.ApplicationWindow({ + application: app, + css_classes: ["grid"], + }) + + LayerShell.init_for_window(win) + LayerShell.set_namespace(win, "app-grid") + LayerShell.set_layer(win, LayerShell.Layer.BOTTOM) + LayerShell.set_keyboard_mode(win, LayerShell.KeyboardMode.NONE) + LayerShell.set_exclusive_zone(win, -1) + for (const edge of [ + LayerShell.Edge.LEFT, + LayerShell.Edge.RIGHT, + LayerShell.Edge.TOP, + LayerShell.Edge.BOTTOM, + ]) + LayerShell.set_anchor(win, edge, true) + + const monitor = win.get_display().get_monitors().get_item(0) + const updateMargin = () => { + const height = monitor.get_geometry().height + const stripBottom = height * (WORKSPACE_ANCHOR * (1 - ZOOM) + ZOOM) + LayerShell.set_margin( + win, + LayerShell.Edge.TOP, + Math.round(stripBottom + GAP), + ) + } + monitor.connect("notify::geometry", updateMargin) + updateMargin() + + const css = new Gtk.CssProvider() + css.load_from_string(CSS) + Gtk.StyleContext.add_provider_for_display( + Gdk.Display.get_default(), + css, + Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION, + ) + + const carousel = new Adw.Carousel({ vexpand: true }) + + const fillPages = () => { + const position = Math.round(carousel.position) + for ( + let page = carousel.get_first_child(); + page; + page = carousel.get_first_child() + ) + carousel.remove(page) + + const apps = Gio.AppInfo.get_all() + .filter((info) => info.should_show()) + .sort((a, b) => + a.get_display_name().localeCompare(b.get_display_name()), + ) + + const perPage = COLUMNS * ROWS + for (let start = 0; start < apps.length; start += perPage) { + const page = new Gtk.Grid({ + row_homogeneous: true, + column_homogeneous: true, + hexpand: true, + margin_start: 12, + margin_end: 12, + }) + + for (let i = 0; i < perPage; i++) { + const info = apps[start + i] + const child = info ? appButton(app, info) : new Gtk.Box() + page.attach(child, i % COLUMNS, Math.floor(i / COLUMNS), 1, 1) + } + + carousel.append(page) + } + + const pages = carousel.get_n_pages() + if (pages > 0) + carousel.scroll_to( + carousel.get_nth_page(Math.min(position, pages - 1)), + false, + ) + } + fillPages() + + appInfoMonitor = Gio.AppInfoMonitor.get() + appInfoMonitor.connect("changed", fillPages) + + const box = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL }) + box.append(carousel) + box.append(new Adw.CarouselIndicatorDots({ carousel, margin_bottom: 12 })) + + win.set_child(box) + win.present() + + watchNiriEvents() +}) + +app.run([]) diff --git a/package.nix b/package.nix new file mode 100644 index 0000000..04cfe9c --- /dev/null +++ b/package.nix @@ -0,0 +1,39 @@ +{ + lib, + stdenvNoCC, + gjs, + gtk4, + gtk4-layer-shell, + libadwaita, + gobject-introspection, + makeWrapper, + wrapGAppsHook4, +}: +stdenvNoCC.mkDerivation { + pname = "app-grid"; + version = "0.1.0"; + src = ./grid.js; + dontUnpack = true; + + nativeBuildInputs = [ + gobject-introspection + makeWrapper + wrapGAppsHook4 + ]; + buildInputs = [ + gjs + gtk4 + gtk4-layer-shell + libadwaita + ]; + + installPhase = '' + install -Dm644 $src $out/share/app-grid/grid.js + # gtk4-layer-shell has to be loaded before libwayland-client. + makeWrapper ${lib.getExe' gjs "gjs"} $out/bin/app-grid \ + --add-flags "-m $out/share/app-grid/grid.js" \ + --set LD_PRELOAD ${gtk4-layer-shell}/lib/libgtk4-layer-shell.so + ''; + + meta.mainProgram = "app-grid"; +}