mouse.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2020 The TCell Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use file except in compliance with the License.
  5. // You may obtain a copy of the license at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tcell
  15. import (
  16. "time"
  17. )
  18. // EventMouse is a mouse event. It is sent on either mouse up or mouse down
  19. // events. It is also sent on mouse motion events - if the terminal supports
  20. // it. We make every effort to ensure that mouse release events are delivered.
  21. // Hence, click drag can be identified by a motion event with the mouse down,
  22. // without any intervening button release. On some terminals only the initiating
  23. // press and terminating release event will be delivered.
  24. //
  25. // Mouse wheel events, when reported, may appear on their own as individual
  26. // impulses; that is, there will normally not be a release event delivered
  27. // for mouse wheel movements.
  28. //
  29. // Most terminals cannot report the state of more than one button at a time --
  30. // and some cannot report motion events unless a button is pressed.
  31. //
  32. // Applications can inspect the time between events to resolve double or
  33. // triple clicks.
  34. type EventMouse struct {
  35. t time.Time
  36. btn ButtonMask
  37. mod ModMask
  38. x int
  39. y int
  40. }
  41. // When returns the time when this EventMouse was created.
  42. func (ev *EventMouse) When() time.Time {
  43. return ev.t
  44. }
  45. // Buttons returns the list of buttons that were pressed or wheel motions.
  46. func (ev *EventMouse) Buttons() ButtonMask {
  47. return ev.btn
  48. }
  49. // Modifiers returns a list of keyboard modifiers that were pressed
  50. // with the mouse button(s).
  51. func (ev *EventMouse) Modifiers() ModMask {
  52. return ev.mod
  53. }
  54. // Position returns the mouse position in character cells. The origin
  55. // 0, 0 is at the upper left corner.
  56. func (ev *EventMouse) Position() (int, int) {
  57. return ev.x, ev.y
  58. }
  59. // NewEventMouse is used to create a new mouse event. Applications
  60. // shouldn't need to use this; its mostly for screen implementors.
  61. func NewEventMouse(x, y int, btn ButtonMask, mod ModMask) *EventMouse {
  62. return &EventMouse{t: time.Now(), x: x, y: y, btn: btn, mod: mod}
  63. }
  64. // ButtonMask is a mask of mouse buttons and wheel events. Mouse button presses
  65. // are normally delivered as both press and release events. Mouse wheel events
  66. // are normally just single impulse events. Windows supports up to eight
  67. // separate buttons plus all four wheel directions, but XTerm can only support
  68. // mouse buttons 1-3 and wheel up/down. Its not unheard of for terminals
  69. // to support only one or two buttons (think Macs). Old terminals, and true
  70. // emulations (such as vt100) won't support mice at all, of course.
  71. type ButtonMask int16
  72. // These are the actual button values. Note that tcell version 1.x reversed buttons
  73. // two and three on *nix based terminals. We use button 1 as the primary, and
  74. // button 2 as the secondary, and button 3 (which is often missing) as the middle.
  75. const (
  76. Button1 ButtonMask = 1 << iota // Usually the left (primary) mouse button.
  77. Button2 // Usually the right (secondary) mouse button.
  78. Button3 // Usually the middle mouse button.
  79. Button4 // Often a side button (thumb/next).
  80. Button5 // Often a side button (thumb/prev).
  81. Button6
  82. Button7
  83. Button8
  84. WheelUp // Wheel motion up/away from user.
  85. WheelDown // Wheel motion down/towards user.
  86. WheelLeft // Wheel motion to left.
  87. WheelRight // Wheel motion to right.
  88. ButtonNone ButtonMask = 0 // No button or wheel events.
  89. ButtonPrimary = Button1
  90. ButtonSecondary = Button2
  91. ButtonMiddle = Button3
  92. )