From 272bb5077068603e51663aab5f84b99d545e7cf2 Mon Sep 17 00:00:00 2001 From: loner <2788892716@qq.com> Date: Thu, 23 Oct 2025 00:35:37 +0800 Subject: [PATCH] feat: Implement automatic regex interpretation for custom button collapse condition --- Assets/Translations/en.json | 2 +- Modules/Bar/Widgets/CustomButton.qml | 30 +++++++++++++++++-- .../WidgetSettings/CustomButtonSettings.qml | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Assets/Translations/en.json b/Assets/Translations/en.json index 7ece8ee3..7e1778c7 100644 --- a/Assets/Translations/en.json +++ b/Assets/Translations/en.json @@ -1339,7 +1339,7 @@ "enter-width-pixels": "Enter width in pixels", "enter-command": "Enter command to execute (app or custom script)", "command-example": "echo \"Hello World\"", - "enter-text": "Enter text to collapse (e.g., 'nothing is playing')", + "enter-text-to-collapse": "Enter text or regex to collapse (e.g., 'nothing is playing' or '^nothing is playing')", "search-wallpapers": "Type to filter wallpapers...", "search-launcher": "Search entries... or use > for commands", "search": "Search...", diff --git a/Modules/Bar/Widgets/CustomButton.qml b/Modules/Bar/Widgets/CustomButton.qml index 0f6f70e5..bea0a3b8 100644 --- a/Modules/Bar/Widgets/CustomButton.qml +++ b/Modules/Bar/Widgets/CustomButton.qml @@ -38,7 +38,7 @@ Item { readonly property string textCommand: widgetSettings.textCommand !== undefined ? widgetSettings.textCommand : (widgetMetadata.textCommand || "") readonly property bool textStream: widgetSettings.textStream !== undefined ? widgetSettings.textStream : (widgetMetadata.textStream || false) readonly property int textIntervalMs: widgetSettings.textIntervalMs !== undefined ? widgetSettings.textIntervalMs : (widgetMetadata.textIntervalMs || 3000) - readonly property string textCollapse: widgetSettings.textCollapse || widgetMetadata.textCollapse || "" + readonly property string textCollapse: widgetSettings.textCollapse !== undefined ? widgetSettings.textCollapse : (widgetMetadata.textCollapse || "") readonly property bool hasExec: (leftClickExec || rightClickExec || middleClickExec) implicitWidth: pill.width @@ -101,7 +101,19 @@ Item { id: textStdoutSplit onRead: function(line) { var lineStr = String(line || "").trim() - if (textCollapse && textCollapse === lineStr) { + var shouldCollapse = false + + if (textCollapse && textCollapse.length > 0) { + try { + var regex = new RegExp(textCollapse) + shouldCollapse = regex.test(lineStr) + } catch (e) { + // If it's not a valid regex, treat it as a plain string + shouldCollapse = (textCollapse === lineStr) + } + } + + if (shouldCollapse) { _dynamicText = "" } else { _dynamicText = lineStr @@ -116,7 +128,19 @@ Item { if (out.indexOf("\n") !== -1) { out = out.split("\n")[0] } - if (textCollapse && textCollapse === out) { + var shouldCollapse = false + + if (textCollapse && textCollapse.length > 0) { + try { + var regex = new RegExp(textCollapse) + shouldCollapse = regex.test(out) + } catch (e) { + // If it's not a valid regex, treat it as a plain string + shouldCollapse = (textCollapse === out) + } + } + + if (shouldCollapse) { _dynamicText = "" } else { _dynamicText = out diff --git a/Modules/Settings/Bar/WidgetSettings/CustomButtonSettings.qml b/Modules/Settings/Bar/WidgetSettings/CustomButtonSettings.qml index ae0c242b..f87ba209 100644 --- a/Modules/Settings/Bar/WidgetSettings/CustomButtonSettings.qml +++ b/Modules/Settings/Bar/WidgetSettings/CustomButtonSettings.qml @@ -116,7 +116,7 @@ ColumnLayout { visible: valueTextStream label: I18n.tr("bar.widget-settings.custom-button.collapse-condition.label") description: I18n.tr("bar.widget-settings.custom-button.collapse-condition.description") - placeholderText: I18n.tr("placeholders.enter-text") + placeholderText: I18n.tr("placeholders.enter-text-to-collapse") text: widgetData?.textCollapse || widgetMetadata.textCollapse }