tg/cmd/test/incdec.go

58 lines
1.2 KiB
Go
Raw Normal View History

2024-03-29 14:30:48 +03:00
package main
import (
"surdeus.su/core/tg"
2024-03-29 14:30:48 +03:00
"fmt"
)
// A simple example widget to show
// how to store and get session data values
// and working with dynamic panels.
2024-03-29 14:30:48 +03:00
var IncDecWidget = tg.RenderFunc(func(c tg.Context) tg.UI {
const format = "Press the buttons" +
"to increment and decrement.\n" +
2024-03-29 14:30:48 +03:00
"Current counter value = %d"
d := ExtractSessionData(c)
2024-03-29 14:30:48 +03:00
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}
}),
),
2024-03-29 14:30:48 +03:00
tg.Messagef("Use the reply keyboard to get back").Reply(
BackKeyboard.Reply(),
),
}
})