panel.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package tg
  2. // Using the interface and all related is
  3. // deprecated. Use the Paneler interface and function.
  4. type Rowser interface {
  5. MakeRows(c Context) []ButtonRow
  6. }
  7. type RowserFunc func(c Context) []ButtonRow
  8. func (fn RowserFunc) MakeRows(c Context) []ButtonRow {
  9. return fn(c)
  10. }
  11. type Paneler interface {
  12. GetPanelRows(*PanelCompo, Context) []ButtonRow
  13. }
  14. type PanelFunc func(*PanelCompo, Context) []ButtonRow
  15. func (fn PanelFunc) GetPanelRows(
  16. panel *PanelCompo, c Context,
  17. ) []ButtonRow {
  18. return fn(panel, c)
  19. }
  20. // The type represents the inline panel with
  21. // scrollable via buttons content.
  22. // Can be used for example to show users via SQL and offset
  23. // or something like that.
  24. type PanelCompo struct {
  25. InlineCompo
  26. Paneler Paneler
  27. }
  28. // Transform to the panel with dynamic rows.
  29. func (compo *MessageCompo) Panel(
  30. c Context, // The context to generate the first page of buttons.
  31. paneler Paneler, // The rows generator.
  32. ) *PanelCompo {
  33. ret := &PanelCompo{}
  34. ret.Paneler = paneler
  35. ret.InlineCompo = (*compo.Inline(
  36. NewKeyboard(
  37. ret.Paneler.GetPanelRows(ret, c)...,
  38. ).Inline(),
  39. ))
  40. return ret
  41. }
  42. // Implementing the Updater.
  43. func (panel *PanelCompo) Update(c Context) error {
  44. panel.Rows = panel.Paneler.GetPanelRows(panel, c)
  45. return panel.InlineCompo.Update(c)
  46. }