simplificaiton + removal of fuzzy

This commit is contained in:
Jesper 2024-06-13 22:18:01 +02:00
parent f9325f2870
commit 7c0bcbb5b7
4 changed files with 50 additions and 117 deletions

View File

@ -8,8 +8,6 @@ import os
import subprocess import subprocess
import threading import threading
import urllib.request, urllib.parse, urllib.error import urllib.request, urllib.parse, urllib.error
from fuzzywuzzy import process
import unidecode
import gi import gi
gi.require_version("Gtk", "3.0") gi.require_version("Gtk", "3.0")
@ -276,11 +274,9 @@ class pluginclass(object):
self.settings.connect("changed::fav-cols", self.changeFavCols) self.settings.connect("changed::fav-cols", self.changeFavCols)
self.settings.connect("changed::remember-filter", self.changeRememberFilter) self.settings.connect("changed::remember-filter", self.changeRememberFilter)
self.settings.connect("changed::enable-internet-search", self.changeEnableInternetSearch) self.settings.connect("changed::enable-internet-search", self.changeEnableInternetSearch)
self.settings.connect("changed::enable-fuzzy-search", self.changeEnableFuzzySearch)
self.settings.connect("changed::category-hover-delay", self.GetGSettingsEntries) self.settings.connect("changed::category-hover-delay", self.GetGSettingsEntries)
self.settings.connect("changed::do-not-filter", self.GetGSettingsEntries) self.settings.connect("changed::do-not-filter", self.GetGSettingsEntries)
self.settings.connect("changed::enable-internet-search", self.GetGSettingsEntries) self.settings.connect("changed::enable-internet-search", self.GetGSettingsEntries)
self.settings.connect("changed::enable-fuzzy-search", self.GetGSettingsEntries)
self.settings.connect("changed::search-command", self.GetGSettingsEntries) self.settings.connect("changed::search-command", self.GetGSettingsEntries)
self.settings.connect("changed::default-tab", self.GetGSettingsEntries) self.settings.connect("changed::default-tab", self.GetGSettingsEntries)
self.settings.connect("changed::favorite-apps-list", self.favoriteAppsChanged) self.settings.connect("changed::favorite-apps-list", self.favoriteAppsChanged)
@ -306,7 +302,6 @@ class pluginclass(object):
self.categoryList = [] self.categoryList = []
self.applicationList = [] self.applicationList = []
self.sortedApplicationList = []
#dirty ugly hack, to get favorites drag origin position #dirty ugly hack, to get favorites drag origin position
self.drag_origin = None self.drag_origin = None
@ -446,9 +441,6 @@ class pluginclass(object):
def changeEnableInternetSearch(self, settings, key): def changeEnableInternetSearch(self, settings, key):
self.enableInternetSearch = settings.get_boolean(key) self.enableInternetSearch = settings.get_boolean(key)
def changeEnableFuzzySearch(self, settings, key):
self.enableFuzzySearch = settings.get_boolean(key)
def changeShowApplicationComments(self, settings, key): def changeShowApplicationComments(self, settings, key):
self.showapplicationcomments = settings.get_boolean(key) self.showapplicationcomments = settings.get_boolean(key)
for child in self.applicationsBox: for child in self.applicationsBox:
@ -524,7 +516,6 @@ class pluginclass(object):
self.useAPT = self.settings.get_boolean("use-apt") self.useAPT = self.settings.get_boolean("use-apt")
self.rememberFilter = self.settings.get_boolean("remember-filter") self.rememberFilter = self.settings.get_boolean("remember-filter")
self.enableInternetSearch = self.settings.get_boolean("enable-internet-search") self.enableInternetSearch = self.settings.get_boolean("enable-internet-search")
self.enableFuzzySearch = self.settings.get_boolean("enable-fuzzy-search")
self.lastActiveTab = self.settings.get_int("last-active-tab") self.lastActiveTab = self.settings.get_int("last-active-tab")
self.defaultTab = self.settings.get_int("default-tab") self.defaultTab = self.settings.get_int("default-tab")
@ -588,7 +579,6 @@ class pluginclass(object):
return False return False
def focusSearchEntry(self, clear = True): def focusSearchEntry(self, clear = True):
self.applicationsBox.get_children()[0].grab_focus()
# grab_focus() does select all text, # grab_focus() does select all text,
# restoring the original selection is somehow broken, so just select the end # restoring the original selection is somehow broken, so just select the end
# of the existing text, that's the most likely candidate anyhow # of the existing text, that's the most likely candidate anyhow
@ -755,86 +745,6 @@ class pluginclass(object):
except Exception as e: except Exception as e:
print(e) print(e)
def strip_case_and_accents(self, string):
if isinstance(string, str):
try:
value = unidecode.unidecode(string.lower())
except:
pass
return value
def fuzzy_application_search(self, search_text):
shownList = []
showns = False # Are any app shown?
keywords = self.strip_case_and_accents(search_text).split()
labels = [app["button"].appName for app in self.applicationList]
results = process.extract(search_text, labels, limit=len(labels))
first_button = True
for match in results:
if match[1] > 60: # Adjust the threshold as needed
for app in self.applicationList:
if app["button"].appName == match[0]:
self.applicationsBox.pack_start(app["button"], False, False, 0)
if first_button is True:
app["button"].grab_focus()
first_button = False
shownList.append(app["button"])
showns = True
# Non-fuzzy results for appGenericName, appComment and appExec
# Again I should really make it a function or something
for app in self.applicationList:
for item in shownList:
if app["button"].desktopFile == item.desktopFile:
continue
for keyword in keywords:
if keyword != "" and (self.strip_case_and_accents(app["button"].appGenericName).find(keyword) != -1 or self.strip_case_and_accents(app["button"].appComment).find(keyword) != -1 or self.strip_case_and_accents(app["button"].appExec).find(keyword) != -1):
self.applicationsBox.pack_start(app["button"], False, False, 0)
if first_button is True:
app["button"].grab_focus()
first_button = False
shownList.append(app["button"])
showns = True
return showns
def exact_application_search(self, search_text):
shownList = []
showns = False # Are any app shown?
first_button = True
keywords = self.strip_case_and_accents(search_text).split()
# I should probably make a function but whatever
# Add applications that match the appName
for app in self.applicationList:
for item in shownList:
if app["button"].desktopFile == item.desktopFile:
continue
for keyword in keywords:
if keyword != "" and (self.strip_case_and_accents(app["button"].appName).find(keyword) != -1):
self.applicationsBox.pack_start(app["button"], False, False, 0)
if first_button is True:
app["button"].grab_focus()
first_button = False
shownList.append(app["button"])
showns = True
# Add applications that match appGenericName, appComment or appExec
for app in self.applicationList:
for item in shownList:
if app["button"].desktopFile == item.desktopFile:
continue
for keyword in keywords:
if keyword != "" and (self.strip_case_and_accents(app["button"].appGenericName).find(keyword) != -1 or self.strip_case_and_accents(app["button"].appComment).find(keyword) != -1 or self.strip_case_and_accents(app["button"].appExec).find(keyword) != -1):
self.applicationsBox.pack_start(app["button"], False, False, 0)
if first_button is True:
app["button"].grab_focus()
first_button = False
shownList.append(app["button"])
showns = True
return showns
def Filter(self, widget, category = None): def Filter(self, widget, category = None):
self.filterTimer = None self.filterTimer = None
@ -851,25 +761,36 @@ class pluginclass(object):
self.changeTab(1, clear = False) self.changeTab(1, clear = False)
text = widget.get_text() text = widget.get_text()
showns = False # Are any app shown? showns = False # Are any app shown?
shownList = []
for item in self.applicationList: for i in self.applicationsBox.get_children():
self.applicationsBox.remove(item["button"]) shown = i.filterText(text)
if shown:
if text == "": # Reset application list dupe = False
for item in self.applicationList: for item in shownList:
self.applicationsBox.remove(item["button"]) if i.desktopFile == item.desktopFile:
for item in self.sortedApplicationList: dupe = True
self.applicationsBox.pack_start(item[1], False, False, 0) if dupe:
if self.enableFuzzySearch: i.hide()
showns = self.fuzzy_application_search(text)
else: else:
showns = self.exact_application_search(text) shownList.append(i)
self.applicationsBox.remove(i)
showns = True
if not showns: if not showns:
if len(text) >= 3: if len(text) >= 3:
self.add_search_suggestions(text) self.add_search_suggestions(text)
if self.useAPT: if self.useAPT:
GLib.timeout_add(300, self.add_apt_filter_results, text) GLib.timeout_add(300, self.add_apt_filter_results, text)
else:
# Sort applications by relevance, and alphabetical within that
shownList = sorted(shownList, key=lambda app: app.appName)
shownList = sorted(shownList, key=lambda app: app.relevance, reverse=True)
focused = False
for i in shownList:
self.applicationsBox.add(i)
if not focused:
# Grab focus of the first app shown
i.grab_focus()
focused = True
for i in self.categoriesBox.get_children(): for i in self.categoriesBox.get_children():
i.released() i.released()
i.set_relief(Gtk.ReliefStyle.NONE) i.set_relief(Gtk.ReliefStyle.NONE)
@ -896,6 +817,7 @@ class pluginclass(object):
i.released() i.released()
i.set_relief(Gtk.ReliefStyle.NONE) i.set_relief(Gtk.ReliefStyle.NONE)
widget.set_relief(Gtk.ReliefStyle.HALF) widget.set_relief(Gtk.ReliefStyle.HALF)
self.applicationsScrolledWindow.get_vadjustment().set_value(0) self.applicationsScrolledWindow.get_vadjustment().set_value(0)
def FilterAndClear(self, widget, category = None): def FilterAndClear(self, widget, category = None):
@ -1678,10 +1600,10 @@ class pluginclass(object):
self.applicationList[key]["button"].destroy() self.applicationList[key]["button"].destroy()
del self.applicationList[key] del self.applicationList[key]
if addedApplications: if addedApplications:
self.sortedApplicationList = [] sortedApplicationList = []
for item in self.applicationList: for item in self.applicationList:
self.applicationsBox.remove(item["button"]) self.applicationsBox.remove(item["button"])
self.sortedApplicationList.append((item["button"].appName, item["button"])) sortedApplicationList.append((item["button"].appName, item["button"]))
for item in addedApplications: for item in addedApplications:
item["button"] = MenuApplicationLauncher(item["entry"].get_desktop_file_path(), item["button"] = MenuApplicationLauncher(item["entry"].get_desktop_file_path(),
self.iconSize, item["category"], self.showapplicationcomments, self.iconSize, item["category"], self.showapplicationcomments,
@ -1696,14 +1618,14 @@ class pluginclass(object):
else: else:
item["button"].filterCategory(self.activeFilter[1]) item["button"].filterCategory(self.activeFilter[1])
item["button"].desktop_file_path = item["entry"].get_desktop_file_path() item["button"].desktop_file_path = item["entry"].get_desktop_file_path()
self.sortedApplicationList.append((item["button"].appName.upper(), item["button"])) sortedApplicationList.append((item["button"].appName.upper(), item["button"]))
self.applicationList.append(item) self.applicationList.append(item)
else: else:
item["button"].destroy() item["button"].destroy()
self.sortedApplicationList.sort() sortedApplicationList.sort()
launcherNames = [] # Keep track of launcher names so we don't add them twice in the list.. launcherNames = [] # Keep track of launcher names so we don't add them twice in the list..
for item in self.sortedApplicationList: for item in sortedApplicationList:
launcherName = item[0] launcherName = item[0]
button = item[1] button = item[1]
self.applicationsBox.add(button) self.applicationsBox.add(button)

View File

@ -234,6 +234,7 @@ class ApplicationLauncher(easyButton):
self.desktopFile = desktopFile self.desktopFile = desktopFile
self.startupMonitorId = 0 self.startupMonitorId = 0
self.relevance = 0
self.loadDesktopEntry(desktopItem) self.loadDesktopEntry(desktopItem)
@ -306,11 +307,28 @@ class ApplicationLauncher(easyButton):
def filterText(self, text): def filterText(self, text):
keywords = self.strip_case_and_accents(text).split() keywords = self.strip_case_and_accents(text).split()
self.relevance = 0
appName = self.strip_case_and_accents(self.appName) appName = self.strip_case_and_accents(self.appName)
appGenericName = self.strip_case_and_accents(self.appGenericName) appGenericName = self.strip_case_and_accents(self.appGenericName)
appComment = self.strip_case_and_accents(self.appComment) appComment = self.strip_case_and_accents(self.appComment)
appExec = self.strip_case_and_accents(self.appExec) appExec = self.strip_case_and_accents(self.appExec)
for keyword in keywords: for keyword in keywords:
if appName == keyword:
self.relevance += 32
elif appName.find(keyword) == 0:
self.relevance += 16
elif appName.find(keyword) != -1:
self.relevance += 8
if appExec.find(keyword) != -1:
self.relevance += 4
if appComment.find(keyword) != -1:
self.relevance += 2
if appGenericName.find(keyword) != -1:
self.relevance += 1
if keyword != "" and appName.find(keyword) == -1 and appGenericName.find(keyword) == -1 and appComment.find(keyword) == -1 and appExec.find(keyword) == -1: if keyword != "" and appName.find(keyword) == -1 and appGenericName.find(keyword) == -1 and appComment.find(keyword) == -1 and appExec.find(keyword) == -1:
self.hide() self.hide()
return False return False

View File

@ -134,7 +134,6 @@ class mintMenuPreferences():
section.add_row(GSettingsSwitch(_("Search for packages to install"), "com.linuxmint.mintmenu.plugins.applications", "use-apt")) section.add_row(GSettingsSwitch(_("Search for packages to install"), "com.linuxmint.mintmenu.plugins.applications", "use-apt"))
section.add_row(GSettingsSwitch(_("Remember the last category or search"), "com.linuxmint.mintmenu.plugins.applications", "remember-filter")) section.add_row(GSettingsSwitch(_("Remember the last category or search"), "com.linuxmint.mintmenu.plugins.applications", "remember-filter"))
section.add_row(GSettingsSwitch(_("Enable Internet search"), "com.linuxmint.mintmenu.plugins.applications", "enable-internet-search")) section.add_row(GSettingsSwitch(_("Enable Internet search"), "com.linuxmint.mintmenu.plugins.applications", "enable-internet-search"))
section.add_row(GSettingsSwitch(_("Enable Fuzzy search"), "com.linuxmint.mintmenu.plugins.applications", "enable-fuzzy-search"))
section.add_row(GSettingsEntry(_("Search command"), "com.linuxmint.mintmenu.plugins.applications", "search-command")) section.add_row(GSettingsEntry(_("Search command"), "com.linuxmint.mintmenu.plugins.applications", "search-command"))
page = SettingsPage() page = SettingsPage()

View File

@ -286,12 +286,6 @@
<description></description> <description></description>
</key> </key>
<key type="b" name="enable-fuzzy-search">
<default>false</default>
<summary></summary>
<description></description>
</key>
<key type="b" name="search-on-top"> <key type="b" name="search-on-top">
<default>false</default> <default>false</default>
<summary></summary> <summary></summary>