From 6ad9fe5e074e523383cb7b62bab0fa40b19024e3 Mon Sep 17 00:00:00 2001 From: zach Date: Sun, 5 Jul 2026 18:01:52 +0200 Subject: [PATCH] settings highlight optimized --- Modules/Settings/NavPane/NavLocations.qml | 4 +-- Modules/Settings/SettingsSearcher.qml | 31 ++++++++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Modules/Settings/NavPane/NavLocations.qml b/Modules/Settings/NavPane/NavLocations.qml index ec0e1fb..f07eb90 100644 --- a/Modules/Settings/NavPane/NavLocations.qml +++ b/Modules/Settings/NavPane/NavLocations.qml @@ -249,7 +249,7 @@ VerticalFadeFlickable { elide: Text.ElideRight font.pointSize: Appearance.font.size.medium text: SettingsSearcher.highlight(result.modelData.title, root.search, DynamicColors.palette.m3primary) - textFormat: Text.StyledText + textFormat: text.includes(" 0 } } diff --git a/Modules/Settings/SettingsSearcher.qml b/Modules/Settings/SettingsSearcher.qml index eb61042..9c41253 100644 --- a/Modules/Settings/SettingsSearcher.qml +++ b/Modules/Settings/SettingsSearcher.qml @@ -10,16 +10,39 @@ Singleton { id: root property var fzfFinder: null + readonly property var highlightCache: ({ + "search": "", + "pattern": null + }) property var inverted: ({}) property var ranking: ({}) function highlight(text: string, search: string, colour: color): string { const escaped = text.replace(/&/g, "&").replace(//g, ">"); - const tokens = tokenize(search); - if (tokens.length === 0) + if (search.length === 0) return escaped; - const escapedTokens = tokens.map(t => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); - const pattern = new RegExp("\\b(" + escapedTokens.join("|") + ")", "gi"); + + const cache = root.highlightCache; + if (search !== cache.search) { + const tokens = tokenize(search); + cache.search = search; + if (tokens.length === 0) + cache.pattern = null; + else { + const escapedTokens = tokens.map(t => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); + cache.pattern = new RegExp("\\b(" + escapedTokens.join("|") + ")", "gi"); + } + } + + const pattern = cache.pattern; + if (!pattern) + return escaped; + + pattern.lastIndex = 0; + if (!pattern.test(escaped)) + return escaped; + + pattern.lastIndex = 0; return escaped.replace(pattern, `$1`); }