diff options
author | Julian Eisel <julian@blender.org> | 2022-07-02 22:49:21 +0300 |
---|---|---|
committer | Julian Eisel <julian@blender.org> | 2022-07-03 02:55:38 +0300 |
commit | c355be6faeacef6a65afbce97f9776d2a2c7f54c (patch) | |
tree | e68bff81fa87b208ed433ade2b8fce7c522ed59a /source/blender/editors/interface/interface_view.cc | |
parent | 4ffee9a48d1bc01442e554d44a1f55dfc459a221 (diff) |
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.
Diffstat (limited to 'source/blender/editors/interface/interface_view.cc')
-rw-r--r-- | source/blender/editors/interface/interface_view.cc | 81 |
1 files changed, 22 insertions, 59 deletions
diff --git a/source/blender/editors/interface/interface_view.cc b/source/blender/editors/interface/interface_view.cc index 699ac0c2b53..72a6e3ba5e3 100644 --- a/source/blender/editors/interface/interface_view.cc +++ b/source/blender/editors/interface/interface_view.cc @@ -25,6 +25,7 @@ #include "UI_interface.hh" +#include "UI_abstract_view.hh" #include "UI_grid_view.hh" #include "UI_tree_view.hh" @@ -36,43 +37,27 @@ using namespace blender::ui; * #std::variant. */ struct ViewLink : public Link { - using TreeViewPtr = std::unique_ptr<AbstractTreeView>; - using GridViewPtr = std::unique_ptr<AbstractGridView>; - std::string idname; - /* NOTE: Can't use std::get() on this until minimum macOS deployment target is 10.14. */ - std::variant<TreeViewPtr, GridViewPtr> view; + std::unique_ptr<AbstractView> view; }; -template<class T> constexpr void check_if_valid_view_type() -{ - static_assert(std::is_same_v<T, AbstractTreeView> || std::is_same_v<T, AbstractGridView>, - "Unsupported view type"); -} - -template<class T> T *get_view_from_link(ViewLink &link) -{ - auto *t_uptr = std::get_if<std::unique_ptr<T>>(&link.view); - return t_uptr ? t_uptr->get() : nullptr; -} - template<class T> -static T *ui_block_add_view_impl(uiBlock &block, StringRef idname, std::unique_ptr<T> view) +static T *ui_block_add_view_impl(uiBlock &block, + StringRef idname, + std::unique_ptr<AbstractView> view) { - check_if_valid_view_type<T>(); - ViewLink *view_link = MEM_new<ViewLink>(__func__); BLI_addtail(&block.views, view_link); view_link->view = std::move(view); view_link->idname = idname; - return get_view_from_link<T>(*view_link); + return dynamic_cast<T *>(view_link->view.get()); } AbstractGridView *UI_block_add_view(uiBlock &block, StringRef idname, - std::unique_ptr<AbstractGridView> tree_view) + std::unique_ptr<AbstractView> tree_view) { return ui_block_add_view_impl<AbstractGridView>(block, idname, std::move(tree_view)); } @@ -96,17 +81,8 @@ void UI_block_views_listen(const uiBlock *block, const wmRegionListenerParams *l ARegion *region = listener_params->region; LISTBASE_FOREACH (ViewLink *, view_link, &block->views) { - if (AbstractGridView *grid_view = get_view_from_link<AbstractGridView>(*view_link)) { - if (UI_grid_view_listen_should_redraw(reinterpret_cast<uiGridViewHandle *>(grid_view), - listener_params->notifier)) { - ED_region_tag_redraw(region); - } - } - else if (AbstractTreeView *tree_view = get_view_from_link<AbstractTreeView>(*view_link)) { - if (UI_tree_view_listen_should_redraw(reinterpret_cast<uiTreeViewHandle *>(tree_view), - listener_params->notifier)) { - ED_region_tag_redraw(region); - } + if (view_link->view->listen(*listener_params->notifier)) { + ED_region_tag_redraw(region); } } } @@ -131,13 +107,11 @@ uiTreeViewItemHandle *UI_block_tree_view_find_active_item(const ARegion *region) return tree_row_but->tree_item; } -template<class T> static StringRef ui_block_view_find_idname(const uiBlock &block, const T &view) +static StringRef ui_block_view_find_idname(const uiBlock &block, const AbstractView &view) { - check_if_valid_view_type<T>(); - /* First get the idname the of the view we're looking for. */ LISTBASE_FOREACH (ViewLink *, view_link, &block.views) { - if (get_view_from_link<T>(*view_link) == &view) { + if (view_link->view.get() == &view) { return view_link->idname; } } @@ -146,10 +120,9 @@ template<class T> static StringRef ui_block_view_find_idname(const uiBlock &bloc } template<class T> -static T *ui_block_view_find_matching_in_old_block(const uiBlock &new_block, const T &new_view) +static T *ui_block_view_find_matching_in_old_block_impl(const uiBlock &new_block, + const T &new_view) { - check_if_valid_view_type<T>(); - uiBlock *old_block = new_block.oldblock; if (!old_block) { return nullptr; @@ -162,31 +135,21 @@ static T *ui_block_view_find_matching_in_old_block(const uiBlock &new_block, con LISTBASE_FOREACH (ViewLink *, old_view_link, &old_block->views) { if (old_view_link->idname == idname) { - return get_view_from_link<T>(*old_view_link); + return dynamic_cast<T *>(old_view_link->view.get()); } } return nullptr; } -uiTreeViewHandle *ui_block_tree_view_find_matching_in_old_block( - const uiBlock *new_block, const uiTreeViewHandle *new_view_handle) -{ - BLI_assert(new_block && new_view_handle); - const AbstractTreeView &new_view = reinterpret_cast<const AbstractTreeView &>(*new_view_handle); - - AbstractTreeView *old_view = ui_block_view_find_matching_in_old_block(*new_block, new_view); - return reinterpret_cast<uiTreeViewHandle *>(old_view); -} - -uiGridViewHandle *ui_block_grid_view_find_matching_in_old_block( - const uiBlock *new_block, const uiGridViewHandle *new_view_handle) +uiViewHandle *ui_block_view_find_matching_in_old_block(const uiBlock *new_block, + const uiViewHandle *new_view_handle) { BLI_assert(new_block && new_view_handle); - const AbstractGridView &new_view = reinterpret_cast<const AbstractGridView &>(*new_view_handle); + const AbstractView &new_view = reinterpret_cast<const AbstractView &>(*new_view_handle); - AbstractGridView *old_view = ui_block_view_find_matching_in_old_block(*new_block, new_view); - return reinterpret_cast<uiGridViewHandle *>(old_view); + AbstractView *old_view = ui_block_view_find_matching_in_old_block_impl(*new_block, new_view); + return reinterpret_cast<uiViewHandle *>(old_view); } uiButTreeRow *ui_block_view_find_treerow_in_old_block(const uiBlock *new_block, @@ -199,9 +162,9 @@ uiButTreeRow *ui_block_view_find_treerow_in_old_block(const uiBlock *new_block, const AbstractTreeViewItem &new_item = *reinterpret_cast<const AbstractTreeViewItem *>( new_item_handle); - const AbstractTreeView *old_tree_view = ui_block_view_find_matching_in_old_block( + const AbstractView *old_view = ui_block_view_find_matching_in_old_block_impl( *new_block, new_item.get_tree_view()); - if (!old_tree_view) { + if (!old_view) { return nullptr; } @@ -216,7 +179,7 @@ uiButTreeRow *ui_block_view_find_treerow_in_old_block(const uiBlock *new_block, AbstractTreeViewItem &old_item = *reinterpret_cast<AbstractTreeViewItem *>( old_treerow_but->tree_item); /* Check if the row is from the expected tree-view. */ - if (&old_item.get_tree_view() != old_tree_view) { + if (&old_item.get_tree_view() != old_view) { continue; } |