swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <stdio.h> |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 3 | #include <string.h> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 4 | #include "webview/webview.h" |
| 5 | #include "nlohmann/json.hpp" |
| 6 | #include "bundle-webview-js.h" |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 7 | #include "webview/menu.h" |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 8 | |
| 9 | using json = nlohmann::json; |
| 10 | |
| 11 | extern "C" { |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 12 | extern void do_a_conversion(char *, char *, char *); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | extern char log_buffer[4096]; |
| 16 | extern char *log_buffer_ptr; |
| 17 | |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 18 | webview::webview *wp; |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 19 | |
| 20 | std::string convert(std::string arg) { |
| 21 | std::cout << "Params: " << arg << std::endl; |
| 22 | json j = json::parse(arg); |
| 23 | |
| 24 | auto from = j[0].template get<std::string>(), |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 25 | to = j[1].template get<std::string>(), |
| 26 | system = j[2].template get<std::string>(); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 27 | |
| 28 | std::cout << from << ' ' << to << std::endl; |
| 29 | |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 30 | do_a_conversion((char *)from.data(), to.size() > 0 ? (char *)to.data() : nullptr, (char *)system.data()); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 31 | std::string res = json(std::string(log_buffer, log_buffer_ptr - log_buffer)).dump(); |
| 32 | log_buffer_ptr = log_buffer; |
| 33 | std::cout << res << std::endl; |
| 34 | return res; |
| 35 | } |
| 36 | |
| 37 | std::string get_html() { |
| 38 | return std::string("<div id=app></div><script>") + std::string((const char *)bundle_webview_js, bundle_webview_js_len) + std::string("</script>"); |
| 39 | } |
| 40 | |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 41 | std::string copy(std::string j) { |
| 42 | std::cout << "copy called " << j << '\n'; |
| 43 | return "undefined"; |
| 44 | } |
| 45 | |
| 46 | void callback(const char *ev) { |
| 47 | std::cout << "menu event " << ev << "\n"; |
| 48 | std::string_view str = ev; |
| 49 | |
| 50 | if (str == "copy") { |
| 51 | wp->eval("document.execCommand('copy');"); |
| 52 | } else if (str == "cut") { |
| 53 | wp->eval("document.execCommand('cut');"); |
| 54 | } else if (str == "paste") { |
| 55 | wp->eval("document.execCommand('paste');"); |
| 56 | } else if (str == "close") { |
| 57 | exit(0); |
| 58 | } else if (str == "undo") { |
| 59 | wp->eval("document.execCommand('undo');"); |
| 60 | } else if (str == "redo") { |
| 61 | wp->eval("document.execCommand('redo');"); |
| 62 | } else if (str == "select_all") { |
| 63 | wp->eval("document.execCommand('selectAll');"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | extern char g_argv_0[128]; |
| 69 | |
| 70 | int main(int argc, char **argv) { |
| 71 | strlcpy(g_argv_0, argv[0], 128); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 72 | // cookie_io_functions_t iofns = {NULL}; |
| 73 | // iofns.cookie_write_function_t = writer; |
| 74 | // |
| 75 | // stdout = fopencookie(NULL, "r", iofns); |
| 76 | |
| 77 | try { |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 78 | webview::webview w(true, nullptr); |
| 79 | wp = &w; |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 80 | w.set_title("Units"); |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 81 | w.set_size(480, 320, WEBVIEW_HINT_MIN); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 82 | w.bind("convert", convert); |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 83 | w.bind("copy", copy); |
| 84 | w.eval("window.webview = {};"); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 85 | w.set_html(get_html()); |
| 86 | // w.navigate("http://localhost:8000/index-webview.html"); |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 87 | |
| 88 | webview_menu_item menu[] = { |
| 89 | {0, "File", "", ""}, |
| 90 | {1, "Open", "C-o", "open"}, |
| 91 | {1, "Save", "C-s", "save"}, |
| 92 | {1, "New", "C-n", "new"}, |
| 93 | {1, "-"}, |
| 94 | {1, "Close Window", "C-w", "close"}, |
| 95 | {0, "Edit", "", ""}, |
| 96 | {1, "Undo", "C-z", "undo"}, |
| 97 | {1, "Redo", "CS-z", "redo"}, |
| 98 | {1, "-"}, |
| 99 | {1, "Cut", "C-x", "cut"}, |
| 100 | {1, "Copy", "C-c", "copy"}, |
| 101 | {1, "Paste", "C-v", "paste"}, |
| 102 | {1, "Select All", "C-a", "select_all"}, |
| 103 | }; |
| 104 | |
| 105 | webview_set_menu(sizeof(menu) / sizeof(menu[0]), menu, callback); |
| 106 | |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 107 | w.run(); |
| 108 | } catch (const webview::exception &e) { |
| 109 | std::cerr << e.what() << std::endl; |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | return 0; |
| 115 | } |