Refactor: UI: Don't use hacky asset handle for asset shelf context

Another step towards removing `AssetHandle`, see #108806.

When introducing the asset shelf, much of it was using the
hacky/temporary `AssetHandle` type since `AssetRepresentation` wasn't
sufficient yet. Passing an asset through context also required using
`AssetHandle`. All this has changed since then, and we can switch the
asset shelf to pass the asset through context via `AssetRepresentation`
instead, as it should be. I think none of the asset shelf uses the asset
handle anymore now, hooray :)

Minor compatibility breakage: In Python `context.active_file` doesn't
work in the asset shelf anymore now. `context.asset` can be used
instead.
This commit is contained in:
Julian Eisel
2025-06-12 12:41:53 +02:00
parent ef570777e9
commit 7cd26d37ea
2 changed files with 13 additions and 29 deletions

View File

@@ -688,7 +688,7 @@ int context(const bContext *C, const char *member, bContextDataResult *result)
static const char *context_dir[] = {
"asset_shelf",
"asset_library_reference",
"active_file", /* XXX yuk... */
"asset",
nullptr,
};
@@ -722,8 +722,7 @@ int context(const bContext *C, const char *member, bContextDataResult *result)
return CTX_RESULT_OK;
}
/* XXX hack. Get the asset from the active item, but needs to be the file... */
if (CTX_data_equals(member, "active_file")) {
if (CTX_data_equals(member, "asset")) {
const ARegion *region = CTX_wm_region(C);
const uiBut *but = UI_region_views_find_active_item_but(region);
if (!but) {
@@ -735,13 +734,13 @@ int context(const bContext *C, const char *member, bContextDataResult *result)
return CTX_RESULT_NO_DATA;
}
const PointerRNA *file_ptr = CTX_store_ptr_lookup(
but_context, "active_file", &RNA_FileSelectEntry);
if (!file_ptr) {
const PointerRNA *asset_ptr = CTX_store_ptr_lookup(
but_context, "asset", &RNA_AssetRepresentation);
if (!asset_ptr) {
return CTX_RESULT_NO_DATA;
}
CTX_data_pointer_set_ptr(result, file_ptr);
CTX_data_pointer_set_ptr(result, asset_ptr);
return CTX_RESULT_OK;
}

View File

@@ -56,14 +56,10 @@ class AssetView : public ui::AbstractGridView {
class AssetViewItem : public ui::PreviewGridItem {
asset_system::AssetRepresentation &asset_;
int asset_index_;
bool allow_asset_drag_ = true;
public:
AssetViewItem(asset_system::AssetRepresentation &asset_,
int asset_index,
StringRef identifier,
StringRef label);
AssetViewItem(asset_system::AssetRepresentation &asset_, StringRef identifier, StringRef label);
void disable_asset_drag();
void build_grid_tile(const bContext &C, uiLayout &layout) const override;
@@ -105,7 +101,7 @@ void AssetView::build_items()
return;
}
list::iterate(library_ref_, [&](asset_system::AssetRepresentation &asset, int asset_index) {
list::iterate(library_ref_, [&](asset_system::AssetRepresentation &asset) {
if (shelf_.type->asset_poll && !shelf_.type->asset_poll(shelf_.type, &asset)) {
return true;
}
@@ -119,8 +115,7 @@ void AssetView::build_items()
const bool show_names = (shelf_.settings.display_flag & ASSETSHELF_SHOW_NAMES);
const StringRef identifier = asset.library_relative_identifier();
AssetViewItem &item = this->add_item<AssetViewItem>(
asset, asset_index, identifier, asset.get_name());
AssetViewItem &item = this->add_item<AssetViewItem>(asset, identifier, asset.get_name());
if (!show_names) {
item.hide_label();
}
@@ -174,10 +169,9 @@ static std::optional<asset_system::AssetCatalogFilter> catalog_filter_from_shelf
/* ---------------------------------------------------------------------- */
AssetViewItem::AssetViewItem(asset_system::AssetRepresentation &asset,
int asset_index,
StringRef identifier,
StringRef label)
: ui::PreviewGridItem(identifier, label, ICON_NONE), asset_(asset), asset_index_(asset_index)
: ui::PreviewGridItem(identifier, label, ICON_NONE), asset_(asset)
{
}
@@ -212,18 +206,9 @@ void AssetViewItem::build_grid_tile(const bContext & /*C*/, uiLayout &layout) co
const AssetView &asset_view = reinterpret_cast<const AssetView &>(this->get_view());
const AssetShelfType &shelf_type = *asset_view.shelf_.type;
AssetHandle asset_handle = list::asset_handle_get_by_index(&asset_view.library_ref_,
asset_index_);
PointerRNA file_ptr = RNA_pointer_create_discrete(
nullptr,
&RNA_FileSelectEntry,
/* XXX passing file pointer here, should be asset handle or asset representation. */
const_cast<FileDirEntry *>(asset_handle.file_data));
UI_but_context_ptr_set(uiLayoutGetBlock(&layout),
reinterpret_cast<uiBut *>(view_item_but_),
"active_file",
&file_ptr);
PointerRNA asset_ptr = RNA_pointer_create_discrete(nullptr, &RNA_AssetRepresentation, &asset_);
UI_but_context_ptr_set(
uiLayoutGetBlock(&layout), reinterpret_cast<uiBut *>(view_item_but_), "asset", &asset_ptr);
uiBut *item_but = reinterpret_cast<uiBut *>(this->view_item_button());
if (std::optional<wmOperatorCallParams> activate_op = create_activate_operator_params(