index_feature.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package choose_fonts
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "kitty/tools/tui"
  7. "kitty/tools/tui/loop"
  8. "kitty/tools/tui/readline"
  9. "kitty/tools/utils"
  10. )
  11. var _ = fmt.Print
  12. type if_panel struct {
  13. handler *handler
  14. rl *readline.Readline
  15. family, which, feat_tag string
  16. settings faces_settings
  17. feature_data FeatureData
  18. current_val uint
  19. }
  20. func (self *if_panel) render_lines(start_y int, lines ...string) (y int) {
  21. sz, _ := self.handler.lp.ScreenSize()
  22. _, y, str := self.handler.render_lines.InRectangle(lines, 0, start_y, int(sz.WidthCells), int(sz.HeightCells)-y, &self.handler.mouse_state, self.on_click)
  23. self.handler.lp.QueueWriteString(str)
  24. return
  25. }
  26. func (self *if_panel) draw_screen() (err error) {
  27. lp := self.handler.lp
  28. feat_name := utils.IfElse(self.feature_data.Name == "", self.feat_tag, self.feature_data.Name)
  29. lp.QueueWriteString(self.handler.format_title("Edit "+feat_name, 0))
  30. lines := []string{
  31. fmt.Sprintf("Enter a value for the '%s' feature of the %s font. Values are non-negative integers. Leaving it blank will cause the feature value to be not set, i.e. take its default value.", feat_name, self.family),
  32. }
  33. if self.feature_data.Tooltip != "" {
  34. lines = append(lines, "")
  35. lines = append(lines, self.feature_data.Tooltip)
  36. }
  37. if len(self.feature_data.Params) > 0 {
  38. lines = append(lines, "")
  39. lines = append(lines, "You can also click on any of the feature names below to choose the corresponding value.")
  40. } else {
  41. lines = append(lines, "")
  42. lines = append(lines, "Consult the documentation for this font to find out what values are valid for this feature.")
  43. }
  44. lines = append(lines, "")
  45. cursor_y := self.render_lines(2, lines...)
  46. if len(self.feature_data.Params) > 0 {
  47. lp.MoveCursorTo(1, cursor_y+3)
  48. num := 1
  49. strings.Join(utils.Map(func(x string) string {
  50. ans := tui.InternalHyperlink(x, fmt.Sprintf("fval:%d", num))
  51. num++
  52. return ans
  53. }, self.feature_data.Params), ", ")
  54. }
  55. lp.MoveCursorTo(1, cursor_y+1)
  56. lp.ClearToEndOfLine()
  57. self.rl.RedrawNonAtomic()
  58. lp.SetCursorVisible(true)
  59. return
  60. }
  61. func (self *if_panel) initialize(h *handler) (err error) {
  62. self.handler = h
  63. self.rl = readline.New(h.lp, readline.RlInit{DontMarkPrompts: true, Prompt: "Value: "})
  64. return
  65. }
  66. func (self *if_panel) on_wakeup() error {
  67. return self.handler.draw_screen()
  68. }
  69. func (self *if_panel) on_click(id string) (err error) {
  70. scheme, val, _ := strings.Cut(id, ":")
  71. if scheme != "fval" {
  72. return
  73. }
  74. v, _ := strconv.ParseUint(val, 10, 64)
  75. if err = self.handler.face_pane.change_feature_value(self.feat_tag, uint(v), false); err != nil {
  76. return err
  77. }
  78. self.handler.current_pane = &self.handler.face_pane
  79. return self.handler.draw_screen()
  80. }
  81. func (self *if_panel) on_key_event(event *loop.KeyEvent) (err error) {
  82. if event.MatchesPressOrRepeat("esc") {
  83. event.Handled = true
  84. self.handler.current_pane = &self.handler.face_pane
  85. return self.handler.draw_screen()
  86. }
  87. if event.MatchesPressOrRepeat("enter") {
  88. event.Handled = true
  89. text := strings.TrimSpace(self.rl.AllText())
  90. remove := false
  91. var val uint64
  92. if text == "" {
  93. remove = true
  94. } else {
  95. val, err = strconv.ParseUint(text, 10, 0)
  96. }
  97. if err != nil {
  98. self.rl.ResetText()
  99. self.handler.lp.Beep()
  100. } else {
  101. if err = self.handler.face_pane.change_feature_value(self.feat_tag, uint(val), remove); err != nil {
  102. return err
  103. }
  104. self.handler.current_pane = &self.handler.face_pane
  105. }
  106. return self.handler.draw_screen()
  107. }
  108. if err = self.rl.OnKeyEvent(event); err != nil {
  109. if err == readline.ErrAcceptInput {
  110. return nil
  111. }
  112. return err
  113. }
  114. return self.draw_screen()
  115. }
  116. func (self *if_panel) on_text(text string, from_key_event bool, in_bracketed_paste bool) (err error) {
  117. if err = self.rl.OnText(text, from_key_event, in_bracketed_paste); err != nil {
  118. return err
  119. }
  120. return self.draw_screen()
  121. }
  122. func (self *if_panel) on_enter(family, which string, settings faces_settings, feat_tag string, fd FeatureData, current_val uint) error {
  123. self.family = family
  124. self.feat_tag = feat_tag
  125. self.settings = settings
  126. self.which = which
  127. self.handler.current_pane = self
  128. self.feature_data = fd
  129. self.current_val = current_val
  130. self.rl.ResetText()
  131. if self.current_val > 0 {
  132. self.rl.SetText(strconv.Itoa(int(self.current_val)))
  133. }
  134. return self.handler.draw_screen()
  135. }