repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(nuguland): add persistent widget-usage tracker

4df0f3efcfa91fa73ea7777de41928099f73edf9

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Usage.qml counts how often each bar widget is opened, persisting to
$XDG_STATE_HOME via FileView+JsonAdapter. Exposes a usage-sorted layout
order, snapshotted at load so the bar stays stable within a session and
re-sorts across restarts.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Changed files (1)

quickshell/nuguland/Usage.qml

Added
new file mode 100644
1 +import QtQuick
2 +import Quickshell
3 +import Quickshell.Io
5 +// Persistent widget-usage tracker: counts how often each bar widget is
6 +// opened and exposes a usage-sorted layout order. The order is snapshotted
7 +// at file load so the bar stays stable within a session and re-sorts across
8 +// restarts, while counts keep accumulating live and persist to disk.
9 +Item {
10 + id: root
12 + // Canonical widget set; array order is the tie-break for equal counts.
13 + readonly property var widgets: ["clock", "launcher", "media", "system", "clipboard", "audio", "brightness", "connectivity", "power", "notifications", "tray", "idle"]
15 + // Snapshotted at load (see FileView.onLoaded); the bar binds to this.
16 + property var layoutOrder: widgets
18 + function countFor(name) {
19 + const counts = usageAdapter.counts || ({
20 + });
21 + return Number(counts[String(name)] || 0);
22 + }
24 + // widgets sorted by usage desc, ties broken by canonical order
25 + function ordered() {
26 + return root.widgets.slice().map((name, index) => {
27 + return {
28 + "name": name,
29 + "index": index,
30 + "count": root.countFor(name)
31 + };
32 + }).sort((a, b) => {
33 + return (b.count - a.count) || (a.index - b.index);
34 + }).map((entry) => {
35 + return entry.name;
36 + });
37 + }
39 + function record(name) {
40 + const key = String(name);
41 + if (root.widgets.indexOf(key) < 0)
42 + return root.stateJson();
44 + const next = Object.assign({
45 + }, usageAdapter.counts);
46 + next[key] = (Number(next[key]) || 0) + 1;
47 + usageAdapter.counts = next;
48 + return root.stateJson();
49 + }
51 + function stateJson() {
52 + return JSON.stringify({
53 + "backend": "Usage.qml",
54 + "path": usageFile.path,
55 + "counts": usageAdapter.counts || ({
56 + }),
57 + "layoutOrder": root.layoutOrder,
58 + "liveOrder": root.ordered()
59 + });
60 + }
62 + FileView {
63 + id: usageFile
65 + path: Quickshell.statePath("nuguland-widget-usage.json")
66 + watchChanges: true
67 + onFileChanged: reload()
68 + onAdapterUpdated: writeAdapter()
69 + onLoaded: root.layoutOrder = root.ordered()
70 + onLoadFailed: root.layoutOrder = root.widgets
72 + JsonAdapter {
73 + id: usageAdapter
75 + property var counts: ({
76 + })
77 + }
79 + }