66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"vultras.su/core/tg"
|
|
"fmt"
|
|
)
|
|
|
|
const (
|
|
IncDecPath tg.Path = "inc-dec"
|
|
)
|
|
|
|
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(),
|
|
),
|
|
}
|
|
})
|
|
|