From 1d5eb91803ca3919f0970e1dc5d7c317d3664c8d Mon Sep 17 00:00:00 2001 From: loner <2788892716@qq.com> Date: Thu, 23 Oct 2025 00:42:50 +0800 Subject: [PATCH] feat: Revert regex interpretation to require // delimiters and update en.json placeholder --- Assets/Translations/en.json | 2 +- Modules/Bar/Widgets/CustomButton.qml | 34 ++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Assets/Translations/en.json b/Assets/Translations/en.json index 7e1778c7..f34f59b3 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-to-collapse": "Enter text or regex to collapse (e.g., 'nothing is playing' or '^nothing is playing')", + "enter-text-to-collapse": "e.g., 'nothing is playing'. Use /regex/ for patterns.", "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 bea0a3b8..dd1c6cc1 100644 --- a/Modules/Bar/Widgets/CustomButton.qml +++ b/Modules/Bar/Widgets/CustomButton.qml @@ -104,11 +104,18 @@ Item { 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 + if (textCollapse.startsWith("/") && textCollapse.endsWith("/") && textCollapse.length > 1) { + // Treat as regex + var pattern = textCollapse.substring(1, textCollapse.length - 1) + try { + var regex = new RegExp(pattern) + shouldCollapse = regex.test(lineStr) + } catch (e) { + Logger.w("CustomButton", `Invalid regex for textCollapse: ${textCollapse} - ${e.message}`) + shouldCollapse = (textCollapse === lineStr) // Fallback to exact match on invalid regex + } + } else { + // Treat as plain string shouldCollapse = (textCollapse === lineStr) } } @@ -131,11 +138,18 @@ Item { 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 + if (textCollapse.startsWith("/") && textCollapse.endsWith("/") && textCollapse.length > 1) { + // Treat as regex + var pattern = textCollapse.substring(1, textCollapse.length - 1) + try { + var regex = new RegExp(pattern) + shouldCollapse = regex.test(out) + } catch (e) { + Logger.w("CustomButton", `Invalid regex for textCollapse: ${textCollapse} - ${e.message}`) + shouldCollapse = (textCollapse === out) // Fallback to exact match on invalid regex + } + } else { + // Treat as plain string shouldCollapse = (textCollapse === out) } }