App grid for a phone shell on niri
This commit is contained in:
commit
4f70f27a44
1 changed files with 375 additions and 0 deletions
268
grid.js
Normal file
268
grid.js
Normal file
|
|
@ -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([])
|
||||
Loading…
Reference in a new issue