-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmouse.go
73 lines (61 loc) · 1.64 KB
/
mouse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"image"
"fyne.io/fyne/driver/desktop"
"github.com/deluan/bring"
)
var mouseBtnMap = map[desktop.MouseButton]bring.MouseButton{
desktop.LeftMouseButton: bring.MouseLeft,
desktop.RightMouseButton: bring.MouseRight,
}
// Handles mouse events mapping between Bring and Fyne
type mouseHandler struct {
display *BringDisplay
buttons map[desktop.MouseButton]bool
x, y int
}
func (ms *mouseHandler) pressedButtons() []bring.MouseButton {
var buttons []bring.MouseButton
for b, pressed := range ms.buttons {
bb := mouseBtnMap[b]
if pressed {
buttons = append(buttons, bb)
}
}
return buttons
}
func (ms *mouseHandler) sendMouse(x, y int) {
ms.x, ms.y = x, y
if err := ms.display.Client.SendMouse(image.Pt(x, y), ms.pressedButtons()...); err != nil {
fmt.Printf("Error: %s\n", err)
}
}
func (ms *mouseHandler) MouseDown(ev *desktop.MouseEvent) {
ms.buttons[ev.Button] = true
ms.sendMouse(ev.Position.X, ev.Position.Y)
ms.display.updateDisplay()
}
func (ms *mouseHandler) MouseUp(ev *desktop.MouseEvent) {
ms.buttons[ev.Button] = false
ms.sendMouse(ev.Position.X, ev.Position.Y)
ms.display.updateDisplay()
}
func (ms *mouseHandler) MouseMoved(ev *desktop.MouseEvent) {
x, y := ev.Position.X, ev.Position.Y
if ms.x == x && ms.y == y {
return
}
ms.sendMouse(x, y)
ms.display.updateDisplay()
}
func (ms *mouseHandler) MouseIn(*desktop.MouseEvent) {
if ms.buttons == nil {
ms.buttons = make(map[desktop.MouseButton]bool)
}
}
func (ms *mouseHandler) MouseOut() {
}
// Make sure all necessary interfaces are implemented
var _ desktop.Hoverable = (*mouseHandler)(nil)
var _ desktop.Mouseable = (*mouseHandler)(nil)