/
send.go
100 lines (79 loc) · 2.72 KB
/
send.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
package main
import (
"fyne.io/fyne"
"fyne.io/fyne/dialog"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
"github.com/Jacalz/wormhole-gui/bridge"
"github.com/Jacalz/wormhole-gui/widgets"
)
func (ad *appData) sendTab() *widget.TabItem {
fileChoice := widget.NewButtonWithIcon("File", theme.FileIcon(), nil)
textChoice := widget.NewButtonWithIcon("Text", theme.DocumentCreateIcon(), nil)
// TODO: Add support for sending directories when fyne supports it in the file picker.
choiceContent := widget.NewVBox(fileChoice, textChoice)
contentPicker := dialog.NewCustom("Pick a content type", "Cancel", choiceContent, ad.Window)
contentPicker.Hide()
sendGrid := fyne.NewContainerWithLayout(layout.NewGridLayout(3), widgets.NewBoldLabel("Filename"), widgets.NewBoldLabel("Code"), widgets.NewBoldLabel("Progress"))
fileChoice.OnTapped = func() {
go func() {
contentPicker.Hide()
dialog.ShowFileOpen(func(file fyne.URIReadCloser, err error) {
if err != nil {
fyne.LogError("Error on selecting file to send", err)
dialog.ShowError(err, ad.Window)
return
} else if file == nil {
return
}
code := make(chan string)
progress := bridge.NewSendProgress()
go func() {
err = ad.Bridge.SendFile(file, code, progress.Update)
if err != nil {
dialog.ShowError(err, ad.Window)
} else if ad.Notifications {
ad.App.SendNotification(fyne.NewNotification("Send completed", "The file was sent successfully"))
}
}()
codeLabel := widgets.NewCodeLabel(code)
sendGrid.AddObject(widget.NewLabel(file.Name()))
sendGrid.AddObject(codeLabel)
sendGrid.AddObject(progress)
}, ad.Window)
}()
}
textChoice.OnTapped = func() {
go func() {
contentPicker.Hide()
text := make(chan string)
ad.Bridge.EnterSendText(ad.App, text)
t := <-text
if t == "" {
return
}
progress := bridge.NewSendProgress()
code := make(chan string)
go func() {
err := ad.Bridge.SendText(t, code)
if err != nil {
dialog.ShowError(err, ad.Window)
} else if ad.Notifications {
ad.App.SendNotification(fyne.NewNotification("Send completed", "The sending of text completed successfully"))
} else {
progress.SetValue(1)
}
}()
codeLabel := widgets.NewCodeLabel(code)
sendGrid.AddObject(widget.NewLabel("Text Snippet"))
sendGrid.AddObject(codeLabel)
sendGrid.AddObject(progress)
}()
}
send := widget.NewButtonWithIcon("Add content to send", theme.ContentAddIcon(), func() {
contentPicker.Show()
})
sendContent := fyne.NewContainerWithLayout(layout.NewVBoxLayout(), send, widget.NewLabel(""), sendGrid)
return widget.NewTabItemWithIcon("Send", theme.MailSendIcon(), widget.NewScrollContainer(sendContent))
}