-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrust_widgets.py
More file actions
198 lines (138 loc) · 8.17 KB
/
rust_widgets.py
File metadata and controls
198 lines (138 loc) · 8.17 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
# Python ctypes wrapper for the rust_widgets C ABI.
import ctypes
import os
import platform
from ctypes import c_char_p, c_float, c_int, c_uint, c_uint64, c_void_p
def _default_library_name() -> str:
system = platform.system().lower()
if system == "darwin":
return "librust_widgets.dylib"
if system == "windows":
return "rust_widgets.dll"
return "librust_widgets.so"
def _resolve_library_path() -> str:
env_path = os.environ.get("RUST_WIDGETS_LIB")
if env_path:
return env_path
root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
candidate = os.path.join(root, "target", "debug", _default_library_name())
if os.path.exists(candidate):
return candidate
return _default_library_name()
class RustWidgets:
def __init__(self, library_path: str | None = None):
self._lib = ctypes.CDLL(library_path or _resolve_library_path())
self._bind_signatures()
def _bind_signatures(self) -> None:
self._lib.rust_widgets_init.argtypes = []
self._lib.rust_widgets_init.restype = None
self._lib.rust_widgets_run.argtypes = []
self._lib.rust_widgets_run.restype = None
self._lib.rust_widgets_quit.argtypes = []
self._lib.rust_widgets_quit.restype = None
self._lib.rust_widgets_create_window.argtypes = [c_char_p, c_int, c_int, c_uint, c_uint]
self._lib.rust_widgets_create_window.restype = c_uint64
self._lib.rust_widgets_create_button.argtypes = [c_uint64, c_char_p, c_int, c_int, c_uint, c_uint]
self._lib.rust_widgets_create_button.restype = c_uint64
self._lib.rust_widgets_set_widget_text.argtypes = [c_uint64, c_char_p]
self._lib.rust_widgets_set_widget_text.restype = None
self._lib.rust_widgets_get_widget_text.argtypes = [c_uint64]
self._lib.rust_widgets_get_widget_text.restype = c_void_p
self._lib.rust_widgets_free_string.argtypes = [c_char_p]
self._lib.rust_widgets_free_string.restype = None
self._lib.rust_widgets_platform_capabilities.argtypes = []
self._lib.rust_widgets_platform_capabilities.restype = c_uint
self._lib.rust_widgets_platform_dpi_scale_factor.argtypes = []
self._lib.rust_widgets_platform_dpi_scale_factor.restype = c_float
self._lib.rust_widgets_set_render_aa_samples_per_axis.argtypes = [c_uint]
self._lib.rust_widgets_set_render_aa_samples_per_axis.restype = c_uint
self._lib.rust_widgets_get_render_aa_samples_per_axis.argtypes = []
self._lib.rust_widgets_get_render_aa_samples_per_axis.restype = c_uint
self._lib.rust_widgets_set_embedded_target_fps.argtypes = [c_uint]
self._lib.rust_widgets_set_embedded_target_fps.restype = c_uint
self._lib.rust_widgets_get_embedded_target_fps.argtypes = []
self._lib.rust_widgets_get_embedded_target_fps.restype = c_uint
self._lib.rust_widgets_submit_embedded_noop_task.argtypes = [c_char_p]
self._lib.rust_widgets_submit_embedded_noop_task.restype = c_uint64
self._lib.rust_widgets_embedded_engine_is_initialized.argtypes = []
self._lib.rust_widgets_embedded_engine_is_initialized.restype = c_uint
self._lib.rust_widgets_embedded_engine_is_running.argtypes = []
self._lib.rust_widgets_embedded_engine_is_running.restype = c_uint
self._lib.rust_widgets_embedded_engine_frame_count.argtypes = []
self._lib.rust_widgets_embedded_engine_frame_count.restype = c_uint64
self._lib.rust_widgets_embedded_engine_pending_task_count.argtypes = []
self._lib.rust_widgets_embedded_engine_pending_task_count.restype = c_uint64
self._lib.rust_widgets_embedded_engine_window_count.argtypes = []
self._lib.rust_widgets_embedded_engine_window_count.restype = c_uint64
self._lib.rust_widgets_embedded_engine_button_count.argtypes = []
self._lib.rust_widgets_embedded_engine_button_count.restype = c_uint64
self._lib.rust_widgets_platform_capability_contract.argtypes = [c_uint]
self._lib.rust_widgets_platform_capability_contract.restype = c_uint
self._lib.rust_widgets_bindings_api_version.argtypes = []
self._lib.rust_widgets_bindings_api_version.restype = c_uint
self._lib.rust_widgets_python_binding_status.argtypes = []
self._lib.rust_widgets_python_binding_status.restype = c_uint
self._lib.rust_widgets_cpp_binding_status.argtypes = []
self._lib.rust_widgets_cpp_binding_status.restype = c_uint
self._lib.rust_widgets_java_binding_status.argtypes = []
self._lib.rust_widgets_java_binding_status.restype = c_uint
self._lib.rust_widgets_java_jni_skeleton_version.argtypes = []
self._lib.rust_widgets_java_jni_skeleton_version.restype = c_uint
def init(self) -> None:
self._lib.rust_widgets_init()
def run(self) -> None:
self._lib.rust_widgets_run()
def quit(self) -> None:
self._lib.rust_widgets_quit()
def create_window(self, title: str, x: int, y: int, width: int, height: int) -> int:
return int(self._lib.rust_widgets_create_window(title.encode("utf-8"), x, y, width, height))
def create_button(self, parent: int, text: str, x: int, y: int, width: int, height: int) -> int:
return int(self._lib.rust_widgets_create_button(parent, text.encode("utf-8"), x, y, width, height))
def set_widget_text(self, widget_id: int, text: str) -> None:
self._lib.rust_widgets_set_widget_text(widget_id, text.encode("utf-8"))
def get_widget_text(self, widget_id: int) -> str:
ptr = self._lib.rust_widgets_get_widget_text(widget_id)
if not ptr:
return ""
value = ctypes.string_at(ptr).decode("utf-8")
self._lib.rust_widgets_free_string(ctypes.cast(ptr, c_char_p))
return value
def platform_capabilities(self) -> int:
return int(self._lib.rust_widgets_platform_capabilities())
def dpi_scale_factor(self) -> float:
return float(self._lib.rust_widgets_platform_dpi_scale_factor())
def set_render_aa_samples_per_axis(self, samples: int) -> int:
return int(self._lib.rust_widgets_set_render_aa_samples_per_axis(samples))
def render_aa_samples_per_axis(self) -> int:
return int(self._lib.rust_widgets_get_render_aa_samples_per_axis())
def set_embedded_target_fps(self, fps: int) -> int:
return int(self._lib.rust_widgets_set_embedded_target_fps(fps))
def embedded_target_fps(self) -> int:
return int(self._lib.rust_widgets_get_embedded_target_fps())
def submit_embedded_noop_task(self, label: str = "python-noop") -> int:
return int(self._lib.rust_widgets_submit_embedded_noop_task(label.encode("utf-8")))
def embedded_engine_is_initialized(self) -> bool:
return bool(self._lib.rust_widgets_embedded_engine_is_initialized())
def embedded_engine_is_running(self) -> bool:
return bool(self._lib.rust_widgets_embedded_engine_is_running())
def embedded_engine_frame_count(self) -> int:
return int(self._lib.rust_widgets_embedded_engine_frame_count())
def embedded_engine_pending_task_count(self) -> int:
return int(self._lib.rust_widgets_embedded_engine_pending_task_count())
def embedded_engine_window_count(self) -> int:
return int(self._lib.rust_widgets_embedded_engine_window_count())
def embedded_engine_button_count(self) -> int:
return int(self._lib.rust_widgets_embedded_engine_button_count())
def capability_contract(self, embedded: bool = False) -> int:
profile_code = 1 if embedded else 0
return int(self._lib.rust_widgets_platform_capability_contract(profile_code))
def bindings_api_version(self) -> int:
return int(self._lib.rust_widgets_bindings_api_version())
def python_binding_status(self) -> int:
return int(self._lib.rust_widgets_python_binding_status())
def cpp_binding_status(self) -> int:
return int(self._lib.rust_widgets_cpp_binding_status())
def java_binding_status(self) -> int:
return int(self._lib.rust_widgets_java_binding_status())
def java_jni_skeleton_version(self) -> int:
return int(self._lib.rust_widgets_java_jni_skeleton_version())