tg/cmd/test/incdec.go

57 lines
1.2 KiB
Go

package main
import (
"surdeus.su/core/tg"
"fmt"
)
// A simple example widget to show
// how to store and get session data values
// and working with dynamic panels.
var IncDecWidget = tg.RenderFunc(func(c tg.Context) tg.UI {
const format = "Press the buttons" +
"to increment and decrement.\n" +
"Current counter value = %d"
d := ExtractSessionData(c)
return tg.UI{
tg.Messagef(format, d.Counter).Panel(
c,
tg.PanelFunc(func(
panel *tg.PanelCompo,
c tg.Context,
) []tg.ButtonRow {
d := ExtractSessionData(c)
row := tg.ButtonRow{}
if d.Counter != -5 {
row = append(
row,
tg.Buttonf(
"-",
).WithAction(tg.Func(func(c tg.Context){
d.Counter--
panel.Text = fmt.Sprintf(format, d.Counter)
c.Update(panel)
})),
)
}
if d.Counter != +5 {
row = append(
row,
tg.Buttonf(
"+",
).WithAction(tg.Func(func(c tg.Context){
d.Counter++
panel.Text = fmt.Sprintf(format, d.Counter)
c.Update(panel)
})),
)
}
return []tg.ButtonRow{row}
}),
),
tg.Messagef("Use the reply keyboard to get back").Reply(
BackKeyboard.Reply(),
),
}
})