1 /// Optional window package, mainly to run gfx-d examples
2 module gfx.window;
3 
4 import gfx.core.rc : AtomicRefCounted;
5 import gfx.graal : Backend, Instance;
6 
7 alias MouseHandler = void delegate(uint x, uint y);
8 alias KeyHandler = void delegate(uint key);
9 alias CloseHandler = bool delegate();
10 
11 interface Display : AtomicRefCounted
12 {
13     @property Instance instance();
14     @property Window[] windows();
15     Window createWindow();
16     void pollAndDispatch();
17 }
18 
19 interface Window
20 {
21     import gfx.graal.presentation : Surface;
22 
23     void show(uint width, uint height);
24     void close();
25 
26     @property Surface surface();
27     @property bool closeFlag() const;
28     @property void closeFlag(in bool flag);
29 
30     @property void onMouseMove(MouseHandler handler);
31     @property void onMouseOn(MouseHandler handler);
32     @property void onMouseOff(MouseHandler handler);
33     @property void onKeyOn(KeyHandler handler);
34     @property void onKeyOff(KeyHandler handler);
35     @property void onClose(CloseHandler handler);
36 }
37 
38 /// The backend load order is the order into which backend load attempts
39 /// will be performed.
40 /// This array provides a default value for createDisplay parameter
41 immutable Backend[] defaultBackendLoadOrder = [
42     Backend.vulkan,
43     Backend.gl3,
44 ];
45 
46 
47 /// Create a display for the running platform.
48 /// The display will load a backend instance during startup.
49 /// It will try the backends in the provided loadOrder
50 Display createDisplay(in Backend[] loadOrder=defaultBackendLoadOrder)
51 {
52     version(linux) {
53         enum useWayland = false;
54         static if (useWayland) {
55             import gfx.window.wayland : WaylandDisplay;
56             return new WaylandDisplay;
57         }
58         else {
59             import gfx.window.xcb : XcbDisplay;
60             return new XcbDisplay(loadOrder);
61         }
62     }
63     else version(Windows) {
64         import gfx.window.win32 : Win32Display;
65         return new Win32Display(loadOrder);
66     }
67     else {
68         pragma(msg, "Unsupported platform");
69         assert(false, "Unsupported platform");
70     }
71 }