UI: Add AbstractViewItem base class

No user visible changes expected.

Similar to rBc355be6faeac, but for view items now instead of the view.
Not much of the item code is ported to use it yet, it's actually a bit
tricky for the most part. But just introducing the base class already
allows me to start unifying the view item buttons (`uiButTreeRow` and
`uiButGridTile`). This would be a nice improvement.
This commit is contained in:
Julian Eisel 2022-07-18 16:51:57 +02:00
parent 2f834bfc14
commit 348ec37f52
7 changed files with 60 additions and 27 deletions

View File

@ -15,12 +15,17 @@
#include <array>
#include <memory>
#include "DNA_defs.h"
#include "BLI_span.hh"
struct wmNotifier;
struct uiBlock;
namespace blender::ui {
class AbstractViewItem;
class AbstractView {
bool is_reconstructed_ = false;
/**
@ -66,4 +71,26 @@ class AbstractView {
bool is_reconstructed() const;
};
class AbstractViewItem {
protected:
bool is_active_ = false;
public:
virtual ~AbstractViewItem() = default;
protected:
AbstractViewItem() = default;
/**
* Copy persistent state (e.g. active, selection, etc.) from a matching item of
* the last redraw to this item. If sub-classes introduce more advanced state they should
* override this and make it update their state accordingly.
*
* \note Always call the base class implementation when overriding this!
*/
virtual void update_from_old(const AbstractViewItem &old);
void set_view(AbstractView &view);
};
} // namespace blender::ui

View File

@ -32,14 +32,12 @@ class AbstractGridView;
/** \name Grid-View Item Type
* \{ */
class AbstractGridViewItem {
class AbstractGridViewItem : public AbstractViewItem {
friend class AbstractGridView;
friend class GridViewLayoutBuilder;
const AbstractGridView *view_;
bool is_active_ = false;
protected:
/** Reference to a string that uniquely identifies this item in the view. */
StringRef identifier_{};
@ -77,13 +75,6 @@ class AbstractGridViewItem {
*/
virtual std::optional<bool> should_be_active() const;
/**
* Copy persistent state (e.g. active, selection, etc.) from a matching item of
* the last redraw to this item. If sub-classes introduce more advanced state they should
* override this and make it update their state accordingly.
*/
virtual void update_from_old(const AbstractGridViewItem &old);
/**
* Activates this item, deactivates other items, and calls the
* #AbstractGridViewItem::on_activate() function.

View File

@ -153,7 +153,7 @@ class AbstractTreeView : public AbstractView, public TreeViewItemContainer {
* It also stores state information that needs to be persistent over redraws, like the collapsed
* state.
*/
class AbstractTreeViewItem : public TreeViewItemContainer {
class AbstractTreeViewItem : public AbstractViewItem, public TreeViewItemContainer {
friend class AbstractTreeView;
friend class TreeViewLayoutBuilder;
/* Higher-level API. */
@ -161,7 +161,6 @@ class AbstractTreeViewItem : public TreeViewItemContainer {
private:
bool is_open_ = false;
bool is_active_ = false;
bool is_renaming_ = false;
protected:
@ -222,12 +221,8 @@ class AbstractTreeViewItem : public TreeViewItemContainer {
*/
virtual bool supports_collapsing() const;
/**
* Copy persistent state (e.g. is-collapsed flag, selection, etc.) from a matching item of
* the last redraw to this item. If sub-classes introduce more advanced state they should
* override this and make it update their state accordingly.
*/
virtual void update_from_old(const AbstractTreeViewItem &old);
/** See #AbstractViewItem::update_from_old(). */
virtual void update_from_old(const AbstractViewItem &old) override;
/**
* Compare this item to \a other to check if they represent the same data.

View File

@ -26,6 +26,7 @@ set(INC
set(SRC
abstract_view.cc
abstract_view_item.cc
grid_view.cc
interface.cc
interface_align.c

View File

@ -0,0 +1,22 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup edinterface
*/
#include "UI_abstract_view.hh"
namespace blender::ui {
/* ---------------------------------------------------------------------- */
/** \name View Reconstruction
* \{ */
void AbstractViewItem::update_from_old(const AbstractViewItem &old)
{
is_active_ = old.is_active_;
}
/** \} */
} // namespace blender::ui

View File

@ -158,11 +158,6 @@ void AbstractGridViewItem::change_state_delayed()
}
}
void AbstractGridViewItem::update_from_old(const AbstractGridViewItem &old)
{
is_active_ = old.is_active_;
}
void AbstractGridViewItem::activate()
{
BLI_assert_msg(get_view().is_reconstructed(),

View File

@ -339,11 +339,13 @@ void AbstractTreeViewItem::build_context_menu(bContext & /*C*/, uiLayout & /*col
/* No context menu by default. */
}
void AbstractTreeViewItem::update_from_old(const AbstractTreeViewItem &old)
void AbstractTreeViewItem::update_from_old(const AbstractViewItem &old)
{
is_open_ = old.is_open_;
is_active_ = old.is_active_;
is_renaming_ = old.is_renaming_;
AbstractViewItem::update_from_old(old);
const AbstractTreeViewItem &old_tree_item = dynamic_cast<const AbstractTreeViewItem &>(old);
is_open_ = old_tree_item.is_open_;
is_renaming_ = old_tree_item.is_renaming_;
}
bool AbstractTreeViewItem::matches(const AbstractTreeViewItem &other) const