[Xfce4-commits] [panel-plugins/xfce4-whiskermenu-plugin] 01/01: Rewrite menu load to match GarconGtkMenu.

noreply at xfce.org noreply at xfce.org
Mon Feb 10 02:15:29 CET 2020


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

g   o   t   t   c   o   d   e       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository panel-plugins/xfce4-whiskermenu-plugin.

commit a191c7631126e59af8350bc8a0e7a8edb4bb36e2
Author: Graeme Gott <graeme at gottcode.org>
Date:   Sun Feb 9 16:12:05 2020 -0500

    Rewrite menu load to match GarconGtkMenu.
---
 panel-plugin/applications-page.cpp | 163 ++++++++++++++++++-------------------
 panel-plugin/applications-page.h   |   3 +-
 panel-plugin/category.cpp          |  43 ++--------
 panel-plugin/category.h            |  13 +--
 4 files changed, 94 insertions(+), 128 deletions(-)

diff --git a/panel-plugin/applications-page.cpp b/panel-plugin/applications-page.cpp
index bb110de..f47abfa 100644
--- a/panel-plugin/applications-page.cpp
+++ b/panel-plugin/applications-page.cpp
@@ -42,17 +42,7 @@ ApplicationsPage::ApplicationsPage(Window* window) :
 	m_garcon_settings_menu(nullptr),
 	m_status(LoadStatus::Invalid)
 {
-	// Set desktop environment for applications
-	const gchar* desktop = g_getenv("XDG_CURRENT_DESKTOP");
-	if (G_LIKELY(!desktop))
-	{
-		desktop = "XFCE";
-	}
-	else if (*desktop == '\0')
-	{
-		desktop = nullptr;
-	}
-	garcon_set_environment(desktop);
+	garcon_set_environment_xdg(GARCON_ENVIRONMENT_XFCE);
 }
 
 //-----------------------------------------------------------------------------
@@ -272,7 +262,7 @@ void ApplicationsPage::load_garcon_menu()
 	}
 
 	g_signal_connect_slot<GarconMenu*>(m_garcon_menu, "reload-required", &ApplicationsPage::invalidate, this);
-	load_menu(m_garcon_menu, nullptr);
+	load_menu(m_garcon_menu, nullptr, wm_settings->load_hierarchy);
 
 	// Create settings menu
 	gchar* path = xfce_resource_lookup(XFCE_RESOURCE_CONFIG, "menus/xfce-settings-manager.menu");
@@ -287,7 +277,9 @@ void ApplicationsPage::load_garcon_menu()
 	// Load settings menu
 	if (m_garcon_settings_menu && garcon_menu_load(m_garcon_settings_menu, nullptr, nullptr))
 	{
-		load_menu(m_garcon_settings_menu, nullptr);
+		Category* category = new Category(nullptr);
+		load_menu(m_garcon_settings_menu, category, false);
+		delete category;
 	}
 
 	// Sort items and categories
@@ -344,93 +336,96 @@ void ApplicationsPage::load_contents()
 
 //-----------------------------------------------------------------------------
 
-void ApplicationsPage::load_menu(GarconMenu* menu, Category* parent_category)
+bool ApplicationsPage::load_menu(GarconMenu* menu, Category* parent_category, bool load_hierarchy)
 {
-	GarconMenuDirectory* directory = garcon_menu_get_directory(menu);
-
-	// Skip hidden categories
-	if (directory && !garcon_menu_directory_get_visible(directory))
-	{
-		return;
-	}
-
-	// Track categories
-	bool first_level = directory && (garcon_menu_get_parent(menu) == m_garcon_menu);
-	Category* category = nullptr;
-	if (directory)
-	{
-		if (first_level)
-		{
-			category = new Category(directory);
-			m_categories.push_back(category);
-		}
-		else if (!wm_settings->load_hierarchy)
-		{
-			category = parent_category;
-		}
-		else if (parent_category)
-		{
-			category = parent_category->append_menu(directory);
-		}
-	}
+	bool has_children = false;
 
 	// Add menu elements
 	GList* elements = garcon_menu_get_elements(menu);
 	for (GList* li = elements; li; li = li->next)
 	{
+		// Add menu item
 		if (GARCON_IS_MENU_ITEM(li->data))
 		{
-			load_menu_item(GARCON_MENU_ITEM(li->data), category);
+			GarconMenuItem* menuitem = GARCON_MENU_ITEM(li->data);
+
+			// Listen for changes
+			g_signal_connect_slot<GarconMenuItem*>(menuitem, "changed", &ApplicationsPage::invalidate, this);
+
+			// Skip hidden items
+			if (!garcon_menu_element_get_visible(GARCON_MENU_ELEMENT(menuitem)))
+			{
+				continue;
+			}
+
+			// Create launcher
+			std::string desktop_id(garcon_menu_item_get_desktop_id(menuitem));
+			auto iter = m_items.find(desktop_id);
+			if (iter == m_items.end())
+			{
+				iter = m_items.emplace(std::move(desktop_id), new Launcher(menuitem)).first;
+			}
+
+			// Add launcher to current category
+			if (parent_category)
+			{
+				parent_category->append_item(iter->second);
+			}
+
+			has_children = true;
 		}
-		else if (GARCON_IS_MENU(li->data))
+		// Add separator
+		else if (GARCON_IS_MENU_SEPARATOR(li->data) && load_hierarchy && parent_category)
 		{
-			load_menu(GARCON_MENU(li->data), category);
+			parent_category->append_separator();
 		}
-		else if (GARCON_IS_MENU_SEPARATOR(li->data) && wm_settings->load_hierarchy && category)
+		// Add submenu
+		else if (GARCON_IS_MENU(li->data))
 		{
-			category->append_separator();
+			GarconMenu* submenu = GARCON_MENU(li->data);
+
+			// Skip hidden categories
+			GarconMenuDirectory* directory = garcon_menu_get_directory(submenu);
+			if (directory && !garcon_menu_directory_get_visible(directory))
+			{
+				continue;
+			}
+
+			// Create category
+			Category* category = nullptr;
+			if (!load_hierarchy && parent_category)
+			{
+				category = parent_category;
+			}
+			else
+			{
+				category = new Category(submenu);
+			}
+
+			// Populate category
+			if (load_menu(submenu, category, load_hierarchy))
+			{
+				if (!parent_category)
+				{
+					m_categories.push_back(category);
+				}
+				else if (category != parent_category)
+				{
+					parent_category->append_category(category);
+				}
+
+				has_children = true;
+			}
+			// Remove empty categories
+			else if (category != parent_category)
+			{
+				delete category;
+			}
 		}
 	}
 	g_list_free(elements);
 
-	// Free unused top-level categories
-	if (first_level && category->empty())
-	{
-		m_categories.erase(std::find(m_categories.begin(), m_categories.end(), category));
-		delete category;
-		category = nullptr;
-	}
-
-	// Listen for menu changes
-	g_signal_connect_slot<GarconMenu*,GarconMenuDirectory*,GarconMenuDirectory*>(menu, "directory-changed", &ApplicationsPage::invalidate, this);
-}
-
-//-----------------------------------------------------------------------------
-
-void ApplicationsPage::load_menu_item(GarconMenuItem* menu_item, Category* category)
-{
-	// Skip hidden items
-	if (!garcon_menu_element_get_visible(GARCON_MENU_ELEMENT(menu_item)))
-	{
-		return;
-	}
-
-	// Add to map
-	std::string desktop_id(garcon_menu_item_get_desktop_id(menu_item));
-	auto iter = m_items.find(desktop_id);
-	if (iter == m_items.end())
-	{
-		iter = m_items.insert(std::make_pair(std::move(desktop_id), new Launcher(menu_item))).first;
-	}
-
-	// Add menu item to current category
-	if (category)
-	{
-		category->append_item(iter->second);
-	}
-
-	// Listen for menu changes
-	g_signal_connect_slot<GarconMenuItem*>(menu_item, "changed", &ApplicationsPage::invalidate, this);
+	return has_children;
 }
 
 //-----------------------------------------------------------------------------
diff --git a/panel-plugin/applications-page.h b/panel-plugin/applications-page.h
index 724bd8b..8492930 100644
--- a/panel-plugin/applications-page.h
+++ b/panel-plugin/applications-page.h
@@ -51,8 +51,7 @@ private:
 	void clear();
 	void load_garcon_menu();
 	void load_contents();
-	void load_menu(GarconMenu* menu, Category* parent_category);
-	void load_menu_item(GarconMenuItem* menu_item, Category* category);
+	bool load_menu(GarconMenu* menu, Category* parent_category, bool load_hierarchy);
 
 	static void load_garcon_menu_slot(GTask* task, gpointer, gpointer task_data, GCancellable*)
 	{
diff --git a/panel-plugin/category.cpp b/panel-plugin/category.cpp
index 2992af5..4128394 100644
--- a/panel-plugin/category.cpp
+++ b/panel-plugin/category.cpp
@@ -29,7 +29,7 @@ using namespace WhiskerMenu;
 
 //-----------------------------------------------------------------------------
 
-Category::Category(GarconMenuDirectory* directory) :
+Category::Category(GarconMenu* menu) :
 	m_button(nullptr),
 	m_model(nullptr),
 	m_has_separators(false),
@@ -38,11 +38,12 @@ Category::Category(GarconMenuDirectory* directory) :
 	const gchar* icon = nullptr;
 	const gchar* text = nullptr;
 	const gchar* tooltip = nullptr;
-	if (directory)
+	if (menu)
 	{
-		icon = garcon_menu_directory_get_icon_name(directory);
-		text = garcon_menu_directory_get_name(directory);
-		tooltip = garcon_menu_directory_get_comment(directory);
+		GarconMenuElement* element = GARCON_MENU_ELEMENT(menu);
+		icon = garcon_menu_element_get_icon_name(element);
+		text = garcon_menu_element_get_name(element);
+		tooltip = garcon_menu_element_get_comment(element);
 	}
 	else
 	{
@@ -117,33 +118,6 @@ GtkTreeModel* Category::get_model()
 
 //-----------------------------------------------------------------------------
 
-bool Category::empty() const
-{
-	for (auto element : m_items)
-	{
-		Category* category = dynamic_cast<Category*>(element);
-		if ((!category && element) || !category->empty())
-		{
-			return false;
-		}
-	}
-
-	return true;
-}
-
-//-----------------------------------------------------------------------------
-
-Category* Category::append_menu(GarconMenuDirectory* directory)
-{
-	m_has_subcategories = true;
-	unset_model();
-	Category* category = new Category(directory);
-	m_items.push_back(category);
-	return category;
-}
-
-//-----------------------------------------------------------------------------
-
 void Category::append_separator()
 {
 	if (!m_items.empty() && m_items.back())
@@ -175,11 +149,6 @@ void Category::insert_items(GtkTreeStore* model, GtkTreeIter* parent)
 	{
 		if (Category* category = dynamic_cast<Category*>(element))
 		{
-			if (category->empty())
-			{
-				continue;
-			}
-
 			gchar* text = g_markup_escape_text(category->get_text(), -1);
 			const gchar* tooltip = category->get_tooltip();
 
diff --git a/panel-plugin/category.h b/panel-plugin/category.h
index 4f159f0..4d0c907 100644
--- a/panel-plugin/category.h
+++ b/panel-plugin/category.h
@@ -33,20 +33,25 @@ class CategoryButton;
 class Category : public Element
 {
 public:
-	explicit Category(GarconMenuDirectory* directory);
+	explicit Category(GarconMenu* menu);
 	~Category();
 
 	CategoryButton* get_button();
 
 	GtkTreeModel* get_model();
 
-	bool empty() const;
-
 	bool has_separators() const
 	{
 		return m_has_separators;
 	}
 
+	void append_category(Category* category)
+	{
+		unset_model();
+		m_has_subcategories = true;
+		m_items.push_back(category);
+	}
+
 	void append_item(Launcher* launcher)
 	{
 		unset_model();
@@ -60,8 +65,6 @@ public:
 		m_items.insert(m_items.end(), launchers.begin(), launchers.end());
 	}
 
-	Category* append_menu(GarconMenuDirectory* directory);
-
 	void append_separator();
 
 	void sort();

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


More information about the Xfce4-commits mailing list