Debian Codegraph @

eventhandler.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // mauview - A Go TUI library based on tcell.
2 // Copyright © 2019 Tulir Asokan
3 //
4 // This Source Code Form is subject to the terms of the Mozilla Public
5 // License, v. 2.0. If a copy of the MPL was not distributed with this
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 package mauview
9
10 import (
•11 "github.com/gdamore/tcell/v2"
12 )
13
14 // KeyEvent is an interface of the *tcell.EventKey type.
15 type KeyEvent interface {
16 tcell.Event
17 // The rune corresponding to the key that was pressed.
18 Rune() rune
19 // The keyboard key that was pressed.
20 Key() tcell.Key
21 // The keyboard modifiers that were pressed during the event.
22 Modifiers() tcell.ModMask
23 }
24
25 type customPasteEvent struct {
26 *tcell.EventPaste
27 text string
28 }
29
30 func (cpe customPasteEvent) Text() string {
31 return cpe.text
32 }
33
34 // PasteEvent is an interface of the customPasteEvent type.
35 type PasteEvent interface {
36 tcell.Event
37 // The text pasted.
38 Text() string
39 }
40
41 type customMouseEvent struct {
42 *tcell.EventMouse
43 motion bool
44 }
45
46 func (cme customMouseEvent) HasMotion() bool {
47 return cme.motion
48 }
49
50 // MouseEvent is an interface of the *tcell.EventMouse type.
51 type MouseEvent interface {
52 tcell.Event
53 // The mouse buttons that were pressed.
54 Buttons() tcell.ButtonMask
55 // The keyboard modifiers that were pressed during the event.
56 Modifiers() tcell.ModMask
57 // The current position of the mouse.
58 Position() (int, int)
59 // Whether or not the event is a mouse move event.
60 HasMotion() bool
61 }
62
63 // SimpleEventHandler is a simple implementation of the event handling methods required for components.
64 type SimpleEventHandler struct {
65 OnKey func(event KeyEvent) bool
66 OnPaste func(event PasteEvent) bool
67 OnMouse func(event MouseEvent) bool
68 }
69
70 func (seh *SimpleEventHandler) OnKeyEvent(event KeyEvent) bool {
71 if seh.OnKey != nil {
72 return seh.OnKey(event)
73 }
74 return false
75 }
76
77 func (seh *SimpleEventHandler) OnPasteEvent(event PasteEvent) bool {
78 if seh.OnPaste != nil {
79 return seh.OnPaste(event)
80 }
81 return false
82 }
83
84 func (seh *SimpleEventHandler) OnMouseEvent(event MouseEvent) bool {
85 if seh.OnMouse != nil {
86 return seh.OnMouse(event)
87 }
88 return false
89 }
90
91 type NoopEventHandler struct{}
92
93 func (neh NoopEventHandler) OnKeyEvent(event KeyEvent) bool {
94 return false
95 }
96
97 func (neh NoopEventHandler) OnPasteEvent(event PasteEvent) bool {
98 return false
99 }
100
101 func (neh NoopEventHandler) OnMouseEvent(event MouseEvent) bool {
102 return false
103 }
104
105 type proxyEventMouse struct {
106 MouseEvent
107 x int
108 y int
109 }
110
111 func (evt *proxyEventMouse) Position() (int, int) {
112 return evt.x, evt.y
113 }
114
115 // OffsetMouseEvent creates a new MouseEvent with the given offset.
116 func OffsetMouseEvent(evt MouseEvent, offsetX, offsetY int) *proxyEventMouse {
117 x, y := evt.Position()
118 proxy, ok := evt.(*proxyEventMouse)
119 if ok {
120 evt = proxy.MouseEvent
121 }
122 return &proxyEventMouse{evt, x + offsetX, y + offsetY}
123 }