Newer
Older
webview-d / source / webview / package.d
@XXIV XXIV on 6 Jul 3 KB fix callback issue
  1. /*
  2. * Copyright (c) 2023 XXIV
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. module webview;
  23.  
  24. import std.string : toStringz, fromStringz;
  25. import webview.raw;
  26.  
  27. alias WebViewVersionInfo = webview_version_info_t;
  28.  
  29. alias DispatchCallback = extern(C) void function (WebView w, void* arg);
  30.  
  31. alias BindCallback = extern(C) void function (string seq, string req, void* arg);
  32.  
  33. enum WindowSizeHint
  34. {
  35. None,
  36. Min,
  37. Max,
  38. Fixed
  39. }
  40.  
  41. struct CallbackContext(T)
  42. {
  43. T func;
  44. void* data = null;
  45. }
  46.  
  47. struct WebView
  48. {
  49. private webview_t webview;
  50.  
  51. this(bool debug_, void* window)
  52. {
  53. webview = webview_create(cast(int)debug_, window);
  54. }
  55.  
  56. void run()
  57. {
  58. webview_run(this.webview);
  59. }
  60.  
  61. void terminate()
  62. {
  63. webview_terminate(this.webview);
  64. }
  65.  
  66. void dispatch(CallbackContext!(DispatchCallback)* ctx)
  67. {
  68. struct RegisterCallback
  69. {
  70. extern(C) static void call(webview_t w, void* arg)
  71. {
  72. CallbackContext!(DispatchCallback)* cb = cast(CallbackContext!(DispatchCallback)*)arg;
  73. WebView webview;
  74. webview.webview = w;
  75. cb.func(webview, cb.data);
  76. }
  77. };
  78. webview_dispatch(this.webview, &RegisterCallback.call, ctx);
  79. }
  80.  
  81. void* getWindow()
  82. {
  83. return webview_get_window(this.webview);
  84. }
  85.  
  86. void setTitle(string title)
  87. {
  88. webview_set_title(this.webview, title.toStringz);
  89. }
  90.  
  91. void setSize(int width, int height, WindowSizeHint hint)
  92. {
  93. webview_set_size(this.webview, width, height, cast(int)hint);
  94. }
  95.  
  96. void navigate(string url)
  97. {
  98. webview_navigate(this.webview, url.toStringz);
  99. }
  100.  
  101. void setHtml(string html)
  102. {
  103. webview_set_html(this.webview, html.toStringz);
  104. }
  105.  
  106. void init(string js)
  107. {
  108. webview_init(this.webview, js.toStringz);
  109. }
  110.  
  111. void eval(string js)
  112. {
  113. webview_eval(this.webview, js.toStringz);
  114. }
  115.  
  116. void bind(string name, CallbackContext!(BindCallback)* ctx)
  117. {
  118. struct RegisterCallback
  119. {
  120. extern(C) static void call(const(char)* seq, const(char)* req, void* arg)
  121. {
  122. CallbackContext!(BindCallback)* cb = cast(CallbackContext!(BindCallback)*)arg;
  123. cb.func(cast(string)(seq.fromStringz), cast(string)(req.fromStringz), cb.data);
  124. }
  125. };
  126. webview_bind(this.webview, name.toStringz, &RegisterCallback.call, ctx);
  127. }
  128.  
  129. void unbind(string name)
  130. {
  131. webview_unbind(this.webview, name.toStringz);
  132. }
  133.  
  134. void ret(string seq, int status, string result)
  135. {
  136. webview_return(this.webview, seq.toStringz, status, result.toStringz);
  137. }
  138.  
  139. const(WebViewVersionInfo)* ver()
  140. {
  141. return webview_version();
  142. }
  143.  
  144. ~this()
  145. {
  146. webview_destroy(this.webview);
  147. }
  148. }