-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
recv.go
110 lines (91 loc) · 2.97 KB
/
recv.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
package bridge
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
"github.com/Jacalz/wormhole-gui/v2/internal/transport"
)
// RecvItem is the item that is being received
type RecvItem struct {
URI fyne.URI
Status string
Name string
}
// RecvList is a list of progress bars that track send progress.
type RecvList struct {
widget.List
client *transport.Client
Items []*RecvItem
}
// Length returns the length of the data.
func (p *RecvList) Length() int {
return len(p.Items)
}
// CreateItem creates a new item in the list.
func (p *RecvList) CreateItem() fyne.CanvasObject {
return container.New(&listLayout{}, widget.NewFileIcon(nil), widget.NewLabel("Waiting for filename..."), newRecvProgress())
}
// UpdateItem updates the data in the list.
func (p *RecvList) UpdateItem(i int, item fyne.CanvasObject) {
item.(*fyne.Container).Objects[0].(*widget.FileIcon).SetURI(p.Items[i].URI)
item.(*fyne.Container).Objects[1].(*widget.Label).SetText(p.Items[i].Name)
item.(*fyne.Container).Objects[2].(*fyne.Container).Objects[0].(*recvProgress).setStatus(p.Items[i].Status)
}
// RemoveItem removes the item at the specified index.
func (p *RecvList) RemoveItem(i int) {
copy(p.Items[i:], p.Items[i+1:])
p.Items[p.Length()-1] = nil // Make sure that GC run on removed element
p.Items = p.Items[:p.Length()-1]
p.Refresh()
}
// OnSelected handles removing items and stopping send (in the future)
func (p *RecvList) OnSelected(i int) {
dialog.ShowConfirm("Remove from list", "Do you wish to remove the item from the list?", func(remove bool) {
if remove {
p.RemoveItem(i)
p.Refresh()
}
}, fyne.CurrentApp().Driver().AllWindows()[0])
p.Unselect(i)
}
// NewReceive adds data about a new send to the list and then returns the channel to update the code.
func (p *RecvList) NewReceive(code string) {
p.Items = append(p.Items, &RecvItem{Name: "Waiting for filename..."})
p.Refresh()
path := make(chan string)
index := p.Length() - 1
go func() {
name := <-path
p.Items[index].URI = storage.NewFileURI(name)
if name != "text" {
p.Items[index].Name = p.Items[index].URI.Name()
} else {
p.Items[index].Name = "Text Snippet"
}
close(path)
p.Refresh()
}()
go func(code string) {
if err := p.client.NewReceive(code, path); err != nil {
p.Items[index].Status = "Failed"
p.client.ShowNotification("Receive failed", "An error occurred when receiving the data.")
dialog.ShowError(err, fyne.CurrentApp().Driver().AllWindows()[0])
} else {
p.Items[index].Status = "Completed"
p.client.ShowNotification("Receive completed", "The data was received successfully.")
}
p.Refresh()
}(code)
}
// NewRecvList greates a list of progress bars.
func NewRecvList(client *transport.Client) *RecvList {
p := &RecvList{client: client}
p.List.Length = p.Length
p.List.CreateItem = p.CreateItem
p.List.UpdateItem = p.UpdateItem
p.List.OnSelected = p.OnSelected
p.ExtendBaseWidget(p)
return p
}