graphics.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package choose_fonts
  2. import (
  3. "fmt"
  4. "strings"
  5. "kitty/tools/tui/graphics"
  6. "kitty/tools/tui/loop"
  7. "kitty/tools/utils"
  8. )
  9. var _ = fmt.Print
  10. type image struct {
  11. id, image_number uint32
  12. current_file string
  13. }
  14. func (i image) new_graphics_command() *graphics.GraphicsCommand {
  15. gc := &graphics.GraphicsCommand{}
  16. if i.id > 0 {
  17. gc.SetImageId(i.id)
  18. } else {
  19. gc.SetImageNumber(i.image_number)
  20. }
  21. return gc
  22. }
  23. type graphics_manager struct {
  24. main, bold, italic, bi, extra image
  25. lp *loop.Loop
  26. images [5]*image
  27. }
  28. func (g *graphics_manager) initialize(lp *loop.Loop) {
  29. g.images = [5]*image{&g.main, &g.bold, &g.italic, &g.bi, &g.extra}
  30. g.lp = lp
  31. payload := []byte("123")
  32. buf := strings.Builder{}
  33. gc := &graphics.GraphicsCommand{}
  34. gc.SetImageNumber(7891230).SetTransmission(graphics.GRT_transmission_direct).SetDataWidth(1).SetDataHeight(1).SetFormat(
  35. graphics.GRT_format_rgb).SetDataSize(uint64(len(payload)))
  36. d := func() uint32 {
  37. im := gc.ImageNumber()
  38. im++
  39. gc.SetImageNumber(im)
  40. _ = gc.WriteWithPayloadTo(&buf, payload)
  41. return im
  42. }
  43. for _, img := range g.images {
  44. img.image_number = d()
  45. }
  46. lp.QueueWriteString(buf.String())
  47. }
  48. func (g *graphics_manager) clear_placements() {
  49. buf := strings.Builder{}
  50. for _, img := range g.images {
  51. if img.current_file == "" {
  52. continue
  53. }
  54. gc := img.new_graphics_command()
  55. gc.SetAction(graphics.GRT_action_delete)
  56. gc.SetDelete(utils.IfElse(img.id > 0, graphics.GRT_delete_by_id, graphics.GRT_delete_by_number))
  57. gc.WriteWithPayloadTo(&buf, nil)
  58. }
  59. g.lp.QueueWriteString(buf.String())
  60. }
  61. func (g *graphics_manager) display_image(slot int, path string, img_width, img_height int) {
  62. img := g.images[slot]
  63. if img.current_file != path {
  64. gc := img.new_graphics_command()
  65. gc.SetAction(graphics.GRT_action_transmit).SetDataWidth(uint64(img_width)).SetDataHeight(uint64(img_height)).SetTransmission(graphics.GRT_transmission_file)
  66. gc.WriteWithPayloadToLoop(g.lp, []byte(path))
  67. img.current_file = path
  68. }
  69. gc := img.new_graphics_command()
  70. gc.SetAction(graphics.GRT_action_display).SetCursorMovement(graphics.GRT_cursor_static)
  71. gc.WriteWithPayloadToLoop(g.lp, nil)
  72. }
  73. func (g *graphics_manager) on_response(gc *graphics.GraphicsCommand) (err error) {
  74. if gc.ResponseMessage() != "OK" {
  75. return fmt.Errorf("Failed to load image with error: %s", gc.ResponseMessage())
  76. }
  77. for _, img := range g.images {
  78. if img.image_number == gc.ImageNumber() {
  79. img.id = gc.ImageId()
  80. break
  81. }
  82. }
  83. return
  84. }
  85. func (g *graphics_manager) finalize() {
  86. buf := strings.Builder{}
  87. for _, img := range g.images {
  88. gc := img.new_graphics_command()
  89. gc.SetAction(graphics.GRT_action_delete)
  90. gc.SetDelete(utils.IfElse(img.id > 0, graphics.GRT_free_by_id, graphics.GRT_free_by_number))
  91. gc.WriteWithPayloadTo(&buf, nil)
  92. }
  93. g.lp.QueueWriteString(buf.String())
  94. }