This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_dialog_ctx.go
115 lines (101 loc) · 2.6 KB
/
update_dialog_ctx.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
package main
import (
"fmt"
"github.com/anaminus/gxui"
"github.com/anaminus/gxui/math"
"io"
)
type UpdateDialogContext struct {
DataLocations *DataLocations
dialog Dialog
ctxc *ContextController
UpdateFinished func()
}
func (c *UpdateDialogContext) Entering(ctxc *ContextController) ([]gxui.Control, bool) {
driver := ctxc.Driver()
theme := ctxc.Theme()
c.dialog = CreateDialog(theme)
c.dialog.SetTitle("Updating Data...")
label := theme.CreateLabel()
pbar := theme.CreateProgressBar()
pbar.SetDesiredSize(math.Size{300, 24})
c.dialog.Container().AddChild(label)
c.dialog.Container().AddChild(pbar)
conn := Data.OnUpdateProgress(func(v ...interface{}) {
driver.Call(func() {
name := v[0].(string)
progress := v[1].(int64)
total := v[2].(int64)
err, _ := v[3].(error)
if err == io.EOF {
label.SetText(fmt.Sprintf("Download of %s completed.", name))
} else if err != nil {
} else {
if total < 0 {
pbar.SetTarget(1)
pbar.SetProgress(1)
label.SetText(fmt.Sprintf("Downloading %s...", name))
} else {
pbar.SetTarget(int(total))
pbar.SetProgress(int(progress))
label.SetText(fmt.Sprintf("Downloading %s (%3.2f%%)...", name, float64(progress)/float64(total)*100))
}
}
})
})
var canceled bool
var actionCancel int
actionCancel = c.dialog.AddAction("Cancel", true, func() {
canceled = true
conn.Disconnect()
Data.CancelUpdate()
label.SetText("Update canceled.")
c.dialog.RemoveAction(actionCancel)
c.dialog.AddAction("Close", true, func() {
ctxc.ExitContext()
})
})
go func() {
var err error
if c.DataLocations == nil {
err = Data.Update(new(DataLocations).FromSettings(Settings))
} else {
err = Data.Update(c.DataLocations)
}
driver.CallSync(func() {
pbar.SetTarget(1)
pbar.SetProgress(0)
if canceled {
return
}
conn.Disconnect()
if err != nil {
label.SetText(err.Error())
} else {
label.SetText("Update successful.")
}
c.dialog.RemoveAction(actionCancel)
c.dialog.AddAction("Close", true, func() {
ctxc.ExitContext()
})
})
}()
return []gxui.Control{c.dialog.Control()}, true
}
func (c *UpdateDialogContext) Exiting(*ContextController) {
if c.UpdateFinished != nil {
c.UpdateFinished()
}
}
func (c *UpdateDialogContext) IsDialog() bool {
return true
}
func (c *UpdateDialogContext) Direction() gxui.Direction {
return gxui.TopToBottom
}
func (c *UpdateDialogContext) HorizontalAlignment() gxui.HorizontalAlignment {
return gxui.AlignCenter
}
func (c *UpdateDialogContext) VerticalAlignment() gxui.VerticalAlignment {
return gxui.AlignMiddle
}