Created
July 23, 2024 02:29
-
-
Save jinzhongjia/e9335d881e073a9e04586670fd2bc25d to your computer and use it in GitHub Desktop.
zig webui
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="/webui.js"></script> | |
</head> | |
Hello World ! | |
<img src="logo.png" alt="图片描述" /> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const mem = std.mem; | |
const webui = @cImport({ | |
@cInclude("webui.h"); | |
}); | |
const indexHTML = @embedFile("index.html"); | |
const logo = @embedFile("logo.png"); | |
var buf: []u8 = undefined; | |
fn file_handler(tmp_filename: [*c]const u8, length: [*c]c_int) callconv(.C) ?*const anyopaque { | |
if (mem.eql(u8, tmp_filename[0..std.mem.len(tmp_filename)], "/logo.png")) { | |
// generate content | |
const content = std.fmt.bufPrint(buf, | |
\\HTTP/1.1 200 OK | |
\\Content-Type: image/png | |
\\Content-Length: {} | |
\\ | |
\\{s} | |
, .{ logo.len, logo }) catch unreachable; | |
length.* = @intCast(content.len); | |
return @ptrCast(content.ptr); | |
} | |
return null; | |
} | |
pub fn main() !void { | |
// malloca memory | |
buf = @as(*[20480]u8, @ptrCast(webui.malloc(20480))); | |
// defer free memory | |
defer webui.free(@ptrCast(buf.ptr)); | |
const mainWindow = webui.webui_new_window(); | |
webui.webui_set_file_handler(mainWindow, file_handler); | |
_ = webui.webui_show(mainWindow, indexHTML); | |
webui.webui_wait(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment