@bake-native/core 0.0.0 → 0.1.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/THIRD_PARTY_NOTICES.txt +548 -0
- package/bake-prelude.js +189 -0
- package/batch-contract.json +21 -0
- package/batch-wire.js +674 -0
- package/compat.json +64 -0
- package/controls.css +108 -0
- package/determinism.js +175 -0
- package/devtools.d.ts +13 -0
- package/devtools.js +73 -0
- package/dialogs.js +59 -0
- package/direct-style.js +105 -0
- package/direct-table.js +261 -0
- package/dist/bake.mjs +2211 -0
- package/dist/build-CMsvCcZ0.mjs +1 -0
- package/dist/build-CkLSZw06.mjs +5514 -0
- package/dist/build-tools-Dxyl6XAf.mjs +80 -0
- package/dist/client-Cm7o6dQd.mjs +17544 -0
- package/dist/compiler.mjs +4 -0
- package/dist/config-B1W8gfZY.mjs +12 -0
- package/dist/dev-Cskd8Hl4.mjs +986 -0
- package/dist/direct-report-Bdda-k_Y.mjs +153 -0
- package/dist/direct-report-Du44nNWl.mjs +1 -0
- package/dist/index.d.mts +176 -0
- package/dist/index.mjs +1 -0
- package/dist/intl-ROnAp2qO.mjs +75 -0
- package/dist/jiti-stub-CKcaShwE.mjs +5 -0
- package/dist/native-views-BuzB9x5H.mjs +7849 -0
- package/dist/octane/package.json +5 -0
- package/dist/package-KkUSMoji.mjs +2784 -0
- package/dist/react/package.json +13 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/dist/run-D2aPm54a.mjs +6632 -0
- package/dist/scope-8W2aIuQ9.mjs +262 -0
- package/dist/screens/BakeNativeScreen.native.ts +9 -0
- package/dist/screens/package.json +5 -0
- package/dom-exception.js +31 -0
- package/element-api.js +158 -0
- package/first-screen.js +30 -0
- package/fonts/Geist-Italic-Variable.ttf +0 -0
- package/fonts/Geist-Variable.ttf +0 -0
- package/fonts/GeistMono-Italic-Variable.ttf +0 -0
- package/fonts/GeistMono-Variable.ttf +0 -0
- package/fonts/OFL.txt +93 -0
- package/fonts/README.md +27 -0
- package/fonts/geist-italic.css +8 -0
- package/fonts/geist-mono-italic.css +8 -0
- package/fonts/geist-mono.css +8 -0
- package/fonts/geist.css +8 -0
- package/header-icons.json +53 -0
- package/host-runtime.js +508 -0
- package/image-props.js +17 -0
- package/index.d.ts +8 -0
- package/index.js +7 -0
- package/licenses/Apache-2.0.txt +73 -0
- package/locale-entry.d.ts +1 -0
- package/locale-entry.js +1 -0
- package/locale.d.ts +28 -0
- package/locale.js +124 -0
- package/markup-support.js +87 -0
- package/media-element.js +115 -0
- package/native-args-entry.js +2 -0
- package/native-args.d.ts +7 -0
- package/native-args.js +155 -0
- package/native-entry.js +1 -0
- package/native-globals.d.ts +156 -0
- package/native-scenarios.d.ts +17 -0
- package/native-scenarios.js +46 -0
- package/native-support.json +349 -0
- package/native.d.ts +52 -0
- package/native.js +237 -0
- package/package.json +142 -4
- package/page-elements.js +123 -0
- package/runtime-provides.json +412 -0
- package/safe-area.d.ts +1 -0
- package/safe-area.js +44 -0
- package/swift-ui/modifiers.js +164 -0
- package/tab-icons.js +62 -0
- package/templates/create/tabs/AGENTS.md +22 -0
- package/templates/create/tabs/README.md +43 -0
- package/templates/create/tabs/_gitignore +13 -0
- package/templates/create/tabs/android/app/build.gradle.kts +79 -0
- package/templates/create/tabs/android/app/src/main/AndroidManifest.xml +39 -0
- package/templates/create/tabs/android/app/src/main/kotlin/MainActivity.kt +35 -0
- package/templates/create/tabs/android/app/src/main/res/values/themes.xml +8 -0
- package/templates/create/tabs/android/build.gradle.kts +5 -0
- package/templates/create/tabs/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/templates/create/tabs/android/gradle/wrapper/gradle-wrapper.properties +9 -0
- package/templates/create/tabs/android/gradle.properties +3 -0
- package/templates/create/tabs/android/gradlew +248 -0
- package/templates/create/tabs/android/settings.gradle.kts +47 -0
- package/templates/create/tabs/bake.config.ts +10 -0
- package/templates/create/tabs/ios/App/App.swift +30 -0
- package/templates/create/tabs/ios/build.sh +23 -0
- package/templates/create/tabs/ios/project.yml +57 -0
- package/templates/create/tabs/macos/Package.swift +24 -0
- package/templates/create/tabs/macos/Sources/__SWIFT_NAME__/App.swift +146 -0
- package/templates/create/tabs/macos/build.sh +4 -0
- package/templates/create/tabs/package.json +23 -0
- package/templates/create/tabs/pnpm-workspace.yaml +6 -0
- package/templates/create/tabs/src/App.tsrx +92 -0
- package/templates/create/tabs/src/App.tsx +67 -0
- package/templates/create/tabs/src/styles.css +32 -0
- package/templates/create/tabs/tsconfig.json +5 -0
- package/templates/create/tabs/windows/Cargo.toml +29 -0
- package/templates/create/tabs/windows/build.rs +9 -0
- package/templates/create/tabs/windows/src/main.rs +52 -0
- package/templates/create/tabs/windows/src/shell.rs +316 -0
- package/templates/module/__NAME__.kt +20 -0
- package/templates/module/__NAME__.native.ts +24 -0
- package/templates/module/__NAME__.swift +25 -0
- package/templates/module/__NAME__.web.ts +13 -0
- package/templates/module/__NAME__Test.kt +26 -0
- package/templates/module/__NAME__Tests.swift +25 -0
- package/templates/module/__SNAKE__.rs +43 -0
- package/text.d.ts +20 -0
- package/text.js +100 -0
- package/transform-entry.js +1 -0
- package/transform.d.ts +15 -0
- package/transform.js +98 -0
- package/tsconfig.native.json +16 -0
- package/ui/scope.js +103 -0
- package/ui/swift.js +106 -0
- package/ui/ui.css +272 -0
- package/view-ref-writer.js +268 -0
- package/view-ref.d.ts +26 -0
- package/view-ref.js +85 -0
- package/views/Video.native.ts +68 -0
- package/views/generated/VideoViewAndroid.h +68 -0
- package/views/generated/VideoViewJSI.h +94 -0
- package/views/generated/VideoViewSpec.kt +113 -0
- package/views/generated/apple/VideoViewJSI/VideoViewJSIApple.cpp +40 -0
- package/views/generated/apple/VideoViewJSI/include/VideoViewJSIApple.h +28 -0
- package/views/generated/apple/VideoViewSpec/VideoViewInterop.swift +39 -0
- package/views/generated/apple/VideoViewSpec/VideoViewSpec.swift +186 -0
- package/views/generated/video_view_spec.rs +189 -0
- package/views.d.ts +24 -0
- package/views.js +45 -0
- package/virtual-window.js +35 -0
- package/visibility.js +75 -0
- package/web-crypto.js +84 -0
- package/web-fetch.js +1071 -0
- package/README.md +0 -3
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
use serde::Deserialize;
|
|
2
|
+
use windows::Win32::Foundation::*;
|
|
3
|
+
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
|
|
4
|
+
use windows::Win32::UI::HiDpi::{AdjustWindowRectExForDpi, GetDpiForWindow};
|
|
5
|
+
use windows::Win32::UI::Shell::*;
|
|
6
|
+
use windows::Win32::UI::WindowsAndMessaging::*;
|
|
7
|
+
use windows::core::{Error, HSTRING, PCWSTR, Result, w};
|
|
8
|
+
|
|
9
|
+
const TRAY_MESSAGE: u32 = WM_APP + 1;
|
|
10
|
+
const NIN_KEYSELECT: u32 = NIN_SELECT + 1;
|
|
11
|
+
|
|
12
|
+
#[derive(Deserialize)]
|
|
13
|
+
#[serde(rename_all = "camelCase")]
|
|
14
|
+
pub struct Config {
|
|
15
|
+
pub name: String,
|
|
16
|
+
pub app_id: String,
|
|
17
|
+
pub width: i32,
|
|
18
|
+
pub height: i32,
|
|
19
|
+
pub window: Window,
|
|
20
|
+
tray: Option<Tray>,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
#[derive(Deserialize)]
|
|
24
|
+
#[serde(rename_all = "camelCase")]
|
|
25
|
+
pub struct Window {
|
|
26
|
+
pub start_visible: bool,
|
|
27
|
+
show_in_taskbar: bool,
|
|
28
|
+
pub always_on_top: bool,
|
|
29
|
+
title_bar: TitleBar,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
#[derive(Deserialize)]
|
|
33
|
+
#[serde(rename_all = "lowercase")]
|
|
34
|
+
enum TitleBar {
|
|
35
|
+
System,
|
|
36
|
+
Hidden,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
#[derive(Deserialize)]
|
|
40
|
+
struct Tray {
|
|
41
|
+
click: Click,
|
|
42
|
+
menu: Vec<MenuItem>,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#[derive(Deserialize)]
|
|
46
|
+
#[serde(rename_all = "lowercase")]
|
|
47
|
+
enum Click {
|
|
48
|
+
Menu,
|
|
49
|
+
Toggle,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#[derive(Deserialize)]
|
|
53
|
+
struct MenuItem {
|
|
54
|
+
label: String,
|
|
55
|
+
#[serde(flatten)]
|
|
56
|
+
action: Action,
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#[derive(Deserialize)]
|
|
60
|
+
#[serde(tag = "action", rename_all = "lowercase")]
|
|
61
|
+
enum Action {
|
|
62
|
+
Show,
|
|
63
|
+
Hide,
|
|
64
|
+
Toggle,
|
|
65
|
+
Quit,
|
|
66
|
+
App { id: String },
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
pub struct Shell {
|
|
70
|
+
// The subclass reads this fixed allocation until Shell drops or the window is destroyed.
|
|
71
|
+
_tray: Option<Box<TrayState>>,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
struct TrayState {
|
|
75
|
+
hwnd: HWND,
|
|
76
|
+
config: Tray,
|
|
77
|
+
always_on_top: bool,
|
|
78
|
+
icon: NOTIFYICONDATAW,
|
|
79
|
+
taskbar_created: u32,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
impl Shell {
|
|
83
|
+
pub fn attach(hwnd: HWND, config: Config) -> Result<Self> {
|
|
84
|
+
unsafe {
|
|
85
|
+
let mut client = RECT::default();
|
|
86
|
+
GetClientRect(hwnd, &mut client)?;
|
|
87
|
+
let mut style = WINDOW_STYLE(GetWindowLongW(hwnd, GWL_STYLE) as u32);
|
|
88
|
+
let mut extended = WINDOW_EX_STYLE(GetWindowLongW(hwnd, GWL_EXSTYLE) as u32);
|
|
89
|
+
if matches!(config.window.title_bar, TitleBar::Hidden) {
|
|
90
|
+
style &= !WS_CAPTION;
|
|
91
|
+
}
|
|
92
|
+
if config.window.show_in_taskbar {
|
|
93
|
+
extended |= WS_EX_APPWINDOW;
|
|
94
|
+
} else {
|
|
95
|
+
extended |= WS_EX_TOOLWINDOW;
|
|
96
|
+
}
|
|
97
|
+
SetWindowLongW(hwnd, GWL_STYLE, style.0 as i32);
|
|
98
|
+
SetWindowLongW(hwnd, GWL_EXSTYLE, extended.0 as i32);
|
|
99
|
+
AdjustWindowRectExForDpi(&mut client, style, false, extended, GetDpiForWindow(hwnd))?;
|
|
100
|
+
SetWindowPos(
|
|
101
|
+
hwnd,
|
|
102
|
+
None,
|
|
103
|
+
0,
|
|
104
|
+
0,
|
|
105
|
+
client.right - client.left,
|
|
106
|
+
client.bottom - client.top,
|
|
107
|
+
SWP_NOMOVE | SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_NOZORDER,
|
|
108
|
+
)?;
|
|
109
|
+
|
|
110
|
+
let instance = GetModuleHandleW(None)?;
|
|
111
|
+
let icon = LoadIconW(Some(instance.into()), PCWSTR(1 as *const u16));
|
|
112
|
+
if let Ok(icon) = icon {
|
|
113
|
+
for size in [ICON_BIG, ICON_SMALL] {
|
|
114
|
+
SendMessageW(
|
|
115
|
+
hwnd,
|
|
116
|
+
WM_SETICON,
|
|
117
|
+
Some(WPARAM(size as usize)),
|
|
118
|
+
Some(LPARAM(icon.0 as isize)),
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let Some(tray) = config.tray else {
|
|
124
|
+
return Ok(Self { _tray: None });
|
|
125
|
+
};
|
|
126
|
+
let mut notify = NOTIFYICONDATAW {
|
|
127
|
+
cbSize: size_of::<NOTIFYICONDATAW>() as u32,
|
|
128
|
+
hWnd: hwnd,
|
|
129
|
+
uID: 1,
|
|
130
|
+
uFlags: NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_SHOWTIP,
|
|
131
|
+
uCallbackMessage: TRAY_MESSAGE,
|
|
132
|
+
hIcon: icon.or_else(|_| LoadIconW(None, IDI_APPLICATION))?,
|
|
133
|
+
..Default::default()
|
|
134
|
+
};
|
|
135
|
+
for (slot, code) in notify
|
|
136
|
+
.szTip
|
|
137
|
+
.iter_mut()
|
|
138
|
+
.take(127)
|
|
139
|
+
.zip(config.name.encode_utf16())
|
|
140
|
+
{
|
|
141
|
+
*slot = code;
|
|
142
|
+
}
|
|
143
|
+
let state = Box::new(TrayState {
|
|
144
|
+
hwnd,
|
|
145
|
+
config: tray,
|
|
146
|
+
always_on_top: config.window.always_on_top,
|
|
147
|
+
icon: notify,
|
|
148
|
+
taskbar_created: RegisterWindowMessageW(w!("TaskbarCreated")),
|
|
149
|
+
});
|
|
150
|
+
if !SetWindowSubclass(
|
|
151
|
+
hwnd,
|
|
152
|
+
Some(tray_proc),
|
|
153
|
+
1,
|
|
154
|
+
(&*state as *const TrayState) as usize,
|
|
155
|
+
)
|
|
156
|
+
.as_bool()
|
|
157
|
+
{
|
|
158
|
+
return Err(Error::new(
|
|
159
|
+
E_FAIL,
|
|
160
|
+
"Could not attach the Windows tray handler",
|
|
161
|
+
));
|
|
162
|
+
}
|
|
163
|
+
state.add_icon()?;
|
|
164
|
+
Ok(Self { _tray: Some(state) })
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
pub fn apply_topmost(hwnd: HWND, always_on_top: bool) -> Result<()> {
|
|
170
|
+
if always_on_top {
|
|
171
|
+
// Changing the Z order with the non-client frame while the window is hidden
|
|
172
|
+
// can leave it outside the topmost band. Apply it once the window is visible.
|
|
173
|
+
unsafe {
|
|
174
|
+
SetWindowPos(
|
|
175
|
+
hwnd,
|
|
176
|
+
Some(HWND_TOPMOST),
|
|
177
|
+
0,
|
|
178
|
+
0,
|
|
179
|
+
0,
|
|
180
|
+
0,
|
|
181
|
+
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE,
|
|
182
|
+
)?;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
Ok(())
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
impl TrayState {
|
|
189
|
+
unsafe fn add_icon(&self) -> Result<()> {
|
|
190
|
+
unsafe {
|
|
191
|
+
if !Shell_NotifyIconW(NIM_ADD, &self.icon).as_bool() {
|
|
192
|
+
return Err(Error::new(E_FAIL, "Could not add the Windows tray icon"));
|
|
193
|
+
}
|
|
194
|
+
let mut icon = self.icon;
|
|
195
|
+
icon.Anonymous.uVersion = NOTIFYICON_VERSION_4;
|
|
196
|
+
if !Shell_NotifyIconW(NIM_SETVERSION, &icon).as_bool() {
|
|
197
|
+
return Err(Error::new(
|
|
198
|
+
E_FAIL,
|
|
199
|
+
"Could not set the Windows tray icon version",
|
|
200
|
+
));
|
|
201
|
+
}
|
|
202
|
+
Ok(())
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
unsafe fn menu(&self, x: i32, y: i32) {
|
|
207
|
+
unsafe {
|
|
208
|
+
let Ok(menu) = CreatePopupMenu() else {
|
|
209
|
+
return;
|
|
210
|
+
};
|
|
211
|
+
for (i, item) in self.config.menu.iter().enumerate() {
|
|
212
|
+
let _ = AppendMenuW(
|
|
213
|
+
menu,
|
|
214
|
+
MF_STRING,
|
|
215
|
+
i + 1,
|
|
216
|
+
&HSTRING::from(item.label.replace('&', "&&")),
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
let _ = SetForegroundWindow(self.hwnd);
|
|
220
|
+
let command = TrackPopupMenu(
|
|
221
|
+
menu,
|
|
222
|
+
TPM_RETURNCMD | TPM_RIGHTBUTTON,
|
|
223
|
+
x,
|
|
224
|
+
y,
|
|
225
|
+
None,
|
|
226
|
+
self.hwnd,
|
|
227
|
+
None,
|
|
228
|
+
)
|
|
229
|
+
.0;
|
|
230
|
+
let _ = PostMessageW(Some(self.hwnd), WM_NULL, WPARAM(0), LPARAM(0));
|
|
231
|
+
let _ = DestroyMenu(menu);
|
|
232
|
+
if command > 0 {
|
|
233
|
+
if let Some(item) = self.config.menu.get(command as usize - 1) {
|
|
234
|
+
self.perform(&item.action);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
unsafe fn perform(&self, action: &Action) {
|
|
241
|
+
unsafe {
|
|
242
|
+
match action {
|
|
243
|
+
Action::Toggle
|
|
244
|
+
if IsWindowVisible(self.hwnd).as_bool() && !IsIconic(self.hwnd).as_bool() =>
|
|
245
|
+
{
|
|
246
|
+
let _ = ShowWindow(self.hwnd, SW_HIDE);
|
|
247
|
+
}
|
|
248
|
+
Action::Show | Action::Toggle => {
|
|
249
|
+
let _ = ShowWindow(
|
|
250
|
+
self.hwnd,
|
|
251
|
+
if IsIconic(self.hwnd).as_bool() {
|
|
252
|
+
SW_RESTORE
|
|
253
|
+
} else {
|
|
254
|
+
SW_SHOW
|
|
255
|
+
},
|
|
256
|
+
);
|
|
257
|
+
let _ = SetForegroundWindow(self.hwnd);
|
|
258
|
+
let _ = apply_topmost(self.hwnd, self.always_on_top);
|
|
259
|
+
}
|
|
260
|
+
Action::Hide => {
|
|
261
|
+
let _ = ShowWindow(self.hwnd, SW_HIDE);
|
|
262
|
+
}
|
|
263
|
+
Action::Quit => {
|
|
264
|
+
let _ = DestroyWindow(self.hwnd);
|
|
265
|
+
}
|
|
266
|
+
Action::App { id } => handle_tray_action(id),
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
impl Drop for TrayState {
|
|
273
|
+
fn drop(&mut self) {
|
|
274
|
+
unsafe {
|
|
275
|
+
let _ = RemoveWindowSubclass(self.hwnd, Some(tray_proc), 1);
|
|
276
|
+
let _ = Shell_NotifyIconW(NIM_DELETE, &self.icon);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
unsafe extern "system" fn tray_proc(
|
|
282
|
+
hwnd: HWND,
|
|
283
|
+
message: u32,
|
|
284
|
+
wparam: WPARAM,
|
|
285
|
+
lparam: LPARAM,
|
|
286
|
+
_: usize,
|
|
287
|
+
state: usize,
|
|
288
|
+
) -> LRESULT {
|
|
289
|
+
unsafe {
|
|
290
|
+
let state = &*(state as *const TrayState);
|
|
291
|
+
match message {
|
|
292
|
+
WM_CLOSE => state.perform(&Action::Hide),
|
|
293
|
+
TRAY_MESSAGE => {
|
|
294
|
+
let x = wparam.0 as i16 as i32;
|
|
295
|
+
let y = (wparam.0 >> 16) as i16 as i32;
|
|
296
|
+
match lparam.0 as u16 as u32 {
|
|
297
|
+
WM_CONTEXTMENU => state.menu(x, y),
|
|
298
|
+
NIN_SELECT | NIN_KEYSELECT => match state.config.click {
|
|
299
|
+
Click::Menu => state.menu(x, y),
|
|
300
|
+
Click::Toggle => state.perform(&Action::Toggle),
|
|
301
|
+
},
|
|
302
|
+
_ => {}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
_ if message == state.taskbar_created => {
|
|
306
|
+
let _ = state.add_icon();
|
|
307
|
+
}
|
|
308
|
+
_ => return DefSubclassProc(hwnd, message, wparam, lparam),
|
|
309
|
+
}
|
|
310
|
+
LRESULT(0)
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
fn handle_tray_action(_id: &str) {
|
|
315
|
+
// Match your app.desktop.tray.menu IDs here. This runs even while the window is hidden.
|
|
316
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// __NAME__ on Android. Spec: __NAME__.native.ts next to this file; bake build generates
|
|
2
|
+
// __NAME__Spec and registers the module (.bake/native/android). A constructor that takes the
|
|
3
|
+
// view's Context gets it: `class __NAME__Module(private val context: Context)`.
|
|
4
|
+
//
|
|
5
|
+
// Threads: sync members run on the JS thread (post UI work to the main thread, never wait for it);
|
|
6
|
+
// suspend members run on a background thread (withContext(Dispatchers.Main) for UI work).
|
|
7
|
+
package __PACKAGE__
|
|
8
|
+
|
|
9
|
+
import dev.bakenative.generated.__NAME__Spec
|
|
10
|
+
|
|
11
|
+
class __NAME__Module : __NAME__Spec() {
|
|
12
|
+
override val version = "1.0"
|
|
13
|
+
|
|
14
|
+
override fun hello(name: String): String = "Hello, $name"
|
|
15
|
+
|
|
16
|
+
override suspend fun later(ms: Double, value: String): String {
|
|
17
|
+
Thread.sleep(ms.toLong().coerceAtLeast(0))
|
|
18
|
+
return value
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// __NAME__: the spec, the one source of truth for the implementations next to it. Write it first
|
|
2
|
+
// (names, JSON-like types, sync or async, error codes), then one implementation per target, then
|
|
3
|
+
// tests. bake build generates each language's contract (__NAME__Spec) from it.
|
|
4
|
+
// docs/design/native-module-specs.md
|
|
5
|
+
//
|
|
6
|
+
// import { native } from '@bake-native/core';
|
|
7
|
+
// native.__NAME__.hello('Ada'); // 'Hello, Ada'
|
|
8
|
+
// await native.__NAME__.later(250, 'done'); // 'done'
|
|
9
|
+
import type { NativeModule } from '@bake-native/core';
|
|
10
|
+
|
|
11
|
+
export interface __NAME__ extends NativeModule {
|
|
12
|
+
/** A constant, read once when the module is installed. */
|
|
13
|
+
readonly version: string;
|
|
14
|
+
/** Sync: runs on the JS thread and returns to JS directly. */
|
|
15
|
+
hello(name: string): string;
|
|
16
|
+
/** Async: returns a Promise; the implementation runs off the JS thread. */
|
|
17
|
+
later(ms: number, value: string): Promise<string>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare module '@bake-native/core' {
|
|
21
|
+
interface NativeModules {
|
|
22
|
+
__NAME__: __NAME__;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// __NAME__ on iOS and macOS. Spec: __NAME__.native.ts next to this file; bake build generates
|
|
2
|
+
// __NAME__Spec and registers the module (.bake/native/<target>). Split into __NAME__.ios.swift and
|
|
3
|
+
// __NAME__.mac.swift when UIKit and AppKit differ.
|
|
4
|
+
//
|
|
5
|
+
// Threads: sync members run on the JS thread (queue UIKit / AppKit work on main, never wait for
|
|
6
|
+
// it); async members run on a background task, or on main when marked @MainActor.
|
|
7
|
+
import Foundation
|
|
8
|
+
#if canImport(BakeNativeKit)
|
|
9
|
+
import BakeNativeKit
|
|
10
|
+
#elseif canImport(BakeNativeAppKit)
|
|
11
|
+
import BakeNativeAppKit
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
public final class __NAME__Module: __NAME__Spec {
|
|
15
|
+
public init() {}
|
|
16
|
+
|
|
17
|
+
public let version = "1.0"
|
|
18
|
+
|
|
19
|
+
public func hello(name: String) -> String { "Hello, \(name)" }
|
|
20
|
+
|
|
21
|
+
public func later(ms: Double, value: String) async throws -> String {
|
|
22
|
+
try await Task.sleep(nanoseconds: UInt64(max(0, ms) * 1_000_000))
|
|
23
|
+
return value
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// __NAME__ on the web, with the browser's own APIs. Spec: __NAME__.native.ts next to this file:
|
|
2
|
+
// the object is typed by it, so a member the spec has and this lacks is a type error. The web
|
|
3
|
+
// build loads it before the app.
|
|
4
|
+
import { registerWebModule } from '@bake-native/core';
|
|
5
|
+
import type { __NAME__ } from './__NAME__.native';
|
|
6
|
+
|
|
7
|
+
const web: __NAME__ = {
|
|
8
|
+
version: '1.0',
|
|
9
|
+
hello: (name) => `Hello, ${name}`,
|
|
10
|
+
later: (ms, value) => new Promise((resolve) => setTimeout(() => resolve(value), Math.max(ms, 0))),
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
registerWebModule('__NAME__', web);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
@file:OptIn(dev.bakenative.BakeNativeTestingApi::class)
|
|
2
|
+
|
|
3
|
+
package __PACKAGE__
|
|
4
|
+
|
|
5
|
+
import dev.bakenative.modules.BakeNativeModuleError
|
|
6
|
+
import dev.bakenative.modules.invoke
|
|
7
|
+
import dev.bakenative.modules.invokeAsync
|
|
8
|
+
import org.junit.Assert.assertEquals
|
|
9
|
+
import org.junit.Assert.assertThrows
|
|
10
|
+
import org.junit.Test
|
|
11
|
+
|
|
12
|
+
/** Members run without a JS runtime through `invoke` / `invokeAsync`, with the conversions and errors JS gets. */
|
|
13
|
+
class __NAME__Test {
|
|
14
|
+
@Test fun hello() {
|
|
15
|
+
assertEquals("Hello, Ada", __NAME__Module().invoke("hello", "Ada"))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@Test fun wrongArgumentType() {
|
|
19
|
+
val e = assertThrows(BakeNativeModuleError::class.java) { __NAME__Module().invoke("hello", 42.0) }
|
|
20
|
+
assertEquals("E_ARGUMENT", e.code)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Test fun later() {
|
|
24
|
+
assertEquals("done", __NAME__Module().invokeAsync("later", 5.0, "done"))
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import XCTest
|
|
2
|
+
#if canImport(BakeNativeKit)
|
|
3
|
+
import BakeNativeKit
|
|
4
|
+
#elseif canImport(BakeNativeAppKit)
|
|
5
|
+
import BakeNativeAppKit
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
/// Members run without a JS runtime through `invoke` / `invokeAsync`, with the conversions and
|
|
9
|
+
/// errors JS gets.
|
|
10
|
+
final class __NAME__Tests: XCTestCase {
|
|
11
|
+
func testHello() throws {
|
|
12
|
+
XCTAssertEqual(try __NAME__Module().invoke("hello", ["Ada"]) as? String, "Hello, Ada")
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
func testWrongArgumentType() {
|
|
16
|
+
XCTAssertThrowsError(try __NAME__Module().invoke("hello", [42.0])) { error in
|
|
17
|
+
XCTAssertEqual((error as? BakeNativeModuleError)?.code, "E_ARGUMENT")
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
func testLater() async throws {
|
|
22
|
+
let value = try await __NAME__Module().invokeAsync("later", [5.0, "done"])
|
|
23
|
+
XCTAssertEqual(value as? String, "done")
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// __NAME__ on Windows. Spec: __NAME__.native.ts next to this file; bake build generates
|
|
2
|
+
// __NAME__Spec and makes this file a module of the generated crate (.bake/native/windows): one file,
|
|
3
|
+
// `__NAME__Module` with `Default` (its fields are what it needs).
|
|
4
|
+
//
|
|
5
|
+
// Threads: sync members run on the JS thread (post window work to the UI thread, never wait for
|
|
6
|
+
// it); async members (a Promise in the spec) run on a worker thread.
|
|
7
|
+
|
|
8
|
+
use crate::__SNAKE___spec::*;
|
|
9
|
+
use bake_runtime::modules::ModuleError;
|
|
10
|
+
|
|
11
|
+
#[derive(Default)]
|
|
12
|
+
pub struct __NAME__Module;
|
|
13
|
+
|
|
14
|
+
impl __NAME__Spec for __NAME__Module {
|
|
15
|
+
fn version(&self) -> String {
|
|
16
|
+
"1.0".into()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fn hello(&self, name: String) -> Result<String, ModuleError> {
|
|
20
|
+
Ok(format!("Hello, {name}"))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fn later(&self, ms: f64, value: String) -> Result<String, ModuleError> {
|
|
24
|
+
std::thread::sleep(std::time::Duration::from_secs_f64(ms.max(0.0) / 1000.0));
|
|
25
|
+
Ok(value)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
#[cfg(test)]
|
|
30
|
+
mod tests {
|
|
31
|
+
use super::*;
|
|
32
|
+
use serde_json::json;
|
|
33
|
+
|
|
34
|
+
/// Members run without a JS runtime through `invoke` / `invoke_async`, with the conversions
|
|
35
|
+
/// and errors JS gets.
|
|
36
|
+
#[test]
|
|
37
|
+
fn hello() {
|
|
38
|
+
let m = __SNAKE___module(__NAME__Module);
|
|
39
|
+
assert_eq!(m.invoke("hello", &[json!("Ada")]).unwrap(), json!("Hello, Ada"));
|
|
40
|
+
assert_eq!(m.invoke("hello", &[json!(42)]).unwrap_err().code, "E_ARGUMENT");
|
|
41
|
+
assert_eq!(m.invoke_async("later", &[json!(5), json!("done")]).unwrap(), json!("done"));
|
|
42
|
+
}
|
|
43
|
+
}
|
package/text.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Width in px of `text` on one line in CSS `font` (a size then a family: `16px Inter`). Throws for a font the `font` shorthand rejects. */
|
|
2
|
+
export function measureText(text: string, font: string): number;
|
|
3
|
+
|
|
4
|
+
/** A Canvas-2D-shaped measuring context (measuring only, nothing draws). */
|
|
5
|
+
export interface MeasureContext {
|
|
6
|
+
/** CSS `font`; a string the `font` shorthand rejects is ignored, as Canvas does. */
|
|
7
|
+
font: string;
|
|
8
|
+
/** Accepted for Canvas compatibility; has no effect. */
|
|
9
|
+
fontKerning: string;
|
|
10
|
+
measureText(text: string): { width: number };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createMeasureContext(): MeasureContext;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Defines `globalThis.OffscreenCanvas` on native targets as a measuring-only canvas (its
|
|
17
|
+
* `getContext('2d')` is a {@link MeasureContext}; other types throw). Nothing on the web. Returns
|
|
18
|
+
* a function that removes it.
|
|
19
|
+
*/
|
|
20
|
+
export function installMeasureOnlyCanvas(): () => void;
|
package/text.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Text measurement for layout libraries (Pretext and the like): the width of a string on one line
|
|
2
|
+
// in a CSS `font`, from the same text system that lays out and draws the app's text.
|
|
3
|
+
//
|
|
4
|
+
// import { measureText, createMeasureContext, installMeasureOnlyCanvas } from '@bake-native/core';
|
|
5
|
+
// measureText('Hello', '600 15px system-ui'); // px, synchronous
|
|
6
|
+
// const ctx = createMeasureContext(); // { font, measureText(text) → { width } }
|
|
7
|
+
// installMeasureOnlyCanvas(); // OffscreenCanvas for libraries that ask for one
|
|
8
|
+
//
|
|
9
|
+
// Native: `__bakeMeasureText(font, text)`, a JSI function of the core (bake-runtime text_measure.rs)
|
|
10
|
+
// over the installed text backend (CoreText on Apple), so a line a library builds from these widths
|
|
11
|
+
// is the line the app draws for the same text in a `white-space: pre` element. Web: Canvas 2D
|
|
12
|
+
// measureText, which is what the browser draws with.
|
|
13
|
+
//
|
|
14
|
+
// Measuring only. There is no canvas on native targets: `installMeasureOnlyCanvas` gives an
|
|
15
|
+
// `OffscreenCanvas` whose `getContext('2d')` has `font`, `fontKerning` (no effect) and
|
|
16
|
+
// `measureText`, and nothing that draws; any other context type throws. Measure in effects and
|
|
17
|
+
// handlers, not in render: the build-time snapshot bake has no text system and throws.
|
|
18
|
+
|
|
19
|
+
const WEB = import.meta.env?.BAKE_TARGET === 'web';
|
|
20
|
+
|
|
21
|
+
function error(message, code) {
|
|
22
|
+
const err = new Error(message);
|
|
23
|
+
err.code = code;
|
|
24
|
+
return err;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let webContext = null;
|
|
28
|
+
function webMeasure(text, font) {
|
|
29
|
+
if (!webContext) {
|
|
30
|
+
if (typeof OffscreenCanvas !== 'undefined') webContext = new OffscreenCanvas(1, 1).getContext('2d');
|
|
31
|
+
else if (typeof document !== 'undefined') webContext = document.createElement('canvas').getContext('2d');
|
|
32
|
+
if (!webContext) throw error('measureText needs Canvas 2D on the web', 'E_UNAVAILABLE');
|
|
33
|
+
}
|
|
34
|
+
if (webContext.font !== font) webContext.font = font;
|
|
35
|
+
return webContext.measureText(text).width;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function nativeMeasure(text, font) {
|
|
39
|
+
const measure = globalThis.__bakeMeasureText;
|
|
40
|
+
if (typeof measure !== 'function') {
|
|
41
|
+
throw error('measureText is not available in this runtime (a host whose core predates @bake-native/core, or a tool without one)', 'E_UNAVAILABLE');
|
|
42
|
+
}
|
|
43
|
+
return measure(font, text);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Width in px of `text` on one line in CSS `font` (a size then a family: `16px Inter`). Throws for a font the `font` shorthand rejects. */
|
|
47
|
+
export function measureText(text, font) {
|
|
48
|
+
return WEB ? webMeasure(String(text), String(font)) : nativeMeasure(String(text), String(font));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A Canvas-2D-shaped measuring context: `font` (a string the `font` shorthand rejects is ignored
|
|
53
|
+
* and the old font kept, as Canvas does) and `measureText(text)` → `{ width }`.
|
|
54
|
+
*/
|
|
55
|
+
export function createMeasureContext() {
|
|
56
|
+
let font = '10px sans-serif';
|
|
57
|
+
return {
|
|
58
|
+
get font() {
|
|
59
|
+
return font;
|
|
60
|
+
},
|
|
61
|
+
set font(value) {
|
|
62
|
+
const next = String(value);
|
|
63
|
+
try {
|
|
64
|
+
measureText('', next);
|
|
65
|
+
font = next;
|
|
66
|
+
} catch (e) {
|
|
67
|
+
if (e?.code === 'E_UNAVAILABLE') throw e;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
fontKerning: 'auto',
|
|
71
|
+
measureText(text) {
|
|
72
|
+
return { width: measureText(text, font) };
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Defines `globalThis.OffscreenCanvas` on native targets as a measuring-only canvas, for libraries
|
|
79
|
+
* that create their own Canvas context to measure text (Pretext). Nothing on the web, or where an
|
|
80
|
+
* OffscreenCanvas exists. Returns a function that removes it again.
|
|
81
|
+
*/
|
|
82
|
+
export function installMeasureOnlyCanvas() {
|
|
83
|
+
if (WEB || typeof globalThis.OffscreenCanvas !== 'undefined') return () => {};
|
|
84
|
+
class MeasureOnlyCanvas {
|
|
85
|
+
constructor(width = 0, height = 0) {
|
|
86
|
+
this.width = width;
|
|
87
|
+
this.height = height;
|
|
88
|
+
}
|
|
89
|
+
getContext(type) {
|
|
90
|
+
if (type !== '2d') {
|
|
91
|
+
throw error(`OffscreenCanvas.getContext('${type}') is not supported on native targets: @bake-native/core measures text only`, 'E_UNSUPPORTED');
|
|
92
|
+
}
|
|
93
|
+
return createMeasureContext();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
globalThis.OffscreenCanvas = MeasureOnlyCanvas;
|
|
97
|
+
return () => {
|
|
98
|
+
if (globalThis.OffscreenCanvas === MeasureOnlyCanvas) delete globalThis.OffscreenCanvas;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Transform, transform } from './transform.js';
|
package/transform.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** A `style.transform` value built from numbers; its CSS text wherever a string is expected. */
|
|
2
|
+
export declare class Transform {
|
|
3
|
+
/** Appends `translate(<x>px, <y>px)`. */
|
|
4
|
+
translate(x: number, y?: number): this;
|
|
5
|
+
/** Appends `scale(<x>)`, or `scale(<x>, <y>)` when they differ. */
|
|
6
|
+
scale(x: number, y?: number): this;
|
|
7
|
+
/** Appends `rotate(<deg>deg)`. */
|
|
8
|
+
rotate(deg: number): this;
|
|
9
|
+
/** The CSS text (`none` without functions). */
|
|
10
|
+
toString(): string;
|
|
11
|
+
toJSON(): string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A new, empty transform (`none`); chain `translate` / `rotate` / `scale` onto it. */
|
|
15
|
+
export declare function transform(): Transform;
|