From ce9afec2a879946306a19c545d47fd5ddfba479c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 20 May 2026 14:28:50 +0200 Subject: [PATCH] Just keep alpha in util --- shell/Commons/Color.qml | 10 ++------ shell/Commons/Style.qml | 36 ++++++++++------------------ shell/Commons/Util.qml | 15 ++++++++++-- shell/Ui/SearchableDropdown.qml | 2 +- shell/plugins/osd/Osd.qml | 4 ++-- shell/plugins/polkit/PolkitAgent.qml | 2 +- 6 files changed, 31 insertions(+), 38 deletions(-) diff --git a/shell/Commons/Color.qml b/shell/Commons/Color.qml index 773a638b..8df6e5b0 100644 --- a/shell/Commons/Color.qml +++ b/shell/Commons/Color.qml @@ -31,18 +31,12 @@ QtObject { if (typeof v !== "string" || v.length === 0) return fallback var n = Number(v) if (!isFinite(n)) return fallback - return Math.max(0, Math.min(1, n)) - } - - function alpha(c, opacity) { - if (!c) return Qt.rgba(0, 0, 0, opacity) - if (typeof c === "string") c = Qt.color(c) - return Qt.rgba(c.r, c.g, c.b, opacity) + return Util.clampAlpha(n) } // Compose a color from a base-color key and its `-alpha` companion. function composed(colorKey, alphaKey, colorFallback, alphaFallback) { - return alpha(pick(colorKey, colorFallback), pickAlpha(alphaKey, alphaFallback)) + return Util.alpha(pick(colorKey, colorFallback), pickAlpha(alphaKey, alphaFallback)) } readonly property QtObject bar: QtObject { diff --git a/shell/Commons/Style.qml b/shell/Commons/Style.qml index 8a881f01..95af0b71 100644 --- a/shell/Commons/Style.qml +++ b/shell/Commons/Style.qml @@ -51,14 +51,8 @@ QtObject { return n === null ? fallback : n } - function clampAlpha(value) { - var n = Number(value) - if (!isFinite(n)) return 0 - return Math.max(0, Math.min(1, n)) - } - function styleAlpha(key, fallback) { - return clampAlpha(styleNum(key, fallback)) + return Util.clampAlpha(styleNum(key, fallback)) } function styleString(key, fallback) { @@ -92,12 +86,6 @@ QtObject { readonly property real selectedBorderAlpha: styleAlpha("selected-border-alpha", 1.0) readonly property real focusBorderAlpha: styleAlpha("focus-border-alpha", hoverBorderAlpha) - function alpha(c, opacity) { - var a = clampAlpha(opacity) - if (!c) return Qt.rgba(0, 0, 0, a) - return Qt.rgba(c.r, c.g, c.b, a) - } - function colorFromHex(value, fallback) { var s = String(value || "").replace(/^\s+|\s+$/g, "") var shortHex = s.match(/^#([0-9A-Fa-f]{3})$/) @@ -158,17 +146,17 @@ QtObject { return resolveStateColor(selectionColorToken, foreground, accent, urgent, foreground || Color.foreground) } - function normalFillFor(foreground, accent, urgent) { return alpha(normalStateColor(foreground, accent, urgent), normalFillAlpha) } - function hoverFillFor(foreground, accent, urgent) { return alpha(hoverStateColor(foreground, accent, urgent), hoverFillAlpha) } - function selectedFillFor(foreground, accent, urgent) { return alpha(selectedStateColor(foreground, accent, urgent), selectedFillAlpha) } - function pressedFillFor(foreground, accent, urgent) { return alpha(pressedStateColor(foreground, accent, urgent), pressedFillAlpha) } - function focusFillFor(foreground, accent, urgent) { return alpha(focusStateColor(foreground, accent, urgent), focusFillAlpha) } - function selectionFillFor(foreground, accent, urgent) { return alpha(selectionStateColor(foreground, accent, urgent), selectionFillAlpha) } + function normalFillFor(foreground, accent, urgent) { return Util.alpha(normalStateColor(foreground, accent, urgent), normalFillAlpha) } + function hoverFillFor(foreground, accent, urgent) { return Util.alpha(hoverStateColor(foreground, accent, urgent), hoverFillAlpha) } + function selectedFillFor(foreground, accent, urgent) { return Util.alpha(selectedStateColor(foreground, accent, urgent), selectedFillAlpha) } + function pressedFillFor(foreground, accent, urgent) { return Util.alpha(pressedStateColor(foreground, accent, urgent), pressedFillAlpha) } + function focusFillFor(foreground, accent, urgent) { return Util.alpha(focusStateColor(foreground, accent, urgent), focusFillAlpha) } + function selectionFillFor(foreground, accent, urgent) { return Util.alpha(selectionStateColor(foreground, accent, urgent), selectionFillAlpha) } - function normalBorderFor(foreground, accent, urgent) { return alpha(normalStateColor(foreground, accent, urgent), normalBorderAlpha) } - function hoverBorderFor(foreground, accent, urgent) { return alpha(hoverStateColor(foreground, accent, urgent), hoverBorderAlpha) } - function selectedBorderFor(foreground, accent, urgent) { return alpha(selectedStateColor(foreground, accent, urgent), selectedBorderAlpha) } - function focusBorderFor(foreground, accent, urgent) { return alpha(focusStateColor(foreground, accent, urgent), focusBorderAlpha) } + function normalBorderFor(foreground, accent, urgent) { return Util.alpha(normalStateColor(foreground, accent, urgent), normalBorderAlpha) } + function hoverBorderFor(foreground, accent, urgent) { return Util.alpha(hoverStateColor(foreground, accent, urgent), hoverBorderAlpha) } + function selectedBorderFor(foreground, accent, urgent) { return Util.alpha(selectedStateColor(foreground, accent, urgent), selectedBorderAlpha) } + function focusBorderFor(foreground, accent, urgent) { return Util.alpha(focusStateColor(foreground, accent, urgent), focusBorderAlpha) } // Convenience colors resolved against the foundational palette. readonly property color normalFill: normalFillFor(Color.foreground, Color.accent, Color.urgent) @@ -180,7 +168,7 @@ QtObject { readonly property color hoverBorderColor: hoverBorderFor(Color.foreground, Color.accent, Color.urgent) readonly property color selectedBorderColor: selectedBorderFor(Color.foreground, Color.accent, Color.urgent) readonly property color focusBorderColor: focusBorderFor(Color.foreground, Color.accent, Color.urgent) - readonly property color selectedAccentFill: alpha(Color.accent, selectedFillAlpha) + readonly property color selectedAccentFill: Util.alpha(Color.accent, selectedFillAlpha) readonly property color selectionFill: selectionFillFor(Color.foreground, Color.accent, Color.urgent) // ---------------------------------------------------------- spacing diff --git a/shell/Commons/Util.qml b/shell/Commons/Util.qml index 608c0cef..9ba8b42b 100644 --- a/shell/Commons/Util.qml +++ b/shell/Commons/Util.qml @@ -6,12 +6,23 @@ import QtQuick QtObject { id: root + function clamp(value, min, max) { + var n = Number(value) + if (!isFinite(n)) return min + return Math.max(min, Math.min(max, n)) + } + + function clampAlpha(value) { + return clamp(value, 0, 1) + } + // Compose a base color with an opacity. Accepts a color object or a hex // string; null/undefined yields transparent black at the requested alpha. function alpha(c, opacity) { - if (!c) return Qt.rgba(0, 0, 0, opacity) + var a = clampAlpha(opacity) + if (!c) return Qt.rgba(0, 0, 0, a) if (typeof c === "string") c = Qt.color(c) - return Qt.rgba(c.r, c.g, c.b, opacity) + return Qt.rgba(c.r, c.g, c.b, a) } // file:// URL with each path segment percent-encoded so spaces and diff --git a/shell/Ui/SearchableDropdown.qml b/shell/Ui/SearchableDropdown.qml index 3a1d5227..759bedab 100644 --- a/shell/Ui/SearchableDropdown.qml +++ b/shell/Ui/SearchableDropdown.qml @@ -244,7 +244,7 @@ Item { Rectangle { width: parent.width height: 1 - color: Style.alpha(root.foreground, 0.10) + color: Util.alpha(root.foreground, 0.10) } Item { diff --git a/shell/plugins/osd/Osd.qml b/shell/plugins/osd/Osd.qml index 2f66b8f5..6d431658 100644 --- a/shell/plugins/osd/Osd.qml +++ b/shell/plugins/osd/Osd.qml @@ -94,7 +94,7 @@ Item { anchors.horizontalCenter: parent.horizontalCenter anchors.bottom: parent.bottom anchors.bottomMargin: Style.space(67) - color: Color.alpha(Color.background, 0.97) + color: Util.alpha(Color.background, 0.97) border.color: Color.popups.border border.width: Math.max(1, Style.space(2)) radius: Style.cornerRadius @@ -119,7 +119,7 @@ Item { width: visible ? Style.space(142) : 0 height: Math.max(Style.space(6), Style.spacing.sm) anchors.verticalCenter: parent.verticalCenter - color: Color.alpha(Color.popups.text, 0.45) + color: Util.alpha(Color.popups.text, 0.45) Rectangle { height: parent.height width: parent.width * (root.hasProgress ? root.value / root.maxValue : 0) diff --git a/shell/plugins/polkit/PolkitAgent.qml b/shell/plugins/polkit/PolkitAgent.qml index 5d004312..94e4215c 100644 --- a/shell/plugins/polkit/PolkitAgent.qml +++ b/shell/plugins/polkit/PolkitAgent.qml @@ -277,7 +277,7 @@ Item { verticalAlignment: TextInput.AlignVCenter activeFocusOnPress: true clip: true - selectionColor: Color.alpha(root.accent, 0.45) + selectionColor: Util.alpha(root.accent, 0.45) selectedTextColor: root.foreground font.family: root.fontFamily font.pixelSize: Style.font.iconLarge