mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2025-12-06 21:45:38 -05:00
192 lines
5.9 KiB
QML
192 lines
5.9 KiB
QML
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Io
|
|
import Quickshell.Widgets
|
|
import "../Common/fuzzysort.js" as Fuzzy
|
|
pragma Singleton
|
|
pragma ComponentBehavior: Bound
|
|
|
|
Singleton {
|
|
id: root
|
|
|
|
property list<DesktopEntry> applications: []
|
|
property var applicationsByName: ({})
|
|
property var applicationsByExec: ({})
|
|
property bool ready: false
|
|
|
|
// Pre-prepared fuzzy search data
|
|
property var preppedApps: []
|
|
|
|
|
|
Component.onCompleted: {
|
|
loadApplications()
|
|
}
|
|
|
|
Connections {
|
|
target: DesktopEntries
|
|
function onApplicationsChanged() {
|
|
console.log("AppSearchService: DesktopEntries applicationsChanged signal received")
|
|
console.log("AppSearchService: Current applications count before reload:", applications.length)
|
|
loadApplications()
|
|
}
|
|
}
|
|
|
|
|
|
function loadApplications() {
|
|
// Trigger rescan on next frame to avoid blocking
|
|
Qt.callLater(function() {
|
|
var allApps = Array.from(DesktopEntries.applications.values)
|
|
|
|
applications = allApps
|
|
.filter(app => !app.noDisplay)
|
|
.sort((a, b) => a.name.localeCompare(b.name))
|
|
|
|
// Build lookup maps
|
|
var byName = {}
|
|
var byExec = {}
|
|
|
|
for (var i = 0; i < applications.length; i++) {
|
|
var app = applications[i]
|
|
byName[app.name.toLowerCase()] = app
|
|
|
|
// Clean exec string for lookup
|
|
var execProp = app.execString || ""
|
|
var cleanExec = execProp ? execProp.replace(/%[fFuU]/g, "").trim() : ""
|
|
if (cleanExec) {
|
|
byExec[cleanExec] = app
|
|
}
|
|
}
|
|
|
|
applicationsByName = byName
|
|
applicationsByExec = byExec
|
|
|
|
// Prepare fuzzy search data
|
|
preppedApps = applications.map(app => ({
|
|
name: Fuzzy.prepare(app.name || ""),
|
|
comment: Fuzzy.prepare(app.comment || ""),
|
|
entry: app
|
|
}))
|
|
|
|
ready = true
|
|
|
|
console.log("AppSearchService: Loaded", applications.length, "applications")
|
|
console.log("AppSearchService: Prepared", preppedApps.length, "apps for fuzzy search")
|
|
console.log("AppSearchService: Ready status:", ready)
|
|
})
|
|
}
|
|
|
|
function searchApplications(query) {
|
|
if (!query || query.length === 0) {
|
|
return applications
|
|
}
|
|
|
|
if (!ready || preppedApps.length === 0) {
|
|
return []
|
|
}
|
|
|
|
// Use fuzzy search with both name and comment fields
|
|
var results = Fuzzy.go(query, preppedApps, {
|
|
all: false,
|
|
keys: ["name", "comment"],
|
|
scoreFn: r => {
|
|
// Prioritize name matches over comment matches
|
|
var nameScore = r[0] ? r[0].score : 0
|
|
var commentScore = r[1] ? r[1].score : 0
|
|
return nameScore > 0 ? nameScore * 0.9 + commentScore * 0.1 : commentScore * 0.5
|
|
},
|
|
limit: 50
|
|
})
|
|
|
|
// Extract the desktop entries from results
|
|
return results.map(r => r.obj.entry)
|
|
}
|
|
|
|
function getAppByName(name) {
|
|
return applicationsByName[name.toLowerCase()] || null
|
|
}
|
|
|
|
function getAppByExec(exec) {
|
|
var cleanExec = exec.replace(/%[fFuU]/g, "").trim()
|
|
return applicationsByExec[cleanExec] || null
|
|
}
|
|
|
|
function getCategoriesForApp(app) {
|
|
if (!app || !app.categories) return []
|
|
|
|
var categoryMap = {
|
|
"AudioVideo": "Media",
|
|
"Audio": "Media",
|
|
"Video": "Media",
|
|
"Development": "Development",
|
|
"TextEditor": "Development",
|
|
"IDE": "Development",
|
|
"Education": "Education",
|
|
"Game": "Games",
|
|
"Graphics": "Graphics",
|
|
"Photography": "Graphics",
|
|
"Network": "Internet",
|
|
"WebBrowser": "Internet",
|
|
"Email": "Internet",
|
|
"Office": "Office",
|
|
"WordProcessor": "Office",
|
|
"Spreadsheet": "Office",
|
|
"Presentation": "Office",
|
|
"Science": "Science",
|
|
"Settings": "Settings",
|
|
"System": "System",
|
|
"Utility": "Utilities",
|
|
"Accessories": "Utilities",
|
|
"FileManager": "Utilities",
|
|
"TerminalEmulator": "Utilities"
|
|
}
|
|
|
|
var mappedCategories = new Set()
|
|
|
|
for (var i = 0; i < app.categories.length; i++) {
|
|
var cat = app.categories[i]
|
|
if (categoryMap[cat]) {
|
|
mappedCategories.add(categoryMap[cat])
|
|
}
|
|
}
|
|
|
|
return Array.from(mappedCategories)
|
|
}
|
|
|
|
function getAllCategories() {
|
|
var categories = new Set(["All"])
|
|
|
|
for (var i = 0; i < applications.length; i++) {
|
|
var appCategories = getCategoriesForApp(applications[i])
|
|
appCategories.forEach(cat => categories.add(cat))
|
|
}
|
|
|
|
return Array.from(categories).sort()
|
|
}
|
|
|
|
function getAppsInCategory(category) {
|
|
if (category === "All") {
|
|
return applications
|
|
}
|
|
|
|
return applications.filter(app => {
|
|
var appCategories = getCategoriesForApp(app)
|
|
return appCategories.includes(category)
|
|
})
|
|
}
|
|
|
|
function launchApp(app) {
|
|
if (!app) {
|
|
console.warn("AppSearchService: Cannot launch app, app is null")
|
|
return false
|
|
}
|
|
|
|
// DesktopEntry objects have an execute() method
|
|
if (typeof app.execute === "function") {
|
|
app.execute()
|
|
return true
|
|
}
|
|
|
|
console.warn("AppSearchService: Cannot launch app, no execute method")
|
|
return false
|
|
}
|
|
} |