[Xfce4-commits] [panel-plugins/xfce4-whiskermenu-plugin] 104/473: Move search results out of launcher.

noreply at xfce.org noreply at xfce.org
Mon Feb 16 23:54:34 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 1e82cccab4a2c2a179fa0c9a6eab09516da0f045
Author: Graeme Gott <graeme at gottcode.org>
Date:   Sun Jul 14 08:20:01 2013 -0400

    Move search results out of launcher.
---
 src/launcher.cpp    |   27 +++++---------------
 src/launcher.hpp    |   10 +-------
 src/query.cpp       |    6 ++---
 src/query.hpp       |    2 +-
 src/search_page.cpp |   68 ++++++++++++++++++++++++++++++++++++++++++---------
 src/search_page.hpp |    3 +++
 6 files changed, 71 insertions(+), 45 deletions(-)

diff --git a/src/launcher.cpp b/src/launcher.cpp
index d7b08ca..a6f7a91 100644
--- a/src/launcher.cpp
+++ b/src/launcher.cpp
@@ -267,38 +267,23 @@ void Launcher::run(GdkScreen* screen) const
 
 //-----------------------------------------------------------------------------
 
-void Launcher::search(const Query& query)
+int Launcher::search(const Query& query) const
 {
-	if (query.empty())
-	{
-		return;
-	}
-
-	// Check if search has been done or if a shorter version has failed before
-	for (std::map<std::string, unsigned int>::const_iterator i = m_searches.begin(), end = m_searches.end(); i != end; ++i)
-	{
-		if ( ((i->second == UINT_MAX) && (query.query().find(i->first) == 0))
-				|| (i->first == query.query()) )
-		{
-			return;
-		}
-	}
-
-	unsigned int match = query.match(m_search_name);
-	if (match == UINT_MAX)
+	int match = query.match(m_search_name);
+	if (match == INT_MAX)
 	{
 		match = query.match(m_search_command);
 	}
-	if ((match == UINT_MAX) && f_show_description)
+	if ((match == INT_MAX) && f_show_description)
 	{
 		match = query.match(m_search_comment);
-		if (match != UINT_MAX)
+		if (match != INT_MAX)
 		{
 			// Sort matches in comments after matches in names
 			match += 10;
 		}
 	}
-	m_searches.insert(std::make_pair(query.query(), match));
+	return match;
 }
 
 //-----------------------------------------------------------------------------
diff --git a/src/launcher.hpp b/src/launcher.hpp
index 98fa06c..c49e391 100644
--- a/src/launcher.hpp
+++ b/src/launcher.hpp
@@ -19,7 +19,6 @@
 
 #include "query.hpp"
 
-#include <map>
 #include <string>
 
 extern "C"
@@ -62,15 +61,9 @@ public:
 		return garcon_menu_item_get_file(m_item);
 	}
 
-	unsigned int get_search_results(const Query& query) const
-	{
-		std::map<std::string, unsigned int>::const_iterator i = m_searches.find(query.query());
-		return (i != m_searches.end()) ? i->second : UINT_MAX;
-	}
-
 	void run(GdkScreen* screen) const;
 
-	void search(const Query& query);
+	int search(const Query& query) const;
 
 	static bool get_show_name();
 	static bool get_show_description();
@@ -89,7 +82,6 @@ private:
 	std::string m_search_name;
 	std::string m_search_comment;
 	std::string m_search_command;
-	std::map<std::string, unsigned int> m_searches;
 };
 
 }
diff --git a/src/query.cpp b/src/query.cpp
index b92888a..cebee10 100644
--- a/src/query.cpp
+++ b/src/query.cpp
@@ -57,12 +57,12 @@ Query::~Query()
 
 //-----------------------------------------------------------------------------
 
-unsigned int Query::match(const std::string& haystack) const
+int Query::match(const std::string& haystack) const
 {
 	// Make sure haystack is longer than query
 	if (m_query.empty() || (m_query.length() > haystack.length()))
 	{
-		return UINT_MAX;
+		return INT_MAX;
 	}
 
 	// Check if haystack begins with or is query
@@ -142,7 +142,7 @@ unsigned int Query::match(const std::string& haystack) const
 			start_word = false;
 		}
 	}
-	unsigned int result = UINT_MAX;
+	int result = INT_MAX;
 	if (*query_string == 0)
 	{
 		result = characters_start_words ? 5 : 7;
diff --git a/src/query.hpp b/src/query.hpp
index d70eb5b..fbd61a5 100644
--- a/src/query.hpp
+++ b/src/query.hpp
@@ -35,7 +35,7 @@ public:
 		return m_query.empty();
 	}
 
-	unsigned int match(const std::string& haystack) const;
+	int match(const std::string& haystack) const;
 
 	std::string query() const
 	{
diff --git a/src/search_page.cpp b/src/search_page.cpp
index 296660b..078a21d 100644
--- a/src/search_page.cpp
+++ b/src/search_page.cpp
@@ -32,7 +32,8 @@ using namespace WhiskerMenu;
 
 SearchPage::SearchPage(Menu* menu) :
 	FilterPage(menu),
-	m_sort_model(NULL)
+	m_sort_model(NULL),
+	m_current_results(NULL)
 {
 	get_view()->set_selection_mode(GTK_SELECTION_BROWSE);
 
@@ -59,19 +60,62 @@ void SearchPage::set_filter(const gchar* filter)
 	}
 	m_query.set(query);
 
-	// Remove previous search results
-	g_object_freeze_notify(G_OBJECT(get_view()->get_widget()));
-	get_view()->unset_model();
-	gtk_tree_model_sort_reset_default_sort_func(m_sort_model);
+	// Find longest previous search that starts query
+	const std::map<Launcher*, int>* previous = NULL;
+	m_current_results = NULL;
+	for (std::map<std::string, std::map<Launcher*, int> >::const_reverse_iterator i = m_results.rbegin(), end = m_results.rend(); i != end; ++i)
+	{
+		if ( (i->first.length() < query.length())
+			&& (query.compare(0, i->first.length(), i->first) == 0) )
+		{
+			previous = &i->second;
+			break;
+		}
+		else if (i->first == query)
+		{
+			m_current_results = &i->second;
+			break;
+		}
+	}
 
 	// Create search results
-	for (std::vector<Launcher*>::iterator i = m_launchers.begin(), end = m_launchers.end(); i != end; ++i)
+	if (!m_current_results && !m_query.empty())
 	{
-		(*i)->search(m_query);
+		std::map<Launcher*, int> results;
+		if (previous)
+		{
+			// Only check launchers that had previous search results
+			for (std::map<Launcher*, int>::const_iterator i = previous->begin(), end = previous->end(); i != end; ++i)
+			{
+				int result = i->first->search(m_query);
+				if (result != INT_MAX)
+				{
+					results.insert(std::make_pair(i->first, result));
+				}
+			}
+		}
+		else
+		{
+			// Check all launchers
+			for (std::vector<Launcher*>::const_iterator i = m_launchers.begin(), end = m_launchers.end(); i != end; ++i)
+			{
+				int result = (*i)->search(m_query);
+				if (result != INT_MAX)
+				{
+					results.insert(std::make_pair(*i, result));
+				}
+			}
+		}
+		m_current_results = &m_results.insert(std::make_pair(query, results)).first->second;
 	}
-	refilter();
 
 	// Show search results
+	g_object_freeze_notify(G_OBJECT(get_view()->get_widget()));
+	get_view()->unset_model();
+	gtk_tree_model_sort_reset_default_sort_func(m_sort_model);
+
+	refilter();
+
 	gtk_tree_sortable_set_default_sort_func(GTK_TREE_SORTABLE(m_sort_model), (GtkTreeIterCompareFunc)&SearchPage::on_sort, this, NULL);
 	get_view()->set_model(GTK_TREE_MODEL(m_sort_model));
 	g_object_thaw_notify(G_OBJECT(get_view()->get_widget()));
@@ -128,7 +172,7 @@ void SearchPage::unset_menu_items()
 
 bool SearchPage::on_filter(GtkTreeModel* model, GtkTreeIter* iter)
 {
-	if (m_query.empty())
+	if (!m_current_results)
 	{
 		return false;
 	}
@@ -136,7 +180,7 @@ bool SearchPage::on_filter(GtkTreeModel* model, GtkTreeIter* iter)
 	// Check if launcher search string contains text
 	Launcher* launcher = NULL;
 	gtk_tree_model_get(model, iter, LauncherModel::COLUMN_LAUNCHER, &launcher, -1);
-	return launcher && (launcher->get_search_results(m_query) != UINT_MAX);
+	return launcher && (m_current_results->find(launcher) != m_current_results->end());
 }
 
 //-----------------------------------------------------------------------------
@@ -146,12 +190,14 @@ gint SearchPage::on_sort(GtkTreeModel* model, GtkTreeIter* a, GtkTreeIter* b, Se
 	Launcher* launcher_a = NULL;
 	gtk_tree_model_get(model, a, LauncherModel::COLUMN_LAUNCHER, &launcher_a, -1);
 	g_assert(launcher_a != NULL);
+	g_assert(page->m_current_results->find(launcher_a) != page->m_current_results->end());
 
 	Launcher* launcher_b = NULL;
 	gtk_tree_model_get(model, b, LauncherModel::COLUMN_LAUNCHER, &launcher_b, -1);
 	g_assert(launcher_b != NULL);
+	g_assert(page->m_current_results->find(launcher_b) != page->m_current_results->end());
 
-	return launcher_a->get_search_results(page->m_query) - launcher_b->get_search_results(page->m_query);
+	return page->m_current_results->find(launcher_a)->second - page->m_current_results->find(launcher_b)->second;
 }
 
 //-----------------------------------------------------------------------------
diff --git a/src/search_page.hpp b/src/search_page.hpp
index cd805b9..7fbb9f7 100644
--- a/src/search_page.hpp
+++ b/src/search_page.hpp
@@ -20,6 +20,7 @@
 #include "filter_page.hpp"
 #include "query.hpp"
 
+#include <map>
 #include <string>
 #include <vector>
 
@@ -49,6 +50,8 @@ private:
 	Query m_query;
 	GtkTreeModelSort* m_sort_model;
 	std::vector<Launcher*> m_launchers;
+	std::map<std::string, std::map<Launcher*, int> > m_results;
+	const std::map<Launcher*, int>* m_current_results;
 
 
 private:

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


More information about the Xfce4-commits mailing list