blob: 67c7b0ad4e83079daa0cee9a2135f27e6c4e269e [file] [log] [blame]
swissChiliab615d82024-03-08 17:15:13 -05001#include <iostream>
2#include <stdio.h>
swissChili281af442024-11-14 23:30:29 -05003#include <string.h>
swissChiliab615d82024-03-08 17:15:13 -05004#include "webview/webview.h"
5#include "nlohmann/json.hpp"
6#include "bundle-webview-js.h"
swissChili281af442024-11-14 23:30:29 -05007#include "webview/menu.h"
swissChiliab615d82024-03-08 17:15:13 -05008
9using json = nlohmann::json;
10
11extern "C" {
swissChili4f0a0a82024-03-08 18:58:38 -050012 extern void do_a_conversion(char *, char *, char *);
swissChiliab615d82024-03-08 17:15:13 -050013}
14
15extern char log_buffer[4096];
16extern char *log_buffer_ptr;
17
swissChili281af442024-11-14 23:30:29 -050018webview::webview *wp;
swissChiliab615d82024-03-08 17:15:13 -050019
20std::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>(),
swissChili4f0a0a82024-03-08 18:58:38 -050025 to = j[1].template get<std::string>(),
26 system = j[2].template get<std::string>();
swissChiliab615d82024-03-08 17:15:13 -050027
28 std::cout << from << ' ' << to << std::endl;
29
swissChili4f0a0a82024-03-08 18:58:38 -050030 do_a_conversion((char *)from.data(), to.size() > 0 ? (char *)to.data() : nullptr, (char *)system.data());
swissChiliab615d82024-03-08 17:15:13 -050031 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
37std::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
swissChili281af442024-11-14 23:30:29 -050041std::string copy(std::string j) {
42 std::cout << "copy called " << j << '\n';
43 return "undefined";
44}
45
46void 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
68extern char g_argv_0[128];
69
70int main(int argc, char **argv) {
71 strlcpy(g_argv_0, argv[0], 128);
swissChiliab615d82024-03-08 17:15:13 -050072 // cookie_io_functions_t iofns = {NULL};
73 // iofns.cookie_write_function_t = writer;
74 //
75 // stdout = fopencookie(NULL, "r", iofns);
76
77 try {
swissChili281af442024-11-14 23:30:29 -050078 webview::webview w(true, nullptr);
79 wp = &w;
swissChiliab615d82024-03-08 17:15:13 -050080 w.set_title("Units");
swissChili281af442024-11-14 23:30:29 -050081 w.set_size(480, 320, WEBVIEW_HINT_MIN);
swissChiliab615d82024-03-08 17:15:13 -050082 w.bind("convert", convert);
swissChili281af442024-11-14 23:30:29 -050083 w.bind("copy", copy);
84 w.eval("window.webview = {};");
swissChiliab615d82024-03-08 17:15:13 -050085 w.set_html(get_html());
86 // w.navigate("http://localhost:8000/index-webview.html");
swissChili281af442024-11-14 23:30:29 -050087
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
swissChiliab615d82024-03-08 17:15:13 -0500107 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}