diff options
author | Julian Eisel <julian@blender.org> | 2022-07-02 23:36:50 +0300 |
---|---|---|
committer | Julian Eisel <julian@blender.org> | 2022-07-03 02:55:38 +0300 |
commit | e86c2f7288724bd6fec33ff43e89816d7520a2b3 (patch) | |
tree | d31a7f2089fa6c2de7a32bde859893d499f1f2f2 /source | |
parent | c355be6faeacef6a65afbce97f9776d2a2c7f54c (diff) |
UI: Move rename buffer management to new view base class
Renaming is a nice example of a feature that shouldn't need a specific
implementation for a specific view type (e.g. grid or tree view). So it's
something that can be supported in the general view code. Individual views can
use it "for free" then. This ports the view level part of the renaming code,
the view item level part of it can be ported once we have a common base class
for the view items.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/editors/include/UI_abstract_view.hh | 24 | ||||
-rw-r--r-- | source/blender/editors/include/UI_tree_view.hh | 9 | ||||
-rw-r--r-- | source/blender/editors/interface/abstract_view.cc | 54 | ||||
-rw-r--r-- | source/blender/editors/interface/tree_view.cc | 33 |
4 files changed, 85 insertions, 35 deletions
diff --git a/source/blender/editors/include/UI_abstract_view.hh b/source/blender/editors/include/UI_abstract_view.hh index 477f68ca03f..82f81f1702b 100644 --- a/source/blender/editors/include/UI_abstract_view.hh +++ b/source/blender/editors/include/UI_abstract_view.hh @@ -6,17 +6,31 @@ * 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. + * One of the most important responsibilities of the base class is managing reconstruction, + * enabling state that is persistent over reconstructions/redraws. */ #pragma once +#include <array> +#include <memory> + +#include "BLI_span.hh" + struct wmNotifier; namespace blender::ui { class AbstractView { bool is_reconstructed_ = false; + /** + * Only one item can be renamed at a time. So rather than giving each item an own rename buffer + * (which just adds unused memory in most cases), have one here that is managed by the view. + * + * This fixed-size buffer is needed because that's what the rename button requires. In future we + * may be able to bind the button to a `std::string` or similar. + */ + std::unique_ptr<std::array<char, MAX_NAME>> rename_buffer_; public: virtual ~AbstractView() = default; @@ -24,6 +38,14 @@ class AbstractView { /** 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; + /** \return If renaming was started successfully. */ + bool begin_renaming(); + void end_renaming(); + Span<char> get_rename_buffer() const; + MutableSpan<char> get_rename_buffer(); + protected: AbstractView() = default; diff --git a/source/blender/editors/include/UI_tree_view.hh b/source/blender/editors/include/UI_tree_view.hh index e9abe4c1d1f..9527df590b7 100644 --- a/source/blender/editors/include/UI_tree_view.hh +++ b/source/blender/editors/include/UI_tree_view.hh @@ -117,20 +117,11 @@ class AbstractTreeView : public AbstractView, public TreeViewItemContainer { friend class AbstractTreeViewItem; friend class TreeViewBuilder; - /** - * Only one item can be renamed at a time. So the tree is informed about the renaming state to - * enforce that. - */ - std::unique_ptr<std::array<char, MAX_NAME>> rename_buffer_; - public: virtual ~AbstractTreeView() = default; void foreach_item(ItemIterFn iter_fn, IterOptions options = IterOptions::None) const; - /** Only one item can be renamed at a time. */ - bool is_renaming() const; - protected: virtual void build_tree() = 0; diff --git a/source/blender/editors/interface/abstract_view.cc b/source/blender/editors/interface/abstract_view.cc index 542d82a56a3..dea9600fde4 100644 --- a/source/blender/editors/interface/abstract_view.cc +++ b/source/blender/editors/interface/abstract_view.cc @@ -27,7 +27,7 @@ void AbstractView::update_from_old(uiBlock &new_block) return; } - const uiViewHandle *old_view_handle = ui_block_view_find_matching_in_old_block( + 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. */ @@ -35,7 +35,15 @@ void AbstractView::update_from_old(uiBlock &new_block) return; } - update_children_from_old(reinterpret_cast<const AbstractView &>(*old_view_handle)); + AbstractView &old_view = reinterpret_cast<AbstractView &>(*old_view_handle); + + /* Update own persistent data. */ + /* Keep the rename buffer persistent while renaming! The rename button uses the buffer's + * pointer to identify itself over redraws. */ + rename_buffer_ = std::move(old_view.rename_buffer_); + old_view.rename_buffer_ = nullptr; + + update_children_from_old(old_view); /* Finished (re-)constructing the tree. */ is_reconstructed_ = true; @@ -43,10 +51,52 @@ void AbstractView::update_from_old(uiBlock &new_block) /** \} */ +/* ---------------------------------------------------------------------- */ +/** \name Default implementations of virtual functions + * \{ */ + bool AbstractView::listen(const wmNotifier & /*notifier*/) const { /* Nothing by default. */ return false; } +/** \} */ + +/* ---------------------------------------------------------------------- */ +/** \name Renaming + * \{ */ + +bool AbstractView::is_renaming() const +{ + return rename_buffer_ != nullptr; +} + +bool AbstractView::begin_renaming() +{ + if (is_renaming()) { + return false; + } + + rename_buffer_ = std::make_unique<decltype(rename_buffer_)::element_type>(); + return true; +} + +void AbstractView::end_renaming() +{ + BLI_assert(is_renaming()); + rename_buffer_ = nullptr; +} + +Span<char> AbstractView::get_rename_buffer() const +{ + return *rename_buffer_; +} +MutableSpan<char> AbstractView::get_rename_buffer() +{ + return *rename_buffer_; +} + +/** \} */ + } // namespace blender::ui diff --git a/source/blender/editors/interface/tree_view.cc b/source/blender/editors/interface/tree_view.cc index e892e7c272c..d29cf367e59 100644 --- a/source/blender/editors/interface/tree_view.cc +++ b/source/blender/editors/interface/tree_view.cc @@ -68,23 +68,9 @@ void AbstractTreeView::foreach_item(ItemIterFn iter_fn, IterOptions options) con foreach_item_recursive(iter_fn, options); } -bool AbstractTreeView::is_renaming() const -{ - return rename_buffer_ != nullptr; -} - void AbstractTreeView::update_children_from_old(const AbstractView &old_view) { - /* TODO: Get rid of const cast. */ - AbstractTreeView &old_tree_view = const_cast<AbstractTreeView &>( - dynamic_cast<const AbstractTreeView &>(old_view)); - - /* TODO: Move to AbstractView. */ - /* Update own persistent data. */ - /* Keep the rename buffer persistent while renaming! The rename button uses the buffer's - * pointer to identify itself over redraws. */ - rename_buffer_ = std::move(old_tree_view.rename_buffer_); - old_tree_view.rename_buffer_ = nullptr; + const AbstractTreeView &old_tree_view = dynamic_cast<const AbstractTreeView &>(old_view); update_children_from_old_recursive(*this, old_tree_view); } @@ -233,7 +219,7 @@ AbstractTreeViewItem *AbstractTreeViewItem::find_tree_item_from_rename_button( AbstractTreeViewItem *item = reinterpret_cast<AbstractTreeViewItem *>(tree_row_but->tree_item); const AbstractTreeView &tree_view = item->get_tree_view(); - if (item->is_renaming() && (tree_view.rename_buffer_->data() == rename_but.poin)) { + if (item->is_renaming() && (tree_view.get_rename_buffer().data() == rename_but.poin)) { return item; } } @@ -248,7 +234,7 @@ void AbstractTreeViewItem::rename_button_fn(bContext *UNUSED(C), void *arg, char BLI_assert(item); const AbstractTreeView &tree_view = item->get_tree_view(); - item->rename(tree_view.rename_buffer_->data()); + item->rename(tree_view.get_rename_buffer().data()); item->end_renaming(); } @@ -270,9 +256,9 @@ void AbstractTreeViewItem::add_rename_button(uiLayout &row) 0, UI_UNIT_X * 10, UI_UNIT_Y, - tree_view.rename_buffer_->data(), + tree_view.get_rename_buffer().data(), 1.0f, - tree_view.rename_buffer_->max_size(), + tree_view.get_rename_buffer().size(), 0, 0, ""); @@ -372,10 +358,11 @@ void AbstractTreeViewItem::begin_renaming() return; } - is_renaming_ = true; + if (tree_view.begin_renaming()) { + is_renaming_ = true; + } - tree_view.rename_buffer_ = std::make_unique<decltype(tree_view.rename_buffer_)::element_type>(); - std::copy(std::begin(label_), std::end(label_), std::begin(*tree_view.rename_buffer_)); + std::copy(std::begin(label_), std::end(label_), std::begin(tree_view.get_rename_buffer())); } void AbstractTreeViewItem::end_renaming() @@ -387,7 +374,7 @@ void AbstractTreeViewItem::end_renaming() is_renaming_ = false; AbstractTreeView &tree_view = get_tree_view(); - tree_view.rename_buffer_ = nullptr; + tree_view.end_renaming(); } AbstractTreeView &AbstractTreeViewItem::get_tree_view() const |