win_bots.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // package win_bots -- окно управления ботами
  2. package win_bots
  3. import (
  4. _ "embed"
  5. "fmt"
  6. "log"
  7. "net/url"
  8. "runtime"
  9. "github.com/zserge/lorca"
  10. "wartank/pkg/types"
  11. )
  12. //go:embed win_bots.html
  13. var strWinHtml string
  14. // WinBots -- окно управления ботами
  15. type WinBots struct {
  16. desktop types.IDesktop
  17. store types.IStore
  18. win lorca.UI
  19. ws types.IWebSocket
  20. fnAdd func()
  21. fnView func(nameBot string)
  22. dictBot map[string]string // Список ботов
  23. }
  24. // NewWinBots -- возвращает новое окно управления ботами
  25. func NewWinBots(desktop types.IDesktop, fnAdd func(), fnView func(nameBot string), dictBot map[string]string) (*WinBots, error) {
  26. { // Предусловия
  27. if desktop == nil {
  28. return nil, fmt.Errorf("NewWinBots(): IDesktop == nil")
  29. }
  30. if fnAdd == nil {
  31. return nil, fmt.Errorf("NewWinBots(): fnAdd == nil")
  32. }
  33. if fnView == nil {
  34. return nil, fmt.Errorf("NewWinBots(): fnView == nil")
  35. }
  36. if dictBot == nil {
  37. return nil, fmt.Errorf("NewWinBots(): dictBot == nil")
  38. }
  39. }
  40. sf := &WinBots{
  41. desktop: desktop,
  42. store: desktop.Store(),
  43. ws: desktop.Ws(),
  44. fnAdd: fnAdd,
  45. fnView: fnView,
  46. dictBot: dictBot,
  47. }
  48. args := []string{}
  49. if runtime.GOOS == "linux" {
  50. args = append(args, "--class=Lorca")
  51. }
  52. var err error
  53. sf.win, err = lorca.New("data:text/html,"+url.PathEscape(strWinHtml), "", 640, 480, args...)
  54. if err != nil {
  55. return nil, fmt.Errorf("NewWinBots(): in create win, err=\n\t%w", err)
  56. }
  57. go sf.close()
  58. return sf, nil
  59. }
  60. // Обновляет список ботов
  61. func (sf *WinBots) UpdateList(dictBot map[string]string) {
  62. log.Println("WinBots.UpdateList()")
  63. sf.dictBot = dictBot
  64. sf.setBots()
  65. }
  66. // Работает в отдельном потоке, главный цикл окна
  67. func (sf *WinBots) Run() {
  68. log.Println("WinBots.Run()")
  69. sf.win.Bind("close_win", sf.onClose)
  70. sf.win.Bind("user_add", sf.onUsersAdd)
  71. sf.win.Bind("user_view", sf.onUsersAdd)
  72. sf.setBots()
  73. <-sf.win.Done() // Ожидание закрытия окна
  74. }
  75. // Заполняет список ботов
  76. func (sf *WinBots) setBots() {
  77. log.Println("WinBots.setBots()")
  78. strList := ""
  79. count := 0
  80. for key := range sf.dictBot {
  81. strCount := fmt.Sprint(count)
  82. strList += `<div style="color:#eee;margin-top:3px;" id="/bot/` + strCount + `">` + key +
  83. `&nbsp;&nbsp;&nbsp;<button type="button" name="add" value="Посм" onclick="bot_` + strCount + `()">Посмотреть</button></div>`
  84. sf.win.Bind("bot_"+strCount, func() {
  85. go sf.fnView(key)
  86. })
  87. count++
  88. }
  89. js := fmt.Sprintf(`
  90. function SetBotList(){
  91. var _el=document.getElementById("/bot/list");
  92. _el.innerHTML=%q
  93. }
  94. SetBotList()
  95. `, strList)
  96. sf.win.Eval(js)
  97. }
  98. // Добавляет бота по требованию
  99. func (sf *WinBots) onUsersAdd() {
  100. log.Printf("WinBots.onUsersAdd()\n")
  101. go sf.fnAdd()
  102. }
  103. // Закрывает приложение
  104. func (sf *WinBots) onClose() {
  105. log.Println("WinBots.onClose()")
  106. sf.win.Close()
  107. }
  108. // close -- ожидает отмены глобального контекста
  109. func (sf *WinBots) close() {
  110. <-sf.desktop.CtxApp().Done()
  111. log.Println("WinBots.close()")
  112. sf.win.Close()
  113. }