noesis_runtime 0.12.1

Rust bindings for the Noesis GUI Native SDK: load XAML UI, drive the view and renderer, and write custom controls in Rust. Renderer-agnostic; Bevy integration lives in noesis_bevy.
Documentation
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Input: finer control.
//
// Element-level mouse/touch capture, keyboard-state queries, focus-state DPs,
// focus engagement + traversal (MoveFocus / PredictFocus), the FocusManager and
// KeyboardNavigation static/attached-property surfaces, and input gestures +
// bindings (KeyGesture / MouseGesture / KeyBinding / MouseBinding /
// InputBinding) wired to the existing RustCommand ICommand bridge.
//
// Everything here narrows an opaque `Noesis::BaseComponent*` to the concrete
// type it needs via DynamicCast and null-checks first, returning false / null /
// a no-op on a type mismatch, never dereferencing blind. Borrowed pointers
// (GetCaptured, GetFocused, GetFocusedElement, GetFocusScope, PredictFocus) are
// returned WITHOUT an extra reference; the create entrypoints (gestures /
// bindings) return a fresh object at +1 that Rust releases on drop, mirroring
// the `new Noesis::RoutedCommand(...)` idiom in noesis_commands.cpp.

#include "noesis_shim.h"

#include <NsCore/DynamicCast.h>
#include <NsCore/Noesis.h>
#include <NsCore/Ptr.h>
#include <NsGui/DependencyObject.h>
#include <NsGui/Enums.h>
#include <NsGui/FocusManager.h>
#include <NsGui/FrameworkElement.h>
#include <NsGui/ICommand.h>
#include <NsGui/InputBinding.h>
#include <NsGui/InputEnums.h>
#include <NsGui/Keyboard.h>
#include <NsGui/KeyBinding.h>
#include <NsGui/KeyGesture.h>
#include <NsGui/KeyboardNavigation.h>
#include <NsGui/Mouse.h>
#include <NsGui/MouseBinding.h>
#include <NsGui/MouseGesture.h>
#include <NsGui/UICollection.h>
#include <NsGui/UIElement.h>

// Lock the FFI enum ordinals against the Noesis headers at compile time, the
// same way noesis_view.cpp pins MouseButton / Key.
static_assert((int32_t)Noesis::ModifierKeys_None == 0, "ModifierKeys::None");
static_assert((int32_t)Noesis::ModifierKeys_Alt == 1, "ModifierKeys::Alt");
static_assert((int32_t)Noesis::ModifierKeys_Control == 2, "ModifierKeys::Control");
static_assert((int32_t)Noesis::ModifierKeys_Shift == 4, "ModifierKeys::Shift");
static_assert((int32_t)Noesis::ModifierKeys_Windows == 8, "ModifierKeys::Windows");

static_assert((int32_t)Noesis::KeyStates_None == 0, "KeyStates::None");
static_assert((int32_t)Noesis::KeyStates_Down == 1, "KeyStates::Down");
static_assert((int32_t)Noesis::KeyStates_Toggled == 2, "KeyStates::Toggled");

static_assert((int32_t)Noesis::CaptureMode_None == 0, "CaptureMode::None");
static_assert((int32_t)Noesis::CaptureMode_Element == 1, "CaptureMode::Element");
static_assert((int32_t)Noesis::CaptureMode_SubTree == 2, "CaptureMode::SubTree");

static_assert((int32_t)Noesis::MouseAction_None == 0, "MouseAction::None");
static_assert((int32_t)Noesis::MouseAction_LeftClick == 1, "MouseAction::LeftClick");
static_assert((int32_t)Noesis::MouseAction_RightClick == 2, "MouseAction::RightClick");
static_assert((int32_t)Noesis::MouseAction_MiddleClick == 3, "MouseAction::MiddleClick");
static_assert((int32_t)Noesis::MouseAction_WheelClick == 4, "MouseAction::WheelClick");
static_assert((int32_t)Noesis::MouseAction_LeftDoubleClick == 5, "MouseAction::LeftDoubleClick");
static_assert((int32_t)Noesis::MouseAction_RightDoubleClick == 6, "MouseAction::RightDoubleClick");
static_assert((int32_t)Noesis::MouseAction_MiddleDoubleClick == 7, "MouseAction::MiddleDoubleClick");

static_assert((int32_t)Noesis::FocusNavigationDirection_Next == 0, "FND::Next");
static_assert((int32_t)Noesis::FocusNavigationDirection_Previous == 1, "FND::Previous");
static_assert((int32_t)Noesis::FocusNavigationDirection_First == 2, "FND::First");
static_assert((int32_t)Noesis::FocusNavigationDirection_Last == 3, "FND::Last");
static_assert((int32_t)Noesis::FocusNavigationDirection_Left == 4, "FND::Left");
static_assert((int32_t)Noesis::FocusNavigationDirection_Right == 5, "FND::Right");
static_assert((int32_t)Noesis::FocusNavigationDirection_Up == 6, "FND::Up");
static_assert((int32_t)Noesis::FocusNavigationDirection_Down == 7, "FND::Down");

static_assert((int32_t)Noesis::KeyboardNavigationMode_Continue == 0, "KNM::Continue");
static_assert((int32_t)Noesis::KeyboardNavigationMode_Once == 1, "KNM::Once");
static_assert((int32_t)Noesis::KeyboardNavigationMode_Cycle == 2, "KNM::Cycle");
static_assert((int32_t)Noesis::KeyboardNavigationMode_None == 3, "KNM::None");
static_assert((int32_t)Noesis::KeyboardNavigationMode_Contained == 4, "KNM::Contained");
static_assert((int32_t)Noesis::KeyboardNavigationMode_Local == 5, "KNM::Local");

namespace {

inline Noesis::UIElement* as_ui(void* p) {
    return Noesis::DynamicCast<Noesis::UIElement*>(static_cast<Noesis::BaseComponent*>(p));
}

inline Noesis::DependencyObject* as_do(void* p) {
    return Noesis::DynamicCast<Noesis::DependencyObject*>(static_cast<Noesis::BaseComponent*>(p));
}

}  // namespace

// ── Mouse / touch capture (element-level) ────────────────────────────────────

extern "C" bool noesis_ui_element_capture_mouse(void* element) {
    Noesis::UIElement* ui = as_ui(element);
    return ui ? ui->CaptureMouse() : false;
}

extern "C" void noesis_ui_element_release_mouse_capture(void* element) {
    Noesis::UIElement* ui = as_ui(element);
    if (ui) {
        ui->ReleaseMouseCapture();
    }
}

extern "C" bool noesis_ui_element_get_is_mouse_captured(void* element) {
    Noesis::UIElement* ui = as_ui(element);
    return ui ? ui->GetIsMouseCaptured() : false;
}

extern "C" bool noesis_ui_element_capture_touch(void* element, uint64_t touch_device) {
    Noesis::UIElement* ui = as_ui(element);
    return ui ? ui->CaptureTouch(touch_device) : false;
}

// Capture `element` through its View's Mouse with the given CaptureMode (0 None,
// 1 Element, 2 SubTree). Returns false if not a UIElement or not yet attached to
// a View (GetMouse() is null until layout connects the element).
extern "C" bool noesis_ui_element_capture_mouse_mode(void* element, int32_t mode) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return false;
    Noesis::Mouse* mouse = ui->GetMouse();
    return mouse ? mouse->Capture(ui, static_cast<Noesis::CaptureMode>(mode)) : false;
}

// Borrowed `UIElement*` currently holding mouse capture in `element`'s View, or
// null if nothing is captured / not a UIElement / no View.
extern "C" void* noesis_ui_element_get_mouse_captured(void* element) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return nullptr;
    Noesis::Mouse* mouse = ui->GetMouse();
    return mouse ? static_cast<void*>(mouse->GetCaptured()) : nullptr;
}

// Pointer position relative to `element`, in element-local DIPs
// (Mouse::GetPosition(UIElement*)). Writes the position into *out_x/*out_y and
// returns true; false (outputs untouched) if `element` is not a UIElement. The
// value is the last position the View's Mouse recorded (set by MouseMove), so it
// is meaningful only once `element` is attached to a live, input-pumped View;
// before any pointer event it reads back as the origin.
extern "C" bool noesis_ui_element_get_mouse_position(
    void* element, float* out_x, float* out_y) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return false;
    Noesis::Point p = Noesis::Mouse::GetPosition(ui);
    if (out_x) *out_x = p.x;
    if (out_y) *out_y = p.y;
    return true;
}

// ── Keyboard state / modifiers (via UIElement::GetKeyboard) ──────────────────

// `ModifierKeys` bitmask currently held down, into *out. False if not a
// UIElement or no Keyboard (no View).
extern "C" bool noesis_ui_element_get_modifiers(void* element, int32_t* out) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui || !out) return false;
    Noesis::Keyboard* kb = ui->GetKeyboard();
    if (!kb) return false;
    *out = static_cast<int32_t>(kb->GetModifiers());
    return true;
}

// `KeyStates` bitmask for `key`, into *out. False if not a UIElement / no
// Keyboard.
extern "C" bool noesis_ui_element_get_key_states(void* element, int32_t key, int32_t* out) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui || !out) return false;
    Noesis::Keyboard* kb = ui->GetKeyboard();
    if (!kb) return false;
    *out = static_cast<int32_t>(kb->GetKeyStates(static_cast<Noesis::Key>(key)));
    return true;
}

extern "C" bool noesis_ui_element_is_key_down(void* element, int32_t key) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return false;
    Noesis::Keyboard* kb = ui->GetKeyboard();
    return kb ? kb->IsKeyDown(static_cast<Noesis::Key>(key)) : false;
}

extern "C" bool noesis_ui_element_is_key_up(void* element, int32_t key) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return true;  // an un-attached element has no key held down
    Noesis::Keyboard* kb = ui->GetKeyboard();
    return kb ? kb->IsKeyUp(static_cast<Noesis::Key>(key)) : true;
}

extern "C" bool noesis_ui_element_is_key_toggled(void* element, int32_t key) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return false;
    Noesis::Keyboard* kb = ui->GetKeyboard();
    return kb ? kb->IsKeyToggled(static_cast<Noesis::Key>(key)) : false;
}

// Borrowed `UIElement*` with keyboard focus in `element`'s View, or null.
extern "C" void* noesis_ui_element_get_keyboard_focused(void* element) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return nullptr;
    Noesis::Keyboard* kb = ui->GetKeyboard();
    return kb ? static_cast<void*>(kb->GetFocused()) : nullptr;
}

// ── Focus-state DPs ──────────────────────────────────────────────────────────

extern "C" bool noesis_ui_element_get_is_focused(void* element) {
    Noesis::UIElement* ui = as_ui(element);
    return ui ? ui->GetIsFocused() : false;
}

extern "C" bool noesis_ui_element_get_is_keyboard_focused(void* element) {
    Noesis::UIElement* ui = as_ui(element);
    return ui ? ui->GetIsKeyboardFocused() : false;
}

extern "C" bool noesis_ui_element_get_is_keyboard_focus_within(void* element) {
    Noesis::UIElement* ui = as_ui(element);
    return ui ? ui->GetIsKeyboardFocusWithin() : false;
}

// ── Focus engagement + traversal ─────────────────────────────────────────────

extern "C" bool noesis_ui_element_focus_engage(void* element, bool engage) {
    Noesis::UIElement* ui = as_ui(element);
    return ui ? ui->Focus(engage) : false;
}

extern "C" bool noesis_ui_element_move_focus(void* element, int32_t direction, bool wrapped) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return false;
    Noesis::TraversalRequest request;
    request.focusNavigationDirection = static_cast<Noesis::FocusNavigationDirection>(direction);
    request.wrapped = wrapped;
    return ui->MoveFocus(request);
}

// Borrowed `DependencyObject*` Noesis predicts focus would land on, or null.
extern "C" void* noesis_ui_element_predict_focus(void* element, int32_t direction) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return nullptr;
    return static_cast<void*>(
        ui->PredictFocus(static_cast<Noesis::FocusNavigationDirection>(direction)));
}

// x:Name of the element PredictFocus would land on, borrowed (const char*, may
// be null), or null if there is no candidate or it is not a FrameworkElement.
extern "C" const char* noesis_ui_element_predict_focus_name(void* element, int32_t direction) {
    Noesis::UIElement* ui = as_ui(element);
    if (!ui) return nullptr;
    auto* fe = Noesis::DynamicCast<Noesis::FrameworkElement*>(
        ui->PredictFocus(static_cast<Noesis::FocusNavigationDirection>(direction)));
    return fe ? fe->GetName() : nullptr;
}

// ── FocusManager statics ─────────────────────────────────────────────────────

extern "C" void* noesis_focus_manager_get_focused_element(void* scope) {
    Noesis::DependencyObject* d = as_do(scope);
    return d ? static_cast<void*>(Noesis::FocusManager::GetFocusedElement(d)) : nullptr;
}

extern "C" bool noesis_focus_manager_set_focused_element(void* scope, void* element) {
    Noesis::DependencyObject* d = as_do(scope);
    if (!d) return false;
    // `element` may be null (clear). A non-null value must be a UIElement.
    Noesis::UIElement* ui = element ? as_ui(element) : nullptr;
    if (element && !ui) return false;
    Noesis::FocusManager::SetFocusedElement(d, ui);
    return true;
}

extern "C" bool noesis_focus_manager_get_is_focus_scope(void* element) {
    Noesis::DependencyObject* d = as_do(element);
    return d ? Noesis::FocusManager::GetIsFocusScope(d) : false;
}

extern "C" bool noesis_focus_manager_set_is_focus_scope(void* element, bool value) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d) return false;
    Noesis::FocusManager::SetIsFocusScope(d, value);
    return true;
}

extern "C" void* noesis_focus_manager_get_focus_scope(void* element) {
    Noesis::DependencyObject* d = as_do(element);
    return d ? static_cast<void*>(Noesis::FocusManager::GetFocusScope(d)) : nullptr;
}

// ── KeyboardNavigation attached properties ───────────────────────────────────

extern "C" bool noesis_keyboard_navigation_get_tab_index(void* element, int32_t* out) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d || !out) return false;
    *out = Noesis::KeyboardNavigation::GetTabIndex(d);
    return true;
}

extern "C" bool noesis_keyboard_navigation_set_tab_index(void* element, int32_t value) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d) return false;
    Noesis::KeyboardNavigation::SetTabIndex(d, value);
    return true;
}

extern "C" bool noesis_keyboard_navigation_get_is_tab_stop(void* element, bool* out) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d || !out) return false;
    *out = Noesis::KeyboardNavigation::GetIsTabStop(d);
    return true;
}

extern "C" bool noesis_keyboard_navigation_set_is_tab_stop(void* element, bool value) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d) return false;
    Noesis::KeyboardNavigation::SetIsTabStop(d, value);
    return true;
}

extern "C" bool noesis_keyboard_navigation_get_tab_navigation(void* element, int32_t* out) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d || !out) return false;
    *out = static_cast<int32_t>(Noesis::KeyboardNavigation::GetTabNavigation(d));
    return true;
}

extern "C" bool noesis_keyboard_navigation_set_tab_navigation(void* element, int32_t mode) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d) return false;
    Noesis::KeyboardNavigation::SetTabNavigation(
        d, static_cast<Noesis::KeyboardNavigationMode>(mode));
    return true;
}

extern "C" bool noesis_keyboard_navigation_get_control_tab_navigation(void* element,
                                                                         int32_t* out) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d || !out) return false;
    *out = static_cast<int32_t>(Noesis::KeyboardNavigation::GetControlTabNavigation(d));
    return true;
}

extern "C" bool noesis_keyboard_navigation_set_control_tab_navigation(void* element,
                                                                         int32_t mode) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d) return false;
    Noesis::KeyboardNavigation::SetControlTabNavigation(
        d, static_cast<Noesis::KeyboardNavigationMode>(mode));
    return true;
}

extern "C" bool noesis_keyboard_navigation_get_directional_navigation(void* element,
                                                                         int32_t* out) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d || !out) return false;
    *out = static_cast<int32_t>(Noesis::KeyboardNavigation::GetDirectionalNavigation(d));
    return true;
}

extern "C" bool noesis_keyboard_navigation_set_directional_navigation(void* element,
                                                                         int32_t mode) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d) return false;
    Noesis::KeyboardNavigation::SetDirectionalNavigation(
        d, static_cast<Noesis::KeyboardNavigationMode>(mode));
    return true;
}

extern "C" bool noesis_keyboard_navigation_get_accepts_return(void* element, bool* out) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d || !out) return false;
    *out = Noesis::KeyboardNavigation::GetAcceptsReturn(d);
    return true;
}

extern "C" bool noesis_keyboard_navigation_set_accepts_return(void* element, bool value) {
    Noesis::DependencyObject* d = as_do(element);
    if (!d) return false;
    Noesis::KeyboardNavigation::SetAcceptsReturn(d, value);
    return true;
}

// ── Input gestures + bindings ────────────────────────────────────────────────
//
// Each create returns a fresh object at +1 (Noesis `new` yields refcount 1,
// like `new RoutedCommand` in noesis_commands.cpp); Rust releases it on drop.
// `add_input_binding` hands the binding to the element's InputBindingCollection,
// which adds its own reference.

extern "C" void* noesis_key_gesture_create(int32_t key, int32_t modifiers) {
    auto* g = new Noesis::KeyGesture(static_cast<Noesis::Key>(key),
                                     static_cast<Noesis::ModifierKeys>(modifiers));
    return static_cast<void*>(g);
}

extern "C" void* noesis_mouse_gesture_create(int32_t action, int32_t modifiers) {
    auto* g = new Noesis::MouseGesture(static_cast<Noesis::MouseAction>(action),
                                       static_cast<Noesis::ModifierKeys>(modifiers));
    return static_cast<void*>(g);
}

extern "C" void* noesis_key_binding_create(void* command, int32_t key, int32_t modifiers) {
    auto* cmd = Noesis::DynamicCast<Noesis::ICommand*>(
        static_cast<Noesis::BaseComponent*>(command));
    if (!cmd) return nullptr;
    auto* b = new Noesis::KeyBinding(cmd, static_cast<Noesis::Key>(key),
                                     static_cast<Noesis::ModifierKeys>(modifiers));
    return static_cast<void*>(static_cast<Noesis::InputBinding*>(b));
}

extern "C" void* noesis_mouse_binding_create(void* command, int32_t action, int32_t modifiers) {
    auto* cmd = Noesis::DynamicCast<Noesis::ICommand*>(
        static_cast<Noesis::BaseComponent*>(command));
    if (!cmd) return nullptr;
    auto* b = new Noesis::MouseBinding(cmd, static_cast<Noesis::MouseAction>(action),
                                       static_cast<Noesis::ModifierKeys>(modifiers));
    return static_cast<void*>(static_cast<Noesis::InputBinding*>(b));
}

extern "C" void* noesis_input_binding_create(void* command, void* gesture) {
    auto* cmd = Noesis::DynamicCast<Noesis::ICommand*>(
        static_cast<Noesis::BaseComponent*>(command));
    auto* g = Noesis::DynamicCast<Noesis::InputGesture*>(
        static_cast<Noesis::BaseComponent*>(gesture));
    if (!cmd || !g) return nullptr;
    auto* b = new Noesis::InputBinding(cmd, g);
    return static_cast<void*>(b);
}

extern "C" bool noesis_ui_element_add_input_binding(void* element, void* binding) {
    Noesis::UIElement* ui = as_ui(element);
    auto* b = Noesis::DynamicCast<Noesis::InputBinding*>(
        static_cast<Noesis::BaseComponent*>(binding));
    if (!ui || !b) return false;
    ui->GetInputBindings()->Add(b);
    return true;
}

extern "C" bool noesis_ui_element_remove_input_binding(void* element, void* binding) {
    Noesis::UIElement* ui = as_ui(element);
    auto* b = Noesis::DynamicCast<Noesis::InputBinding*>(
        static_cast<Noesis::BaseComponent*>(binding));
    if (!ui || !b) return false;
    return ui->GetInputBindings()->Remove(b);
}