makepad-platform 1.0.0

Makepad platform layer
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use {
    std::cell::Cell,
    crate::{
        makepad_live_id::*,
        makepad_wasm_bridge::*,
        makepad_math::{dvec2, DVec2},
        cx::{OsType, XrCapabilities, WebParams},
        window::CxWindowPool,
        area::Area,
        //midi::{MidiData},
        event::{
            KeyCode,
            KeyModifiers,
            MouseButton,
            MouseDownEvent,
            MouseMoveEvent,
            MouseUpEvent,
            TouchPoint,
            TouchState,
            TouchUpdateEvent,
            ScrollEvent,
            KeyEvent,
            TextInputEvent,
            WindowGeom
        },
    }
};

#[derive(ToWasm)]
pub struct WGpuInfo {
    pub min_uniform_vectors: u32,
    pub vendor: String,
    pub renderer: String
}

#[derive(ToWasm)]
pub struct WBrowserInfo {
    pub protocol: String,
    pub hostname: String,
    pub host: String,
    pub pathname: String,
    pub search: String,
    pub hash: String,
    pub has_thread_support: bool,
}

impl Into<OsType> for WBrowserInfo {
    fn into(self) -> OsType {
        OsType::Web(WebParams{
            protocol: self.protocol,
            hostname: self.hostname,
            host: self.host,
            pathname: self.pathname,
            search: self.search,
            hash: self.hash,
        })
    }
}

#[derive(ToWasm)]
pub struct ToWasmGetDeps {
    pub gpu_info: WGpuInfo,
    pub cpu_cores: u32,
    pub xr_capabilities: WXrCapabilities,
    pub browser_info: WBrowserInfo,
}

#[derive(ToWasm)]
pub struct WDepLoaded {
    pub path: String,
    pub data: WasmDataU8
}

#[derive(ToWasm)]
pub struct WWindowInfo {
    pub is_fullscreen: bool,
    pub can_fullscreen: bool,
    pub xr_is_presenting: bool,
    pub vr_supported: bool,
    pub ar_supported: bool,
    pub dpi_factor: f64,
    pub inner_width: f64,
    pub inner_height: f64
}

impl Into<WindowGeom> for WWindowInfo {
    fn into(self) -> WindowGeom {
        WindowGeom {
            is_fullscreen: self.is_fullscreen,
            is_topmost: false,
            inner_size: DVec2 {x: self.inner_width, y: self.inner_height},
            dpi_factor: self.dpi_factor,
            outer_size: DVec2 {x: 0., y: 0.},
            position: DVec2 {x: 0., y: 0.},
            xr_is_presenting: self.xr_is_presenting,
            can_fullscreen: self.can_fullscreen
        }
    }
}

#[derive(ToWasm)]
pub struct WXrCapabilities {
    pub vr_supported: bool,
    pub ar_supported: bool,
}

impl Into<XrCapabilities> for WXrCapabilities {
    fn into(self) -> XrCapabilities {
        XrCapabilities {
            vr_supported: self.vr_supported,
            ar_supported: self.ar_supported,
        }
    }
}

#[derive(ToWasm)]
pub struct ToWasmInit {
    pub deps: Vec<WDepLoaded>,
    pub window_info: WWindowInfo
}

#[derive(ToWasm)]
pub struct ToWasmResizeWindow {
    pub window_info: WWindowInfo
}

#[derive(ToWasm)]
pub struct ToWasmAnimationFrame {
    pub time: f64
}

#[derive(ToWasm)]
pub struct ToWasmTimerFired {
    pub timer_id: usize
}

#[derive(ToWasm)]
pub struct ToWasmSignal {
    pub flags:u32
}

#[derive(ToWasm)]
pub struct ToWasmPaintDirty {
}

#[derive(ToWasm)]
pub struct ToWasmRedrawAll {}





// Touch API



#[derive(ToWasm, Clone, Debug)]
pub struct WTouchPoint {
    pub time: f64,
    pub state: u32, // 0 stable, 1 start, 2 end, 3 move
    pub x: f64,
    pub y: f64,
    pub radius_x: f64,
    pub radius_y: f64,
    pub rotation_angle: f64,
    pub force: f64,
    pub uid: u32,
}

impl From<WTouchPoint> for TouchPoint {
    fn from(v:WTouchPoint) -> Self {
        Self {
            abs: dvec2(v.x, v.y),
            state: match v.state{
                0=>TouchState::Stable,
                1=>TouchState::Start,
                2=>TouchState::Move,
                _=>TouchState::Stop,
            },
            time: v.time, 
            uid: v.uid as u64,
            force: v.force,
            radius: dvec2(v.radius_x, v.radius_y),
            rotation_angle: v.rotation_angle,
            sweep_lock: Cell::new(Area::Empty),
            handled: Cell::new(Area::Empty),
        }
    }
}

#[derive(ToWasm, Debug)]
pub struct ToWasmTouchUpdate {
    pub modifiers: u32,
    pub time: f64,
    pub touches: Vec<WTouchPoint>,
}

impl From<ToWasmTouchUpdate> for TouchUpdateEvent {
    fn from(v:ToWasmTouchUpdate) -> Self {
        Self {
            time: v.time,
            window_id: CxWindowPool::id_zero(),
            modifiers: unpack_key_modifier(v.modifiers),
            touches: v.touches.iter().map(|v| v.clone().into()).collect()
        }
    }
}

fn unpack_key_modifier(modifiers: u32) -> KeyModifiers {
    KeyModifiers {
        shift: (modifiers & 1) != 0,
        control: (modifiers & 2) != 0,
        alt: (modifiers & 4) != 0,
        logo: (modifiers & 8) != 0
    }
}


// Mouse API


#[derive(ToWasm)]
pub struct WMouse {
    pub x: f64,
    pub y: f64,
    pub modifiers: u32,
    pub button: u32,
    pub time: f64,
}

/// Convert a raw Web mouse button code to a Makepad `MouseButton`.
fn wmouse_to_mouse_button(button: u32) -> MouseButton {
    match button {
        0 => MouseButton::PRIMARY,
        1 => MouseButton::MIDDLE,
        2 => MouseButton::SECONDARY,
        n => MouseButton::from_raw_button(n as usize),
    }
}

#[derive(ToWasm)]
pub struct ToWasmMouseDown {
    pub mouse: WMouse,
}

impl From<ToWasmMouseDown> for MouseDownEvent {
    fn from(v:ToWasmMouseDown) -> Self {
        Self {
            abs: dvec2(v.mouse.x, v.mouse.y),
            button: wmouse_to_mouse_button(v.mouse.button),
            window_id: CxWindowPool::id_zero(),
            modifiers: unpack_key_modifier(v.mouse.modifiers),
            handled: Cell::new(Area::Empty),
            time: v.mouse.time
        }
    }
}

#[derive(ToWasm)]
pub struct ToWasmMouseMove {
    pub was_out: bool,
    pub mouse: WMouse,
}

impl From<ToWasmMouseMove> for MouseMoveEvent {
    fn from(v:ToWasmMouseMove) -> Self {
        Self {
            abs: dvec2(v.mouse.x, v.mouse.y),
            window_id: CxWindowPool::id_zero(),
            modifiers: unpack_key_modifier(v.mouse.modifiers),
            handled: Cell::new(Area::Empty),
            time: v.mouse.time
        }
    }
}


#[derive(ToWasm)]
pub struct ToWasmMouseUp {
    pub mouse: WMouse,
}

impl From<ToWasmMouseUp> for MouseUpEvent {
    fn from(v:ToWasmMouseUp) -> Self {
        Self {
            abs: dvec2(v.mouse.x, v.mouse.y),
            button: wmouse_to_mouse_button(v.mouse.button),
            window_id: CxWindowPool::id_zero(),
            modifiers: unpack_key_modifier(v.mouse.modifiers),
            time: v.mouse.time
        }
    }
}

// scroll

#[derive(ToWasm)]
pub struct ToWasmScroll {
    pub x: f64,
    pub y: f64,
    pub modifiers: u32,
    pub is_touch: bool,
    pub scroll_x: f64,
    pub scroll_y: f64,
    pub time: f64
}

impl From<ToWasmScroll> for ScrollEvent {
    fn from(v:ToWasmScroll) -> Self {
        Self {
            window_id: CxWindowPool::id_zero(),
            scroll: dvec2(v.scroll_x, v.scroll_y),
            abs: dvec2(v.x, v.y),
            modifiers: unpack_key_modifier(v.modifiers),
            handled_x: Cell::new(false),
            handled_y: Cell::new(false),
            is_mouse: true,
            time: v.time,
        }
    }
}

// Keyboard API


fn web_to_key_code(key_code: u32) -> KeyCode {
    match key_code {
        27 => KeyCode::Escape,
        192 => KeyCode::Backtick,
        48 => KeyCode::Key0,
        49 => KeyCode::Key1,
        50 => KeyCode::Key2,
        51 => KeyCode::Key3,
        52 => KeyCode::Key4,
        53 => KeyCode::Key5,
        54 => KeyCode::Key6,
        55 => KeyCode::Key7,
        56 => KeyCode::Key8,
        57 => KeyCode::Key9,
        173 => KeyCode::Minus,
        189 => KeyCode::Minus,
        61 => KeyCode::Equals,
        187 => KeyCode::Equals,
        
        8 => KeyCode::Backspace,
        9 => KeyCode::Tab,
        
        81 => KeyCode::KeyQ,
        87 => KeyCode::KeyW,
        69 => KeyCode::KeyE,
        82 => KeyCode::KeyR,
        84 => KeyCode::KeyT,
        89 => KeyCode::KeyY,
        85 => KeyCode::KeyU,
        73 => KeyCode::KeyI,
        79 => KeyCode::KeyO,
        80 => KeyCode::KeyP,
        219 => KeyCode::LBracket,
        221 => KeyCode::RBracket,
        13 => KeyCode::ReturnKey,
        
        65 => KeyCode::KeyA,
        83 => KeyCode::KeyS,
        68 => KeyCode::KeyD,
        70 => KeyCode::KeyF,
        71 => KeyCode::KeyG,
        72 => KeyCode::KeyH,
        74 => KeyCode::KeyJ,
        75 => KeyCode::KeyK,
        76 => KeyCode::KeyL,
        
        59 => KeyCode::Semicolon,
        186 => KeyCode::Semicolon,
        222 => KeyCode::Quote,
        220 => KeyCode::Backslash,
        
        90 => KeyCode::KeyZ,
        88 => KeyCode::KeyX,
        67 => KeyCode::KeyC,
        86 => KeyCode::KeyV,
        66 => KeyCode::KeyB,
        78 => KeyCode::KeyN,
        77 => KeyCode::KeyM,
        188 => KeyCode::Comma,
        190 => KeyCode::Period,
        191 => KeyCode::Slash,
        
        17 => KeyCode::Control,
        18 => KeyCode::Alt,
        16 => KeyCode::Shift,
        224 => KeyCode::Logo,
        91 => KeyCode::Logo,
        
        //RightControl,
        //RightShift,
        //RightAlt,
        93 => KeyCode::Logo,
        
        32 => KeyCode::Space,
        20 => KeyCode::Capslock,
        112 => KeyCode::F1,
        113 => KeyCode::F2,
        114 => KeyCode::F3,
        115 => KeyCode::F4,
        116 => KeyCode::F5,
        117 => KeyCode::F6,
        118 => KeyCode::F7,
        119 => KeyCode::F8,
        120 => KeyCode::F9,
        121 => KeyCode::F10,
        122 => KeyCode::F11,
        123 => KeyCode::F12,
        
        44 => KeyCode::PrintScreen,
        124 => KeyCode::PrintScreen,
        //Scrolllock,
        //Pause,
        
        45 => KeyCode::Insert,
        46 => KeyCode::Delete,
        36 => KeyCode::Home,
        35 => KeyCode::End,
        33 => KeyCode::PageUp,
        34 => KeyCode::PageDown,
        
        96 => KeyCode::Numpad0,
        97 => KeyCode::Numpad1,
        98 => KeyCode::Numpad2,
        99 => KeyCode::Numpad3,
        100 => KeyCode::Numpad4,
        101 => KeyCode::Numpad5,
        102 => KeyCode::Numpad6,
        103 => KeyCode::Numpad7,
        104 => KeyCode::Numpad8,
        105 => KeyCode::Numpad9,
        
        //NumpadEquals,
        109 => KeyCode::NumpadSubtract,
        107 => KeyCode::NumpadAdd,
        110 => KeyCode::NumpadDecimal,
        106 => KeyCode::NumpadMultiply,
        111 => KeyCode::NumpadDivide,
        12 => KeyCode::Numlock,
        //NumpadEnter,
        
        38 => KeyCode::ArrowUp,
        40 => KeyCode::ArrowDown,
        37 => KeyCode::ArrowLeft,
        39 => KeyCode::ArrowRight,
        _ => KeyCode::Unknown
    }
}

#[derive(ToWasm, Clone)]
pub struct WKey {
    pub char_code: u32,
    pub key_code: u32,
    pub modifiers: u32,
    pub time: f64,
    pub is_repeat: bool
}

impl Into<KeyEvent> for WKey {
    fn into(self) -> KeyEvent {
        KeyEvent {
            key_code: web_to_key_code(self.key_code),
            is_repeat: self.is_repeat,
            modifiers: unpack_key_modifier(self.modifiers),
            time: self.time,
        }
    }
}

#[derive(ToWasm)]
pub struct ToWasmKeyDown {
    pub key: WKey
}

#[derive(ToWasm)]
pub struct ToWasmKeyUp {
    pub key: WKey
}


#[derive(ToWasm)]
pub struct ToWasmTextInput {
    pub was_paste: bool,
    pub replace_last: bool,
    pub input: String,
}

impl Into<TextInputEvent> for ToWasmTextInput {
    fn into(self) -> TextInputEvent {
        TextInputEvent {
            was_paste: self.was_paste,
            replace_last: self.replace_last,
            input: self.input
        }
    }
}

#[derive(ToWasm)]
pub struct ToWasmTextCopy {
}

// Keyboard API


#[derive(ToWasm)]
pub struct ToWasmAppGotFocus {}

#[derive(ToWasm)]
pub struct ToWasmAppLostFocus {}

#[derive(ToWasm)]
pub struct ToWasmHTTPResponse {
    pub request_id_lo: u32,
    pub request_id_hi: u32,
    pub metadata_id_hi: u32,
    pub metadata_id_lo: u32,
    pub status: u32,
    pub headers: String,
    pub body: WasmDataU8
}

#[derive(ToWasm)]
pub struct ToWasmHttpRequestError {
    pub request_id_lo: u32,
    pub request_id_hi: u32,
    pub metadata_id_hi: u32,
    pub metadata_id_lo: u32,
    pub error: String
}

#[derive(ToWasm)]
pub struct ToWasmHttpResponseProgress {
    pub request_id_lo: u32,
    pub request_id_hi: u32,
    pub metadata_id_hi: u32,
    pub metadata_id_lo: u32,
    pub loaded: u32,
    pub total: u32
}

#[derive(ToWasm)]
pub struct ToWasmHttpUploadProgress {
    pub request_id_lo: u32,
    pub request_id_hi: u32,
    pub loaded: u32,
    pub total: u32
}
/*
#[derive(ToWasm)]
pub struct ToWasmWebSocketClose {
    pub request_id_lo: u32,
    pub request_id_hi: u32,
}

#[derive(ToWasm)]
pub struct ToWasmWebSocketOpen {
    pub request_id_lo: u32,
    pub request_id_hi: u32,
}

#[derive(ToWasm)]
pub struct ToWasmWebSocketError {
    pub request_id_lo: u32,
    pub request_id_hi: u32,
    pub error: String
}

#[derive(ToWasm)]
pub struct ToWasmWebSocketString {
    pub request_id_lo: u32,
    pub request_id_hi: u32,
    pub data: String
}

#[derive(ToWasm)]
pub struct ToWasmWebSocketBinary {
    pub request_id_lo: u32,
    pub request_id_hi: u32,
    pub data: WasmDataU8
}
*/
#[derive(ToWasm)]
pub struct ToWasmMidiInputData {
    pub uid: String,
    pub data: u32,
}

#[derive(ToWasm)]
pub struct WMidiPortInfo {
    pub name: String,
    pub uid: String,
    pub is_output: bool
}

#[derive(ToWasm)]
pub struct ToWasmMidiPortList {
    pub ports: Vec<WMidiPortInfo>,
}

#[derive(ToWasm)]
pub struct WAudioDevice {
    pub web_device_id: String,
    pub label: String,
    pub is_output: bool,
}

#[derive(ToWasm)]
pub struct ToWasmAudioDeviceList {
    pub devices: Vec<WAudioDevice>
}