# Patterns Reusable recipes. Each pattern solves a recurring shape of problem. Combine them. ## P1. Per-monitor instantiation ```qml import Quickshell Variants { model: Quickshell.screens delegate: Component { PanelWindow { required property var modelData screen: modelData // ... rest of bar } } } ``` `modelData` is the per-iteration value. Always mark it `required` so qmlls catches missing bindings. `Quickshell.screens` is a `list` — each has `name`, `width`, `height`, `scale`. Variants survives monitor hot-plug: when a screen disconnects, its instance is destroyed; when it reconnects, a fresh one is created. ## P2. Live-updating clock ```qml PanelWindow { SystemClock { id: clock; precision: SystemClock.Seconds } Text { text: Qt.formatTime(clock.date, "HH:mm:ss") } } ``` `SystemClock` is **not** a singleton — you must instantiate it. `clock.date` is a reactive JS `Date`; format it with `Qt.formatTime` / `Qt.formatDate` / `Qt.formatDateTime`. Updates trigger within 50ms of the system clock changing. ## P3. Run a shell command, capture output ```qml import Quickshell.Io Process { id: proc command: ["wpctl", "get-volume", "@DEFAULT_AUDIO_SINK@"] running: true stdout: StdioCollector { onStreamFinished: lastOutput.text = this.text } } ``` `StdioCollector` is a `Property`-like that becomes the full output string after the process closes stdout. **One process, many widgets** — don't spawn per widget (memory + startup cost). ## P4. Long-running stream, regex match per line ```qml import Quickshell.Io Process { command: ["some-streaming-tool"] running: true stdout: SplitParser { property var pattern: /focusedmon>>(.+),/ onRead: msg => { const m = pattern.exec(msg) if (m) doStuff(m[1]) } } } ``` `SplitParser` splits on newlines by default; you can override with `split`. Each line fires `onRead(line)`. Use this for any event-stream consumer (Hyprland socket, log tailer, status feed). ## P5. Bind a service object's state ```qml import Quickshell.Services.Pipewire PwObjectTracker { objects: [ Pipewire.defaultAudioSink ] } Text { text: `vol: ${(Pipewire.defaultAudioSink?.audio.volume ?? 0) * 100 | 0}%` } ``` `PwObjectTracker` is a sentinel — it does nothing visible, but its presence keeps the object alive in the service's internal model. Without it, the binding can drop to `null`. Pattern: every time you read a non-singleton service object, declare a tracker for it. Same for `Mpris.players`, `UPower.devices`, `Bluetooth.adapters`. The `?.` optional chain is the safe way to read state that might not be present yet (e.g. before the service has connected). ## P6. React to a service signal ```qml import Quickshell.Services.Pipewire Connections { target: Pipewire.defaultAudioSink?.audio function onVolumeChanged() { flashOsd() } } ``` `Connections` is the idiomatic way to listen to a target's signals. The `target` must be live — wrap with `?.` and use the `target:` binding (not a static `target:` assignment) to make it reactive. The handler name follows `on` convention. ## P7. Show a window only when a condition holds ```qml // Cheap: always in memory, just hides Item { visible: showIt } // Frees memory: builds only when active LazyLoader { active: showIt; PanelWindow { ... } } ``` Use `LazyLoader` for windows and other non-`Item` roots. The OSD-on-volume-change pattern is exactly this: ```qml LazyLoader { active: showOsd PanelWindow { /* the OSD UI */ } } ``` The window is destroyed when `active` flips to false. Good for rare/expensive UI. ## P8. Click-through overlay ```qml PanelWindow { color: "transparent" mask: Region {} // empty = no input } ``` The `Region` input mask is the only correct way to make a panel ignore clicks while still rendering. For a window that should be opaque to clicks, leave `mask` unset. ## P9. System icon ```qml import Quickshell.Widgets IconImage { source: Quickshell.iconPath("audio-volume-high-symbolic") implicitSize: 24 } ``` `Quickshell.iconPath` looks up the name in the current icon theme. Three variants: missing icon → purple/black square (default), fallback to a name, or empty string. If the purple square bugs you, use the empty-string overload and `source !== ""` to gate the `IconImage`. ## P10. Custom file-bound properties (config-as-code) ```qml import Quickshell.Io JsonAdapter { id: config path: Qt.resolvedUrl("config.json") // adapter is auto-generated; properties become QML properties } ``` `JsonAdapter` reads a JSON file at startup and exposes its top-level keys as reactive QML properties. Edits to the file are picked up on next reload. Use this for user-tweakable config (theme, layout offsets, monitor list). ## P11. Multi-file shell ``` ~/.config/quickshell/mybar/ ├── shell.qml # entry, contains the Variants ├── Bar.qml # the bar component (implicit import) ├── Clock.qml # implicit import └── widgets/ └── Volume.qml # import via: import qs.widgets as W ``` Inside `shell.qml`: ```qml import qs.widgets as W import "." // or use the implicit Bar / Clock from this directory Variants { model: Quickshell.screens delegate: Bar { screen: modelData } } ``` Rules: - `*.qml` next to `shell.qml` with PascalCase name → implicitly importable. - `widgets/*.qml` etc. → import with `import qs.widgets as W` (LSP-friendly, v0.2.0+). - `import "relative/path"` works but loses LSP coverage; prefer `qs.*`. ## P12. Version-gate a feature ```qml import Quickshell pragma ComponentBehavior: bound Loader { active: Quickshell.hasVersion("0.3.0") sourceComponent: someNewFeature } ``` `Quickshell.hasVersion` is the API; the `#pragma`-style preprocessor (different syntax) lets you gate by version at parse time. Use these when you want the same shell.qml to work across quickshell releases. ## P13. Reuse state across components Put the state in the `Scope` root: ```qml Scope { id: root property string currentWallpaper: "/path/to/wp" property int volumeStep: 5 Variants { model: Quickshell.screens; delegate: Component { Bar { ... } } } LazyLoader { active: showMixer; Mixer { ... } } } ``` Components declared inside `Scope` can reach `root.foo` by id. If you find yourself passing the same property into 4+ components, hoist it to `Scope`. ## P14. Custom re-usable component file ```qml // MyButton.qml (PascalCase, placed in same dir as shell.qml or imported via qs.*) import QtQuick import QtQuick.Controls Button { property string iconName: "" property var onActivate: () => {} icon.name: iconName onClicked: onActivate() } ``` Consume from `shell.qml` (auto-imported) or `import qs.widgets as W; W.MyButton { ... }`. Components are just `Item`-like root objects — anything that's a QML object can be a component. ## P15. Hide the reload popup If you have your own reloader indicator, you can disable the built-in one (v0.3.0+): ```qml import Quickshell ShellRoot { // The default reload popup is added automatically. // To replace it, add a ReloadPopup from your own module path. // (See official examples/reload-popup.) } ``` The default reload popup shows on every file save. For noisier iteration, replace it with a smaller indicator or disable via `QuickshellSettings`. ## P16. Process IPC (call into quickshell from outside) ```qml import Quickshell.Io IpcHandler { target: "myShell" // becomes the IPC namespace function toggleBar(): void { showBar = !showBar } } ``` Call it from the shell: ```sh qs -c mybar ipc call myShell toggleBar ``` Use this for keybind triggers, scripts, and external automation.