/
main.go
114 lines (96 loc) · 2.66 KB
/
main.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
package main
import (
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
"github.com/PacktPublishing/Hands-On-GUI-Application-Development-in-Go/client"
)
type mainUI struct {
app fyne.App
server client.EmailServer
list *widget.Group
content, subject, to, from, date *widget.Label
}
func (m *mainUI) buildToolbar() *widget.Toolbar {
return widget.NewToolbar(
widget.NewToolbarAction(theme.MailComposeIcon(), func() {
newCompose(m.app, m.server).loadUI().Show()
}),
widget.NewToolbarAction(theme.MailReplyIcon(), func() {
}),
widget.NewToolbarAction(theme.MailReplyAllIcon(), func() {
}),
widget.NewToolbarSeparator(),
widget.NewToolbarAction(theme.DeleteIcon(), func() {
}),
widget.NewToolbarAction(theme.ContentCutIcon(), func() {
}),
widget.NewToolbarAction(theme.ContentCopyIcon(), func() {
}),
widget.NewToolbarAction(theme.ContentPasteIcon(), func() {
}),
)
}
func (m *mainUI) loadUI() fyne.Window {
browse := m.app.NewWindow("GoMail")
toolbar := m.buildToolbar()
m.list = widget.NewGroup("Inbox")
for _, email := range m.server.ListMessages() {
m.list.Append(m.addEmail(email))
}
m.content = widget.NewLabel("content")
m.subject = widget.NewLabel("subject")
m.subject.TextStyle = fyne.TextStyle{Bold: true}
meta := widget.NewForm()
m.to = widget.NewLabel("email")
meta.Append("To", m.to)
m.from = widget.NewLabel("email")
meta.Append("From", m.from)
m.date = widget.NewLabel("date")
meta.Append("Date", m.date)
box := widget.NewVBox(meta, m.content)
detail := fyne.NewContainerWithLayout(
layout.NewBorderLayout(m.subject, nil, box, nil),
m.subject, box)
container := fyne.NewContainerWithLayout(
layout.NewBorderLayout(toolbar, nil, m.list, nil),
toolbar, m.list, detail,
)
browse.SetContent(container)
browse.Resize(fyne.NewSize(600, 400))
return browse
}
func (m *mainUI) setMessage(email *client.EmailMessage) {
m.subject.SetText(email.Subject)
m.to.SetText(email.ToEmailString())
m.from.SetText(email.FromEmailString())
m.date.SetText(email.DateString())
m.content.SetText(email.Content)
}
func (m *mainUI) addEmail(email *client.EmailMessage) fyne.CanvasObject {
return widget.NewButton(email.Subject, func() {
m.setMessage(email)
})
}
func newMainUI(mailApp fyne.App, server client.EmailServer) *mainUI {
ui := &mainUI{
app: mailApp,
server: server,
}
return ui
}
func main() {
mailApp := app.New()
server := client.NewTestServer()
ui := newMainUI(mailApp, server)
win := ui.loadUI()
go func() {
for email := range server.Incoming() {
ui.list.Prepend(ui.addEmail(email))
}
}()
ui.setMessage(server.CurrentMessage())
win.ShowAndRun()
}