mouse.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package diff
  3. import (
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "kitty"
  9. "kitty/tools/config"
  10. "kitty/tools/tty"
  11. "kitty/tools/tui"
  12. "kitty/tools/tui/loop"
  13. "kitty/tools/utils"
  14. "kitty/tools/wcswidth"
  15. )
  16. var _ = fmt.Print
  17. type KittyOpts struct {
  18. Wheel_scroll_multiplier int
  19. Copy_on_select bool
  20. }
  21. func read_relevant_kitty_opts() KittyOpts {
  22. ans := KittyOpts{Wheel_scroll_multiplier: kitty.KittyConfigDefaults.Wheel_scroll_multiplier}
  23. handle_line := func(key, val string) error {
  24. switch key {
  25. case "wheel_scroll_multiplier":
  26. v, err := strconv.Atoi(val)
  27. if err == nil {
  28. ans.Wheel_scroll_multiplier = v
  29. }
  30. case "copy_on_select":
  31. ans.Copy_on_select = strings.ToLower(val) == "clipboard"
  32. }
  33. return nil
  34. }
  35. config.ReadKittyConfig(handle_line)
  36. return ans
  37. }
  38. var RelevantKittyOpts = sync.OnceValue(func() KittyOpts {
  39. return read_relevant_kitty_opts()
  40. })
  41. func (self *Handler) handle_wheel_event(up bool) {
  42. amt := RelevantKittyOpts().Wheel_scroll_multiplier
  43. if up {
  44. amt *= -1
  45. }
  46. _ = self.dispatch_action(`scroll_by`, strconv.Itoa(amt))
  47. }
  48. type line_pos struct {
  49. min_x, max_x int
  50. y ScrollPos
  51. }
  52. func (self *line_pos) MinX() int { return self.min_x }
  53. func (self *line_pos) MaxX() int { return self.max_x }
  54. func (self *line_pos) Equal(other tui.LinePos) bool {
  55. if o, ok := other.(*line_pos); ok {
  56. return self.y == o.y
  57. }
  58. return false
  59. }
  60. func (self *line_pos) LessThan(other tui.LinePos) bool {
  61. if o, ok := other.(*line_pos); ok {
  62. return self.y.Less(o.y)
  63. }
  64. return false
  65. }
  66. func (self *Handler) line_pos_from_pos(x int, pos ScrollPos) *line_pos {
  67. ans := line_pos{min_x: self.logical_lines.margin_size, y: pos}
  68. available_cols := self.logical_lines.columns / 2
  69. if x >= available_cols {
  70. ans.min_x += available_cols
  71. ans.max_x = utils.Max(ans.min_x, ans.min_x+self.logical_lines.ScreenLineAt(pos).right.wcswidth()-1)
  72. } else {
  73. ans.max_x = utils.Max(ans.min_x, ans.min_x+self.logical_lines.ScreenLineAt(pos).left.wcswidth()-1)
  74. }
  75. return &ans
  76. }
  77. func (self *Handler) start_mouse_selection(ev *loop.MouseEvent) {
  78. available_cols := self.logical_lines.columns / 2
  79. if ev.Cell.Y >= self.screen_size.num_lines || ev.Cell.X < self.logical_lines.margin_size || (ev.Cell.X >= available_cols && ev.Cell.X < available_cols+self.logical_lines.margin_size) {
  80. return
  81. }
  82. pos := self.scroll_pos
  83. self.logical_lines.IncrementScrollPosBy(&pos, ev.Cell.Y)
  84. ll := self.logical_lines.At(pos.logical_line)
  85. if ll.line_type == EMPTY_LINE || ll.line_type == IMAGE_LINE {
  86. return
  87. }
  88. self.mouse_selection.StartNewSelection(ev, self.line_pos_from_pos(ev.Cell.X, pos), 0, self.screen_size.num_lines-1, self.screen_size.cell_width, self.screen_size.cell_height)
  89. }
  90. func (self *Handler) drag_scroll_tick(timer_id loop.IdType) error {
  91. return self.mouse_selection.DragScrollTick(timer_id, self.lp, self.drag_scroll_tick, func(amt int, ev *loop.MouseEvent) error {
  92. if self.scroll_lines(amt) != 0 {
  93. self.do_update_mouse_selection(ev)
  94. self.draw_screen()
  95. }
  96. return nil
  97. })
  98. }
  99. var debugprintln = tty.DebugPrintln
  100. func (self *Handler) update_mouse_selection(ev *loop.MouseEvent) {
  101. if !self.mouse_selection.IsActive() {
  102. return
  103. }
  104. if self.mouse_selection.OutOfVerticalBounds(ev) {
  105. self.mouse_selection.DragScroll(ev, self.lp, self.drag_scroll_tick)
  106. return
  107. }
  108. self.do_update_mouse_selection(ev)
  109. }
  110. func (self *Handler) do_update_mouse_selection(ev *loop.MouseEvent) {
  111. pos := self.scroll_pos
  112. y := ev.Cell.Y
  113. y = utils.Max(0, utils.Min(y, self.screen_size.num_lines-1))
  114. self.logical_lines.IncrementScrollPosBy(&pos, y)
  115. x := self.mouse_selection.StartLine().MinX()
  116. self.mouse_selection.Update(ev, self.line_pos_from_pos(x, pos))
  117. self.draw_screen()
  118. }
  119. func (self *Handler) clear_mouse_selection() {
  120. self.mouse_selection.Clear()
  121. }
  122. func (self *Handler) text_for_current_mouse_selection() string {
  123. if self.mouse_selection.IsEmpty() {
  124. return ""
  125. }
  126. text := make([]byte, 0, 2048)
  127. start_pos, end_pos := *self.mouse_selection.StartLine().(*line_pos), *self.mouse_selection.EndLine().(*line_pos)
  128. // if start is after end, swap them
  129. if end_pos.y.Less(start_pos.y) {
  130. start_pos, end_pos = end_pos, start_pos
  131. }
  132. start, end := start_pos.y, end_pos.y
  133. is_left := start_pos.min_x == self.logical_lines.margin_size
  134. line_for_pos := func(pos ScrollPos) string {
  135. if is_left {
  136. return self.logical_lines.ScreenLineAt(pos).left.marked_up_text
  137. }
  138. return self.logical_lines.ScreenLineAt(pos).right.marked_up_text
  139. }
  140. for pos, prev_ll_idx := start, start.logical_line; pos.Less(end) || pos == end; {
  141. ll := self.logical_lines.At(pos.logical_line)
  142. var line string
  143. switch ll.line_type {
  144. case EMPTY_LINE:
  145. case IMAGE_LINE:
  146. if pos.screen_line < ll.image_lines_offset {
  147. line = line_for_pos(pos)
  148. }
  149. default:
  150. line = line_for_pos(pos)
  151. }
  152. line = wcswidth.StripEscapeCodes(line)
  153. s, e := self.mouse_selection.LineBounds(self.line_pos_from_pos(start_pos.min_x, pos))
  154. s -= start_pos.min_x
  155. e -= start_pos.min_x
  156. line = wcswidth.TruncateToVisualLength(line, e+1)
  157. if s > 0 {
  158. prefix := wcswidth.TruncateToVisualLength(line, s)
  159. line = line[len(prefix):]
  160. }
  161. // TODO: look at the original line from the source and handle leading tabs as per it
  162. if pos.logical_line > prev_ll_idx {
  163. line = "\n" + line
  164. }
  165. prev_ll_idx = pos.logical_line
  166. if line != "" {
  167. text = append(text, line...)
  168. }
  169. if self.logical_lines.IncrementScrollPosBy(&pos, 1) == 0 {
  170. break
  171. }
  172. }
  173. return utils.UnsafeBytesToString(text)
  174. }
  175. func (self *Handler) finish_mouse_selection(ev *loop.MouseEvent) {
  176. if !self.mouse_selection.IsActive() {
  177. return
  178. }
  179. self.update_mouse_selection(ev)
  180. self.mouse_selection.Finish()
  181. text := self.text_for_current_mouse_selection()
  182. if text != "" {
  183. if RelevantKittyOpts().Copy_on_select {
  184. self.lp.CopyTextToClipboard(text)
  185. } else {
  186. self.lp.CopyTextToPrimarySelection(text)
  187. }
  188. }
  189. }
  190. func (self *Handler) add_mouse_selection_to_line(line_pos ScrollPos, y int) string {
  191. if self.mouse_selection.IsEmpty() {
  192. return ""
  193. }
  194. selection_sgr := format_as_sgr.selection
  195. x := self.mouse_selection.StartLine().MinX()
  196. return self.mouse_selection.LineFormatSuffix(self.line_pos_from_pos(x, line_pos), selection_sgr[2:len(selection_sgr)-1], y)
  197. }