[Xfce4-commits] [panel-plugins/xfce4-whiskermenu-plugin] 318/473: Fix sorting of search action matches. Closes #52.

noreply at xfce.org noreply at xfce.org
Mon Feb 16 23:58:08 CET 2015


This is an automated email from the git hooks/post-receive script.

gottcode pushed a commit to branch master
in repository panel-plugins/xfce4-whiskermenu-plugin.

commit 291814e370ff6eb1331eb59e6397244a8246a9bd
Author: Graeme Gott <graeme at gottcode.org>
Date:   Thu Dec 26 16:06:31 2013 -0500

    Fix sorting of search action matches. Closes #52.
---
 panel-plugin/search-action.cpp |   18 +++++++++---------
 panel-plugin/search-action.h   |    4 ++--
 panel-plugin/search-page.cpp   |   36 +++++++++++++++++++++++-------------
 3 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/panel-plugin/search-action.cpp b/panel-plugin/search-action.cpp
index 4ba5510..5f520b6 100644
--- a/panel-plugin/search-action.cpp
+++ b/panel-plugin/search-action.cpp
@@ -71,7 +71,7 @@ int SearchAction::search(const Query& query)
 	m_expanded_command.clear();
 
 	const gchar* haystack = query.raw_query().c_str();
-	bool found = !m_is_regex ? match_prefix(haystack) : match_regex(haystack);
+	int found = !m_is_regex ? match_prefix(haystack) : match_regex(haystack);
 
 	if (found && (m_show_description != wm_settings->launcher_show_description))
 	{
@@ -79,16 +79,16 @@ int SearchAction::search(const Query& query)
 		update_text();
 	}
 
-	return found ? 0 : G_MAXINT;
+	return found;
 }
 
 //-----------------------------------------------------------------------------
 
-bool SearchAction::match_prefix(const gchar* haystack)
+int SearchAction::match_prefix(const gchar* haystack)
 {
 	if (!g_str_has_prefix(haystack, m_pattern.c_str()))
 	{
-		return false;
+		return G_MAXINT;
 	}
 
 	gchar* trimmed = g_strdup(haystack + m_pattern.length());
@@ -140,21 +140,21 @@ bool SearchAction::match_prefix(const gchar* haystack)
 	g_free(trimmed);
 	g_free(uri);
 
-	return true;
+	return m_pattern.length();
 }
 
 //-----------------------------------------------------------------------------
 
-bool SearchAction::match_regex(const gchar* haystack)
+int SearchAction::match_regex(const gchar* haystack)
 {
-	bool found = false;
+	int found = G_MAXINT;
 
 	if (!m_regex)
 	{
 		m_regex = g_regex_new(m_pattern.c_str(), G_REGEX_OPTIMIZE, GRegexMatchFlags(0), NULL);
 		if (!m_regex)
 		{
-			return false;
+			return found;
 		}
 	}
 	GMatchInfo* match = NULL;
@@ -165,7 +165,7 @@ bool SearchAction::match_regex(const gchar* haystack)
 		{
 			m_expanded_command = expanded;
 			g_free(expanded);
-			found = true;
+			found = m_pattern.length();
 		}
 	}
 	if (match != NULL)
diff --git a/panel-plugin/search-action.h b/panel-plugin/search-action.h
index 0229b35..3d64a4c 100644
--- a/panel-plugin/search-action.h
+++ b/panel-plugin/search-action.h
@@ -70,8 +70,8 @@ public:
 	void set_is_regex(bool is_regex);
 
 private:
-	bool match_prefix(const gchar* haystack);
-	bool match_regex(const gchar* haystack);
+	int match_prefix(const gchar* haystack);
+	int match_regex(const gchar* haystack);
 	void update_text();
 
 private:
diff --git a/panel-plugin/search-page.cpp b/panel-plugin/search-page.cpp
index df284f4..63b2736 100644
--- a/panel-plugin/search-page.cpp
+++ b/panel-plugin/search-page.cpp
@@ -84,6 +84,20 @@ void SearchPage::set_filter(const gchar* filter)
 	m_query.set(query);
 
 	// Create search results
+	std::vector<Match> search_action_matches;
+	search_action_matches.reserve(wm_settings->search_actions.size());
+	for (std::vector<SearchAction*>::size_type i = 0, end = wm_settings->search_actions.size(); i < end; ++i)
+	{
+		Match match(wm_settings->search_actions[i]);
+		match.update(m_query);
+		if (!Match::invalid(match))
+		{
+			search_action_matches.push_back(match);
+		}
+	}
+	std::stable_sort(search_action_matches.begin(), search_action_matches.end());
+	std::reverse(search_action_matches.begin(), search_action_matches.end());
+
 	for (std::vector<Match>::size_type i = 0, end = m_matches.size(); i < end; ++i)
 	{
 		m_matches[i].update(m_query);
@@ -97,21 +111,17 @@ void SearchPage::set_filter(const gchar* filter)
 			G_TYPE_STRING,
 			G_TYPE_STRING,
 			G_TYPE_POINTER);
-	SearchAction* action;
-	for (std::vector<SearchAction*>::size_type i = 0, end = wm_settings->search_actions.size(); i < end; ++i)
+	Element* element;
+	for (std::vector<Match>::size_type i = 0, end = search_action_matches.size(); i < end; ++i)
 	{
-		action = wm_settings->search_actions[i];
-		if (action->search(m_query) != G_MAXINT)
-		{
-			gtk_list_store_insert_with_values(
-					store, NULL, G_MAXINT,
-					LauncherView::COLUMN_ICON, action->get_icon(),
-					LauncherView::COLUMN_TEXT, action->get_text(),
-					LauncherView::COLUMN_LAUNCHER, action,
-					-1);
-		}
+		element = search_action_matches[i].element();
+		gtk_list_store_insert_with_values(
+				store, NULL, G_MAXINT,
+				LauncherView::COLUMN_ICON, element->get_icon(),
+				LauncherView::COLUMN_TEXT, element->get_text(),
+				LauncherView::COLUMN_LAUNCHER, element,
+				-1);
 	}
-	Element* element;
 	for (std::vector<Match>::size_type i = 0, end = m_matches.size(); i < end; ++i)
 	{
 		element = m_matches[i].element();

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list