/
app_safe.go
170 lines (144 loc) · 2.92 KB
/
app_safe.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// ˅
package state
import (
"fmt"
"os"
"strconv"
"time"
"github.com/lxn/walk"
"github.com/lxn/walk/declarative"
)
// ˄
// Safe security system that the security status changes with time.
type AppSafe struct {
// ˅
// ˄
// Current state
state State
// Current time
textTime *walk.LineEdit
// Display of security center
textMessage *walk.TextEdit
// ˅
// ˄
}
func NewAppSafe() *AppSafe {
// ˅
var le *walk.LineEdit
var te *walk.TextEdit
var pb1 *walk.PushButton
var pb2 *walk.PushButton
var pb3 *walk.PushButton
appSafe := &AppSafe{
textTime: le,
textMessage: te,
state: NewDaytimeState(),
}
go appSafe.countTime()
_, err := declarative.MainWindow{
Title: "State Example",
MinSize: declarative.Size{Width: 300, Height: 300},
Size: declarative.Size{Width: 300, Height: 300},
Layout: declarative.VBox{},
Children: []declarative.Widget{
declarative.LineEdit{
AssignTo: &appSafe.textTime,
ReadOnly: true,
},
declarative.TextEdit{
AssignTo: &appSafe.textMessage,
ReadOnly: true,
VScroll: true,
},
declarative.Composite{
Layout: declarative.HBox{},
Children: []declarative.Widget{
declarative.PushButton{
AssignTo: &pb1,
Text: "Use",
OnClicked: appSafe.pressedUseButton,
},
declarative.PushButton{
AssignTo: &pb2,
Text: "Alarm",
OnClicked: appSafe.pressedAlarmButton,
},
declarative.PushButton{
AssignTo: &pb3,
Text: "Phone",
OnClicked: appSafe.pressedPhoneButton,
},
},
},
},
}.Run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return appSafe
// ˄
}
// Set time
func (a *AppSafe) SetTime(hour int) {
// ˅
var clockString string = "Current Time : "
if hour < 10 {
clockString += "0" + strconv.Itoa(hour) + ":00"
} else {
clockString += strconv.Itoa(hour) + ":00"
}
fmt.Println(clockString)
if a.textTime != nil {
_ = a.textTime.SetText(clockString)
}
a.state.SetTime(a, hour)
// ˄
}
// Change state
func (a *AppSafe) ChangeState(state State) {
// ˅
fmt.Println("The state changed from " + a.state.String() + " to " + state.String())
a.state = state
// ˄
}
// Call a security guard room
func (a *AppSafe) CallSecurityGuardsRoom(msg string) {
// ˅
a.textMessage.AppendText("call! " + msg + "\r\n")
// ˄
}
// Record security log
func (a *AppSafe) RecordSecurityLog(msg string) {
// ˅
a.textMessage.AppendText("record ... " + msg + "\r\n")
// ˄
}
func (a *AppSafe) pressedUseButton() {
// ˅
a.state.Use(a)
// ˄
}
func (a *AppSafe) pressedAlarmButton() {
// ˅
a.state.Alarm(a)
// ˄
}
func (a *AppSafe) pressedPhoneButton() {
// ˅
a.state.Phone(a)
// ˄
}
func (a *AppSafe) countTime() {
// ˅
for {
// Advance one hour for every second of real time.
for hour := 0; hour < 24; hour++ {
a.SetTime(hour) // Set the time
time.Sleep(1 * time.Second)
}
}
// ˄
}
// ˅
// ˄