[Bf-blender-cvs] [c355be6faea] master: UI: Add AbstractView base class for views, unify reconstruction in there

Julian Eisel noreply at git.blender.org
Sun Jul 3 01:56:18 CEST 2022


Commit: c355be6faeacef6a65afbce97f9776d2a2c7f54c
Author: Julian Eisel
Date:   Sat Jul 2 21:49:21 2022 +0200
Branches: master
https://developer.blender.org/rBc355be6faeacef6a65afbce97f9776d2a2c7f54c

UI: Add AbstractView base class for views, unify reconstruction in there

No user visible changes expected.

There's plenty of duplicated code in the grid and the tree view, and I expect
this to become more. This starts the process of unifying these parts, which
should also make it easier to add new views. Complexity in the view classes is
reduced, and some type shenanigans for C compatibility and general view
management can be removed, since there is now a common base type.

For the start this ports some of the view reconstruction, where the view and
its items are compared to the version of itself in the previous redraw, so that
state (highlighted, active, renaming, collapsed, ...) can be preserved.
Notifier listening is also ported.

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

A	source/blender/editors/include/UI_abstract_view.hh
M	source/blender/editors/include/UI_grid_view.hh
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/include/UI_tree_view.hh
M	source/blender/editors/interface/CMakeLists.txt
A	source/blender/editors/interface/abstract_view.cc
M	source/blender/editors/interface/grid_view.cc
M	source/blender/editors/interface/interface_intern.h
M	source/blender/editors/interface/interface_view.cc
M	source/blender/editors/interface/tree_view.cc
M	source/blender/editors/util/CMakeLists.txt

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

diff --git a/source/blender/editors/include/UI_abstract_view.hh b/source/blender/editors/include/UI_abstract_view.hh
new file mode 100644
index 00000000000..477f68ca03f
--- /dev/null
+++ b/source/blender/editors/include/UI_abstract_view.hh
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup editorui
+ *
+ * Base for all views (UIs to display data sets), supporting common features.
+ * https://wiki.blender.org/wiki/Source/Interface/Views
+ *
+ * The base class manages reconstruction, most importantly keeping state over reconstructions.
+ */
+
+#pragma once
+
+struct wmNotifier;
+
+namespace blender::ui {
+
+class AbstractView {
+  bool is_reconstructed_ = false;
+
+ public:
+  virtual ~AbstractView() = default;
+
+  /** Listen to a notifier, returning true if a redraw is needed. */
+  virtual bool listen(const wmNotifier &) const;
+
+ protected:
+  AbstractView() = default;
+
+  virtual void update_children_from_old(const AbstractView &old_view) = 0;
+
+  /**
+   * Match the view and its items against an earlier version of itself (if any) and copy the old UI
+   * state (e.g. collapsed, active, selected, renaming, etc.) to the new one. See
+   * #AbstractViewItem.update_from_old().
+   * After this, reconstruction is complete (see #is_reconstructed()).
+   */
+  void update_from_old(uiBlock &new_block);
+
+  /**
+   * Check if the view is fully (re-)constructed. That means, both the build function and
+   * #update_from_old() have finished.
+   */
+  bool is_reconstructed() const;
+};
+
+}  // namespace blender::ui
diff --git a/source/blender/editors/include/UI_grid_view.hh b/source/blender/editors/include/UI_grid_view.hh
index 6f553f4fad1..cabc49e411c 100644
--- a/source/blender/editors/include/UI_grid_view.hh
+++ b/source/blender/editors/include/UI_grid_view.hh
@@ -13,6 +13,7 @@
 #include "BLI_map.hh"
 #include "BLI_vector.hh"
 
+#include "UI_abstract_view.hh"
 #include "UI_resources.h"
 
 struct bContext;
@@ -111,7 +112,7 @@ struct GridViewStyle {
   int tile_height = 0;
 };
 
-class AbstractGridView {
+class AbstractGridView : public AbstractView {
   friend class AbstractGridViewItem;
   friend class GridViewBuilder;
   friend class GridViewLayoutBuilder;
@@ -122,7 +123,6 @@ class AbstractGridView {
    * #update_from_old(). */
   Map<StringRef, AbstractGridViewItem *> item_map_;
   GridViewStyle style_;
-  bool is_reconstructed_ = false;
 
  public:
   AbstractGridView();
@@ -131,9 +131,6 @@ class AbstractGridView {
   using ItemIterFn = FunctionRef<void(AbstractGridViewItem &)>;
   void foreach_item(ItemIterFn iter_fn) const;
 
-  /** Listen to a notifier, returning true if a redraw is needed. */
-  virtual bool listen(const wmNotifier &) const;
-
   /**
    * Convenience wrapper constructing the item by forwarding given arguments to the constructor of
    * the type (\a ItemT).
@@ -154,19 +151,8 @@ class AbstractGridView {
  protected:
   virtual void build_items() = 0;
 
-  /**
-   * Check if the view is fully (re-)constructed. That means, both #build_items() and
-   * #update_from_old() have finished.
-   */
-  bool is_reconstructed() const;
-
  private:
-  /**
-   * Match the grid-view against an earlier version of itself (if any) and copy the old UI state
-   * (e.g. active, selected, renaming, etc.) to the new one. See
-   * #AbstractGridViewItem.update_from_old().
-   */
-  void update_from_old(uiBlock &new_block);
+  void update_children_from_old(const AbstractView &old_view) override;
   AbstractGridViewItem *find_matching_item(const AbstractGridViewItem &item_to_match,
                                            const AbstractGridView &view_to_search_in) const;
   /**
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index b2ec2102ddd..f39c62d8e2b 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -72,12 +72,10 @@ typedef struct uiBut uiBut;
 typedef struct uiButExtraOpIcon uiButExtraOpIcon;
 typedef struct uiLayout uiLayout;
 typedef struct uiPopupBlockHandle uiPopupBlockHandle;
-/* C handle for C++ #ui::AbstractTreeView type. */
-typedef struct uiTreeViewHandle uiTreeViewHandle;
+/* C handle for C++ #ui::AbstractView type. */
+typedef struct uiViewHandle uiViewHandle;
 /* C handle for C++ #ui::AbstractTreeViewItem type. */
 typedef struct uiTreeViewItemHandle uiTreeViewItemHandle;
-/* C handle for C++ #ui::AbstractGridView type. */
-typedef struct uiGridViewHandle uiGridViewHandle;
 /* C handle for C++ #ui::AbstractGridViewItem type. */
 typedef struct uiGridViewItemHandle uiGridViewItemHandle;
 
@@ -3243,15 +3241,6 @@ uiTreeViewItemHandle *UI_block_tree_view_find_item_at(const struct ARegion *regi
                                                       const int xy[2]) ATTR_NONNULL(1, 2);
 uiTreeViewItemHandle *UI_block_tree_view_find_active_item(const struct ARegion *region);
 
-/**
- * Listen to \a notifier, returning true if the region should redraw.
- */
-bool UI_tree_view_listen_should_redraw(const uiTreeViewHandle *view, const wmNotifier *notifier);
-/**
- * Listen to \a notifier, returning true if the region should redraw.
- */
-bool UI_grid_view_listen_should_redraw(const uiGridViewHandle *view, const wmNotifier *notifier);
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/editors/include/UI_tree_view.hh b/source/blender/editors/include/UI_tree_view.hh
index 1aeb13ca5cc..e9abe4c1d1f 100644
--- a/source/blender/editors/include/UI_tree_view.hh
+++ b/source/blender/editors/include/UI_tree_view.hh
@@ -19,6 +19,7 @@
 #include "BLI_function_ref.hh"
 #include "BLI_vector.hh"
 
+#include "UI_abstract_view.hh"
 #include "UI_resources.h"
 
 struct bContext;
@@ -112,7 +113,7 @@ using TreeViewOrItem = TreeViewItemContainer;
 /** \name Tree-View Base Class
  * \{ */
 
-class AbstractTreeView : public TreeViewItemContainer {
+class AbstractTreeView : public AbstractView, public TreeViewItemContainer {
   friend class AbstractTreeViewItem;
   friend class TreeViewBuilder;
 
@@ -122,35 +123,19 @@ class AbstractTreeView : public TreeViewItemContainer {
    */
   std::unique_ptr<std::array<char, MAX_NAME>> rename_buffer_;
 
-  bool is_reconstructed_ = false;
-
  public:
   virtual ~AbstractTreeView() = default;
 
   void foreach_item(ItemIterFn iter_fn, IterOptions options = IterOptions::None) const;
 
-  /** Listen to a notifier, returning true if a redraw is needed. */
-  virtual bool listen(const wmNotifier &) const;
-
   /** Only one item can be renamed at a time. */
   bool is_renaming() const;
 
  protected:
   virtual void build_tree() = 0;
 
-  /**
-   * Check if the tree is fully (re-)constructed. That means, both #build_tree() and
-   * #update_from_old() have finished.
-   */
-  bool is_reconstructed() const;
-
  private:
-  /**
-   * Match the tree-view against an earlier version of itself (if any) and copy the old UI state
-   * (e.g. collapsed, active, selected, renaming, etc.) to the new one. See
-   * #AbstractTreeViewItem.update_from_old().
-   */
-  void update_from_old(uiBlock &new_block);
+  void update_children_from_old(const AbstractView &old_view) override;
   static void update_children_from_old_recursive(const TreeViewOrItem &new_items,
                                                  const TreeViewOrItem &old_items);
   static AbstractTreeViewItem *find_matching_child(const AbstractTreeViewItem &lookup_item,
diff --git a/source/blender/editors/interface/CMakeLists.txt b/source/blender/editors/interface/CMakeLists.txt
index 2a1852bd6e7..1bdec57ac59 100644
--- a/source/blender/editors/interface/CMakeLists.txt
+++ b/source/blender/editors/interface/CMakeLists.txt
@@ -25,6 +25,7 @@ set(INC
 )
 
 set(SRC
+  abstract_view.cc
   grid_view.cc
   interface.cc
   interface_align.c
diff --git a/source/blender/editors/interface/abstract_view.cc b/source/blender/editors/interface/abstract_view.cc
new file mode 100644
index 00000000000..542d82a56a3
--- /dev/null
+++ b/source/blender/editors/interface/abstract_view.cc
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup edinterface
+ */
+
+#include "interface_intern.h"
+
+#include "UI_abstract_view.hh"
+
+namespace blender::ui {
+
+/* ---------------------------------------------------------------------- */
+/** \name View Reconstruction
+ * \{ */
+
+bool AbstractView::is_reconstructed() const
+{
+  return is_reconstructed_;
+}
+
+void AbstractView::update_from_old(uiBlock &new_block)
+{
+  uiBlock *old_block = new_block.oldblock;
+  if (!old_block) {
+    is_reconstructed_ = true;
+    return;
+  }
+
+  const uiViewHandle *old_view_handle = ui_block_view_find_matching_in_old_block(
+      &new_block, reinterpret_cast<uiViewHandle *>(this));
+  if (old_view_handle == nullptr) {
+    /* Initial construction, nothing to update. */
+    is_reconstructed_ = true;
+    return;
+  }
+
+  update_children_from_old(reinterpret_cast<const AbstractView &>(*old_view_handle));
+
+  /* Finished (re-)constructing the tree. */
+  is_reconstructed_ = true;
+}
+
+/** \} */
+
+bool AbstractView::listen(const wmNotifier & /*notifier*/) const
+{
+  /* Nothing by default. */
+  return false;
+}
+
+}  // namespace blender::ui
diff --git a/source/blender/editors/interface/grid_view.cc b/source/blender/editors/interface/grid_view.cc
index 194052862cf..19a2326fba1 100644
--- a/source/blender/editors/interface/grid_view.cc
+++ b/source/blender/editors/interface/grid_view.cc
@@ -43,12 +43,6 @@ void AbstractGridView::foreach_item(ItemIterFn iter_fn) const
   }
 }
 
-bool AbstractGridView::listen(const wmNotifier & /*notifier*/) const
-{
-  /* Nothing by default. */
-  return false;
-}
-
 AbstractGridViewItem *AbstractGridView::find_matching_item(
     const AbstractGridViewItem &item_to_match, const AbstractGridView &view_to_search_in) const
 {
@@ -67,34 +61,18 @@ void AbstractGridView::change_state_delayed()
   foreach_item([](AbstractGridViewItem &item) { item.change_state_delayed(); });
 }
 
-void AbstractGridView::update_from_old(uiBlock &new_block)
+void AbstractGridView::update_children_from_old(const AbstractView &old_view)
 {
-  uiGridViewHandle *old_view_handle = ui_block_grid_view_find_m

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list