|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "time" |
| 4 | + "fmt" |
5 | 5 |
|
6 | 6 | ui "github.com/webui-dev/go-webui/v2" |
7 | 7 | ) |
8 | 8 |
|
| 9 | +// Those constants are to avoid using global variables |
| 10 | +// you can also use `const w ui.Window = 1`. |
| 11 | +// A window ID can be between 1 and 512 (WEBUI_MAX_IDS) |
9 | 12 | const ( |
10 | | - w = ui.Window(1) |
11 | | - w2 = ui.Window(2) |
| 13 | + MyWindow = ui.Window(1) |
| 14 | + MySecondWindow = ui.Window(2) |
12 | 15 | ) |
13 | 16 |
|
| 17 | +func exitApp(e ui.Event) any { |
| 18 | + // Close all opened windows |
| 19 | + ui.Exit() |
| 20 | + return nil |
| 21 | +} |
| 22 | + |
14 | 23 | func events(e ui.Event) any { |
| 24 | + // This function gets called every time |
| 25 | + // there is an event |
| 26 | + |
15 | 27 | if e.EventType == ui.Connected { |
16 | | - println("Connected.") |
| 28 | + fmt.Println("Connected.") |
17 | 29 | } else if e.EventType == ui.Disconnected { |
18 | | - println("Disconnected.") |
| 30 | + fmt.Println("Disconnected.") |
19 | 31 | } else if e.EventType == ui.MouseClick { |
20 | | - println("Click.") |
| 32 | + fmt.Println("Click.") |
21 | 33 | } else if e.EventType == ui.Navigation { |
22 | | - target, _ := ui.GetArg[string](e) |
23 | | - println("Starting navigation to: ", target) |
24 | | - // Since we bind all events, following `href` links is blocked by WebUI. |
25 | | - // To control the navigation, we need to use `Navigate()`. |
26 | | - e.Window.Navigate(target) |
| 34 | + url, _ := ui.GetArg[string](e) |
| 35 | + fmt.Printf("Starting navigation to: %s\n", url) |
| 36 | + |
| 37 | + // Because we used `w.Bind("", events)` |
| 38 | + // WebUI will block all `href` link clicks and sent here instead. |
| 39 | + // We can then control the behaviour of links as needed. |
| 40 | + e.Window.Navigate(url) |
27 | 41 | } |
28 | 42 | return nil |
29 | 43 | } |
30 | 44 |
|
31 | 45 | func switchToSecondPage(e ui.Event) any { |
| 46 | + // This function gets called every |
| 47 | + // time the user clicks on "SwitchToSecondPage" |
| 48 | + |
| 49 | + // Switch to `/second.html` in the same opened window. |
32 | 50 | e.Window.Show("second.html") |
33 | 51 | return nil |
34 | 52 | } |
35 | 53 |
|
36 | 54 | func showSecondWindow(e ui.Event) any { |
37 | | - w2.Show("second.html") |
38 | | - // Remove the Go Back button when showing the second page in another window. |
39 | | - // Wait max. 10 seconds until the window is recognized as shown. |
40 | | - for i := 0; i < 1000; i++ { |
41 | | - if w2.IsShown() { |
42 | | - break |
43 | | - } |
44 | | - // Slow down check interval to reduce load. |
45 | | - time.Sleep(10 * time.Millisecond) |
46 | | - } |
47 | | - if !w2.IsShown() { |
48 | | - return nil |
49 | | - } |
50 | | - // Let the DOM load. |
51 | | - time.Sleep(50 * time.Millisecond) |
52 | | - // Remove `Go Back` button. |
53 | | - w2.Run("document.getElementById('go-back').remove();") |
| 55 | + // This function gets called every |
| 56 | + // time the user clicks on "OpenNewWindow" |
54 | 57 |
|
| 58 | + // Show a new window, and navigate to `/second.html` |
| 59 | + // if it's already open, then switch in the same window |
| 60 | + MySecondWindow.Show("second.html") |
55 | 61 | return nil |
56 | 62 | } |
57 | 63 |
|
58 | | -func exit(e ui.Event) any { |
59 | | - ui.Exit() |
60 | | - return nil |
| 64 | +// Counter for dynamic content |
| 65 | +var count int |
| 66 | + |
| 67 | +func myFilesHandler(filename string) ([]byte, int) { |
| 68 | + fmt.Printf("File: %s\n", filename) |
| 69 | + |
| 70 | + if filename == "/test.txt" { |
| 71 | + // Const static file example |
| 72 | + response := "HTTP/1.1 200 OK\r\n" + |
| 73 | + "Content-Type: text/html\r\n" + |
| 74 | + "Content-Length: 99\r\n\r\n" + |
| 75 | + "<html>" + |
| 76 | + " This is a static embedded file content example." + |
| 77 | + " <script src=\"webui.js\"></script>" + // To keep connection with WebUI |
| 78 | + "</html>" |
| 79 | + return []byte(response), len(response) |
| 80 | + } else if filename == "/dynamic.html" { |
| 81 | + // Dynamic file example |
| 82 | + |
| 83 | + // Generate body |
| 84 | + count++ |
| 85 | + body := fmt.Sprintf( |
| 86 | + "<html>"+ |
| 87 | + " This is a dynamic file content example. <br>"+ |
| 88 | + " Count: %d <a href=\"dynamic.html\">[Refresh]</a><br>"+ |
| 89 | + " <script src=\"webui.js\"></script>"+ // To keep connection with WebUI |
| 90 | + "</html>", |
| 91 | + count, |
| 92 | + ) |
| 93 | + |
| 94 | + // Generate header + body |
| 95 | + response := fmt.Sprintf( |
| 96 | + "HTTP/1.1 200 OK\r\n"+ |
| 97 | + "Content-Type: text/html\r\n"+ |
| 98 | + "Content-Length: %d\r\n\r\n"+ |
| 99 | + "%s", |
| 100 | + len(body), body, |
| 101 | + ) |
| 102 | + |
| 103 | + return []byte(response), len(response) |
| 104 | + } |
| 105 | + |
| 106 | + // Other files: |
| 107 | + // A nil return will make WebUI |
| 108 | + // look for the file locally. |
| 109 | + return nil, 0 |
61 | 110 | } |
62 | 111 |
|
63 | 112 | func main() { |
64 | 113 | ui.SetDefaultRootFolder("ui") |
| 114 | + |
| 115 | + // Create new windows |
| 116 | + MyWindow.NewWindow() |
| 117 | + MySecondWindow.NewWindow() |
| 118 | + |
| 119 | + // Bind HTML element IDs with Go functions |
| 120 | + MyWindow.Bind("SwitchToSecondPage", switchToSecondPage) |
| 121 | + MyWindow.Bind("OpenNewWindow", showSecondWindow) |
| 122 | + MyWindow.Bind("Exit", exitApp) |
| 123 | + MySecondWindow.Bind("Exit", exitApp) |
| 124 | + |
| 125 | + // Bind events |
| 126 | + MyWindow.Bind("", events) |
65 | 127 |
|
66 | | - w.NewWindow() |
| 128 | + // Set the `.ts` and `.js` runtime |
| 129 | + // MyWindow.SetRuntime(ui.Nodejs) |
| 130 | + // MyWindow.SetRuntime(ui.Bun) |
| 131 | + MyWindow.SetRuntime(ui.Deno) |
67 | 132 |
|
68 | | - // Bind HTML elements to functions |
69 | | - w.Bind("switch-to-second-page", switchToSecondPage) |
70 | | - w.Bind("open-new-window", showSecondWindow) |
71 | | - w.Bind("exit", exit) |
72 | | - // Bind all events. |
73 | | - w.Bind("", events) |
| 133 | + // Set a custom files handler |
| 134 | + MyWindow.SetFileHandler(myFilesHandler) |
74 | 135 |
|
75 | | - // Show the main window. |
76 | | - w.Show("index.html") |
| 136 | + // Set window size |
| 137 | + MyWindow.SetSize(800, 800) |
77 | 138 |
|
78 | | - // Prepare the second window. |
79 | | - w2.NewWindow() |
80 | | - w2.Bind("exit", exit) |
| 139 | + // Set window position |
| 140 | + MyWindow.SetPosition(200, 200) |
81 | 141 |
|
| 142 | + // Show a new window |
| 143 | + // MyWindow.SetRootFolder("_MY_PATH_HERE_") |
| 144 | + // MyWindow.ShowBrowser("index.html", ui.Chrome) |
| 145 | + MyWindow.Show("index.html") |
| 146 | + |
| 147 | + // Wait until all windows get closed |
82 | 148 | ui.Wait() |
| 149 | + |
| 150 | + // Free all memory resources (Optional) |
| 151 | + ui.Clean() |
83 | 152 | } |
0 commit comments