[Bf-blender-cvs] [dbe3981b0a8] master: Cleanup: Better way to pass activate callbacks to Tree-View items

Julian Eisel noreply at git.blender.org
Tue Oct 5 16:11:08 CEST 2021


Commit: dbe3981b0a805c1a40f42f57dc7ccc3d28270fda
Author: Julian Eisel
Date:   Tue Oct 5 14:25:40 2021 +0200
Branches: master
https://developer.blender.org/rBdbe3981b0a805c1a40f42f57dc7ccc3d28270fda

Cleanup: Better way to pass activate callbacks to Tree-View items

The `ui::BasicTreeViewItem` took a function-like object to execute on
item activation via the constructor. This was mainly intended to be used
with lambdas. However, it's confusing to just have this lambda there,
with no indication of what it's for (activation).
Instead, assign the function-like object via an explicit `on_activate()`
function.

===================================================================

M	source/blender/editors/include/UI_tree_view.hh
M	source/blender/editors/interface/tree_view.cc
M	source/blender/editors/space_file/asset_catalog_tree_view.cc

===================================================================

diff --git a/source/blender/editors/include/UI_tree_view.hh b/source/blender/editors/include/UI_tree_view.hh
index a82aae021f2..4028bee9e15 100644
--- a/source/blender/editors/include/UI_tree_view.hh
+++ b/source/blender/editors/include/UI_tree_view.hh
@@ -239,10 +239,11 @@ class BasicTreeViewItem : public AbstractTreeViewItem {
   using ActivateFn = std::function<void(BasicTreeViewItem &new_active)>;
   BIFIconID icon;
 
-  BasicTreeViewItem(StringRef label, BIFIconID icon = ICON_NONE, ActivateFn activate_fn = nullptr);
+  BasicTreeViewItem(StringRef label, BIFIconID icon = ICON_NONE);
 
   void build_row(uiLayout &row) override;
   void on_activate() override;
+  void on_activate(ActivateFn fn);
 
  protected:
   /** Created in the #build() function. */
diff --git a/source/blender/editors/interface/tree_view.cc b/source/blender/editors/interface/tree_view.cc
index 202e3dba2ab..63b12d4fc89 100644
--- a/source/blender/editors/interface/tree_view.cc
+++ b/source/blender/editors/interface/tree_view.cc
@@ -278,8 +278,7 @@ uiLayout *TreeViewLayoutBuilder::current_layout() const
 
 /* ---------------------------------------------------------------------- */
 
-BasicTreeViewItem::BasicTreeViewItem(StringRef label, BIFIconID icon_, ActivateFn activate_fn)
-    : icon(icon_), activate_fn_(activate_fn)
+BasicTreeViewItem::BasicTreeViewItem(StringRef label, BIFIconID icon_) : icon(icon_)
 {
   label_ = label;
 }
@@ -330,6 +329,11 @@ void BasicTreeViewItem::on_activate()
   }
 }
 
+void BasicTreeViewItem::on_activate(ActivateFn fn)
+{
+  activate_fn_ = fn;
+}
+
 BIFIconID BasicTreeViewItem::get_draw_icon() const
 {
   if (icon) {
diff --git a/source/blender/editors/space_file/asset_catalog_tree_view.cc b/source/blender/editors/space_file/asset_catalog_tree_view.cc
index 9d6af5136d9..ff8775155c2 100644
--- a/source/blender/editors/space_file/asset_catalog_tree_view.cc
+++ b/source/blender/editors/space_file/asset_catalog_tree_view.cc
@@ -165,11 +165,12 @@ void AssetCatalogTreeView::add_all_item()
 {
   FileAssetSelectParams *params = params_;
 
-  ui::AbstractTreeViewItem &item = add_tree_item<AssetCatalogTreeViewAllItem>(
-      IFACE_("All"), ICON_HOME, [params](ui::BasicTreeViewItem & /*item*/) {
-        params->asset_catalog_visibility = FILE_SHOW_ASSETS_ALL_CATALOGS;
-        WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, nullptr);
-      });
+  AssetCatalogTreeViewAllItem &item = add_tree_item<AssetCatalogTreeViewAllItem>(IFACE_("All"),
+                                                                                 ICON_HOME);
+  item.on_activate([params](ui::BasicTreeViewItem & /*item*/) {
+    params->asset_catalog_visibility = FILE_SHOW_ASSETS_ALL_CATALOGS;
+    WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, nullptr);
+  });
   if (params->asset_catalog_visibility == FILE_SHOW_ASSETS_ALL_CATALOGS) {
     item.activate();
   }
@@ -180,10 +181,13 @@ void AssetCatalogTreeView::add_unassigned_item()
   FileAssetSelectParams *params = params_;
 
   AssetCatalogTreeViewUnassignedItem &item = add_tree_item<AssetCatalogTreeViewUnassignedItem>(
-      IFACE_("Unassigned"), ICON_FILE_HIDDEN, [params](ui::BasicTreeViewItem & /*item*/) {
-        params->asset_catalog_visibility = FILE_SHOW_ASSETS_WITHOUT_CATALOG;
-        WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, nullptr);
-      });
+      IFACE_("Unassigned"), ICON_FILE_HIDDEN);
+
+  item.on_activate([params](ui::BasicTreeViewItem & /*item*/) {
+    params->asset_catalog_visibility = FILE_SHOW_ASSETS_WITHOUT_CATALOG;
+    WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, nullptr);
+  });
+
   if (params->asset_catalog_visibility == FILE_SHOW_ASSETS_WITHOUT_CATALOG) {
     item.activate();
   }



More information about the Bf-blender-cvs mailing list