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
#![allow(
    non_camel_case_types,
    non_upper_case_globals,
    non_snake_case,
    dead_code,
    unused_variables,
    unused_must_use
)]

pub extern crate ul_sys;

#[macro_use]
mod config_macros;

pub mod config;
pub mod settings;

pub mod helpers;

use helpers::{create_js_function, evaluate_script, set_js_object_property};

mod helpers_internal;
use helpers_internal::{
    unpack_window_resize_cb,
    unpack_window_close_cb,
    log_forward_cb,
    unpack_closure_view_cb,
};

mod cursor;

pub mod jsc;

use std::marker::PhantomData;
use std::os::raw::c_void;

pub type App = ul_sys::ULApp;
pub type Config = config::UltralightConfig;
pub type Settings = settings::UltralightSettings;
pub type Monitor = ul_sys::ULMonitor;
pub type Overlay = ul_sys::ULOverlay;
pub type Renderer = ul_sys::ULRenderer;
pub type View = ul_sys::ULView;
pub type Window = ul_sys::ULWindow;

pub type Cursor = cursor::Cursor;

#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
pub struct NoneError {}

/*
    Current flow

    initialize using
        - None as renderer
            -> initialize empty config, create new renderer
        - Some renderer
            -> initialize view using renderer

    Desired flow

    - Create clear API for instanciating an app from AppCore
      using a new UltralightApp (?) impl that initializes
      all requirements of an AppCore instance:

        - initialize empty config
        - initialize app
        - obtain monitor from app
            - uses macOS internal APIs to resolve "main" monitor,
              which always becomes the currently focused oned
        - create window monitor using
            width,
            height,
            fullscreen-flag,
            configuration bitmap:
                  borderless = 1 << 0,
                  titled = 1 << 1,
                  resizable = 1 << 2,
                  maximizable = 1 << 3,

                  0b0000
                    ^^^^
        - Configure app using created window on monitor
          via ulAppSetWindow
        - Obtain renderer from app via ulAppGetRenderer
*/

/*

    let config = ul_sys::ulCreateConfig(); -> config

    let app = ul_sys::ulCreateApp(config); -> app
    let monitor = ul_sys::ulAppGetMainMonitor(app); -> monitor

    let (width, height): (u32, u32) = (1280, 768);

    let window = ul_sys::ulCreateWindow(
        monitor, width, height, false, 0b0110
    ); -> window

    ul_sys::ulAppSetWindow(app, window); -> void

    let renderer = ul_sys::ulAppGetRenderer(app); -> renderer
*/

pub struct UltralightApp<'a> {
    config: Config,
    settings: Settings,
    app: App,
    // check if we really want to store
    // the monitor instance here as
    // in the future we might be able
    // to obtain a non-main monitor
    monitor: Monitor,
    overlay: Option<Overlay>,
    window: Option<Window>,

    phantom: PhantomData<&'a ()>,
}

impl<'a> UltralightApp<'a> {
    pub fn new(settings: Option<Settings>, config: Option<Config>) -> UltralightApp<'a> {
        let ulconfig = match config {
            Some(config) => config,
            None => Config::new(),
        };

        let ulsettings = match settings {
            Some(settings) => settings,
            None => Settings::new(),
        };

        unsafe {
            let app = ul_sys::ulCreateApp(
                ulsettings.to_ulsettings(),
                ulconfig.to_ulconfig(),
            );

            let monitor = ul_sys::ulAppGetMainMonitor(app);

            UltralightApp {
                config: ulconfig,
                settings: ulsettings,
                app,
                monitor,
                window: None,
                overlay: None,
                phantom: PhantomData,
            }
        }
    }

    pub fn window(
        &mut self,
        height: u32,
        width: u32,
        fullscreen: bool,
        borderless: bool,
        titled: bool,
        resizable: bool,
        maximizable: bool,
    ) {
        let mut window_flags = 0u32;

        if borderless {
            window_flags ^= 0b0001;
        }

        if titled {
            window_flags ^= 0b0010;
        }

        if resizable {
            window_flags ^= 0b0100;
        }

        if maximizable {
            window_flags ^= 0b1000;
        }

        let window =
            unsafe { ul_sys::ulCreateWindow(self.monitor, height, width, fullscreen, window_flags) };

        unsafe {
            ul_sys::ulAppSetWindow(self.app, window);
        }

        let overlay = unsafe { ul_sys::ulCreateOverlay(window, width, height, 0, 0) };

        self.window = Some(window);
        self.overlay = Some(overlay);
    }

    pub fn get_renderer(&mut self) -> Renderer {
        unsafe { ul_sys::ulAppGetRenderer(self.app) }
    }

    pub fn run(&mut self) {
        unsafe {
            ul_sys::ulAppRun(self.app);
        }
    }
}

pub trait UltralightAppOverlay {
    fn overlay_get_view(&mut self) -> Result<View, NoneError>;

    fn overlay_get_height(&mut self) -> Result<u32, NoneError>;
    fn overlay_get_width(&mut self) -> Result<u32, NoneError>;

    fn overlay_get_x(&mut self) -> Result<i32, NoneError>;
    fn overlay_get_y(&mut self) -> Result<i32, NoneError>;

    fn overlay_focus(&mut self) -> Result<(), NoneError>;
    fn overlay_unfocus(&mut self) -> Result<(), NoneError>;
    fn overlay_has_focus(&mut self) -> Result<bool, NoneError>;

    fn overlay_hide(&mut self) -> Result<(), NoneError>;
    fn overlay_is_hidden(&mut self) -> Result<bool, NoneError>;

    fn overlay_move_to(&self, x: i32, y: i32) -> Result<(), NoneError>;
    fn overlay_resize(&self, width: u32, height: u32) -> Result<(), NoneError>;
}

impl<'a> UltralightAppOverlay for UltralightApp<'a> {
    fn overlay_get_view(&mut self) -> Result<View, NoneError> {
        unsafe { Ok(ul_sys::ulOverlayGetView(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_get_height(&mut self) -> Result<u32, NoneError> {
        unsafe { Ok(ul_sys::ulOverlayGetHeight(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_get_width(&mut self) -> Result<u32, NoneError> {
        unsafe { Ok(ul_sys::ulOverlayGetWidth(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_get_x(&mut self) -> Result<i32, NoneError> {
        unsafe { Ok(ul_sys::ulOverlayGetX(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_get_y(&mut self) -> Result<i32, NoneError> {
        unsafe { Ok(ul_sys::ulOverlayGetY(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_focus(&mut self) -> Result<(), NoneError> {
        unsafe { Ok(ul_sys::ulOverlayFocus(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_unfocus(&mut self) -> Result<(), NoneError> {
        unsafe { Ok(ul_sys::ulOverlayUnfocus(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_has_focus(&mut self) -> Result<bool, NoneError> {
        unsafe { Ok(ul_sys::ulOverlayHasFocus(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_hide(&mut self) -> Result<(), NoneError> {
        unsafe { Ok(ul_sys::ulOverlayHide(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_is_hidden(&mut self) -> Result<bool, NoneError> {
        unsafe { Ok(ul_sys::ulOverlayIsHidden(self.overlay.ok_or(NoneError {})?)) }
    }

    fn overlay_move_to(&self, x: i32, y: i32) -> Result<(), NoneError> {
        unsafe {
            ul_sys::ulOverlayMoveTo(self.overlay.ok_or(NoneError {})?, x, y);
        }

        Ok(())
    }

    fn overlay_resize(&self, width: u32, height: u32) -> Result<(), NoneError> {
        unsafe {
            ul_sys::ulOverlayResize(self.overlay.ok_or(NoneError {})?, width, height);
        }

        Ok(())
    }
}

pub trait UltralightAppWindow {
    fn window_close(&self) -> Result<(), NoneError>;

    fn window_device_to_pixel(&self, val: i32) -> Result<i32, NoneError>;
    fn window_pixels_to_device(&self, val: i32) -> Result<i32, NoneError>;

    fn window_get_height(&self) -> Result<u32, NoneError>;
    fn window_get_width(&self) -> Result<u32, NoneError>;
    fn window_get_scale(&self) -> Result<f64, NoneError>;

    fn window_set_title(&mut self, title: &str) -> Result<(), NoneError>;
    fn window_set_cursor(&mut self, cursor: Cursor) -> Result<(), NoneError>;
}

impl<'a> UltralightAppWindow for UltralightApp<'a> {
    fn window_close(&self) -> Result<(), NoneError> {
        unsafe { Ok(ul_sys::ulWindowClose(self.window.ok_or(NoneError {})?)) }
    }

    fn window_device_to_pixel(&self, val: i32) -> Result<i32, NoneError> {
        unsafe { Ok(ul_sys::ulWindowDeviceToPixel(self.window.ok_or(NoneError {})?, val)) }
    }

    fn window_pixels_to_device(&self, val: i32) -> Result<i32, NoneError> {
        unsafe { Ok(ul_sys::ulWindowPixelsToDevice(self.window.ok_or(NoneError {})?, val)) }
    }

    fn window_get_height(&self) -> Result<u32, NoneError> {
        unsafe { Ok(ul_sys::ulWindowGetHeight(self.window.ok_or(NoneError {})?)) }
    }

    fn window_get_width(&self) -> Result<u32, NoneError> {
        unsafe { Ok(ul_sys::ulWindowGetWidth(self.window.ok_or(NoneError {})?)) }
    }

    fn window_get_scale(&self) -> Result<f64, NoneError> {
        unsafe { Ok(ul_sys::ulWindowGetScale(self.window.ok_or(NoneError {})?)) }
    }

    fn window_set_title(&mut self, title: &str) -> Result<(), NoneError> {
        unsafe {
            ul_sys::ulWindowSetTitle(
                self.window.ok_or(NoneError {})?,
                std::ffi::CString::new(title).unwrap().as_ptr(),
            );
        }

        Ok(())
    }

    fn window_set_cursor(&mut self, cursor: Cursor) -> Result<(), NoneError> {
        unsafe {
            Ok(ul_sys::ulWindowSetCursor(
                self.window.ok_or(NoneError {})?,
                cursor as u32
            ))
        }
    }
}

pub trait UltralightAppWindowCallbacks<'a> {
    fn window_set_close_callback<T>(&self, cb: &'a mut T) -> Result<(), NoneError>
        where T: FnMut();

    fn window_set_resize_callback<T>(&self, cb: &'a mut T) -> Result<(), NoneError>
        where T: FnMut(u32, u32);
}

impl<'a> UltralightAppWindowCallbacks<'a> for UltralightApp<'a> {
    fn window_set_close_callback<T>(&self, cb: &'a mut T) -> Result<(), NoneError>
        where T: FnMut(),
    {
        unsafe {
            let (cb_closure, cb_function) = unpack_window_close_cb(cb);

            Ok(ul_sys::ulWindowSetCloseCallback(
                self.window.ok_or(NoneError {})?,
                Some(cb_function),
                cb_closure,
            ))
        }
    }

    fn window_set_resize_callback<T>(&self, cb: &'a mut T) -> Result<(), NoneError>
        where T: FnMut(u32, u32),
    {
        unsafe {
            let (cb_closure, cb_function) = unpack_window_resize_cb(cb);

            Ok(ul_sys::ulWindowSetResizeCallback(
                self.window.ok_or(NoneError {})?,
                Some(cb_function),
                cb_closure,
            ))
        }
    }
}

pub struct Ultralight<'a> {
    config: Config,
    renderer: Renderer,
    view: Option<View>,

    phantom: PhantomData<&'a ()>,
}

impl<'a> Ultralight<'a> {
    pub fn new(config: Option<Config>, renderer: Option<Renderer>) -> Ultralight<'a> {
        let ulconfig = match config {
            Some(config) => config,
            None => Config::new(),
        };

        let used_renderer = match renderer {
            Some(renderer) => renderer,
            None => unsafe { ul_sys::ulCreateRenderer(ulconfig.to_ulconfig()) },
        };

        Ultralight {
            config: ulconfig,
            renderer: used_renderer,
            view: None,

            phantom: PhantomData,
        }
    }

    pub fn app(&mut self, app: &mut UltralightApp) -> Result<(), NoneError> {
        self.view = Some(app.overlay_get_view()?);

        Ok(())
    }

    pub fn set_view(&mut self, view: View) {
        self.view = Some(view);
    }

    pub fn view(&mut self, width: u32, height: u32, transparent: bool) {
        unsafe {
            self.view = Some(ul_sys::ulCreateView(self.renderer, width, height, transparent));
        }
    }

    pub fn load_url(&mut self, url: &'static str) -> Result<(), NoneError> {
        unsafe {
            let url_ulstr = helpers_internal::ul_string(url);

            ul_sys::ulViewLoadURL(self.view.ok_or(NoneError {})?, url_ulstr);
        }

        Ok(())
    }

    pub fn load_html(&mut self, code: &'static str) -> Result<(), NoneError> {
        unsafe {
            let code_ulstr = helpers_internal::ul_string(code);

            ul_sys::ulViewLoadHTML(self.view.ok_or(NoneError {})?, code_ulstr);
        }

        Ok(())
    }

    pub fn update(&mut self) {
        unsafe {
            ul_sys::ulUpdate(self.renderer);
        }
    }

    pub fn update_until_loaded(&mut self) -> Result<(), NoneError> {
        unsafe {
            while ul_sys::ulViewIsLoading(self.view.ok_or(NoneError {})?) {
                ul_sys::ulUpdate(self.renderer);
            }
        }

        Ok(())
    }

    pub fn render(&mut self) {
        unsafe {
            ul_sys::ulRender(self.renderer);
        }
    }

    pub fn scroll(&mut self, delta_x: i32, delta_y: i32) -> Result<(), NoneError> {
        unsafe {
            let scrollEvent = ul_sys::ulCreateScrollEvent(
                ul_sys::ULScrollEventType_kScrollEventType_ScrollByPixel,
                delta_x,
                delta_y,
            );

            ul_sys::ulViewFireScrollEvent(self.view.ok_or(NoneError {})?, scrollEvent);

            ul_sys::ulDestroyScrollEvent(scrollEvent);

            Ok(())
        }
    }

    pub fn get_scroll_height(&mut self) -> Result<f64, NoneError> {
        unsafe {
            let (jsgctx, _) = helpers::getJSContextFromView(self.view.ok_or(NoneError {})?);

            Ok(ul_sys::JSValueToNumber(
                jsgctx,
                self.evaluate_script("document.body.scrollHeight").unwrap(),
                std::ptr::null_mut(),
            ))
        }
    }

    pub fn set_finish_loading_callback<T>(&mut self, cb: &'a mut T) -> Result<(), NoneError>
        where
            T: FnMut(View),
    {
        let view = self.view.ok_or(NoneError {})?;

        unsafe {
            let (cb_closure, cb_function) = unpack_closure_view_cb(cb);

            ul_sys::ulViewSetFinishLoadingCallback(view, Some(cb_function), cb_closure);
        }

        Ok(())
    }

    pub fn set_dom_ready_callback<T>(&mut self, cb: &'a mut T) -> Result<(), NoneError>
        where
            T: FnMut(View),
    {
        let view = self.view.ok_or(NoneError {})?;

        unsafe {
            let (cb_closure, cb_function) = unpack_closure_view_cb(cb);

            ul_sys::ulViewSetDOMReadyCallback(view, Some(cb_function), cb_closure);
        }

        Ok(())
    }

    pub fn create_function<T>(
        &mut self,
        name: &'static str,
        hook: &'a mut T,
    ) -> Result<ul_sys::JSObjectRef, NoneError>
        where
            T: FnMut(
                ul_sys::JSContextRef,
                ul_sys::JSObjectRef,
                ul_sys::JSObjectRef,
                usize,
                *const ul_sys::JSValueRef,
                *mut ul_sys::JSValueRef,
            ) -> ul_sys::JSValueRef,
    {
        Ok(create_js_function(
            self.view.ok_or(NoneError {})?,
            name,
            hook,
        ))
    }

    pub fn set_js_object_property(
        &mut self,
        name: &'static str,
        object: ul_sys::JSObjectRef,
    ) -> Result<(), NoneError> {
        set_js_object_property(self.view.ok_or(NoneError {})?, name, object);

        Ok(())
    }

    pub fn evaluate_script(&mut self, script: &'static str) -> Result<ul_sys::JSValueRef, NoneError> {
        Ok(evaluate_script(self.view.ok_or(NoneError {})?, script))
    }

    pub fn get_raw_pixels(&mut self) -> Result<Vec<u8>, NoneError> {
        unsafe {
            let bitmap_obj = ul_sys::ulViewGetBitmap(self.view.ok_or(NoneError {})?);

            let bitmap = ul_sys::ulBitmapLockPixels(bitmap_obj);
            let bitmap_size = ul_sys::ulBitmapGetSize(bitmap_obj);

            let bitmap_raw = std::slice::from_raw_parts_mut(bitmap as *mut u8, bitmap_size);

            ul_sys::ulBitmapUnlockPixels(bitmap_obj);

            Ok(bitmap_raw.to_vec())
        }
    }

    pub fn write_png_to_file(&mut self, file_name: &'static str) -> Result<bool, NoneError> {
        unsafe {
            let bitmap_obj = ul_sys::ulViewGetBitmap(self.view.ok_or(NoneError {})?);

            let bitmap = ul_sys::ulBitmapLockPixels(bitmap_obj);
            let bitmap_size = ul_sys::ulBitmapGetSize(bitmap_obj);

            let bitmap_raw = std::slice::from_raw_parts_mut(bitmap as *mut u8, bitmap_size);

            let fn_c_str = std::ffi::CString::new(file_name).unwrap();

            Ok(ul_sys::ulBitmapWritePNG(bitmap_obj, fn_c_str.as_ptr()))
        }
    }

    pub fn is_loading(&self) -> bool {
        match self.view {
            Some(view) => unsafe { ul_sys::ulViewIsLoading(view) },
            None => false,
        }
    }

    pub fn log_to_stdout(&mut self) -> Result<(), NoneError> {
        unsafe {
            ul_sys::ulViewSetAddConsoleMessageCallback(
                self.view.ok_or(NoneError {})?,
                Some(log_forward_cb),
                std::ptr::null_mut() as *mut c_void,
            );
        }

        Ok(())
    }
}