mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-06-08 04:09:15 -04:00
fix(settings): support localized settings search (#2521)
This commit is contained in:
@@ -20,6 +20,18 @@ Singleton {
|
|||||||
property bool indexLoaded: false
|
property bool indexLoaded: false
|
||||||
property var _translatedCache: []
|
property var _translatedCache: []
|
||||||
|
|
||||||
|
Connections {
|
||||||
|
target: I18n
|
||||||
|
|
||||||
|
function onTranslationsChanged() {
|
||||||
|
root._refreshTranslatedCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onTranslationsLoadedChanged() {
|
||||||
|
root._refreshTranslatedCache();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
readonly property var conditionMap: ({
|
readonly property var conditionMap: ({
|
||||||
"isNiri": () => CompositorService.isNiri,
|
"isNiri": () => CompositorService.isNiri,
|
||||||
"isHyprland": () => CompositorService.isHyprland,
|
"isHyprland": () => CompositorService.isHyprland,
|
||||||
@@ -143,6 +155,7 @@ Singleton {
|
|||||||
for (var i = 0; i < settingsIndex.length; i++) {
|
for (var i = 0; i < settingsIndex.length; i++) {
|
||||||
var item = settingsIndex[i];
|
var item = settingsIndex[i];
|
||||||
var t = translateItem(item);
|
var t = translateItem(item);
|
||||||
|
var sourceDescription = item.description || "";
|
||||||
cache.push({
|
cache.push({
|
||||||
section: t.section,
|
section: t.section,
|
||||||
label: t.label,
|
label: t.label,
|
||||||
@@ -152,13 +165,58 @@ Singleton {
|
|||||||
icon: t.icon,
|
icon: t.icon,
|
||||||
description: t.description,
|
description: t.description,
|
||||||
conditionKey: t.conditionKey,
|
conditionKey: t.conditionKey,
|
||||||
labelLower: t.label.toLowerCase(),
|
labelSearch: _lowerVariants([item.label, t.label]),
|
||||||
categoryLower: t.category.toLowerCase()
|
categorySearch: _lowerVariants([item.category, t.category]),
|
||||||
|
descriptionSearch: _lowerVariants([sourceDescription, t.description])
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_translatedCache = cache;
|
_translatedCache = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _lowerVariants(values) {
|
||||||
|
var out = [];
|
||||||
|
for (var i = 0; i < values.length; i++) {
|
||||||
|
var value = values[i];
|
||||||
|
if (!value)
|
||||||
|
continue;
|
||||||
|
var lower = String(value).toLowerCase();
|
||||||
|
if (out.indexOf(lower) === -1)
|
||||||
|
out.push(lower);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _bestFieldScore(fields, queryLower, exactScore, prefixScore, includesScore) {
|
||||||
|
var score = 0;
|
||||||
|
for (var i = 0; i < fields.length; i++) {
|
||||||
|
var field = fields[i];
|
||||||
|
if (field === queryLower) {
|
||||||
|
score = Math.max(score, exactScore);
|
||||||
|
} else if (field.startsWith(queryLower)) {
|
||||||
|
score = Math.max(score, prefixScore);
|
||||||
|
} else if (field.includes(queryLower)) {
|
||||||
|
score = Math.max(score, includesScore);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _fieldsContainWord(fields, word) {
|
||||||
|
for (var i = 0; i < fields.length; i++) {
|
||||||
|
if (fields[i].includes(word))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _refreshTranslatedCache() {
|
||||||
|
if (!indexLoaded)
|
||||||
|
return;
|
||||||
|
_rebuildTranslationCache();
|
||||||
|
if (query)
|
||||||
|
results = _searchEntries(query, 15);
|
||||||
|
}
|
||||||
|
|
||||||
function _searchEntries(text, maxResults) {
|
function _searchEntries(text, maxResults) {
|
||||||
if (!text)
|
if (!text)
|
||||||
return [];
|
return [];
|
||||||
@@ -174,19 +232,11 @@ Singleton {
|
|||||||
if (!checkCondition(entry))
|
if (!checkCondition(entry))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var labelLower = entry.labelLower;
|
|
||||||
var categoryLower = entry.categoryLower;
|
|
||||||
var score = 0;
|
var score = 0;
|
||||||
|
|
||||||
if (labelLower === queryLower) {
|
score = Math.max(score, _bestFieldScore(entry.labelSearch, queryLower, 10000, 5000, 1000));
|
||||||
score = 10000;
|
score = Math.max(score, _bestFieldScore(entry.categorySearch, queryLower, 500, 500, 500));
|
||||||
} else if (labelLower.startsWith(queryLower)) {
|
score = Math.max(score, _bestFieldScore(entry.descriptionSearch, queryLower, 250, 250, 250));
|
||||||
score = 5000;
|
|
||||||
} else if (labelLower.includes(queryLower)) {
|
|
||||||
score = 1000;
|
|
||||||
} else if (categoryLower.includes(queryLower)) {
|
|
||||||
score = 500;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (score === 0) {
|
if (score === 0) {
|
||||||
var keywords = entry.keywords;
|
var keywords = entry.keywords;
|
||||||
@@ -205,7 +255,11 @@ Singleton {
|
|||||||
var allMatch = true;
|
var allMatch = true;
|
||||||
for (var w = 0; w < queryWords.length; w++) {
|
for (var w = 0; w < queryWords.length; w++) {
|
||||||
var word = queryWords[w];
|
var word = queryWords[w];
|
||||||
if (labelLower.includes(word))
|
if (_fieldsContainWord(entry.labelSearch, word))
|
||||||
|
continue;
|
||||||
|
if (_fieldsContainWord(entry.descriptionSearch, word))
|
||||||
|
continue;
|
||||||
|
if (_fieldsContainWord(entry.categorySearch, word))
|
||||||
continue;
|
continue;
|
||||||
var inKeywords = false;
|
var inKeywords = false;
|
||||||
for (var k = 0; k < entry.keywords.length; k++) {
|
for (var k = 0; k < entry.keywords.length; k++) {
|
||||||
@@ -214,7 +268,7 @@ Singleton {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!inKeywords && !categoryLower.includes(word)) {
|
if (!inKeywords) {
|
||||||
allMatch = false;
|
allMatch = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user