Repository navigation
Expand file tree
/
Copy pathpy_uilist.cpp
More file actions
185 lines (165 loc) · 6.88 KB
/
Copy pathpy_uilist.cpp
File metadata and controls
185 lines (165 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* SPDX-FileCopyrightText: 2025 LichtFeld Studio Authors
*
* SPDX-License-Identifier: GPL-3.0-or-later */
#include "py_uilist.hpp"
#include "core/logger.hpp"
#include "python/python_runtime.hpp"
#include "rml_im_mode_layout.hpp"
namespace lfs::python {
PyUIListRegistry& PyUIListRegistry::instance() {
static PyUIListRegistry registry;
return registry;
}
void PyUIListRegistry::register_uilist(nb::object list_class) {
std::lock_guard lock(mutex_);
std::string id;
if (nb::hasattr(list_class, "list_id")) {
id = nb::cast<std::string>(list_class.attr("list_id"));
} else if (nb::hasattr(list_class, "__name__")) {
id = nb::cast<std::string>(list_class.attr("__name__"));
} else {
LOG_ERROR("UIList class missing list_id or __name__ attribute");
return;
}
uilists_[id] = {id, list_class, nb::object()};
}
void PyUIListRegistry::unregister_uilist(const std::string& id) {
std::lock_guard lock(mutex_);
uilists_.erase(id);
}
void PyUIListRegistry::unregister_all() {
std::lock_guard lock(mutex_);
uilists_.clear();
}
nb::object PyUIListRegistry::get_uilist(const std::string& id) {
nb::object list_class;
{
std::lock_guard lock(mutex_);
auto it = uilists_.find(id);
if (it == uilists_.end())
return {};
if (it->second.list_instance.is_valid())
return it->second.list_instance;
list_class = it->second.list_class;
}
// A constructor can mutate the registry. Keep Python outside the lock
// and return an owned reference that survives unregistering during draw.
nb::object instance;
try {
instance = list_class();
} catch (const std::exception& e) {
LOG_ERROR("Failed to instantiate UIList {}: {}", id, e.what());
return {};
}
std::lock_guard lock(mutex_);
auto it = uilists_.find(id);
if (it == uilists_.end() || !it->second.list_class.is(list_class))
return {};
it->second.list_instance = instance;
return instance;
}
std::vector<std::string> PyUIListRegistry::get_uilist_ids() const {
std::lock_guard lock(mutex_);
std::vector<std::string> ids;
ids.reserve(uilists_.size());
for (const auto& [id, _] : uilists_) {
ids.push_back(id);
}
return ids;
}
std::tuple<bool, int> PyUILayoutTemplates::template_list(
PyUILayout& layout,
const std::string&,
const std::string&,
nb::object,
const std::string&,
const int active_index,
int) {
if (layout.isDrawHook()) {
layout.warnUnsupportedInDrawHook("template_list");
return {false, active_index};
}
throw nb::type_error(
"UILayout.template_list is unsupported; use the live "
"RmlUILayout.template_list API from a Python panel draw callback");
}
bool PyUILayoutTemplates::template_tree(
PyUILayout& layout,
const std::string&,
nb::object,
bool) {
if (layout.isDrawHook()) {
layout.warnUnsupportedInDrawHook("template_tree");
return false;
}
throw nb::type_error(
"UILayout.template_tree is unsupported; use the live "
"RmlUILayout.collapsing_header API from a Python panel draw callback");
}
std::tuple<bool, std::string> PyUILayoutTemplates::template_id(
PyUILayout& layout,
const std::string&,
const std::vector<std::string>&,
const std::string& current_id) {
if (layout.isDrawHook()) {
layout.warnUnsupportedInDrawHook("template_id");
return {false, current_id};
}
throw nb::type_error(
"UILayout.template_id is unsupported; use the live "
"RmlUILayout.combo API from a Python panel draw callback");
}
void register_uilist(nb::module_& m) {
set_uilist_callbacks(
[](const char* id) -> void* {
return PyUIListRegistry::instance().get_uilist(id).release().ptr();
},
[](void* layout) -> void* {
return nb::cast(static_cast<RmlImModeLayout*>(layout), nb::rv_policy::reference).release().ptr();
});
m.def(
"register_uilist", [](nb::object list_class) {
PyUIListRegistry::instance().register_uilist(list_class);
},
nb::arg("list_class"), "Register a UIList class for custom list rendering");
m.def(
"unregister_uilist", [](const std::string& id) {
PyUIListRegistry::instance().unregister_uilist(id);
},
nb::arg("id"), "Unregister a UIList by ID");
m.def(
"unregister_all_uilists", []() { PyUIListRegistry::instance().unregister_all(); },
"Unregister all UIList classes");
m.def(
"get_uilist_ids", []() { return PyUIListRegistry::instance().get_uilist_ids(); },
"Get all registered UIList IDs");
}
void add_template_methods_to_uilayout(nb::class_<PyUILayout>& layout_class) {
layout_class
.def(
"template_list",
[](PyUILayout& self, const std::string& listtype_name, const std::string& list_id,
nb::object data, const std::string& propname, int active_index, int rows) {
return PyUILayoutTemplates::template_list(self, listtype_name, list_id,
data, propname, active_index, rows);
},
nb::arg("listtype_name"), nb::arg("list_id"), nb::arg("data"),
nb::arg("propname"), nb::arg("active_index"), nb::arg("rows") = 5,
"Unsupported on UILayout; use RmlUILayout.template_list.")
.def(
"template_tree",
[](PyUILayout& self, const std::string& label, nb::object draw_callback, bool default_open) {
return PyUILayoutTemplates::template_tree(self, label, draw_callback, default_open);
},
nb::arg("label"), nb::arg("draw_callback"), nb::arg("default_open") = false,
"Unsupported on UILayout; use RmlUILayout.collapsing_header.")
.def(
"template_id",
[](PyUILayout& self, const std::string& label, const std::vector<std::string>& items,
const std::string& current_id) {
return PyUILayoutTemplates::template_id(self, label, items, current_id);
},
nb::arg("label"), nb::arg("items"), nb::arg("current_id"),
"Unsupported on UILayout; use RmlUILayout.combo.");
}
} // namespace lfs::python