1 module ui.App;
2 
3 import ui.Control;
4 
5 struct App {
6     private static bool _appRun = false;
7     private static Control _main;
8 
9     mixin EventListenerMixin!("QueueMain", NoneTypeTag);
10     mixin EventListenerMixin!("OnShouldQuit", NoneTypeTag, int);
11 
12     static this() {
13         auto io = uiInitOptions();
14         auto msg = uiInit(&io);
15         if (msg) {
16             scope(exit) uiFreeInitError(msg);
17             import core.stdc.string: strlen;
18             assert(false, "libui init error: " ~ msg[0..strlen(msg)]);
19         }
20 
21         uiQueueMain(&QueueMainCallback, null);
22         uiOnShouldQuit(&OnShouldQuitCallback, null);
23     }
24 
25     static void init() {
26         auto io = uiInitOptions();
27         auto msg = uiInit(&io);
28         if (msg) {
29             scope(exit) uiFreeInitError(msg);
30             import core.stdc.string: strlen;
31             assert(false, "libui init error: " ~ msg[0..strlen(msg)]);
32         }
33 
34         uiQueueMain(&QueueMainCallback, null);
35         uiOnShouldQuit(&OnShouldQuitCallback, null);
36     }
37 
38     static ~this() {
39         uiUninit;
40     }
41 
42     static void run(Control main) {
43         import std.exception: enforce;
44         if (_appRun) {
45             enforce(false, "Application cannot be run twice");
46         }
47         if (main.parent) {
48             enforce(false, "Application can only run with top level control");
49         }
50         _appRun = true;
51 
52         _main = main;
53         OnShouldQuit(delegate() {
54                 if (!_main.parent) {
55                     uiControlDestroy(_main._control);
56                 }
57             });
58         uiMain;
59     }
60 
61     static void quit() {
62         uiQuit;
63     }
64 
65     static bool step(bool wait) {
66         uiMainSteps;
67         return cast(bool) uiMainStep(cast(int) wait);
68     }
69 }