Consolidate utils
This commit is contained in:
+15
-41
@@ -101,40 +101,14 @@ Item {
|
||||
return /^(top|bottom|left|right)$/.test(next) ? next : "top"
|
||||
}
|
||||
|
||||
function shellQuote(value) {
|
||||
return "'" + String(value).replace(/'/g, "'\\''") + "'"
|
||||
}
|
||||
|
||||
function fileUrl(path) {
|
||||
return "file://" + path.split("/").map(encodeURIComponent).join("/")
|
||||
}
|
||||
|
||||
function isPlainObject(value) {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function normalizeLayoutEntry(entry) {
|
||||
if (typeof entry === "string") return { id: entry }
|
||||
if (isPlainObject(entry) && entry.id) return entry
|
||||
return null
|
||||
}
|
||||
|
||||
function normalizeLayoutSection(list) {
|
||||
if (!Array.isArray(list)) return []
|
||||
var result = []
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
var normalized = normalizeLayoutEntry(list[i])
|
||||
if (normalized) result.push(normalized)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Apply tray-pinning on top of the shared layout normalization so the
|
||||
// bar host and the bar settings panel can't drift on entry shape.
|
||||
function normalizeLayout(layout) {
|
||||
if (!isPlainObject(layout)) layout = fallbackBarConfig.layout
|
||||
var normalized = Util.normalizeLayout(Util.isPlainObject(layout) ? layout : fallbackBarConfig.layout)
|
||||
return {
|
||||
left: pinTrayToInner(normalizeLayoutSection(layout.left), "left"),
|
||||
center: pinTrayToInner(normalizeLayoutSection(layout.center), "center"),
|
||||
right: pinTrayToInner(normalizeLayoutSection(layout.right), "right")
|
||||
left: pinTrayToInner(normalized.left, "left"),
|
||||
center: pinTrayToInner(normalized.center, "center"),
|
||||
right: pinTrayToInner(normalized.right, "right")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +131,7 @@ Item {
|
||||
}
|
||||
|
||||
function applyBarConfig() {
|
||||
var config = isPlainObject(barConfig) ? barConfig : fallbackBarConfig
|
||||
var config = Util.isPlainObject(barConfig) ? barConfig : fallbackBarConfig
|
||||
|
||||
position = normalizePosition(config.position)
|
||||
transparent = config.transparent === true
|
||||
@@ -175,7 +149,7 @@ Item {
|
||||
}
|
||||
|
||||
function entrySettings(entry) {
|
||||
if (!isPlainObject(entry)) return {}
|
||||
if (!Util.isPlainObject(entry)) return {}
|
||||
var copy = {}
|
||||
for (var key in entry) {
|
||||
if (key === "id") continue
|
||||
@@ -186,7 +160,7 @@ Item {
|
||||
|
||||
function entryId(entry) {
|
||||
if (typeof entry === "string") return entry
|
||||
if (isPlainObject(entry) && entry.id) return String(entry.id)
|
||||
if (Util.isPlainObject(entry) && entry.id) return String(entry.id)
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -270,7 +244,7 @@ Item {
|
||||
if (!source && customModuleSafeName(name))
|
||||
source = omarchyConfigDir + "/bar/modules/" + String(name) + ".qml"
|
||||
|
||||
return source ? fileUrl(source) : ""
|
||||
return source ? Util.fileUrl(source) : ""
|
||||
}
|
||||
|
||||
// First-party widgets are registered with the BarWidgetRegistry at startup.
|
||||
@@ -357,7 +331,7 @@ Item {
|
||||
var nextTransparent = !(root.transparent === true)
|
||||
if (root.shell && typeof root.shell.mutateShellConfig === "function") {
|
||||
root.shell.mutateShellConfig(function(config) {
|
||||
if (!root.isPlainObject(config.bar)) config.bar = {}
|
||||
if (!Util.isPlainObject(config.bar)) config.bar = {}
|
||||
config.bar.transparent = nextTransparent
|
||||
})
|
||||
} else {
|
||||
@@ -470,7 +444,7 @@ Item {
|
||||
|
||||
var name = value.substring(0, markerIndex).split("/").pop()
|
||||
var iconPath = value.substring(markerIndex + marker.length).split("&")[0]
|
||||
return fileUrl(iconPath + "/hicolor/16x16/status/" + name + ".png")
|
||||
return Util.fileUrl(iconPath + "/hicolor/16x16/status/" + name + ".png")
|
||||
}
|
||||
|
||||
function trayTooltip(item) {
|
||||
@@ -478,7 +452,7 @@ Item {
|
||||
}
|
||||
|
||||
function focusWorkspace(id) {
|
||||
root.run("hyprctl dispatch " + shellQuote("hl.dsp.focus({ workspace = \"" + id + "\" })"))
|
||||
root.run("hyprctl dispatch " + Util.shellQuote("hl.dsp.focus({ workspace = \"" + id + "\" })"))
|
||||
}
|
||||
|
||||
function clockEntry() {
|
||||
@@ -562,7 +536,7 @@ Item {
|
||||
|
||||
Process {
|
||||
id: screenRecordingProc
|
||||
command: ["bash", "-lc", root.shellQuote(root.omarchyPath + "/shell/scripts/indicators/screen-recording.sh")]
|
||||
command: ["bash", "-lc", Util.shellQuote(root.omarchyPath + "/shell/scripts/indicators/screen-recording.sh")]
|
||||
stdout: StdioCollector {
|
||||
waitForEnd: true
|
||||
onStreamFinished: root.updateIndicator("screenRecording", text)
|
||||
@@ -571,7 +545,7 @@ Item {
|
||||
|
||||
Process {
|
||||
id: notificationSilencingProc
|
||||
command: ["bash", "-lc", root.shellQuote(root.omarchyPath + "/shell/scripts/indicators/notification-silencing.sh")]
|
||||
command: ["bash", "-lc", Util.shellQuote(root.omarchyPath + "/shell/scripts/indicators/notification-silencing.sh")]
|
||||
stdout: StdioCollector {
|
||||
waitForEnd: true
|
||||
onStreamFinished: root.updateIndicator("notifications", text)
|
||||
|
||||
@@ -316,8 +316,8 @@ Item {
|
||||
if (!node) return
|
||||
Pipewire.preferredDefaultAudioSink = node
|
||||
if (root.bar && node.id !== undefined && node.name) {
|
||||
var idArg = root.bar.shellQuote(String(node.id))
|
||||
var nameArg = root.bar.shellQuote(String(node.name))
|
||||
var idArg = Util.shellQuote(String(node.id))
|
||||
var nameArg = Util.shellQuote(String(node.name))
|
||||
root.bar.run("wpctl set-default " + idArg + " 2>/dev/null || true; "
|
||||
+ "pactl set-default-sink " + nameArg + " 2>/dev/null || true; "
|
||||
+ "pactl list short sink-inputs 2>/dev/null | awk '{ print $1 }' | while read -r input; do "
|
||||
@@ -329,8 +329,8 @@ Item {
|
||||
if (!node) return
|
||||
Pipewire.preferredDefaultAudioSource = node
|
||||
if (root.bar && node.id !== undefined && node.name) {
|
||||
var idArg = root.bar.shellQuote(String(node.id))
|
||||
var nameArg = root.bar.shellQuote(String(node.name))
|
||||
var idArg = Util.shellQuote(String(node.id))
|
||||
var nameArg = Util.shellQuote(String(node.name))
|
||||
root.bar.run("wpctl set-default " + idArg + " 2>/dev/null || true; "
|
||||
+ "pactl set-default-source " + nameArg + " 2>/dev/null || true; "
|
||||
+ "pactl list short source-outputs 2>/dev/null | awk '{ print $1 }' | while read -r output; do "
|
||||
|
||||
@@ -149,7 +149,7 @@ Item {
|
||||
if (!net) return
|
||||
if (net.connected) { disconnect(net.ssid); return }
|
||||
if (isProtected(net.security) && !net.known) {
|
||||
var quotedSsid = bar.shellQuote(net.ssid)
|
||||
var quotedSsid = Util.shellQuote(net.ssid)
|
||||
knownCheck.targetSsid = net.ssid
|
||||
knownCheck.command = ["bash", "-c", `
|
||||
iwctl known-networks list 2>/dev/null \\
|
||||
@@ -193,7 +193,7 @@ iwctl known-networks list 2>/dev/null \\
|
||||
|
||||
function copyToClipboard(value) {
|
||||
if (!value || !root.bar) return
|
||||
Quickshell.execDetached(["bash", "-lc", "printf %s " + root.bar.shellQuote(value) + " | wl-copy"])
|
||||
Quickshell.execDetached(["bash", "-lc", "printf %s " + Util.shellQuote(value) + " | wl-copy"])
|
||||
}
|
||||
|
||||
function networkCommand() {
|
||||
@@ -374,8 +374,8 @@ iwctl station "$station" get-networks rssi-dbms 2>/dev/null \\
|
||||
}
|
||||
|
||||
function dnsCommand(provider) {
|
||||
var command = root.bar ? root.bar.shellQuote(root.bar.omarchyPath + "/bin/omarchy-dns") : "omarchy-dns"
|
||||
if (provider) command += " " + root.bar.shellQuote(provider)
|
||||
var command = root.bar ? Util.shellQuote(root.bar.omarchyPath + "/bin/omarchy-dns") : "omarchy-dns"
|
||||
if (provider) command += " " + Util.shellQuote(provider)
|
||||
return command
|
||||
}
|
||||
|
||||
@@ -383,8 +383,8 @@ iwctl station "$station" get-networks rssi-dbms 2>/dev/null \\
|
||||
if (!root.bar || !provider || actionProc.running) return
|
||||
|
||||
if (provider === "Custom") {
|
||||
var launcher = root.bar.shellQuote(root.bar.omarchyPath + "/bin/omarchy-launch-floating-terminal-with-presentation")
|
||||
root.bar.run(launcher + " " + root.bar.shellQuote(root.dnsCommand(provider)))
|
||||
var launcher = Util.shellQuote(root.bar.omarchyPath + "/bin/omarchy-launch-floating-terminal-with-presentation")
|
||||
root.bar.run(launcher + " " + Util.shellQuote(root.dnsCommand(provider)))
|
||||
root.closePopout()
|
||||
return
|
||||
}
|
||||
@@ -415,7 +415,7 @@ iwctl station "$station" get-networks rssi-dbms 2>/dev/null \\
|
||||
}
|
||||
|
||||
function connectKnown(ssid) {
|
||||
var quotedSsid = bar.shellQuote(ssid)
|
||||
var quotedSsid = Util.shellQuote(ssid)
|
||||
runAction("connect", ssid, `
|
||||
station=$(iwctl station list 2>/dev/null | sed -e 's/\\x1b\\[[0-9;]*m//g' | awk '/^[[:space:]]*wl/ { print $1; exit }')
|
||||
[[ -z $station ]] && { echo "no Wi-Fi station available" >&2; exit 1; }
|
||||
@@ -424,8 +424,8 @@ iwctl --dont-ask station "$station" connect ${quotedSsid}
|
||||
}
|
||||
|
||||
function connectWithPassphrase(ssid, passphrase) {
|
||||
var quotedSsid = bar.shellQuote(ssid)
|
||||
var quotedPass = bar.shellQuote(passphrase)
|
||||
var quotedSsid = Util.shellQuote(ssid)
|
||||
var quotedPass = Util.shellQuote(passphrase)
|
||||
runAction("connect", ssid, `
|
||||
station=$(iwctl station list 2>/dev/null | sed -e 's/\\x1b\\[[0-9;]*m//g' | awk '/^[[:space:]]*wl/ { print $1; exit }')
|
||||
[[ -z $station ]] && { echo "No Wi-Fi station available" >&2; exit 1; }
|
||||
@@ -452,7 +452,7 @@ iwctl station "$station" disconnect
|
||||
// success. If the station isn't on this SSID (or there is no station),
|
||||
// skip straight to forget.
|
||||
function forget(ssid) {
|
||||
var quotedSsid = bar.shellQuote(ssid)
|
||||
var quotedSsid = Util.shellQuote(ssid)
|
||||
runAction("forget", ssid, `
|
||||
station=$(iwctl station list 2>/dev/null | sed -e 's/\\x1b\\[[0-9;]*m//g' | awk '/^[[:space:]]*wl/ { print $1; exit }')
|
||||
if [[ -n $station ]]; then
|
||||
@@ -1098,7 +1098,7 @@ fi
|
||||
// held a reference to the row delegate it could be destroyed by a
|
||||
// model refresh, and a rapid second click would overwrite it and
|
||||
// misroute the first result.
|
||||
var quotedSsid = root.bar.shellQuote(row.net.ssid)
|
||||
var quotedSsid = Util.shellQuote(row.net.ssid)
|
||||
knownCheck.targetSsid = row.net.ssid
|
||||
knownCheck.command = ["bash", "-c", `
|
||||
iwctl known-networks list 2>/dev/null \\
|
||||
|
||||
@@ -552,7 +552,7 @@ Item {
|
||||
// Poll the weather pill text/class every minute. Local to this widget.
|
||||
Process {
|
||||
id: weatherProc
|
||||
command: ["bash", "-lc", root.bar ? root.bar.shellQuote(root.bar.omarchyPath + "/shell/scripts/weather.sh") : ""]
|
||||
command: ["bash", "-lc", root.bar ? Util.shellQuote(root.bar.omarchyPath + "/shell/scripts/weather.sh") : ""]
|
||||
stdout: StdioCollector {
|
||||
waitForEnd: true
|
||||
onStreamFinished: root.updateWeather(text)
|
||||
|
||||
Reference in New Issue
Block a user