itd/cmd/itgui/progress.go

65 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-05-05 21:00:49 +00:00
package main
import (
"fmt"
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
type progress struct {
2022-11-16 03:20:34 +00:00
lbl *widget.Label
progLbl *widget.Label
pb *widget.ProgressBar
2022-05-05 21:00:49 +00:00
*widget.PopUp
}
func newProgress(w fyne.Window) progress {
out := progress{}
2022-11-16 03:20:34 +00:00
out.lbl = widget.NewLabel("")
out.lbl.Hide()
2022-05-05 21:00:49 +00:00
// Create label to show how many bytes transfered and center it
2022-11-16 03:20:34 +00:00
out.progLbl = widget.NewLabel("0 / 0 B")
out.progLbl.Alignment = fyne.TextAlignCenter
2022-05-05 21:00:49 +00:00
// Create new progress bar
out.pb = widget.NewProgressBar()
// Create new rectangle to set the size of the popup
sizeRect := canvas.NewRectangle(color.Transparent)
sizeRect.SetMinSize(fyne.NewSize(300, 50))
2022-05-05 21:00:49 +00:00
// Create vbox for label and progress bar
2022-11-16 03:20:34 +00:00
l := container.NewVBox(out.lbl, out.progLbl, out.pb)
2022-05-05 21:00:49 +00:00
// Create popup
out.PopUp = widget.NewModalPopUp(container.NewMax(l, sizeRect), w.Canvas())
return out
}
2022-11-16 03:20:34 +00:00
func (p progress) SetText(s string) {
p.lbl.SetText(s)
if s == "" {
p.lbl.Hide()
} else {
p.lbl.Show()
}
}
2022-05-05 21:00:49 +00:00
func (p progress) SetTotal(v float64) {
p.pb.Max = v
p.pb.Refresh()
2022-11-16 03:20:34 +00:00
p.progLbl.SetText(fmt.Sprintf("%.0f / %.0f B", p.pb.Value, v))
2022-05-05 21:00:49 +00:00
}
func (p progress) SetValue(v float64) {
p.pb.SetValue(v)
2022-11-16 03:20:34 +00:00
p.progLbl.SetText(fmt.Sprintf("%.0f / %.0f B", v, p.pb.Max))
2022-05-05 21:00:49 +00:00
}