-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
93 lines (81 loc) · 2.22 KB
/
main.swift
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
import SwiftWebUI
let doc = """
<!doctype html>
<html>
<head>
<title>Call Swift from JavaScript Example</title>
<script src="webui.js"></script>
<style>
body {
background: linear-gradient(to left, #36265a, #654da9);
color: AliceBlue;
font: 16px sans-serif;
text-align: center;
margin-top: 30px;
}
button {
margin: 5px 0 10px;
}
</style>
</head>
<body>
<h1>WebUI - Call Swift from JavaScript</h1>
<br />
<p>
Call Swift functions with arguments (<em>See the logs in your terminal</em>)
</p>
<button onclick="webui.handleStr('Hello', 'World');">Call handle_str()</button>
<br />
<button onclick="webui.handleInt(123, 456, 789);">Call handleInt()</button>
<br />
<button onclick="webui.handleBool(true, false);">Call handleBool()</button>
<br />
<p>Call a Swift function that returns a response</p>
<button onclick="getRespFromSwift();">Call handleResponse()</button>
<div>Double: <input type="text" id="input-number" value="2" /></div>
<script>
async function getRespFromSwift() {
const input = document.getElementById('input-number');
const number = input.value;
const result = await webui.handleResp(number);
input.value = result;
}
</script>
</body>
</html>
"""
func handleStr(e: Event) {
let str1: String = try! e.getArg()
let str2: String = try! e.getArg(1)
print("handleStr 1: \(str1)") // Hello
print("handleStr 2: \(str2)") // World
}
func handleInt(e: Event) {
let num1: Int = try! e.getArg()
let num2: Int = try! e.getArg(1)
let num3: Int = try! e.getArg(2)
print("handleInt 1: \(num1)") // 123
print("handleInt 2: \(num2)") // 456
print("handleInt 3: \(num3)") // 789
}
func handleBool(e: Event) {
let status1: Bool = try! e.getArg()
let status2: Bool = try! e.getArg(1)
print("handleBool 1: \(status1)") // true
print("handleBool 2: \(status2)") // false
}
func handleResp(e: Event) -> Int {
let count: Int = try! e.getArg()
return count * 2
}
// Create a new window.
let win = newWindow()
// Bind Swift functions.
win.bind("handleStr", handleStr)
win.bind("handleInt", handleInt)
win.bind("handleBool", handleBool)
win.bind("handleResp", handleResp)
// Show html frontend.
try! win.show(doc)
// Wait until all windows get closed.
wait()