Blender V5.3
interface_template_keymap.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
11#include "RNA_access.hh"
12#include "RNA_prototypes.hh"
13
14#include "WM_api.hh"
15
17#include "interface_intern.hh"
18
19namespace blender::ui {
20
21static void keymap_item_modified(bContext * /*C*/, void *kmi_p, void * /*unused*/)
22{
23 wmKeyMapItem *kmi = static_cast<wmKeyMapItem *>(kmi_p);
24 WM_keyconfig_update_tag(nullptr, kmi);
25 U.runtime.is_dirty = true;
26}
27
28static void template_keymap_item_properties(Layout &layout, const char *title, PointerRNA *ptr)
29{
30 layout.separator();
31
32 if (title) {
33 layout.label(title, ICON_NONE);
34 }
35
36 Layout &flow = layout.column_flow(2, false);
37
39 const bool is_set = RNA_property_is_set(ptr, prop);
40 Button *but;
41
42 /* recurse for nested properties */
43 if (RNA_property_type(prop) == PROP_POINTER) {
44 PointerRNA propptr = RNA_property_pointer_get(ptr, prop);
45
46 if (propptr && RNA_struct_is_a(propptr.type, RNA_OperatorProperties)) {
47 const char *name = RNA_property_ui_name(prop);
48 template_keymap_item_properties(layout, name, &propptr);
49 continue;
50 }
51 }
52
53 Layout &box = flow.box();
54 box.active_set(is_set);
55 Layout &row = box.row(false);
56
57 /* property value */
58 row.prop(ptr, prop, -1, 0, UI_ITEM_NONE, std::nullopt, ICON_NONE);
59
60 if (is_set) {
61 /* unset operator */
62 Block *block = row.block();
64 but = uiDefIconButO(block,
66 "UI_OT_unset_property_button",
68 ICON_X,
69 0,
70 0,
73 std::nullopt);
74 but->rnapoin = *ptr;
75 but->rnaprop = prop;
77 }
78 }
80}
81
83{
84 PointerRNA propptr = RNA_pointer_get(ptr, "properties");
85
86 if (propptr) {
87 Block *block = layout->block();
88 const int old_but_count = layout->block()->buttons_ptrs.size();
89
90 WM_operator_properties_sanitize(&propptr, false);
91 template_keymap_item_properties(*layout, nullptr, &propptr);
92 if (old_but_count < 0) {
93 return;
94 }
95 /* attach callbacks to compensate for missing properties update,
96 * we don't know which keymap (item) is being modified there */
97 for (Button &but : block->buttons() | std::views::drop(old_but_count)) {
98 /* operator buttons may store props for use (file selector, #36492) */
99 if (but.rnaprop) {
100 button_func_set(&but, keymap_item_modified, ptr->data, nullptr);
101
102 /* Otherwise the keymap will be re-generated which we're trying to edit,
103 * see: #47685 */
105 }
106 }
107 }
108}
109
110} // namespace blender::ui
#define RNA_STRUCT_END
#define RNA_STRUCT_BEGIN_SKIP_RNA_TYPE(sptr, prop)
#define UI_UNIT_Y
#define UI_UNIT_X
#define UI_ITEM_NONE
static void keymap_item_modified(bContext *, void *kmi_p, void *)
void button_func_set(Button *but, std::function< void(bContext &)> func)
void uiTemplateKeymapItemProperties(Layout *layout, PointerRNA *ptr)
Button * uiDefIconButO(Block *block, ButtonType type, StringRefNull opname, wm::OpCallContext opcontext, int icon, int x, int y, short width, short height, std::optional< StringRef > tip)
void button_flag_enable(Button *but, int64_t flag)
static void template_keymap_item_properties(Layout &layout, const char *title, PointerRNA *ptr)
void block_emboss_set(Block *block, EmbossType emboss)
UserDef U
Definition blender.cc:58
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
void WM_operator_properties_sanitize(PointerRNA *ptr, const bool no_context)
PropertyType RNA_property_type(PropertyRNA *prop)
@ PROP_POINTER
Definition RNA_types.hh:215
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
void WM_keyconfig_update_tag(wmKeyMap *keymap, wmKeyMapItem *kmi)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
StructRNA * type
Definition RNA_types.hh:63
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
void label(StringRef name, int icon)
Layout & column_flow(int number, bool align)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & row(bool align)
bool is_set(uint i)