Blender V5.3
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 Block *block = layout.block();
40
41 Layout &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 Button *but = block->buttons_ptrs.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, Layout *layout, void *arg_pt) {
89 layout->context_string_set("asset_shelf_idname", asset_shelf_id_str);
90 item_paneltype_func(C, layout, arg_pt);
91 },
92 pt);
93
94 return true;
95}
96
97std::optional<StringRefNull> asset_shelf_idname_from_button_context(const Button *but)
98{
99 return button_context_string_get(but, "asset_shelf_idname");
100}
101
102} // namespace blender::ui
#define RGN_TYPE_IS_HEADER_ANY(regiontype)
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
constexpr const char * c_str() const
constexpr bool is_empty() const
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)
void button_menu_disable_hover_open(Button *but)
std::optional< StringRefNull > button_context_string_get(const Button *but, StringRef name)
void item_paneltype_func(bContext *C, Layout *layout, void *arg_pt)
std::optional< StringRefNull > asset_shelf_idname_from_button_context(const Button *but)
void def_but_icon(Button *but, const int icon, const int64_t flag)
PopupBlockHandle * popover_panel_create(bContext *C, ARegion *butregion, Button *but, PopoverCreateFunc popover_func, const PanelType *panel_type)
void template_asset_shelf_popover(Layout &layout, const bContext &C, StringRefNull asset_shelf_id, StringRef name, int icon)
bool asset_shelf_popover_invoke(bContext &C, StringRef asset_shelf_idname, ReportList &reports)
PanelType * WM_paneltype_find(const StringRef idname, bool quiet)
int BIFIconID
Definition ED_asset.hh:30
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
ARegion * CTX_wm_region(const bContext *C)
eRegion_Type regiontype
Vector< std::unique_ptr< Button > > buttons_ptrs
void ui_units_x_set(float width)
void scale_x_set(float scale)
void context_string_set(StringRef name, StringRef value)
void scale_y_set(float scale)
Layout & row(bool align)
void popover(const bContext *C, PanelType *pt, std::optional< StringRef > name_opt, int icon)