/* A caller of libjoltzvui: builds a tree, paints it, drains events. * This is the shape glimmer's reconciler drives the library in, in C. */ #include #include #include #include "include/zvui.h" static int mk(const char *tag, int parent) { int n = zvui_node_new(tag); zvui_node_append(parent, n); return n; } int main(int argc, char **argv) { int headless = (argc > 1 && strcmp(argv[1], "--dump") == 0); /* --frames N paints N frames and leaves: a smoke test that ends by itself. */ int limit = 0; for (int i = 1; i < argc - 1; i++) if (strcmp(argv[i], "--frames") == 0) limit = atoi(argv[i + 1]); int root = zvui_tree_root(); zvui_node_set_num(root, "spacing", 8); int card = mk("card", root); int title = mk("title", card); zvui_node_set_str(title, "text", "jolt-zvui"); int sub = mk("dim-label", card); zvui_node_set_str(sub, "text", "the vidya tree ABI, painted by dvui"); mk("separator", card); int name = mk("entry", card); zvui_node_set_str(name, "placeholder", "your handle"); int tls = mk("checkbox", card); zvui_node_set_str(tls, "label", "Use TLS"); zvui_node_set_bool(tls, "value", 1); int row = mk("hbox", card); zvui_node_set_num(row, "spacing", 6); int connect = mk("button", row); zvui_node_set_str(connect, "text", "Connect"); zvui_node_set_str(connect, "kind", "primary"); int quit = mk("button", row); zvui_node_set_str(quit, "text", "Quit"); int status = mk("label", card); zvui_node_set_str(status, "text", "idle"); if (headless) { printf("%s", zvui_tree_dump(root)); printf("child_count(root)=%d tag(card)=%s\n", zvui_node_child_count(root), zvui_node_tag(card)); return 0; } if (!zvui_open(520, 420, "jolt-zvui demo")) { fprintf(stderr, "could not open a window\n"); return 1; } int painted = 0; while (!zvui_should_close()) { zvui_frame(); if (limit && ++painted >= limit) { printf("painted %d frames, %.0fx%.0f points\n", painted, zvui_screen_width(), zvui_screen_height()); break; } while (zvui_tree_poll_event()) { int node = zvui_tree_event_node(); const char *ev = zvui_tree_event_name(); printf("event %s on #%d text=\"%s\" num=%f\n", ev, node, zvui_tree_event_text(), zvui_tree_event_num()); if (node == quit && strcmp(ev, "click") == 0) goto done; if (node == connect && strcmp(ev, "click") == 0) zvui_node_set_str(status, "text", "connecting..."); if (node == name && strcmp(ev, "change") == 0) { static char buf[256]; snprintf(buf, sizeof buf, "hello, %s", zvui_tree_event_text()); zvui_node_set_str(status, "text", buf); } } } done: zvui_close(); return 0; }