tg/cmd/test/incdec.go

64 lines
1.3 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 {
var (
kbd *tg.InlineCompo
//cntMsg *tg.MessageCompo
inline, std, onlyInc, onlyDec tg.Inline
)
d := ExtractSessionData(c)
format := "Press the buttons to increment and decrement.\n" +
"Current counter value = %d"
incBtn := tg.Buttonf("+").WithAction(tg.Func(func(c tg.Context) {
d.Counter++
kbd.Text = fmt.Sprintf(format, d.Counter)
if d.Counter == 5 {
kbd.Inline = onlyDec
} else {
kbd.Inline = std
}
kbd.Update(c)
}))
decBtn := tg.Buttonf("-").WithAction(tg.Func(func(c tg.Context) {
d.Counter--
kbd.Text = fmt.Sprintf(format, d.Counter)
if d.Counter == -5 {
kbd.Inline = onlyInc
} else {
kbd.Inline = std
}
kbd.Update(c)
//c.Sendf("%d", d.Counter)
}))
onlyInc = tg.NewKeyboard().Row(incBtn).Inline()
onlyDec = tg.NewKeyboard().Row(decBtn).Inline()
std = tg.NewKeyboard().Row(incBtn, decBtn).Inline()
if d.Counter == 5 {
inline = onlyDec
} else if d.Counter == -5 {
inline = onlyInc
} else {
inline = std
}
kbd = tg.Messagef(format, d.Counter).Inline(inline)
return tg.UI{
kbd,
tg.Messagef("Use the reply keyboard to get back").Reply(
BackKeyboard.Reply(),
),
}
})