Blender V5.0
interface_template_asset_shelf_popover.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10#include "BKE_report.hh"
11#include "BKE_screen.hh"
12
13#include "RNA_access.hh"
14
15#include "UI_interface_c.hh"
17#include "UI_resources.hh"
18#include "interface_intern.hh"
19
20#include "ED_asset_shelf.hh"
21
22#include "WM_api.hh"
23
24namespace blender::ui {
25
27 const bContext &C,
28 const StringRefNull asset_shelf_id,
29 const StringRef name,
30 const BIFIconID icon)
31{
32 AssetShelfType *shelf_type = ed::asset::shelf::type_find_from_idname(asset_shelf_id);
33 if (!shelf_type) {
34 RNA_warning("Asset shelf type not found: %s", asset_shelf_id.c_str());
35 return;
36 }
37
38 const ARegion *region = CTX_wm_region(&C);
39 uiBlock *block = layout.block();
40
41 uiLayout *row = &layout.row(true);
42 const bool use_big_size = !RGN_TYPE_IS_HEADER_ANY(region->regiontype);
43 const bool use_preview_icon = use_big_size;
44
45 row->context_string_set("asset_shelf_idname", asset_shelf_id);
46 if (use_big_size) {
47 row->scale_x_set(6);
48 row->scale_y_set(6);
49 }
50 else {
51 row->ui_units_x_set(name.is_empty() ? 1.6f : 7);
52 }
53
55
56 row->popover(&C, "ASSETSHELF_PT_popover_panel", name, icon);
57 uiBut *but = block->buttons.last().get();
58 if (use_preview_icon) {
60 /* Avoid small annoyance where asset shelf popover gets spawned unintentionally on mouse hover,
61 * see #132293. */
63 }
64}
65
66bool asset_shelf_popover_invoke(bContext &C, StringRef asset_shelf_idname, ReportList &reports)
67{
68 AssetShelfType *shelf_type = ed::asset::shelf::type_find_from_idname(asset_shelf_idname);
69 if (ed::asset::shelf::type_poll_for_popup(C, shelf_type) == false) {
70 return false;
71 }
72
73 PanelType *pt = WM_paneltype_find("ASSETSHELF_PT_popover_panel", true);
74 if (pt == nullptr) {
75 BKE_reportf(&reports, RPT_ERROR, "Asset shelf popover panel type not found");
76 return false;
77 }
78
79 /* Skip panel poll check here. Should usually be done, but requires passing the asset shelf type
80 * name via some context-store, but there's nothing to provide that here. Asset shelf type is
81 * polled above, so it's okay. */
82
83 std::string asset_shelf_id_str = asset_shelf_idname;
85 &C,
86 nullptr,
87 nullptr,
88 [asset_shelf_id_str](bContext *C, uiLayout *layout, void *arg_pt) {
89 layout->context_string_set("asset_shelf_idname", asset_shelf_id_str);
90 ui_item_paneltype_func(C, layout, arg_pt);
91 },
92 pt);
93
94 return true;
95}
96
97} // namespace blender::ui
98
99using namespace blender;
100
101std::optional<StringRefNull> UI_asset_shelf_idname_from_button_context(const uiBut *but)
102{
103 return UI_but_context_string_get(but, "asset_shelf_idname");
104}
ARegion * CTX_wm_region(const bContext *C)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ RPT_ERROR
Definition BKE_report.hh:39
#define RGN_TYPE_IS_HEADER_ANY(regiontype)
int BIFIconID
Definition ED_asset.hh:28
#define RNA_warning(format,...)
@ UI_BUT_ICON_PREVIEW
void UI_but_menu_disable_hover_open(uiBut *but)
std::optional< blender::StringRefNull > UI_but_context_string_get(const uiBut *but, blender::StringRef name)
const T & last(const int64_t n=0) const
constexpr const char * c_str() const
void ui_def_but_icon(uiBut *but, const int icon, const int flag)
void ui_item_paneltype_func(bContext *C, uiLayout *layout, void *arg_pt)
uiPopupBlockHandle * ui_popover_panel_create(bContext *C, ARegion *butregion, uiBut *but, uiPopoverCreateFunc popover_func, const PanelType *panel_type)
@ UI_HAS_ICON
std::optional< StringRefNull > UI_asset_shelf_idname_from_button_context(const uiBut *but)
AssetShelfType * type_find_from_idname(StringRef idname)
bool type_poll_for_popup(const bContext &C, const AssetShelfType *shelf_type)
void ensure_asset_library_fetched(const bContext &C, const AssetShelfType &shelf_type)
bool asset_shelf_popover_invoke(bContext &C, blender::StringRef asset_shelf_idname, ReportList &reports)
void template_asset_shelf_popover(uiLayout &layout, const bContext &C, StringRefNull asset_shelf_id, StringRef name, int icon)
const char * name
blender::Vector< std::unique_ptr< uiBut > > buttons
void popover(const bContext *C, PanelType *pt, std::optional< blender::StringRef > name_opt, int icon)
void scale_y_set(float scale)
uiBlock * block() const
void ui_units_x_set(float width)
void scale_x_set(float scale)
uiLayout & row(bool align)
void context_string_set(blender::StringRef name, blender::StringRef value)
PanelType * WM_paneltype_find(const StringRef idname, bool quiet)