Blender V5.3
interface_region_popover.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
27
28#include "MEM_guardedalloc.h"
29
30#include "DNA_userdef_types.h"
31
32#include "BLI_listbase.hh"
33
34#include "BLI_math_vector_c.hh"
35#include "BLI_rect.hh"
36
37#include "BKE_context.hh"
38#include "BKE_report.hh"
39#include "BKE_screen.hh"
40
41#include "ED_screen.hh"
42
43#include "WM_api.hh"
44#include "WM_types.hh"
45
47
48#include "interface_intern.hh"
49
50namespace blender::ui {
51
52/* -------------------------------------------------------------------- */
55
56struct Popover {
61
62 /* Needed for keymap removal. */
66
69
70 /* Size in pixels (ui scale applied). */
74
75#ifdef USE_UI_POPOVER_ONCE
76 bool is_once;
77#endif
78};
79
85 ARegion *region,
86 Popover *pup,
87 wm::OpCallContext opcontext)
88{
89 BLI_assert(pup->ui_size_x != 0);
90
91 const uiStyle *style = style_get_dpi();
92
93 pup->block = block_begin(C, region, __func__, EmbossType::Emboss);
94
96#ifdef USE_UI_POPOVER_ONCE
97 if (pup->is_once) {
99 }
100#endif
101
102 pup->layout = &block_layout(
103 pup->block, LayoutDirection::Vertical, LayoutType::Panel, 0, 0, pup->ui_size_x, 0, 0, style);
104
105 pup->layout->operator_context_set(opcontext);
106
107 if (pup->but) {
108 if (pup->but->context) {
109 pup->layout->context_copy(pup->but->context);
110 }
111 }
112}
113
114static Block *block_func_POPOVER(bContext *C, PopupBlockHandle *handle, void *arg_pup)
115{
116 Popover *pup = static_cast<Popover *>(arg_pup);
117
118 /* Create UI block and layout now if it wasn't done between begin/end. */
119 if (!pup->layout) {
121
122 if (pup->popover_func) {
123 pup->block->handle = handle;
124 pup->popover_func(C, pup->layout, const_cast<PanelType *>(pup->panel_type));
125 pup->block->handle = nullptr;
126 }
127
128 pup->layout = nullptr;
129 }
130
131 /* Setup and resolve UI layout for block. */
132 Block *block = pup->block;
133
134 if (pup->use_numselect) {
136 }
137
138 /* in some cases we create the block before the region,
139 * so we set it delayed here if necessary */
140 if (BLI_findindex(&handle->region->runtime->uiblocks, block) == -1) {
141 block_region_set(block, handle->region);
142 }
143
145 const ButtonMenu *popover_button = pup->but && pup->but->type == ButtonType::Popover ?
146 static_cast<ButtonMenu *>(pup->but) :
147 nullptr;
148 const int direction = (popover_button && (popover_button->popup_attach_direction ==
152 block_direction_set(block, direction);
153
154 const int block_margin = U.widget_unit / 2;
155
156 if (pup->but) {
157 /* For a header menu we set the direction automatic. */
158 block->minbounds = BLI_rctf_size_x(&pup->but->rect);
159 block_bounds_set_normal(block, block_margin);
160
161 /* If menu slides out of other menu, override direction. */
162 const bool slideout = block_is_menu(pup->but->block);
163 if (slideout) {
165 }
166
167 /* Store the button location for positioning the popover arrow hint. */
168 if (!handle->refresh) {
169 float center[2] = {BLI_rctf_cent_x(&pup->but->rect), BLI_rctf_cent_y(&pup->but->rect)};
170 block_to_window_fl(handle->ctx_region, pup->but->block, &center[0], &center[1]);
171 /* These variables aren't used for popovers,
172 * we could add new variables if there is a conflict. */
173 block->bounds_offset[0] = int(center[0]);
174 block->bounds_offset[1] = int(center[1]);
176 }
177 else {
179 }
180
181 if (!slideout) {
182 ARegion *region = CTX_wm_region(C);
183
184 if (region && region->panels.first() && (direction & UI_DIR_DOWN)) {
185 /* For regions with panels, prefer to open to top so we can
186 * see the values of the buttons below changing. */
188 }
189 /* Prefer popover from header to be positioned into the editor. */
190 else if (region) {
191 if (RGN_TYPE_IS_HEADER_ANY(region->regiontype)) {
194 }
195 }
196 }
197 }
198
199 /* Estimated a maximum size so we don't go off-screen for low height
200 * areas near the bottom of the window on refreshes. */
201 handle->max_size_y = UI_UNIT_Y * 16.0f;
202 }
203 else if (pup->panel_type &&
205 {
208 block_direction_set(block, block->direction);
210
211 const int bounds_offset[2] = {
212 int(pup->panel_type->offset_units_xy.x * UI_UNIT_X),
213 int(pup->panel_type->offset_units_xy.y * UI_UNIT_Y),
214 };
215 block_bounds_set_popup(block, block_margin, bounds_offset);
216 }
217 else {
218 /* Not attached to a button. */
219 int bounds_offset[2] = {0, 0};
222 block_direction_set(block, block->direction);
224
225 if (!handle->refresh) {
226 const Button *but = nullptr;
227 const Button *but_first = nullptr;
228 for (const Button &but_iter : block->buttons()) {
229 if ((but_first == nullptr) && button_is_editable(&but_iter)) {
230 but_first = &but_iter;
231 }
232 if (but_iter.flag & (UI_SELECT | UI_SELECT_DRAW)) {
233 but = &but_iter;
234 break;
235 }
236 }
237
238 if (but) {
239 bounds_offset[0] = -(but->rect.xmin + 0.8f * BLI_rctf_size_x(&but->rect));
240 bounds_offset[1] = -BLI_rctf_cent_y(&but->rect);
241 }
242 else {
243 bounds_offset[0] = -(pup->ui_size_x / 2);
244 bounds_offset[1] = but_first ? -BLI_rctf_cent_y(&but_first->rect) : (UI_UNIT_Y / 2);
245 }
246 copy_v2_v2_int(handle->prev_bounds_offset, bounds_offset);
247 }
248 else {
249 copy_v2_v2_int(bounds_offset, handle->prev_bounds_offset);
250 }
251
252 block_bounds_set_popup(block, block_margin, bounds_offset);
253 }
254
255 return block;
256}
257
258static void block_free_func_POPOVER(void *arg_pup)
259{
260 Popover *pup = static_cast<Popover *>(arg_pup);
261 if (pup->keymap != nullptr) {
262 wmWindow *window = pup->window;
264 }
265 MEM_delete(pup);
266}
267
269 ARegion *butregion,
270 Button *but,
271 PopoverCreateFunc popover_func,
272 const PanelType *panel_type)
273{
274 wmWindow *window = CTX_wm_window(C);
275 const uiStyle *style = style_get_dpi();
276
277 /* Create popover, buttons are created from callback. */
278 Popover *pup = MEM_new<Popover>(__func__);
279 pup->but = but;
280
281 /* FIXME: maybe one day we want non panel popovers? */
282 {
283 const int ui_units_x = (panel_type->ui_units_x == 0) ? UI_POPOVER_WIDTH_UNITS :
284 panel_type->ui_units_x;
285 /* Scale width by changes to Text Style point size. */
286 pup->ui_size_x = ui_units_x * U.widget_unit * (style->widget.points / UI_DEFAULT_TEXT_POINTS);
287 }
288
289 pup->popover_func = popover_func;
290 pup->panel_type = panel_type;
291
292#ifdef USE_UI_POPOVER_ONCE
293 {
294 /* Ideally this would be passed in. */
295 const wmEvent *event = window->runtime->eventstate;
296 pup->is_once = (event->type == LEFTMOUSE) && (event->val == KM_PRESS);
297 }
298#endif
299
300 /* Create popup block. */
302 C, butregion, but, nullptr, block_func_POPOVER, pup, block_free_func_POPOVER, true);
303
304 /* Add handlers. If attached to a button, the button will already
305 * add a modal handler and pass on events. */
306 if (!but) {
307 popup_handlers_add(C, &window->runtime->modalhandlers, handle, 0);
309 handle->popup = true;
310 }
311 handle->srna_owner = panel_type->rna_ext.srna;
312 return handle;
313}
314
316
317/* -------------------------------------------------------------------- */
320
322 bContext *C, const char *idname, bool keep_open, bool use_numselect, ReportList *reports)
323{
324 Layout *layout;
325 PanelType *pt = WM_paneltype_find(idname, true);
326 if (pt == nullptr) {
327 BKE_reportf(reports, RPT_ERROR, "Panel \"%s\" not found", idname);
328 return OPERATOR_CANCELLED;
329 }
330
331 if (pt->poll && (pt->poll(C, pt) == false)) {
332 /* cancel but allow event to pass through, just like operators do */
334 }
335
336 Block *block = nullptr;
337 if (keep_open) {
338 PopupBlockHandle *handle = popover_panel_create(C, nullptr, nullptr, item_paneltype_func, pt);
339 Popover *pup = static_cast<Popover *>(handle->popup_create_vars.arg);
340 pup->use_numselect = use_numselect;
341 block = pup->block;
342
343 /* Refresh so the block is recreated with the region visible
344 * (this also applies `use_numselect`).
345 *
346 * Without this, `block_begin` sets #BLOCK_LOOP before the region is shown,
347 * causing `template_popup_confirm` to skip attaching its close callback
348 * (since it assumes #BLOCK_LOOP menus close via `menuretval`).
349 * With #BLOCK_KEEP_OPEN set this doesn't happen, see #151778. */
351 }
352 else {
353 Popover *pup = popover_begin(C, U.widget_unit * pt->ui_units_x, false, use_numselect);
354 layout = popover_layout(pup);
355 ui::UI_paneltype_draw(C, pt, layout);
356 ui::popover_end(C, pup, nullptr);
357 block = pup->block;
358 }
359
360 if (block) {
361 PopupBlockHandle *handle = block->handle;
363 }
364 return OPERATOR_INTERFACE;
365}
366
368
369/* -------------------------------------------------------------------- */
372
373Popover *popover_begin(bContext *C, int ui_menu_width, bool from_active_button, bool use_numselect)
374{
375 Popover *pup = MEM_new<Popover>(__func__);
376 if (ui_menu_width == 0) {
377 ui_menu_width = U.widget_unit * UI_POPOVER_WIDTH_UNITS;
378 }
379 pup->ui_size_x = ui_menu_width;
380 pup->use_numselect = use_numselect;
381
382 ARegion *butregion = nullptr;
383 Button *but = nullptr;
384
385 if (from_active_button) {
386 butregion = CTX_wm_region(C);
387 but = region_active_but_get(butregion);
388 if (but == nullptr) {
389 butregion = nullptr;
390 }
391 }
392
393 pup->but = but;
394 pup->butregion = butregion;
395
396 /* Operator context default same as menus, change if needed. */
398
399 /* Create in advance so we can let buttons point to #PopupBlockHandle::retvalue
400 * (and other return values) already. */
401 pup->block->handle = MEM_new<PopupBlockHandle>(__func__);
402
403 return pup;
404}
405
406static void popover_keymap_fn(wmKeyMap * /*keymap*/, wmKeyMapItem * /*kmi*/, void *user_data)
407{
408 Popover *pup = static_cast<Popover *>(user_data);
410}
411
412void popover_end(bContext *C, Popover *pup, wmKeyMap *keymap)
413{
414 wmWindow *window = CTX_wm_window(C);
415
416 if (keymap) {
417 /* Add so we get keymaps shown in the buttons. */
419 pup->keymap = keymap;
421 &window->runtime->modalhandlers, keymap, 0);
423 }
424
425 /* Create popup block. No refresh support since the buttons were created
426 * between begin/end and we have no callback to recreate them. */
428 pup->butregion,
429 pup->but,
430 nullptr,
432 pup,
434 false);
435
436 /* Add handlers. */
437 popup_handlers_add(C, &window->runtime->modalhandlers, handle, 0);
439 handle->popup = true;
440
441 /* Re-add so it gets priority. */
442 if (keymap) {
445 }
446
447 pup->window = window;
448
449 /* TODO(@ideasman42): we may want to make this configurable.
450 * The begin/end type of calling popups doesn't allow 'can_refresh' to be set.
451 * For now close this style of popovers when accessed. */
453}
454
456{
457 return pup->layout;
458}
459
460#ifdef USE_UI_POPOVER_ONCE
461void popover_once_clear(Popover *pup)
462{
463 pup->is_once = false;
464}
465#endif
466
468
469} // namespace blender::ui
#define RGN_ALIGN_ENUM_FROM_MASK(align)
#define RGN_TYPE_IS_HEADER_ANY(regiontype)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_DEFAULT_TEXT_POINTS
#define UI_UNIT_X
#define UI_POPOVER_WIDTH_UNITS
#define UI_MENU_WIDTH_MIN
void block_direction_set(Block *block, char direction)
void block_bounds_set_normal(Block *block, int addval)
Definition interface.cc:651
static Block * block_func_POPOVER(bContext *C, PopupBlockHandle *handle, void *arg_pup)
void block_to_window_fl(const ARegion *region, const Block *block, float *x, float *y)
Definition interface.cc:144
std::function< void(bContext *, Layout *, PanelType *)> PopoverCreateFunc
void block_flag_enable(Block *block, int flag)
bool block_is_menu(const Block *block) ATTR_WARN_UNUSED_RESULT
const uiStyle * style_get_dpi()
wmOperatorStatus popover_panel_invoke(bContext *C, const char *idname, bool keep_open, bool use_numselect, ReportList *reports)
void popover_end(bContext *C, Popover *pup, wmKeyMap *keymap)
bool block_active_only_flagged_buttons(const bContext *C, ARegion *region, Block *block)
void popup_handlers_add(bContext *C, ListBaseT< wmEventHandler > *handlers, PopupBlockHandle *popup, char flag)
Block * block_begin(const bContext *C, ARegion *region, std::string name, EmbossType emboss)
void block_theme_style_set(Block *block, char theme_style)
Layout * popover_layout(Popover *pup)
void UI_paneltype_draw(bContext *C, PanelType *pt, Layout *layout)
PopupBlockHandle * popup_block_create(bContext *C, ARegion *butregion, Button *but, BlockCreateFunc create_func, BlockHandleCreateFunc handle_create_func, void *arg, FreeArgFunc arg_free, bool can_refresh)
static void popover_create_block(bContext *C, ARegion *region, Popover *pup, wm::OpCallContext opcontext)
void item_paneltype_func(bContext *C, Layout *layout, void *arg_pt)
bool button_is_editable(const Button *but) ATTR_WARN_UNUSED_RESULT
void block_flag_disable(Block *block, int flag)
static void popover_keymap_fn(wmKeyMap *, wmKeyMapItem *, void *user_data)
int2 block_layout_resolve(Block *block)
Popover * popover_begin(bContext *C, int ui_menu_width, bool from_active_button, bool use_numselect) ATTR_NONNULL(1)
void block_bounds_set_popup(Block *block, int addval, const int bounds_offset[2])
Definition interface.cc:667
static void block_free_func_POPOVER(void *arg_pup)
Button * region_active_but_get(const ARegion *region)
void block_region_set(Block *block, ARegion *region)
Layout & block_layout(Block *block, LayoutDirection direction, LayoutType type, int x, int y, int size, int em, int padding, const uiStyle *style)
PopupBlockHandle * popover_panel_create(bContext *C, ARegion *butregion, Button *but, PopoverCreateFunc popover_func, const PanelType *panel_type)
void popover_once_clear(Popover *pup)
void WM_event_set_keymap_handler_post_callback(wmEventHandler_Keymap *handler, void(keymap_tag)(wmKeyMap *keymap, wmKeyMapItem *kmi, void *user_data), void *user_data)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
wmWindow * CTX_wm_window(const bContext *C)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void ED_region_tag_refresh_ui(ARegion *region)
Definition area.cc:738
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void WM_event_remove_keymap_handler(ListBaseT< wmEventHandler > *handlers, wmKeyMap *keymap)
PanelType * WM_paneltype_find(const StringRef idname, bool quiet)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
Definition BLI_rect.hh:198
wmEventHandler_Keymap * WM_event_add_keymap_handler_priority(ListBaseT< wmEventHandler > *handlers, wmKeyMap *keymap, int)
void WM_event_add_mousemove(wmWindow *win)
ARegion * CTX_wm_region(const bContext *C)
BLI_INLINE float BLI_rctf_cent_x(const struct rctf *rct)
Definition BLI_rect.hh:194
ListBaseT< Panel > panels
bke::ARegionRuntime * runtime
eRegion_Alignment alignment
eRegion_Type regiontype
bool(*) poll(const bContext *C, PanelType *pt)
ExtensionRNA rna_ext
ListBaseT< ui::Block > uiblocks
ListBaseT< wmEventHandler > modalhandlers
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
PopupBlockHandle * handle
PopupAttachDirection popup_attach_direction
const bContextStore * context
void operator_context_set(wm::OpCallContext opcontext)
void context_copy(const bContextStore *context)
wmEventHandler_Keymap * keymap_handler
bke::WindowRuntime * runtime
uiFontStyle widget