Blender V5.3
interface_template_search_menu.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <cstdio>
13#include <cstring>
14#include <variant>
15
16#include "MEM_guardedalloc.h"
17
18#include "DNA_action_types.h"
19#include "DNA_node_types.h"
20
21#include "BLI_listbase.hh"
22#include "BLI_map.hh"
23#include "BLI_math_matrix_c.hh"
24#include "BLI_resource_scope.hh"
25#include "BLI_set.hh"
26#include "BLI_stack.hh"
27#include "BLI_string.hh"
28#include "BLI_string_utf8.hh"
29#include "BLI_utildefines.hh"
30
31#include "BLT_translation.hh"
32
33#include "BKE_context.hh"
34#include "BKE_global.hh"
35#include "BKE_screen.hh"
36
37#include "ED_screen.hh"
38
39#include "RNA_access.hh"
40#include "RNA_prototypes.hh"
41
42#include "WM_api.hh"
43#include "WM_types.hh"
44
46#include "UI_string_search.hh"
47#include "interface_intern.hh"
48
49/* For key-map item access. */
50#include "wm_event_system.hh"
51
52#include <fmt/format.h>
53
54namespace blender::ui {
55
56/* -------------------------------------------------------------------- */
59
74
82
86 int icon = 0;
87 float weight = 0.0f;
89
91 MenuType *mt = nullptr;
92
93 struct OperatorData {
99 {
100 if (this->opptr != nullptr) {
101 WM_operator_properties_free(this->opptr);
102 MEM_delete(this->opptr);
103 }
104 MEM_delete(this->context);
105 }
106 };
114 std::variant<OperatorData, PropertyData> data;
115
118};
119
132
133static bool menu_item_sort_by_drawstr_full(const MenuSearch_Item &menu_item_a,
134 const MenuSearch_Item &menu_item_b)
135{
136 return menu_item_a.drawwstr_full < menu_item_b.drawwstr_full;
137}
138
140 ResourceScope &scope,
141 MenuType *mt,
142 Button *but,
143 MenuSearch_Context *wm_context,
144 MenuSearch_Parent *menu_parent,
145 const Set<std::string> &ignored_idnames)
146{
147 MenuSearch_Item *item = nullptr;
148
149 /* Use override if the name is empty, this can happen with popovers. */
150 std::string drawstr_override;
151 const size_t sep_index = (but->flag & BUT_HAS_SEP_CHAR) ? but->drawstr.find(UI_SEP_CHAR) :
152 std::string::npos;
153 const bool drawstr_is_empty = sep_index == 0 || but->drawstr.empty();
154
155 if (but->optype != nullptr) {
156 if (ignored_idnames.contains_as(but->optype->idname)) {
157 return false;
158 }
159 if (drawstr_is_empty) {
160 drawstr_override = WM_operatortype_name(but->optype, but->opptr);
161 }
162
163 item = &scope.construct<MenuSearch_Item>();
165 auto &op_data = std::get<MenuSearch_Item::OperatorData>(item->data);
166 op_data.type = but->optype;
167 op_data.opcontext = but->opcontext;
168 op_data.context = but->context ? MEM_new<bContextStore>(__func__, *but->context) : nullptr;
169 op_data.opptr = but->opptr;
170
171 item->weight = but->search_weight;
172
173 but->opptr = nullptr;
174 }
175 else if (but->rnaprop != nullptr) {
176 const int prop_type = RNA_property_type(but->rnaprop);
177
178 if (drawstr_is_empty) {
179 if (prop_type == PROP_ENUM) {
180 const int value_enum = int(but->hardmax);
181 EnumPropertyItem enum_item;
183 static_cast<bContext *>(but->block->evil_C),
184 &but->rnapoin,
185 but->rnaprop,
186 value_enum,
187 &enum_item))
188 {
189 drawstr_override = enum_item.name;
190 }
191 else {
192 /* Should never happen. */
193 drawstr_override = "Unknown";
194 }
195 }
196 else {
197 drawstr_override = RNA_property_ui_name(but->rnaprop);
198 }
199 }
200
201 if (!ELEM(prop_type, PROP_BOOLEAN, PROP_ENUM)) {
202 /* Note that these buttons are not prevented,
203 * but aren't typically used in menus. */
204 printf("Button '%s' in menu '%s' is a menu item with unsupported RNA type %d\n",
205 but->drawstr.c_str(),
206 mt->idname,
207 prop_type);
208 }
209 else {
210 item = &scope.construct<MenuSearch_Item>();
211 item->weight = but->search_weight;
212
214 auto &rna_data = std::get<MenuSearch_Item::PropertyData>(item->data);
215 rna_data.ptr = but->rnapoin;
216 rna_data.prop = but->rnaprop;
217 rna_data.index = but->rnaindex;
218 if (prop_type == PROP_ENUM) {
219 rna_data.enum_value = int(but->hardmax);
220 }
221 }
222 }
223
224 if (item != nullptr) {
225 /* Handle shared settings. */
226 if (!drawstr_override.empty()) {
227 const StringRef drawstr_suffix = sep_index == std::string::npos ?
228 "" :
229 StringRef(but->drawstr).drop_prefix(sep_index);
230 std::string drawstr = std::string("(") + drawstr_override + ")" + drawstr_suffix;
231 item->drawstr = scope.allocator().copy_string(drawstr);
232 }
233 else {
234 item->drawstr = scope.allocator().copy_string(but->drawstr);
235 }
236
237 item->icon = button_icon(but);
239 item->mt = mt;
240
241 item->wm_context = wm_context;
242 item->menu_parent = menu_parent;
243
244 data->items.append(*item);
245 return true;
246 }
247
248 return false;
249}
250
255{
256 bool changed = false;
257 if (auto *op_data = std::get_if<MenuSearch_Item::OperatorData>(&item->data)) {
258 but->optype = op_data->type;
259 but->opcontext = op_data->opcontext;
260 but->context = op_data->context;
261 but->opptr = op_data->opptr;
262 changed = true;
263 }
264 else if (auto *rna_data = std::get_if<MenuSearch_Item::PropertyData>(&item->data)) {
265 const int prop_type = RNA_property_type(rna_data->prop);
266
267 but->rnapoin = rna_data->ptr;
268 but->rnaprop = rna_data->prop;
269 but->rnaindex = rna_data->index;
270
271 if (prop_type == PROP_ENUM) {
272 but->hardmax = rna_data->enum_value;
273 }
274 changed = true;
275 }
276
277 if (changed) {
278 but->drawstr = item->drawstr;
279 const size_t sep_index = but->drawstr.find(UI_SEP_CHAR);
280
281 if (sep_index != std::string::npos) {
282 but->drawstr.resize(sep_index);
283 }
284
285 but->icon = item->icon;
286 }
287
288 return changed;
289}
290
292 MenuType *mt = nullptr;
296 std::optional<bContextStore> context;
297};
298
303 wmWindow *win,
304 ScrArea *area,
305 ARegion *region,
306 Stack<MenuStackEntry> &menu_stack,
308 Set<MenuType *> &menu_tagged)
309{
311 ListBaseT<wmEventHandler> *handlers[] = {
312 region ? &region->runtime->handlers : nullptr,
313 area ? &area->handlers : nullptr,
314 &win->runtime->handlers,
315 };
316
317 for (int handler_index = 0; handler_index < ARRAY_SIZE(handlers); handler_index++) {
318 if (handlers[handler_index] == nullptr) {
319 continue;
320 }
321 for (wmEventHandler &handler_base : *handlers[handler_index]) {
322 /* During this loop, UI handlers for nested menus can tag multiple handlers free. */
323 if (handler_base.flag & WM_HANDLER_DO_FREE) {
324 continue;
325 }
326 if (handler_base.type != WM_HANDLER_TYPE_KEYMAP) {
327 continue;
328 }
329
330 if (handler_base.poll == nullptr ||
331 handler_base.poll(win, area, region, win->runtime->eventstate))
332 {
333 wmEventHandler_Keymap *handler = reinterpret_cast<wmEventHandler_Keymap *>(&handler_base);
335 WM_event_get_keymaps_from_handler(wm, win, handler, &km_result);
336 for (int km_index = 0; km_index < km_result.keymaps_len; km_index++) {
337 wmKeyMap *keymap = km_result.keymaps[km_index];
338 if (keymap && WM_keymap_poll(C, keymap)) {
339 for (wmKeyMapItem &kmi : keymap->items) {
340 if (kmi.flag & KMI_INACTIVE) {
341 continue;
342 }
343 if (STR_ELEM(kmi.idname, "WM_OT_call_menu", "WM_OT_call_menu_pie")) {
344 char menu_idname[MAX_NAME];
345 RNA_string_get(kmi.ptr, "name", menu_idname);
346 MenuType *mt = WM_menutype_find(menu_idname, false);
347
348 if (mt && menu_tagged.add(mt)) {
349 /* Unlikely, but possible this will be included twice. */
350 menu_stack.push({mt});
351 menu_to_kmi.add(mt, &kmi);
352 }
353 }
354 }
355 }
356 }
357 }
358 }
359 }
360}
361
366{
367 /* Add to temporary list so we can sort them separately. */
369
370 ResourceScope &scope = data->scope;
372 if ((ot->flag & OPTYPE_INTERNAL) && (G.debug & G_DEBUG_WM) == 0) {
373 continue;
374 }
375
376 if (WM_operator_poll(C, ot)) {
377 const char *ot_ui_name = CTX_IFACE_(ot->translation_context, ot->name);
378
381 auto &op_data = std::get<MenuSearch_Item::OperatorData>(item.data);
382 op_data.type = ot;
383 op_data.opcontext = wm::OpCallContext::InvokeDefault;
384 op_data.context = nullptr;
385 op_data.opptr = MEM_new<PointerRNA>(__func__, WM_operator_properties_create_ptr(ot));
386
387 char idname_as_py[OP_MAX_TYPENAME];
388 char uiname[256];
389 WM_operator_py_idname(idname_as_py, ot->idname);
390
391 SNPRINTF_UTF8(uiname, "%s " UI_MENU_ARROW_SEP " %s", idname_as_py, ot_ui_name);
392
393 item.drawwstr_full = scope.allocator().copy_string(uiname);
394 item.drawstr = ot_ui_name;
395
396 item.wm_context = nullptr;
397
398 operator_items.append(item);
399 }
400 }
401
402 std::ranges::sort(operator_items, menu_item_sort_by_drawstr_full);
403
404 data->items.extend(operator_items);
405}
406
414 wmWindow *win,
415 ScrArea *area_init,
416 ARegion *region_init,
417 bool include_all_areas,
418 const char *single_menu_idname)
419{
420 Map<MenuType *, const char *> menu_display_name_map;
421 const uiStyle *style = style_get_dpi();
422
423 const bContextStore *old_context_store = CTX_store_get(C);
424 BLI_SCOPED_DEFER([&]() { CTX_store_set(C, old_context_store); });
425 bContextStore context_store;
426 if (old_context_store) {
427 context_store = *old_context_store;
428 }
429 context_store.entries.append({"is_menu_search", true});
430 CTX_store_set(C, &context_store);
431
432 /* Convert into non-ui structure. */
433 MenuSearch_Data *data = MEM_new<MenuSearch_Data>(__func__);
434 ResourceScope &scope = data->scope;
435
436 fmt::memory_buffer str_buf;
437
438 /* Use a stack of menus to handle and discover new menus in passes. */
439 Stack<MenuStackEntry> menu_stack;
440
441 /* Tag menu types not to add, either because they have already been added
442 * or they have been blacklisted. */
443 Set<MenuType *> menu_tagged;
445
446 /* Avoid showing the search operator in the menu search itself. */
447 static const Set<std::string> ignored_operator_idnames = {
448 "WM_OT_search_single_menu",
449 };
450
451 /* Blacklist menus we don't want to show. */
452 {
453 const char *idname_array[] = {
454 /* While we could include this, it's just showing filenames to load. */
455 (single_menu_idname && STREQ(single_menu_idname, "TOPBAR_MT_file_open_recent")) ?
456 nullptr :
457 "TOPBAR_MT_file_open_recent",
458 /* Showing undo history is not helpful since users may accidentally undo
459 * an action they intend to run. */
460 "TOPBAR_MT_undo_history",
461 };
462 for (int i = 0; i < ARRAY_SIZE(idname_array); i++) {
463 if (!idname_array[i]) {
464 continue;
465 }
466 MenuType *mt = WM_menutype_find(idname_array[i], false);
467 if (mt != nullptr) {
468 menu_tagged.add(mt);
469 }
470 }
471 }
472
473 if (!single_menu_idname) {
474 /* Exclude context menus (when not searching in a specific single menu) because:
475 * - The menu items are available elsewhere (and will show up multiple times).
476 * - Menu items depend on exact context, making search results unpredictable
477 * (exact number of items selected for example). See design doc #74158.
478 * There is one exception,
479 * as the outliner only exposes functionality via the context menu. */
481 if (BLI_str_endswith(mt->idname, "_context_menu")) {
482 menu_tagged.add(mt);
483 }
484 }
485 const char *idname_array[] = {
486 /* Add back some context menus. */
487 "OUTLINER_MT_context_menu",
488 };
489 for (int i = 0; i < ARRAY_SIZE(idname_array); i++) {
490 MenuType *mt = WM_menutype_find(idname_array[i], false);
491 if (mt != nullptr) {
492 menu_tagged.remove(mt);
493 }
494 }
495 }
496
497 /* Collect contexts, one for each 'ui_type'. */
498 MenuSearch_Context *wm_contexts = nullptr;
499
500 const EnumPropertyItem *space_type_ui_items = nullptr;
501 int space_type_ui_items_len = 0;
502 bool space_type_ui_items_free = false;
503
504 /* Text used as prefix for top-bar menu items. */
505 const char *global_menu_prefix = nullptr;
506
507 if (include_all_areas) {
509
510 /* First create arrays for ui_type. */
511 PropertyRNA *prop_ui_type = nullptr;
512 {
513 /* This must be a valid pointer, with only it's type checked. */
514 ScrArea area_dummy{};
515 /* Anything besides #SPACE_EMPTY is fine,
516 * as this value is only included in the enum when set. */
517 area_dummy.spacetype = SPACE_TOPBAR;
518 PointerRNA ptr = RNA_pointer_create_discrete(&screen->id, RNA_Area, &area_dummy);
519 prop_ui_type = RNA_struct_find_property(&ptr, "ui_type");
521 &ptr,
522 prop_ui_type,
523 &space_type_ui_items,
524 &space_type_ui_items_len,
525 &space_type_ui_items_free);
526
527 wm_contexts = scope.construct<Array<MenuSearch_Context>>(space_type_ui_items_len).data();
528 for (int i = 0; i < space_type_ui_items_len; i++) {
529 wm_contexts[i].space_type_ui_index = -1;
530 }
531 }
532
533 for (ScrArea &area : screen->areabase) {
535 if (region != nullptr) {
536 PointerRNA ptr = RNA_pointer_create_discrete(&screen->id, RNA_Area, &area);
537 const int space_type_ui = RNA_property_enum_get(&ptr, prop_ui_type);
538
539 const int space_type_ui_index = RNA_enum_from_value(space_type_ui_items, space_type_ui);
540 if (space_type_ui_index == -1) {
541 continue;
542 }
543
544 if (wm_contexts[space_type_ui_index].space_type_ui_index != -1) {
545 ScrArea *area_best = wm_contexts[space_type_ui_index].area;
546 const uint value_best = uint(area_best->winx) * uint(area_best->winy);
547 const uint value_test = uint(area.winx) * uint(area.winy);
548 if (value_best > value_test) {
549 continue;
550 }
551 }
552
553 wm_contexts[space_type_ui_index].space_type_ui_index = space_type_ui_index;
554 wm_contexts[space_type_ui_index].area = &area;
555 wm_contexts[space_type_ui_index].region = region;
556 }
557 }
558
559 global_menu_prefix = CTX_IFACE_(RNA_property_translation_context(prop_ui_type), "Top Bar");
560 }
561
562 for (int space_type_ui_index = -1; space_type_ui_index < space_type_ui_items_len;
563 space_type_ui_index += 1)
564 {
565
566 ScrArea *area = nullptr;
567 ARegion *region = nullptr;
568 MenuSearch_Context *wm_context = nullptr;
569
570 if (include_all_areas) {
571 if (space_type_ui_index == -1) {
572 /* First run without any context, to populate the top-bar without. */
573 wm_context = nullptr;
574 area = nullptr;
575 region = nullptr;
576 }
577 else {
578 wm_context = &wm_contexts[space_type_ui_index];
579 if (wm_context->space_type_ui_index == -1) {
580 continue;
581 }
582
583 area = wm_context->area;
584 region = wm_context->region;
585
586 CTX_wm_area_set(C, area);
587 CTX_wm_region_set(C, region);
588 }
589 }
590 else {
591 area = area_init;
592 region = region_init;
593 }
594
595 if (single_menu_idname) {
596 if (MenuType *mt = WM_menutype_find(single_menu_idname, false)) {
597 if (menu_tagged.add(mt)) {
598 menu_stack.push({mt});
599 }
600 }
601 }
602 else {
603 /* Populate menus from the editors,
604 * note that we could create a fake header, draw the header and extract the menus
605 * from the buttons, however this is quite involved and can be avoided as by convention
606 * each space-type has a single root-menu that headers use. */
607 const char *idname_array[2] = {nullptr};
608 int idname_array_len = 0;
609
610 /* Use negative for global (no area) context, populate the top-bar. */
611 if (space_type_ui_index == -1) {
612 idname_array[idname_array_len++] = "TOPBAR_MT_editor_menus";
613 }
614
615#define SPACE_MENU_MAP(space_type, menu_id) \
616 case space_type: \
617 idname_array[idname_array_len++] = menu_id; \
618 break
619#define SPACE_MENU_NOP(space_type) \
620 case space_type: \
621 break
622
623 if (area != nullptr) {
624 SpaceLink *sl = area->spacedata.first_as<SpaceLink>();
625 switch (eSpace_Type(area->spacetype)) {
626 SPACE_MENU_MAP(SPACE_VIEW3D, "VIEW3D_MT_editor_menus");
627 SPACE_MENU_MAP(SPACE_GRAPH, "GRAPH_MT_editor_menus");
628 SPACE_MENU_MAP(SPACE_OUTLINER, "OUTLINER_MT_editor_menus");
630 SPACE_MENU_MAP(SPACE_FILE, "FILEBROWSER_MT_editor_menus");
631 SPACE_MENU_MAP(SPACE_IMAGE, "IMAGE_MT_editor_menus");
632 SPACE_MENU_MAP(SPACE_INFO, "INFO_MT_editor_menus");
633 SPACE_MENU_MAP(SPACE_SEQ, "SEQUENCER_MT_editor_menus");
634 SPACE_MENU_MAP(SPACE_TEXT, "TEXT_MT_editor_menus");
635 SPACE_MENU_MAP(SPACE_ACTION, "DOPESHEET_MT_editor_menus");
636 SPACE_MENU_MAP(SPACE_NLA, "NLA_MT_editor_menus");
637 SPACE_MENU_MAP(SPACE_NODE, "NODE_MT_editor_menus");
638 SPACE_MENU_MAP(SPACE_CONSOLE, "CONSOLE_MT_editor_menus");
639 SPACE_MENU_MAP(SPACE_USERPREF, "USERPREF_MT_editor_menus");
641 (((const SpaceClip *)sl)->mode == SC_MODE_TRACKING) ?
642 "CLIP_MT_tracking_editor_menus" :
643 "CLIP_MT_masking_editor_menus");
650 }
651 }
652 for (int i = 0; i < idname_array_len; i++) {
653 MenuType *mt = WM_menutype_find(idname_array[i], false);
654 if (mt != nullptr) {
655 /* Check if this exists because of 'include_all_areas'. */
656 if (menu_tagged.add(mt)) {
657 menu_stack.push({mt});
658 }
659 }
660 }
661 }
662#undef SPACE_MENU_MAP
663#undef SPACE_MENU_NOP
664
665 bool has_keymap_menu_items = false;
666
667 while (!menu_stack.is_empty()) {
668 MenuStackEntry current_menu = menu_stack.pop();
669 MenuType *mt = current_menu.mt;
670 if (!WM_menutype_poll(C, mt)) {
671 continue;
672 }
673
674 Block *block = block_begin(C, region, __func__, EmbossType::Emboss);
675 Layout &layout = block_layout(block,
678 0,
679 0,
680 200,
681 0,
683 style);
684
686
687 if (current_menu.context.has_value()) {
688 layout.context_copy(&*current_menu.context);
689 }
691 menutype_draw(C, mt, &layout);
692
693 block_end(C, block);
694
695 for (const int i : block->buttons_ptrs.index_range()) {
696 const std::unique_ptr<Button> &but = block->buttons_ptrs[i];
697 MenuType *mt_from_but = nullptr;
698 /* Support menu titles with dynamic from initial labels
699 * (used by edit-mesh context menu). */
700 if (but->type == ButtonType::Label) {
701
702 /* Check if the label is the title. */
703 const std::unique_ptr<Button> *but_test = block->buttons_ptrs.begin() + i - 1;
704 while (but_test >= block->buttons_ptrs.begin() && (*but_test)->type == ButtonType::Sepr)
705 {
706 but_test--;
707 }
708
709 if (but_test < block->buttons_ptrs.begin()) {
710 menu_display_name_map.add(mt, scope.allocator().copy_string(but->drawstr).c_str());
711 }
712 }
714 scope,
715 mt,
716 but.get(),
717 wm_context,
718 current_menu.self_as_parent,
719 ignored_operator_idnames))
720 {
721 /* pass */
722 }
723 else if ((mt_from_but = button_menutype_get(but.get()))) {
724 const bool uses_context = but->context &&
725 flag_is_set(mt_from_but->flag, MenuTypeFlag::ContextDependent);
726 const bool tagged_first_time = menu_tagged.add(mt_from_but);
727 const bool scan_submenu = tagged_first_time || uses_context;
728
729 if (scan_submenu) {
730 MenuSearch_Parent *menu_parent = &scope.construct<MenuSearch_Parent>();
731 /* Use brackets for menu key shortcuts,
732 * converting "Text|Some-Shortcut" to "Text (Some-Shortcut)".
733 * This is needed so we don't right align sub-menu contents
734 * we only want to do that for the last menu item, not the path that leads to it.
735 */
736 const char *drawstr_sep = but->flag & BUT_HAS_SEP_CHAR ?
737 strrchr(but->drawstr.c_str(), UI_SEP_CHAR) :
738 nullptr;
739 bool drawstr_is_empty = false;
740 if (drawstr_sep != nullptr) {
741 BLI_assert(str_buf.size() == 0);
742 /* Detect empty string, fall back to menu name. */
743 const char *drawstr = but->drawstr.c_str();
744 int drawstr_len = drawstr_sep - but->drawstr.c_str();
745 if (drawstr_len == 0) [[unlikely]] {
746 drawstr = CTX_IFACE_(mt_from_but->translation_context, mt_from_but->label);
747 drawstr_len = strlen(drawstr);
748 if (drawstr[0] == '\0') {
749 drawstr_is_empty = true;
750 }
751 }
752 str_buf.append(StringRef(drawstr, drawstr_len));
753 fmt::format_to(fmt::appender(str_buf), " ({})", drawstr_sep + 1);
754 menu_parent->drawstr = scope.allocator().copy_string(
755 StringRef(str_buf.data(), str_buf.size()));
756 str_buf.clear();
757 }
758 else {
759 const char *drawstr = but->drawstr.c_str();
760 if (drawstr[0] == '\0') [[unlikely]] {
761 drawstr = CTX_IFACE_(mt_from_but->translation_context, mt_from_but->label);
762 if (drawstr[0] == '\0') {
763 drawstr_is_empty = true;
764 }
765 }
766 menu_parent->drawstr = scope.allocator().copy_string(drawstr);
767 }
768 menu_parent->parent = current_menu.self_as_parent;
769
770 if (drawstr_is_empty) {
771 printf("Warning: '%s' menu has empty 'bl_label'.\n", mt_from_but->idname);
772 }
773
774 if (uses_context) {
775 menu_stack.push({mt_from_but, menu_parent, *but->context});
776 }
777 else {
778 menu_stack.push({mt_from_but, menu_parent});
779 }
780 }
781 }
782 else if (but->menu_create_func != nullptr) {
783 /* A non 'MenuType' menu button. */
784
785 /* +1 to avoid overlap with the current 'block'. */
786 Block *sub_block = block_begin(C, region, __func__ + 1, EmbossType::Emboss);
787 Layout &sub_layout = block_layout(sub_block,
790 0,
791 0,
792 200,
793 0,
795 style);
796
798
800
801 /* If this is a panel, check it's poll function succeeds before drawing.
802 * otherwise draw(..) may be called in an unsupported context and crash, see: #130744.
803 *
804 * NOTE(@ideasman42): it would be good if the buttons #BUT_DISABLED flag
805 * could be used as a more general way to know if poll succeeded,
806 * at this point it's not set - this could be further investigated. */
807 bool poll_success = true;
808 if (PanelType *pt = button_paneltype_get(but.get())) {
809 if (pt->poll && (pt->poll(C, pt) == false)) {
810 poll_success = false;
811 }
812 }
813
814 if (poll_success) {
815 but->menu_create_func(C, &sub_layout, but->poin);
816 }
817
818 block_end(C, sub_block);
819
820 if (poll_success) {
821 MenuSearch_Parent *menu_parent = &scope.construct<MenuSearch_Parent>();
822 menu_parent->drawstr = scope.allocator().copy_string(but->drawstr);
823 menu_parent->parent = current_menu.self_as_parent;
824
825 for (Button &sub_but : sub_block->buttons()) {
827 data, scope, mt, &sub_but, wm_context, menu_parent, ignored_operator_idnames);
828 }
829 }
830
831 if (region) {
832 region->runtime->block_name_map.remove(sub_block->name);
833 BLI_remlink(&region->runtime->uiblocks, sub_block);
834 }
835 block_free(nullptr, sub_block);
836 }
837 }
838 if (region) {
839 region->runtime->block_name_map.remove(block->name);
840 BLI_remlink(&region->runtime->uiblocks, block);
841 }
842 block_free(nullptr, block);
843
844 if (single_menu_idname == nullptr) {
845 /* Add key-map items as a second pass, so all menus are accessed from the header & top-bar
846 * before key shortcuts are expanded. */
847 if (menu_stack.is_empty() && (has_keymap_menu_items == false)) {
848 has_keymap_menu_items = true;
850 C, win, area, region, menu_stack, menu_to_kmi, menu_tagged);
851 }
852 }
853 }
854 }
855
856 /* NOTE: currently this builds the full path for each menu item,
857 * that could be moved into the parent menu. */
858
859 /* Set names as full paths. */
860 for (MenuSearch_Item &item : data->items) {
861 BLI_assert(str_buf.size() == 0);
862
863 if (include_all_areas) {
864 fmt::format_to(fmt::appender(str_buf),
865 "{}: ",
866 (item.wm_context != nullptr) ?
867 space_type_ui_items[item.wm_context->space_type_ui_index].name :
868 global_menu_prefix);
869 }
870
871 if (item.menu_parent != nullptr) {
872 MenuSearch_Parent *menu_parent = item.menu_parent;
873 menu_parent->temp_child = nullptr;
874 while (menu_parent && menu_parent->parent) {
875 menu_parent->parent->temp_child = menu_parent;
876 menu_parent = menu_parent->parent;
877 }
878 while (menu_parent) {
879 str_buf.append(menu_parent->drawstr);
880 str_buf.append(StringRef(" " UI_MENU_ARROW_SEP " "));
881 menu_parent = menu_parent->temp_child;
882 }
883 }
884 else {
885 const char *drawstr = menu_display_name_map.lookup_default(item.mt, nullptr);
886 if (drawstr == nullptr) {
887 drawstr = CTX_IFACE_(item.mt->translation_context, item.mt->label);
888 }
889 str_buf.append(StringRef(drawstr));
890
891 wmKeyMapItem *kmi = menu_to_kmi.lookup_default(item.mt, nullptr);
892 if (kmi != nullptr) {
893 std::string kmi_str = WM_keymap_item_to_string(kmi, false).value_or("");
894 fmt::format_to(fmt::appender(str_buf), " ({})", kmi_str);
895 }
896
897 str_buf.append(StringRef(" " UI_MENU_ARROW_SEP " "));
898 }
899
900 str_buf.append(item.drawstr);
901
902 item.drawwstr_full = scope.allocator().copy_string(StringRef(str_buf.data(), str_buf.size()));
903 str_buf.clear();
904 }
905
906 /* Finally sort menu items.
907 *
908 * NOTE: we might want to keep the in-menu order, for now sort all. */
909 std::ranges::sort(data->items, menu_item_sort_by_drawstr_full);
910
911 if (include_all_areas) {
912 CTX_wm_area_set(C, area_init);
913 CTX_wm_region_set(C, region_init);
914
915 if (space_type_ui_items_free) {
916 MEM_delete(space_type_ui_items);
917 }
918 }
919
920 /* Include all operators for developers,
921 * since it can be handy to have a quick way to access any operator,
922 * including operators being developed which haven't yet been added into the interface.
923 *
924 * These are added after all menu items so developers still get normal behavior by default,
925 * unless searching for something that isn't already in a menu (or scroll down).
926 *
927 * Keep this behind a developer only check:
928 * - Many operators need options to be set to give useful results, see: #74157.
929 * - User who really prefer to list all operators can use #WM_OT_search_operator.
930 */
931 if ((U.flag & USER_DEVELOPER_UI) && single_menu_idname == nullptr) {
933 }
934
935 return data;
936}
937
938static void menu_search_arg_free_fn(void *data_v)
939{
940 MEM_delete(static_cast<MenuSearch_Data *>(data_v));
941}
942
943static void menu_search_exec_fn(bContext *C, void * /*arg1*/, void *arg2)
944{
945 MenuSearch_Item *item = static_cast<MenuSearch_Item *>(arg2);
946 if (item == nullptr) {
947 return;
948 }
949 if (item->state & BUT_DISABLED) {
950 return;
951 }
952
953 ScrArea *area_prev = CTX_wm_area(C);
954 ARegion *region_prev = CTX_wm_region(C);
955
956 if (item->wm_context != nullptr) {
959 }
960
961 if (auto *op_data = std::get_if<MenuSearch_Item::OperatorData>(&item->data)) {
962 CTX_store_set(C, op_data->context);
964 C, op_data->type, op_data->opcontext, op_data->opptr, nullptr, item->drawstr);
965 CTX_store_set(C, nullptr);
966 }
967 else if (auto *rna_data = std::get_if<MenuSearch_Item::PropertyData>(&item->data)) {
968 PointerRNA *ptr = &rna_data->ptr;
969 PropertyRNA *prop = rna_data->prop;
970 const int index = rna_data->index;
971 const int prop_type = RNA_property_type(prop);
972 bool changed = false;
973
974 if (prop_type == PROP_BOOLEAN) {
975 const bool is_array = RNA_property_array_check(prop);
976 if (is_array) {
977 const bool value = RNA_property_boolean_get_index(ptr, prop, index);
978 RNA_property_boolean_set_index(ptr, prop, index, !value);
979 }
980 else {
981 const bool value = RNA_property_boolean_get(ptr, prop);
982 RNA_property_boolean_set(ptr, prop, !value);
983 }
984 changed = true;
985 }
986 else if (prop_type == PROP_ENUM) {
987 RNA_property_enum_set(ptr, prop, rna_data->enum_value);
988 changed = true;
989 }
990
991 if (changed) {
992 RNA_property_update(C, ptr, prop);
993 }
994 }
995
996 if (item->wm_context != nullptr) {
997 CTX_wm_area_set(C, area_prev);
998 CTX_wm_region_set(C, region_prev);
999 }
1000}
1001
1002static void menu_search_update_fn(const bContext * /*C*/,
1003 void *arg,
1004 const char *str,
1005 SearchItems *items,
1006 const bool /*is_first*/)
1007{
1008 MenuSearch_Data *data = static_cast<MenuSearch_Data *>(arg);
1009
1011
1012 for (MenuSearch_Item &item : data->items) {
1013 search.add(item.drawwstr_full, &item, item.weight);
1014 }
1015
1016 const Vector<MenuSearch_Item *> filtered_items = search.query(str);
1017
1018 for (MenuSearch_Item *item : filtered_items) {
1019 if (!search_item_add(items, item->drawwstr_full, item, item->icon, item->state, 0)) {
1020 break;
1021 }
1022 }
1023}
1024
1026
1027/* -------------------------------------------------------------------- */
1034
1036 void *arg,
1037 void *active,
1038 const wmEvent *event)
1039{
1040 MenuSearch_Data *data = static_cast<MenuSearch_Data *>(arg);
1041 MenuSearch_Item *item = static_cast<MenuSearch_Item *>(active);
1042 bool has_menu = false;
1043
1044 new (&data->context_menu_data.but) Button();
1045 new (&data->context_menu_data.block) Block();
1046 Button *but = &data->context_menu_data.but;
1047 Block *block = &data->context_menu_data.block;
1048
1049 but->block = block;
1050
1051 if (menu_items_to_ui_button(item, but)) {
1052 ScrArea *area_prev = CTX_wm_area(C);
1053 ARegion *region_prev = CTX_wm_region(C);
1054
1055 if (item->wm_context != nullptr) {
1058 }
1059
1060 if (popup_context_menu_for_button(C, but, event)) {
1061 has_menu = true;
1062 }
1063
1064 if (item->wm_context != nullptr) {
1065 CTX_wm_area_set(C, area_prev);
1066 CTX_wm_region_set(C, region_prev);
1067 }
1068 }
1069
1070 return has_menu;
1071}
1072
1074
1075/* -------------------------------------------------------------------- */
1078
1080 bContext *C, ARegion *region, const rcti * /*item_rect*/, void *arg, void *active)
1081{
1082 MenuSearch_Data *data = static_cast<MenuSearch_Data *>(arg);
1083 MenuSearch_Item *item = static_cast<MenuSearch_Item *>(active);
1084
1085 new (&data->context_menu_data.but) Button();
1086 new (&data->context_menu_data.block) Block();
1087 Button *but = &data->context_menu_data.but;
1088 Block *block = &data->context_menu_data.block;
1089 unit_m4(block->winmat);
1090 block->aspect = 1;
1091
1092 but->block = block;
1093
1094 /* Place the fake button at the cursor so the tool-tip is places properly. */
1095 float tip_init[2];
1096 const wmEvent *event = CTX_wm_window(C)->runtime->eventstate;
1097 tip_init[0] = event->xy[0];
1098 tip_init[1] = event->xy[1] - (UI_UNIT_Y / 2);
1099 window_to_block_fl(region, block, &tip_init[0], &tip_init[1]);
1100
1101 but->rect.xmin = tip_init[0];
1102 but->rect.xmax = tip_init[0];
1103 but->rect.ymin = tip_init[1];
1104 but->rect.ymax = tip_init[1];
1105
1106 if (menu_items_to_ui_button(item, but)) {
1107 ScrArea *area_prev = CTX_wm_area(C);
1108 ARegion *region_prev = CTX_wm_region(C);
1109
1110 if (item->wm_context != nullptr) {
1113 }
1114
1115 ARegion *region_tip = tooltip_create_from_button(C, region, but, false);
1116
1117 if (item->wm_context != nullptr) {
1118 CTX_wm_area_set(C, area_prev);
1119 CTX_wm_region_set(C, region_prev);
1120 }
1121 return region_tip;
1122 }
1123
1124 return nullptr;
1125}
1126
1128
1129/* -------------------------------------------------------------------- */
1132
1133void button_func_menu_search(Button *but, const char *single_menu_idname)
1134{
1135 bContext *C = static_cast<bContext *>(but->block->evil_C);
1136 wmWindow *win = CTX_wm_window(C);
1137 ScrArea *area = CTX_wm_area(C);
1138 ARegion *region = CTX_wm_region(C);
1139 /* When run from top-bar scan all areas in the current window. */
1140 const bool include_all_areas = (area && (area->spacetype == SPACE_TOPBAR)) &&
1141 !single_menu_idname;
1143 C, win, area, region, include_all_areas, single_menu_idname);
1145 /* Generic callback. */
1148 data,
1149 false,
1152 nullptr);
1153
1157}
1158
1160{
1161 Block *block;
1162 Button *but;
1163 static char search[256] = "";
1164
1165 block = layout->block();
1166 block_layout_set_current(block, layout);
1167
1168 but = uiDefSearchBut(
1169 block, search, ICON_VIEWZOOM, sizeof(search), 0, 0, UI_UNIT_X * 6, UI_UNIT_Y, "");
1171}
1172
1174
1175} // namespace blender::ui
#define BLI_SCOPED_DEFER(function_to_defer)
#define STR_ELEM(...)
#define SNPRINTF_UTF8(dst, format,...)
#define CTX_IFACE_(context, msgid)
#define MAX_NAME
Definition DNA_defs.h:63
#define OP_MAX_TYPENAME
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_SEP_CHAR
#define UI_UNIT_X
#define ELEM(...)
#define STREQ(a, b)
StringRefNull copy_string(StringRef str)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
T & construct(Args &&...args)
LinearAllocator & allocator()
bool contains_as(const ForwardKey &key) const
Definition BLI_set.hh:314
bool add(const Key &key)
Definition BLI_set.hh:248
bool remove(const Key &key)
Definition BLI_set.hh:404
bool is_empty() const
Definition BLI_stack.hh:310
void push(const T &value)
Definition BLI_stack.hh:215
constexpr const char * c_str() const
constexpr StringRef drop_prefix(int64_t n) const
constexpr StringRef()
void append(const T &value)
void add(const StringRef str, T *user_data, const int weight=0)
Vector< T * > query(const StringRef query) const
#define str(s)
#define UI_MENU_PADDING
#define SPACE_MENU_NOP(space_type)
#define SPACE_MENU_MAP(space_type, menu_id)
int button_icon(const Button *but)
ARegion * searchbox_create_menu(bContext *C, ARegion *butregion, ButtonSearch *search_but)
static bool menu_item_sort_by_drawstr_full(const MenuSearch_Item &menu_item_a, const MenuSearch_Item &menu_item_b)
void button_func_search_set_sep_string(Button *but, const char *search_sep_string)
static bool menu_items_to_ui_button(MenuSearch_Item *item, Button *but)
void block_free(const bContext *C, Block *block)
void button_func_menu_search(Button *but, const char *single_menu_idname=nullptr)
void block_flag_enable(Block *block, int flag)
const uiStyle * style_get_dpi()
PanelType * button_paneltype_get(const Button *but)
static void menu_search_exec_fn(bContext *C, void *, void *arg2)
static MenuSearch_Data * menu_items_from_ui_create(bContext *C, wmWindow *win, ScrArea *area_init, ARegion *region_init, bool include_all_areas, const char *single_menu_idname)
Block * block_begin(const bContext *C, ARegion *region, std::string name, EmbossType emboss)
static void menu_items_from_all_operators(bContext *C, MenuSearch_Data *data)
Button * uiDefSearchBut(Block *block, void *arg, int icon, int maxncpy, int x, int y, short width, short height, std::optional< StringRef > tip)
static bool search_menu_create_context_menu(bContext *C, void *arg, void *active, const wmEvent *event)
bool search_item_add(SearchItems *items, StringRef name, void *poin, int iconid, int64_t but_flag, uint8_t name_prefix_offset)
void block_end(const bContext *C, Block *block, bool postpone_callbacks=false)
static ARegion * search_menu_create_tooltip(bContext *C, ARegion *region, const rcti *, void *arg, void *active)
void menutype_draw(bContext *C, MenuType *mt, Layout *layout)
static void menu_search_arg_free_fn(void *data_v)
void uiTemplateMenuSearch(Layout *layout)
bool popup_context_menu_for_button(bContext *C, Button *but, const wmEvent *event)
static void menu_search_update_fn(const bContext *, void *arg, const char *str, SearchItems *items, const bool)
MenuType * button_menutype_get(const Button *but)
void window_to_block_fl(const ARegion *region, const Block *block, float *x, float *y)
Definition interface.cc:193
Layout & block_layout(Block *block, LayoutDirection direction, LayoutType type, int x, int y, int size, int em, int padding, const uiStyle *style)
void block_layout_set_current(Block *block, Layout *layout)
ARegion * tooltip_create_from_button(bContext *C, ARegion *butregion, Button *but, bool is_quick_tip)
static bool menu_items_from_ui_create_item_from_button(MenuSearch_Data *data, ResourceScope &scope, MenuType *mt, Button *but, MenuSearch_Context *wm_context, MenuSearch_Parent *menu_parent, const Set< std::string > &ignored_idnames)
static void menu_types_add_from_keymap_items(bContext *C, wmWindow *win, ScrArea *area, ARegion *region, Stack< MenuStackEntry > &menu_stack, Map< MenuType *, wmKeyMapItem * > &menu_to_kmi, Set< MenuType * > &menu_tagged)
void button_func_search_set_context_menu(Button *but, ButtonSearchContextMenuFn context_menu_fn)
void button_func_search_set(Button *but, ButtonSearchCreateFn search_create_fn, ButtonSearchUpdateFn search_update_fn, void *arg, bool free_arg, FreeArgFunc search_arg_free_fn, ButtonHandleFunc search_exec_fn, void *active)
void button_func_search_set_tooltip(Button *but, ButtonSearchTooltipFn tooltip_fn)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
__int64 int64_t
Definition BKE_text.h:90
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
bool WM_keymap_poll(bContext *C, wmKeyMap *keymap)
Definition wm_keymap.cc:485
unsigned int uint
Definition BKE_text.h:55
std::string WM_operatortype_name(wmOperatorType *ot, PointerRNA *properties)
Span< MenuType * > WM_menutypes_registered_get()
int bool bool BLI_str_endswith(const char *__restrict str, const char *__restrict end) ATTR_NONNULL(1
void WM_operator_properties_free(PointerRNA *ptr)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_enum_items(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const EnumPropertyItem **r_item, int *r_totitem, bool *r_free)
MenuType * WM_menutype_find(const StringRef idname, bool quiet)
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
wmWindow * CTX_wm_window(const bContext *C)
const char * RNA_property_translation_context(const PropertyRNA *prop)
int RNA_enum_from_value(const EnumPropertyItem *item, const int value)
Span< wmOperatorType * > WM_operatortypes_registered_get()
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_array_check(PropertyRNA *prop)
Global G
Definition blender.cc:57
const bContextStore * CTX_store_get(const bContext *C)
PropertyType RNA_property_type(PropertyRNA *prop)
@ PROP_BOOLEAN
Definition RNA_types.hh:210
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
@ WM_HANDLER_DO_FREE
Definition WM_api.hh:715
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
bool RNA_property_boolean_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool RNA_property_enum_item_from_value_gettexted(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const int value, EnumPropertyItem *r_item)
void unit_m4(float m[4][4])
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
bool WM_menutype_poll(bContext *C, MenuType *mt)
@ WM_HANDLER_TYPE_KEYMAP
std::optional< std::string > WM_keymap_item_to_string(const wmKeyMapItem *kmi, const bool compact)
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:1024
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
void CTX_wm_region_set(bContext *C, ARegion *region)
void WM_event_get_keymaps_from_handler(wmWindowManager *wm, wmWindow *win, wmEventHandler_Keymap *handler, wmEventHandler_KeymapResult *km_result)
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
std::string RNA_string_get(PointerRNA *ptr, const char *name)
void RNA_property_boolean_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, bool value)
void CTX_store_set(bContext *C, const bContextStore *store)
bool WM_operator_poll(bContext *C, wmOperatorType *ot)
bScreen * WM_window_get_active_screen(const wmWindow *win)
size_t WM_operator_py_idname(char *dst, const char *src)
ARegion * CTX_wm_region(const bContext *C)
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
void CTX_wm_area_set(bContext *C, ScrArea *area)
void WM_operator_name_call_ptr_with_depends_on_cursor(bContext *C, wmOperatorType *ot, wm::OpCallContext opcontext, PointerRNA *properties, const wmEvent *event, const StringRef drawstr)
#define UI_MENU_ARROW_SEP
bke::ARegionRuntime * runtime
char idname[BKE_ST_MAXNAME]
char translation_context[BKE_ST_MAXNAME]
char label[BKE_ST_MAXNAME]
MenuTypeFlag flag
ListBaseT< struct wmEventHandler > handlers
ListBaseT< SpaceLink > spacedata
Vector< bContextStoreEntry > entries
ListBaseT< struct ScrArea > areabase
ListBaseT< wmEventHandler > handlers
ListBaseT< ui::Block > uiblocks
Map< std::string, ui::Block * > block_name_map
ListBaseT< wmEventHandler > handlers
Vector< std::unique_ptr< Button > > buttons_ptrs
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
wm::OpCallContext opcontext
wmOperatorType * optype
const bContextStore * context
void operator_context_set(wm::OpCallContext opcontext)
void context_copy(const bContextStore *context)
Vector< std::reference_wrapper< MenuSearch_Item > > items
struct blender::ui::MenuSearch_Data::@155231135140331001337335320071155057127353241325 context_menu_data
std::variant< OperatorData, PropertyData > data
std::optional< bContextStore > context
ListBaseT< wmKeyMapItem > items
bke::WindowRuntime * runtime