whispervis/widgets/loader.go

61 lines
985 B
Go
Raw Normal View History

2018-09-06 11:45:13 +03:00
package widgets
import (
"fmt"
"github.com/gopherjs/vecty"
"github.com/gopherjs/vecty/elem"
)
type Loader struct {
vecty.Core
steps int
current int
}
func (l *Loader) Render() vecty.ComponentOrHTML {
2018-09-06 15:15:55 +03:00
text := l.text()
2018-09-06 11:45:13 +03:00
return elem.Div(
2018-09-06 15:15:55 +03:00
vecty.Markup(
vecty.Style("text-align", "center"),
vecty.Style("position", "relative"),
vecty.Style("top", "50%"),
),
elem.Heading1(
vecty.Text(text),
),
2018-09-06 11:45:13 +03:00
)
}
func NewLoader(steps int) *Loader {
return &Loader{
steps: steps,
}
}
func (l *Loader) Inc() {
l.current++
}
2018-09-06 18:37:46 +03:00
func (l *Loader) Steps() int {
return l.steps
}
2018-09-06 11:45:13 +03:00
// Progress reports loader's progress in percentage.
func (l *Loader) Progress() float64 {
return 100 * float64(l.current) / float64(l.steps)
}
2018-09-06 15:15:55 +03:00
// Text returns formatted string to display.
func (l *Loader) text() string {
var text string
progress := l.Progress()
if progress > 99.9 {
text = "Completed"
} else {
text = fmt.Sprintf("Loading %.0f%%...", progress)
}
return text
}