UI: Refactor dropping support for the tree-view API
Introduces a dropping-controller API for the tree-view items, `AbstractTreeViewItemDropController`. This reduces responsibilities of the main tree-view item classes, which are already getting quite big. As I expect even more functionality to be needed for it (e.g. drag support), it's better to start introducing such controller types already.
This commit is contained in:
parent
c4fa17c67a
commit
d16e732638
|
@ -2781,11 +2781,8 @@ void UI_interface_tag_script_reload(void);
|
|||
bool UI_tree_view_item_is_active(const uiTreeViewItemHandle *item);
|
||||
bool UI_tree_view_item_matches(const uiTreeViewItemHandle *a, const uiTreeViewItemHandle *b);
|
||||
bool UI_tree_view_item_can_drop(const uiTreeViewItemHandle *item_, const struct wmDrag *drag);
|
||||
char *UI_tree_view_item_drop_tooltip(const uiTreeViewItemHandle *item, const struct wmDrag *drag);
|
||||
bool UI_tree_view_item_drop_handle(uiTreeViewItemHandle *item_, const struct ListBase *drags);
|
||||
char *UI_tree_view_item_drop_tooltip(const uiTreeViewItemHandle *item,
|
||||
const struct bContext *C,
|
||||
const struct wmDrag *drag,
|
||||
const struct wmEvent *event);
|
||||
bool UI_tree_view_item_can_rename(const uiTreeViewItemHandle *item_handle);
|
||||
void UI_tree_view_item_begin_rename(uiTreeViewItemHandle *item_handle);
|
||||
|
||||
|
|
|
@ -48,6 +48,7 @@ namespace blender::ui {
|
|||
|
||||
class AbstractTreeView;
|
||||
class AbstractTreeViewItem;
|
||||
class AbstractTreeViewItemDropController;
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/** \name Tree-View Item Container
|
||||
|
@ -242,17 +243,7 @@ class AbstractTreeViewItem : public TreeViewItemContainer {
|
|||
* arguments for checking if the item is currently in an active state.
|
||||
*/
|
||||
virtual void is_active(IsActiveFn is_active_fn);
|
||||
virtual bool on_drop(const wmDrag &drag);
|
||||
virtual bool can_drop(const wmDrag &drag) const;
|
||||
/**
|
||||
* Custom text to display when dragging over a tree item. Should explain what happens when
|
||||
* dropping the data onto this item. Will only be used if #AbstractTreeViewItem::can_drop()
|
||||
* returns true, so the implementing override doesn't have to check that again.
|
||||
* The returned value must be a translated string.
|
||||
*/
|
||||
virtual std::string drop_tooltip(const bContext &C,
|
||||
const wmDrag &drag,
|
||||
const wmEvent &event) const;
|
||||
|
||||
/**
|
||||
* Queries if the tree-view item supports renaming in principle. Renaming may still fail, e.g. if
|
||||
* another item is already being renamed.
|
||||
|
@ -282,6 +273,15 @@ class AbstractTreeViewItem : public TreeViewItemContainer {
|
|||
*/
|
||||
virtual bool matches(const AbstractTreeViewItem &other) const;
|
||||
|
||||
/**
|
||||
* If an item wants to support dropping data into it, it has to return a drop controller here.
|
||||
* That is an object implementing #AbstractTreeViewItemDropController.
|
||||
*
|
||||
* \note This drop controller may be requested for each event. The tree-view doesn't keep a drop
|
||||
* controller around currently. So it can not contain persistent state.
|
||||
*/
|
||||
virtual std::unique_ptr<AbstractTreeViewItemDropController> create_drop_controller() const;
|
||||
|
||||
void begin_renaming();
|
||||
void end_renaming();
|
||||
|
||||
|
@ -343,6 +343,45 @@ class AbstractTreeViewItem : public TreeViewItemContainer {
|
|||
|
||||
/** \} */
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/** \name Drag 'n Drop
|
||||
* \{ */
|
||||
|
||||
/**
|
||||
* Class to customize the drop behavior of a tree-item, plus the behavior when dragging over this
|
||||
* item. An item can return a drop controller for itself via a custom implementation of
|
||||
* #AbstractTreeViewItem::create_drop_controller().
|
||||
*/
|
||||
class AbstractTreeViewItemDropController {
|
||||
protected:
|
||||
AbstractTreeView &tree_view_;
|
||||
|
||||
public:
|
||||
AbstractTreeViewItemDropController(AbstractTreeView &tree_view);
|
||||
virtual ~AbstractTreeViewItemDropController() = default;
|
||||
|
||||
/**
|
||||
* Check if the data dragged with \a drag can be dropped on the item this controller is for.
|
||||
*/
|
||||
virtual bool can_drop(const wmDrag &drag) const = 0;
|
||||
/**
|
||||
* Custom text to display when dragging over a tree item. Should explain what happens when
|
||||
* dropping the data onto this item. Will only be used if #AbstractTreeViewItem::can_drop()
|
||||
* returns true, so the implementing override doesn't have to check that again.
|
||||
* The returned value must be a translated string.
|
||||
*/
|
||||
virtual std::string drop_tooltip(const wmDrag &drag) const = 0;
|
||||
/**
|
||||
* Execute the logic to apply a drop of the data dragged with \a drag onto/into the item this
|
||||
* controller is for.
|
||||
*/
|
||||
virtual bool on_drop(const wmDrag &drag) = 0;
|
||||
|
||||
template<class TreeViewType> inline TreeViewType &tree_view() const;
|
||||
};
|
||||
|
||||
/** \} */
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/** \name Predefined Tree-View Item Types
|
||||
*
|
||||
|
@ -390,4 +429,9 @@ inline ItemT &TreeViewItemContainer::add_tree_item(Args &&...args)
|
|||
add_tree_item(std::make_unique<ItemT>(std::forward<Args>(args)...)));
|
||||
}
|
||||
|
||||
template<class TreeViewType> TreeViewType &AbstractTreeViewItemDropController::tree_view() const
|
||||
{
|
||||
return static_cast<TreeViewType &>(tree_view_);
|
||||
}
|
||||
|
||||
} // namespace blender::ui
|
||||
|
|
|
@ -50,7 +50,7 @@ static char *ui_tree_view_drop_tooltip(bContext *C,
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
return UI_tree_view_item_drop_tooltip(hovered_tree_item, C, drag, event);
|
||||
return UI_tree_view_item_drop_tooltip(hovered_tree_item, drag);
|
||||
}
|
||||
|
||||
void ED_dropboxes_ui()
|
||||
|
|
|
@ -354,22 +354,11 @@ void AbstractTreeViewItem::is_active(IsActiveFn is_active_fn)
|
|||
is_active_fn_ = is_active_fn;
|
||||
}
|
||||
|
||||
bool AbstractTreeViewItem::on_drop(const wmDrag & /*drag*/)
|
||||
std::unique_ptr<AbstractTreeViewItemDropController> AbstractTreeViewItem::create_drop_controller()
|
||||
const
|
||||
{
|
||||
/* Do nothing by default. */
|
||||
return false;
|
||||
}
|
||||
|
||||
bool AbstractTreeViewItem::can_drop(const wmDrag & /*drag*/) const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string AbstractTreeViewItem::drop_tooltip(const bContext & /*C*/,
|
||||
const wmDrag & /*drag*/,
|
||||
const wmEvent & /*event*/) const
|
||||
{
|
||||
return TIP_("Drop into/onto tree item");
|
||||
/* There's no drop controller (and hence no drop support) by default. */
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool AbstractTreeViewItem::can_rename() const
|
||||
|
@ -553,6 +542,12 @@ void AbstractTreeViewItem::change_state_delayed()
|
|||
activate();
|
||||
}
|
||||
}
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
AbstractTreeViewItemDropController::AbstractTreeViewItemDropController(AbstractTreeView &tree_view)
|
||||
: tree_view_(tree_view)
|
||||
{
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
|
@ -683,16 +678,25 @@ bool UI_tree_view_item_matches(const uiTreeViewItemHandle *a_handle,
|
|||
bool UI_tree_view_item_can_drop(const uiTreeViewItemHandle *item_, const wmDrag *drag)
|
||||
{
|
||||
const AbstractTreeViewItem &item = reinterpret_cast<const AbstractTreeViewItem &>(*item_);
|
||||
return item.can_drop(*drag);
|
||||
const std::unique_ptr<AbstractTreeViewItemDropController> drop_controller =
|
||||
item.create_drop_controller();
|
||||
if (!drop_controller) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return drop_controller->can_drop(*drag);
|
||||
}
|
||||
|
||||
char *UI_tree_view_item_drop_tooltip(const uiTreeViewItemHandle *item_,
|
||||
const bContext *C,
|
||||
const wmDrag *drag,
|
||||
const wmEvent *event)
|
||||
char *UI_tree_view_item_drop_tooltip(const uiTreeViewItemHandle *item_, const wmDrag *drag)
|
||||
{
|
||||
const AbstractTreeViewItem &item = reinterpret_cast<const AbstractTreeViewItem &>(*item_);
|
||||
return BLI_strdup(item.drop_tooltip(*C, *drag, *event).c_str());
|
||||
const std::unique_ptr<AbstractTreeViewItemDropController> drop_controller =
|
||||
item.create_drop_controller();
|
||||
if (!drop_controller) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return BLI_strdup(drop_controller->drop_tooltip(*drag).c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -702,10 +706,12 @@ char *UI_tree_view_item_drop_tooltip(const uiTreeViewItemHandle *item_,
|
|||
bool UI_tree_view_item_drop_handle(uiTreeViewItemHandle *item_, const ListBase *drags)
|
||||
{
|
||||
AbstractTreeViewItem &item = reinterpret_cast<AbstractTreeViewItem &>(*item_);
|
||||
std::unique_ptr<AbstractTreeViewItemDropController> drop_controller =
|
||||
item.create_drop_controller();
|
||||
|
||||
LISTBASE_FOREACH (const wmDrag *, drag, drags) {
|
||||
if (item.can_drop(*drag)) {
|
||||
return item.on_drop(*drag);
|
||||
if (drop_controller->can_drop(*drag)) {
|
||||
return drop_controller->on_drop(*drag);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -60,6 +60,7 @@ class AssetCatalogTreeView : public ui::AbstractTreeView {
|
|||
SpaceFile &space_file_;
|
||||
|
||||
friend class AssetCatalogTreeViewItem;
|
||||
friend class AssetCatalogDropController;
|
||||
|
||||
public:
|
||||
AssetCatalogTreeView(::AssetLibrary *library,
|
||||
|
@ -86,25 +87,34 @@ class AssetCatalogTreeViewItem : public ui::BasicTreeViewItem {
|
|||
public:
|
||||
AssetCatalogTreeViewItem(AssetCatalogTreeItem *catalog_item);
|
||||
|
||||
static bool has_droppable_item(const wmDrag &drag);
|
||||
static bool drop_into_catalog(const AssetCatalogTreeView &tree_view,
|
||||
const wmDrag &drag,
|
||||
CatalogID catalog_id,
|
||||
StringRefNull simple_name = "");
|
||||
|
||||
void on_activate() override;
|
||||
|
||||
void build_row(uiLayout &row) override;
|
||||
void build_context_menu(bContext &C, uiLayout &column) const override;
|
||||
|
||||
bool can_drop(const wmDrag &drag) const override;
|
||||
std::string drop_tooltip(const bContext &C,
|
||||
const wmDrag &drag,
|
||||
const wmEvent &event) const override;
|
||||
bool on_drop(const wmDrag &drag) override;
|
||||
|
||||
bool can_rename() const override;
|
||||
bool rename(StringRefNull new_name) override;
|
||||
|
||||
/** Add dropping support for catalog items. */
|
||||
std::unique_ptr<ui::AbstractTreeViewItemDropController> create_drop_controller() const override;
|
||||
};
|
||||
|
||||
class AssetCatalogDropController : public ui::AbstractTreeViewItemDropController {
|
||||
AssetCatalogTreeItem &catalog_item_;
|
||||
|
||||
public:
|
||||
explicit AssetCatalogDropController(AssetCatalogTreeView &tree_view,
|
||||
AssetCatalogTreeItem &catalog_item);
|
||||
|
||||
bool can_drop(const wmDrag &drag) const override;
|
||||
std::string drop_tooltip(const wmDrag &drag) const override;
|
||||
bool on_drop(const wmDrag &drag) override;
|
||||
|
||||
static bool has_droppable_item(const wmDrag &drag);
|
||||
static bool drop_into_catalog(const AssetCatalogTreeView &tree_view,
|
||||
const wmDrag &drag,
|
||||
CatalogID catalog_id,
|
||||
StringRefNull simple_name = "");
|
||||
};
|
||||
|
||||
/** Only reason this isn't just `BasicTreeViewItem` is to add a '+' icon for adding a root level
|
||||
|
@ -118,11 +128,15 @@ class AssetCatalogTreeViewAllItem : public ui::BasicTreeViewItem {
|
|||
class AssetCatalogTreeViewUnassignedItem : public ui::BasicTreeViewItem {
|
||||
using BasicTreeViewItem::BasicTreeViewItem;
|
||||
|
||||
bool can_drop(const wmDrag &drag) const override;
|
||||
std::string drop_tooltip(const bContext &C,
|
||||
const wmDrag &drag,
|
||||
const wmEvent &event) const override;
|
||||
bool on_drop(const wmDrag &drag) override;
|
||||
struct DropController : public ui::AbstractTreeViewItemDropController {
|
||||
DropController(AssetCatalogTreeView &tree_view);
|
||||
|
||||
bool can_drop(const wmDrag &drag) const override;
|
||||
std::string drop_tooltip(const wmDrag &drag) const override;
|
||||
bool on_drop(const wmDrag &drag) override;
|
||||
};
|
||||
|
||||
std::unique_ptr<ui::AbstractTreeViewItemDropController> create_drop_controller() const override;
|
||||
};
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
@ -275,20 +289,38 @@ void AssetCatalogTreeViewItem::build_context_menu(bContext &C, uiLayout &column)
|
|||
UI_menutype_draw(&C, mt, &column);
|
||||
}
|
||||
|
||||
bool AssetCatalogTreeViewItem::has_droppable_item(const wmDrag &drag)
|
||||
bool AssetCatalogTreeViewItem::can_rename() const
|
||||
{
|
||||
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
|
||||
|
||||
/* There needs to be at least one asset from the current file. */
|
||||
LISTBASE_FOREACH (const wmDragAssetListItem *, asset_item, asset_drags) {
|
||||
if (!asset_item->is_external) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AssetCatalogTreeViewItem::can_drop(const wmDrag &drag) const
|
||||
bool AssetCatalogTreeViewItem::rename(StringRefNull new_name)
|
||||
{
|
||||
/* Important to keep state. */
|
||||
BasicTreeViewItem::rename(new_name);
|
||||
|
||||
const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
|
||||
get_tree_view());
|
||||
ED_asset_catalog_rename(tree_view.asset_library_, catalog_item_.get_catalog_id(), new_name);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<ui::AbstractTreeViewItemDropController> AssetCatalogTreeViewItem::
|
||||
create_drop_controller() const
|
||||
{
|
||||
return std::make_unique<AssetCatalogDropController>(
|
||||
static_cast<AssetCatalogTreeView &>(get_tree_view()), catalog_item_);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
AssetCatalogDropController::AssetCatalogDropController(AssetCatalogTreeView &tree_view,
|
||||
AssetCatalogTreeItem &catalog_item)
|
||||
: ui::AbstractTreeViewItemDropController(tree_view), catalog_item_(catalog_item)
|
||||
{
|
||||
}
|
||||
|
||||
bool AssetCatalogDropController::can_drop(const wmDrag &drag) const
|
||||
{
|
||||
if (drag.type != WM_DRAG_ASSET_LIST) {
|
||||
return false;
|
||||
|
@ -296,9 +328,7 @@ bool AssetCatalogTreeViewItem::can_drop(const wmDrag &drag) const
|
|||
return has_droppable_item(drag);
|
||||
}
|
||||
|
||||
std::string AssetCatalogTreeViewItem::drop_tooltip(const bContext & /*C*/,
|
||||
const wmDrag &drag,
|
||||
const wmEvent & /*event*/) const
|
||||
std::string AssetCatalogDropController::drop_tooltip(const wmDrag &drag) const
|
||||
{
|
||||
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
|
||||
const bool is_multiple_assets = !BLI_listbase_is_single(asset_drags);
|
||||
|
@ -312,10 +342,18 @@ std::string AssetCatalogTreeViewItem::drop_tooltip(const bContext & /*C*/,
|
|||
")";
|
||||
}
|
||||
|
||||
bool AssetCatalogTreeViewItem::drop_into_catalog(const AssetCatalogTreeView &tree_view,
|
||||
const wmDrag &drag,
|
||||
CatalogID catalog_id,
|
||||
StringRefNull simple_name)
|
||||
bool AssetCatalogDropController::on_drop(const wmDrag &drag)
|
||||
{
|
||||
return drop_into_catalog(tree_view<AssetCatalogTreeView>(),
|
||||
drag,
|
||||
catalog_item_.get_catalog_id(),
|
||||
catalog_item_.get_simple_name());
|
||||
}
|
||||
|
||||
bool AssetCatalogDropController::drop_into_catalog(const AssetCatalogTreeView &tree_view,
|
||||
const wmDrag &drag,
|
||||
CatalogID catalog_id,
|
||||
StringRefNull simple_name)
|
||||
{
|
||||
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
|
||||
if (!asset_drags) {
|
||||
|
@ -339,28 +377,17 @@ bool AssetCatalogTreeViewItem::drop_into_catalog(const AssetCatalogTreeView &tre
|
|||
return true;
|
||||
}
|
||||
|
||||
bool AssetCatalogTreeViewItem::on_drop(const wmDrag &drag)
|
||||
bool AssetCatalogDropController::has_droppable_item(const wmDrag &drag)
|
||||
{
|
||||
const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
|
||||
get_tree_view());
|
||||
return drop_into_catalog(
|
||||
tree_view, drag, catalog_item_.get_catalog_id(), catalog_item_.get_simple_name());
|
||||
}
|
||||
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
|
||||
|
||||
bool AssetCatalogTreeViewItem::can_rename() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AssetCatalogTreeViewItem::rename(StringRefNull new_name)
|
||||
{
|
||||
/* Important to keep state. */
|
||||
BasicTreeViewItem::rename(new_name);
|
||||
|
||||
const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
|
||||
get_tree_view());
|
||||
ED_asset_catalog_rename(tree_view.asset_library_, catalog_item_.get_catalog_id(), new_name);
|
||||
return true;
|
||||
/* There needs to be at least one asset from the current file. */
|
||||
LISTBASE_FOREACH (const wmDragAssetListItem *, asset_item, asset_drags) {
|
||||
if (!asset_item->is_external) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
@ -382,17 +409,28 @@ void AssetCatalogTreeViewAllItem::build_row(uiLayout &row)
|
|||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
bool AssetCatalogTreeViewUnassignedItem::can_drop(const wmDrag &drag) const
|
||||
std::unique_ptr<ui::AbstractTreeViewItemDropController> AssetCatalogTreeViewUnassignedItem::
|
||||
create_drop_controller() const
|
||||
{
|
||||
return std::make_unique<AssetCatalogTreeViewUnassignedItem::DropController>(
|
||||
static_cast<AssetCatalogTreeView &>(get_tree_view()));
|
||||
}
|
||||
|
||||
AssetCatalogTreeViewUnassignedItem::DropController::DropController(AssetCatalogTreeView &tree_view)
|
||||
: ui::AbstractTreeViewItemDropController(tree_view)
|
||||
{
|
||||
}
|
||||
|
||||
bool AssetCatalogTreeViewUnassignedItem::DropController::can_drop(const wmDrag &drag) const
|
||||
{
|
||||
if (drag.type != WM_DRAG_ASSET_LIST) {
|
||||
return false;
|
||||
}
|
||||
return AssetCatalogTreeViewItem::has_droppable_item(drag);
|
||||
return AssetCatalogDropController::has_droppable_item(drag);
|
||||
}
|
||||
|
||||
std::string AssetCatalogTreeViewUnassignedItem::drop_tooltip(const bContext & /*C*/,
|
||||
const wmDrag &drag,
|
||||
const wmEvent & /*event*/) const
|
||||
std::string AssetCatalogTreeViewUnassignedItem::DropController::drop_tooltip(
|
||||
const wmDrag &drag) const
|
||||
{
|
||||
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
|
||||
const bool is_multiple_assets = !BLI_listbase_is_single(asset_drags);
|
||||
|
@ -401,16 +439,17 @@ std::string AssetCatalogTreeViewUnassignedItem::drop_tooltip(const bContext & /*
|
|||
TIP_("Move asset out of any catalog");
|
||||
}
|
||||
|
||||
bool AssetCatalogTreeViewUnassignedItem::on_drop(const wmDrag &drag)
|
||||
bool AssetCatalogTreeViewUnassignedItem::DropController::on_drop(const wmDrag &drag)
|
||||
{
|
||||
const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
|
||||
get_tree_view());
|
||||
/* Assign to nil catalog ID. */
|
||||
return AssetCatalogTreeViewItem::drop_into_catalog(tree_view, drag, CatalogID{});
|
||||
return AssetCatalogDropController::drop_into_catalog(
|
||||
tree_view<AssetCatalogTreeView>(), drag, CatalogID{});
|
||||
}
|
||||
|
||||
} // namespace blender::ed::asset_browser
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
namespace blender::ed::asset_browser {
|
||||
|
||||
class AssetCatalogFilterSettings {
|
||||
|
|
Loading…
Reference in New Issue