1 /**
2 Dplug's wren bridge. 
3 
4 Copyright: Guillaume Piolat 2021.
5 License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6 */
7 module dplug.wren;
8 
9 import dplug.core.nogc;
10 import dplug.gui.context;
11 import dplug.gui.element;
12 
13 nothrow:
14 
15 
16 public import dplug.wren.wrensupport;
17 
18 /// Create wren support for this UI tree, puts it in UIContext under pimpl idiom.
19 /// Returns the Wren support object.
20 /// Call this in your gui.d this.
21 void enableWrenSupport(IUIContext context) @nogc
22 {
23     WrenSupport w = mallocNew!WrenSupport(context);
24     context.setUserPointer(UICONTEXT_POINTERID_WREN_SUPPORT, cast(void*)w);
25 }
26 
27 /// Disable wren support, meaning it will release the Wren VM and integration.
28 /// Call this in your gui.d ~this.
29 void disableWrenSupport(IUIContext context) @nogc
30 {
31     WrenSupport w = wrenSupport(context);
32     context.setUserPointer(UICONTEXT_POINTERID_WREN_SUPPORT, null);
33     destroyFree(w);
34 }
35 
36 /// Get the `WrenSupport` object that holds all wren state and integration with the plugin.
37 /// The rest of the public API follows in dplug.wren.wrensupport
38 WrenSupport wrenSupport(IUIContext context) @nogc
39 {
40     return cast(WrenSupport) context.getUserPointer(UICONTEXT_POINTERID_WREN_SUPPORT);
41 }
42 
43 /// All widgets (derivatives of UIElement) have a string ID.
44 /// mixin this code to automatically set widgets ID. 
45 /// It generates 
46 ///     _member.id = "_member";
47 /// for every field that is @ScriptExport, in order to find them from Wren.
48 ///
49 /// Example:
50 /// ---
51 /// mixin(fieldIdentifiersAreIDs!MyPluginGUI);
52 /// ---
53 string fieldIdentifiersAreIDs(T)()
54 {
55     import std.traits: getSymbolsByUDA;
56     string s;
57     static foreach(m; getSymbolsByUDA!(T, ScriptExport))
58     {{
59         string fieldName = m.stringof;
60         s ~= "if(" ~ fieldName ~ ")" ~ fieldName ~ ".id = \"" ~ fieldName ~ "\";\n";
61     }}
62     return s;
63 }