display-test/api/pages/initpage.go

78 lines
1.7 KiB
Go
Raw Permalink Normal View History

2024-09-29 18:09:59 +00:00
package pages
import (
"fmt"
"gitea.unprism.ru/yotia/display-test/components"
"gitea.unprism.ru/yotia/display-test/drawer"
)
type initPage struct {
drawer drawer.Drawer // Drawer with dysplay
title components.Text
bar components.ProgressBar
}
// Only functions that control content of page
type InitPageContent interface {
SetTitle(title string)
GetProgressBar() components.ProgressBar
}
type InitPage interface {
Page
InitPageContent
}
func NewInitPage(d drawer.Drawer) (InitPage, error) {
if err := d.GetDisplay().IsReady(); err != nil {
return nil, fmt.Errorf("display is ready: %w", err)
}
title, err := components.NewText(d, 0, 0)
if err != nil {
return nil, fmt.Errorf("create title: %w", err)
}
pb, err := components.NewProgressBar(d, 0, drawer.LineH*3, d.W(), drawer.LineH) // Bottom
if err != nil {
return nil, fmt.Errorf("create progress bar: %w", err)
}
return &initPage{
drawer: d,
title: title,
bar: pb,
}, nil
}
func (p *initPage) Activate() {
// Draw
p.drawer.Clear()
p.title.Draw()
p.bar.Draw()
}
func (p *initPage) Diactivate() {
// Do not clear because next page will have to clear whole screen any way
}
func (p *initPage) SetTitle(title string) {
p.title.SetPos((p.drawer.W()-len(title)*(drawer.FontCharW+drawer.CharGap))/2, drawer.FontCharH) // Text in center
p.title.SetStr(title)
}
func (p *initPage) GetProgressBar() components.ProgressBar {
return p.bar
}
func (p *initPage) Close() (outErr error) {
// TODO Not the best way...
if err := p.title.Close(); err != nil {
outErr = fmt.Errorf("title close: %w:", err)
}
if err := p.bar.Close(); err != nil {
outErr = fmt.Errorf("progress bar close: %w:", err)
}
return
}