Fix #121490: Unable to access asset from context in asset shelf

Displaying an operator in the context menu that would access the asset
via context would fail.

3 issues here in fact:
- 798219225d removed code to attach the asset to the button's context.
  Brought this back.
- If the `AssetShelf.get_active_asset()` method (in Python) isn't set,
  `AssetViewItem::should_be_active()` would always return false, making
  it impossible to lookup the active item. Instead it should return an
  empty value so the view's internal active state is used instead.
  Mistake in a700c90ec3.
- These kind of context queries can't rely on cursor coordinates, it's
  not well defined what state they are in. In fact `wmWindow.eventstate`
  is unset in debug builds to help enforce this. Also an issue from
  a700c90ec3. With the above two corrections this change can be
  undone so the query doesn't depend on cursor coordinates anymore.
This commit is contained in:
Julian Eisel
2024-05-07 17:27:45 +02:00
parent 286cbc8317
commit 952bc075ae
4 changed files with 15 additions and 5 deletions

View File

@@ -656,7 +656,7 @@ int context(const bContext *C, const char *member, bContextDataResult *result)
/* XXX hack. Get the asset from the active item, but needs to be the file... */
if (CTX_data_equals(member, "active_file")) {
const ARegion *region = CTX_wm_region(C);
const uiBut *but = UI_region_views_find_mouse_over_but(CTX_wm_window(C), region);
const uiBut *but = UI_region_views_find_active_item_but(region);
if (!but) {
return CTX_RESULT_NO_DATA;
}

View File

@@ -206,6 +206,16 @@ void AssetViewItem::build_grid_tile(uiLayout &layout) const
const AssetView &asset_view = reinterpret_cast<const AssetView &>(this->get_view());
const AssetShelfType &shelf_type = *asset_view.shelf_.type;
PointerRNA file_ptr = RNA_pointer_create(
nullptr,
&RNA_FileSelectEntry,
/* XXX passing file pointer here, should be asset handle or asset representation. */
const_cast<FileDirEntry *>(asset_.file_data));
UI_but_context_ptr_set(uiLayoutGetBlock(&layout),
reinterpret_cast<uiBut *>(view_item_but_),
"active_file",
&file_ptr);
wmOperatorType *ot = WM_operatortype_find(shelf_type.activate_operator.c_str(), true);
PointerRNA op_props = PointerRNA_NULL;
if (ot) {
@@ -230,7 +240,7 @@ std::optional<bool> AssetViewItem::should_be_active() const
{
const AssetView &asset_view = dynamic_cast<const AssetView &>(this->get_view());
if (!asset_view.active_asset_) {
return false;
return {};
}
const asset_system::AssetRepresentation *asset = handle_get_representation(&asset_);
AssetWeakReference weak_ref = asset->make_weak_reference();

View File

@@ -3397,4 +3397,4 @@ blender::ui::AbstractView *UI_region_view_find_at(const ARegion *region, const i
blender::ui::AbstractViewItem *UI_region_views_find_item_at(const ARegion &region,
const int xy[2]);
blender::ui::AbstractViewItem *UI_region_views_find_active_item(const ARegion *region);
uiBut *UI_region_views_find_mouse_over_but(const wmWindow *win, const ARegion *region);
uiBut *UI_region_views_find_active_item_but(const ARegion *region);

View File

@@ -201,9 +201,9 @@ ui::AbstractViewItem *UI_region_views_find_active_item(const ARegion *region)
return item_but->view_item;
}
uiBut *UI_region_views_find_mouse_over_but(const wmWindow *win, const ARegion *region)
uiBut *UI_region_views_find_active_item_but(const ARegion *region)
{
return ui_view_item_find_mouse_over(region, win->eventstate->xy);
return ui_view_item_find_active(region);
}
namespace blender::ui {