Repository navigation
Expand file tree
/
Copy pathrml_im_mode_panel_adapter.cpp
More file actions
358 lines (302 loc) · 12.2 KB
/
Copy pathrml_im_mode_panel_adapter.cpp
File metadata and controls
358 lines (302 loc) · 12.2 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* SPDX-FileCopyrightText: 2025 LichtFeld Studio Authors
*
* SPDX-License-Identifier: GPL-3.0-or-later */
#include "rml_im_mode_panel_adapter.hpp"
#include "core/logger.hpp"
#include "py_ui.hpp"
#include "python/gil.hpp"
#include "python/python_runtime.hpp"
#include "python_panel_chrome.hpp"
#include <RmlUi/Core/ElementDocument.h>
#include <cassert>
namespace lfs::vis::gui {
namespace {
lfs::python::MouseState makeMouseState(const std::optional<PanelInputState>& input,
float prev_mouse_x, float prev_mouse_y,
bool have_prev_mouse,
bool& have_left_click_time,
std::chrono::steady_clock::time_point& last_left_click_at) {
lfs::python::MouseState mouse;
if (!input) {
mouse.pos_x = prev_mouse_x;
mouse.pos_y = prev_mouse_y;
return mouse;
}
mouse.pos_x = input->mouse_x;
mouse.pos_y = input->mouse_y;
if (have_prev_mouse) {
mouse.delta_x = mouse.pos_x - prev_mouse_x;
mouse.delta_y = mouse.pos_y - prev_mouse_y;
}
mouse.wheel = input->mouse_wheel;
mouse.right_clicked = input->mouse_clicked[1];
if (input->mouse_clicked[0]) {
constexpr auto kDoubleClickWindow = std::chrono::milliseconds(350);
const auto now = std::chrono::steady_clock::now();
mouse.double_clicked =
have_left_click_time && (now - last_left_click_at) <= kDoubleClickWindow;
last_left_click_at = now;
have_left_click_time = true;
}
mouse.dragging = input->mouse_down[0];
return mouse;
}
} // namespace
RmlImModePanelAdapter::RmlImModePanelAdapter(void* manager, nb::object panel_instance,
const bool has_poll,
const std::string& rml_path)
: manager_(manager),
rml_path_(rml_path),
panel_instance_(std::move(panel_instance)),
has_poll_(has_poll) {
assert(manager_);
}
RmlImModePanelAdapter::~RmlImModePanelAdapter() {
const bool has_gil = lfs::python::can_acquire_gil();
std::optional<lfs::python::GilAcquire> gil;
if (has_gil)
gil.emplace();
layout_.release_elements();
if (host_) {
const auto& ops = lfs::python::get_rml_panel_host_ops();
assert(ops.destroy);
ops.destroy(host_);
host_ = nullptr;
}
// Panel adapters are released by the graphics-thread cleanup queue.
// Drop the Python instance while that thread holds the GIL so its
// final decref cannot touch interpreter state without a thread state.
if (has_gil)
panel_instance_ = nb::object();
}
void RmlImModePanelAdapter::ensureHost() {
if (host_)
return;
const auto& ops = lfs::python::get_rml_panel_host_ops();
assert(ops.create);
static int ctx_counter = 0;
std::string ctx_name = "im_mode_" + std::to_string(ctx_counter++);
host_ = ops.create(manager_, ctx_name.c_str(), rml_path_.c_str(), "");
if (host_ && ops.set_height_mode)
ops.set_height_mode(host_, 1);
if (host_ && ops.set_floating)
ops.set_floating(host_, floating_);
}
void RmlImModePanelAdapter::drawLayout(const PanelDrawContext* ctx) {
const auto& ops = lfs::python::get_rml_panel_host_ops();
if (ops.ensure_document && !ops.ensure_document(host_))
return;
auto* doc = static_cast<Rml::ElementDocument*>(ops.get_document(host_));
if (!doc)
return;
if (!lfs::python::can_acquire_gil())
return;
const uint64_t frame_serial = ctx ? ctx->frame_serial : 0;
if (frame_serial != 0 && last_layout_frame_ == frame_serial)
return;
if (lfs::python::bridge().prepare_ui)
lfs::python::bridge().prepare_ui();
const lfs::python::GilAcquire gil;
const lfs::python::MouseState mouse = makeMouseState(
current_input_, prev_mouse_x_, prev_mouse_y_, have_prev_mouse_,
have_left_click_time_, last_left_click_at_);
prev_mouse_x_ = mouse.pos_x;
prev_mouse_y_ = mouse.pos_y;
have_prev_mouse_ = true;
layout_.begin_frame(doc, mouse);
try {
panel_instance_.attr("draw")(nb::cast(layout_, nb::rv_policy::reference));
} catch (const std::exception& e) {
LOG_ERROR("RmlImMode draw error: {}", e.what());
}
layout_.end_frame();
if (ops.mark_content_dirty)
ops.mark_content_dirty(host_);
if (frame_serial != 0)
last_layout_frame_ = frame_serial;
}
void RmlImModePanelAdapter::draw(const PanelDrawContext& ctx) {
ensureHost();
if (!host_)
return;
const auto& ops = lfs::python::get_rml_panel_host_ops();
const lfs::python::SceneContextGuard scene_guard(ctx.scene);
drawLayout(&ctx);
ops.draw(host_, &ctx);
}
PanelDirectRenderResult RmlImModePanelAdapter::renderDirect(
const PanelDirectRenderRequest& request,
const PanelDrawContext& ctx) {
setPanelSpace(request.space);
if (request.mode == PanelDirectRenderMode::Measure)
return {.handled = true, .height = getDirectDrawHeight()};
setInputClipY(request.clip_y_min, request.clip_y_max);
setInput(request.input);
setForcedHeight(request.forced_height);
bool handled = true;
try {
switch (request.mode) {
case PanelDirectRenderMode::Measure:
break;
case PanelDirectRenderMode::Draw:
drawDirect(request.x, request.y, request.width, request.height, ctx);
break;
case PanelDirectRenderMode::Cached:
handled = drawDirectCached(request.x, request.y, request.width,
request.height, ctx);
break;
case PanelDirectRenderMode::Preload:
preloadDirect(request.width, request.height, ctx,
request.clip_y_min, request.clip_y_max, request.input);
break;
}
} catch (...) {
setForcedHeight(0.0f);
setInput(nullptr);
setInputClipY(-1.0f, -1.0f);
throw;
}
const float height = getDirectDrawHeight();
setForcedHeight(0.0f);
setInput(nullptr);
setInputClipY(-1.0f, -1.0f);
return {.handled = handled, .height = height};
}
void RmlImModePanelAdapter::preloadDirect(float w, float h, const PanelDrawContext& ctx,
float clip_y_min, float clip_y_max,
const PanelInputState* input) {
ensureHost();
if (!host_)
return;
const auto& ops = lfs::python::get_rml_panel_host_ops();
if (!ops.prepare_direct)
return;
if (ops.set_input_clip_y)
ops.set_input_clip_y(host_, clip_y_min, clip_y_max);
if (ops.set_input)
ops.set_input(host_, input);
const lfs::python::SceneContextGuard scene_guard(ctx.scene);
drawLayout(&ctx);
ops.prepare_direct(host_, w, h);
if (ops.set_input)
ops.set_input(host_, nullptr);
if (ops.set_input_clip_y)
ops.set_input_clip_y(host_, -1.0f, -1.0f);
}
void RmlImModePanelAdapter::drawDirect(float x, float y, float w, float h,
const PanelDrawContext& ctx) {
ensureHost();
if (!host_)
return;
const auto& ops = lfs::python::get_rml_panel_host_ops();
const lfs::python::SceneContextGuard scene_guard(ctx.scene);
drawLayout(&ctx);
ops.draw_direct(host_, x, y, w, h);
}
bool RmlImModePanelAdapter::drawDirectCached(float x, float y, float w, float h,
const PanelDrawContext& ctx) {
(void)ctx;
if (!host_)
return false;
if (needsAnimationFrame())
return false;
const auto& ops = lfs::python::get_rml_panel_host_ops();
return ops.draw_direct_cached ? ops.draw_direct_cached(host_, x, y, w, h) : false;
}
float RmlImModePanelAdapter::getDirectDrawHeight() const {
if (!host_)
return 0.0f;
const auto& ops = lfs::python::get_rml_panel_host_ops();
return ops.get_content_height ? ops.get_content_height(host_) : 0.0f;
}
void RmlImModePanelAdapter::setInputClipY(float y_min, float y_max) {
if (host_) {
const auto& ops = lfs::python::get_rml_panel_host_ops();
if (ops.set_input_clip_y)
ops.set_input_clip_y(host_, y_min, y_max);
}
}
void RmlImModePanelAdapter::setInput(const PanelInputState* input) {
if (input)
current_input_ = *input;
else
current_input_.reset();
if (!host_)
return;
const auto& ops = lfs::python::get_rml_panel_host_ops();
if (ops.set_input)
ops.set_input(host_, input);
}
void RmlImModePanelAdapter::setForcedHeight(float h) {
if (host_) {
const auto& ops = lfs::python::get_rml_panel_host_ops();
if (ops.set_forced_height)
ops.set_forced_height(host_, h);
}
}
void RmlImModePanelAdapter::setPanelSpace(const PanelSpace space) {
const bool floating = space == PanelSpace::Floating;
if (floating_ == floating)
return;
floating_ = floating;
if (host_) {
const auto& ops = lfs::python::get_rml_panel_host_ops();
if (ops.set_floating)
ops.set_floating(host_, floating_);
}
}
bool RmlImModePanelAdapter::needsAnimationFrame() const {
if (!host_)
return false;
const auto& ops = lfs::python::get_rml_panel_host_ops();
return ops.needs_animation ? ops.needs_animation(host_) : false;
}
std::optional<double> RmlImModePanelAdapter::nextScheduledAnimationDelay() const {
if (!host_)
return std::nullopt;
const auto& ops = lfs::python::get_rml_panel_host_ops();
if (!ops.next_scheduled_update_delay)
return std::nullopt;
double host_delay = 0.0;
if (!ops.next_scheduled_update_delay(host_, &host_delay))
return std::nullopt;
return host_delay;
}
void RmlImModePanelAdapter::reloadRmlResources() {
if (!host_)
return;
const auto& ops = lfs::python::get_rml_panel_host_ops();
if (!ops.reload_document)
return;
layout_.release_elements();
if (!ops.reload_document(host_)) {
LOG_ERROR("RmlImMode reload_document failed for '{}'", rml_path_);
return;
}
if (ops.mark_content_dirty)
ops.mark_content_dirty(host_);
last_layout_frame_ = 0;
}
bool RmlImModePanelAdapter::poll(const PanelDrawContext& ctx) {
(void)ctx;
if (!has_poll_)
return true;
if (!lfs::python::can_acquire_gil())
return false;
if (lfs::python::bridge().prepare_ui)
lfs::python::bridge().prepare_ui();
const lfs::python::GilAcquire gil;
try {
return nb::cast<bool>(panel_instance_.attr("poll")(lfs::python::get_app_context()));
} catch (const std::exception& e) {
LOG_ERROR("Panel poll error: {}", e.what());
return false;
}
}
std::string RmlImModePanelAdapter::captureChromeJson() const {
return capture_python_panel_chrome(panel_instance_);
}
void RmlImModePanelAdapter::applyChromeJson(const std::string_view json) {
apply_python_panel_chrome(panel_instance_, json);
}
} // namespace lfs::vis::gui