-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhello_world.nim
116 lines (87 loc) · 2.43 KB
/
hello_world.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import webui
# HTML
const loginHtml = """
<!DOCTYPE html>
<html>
<head>
<title>WebUI 2 - Nim Example</title>
<style>
body {
color: white;
background: #0F2027;
background: -webkit-linear-gradient(to right, #2C5364, #203A43, #0F2027);
background: linear-gradient(to right, #2C5364, #203A43, #0F2027);
text-align: center;
font-size: 18px;
font-family: sans-serif;
}
</style>
<script src="/webui.js"></script>
</head>
<body>
<h1>WebUI 2 - Nim Example</h1>
<br>
<input type="password" id="MyInput" OnKeyUp="document.getElementById('err').innerHTML=' ';" autocomplete="off">
<br>
<h3 id="err" style="color: #dbdd52"> </h3>
<br>
<button id="CheckPassword">Check Password</button> - <button id="Exit">Exit</button>
</body>
</html>
"""
const dashboardHtml = """
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
<style>
body {
color: white;
background: #0F2027;
background: -webkit-linear-gradient(to right, #4e99bb, #2c91b5, #07587a);
background: linear-gradient(to right, #4e99bb, #2c91b5, #07587a);
text-align: center;
font-size: 18px;
font-family: sans-serif;
}
</style>
<script src="/webui.js"></script>
</head>
<body>
<h1>Welcome !</h1>
<br>
<br>
<button id="Exit">Exit</button>
</body>
</html>
"""
proc main =
# Create a window
let window = newWindow()
window.bind("CheckPassword") do (e: Event): # Check the password function
# This function gets called every time the user clicks on "MyButton1"
# Run the JavaScript on the UI (Web Browser)
var js = e.window.script("return document.getElementById(\"MyInput\").value;")
# Check if there is any JavaScript error
if js.error:
echo "JavaScript Error: ", js.data
return
# Get the password
let password = js.data
# Check the password
if password == "123456":
# Correct password
echo "Password is correct."
e.window.show(dashboardHtml)
else:
# Wrong password
echo "Wrong password: ", password
discard e.window.script("document.getElementById('err').innerHTML = 'Sorry. Wrong password';")
window.bind("Exit") do (_: Event):
# Close all opened windows
webui.exit()
# Show the window
window.show(loginHtml)
# Wait until all windows get closed
wait()
main()