swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame^] | 1 | #include <iostream> |
| 2 | #include <stdio.h> |
| 3 | #include "webview/webview.h" |
| 4 | #include "nlohmann/json.hpp" |
| 5 | #include "bundle-webview-js.h" |
| 6 | |
| 7 | using json = nlohmann::json; |
| 8 | |
| 9 | extern "C" { |
| 10 | extern void do_a_conversion(char *, char *); |
| 11 | } |
| 12 | |
| 13 | extern char log_buffer[4096]; |
| 14 | extern char *log_buffer_ptr; |
| 15 | |
| 16 | |
| 17 | std::string convert(std::string arg) { |
| 18 | std::cout << "Params: " << arg << std::endl; |
| 19 | json j = json::parse(arg); |
| 20 | |
| 21 | auto from = j[0].template get<std::string>(), |
| 22 | to = j[1].template get<std::string>(); |
| 23 | |
| 24 | std::cout << from << ' ' << to << std::endl; |
| 25 | |
| 26 | do_a_conversion((char *)from.data(), (char *)to.data()); |
| 27 | std::string res = json(std::string(log_buffer, log_buffer_ptr - log_buffer)).dump(); |
| 28 | log_buffer_ptr = log_buffer; |
| 29 | std::cout << res << std::endl; |
| 30 | return res; |
| 31 | } |
| 32 | |
| 33 | std::string get_html() { |
| 34 | return std::string("<div id=app></div><script>") + std::string((const char *)bundle_webview_js, bundle_webview_js_len) + std::string("</script>"); |
| 35 | } |
| 36 | |
| 37 | #ifdef WIN32 |
| 38 | int WINAPI WinMain(HINSTANCE hInt, HINSTANCE hPrevInst, LPSTR lpCmdLine, |
| 39 | int nCmdShow) { |
| 40 | #else |
| 41 | int main() { |
| 42 | #endif |
| 43 | // cookie_io_functions_t iofns = {NULL}; |
| 44 | // iofns.cookie_write_function_t = writer; |
| 45 | // |
| 46 | // stdout = fopencookie(NULL, "r", iofns); |
| 47 | |
| 48 | try { |
| 49 | webview::webview w(false, nullptr); |
| 50 | w.set_title("Units"); |
| 51 | w.set_size(480, 320, WEBVIEW_HINT_NONE); |
| 52 | w.bind("convert", convert); |
| 53 | w.set_html(get_html()); |
| 54 | // w.navigate("http://localhost:8000/index-webview.html"); |
| 55 | w.run(); |
| 56 | } catch (const webview::exception &e) { |
| 57 | std::cerr << e.what() << std::endl; |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | return 0; |
| 63 | } |