-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathwebui.zig
More file actions
1296 lines (1137 loc) · 44.2 KB
/
webui.zig
File metadata and controls
1296 lines (1137 loc) · 44.2 KB
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! This is Zig Wrapping for [Webui](https://github.com/webui-dev/webui),
//! Zig-WebUI Library
//!
//! WebSite: [http://webui.me](http://webui.me),
//! Github: [https://github.com/webui-dev/zig-webui](https://github.com/webui-dev/zig-webui)
//!
//! Copyright (c) 2020-2024 [Jinzhongjia](https://github.com/jinzhongjia),
//! Licensed under MIT License.
const webui = @This();
const builtin = @import("builtin");
const std = @import("std");
const windows = std.os.windows;
const flags = @import("flags");
pub const c = @import("c.zig");
pub const WebUIError = error{
// WebUI does not currently provide any information as to what caused errors
// in most functions, instead we will just return a GenericError
GenericError,
/// this present create window new id failed
CreateWindowError,
/// this present bind element with callback failed
BindError,
/// show window failed
ShowError,
/// start server failed(like show, but no window)
ServerError,
/// encode failed
EncodeError,
/// decode failed
DecodeError,
/// get url failed
UrlError,
/// get process info failed
ProcessError,
/// get HWND failed, this is only occur on MS window
HWNDError,
/// get or set windows listening prot failed
PortError,
/// run javascript failed
ScriptError,
/// allocate memory failed
AllocateFailed,
};
/// webui error wrapper
pub const WebUIErrorInfo = struct {
num: i32,
msg: [:0]const u8,
};
/// through this func, we can get webui's lastest error number and error message
pub fn getLastError() WebUIErrorInfo {
return .{
.num = c.webui_get_last_error_number(),
.msg = std.mem.span(c.webui_get_last_error_message()),
};
}
/// The window number. Do not modify.
window_handle: usize,
/// Creating a new WebUI window object.
pub fn newWindow() webui {
const window_handle = c.webui_new_window();
return .{
.window_handle = window_handle,
};
}
/// Create a new webui window object using a specified window number.
pub fn newWindowWithId(id: usize) !webui {
if (id == 0 or id >= WEBUI_MAX_IDS) {
return WebUIError.CreateWindowError;
}
const window_handle = c.webui_new_window_id(id);
return .{
.window_handle = window_handle,
};
}
/// Get a free window number that can be used with
/// `newWindowWithId`
pub fn getNewWindowId() usize {
const window_id = c.webui_get_new_window_id();
return window_id;
}
/// Bind an HTML element and a JavaScript object with a backend function.
/// Empty element name means all events.
/// `element` The HTML element / JavaScript object
/// `func` is The callback function,
/// Returns a unique bind ID.
pub fn bind(
self: webui,
element: [:0]const u8,
comptime func: fn (e: *Event) void,
) !usize {
const tmp_struct = struct {
fn handle(tmp_e: *Event) callconv(.c) void {
func(tmp_e);
}
};
const index = c.webui_bind(self.window_handle, element.ptr, tmp_struct.handle);
if (index == 0) return WebUIError.BindError;
return index;
}
/// Use this API after using `bind()` to add any user data to it that can be
/// read later using `getContext()`
pub fn setContext(self: webui, element: [:0]const u8, context: *anyopaque) void {
c.webui_set_context(self.window_handle, element.ptr, context);
}
/// Get the recommended web browser ID to use. If you
/// are already using one, this function will return the same ID.
pub fn getBestBrowser(self: webui) Browser {
return c.webui_get_best_browser(self.window_handle);
}
/// Show a window using embedded HTML, or a file.
/// If the window is already open, it will be refreshed.
/// This will refresh all windows in multi-client mode.
/// Returns True if showing the window is successed
/// `content` is the html which will be shown
pub fn show(self: webui, content: [:0]const u8) !void {
const success = c.webui_show(self.window_handle, content.ptr);
if (!success) return WebUIError.ShowError;
}
/// Same as `show()`. But using a specific web browser
/// Returns True if showing the window is successed
pub fn showBrowser(self: webui, content: [:0]const u8, browser: Browser) !void {
const success = c.webui_show_browser(self.window_handle, content.ptr, browser);
if (!success) return WebUIError.ShowError;
}
/// Same as `show()`. But start only the web server and return the URL.
/// No window will be shown.
pub fn startServer(self: webui, path: [:0]const u8) ![:0]const u8 {
const url = c.webui_start_server(self.window_handle, path.ptr);
const url_len = std.mem.len(url);
if (url_len == 0) return WebUIError.ServerError;
return url[0..url_len :0];
}
/// Show a WebView window using embedded HTML, or a file. If the window is already
/// opend, it will be refreshed. Note: Win32 need `WebView2Loader.dll`.
/// Returns True if if showing the WebView window is successed.
pub fn showWv(self: webui, content: [:0]const u8) !void {
const success = c.webui_show_wv(self.window_handle, content.ptr);
if (!success) return WebUIError.ShowError;
}
/// Set the window in Kiosk mode (Full screen)
pub fn setKiosk(self: webui, status: bool) void {
c.webui_set_kiosk(self.window_handle, status);
}
/// Bring a window to the front and focus it.
pub fn focus(self: webui) void {
c.webui_focus(self.window_handle);
}
/// Add a user-defined web browser's CLI parameters.
pub fn setCustomParameters(self: webui, params: [:0]const u8) void {
c.webui_set_custom_parameters(self.window_handle, params.ptr);
}
/// Set the window with high-contrast support. Useful when you want to
/// build a better high-contrast theme with CSS.
pub fn setHighContrast(self: webui, status: bool) void {
c.webui_set_high_contrast(self.window_handle, status);
}
/// Sets whether the window frame is resizable or fixed.
/// Works only on WebView window.
pub fn setResizable(self: webui, status: bool) void {
c.webui_set_resizable(self.window_handle, status);
}
pub fn isHighConstrast() bool {
return c.webui_is_high_contrast();
}
/// Check if a web browser is installed.
pub fn browserExist(browser: Browser) bool {
return c.webui_browser_exist(browser);
}
/// Wait until all opened windows get closed.
/// This function should be **called** at the end, it will **block** the current thread
pub fn wait() void {
c.webui_wait();
}
/// Wait asynchronously until all opened windows get closed.
/// Returns `true` while there are still windows open.
pub fn waitAsync() bool {
return c.webui_wait_async();
}
/// Set a custom logger function.
/// The logger callback receives the log level, message, and optional user data.
pub fn setLogger(comptime logger: fn (level: LoggerLevel, log: []const u8, user_data: ?*anyopaque) void, user_data: ?*anyopaque) void {
const tmp_struct = struct {
fn handle(lvl: usize, msg: [*:0]const u8, data: ?*anyopaque) callconv(.c) void {
const log_level: LoggerLevel = @enumFromInt(lvl);
const message = std.mem.span(msg);
logger(log_level, message, data);
}
};
c.webui_set_logger(tmp_struct.handle, user_data);
}
/// Close a specific window only. The window object will still exist.
/// All clients.
pub fn close(self: webui) void {
c.webui_close(self.window_handle);
}
/// Minimize a WebView window.
pub fn minimize(self: webui) void {
c.webui_minimize(self.window_handle);
}
/// Maximize a WebView window.
pub fn maximize(self: webui) void {
c.webui_maximize(self.window_handle);
}
/// Close a specific window and free all memory resources.
pub fn destroy(self: webui) void {
c.webui_destroy(self.window_handle);
}
/// Close all open windows.
/// `wait()` will return (Break)
pub fn exit() void {
c.webui_exit();
}
/// Set the web-server root folder path for a specific window.
pub fn setRootFolder(self: webui, path: [:0]const u8) !void {
const success = c.webui_set_root_folder(self.window_handle, path.ptr);
// TODO: replace this error
if (!success) return WebUIError.GenericError;
}
/// Set custom browser folder path.
pub fn setBrowserFolder(path: [:0]const u8) void {
c.webui_set_browser_folder(path.ptr);
}
/// Set the web-server root folder path for all windows.
/// Should be used before `show()`.
pub fn setDefaultRootFolder(path: [:0]const u8) !void {
const success = c.webui_set_default_root_folder(path.ptr);
// TODO: replace this error
if (!success) return WebUIError.GenericError;
}
/// Set a custom handler to serve files. This custom handler should
/// return full HTTP header and body.
/// This deactivates any previous handler set with `setFileHandlerWindow`.
pub fn setFileHandler(self: webui, comptime handler: fn (filename: []const u8) ?[]const u8) void {
const tmp_struct = struct {
fn handle(tmp_filename: [*:0]const u8, length: *c_int) callconv(.c) ?*const anyopaque {
const len = std.mem.len(tmp_filename);
const content = handler(tmp_filename[0..len]);
if (content) |val| {
length.* = @intCast(val.len);
return @ptrCast(val.ptr);
}
return null;
}
};
c.webui_set_file_handler(self.window_handle, tmp_struct.handle);
}
/// Set a custom handler to serve files. This custom handler should
/// return full HTTP header and body.
/// This deactivates any previous handler set with `setFileHandler`.
pub fn setFileHandlerWindow(self: webui, comptime handler: fn (window_handle: usize, filename: []const u8) ?[]const u8) void {
const tmp_struct = struct {
fn handle(window: usize, tmp_filename: [*:0]const u8, length: *c_int) callconv(.c) ?*const anyopaque {
const len = std.mem.len(tmp_filename);
const content = handler(window, tmp_filename[0..len]);
if (content) |val| {
length.* = @intCast(val.len);
return @ptrCast(val.ptr);
}
return null;
}
};
c.webui_set_file_handler_window(self.window_handle, tmp_struct.handle);
}
/// Use this API to set a file handler response if your backend need async
/// response for `setFileHandler()`.
pub fn interfaceSetResponseFileHandler(self: webui, response: []u8) void {
c.webui_interface_set_response_file_handler(
self.window_handle,
@ptrCast(response.ptr),
response.len,
);
}
/// Check if the specified window is still running.
pub fn isShown(self: webui) bool {
return c.webui_is_shown(self.window_handle);
}
/// Set the maximum time in seconds to wait for the window to connect
/// This effect `show()` and `wait()`. Value of `0` means wait forever.
pub fn setTimeout(time: usize) void {
c.webui_set_timeout(time);
}
/// Set the default embedded HTML favicon.
pub fn setIcon(self: webui, icon: [:0]const u8, icon_type: [:0]const u8) void {
c.webui_set_icon(self.window_handle, icon.ptr, icon_type);
}
/// Base64 encoding. Use this to safely send text based data to the UI. If
/// it fails it will return NULL.
/// you need free the return memory with free function
pub fn encode(str: [:0]const u8) ![]u8 {
const ptr = c.webui_encode(str.ptr);
if (ptr) |valid_ptr| {
const len = std.mem.len(valid_ptr);
return valid_ptr[0..len];
} else {
return WebUIError.EncodeError;
}
}
/// Base64 decoding.
/// Use this to safely decode received Base64 text from the UI.
/// If it fails it will return NULL.
/// you need free the return memory with free function
pub fn decode(str: [:0]const u8) ![]u8 {
const ptr = c.webui_decode(str.ptr);
if (ptr) |valid_ptr| {
const len = std.mem.len(valid_ptr);
return valid_ptr[0..len];
} else {
return WebUIError.DecodeError;
}
}
/// Safely free a buffer allocated by WebUI using
pub fn free(buf: []const u8) void {
c.webui_free(@ptrCast(@constCast(buf.ptr)));
}
/// Safely allocate memory using the WebUI memory management system
/// it can be safely freed using `free()` at any time.
/// In general, you should not use this function
pub fn malloc(size: usize) ![]u8 {
const ptr = c.webui_malloc(size) orelse return WebUIError.AllocateFailed;
return @as([*]u8, @ptrCast(ptr))[0..size];
}
/// Copy raw data
/// In general, you should not use this function
pub fn memcpy(dst: []u8, src: []const u8) void {
c.webui_memcpy(@ptrCast(dst.ptr), @ptrCast(@constCast(src.ptr)), src.len);
}
/// Safely send raw data to the UI. All clients.
pub fn sendRaw(self: webui, js_func: [:0]const u8, raw: []u8) void {
c.webui_send_raw(self.window_handle, js_func.ptr, raw.ptr, raw.len);
}
/// Set a window in hidden mode.
/// Should be called before `show()`
pub fn setHide(self: webui, status: bool) void {
c.webui_set_hide(self.window_handle, status);
}
/// Set the window size.
pub fn setSize(self: webui, width: u32, height: u32) void {
c.webui_set_size(self.window_handle, width, height);
}
/// Set the window minimum size.
pub fn setMinimumSize(self: webui, width: u32, height: u32) void {
c.webui_set_minimum_size(self.window_handle, width, height);
}
/// Set the window position.
pub fn setPosition(self: webui, x: u32, y: u32) void {
c.webui_set_position(self.window_handle, x, y);
}
/// Centers the window on the screen. Works better with
/// WebView. Call this function before `webui_show()` for better results.
pub fn setCenter(self: webui) void {
c.webui_set_center(self.window_handle);
}
/// Set the web browser profile to use.
/// An empty `name` and `path` means the default user profile.
/// Need to be called before `show()`.
pub fn setProfile(self: webui, name: [:0]const u8, path: [:0]const u8) void {
c.webui_set_profile(self.window_handle, name.ptr, path.ptr);
}
/// Set the web browser proxy_server to use. Need to be called before `show()`
pub fn setProxy(self: webui, proxy_server: [:0]const u8) void {
c.webui_set_proxy(self.window_handle, proxy_server.ptr);
}
/// Get the full current URL.
pub fn getUrl(self: webui) ![:0]const u8 {
const ptr = c.webui_get_url(self.window_handle);
const len = std.mem.len(ptr);
if (len == 0) return WebUIError.UrlError;
return ptr[0..len :0];
}
/// Open an URL in the native default web browser.
pub fn openUrl(url: [:0]const u8) void {
c.webui_open_url(url.ptr);
}
/// Allow a specific window address to be accessible from a public network
pub fn setPublic(self: webui, status: bool) void {
c.webui_set_public(self.window_handle, status);
}
/// Navigate to a specific URL. All clients.
pub fn navigate(self: webui, url: [:0]const u8) void {
c.webui_navigate(self.window_handle, url.ptr);
}
/// Free all memory resources.
/// Should be called only at the end.
pub fn clean() void {
c.webui_clean();
}
/// Delete all local web-browser profiles folder.
/// It should called at the end.
pub fn deleteAllProfiles() void {
c.webui_delete_all_profiles();
}
/// Delete a specific window web-browser local folder profile.
pub fn deleteProfile(self: webui) void {
c.webui_delete_profile(self.window_handle);
}
/// Get the ID of the parent process (The web browser may re-create another new process).
pub fn getParentProcessId(self: webui) !usize {
const process_id = c.webui_get_parent_process_id(self.window_handle);
if (process_id == 0) return WebUIError.ProcessError;
return process_id;
}
/// Get the ID of the last child process.
pub fn getChildProcessId(self: webui) !usize {
const process_id = c.webui_get_child_process_id(self.window_handle);
if (process_id == 0) return WebUIError.ProcessError;
return process_id;
}
/// Gets Win32 window `HWND`. More reliable with WebView
/// than web browser window, as browser PIDs may change on launch.
pub fn win32GetHwnd(self: webui) !windows.HWND {
if (builtin.os.tag != .windows) {
@compileError("Note: method win32GetHwnd only can call on MS windows!");
}
const tmp_hwnd = c.webui_win32_get_hwnd(self.window_handle);
if (tmp_hwnd) |hwnd| {
return @ptrCast(hwnd);
} else {
return WebUIError.HWNDError;
}
}
/// Get window `HWND` (Win32) or `GtkWindow` (Linux). More reliable with WebView
/// than web browser window, as browser PIDs may change on launch.
/// Returns the window handle as `void*`.
pub fn getHwnd(self: webui) !*anyopaque {
const hwnd = c.webui_get_hwnd(self.window_handle);
if (hwnd) |handle| {
return handle;
} else {
return WebUIError.HWNDError;
}
}
/// Set a callback to catch the close event of the WebView window.
/// The callback must return `false` to prevent the close event, `true` otherwise.
pub fn setCloseHandlerWv(self: webui, comptime handler: fn (window_handle: usize) bool) void {
const tmp_struct = struct {
fn handle(window: usize) callconv(.c) bool {
return handler(window);
}
};
c.webui_set_close_handler_wv(self.window_handle, tmp_struct.handle);
}
/// Get the network port of a running window.
/// This can be useful to determine the HTTP link of `webui.js`
pub fn getPort(self: webui) !usize {
const port = c.webui_get_port(self.window_handle);
if (port == 0) return WebUIError.PortError;
return port;
}
/// Set a custom web-server network port to be used by WebUI.
/// This can be useful to determine the HTTP link of `webui.js` in case
/// you are trying to use WebUI with an external web-server like NGNIX
/// Returns True if the port is free and usable by WebUI
pub fn setPort(self: webui, port: usize) !void {
const success = c.webui_set_port(self.window_handle, port);
if (!success) return WebUIError.PortError;
}
// Get an available usable free network port.
pub fn getFreePort() usize {
return c.webui_get_free_port();
}
/// Control the WebUI behaviour. It's recommended to be called at the beginning.
pub fn setConfig(option: Config, status: bool) void {
c.webui_set_config(option, status);
}
/// Control if UI events comming from this window should be processed
/// one a time in a single blocking thread `True`, or process every event in
/// a new non-blocking thread `False`. This update single window. You can use
/// `setConfig(ui_event_blocking, ...)` to update all windows.
pub fn setEventBlocking(self: webui, status: bool) void {
c.webui_set_event_blocking(self.window_handle, status);
}
/// Make a WebView window frameless.
pub fn setFrameless(self: webui, status: bool) void {
c.webui_set_frameless(self.window_handle, status);
}
/// Make a WebView window transparent.
pub fn setTransparent(self: webui, status: bool) void {
c.webui_set_transparent(self.window_handle, status);
}
/// Get the HTTP mime type of a file.
pub fn getMimeType(file: [:0]const u8) [:0]const u8 {
const res = c.webui_get_mime_type(file.ptr);
return res[0..std.mem.len(res) :0];
}
/// Set the SSL/TLS certificate and the private key content,
/// both in PEM format.
/// This works only with `webui-2-secure` library.
/// If set empty WebUI will generate a self-signed certificate.
pub fn setTlsCertificate(certificate_pem: [:0]const u8, private_key_pem: [:0]const u8) !void {
if (comptime !flags.enableTLS) {
@compileError("not enable tls");
}
const success = c.webui_set_tls_certificate(certificate_pem.ptr, private_key_pem.ptr);
// TODO: replace this error
if (!success) return WebUIError.GenericError;
}
/// Run JavaScript without waiting for the response.All clients.
pub fn run(self: webui, script_content: [:0]const u8) void {
c.webui_run(self.window_handle, script_content.ptr);
}
/// Run JavaScript and get the response back. Work only in single client mode.
/// Make sure your local buffer can hold the response.
/// Return True if there is no execution error
pub fn script(self: webui, script_content: [:0]const u8, timeout: usize, buffer: []u8) !void {
const success = c.webui_script(
self.window_handle,
script_content.ptr,
timeout,
buffer.ptr,
buffer.len,
);
if (!success) return WebUIError.ScriptError;
}
/// Chose between Deno and Nodejs as runtime for .js and .ts files.
pub fn setRuntime(self: webui, runtime: Runtime) void {
c.webui_set_runtime(self.window_handle, runtime);
}
/// Get how many arguments there are in an event
pub fn getCount(_: *Event) usize {
@compileError("please use Event.getCount, this will be removed when zig-webui release");
}
/// Get an argument as integer at a specific index
pub fn getIntAt(_: *Event, _: usize) i64 {
@compileError("please use Event.getIntAt, this will be removed when zig-webui release");
}
/// Get the first argument as integer
pub fn getInt(_: *Event) i64 {
@compileError("please use Event.getInt, this will be removed when zig-webui release");
}
/// Get an argument as float at a specific index
pub fn getFloatAt(_: *Event, _: usize) f64 {
@compileError("please use Event.getFloatAt, this will be removed when zig-webui release");
}
/// Get the first argument as float
pub fn getFloat(_: *Event) f64 {
@compileError("please use Event.getFloat, this will be removed when zig-webui release");
}
/// Get an argument as string at a specific index
pub fn getStringAt(_: *Event, _: usize) [:0]const u8 {
@compileError("please use Event.getStringAt, this will be removed when zig-webui release");
}
/// Get the first argument as string
pub fn getString(_: *Event) [:0]const u8 {
@compileError("please use Event.getString, this will be removed when zig-webui release");
}
/// Get an argument as boolean at a specific index
pub fn getBoolAt(_: *Event, _: usize) bool {
@compileError("please use Event.getBoolAt, this will be removed when zig-webui release");
}
/// Get the first argument as boolean
pub fn getBool(_: *Event) bool {
@compileError("please use Event.getBool, this will be removed when zig-webui release");
}
/// Get the size in bytes of an argument at a specific index
pub fn getSizeAt(_: *Event, _: usize) usize {
@compileError("please use Event.getSizeAt, this will be removed when zig-webui release");
}
/// Get size in bytes of the first argument
pub fn getSize(_: *Event) usize {
@compileError("please use Event.getSize, this will be removed when zig-webui release");
}
/// **deprecated**: use Event.returnInt
/// Return the response to JavaScript as integer.
pub fn returnInt(_: *Event, _: i64) void {
@compileError("please use Event.returnInt, this will be removed when zig-webui release");
}
/// **deprecated**: use Event.returnString
/// Return the response to JavaScript as string.
pub fn returnString(_: *Event, _: [:0]const u8) void {
@compileError("please use Event.returnString, this will be removed when zig-webui release");
}
/// **deprecated**: use Event.returnBool
/// Return the response to JavaScript as boolean.
pub fn returnBool(_: *Event, _: bool) void {
@compileError("please use Event.returnBool, this will be removed when zig-webui release");
}
/// Bind a specific HTML element click event with a function.
/// Empty element means all events.
pub fn interfaceBind(
self: webui,
element: [:0]const u8,
comptime callback: fn (
window_handle: usize,
event_type: EventKind,
element: []u8,
event_number: usize,
bind_id: usize,
) void,
) !usize {
const tmp_struct = struct {
fn handle(
tmp_window: usize,
tmp_event_type: EventKind,
tmp_element: [*:0]u8,
tmp_event_number: usize,
tmp_bind_id: usize,
) callconv(.c) void {
const len = std.mem.len(tmp_element);
callback(tmp_window, tmp_event_type, tmp_element[0..len], tmp_event_number, tmp_bind_id);
}
};
const index = c.webui_interface_bind(self.window_handle, element.ptr, tmp_struct.handle);
if (index == 0) return WebUIError.BindError;
return index;
}
/// When using `interfaceBind()`,
/// you may need this function to easily set a response.
pub fn interfaceSetResponse(self: webui, event_number: usize, response: [:0]const u8) void {
c.webui_interface_set_response(self.window_handle, event_number, response.ptr);
}
/// Check if the app still running.
pub fn interfaceIsAppRunning() bool {
return c.webui_interface_is_app_running();
}
/// Get a unique window ID.
pub fn interfaceGetWindowId(self: webui) usize {
const window_id = c.webui_interface_get_window_id(self.window_handle);
return window_id;
}
/// Get an argument as string at a specific index
pub fn interfaceGetStringAt(self: webui, event_number: usize, index: usize) [:0]const u8 {
const ptr = c.webui_interface_get_string_at(self.window_handle, event_number, index);
// TODO: Error handling here.
const len = std.mem.len(ptr);
return ptr[0..len :0];
}
/// Get an argument as integer at a specific index
pub fn interfaceGetIntAt(self: webui, event_number: usize, index: usize) i64 {
// TODO: Error handling here
return c.webui_interface_get_int_at(self.window_handle, event_number, index);
}
/// Get an argument as float at a specific index.
pub fn interfaceGetFloatAt(self: webui, event_number: usize, index: usize) f64 {
// TODO: Error handling here
return c.webui_interface_get_float_at(self.window_handle, event_number, index);
}
/// Get an argument as boolean at a specific index
pub fn interfaceGetBoolAt(self: webui, event_number: usize, index: usize) bool {
// TODO: Error handling here
return c.webui_interface_get_bool_at(self.window_handle, event_number, index);
}
/// Get the size in bytes of an argument at a specific index
pub fn interfaceGetSizeAt(self: webui, event_number: usize, index: usize) usize {
return c.webui_interface_get_size_at(self.window_handle, event_number, index);
}
// Show a window using embedded HTML, or a file. If the window is already
pub fn interfaceShowClient(self: webui, event_number: usize, content: [:0]const u8) !void {
const success = c.webui_interface_show_client(self.window_handle, event_number, content.ptr);
if (!success) return WebUIError.ShowError;
}
// Close a specific client.
pub fn interfaceCloseClient(self: webui, event_number: usize) void {
c.webui_interface_close_client(self.window_handle, event_number);
}
// Safely send raw data to the UI. Single client.
pub fn interfaceSendRawClient(
self: webui,
event_number: usize,
function: [:0]const u8,
raw: []const u8,
) void {
c.webui_interface_send_raw_client(self.window_handle, event_number, function.ptr, raw.ptr, raw.len);
}
// Navigate to a specific URL. Single client.
pub fn interfaceNavigateClient(self: webui, event_number: usize, url: [:0]const u8) void {
c.webui_interface_navigate_client(self.window_handle, event_number, url.ptr);
}
// Run JavaScript without waiting for the response. Single client.
pub fn interfaceRunClient(self: webui, event_number: usize, script_content: [:0]const u8) void {
c.webui_interface_run_client(self.window_handle, event_number, script_content.ptr);
}
// Run JavaScript and get the response back. Single client.
pub fn interfaceScriptClient(self: webui, event_number: usize, script_content: [:0]const u8, timeout: usize, buffer: []u8) !void {
const success = c.webui_interface_script_client(self.window_handle, event_number, script_content.ptr, timeout, buffer.ptr, buffer.len);
if (!success) return WebUIError.ScriptError;
}
/// binding function: Creates a binding between an HTML element and a callback function
/// binding function: Creates a binding between an HTML element and a callback function
/// - element: A null-terminated string identifying the HTML element(s) to bind to
/// - callback: A function to be called when the bound event triggers
///
/// This function performs compile-time validation on the callback function to ensure it:
/// 1. Is a proper function (not another type)
/// 2. Returns void
/// 3. Is not generic
/// 4. Does not use variable arguments
///
/// The callback function can accept various parameter types:
/// - Event: Gets the full event object
/// - *Event: Gets a pointer to the event object
/// - Other parameters will be automatically converted from event arguments in order:
/// * bool: Converted from event argument bool value
/// * int types: Converted from event argument integer value
/// * float types: Converted from event argument float value
/// * [:0]const u8: For null-terminated string data from event
/// * [*]const u8: For raw pointer data from event
///
/// Note: Event and *Event parameters do not consume argument indices from the event,
/// but all other parameter types will consume arguments in the order they appear.
///
/// Returns:
/// - The binding ID that can be used to unbind later
pub fn binding(self: webui, element: [:0]const u8, comptime callback: anytype) !usize {
const T = @TypeOf(callback);
const TInfo = @typeInfo(T);
// Verify the callback is a function
if (TInfo != .@"fn") {
const err_msg = std.fmt.comptimePrint(
"callback's type ({}), it must be a function!",
.{T},
);
@compileError(err_msg);
}
const fnInfo = TInfo.@"fn";
// Verify return type is void
const Ret = fnInfo.return_type orelse @compileError("return_type can't be null");
if (Ret != void) {
const err_msg = std.fmt.comptimePrint(
"callback's return type ({}), it must be void!",
.{Ret},
);
@compileError(err_msg);
}
// Verify function is not generic
if (fnInfo.is_generic) {
const err_msg = std.fmt.comptimePrint(
"callback's type ({}), it can not be a generic function!",
.{T},
);
@compileError(err_msg);
}
// Verify function does not use varargs
if (fnInfo.is_var_args) {
const err_msg = std.fmt.comptimePrint(
"callback's type ({}), it can not have variable args!",
.{T},
);
@compileError(err_msg);
}
const tmp_struct = struct {
const tup_t = fnParamsToTuple(fnInfo.params);
// Event handler that will convert parameters and call the user's callback
fn handle(e: *Event) void {
var param_tup: tup_t = undefined;
var index: usize = 0;
// Process each parameter of the callback function
inline for (fnInfo.params, 0..fnInfo.params.len) |param, i| {
if (param.type) |tt| {
const paramTInfo = @typeInfo(tt);
switch (paramTInfo) {
// Handle struct type parameters (only Event is allowed)
.@"struct" => {
if (tt == Event) {
param_tup[i] = e.*;
index += 1;
} else {
const err_msg = std.fmt.comptimePrint(
"the struct type is ({}), the struct type you can use only is Event in params!",
.{tt},
);
@compileError(err_msg);
}
},
// Convert boolean values
.bool => {
const res = e.getBoolAt(i - index);
param_tup[i] = res;
},
// Convert integer values with appropriate casting
.int => {
const res = e.getIntAt(i - index);
param_tup[i] = @intCast(res);
},
// Convert floating point values
.float => {
const res = e.getFloatAt(i - index);
param_tup[i] = @floatCast(res);
},
// Handle pointer types with special cases
.pointer => |pointer| {
// Handle null-terminated string slices
if (pointer.size == .slice and pointer.child == u8 and pointer.is_const == true) {
if (pointer.sentinel()) |sentinel| {
if (sentinel == 0) {
const str_ptr = e.getStringAt(i - index);
param_tup[i] = str_ptr;
}
}
// Handle Event pointers
} else if (pointer.size == .one and pointer.child == Event) {
param_tup[i] = e;
index += 1;
// Handle raw byte pointers
} else if (pointer.size == .many and pointer.child == u8 and pointer.is_const == true and pointer.sentinel() == null) {
const raw_ptr = e.getRawAt(i - index);
param_tup[i] = raw_ptr;
} else {
const err_msg = std.fmt.comptimePrint(
"the pointer type is ({}), now we only support [:0]const u8 or [*]const u8 or *webui.Event !",
.{tt},
);
@compileError(err_msg);
}
},
// Reject unsupported types
else => {
const err_msg = std.fmt.comptimePrint(
"type is ({}), only support these types: Event, Bool, Int, Float, []u8!",
.{tt},
);
@compileError(err_msg);
},
}
} else {
@compileError("param must have type");
}
}
// Call the user's callback with the properly converted parameters
@call(.auto, callback, param_tup);
}
};
// Create the actual binding with the webui backend
return self.bind(element, tmp_struct.handle);
}
/// this funciton will return a fn's params tuple
fn fnParamsToTuple(comptime params: []const std.builtin.Type.Fn.Param) type {
const Type = std.builtin.Type;
const fields: [params.len]Type.StructField = blk: {
var res: [params.len]Type.StructField = undefined;
for (params, 0..params.len) |param, i| {
res[i] = Type.StructField{
.type = param.type.?,
.alignment = @alignOf(param.type.?),
.default_value_ptr = null,
.is_comptime = false,
.name = std.fmt.comptimePrint("{}", .{i}),
};
}
break :blk res;
};
return @Type(.{
.@"struct" = std.builtin.Type.Struct{
.layout = .auto,
.is_tuple = true,
.decls = &.{},
.fields = &fields,
},
});
}
pub const WEBUI_VERSION: std.SemanticVersion = .{
.major = 2,
.minor = 5,
.patch = 0,
.pre = "beta.2",
};
/// Max windows, servers and threads
pub const WEBUI_MAX_IDS = 256;
/// Max allowed argument's index
pub const WEBUI_MAX_ARG = 16;
pub const Browser = enum(usize) {
/// 0. No web browser
NoBrowser = 0,
/// 1. Default recommended web browser
AnyBrowser,
/// 2. Google Chrome
Chrome,
/// 3. Mozilla Firefox
Firefox,
/// 4. Microsoft Edge
Edge,
/// 5. Apple Safari
Safari,