| Paint the tree ABI a third way: Zig and dvui 7289263 nandi 9d ago | 1 | /* A caller of libjoltzvui: builds a tree, paints it, drains events. |
| 2 | * This is the shape glimmer's reconciler drives the library in, in C. */ |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include "include/zvui.h" |
| 7 | |
| 8 | static int mk(const char *tag, int parent) { |
| 9 | int n = zvui_node_new(tag); |
| 10 | zvui_node_append(parent, n); |
| 11 | return n; |
| 12 | } |
| 13 | |
| 14 | int main(int argc, char **argv) { |
| 15 | int headless = (argc > 1 && strcmp(argv[1], "--dump") == 0); |
| 16 | /* --frames N paints N frames and leaves: a smoke test that ends by itself. */ |
| 17 | int limit = 0; |
| 18 | for (int i = 1; i < argc - 1; i++) |
| 19 | if (strcmp(argv[i], "--frames") == 0) limit = atoi(argv[i + 1]); |
| 20 | |
| 21 | int root = zvui_tree_root(); |
| 22 | zvui_node_set_num(root, "spacing", 8); |
| 23 | |
| 24 | int card = mk("card", root); |
| 25 | int title = mk("title", card); |
| 26 | zvui_node_set_str(title, "text", "jolt-zvui"); |
| 27 | |
| 28 | int sub = mk("dim-label", card); |
| 29 | zvui_node_set_str(sub, "text", "the vidya tree ABI, painted by dvui"); |
| 30 | |
| 31 | mk("separator", card); |
| 32 | |
| 33 | int name = mk("entry", card); |
| 34 | zvui_node_set_str(name, "placeholder", "your handle"); |
| 35 | |
| 36 | int tls = mk("checkbox", card); |
| 37 | zvui_node_set_str(tls, "label", "Use TLS"); |
| 38 | zvui_node_set_bool(tls, "value", 1); |
| 39 | |
| 40 | int row = mk("hbox", card); |
| 41 | zvui_node_set_num(row, "spacing", 6); |
| 42 | int connect = mk("button", row); |
| 43 | zvui_node_set_str(connect, "text", "Connect"); |
| 44 | zvui_node_set_str(connect, "kind", "primary"); |
| 45 | int quit = mk("button", row); |
| 46 | zvui_node_set_str(quit, "text", "Quit"); |
| 47 | |
| 48 | int status = mk("label", card); |
| 49 | zvui_node_set_str(status, "text", "idle"); |
| 50 | |
| 51 | if (headless) { |
| 52 | printf("%s", zvui_tree_dump(root)); |
| 53 | printf("child_count(root)=%d tag(card)=%s\n", |
| 54 | zvui_node_child_count(root), zvui_node_tag(card)); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | if (!zvui_open(520, 420, "jolt-zvui demo")) { |
| 59 | fprintf(stderr, "could not open a window\n"); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | int painted = 0; |
| 64 | while (!zvui_should_close()) { |
| 65 | zvui_frame(); |
| 66 | if (limit && ++painted >= limit) { |
| 67 | printf("painted %d frames, %.0fx%.0f points\n", |
| 68 | painted, zvui_screen_width(), zvui_screen_height()); |
| 69 | break; |
| 70 | } |
| 71 | while (zvui_tree_poll_event()) { |
| 72 | int node = zvui_tree_event_node(); |
| 73 | const char *ev = zvui_tree_event_name(); |
| 74 | printf("event %s on #%d text=\"%s\" num=%f\n", |
| 75 | ev, node, zvui_tree_event_text(), zvui_tree_event_num()); |
| 76 | if (node == quit && strcmp(ev, "click") == 0) goto done; |
| 77 | if (node == connect && strcmp(ev, "click") == 0) |
| 78 | zvui_node_set_str(status, "text", "connecting..."); |
| 79 | if (node == name && strcmp(ev, "change") == 0) { |
| 80 | static char buf[256]; |
| 81 | snprintf(buf, sizeof buf, "hello, %s", zvui_tree_event_text()); |
| 82 | zvui_node_set_str(status, "text", buf); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | done: |
| 87 | zvui_close(); |
| 88 | return 0; |
| 89 | } |