Debian Codegraph @

box.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 type KeyCaptureFunc func(event KeyEvent) KeyEvent
15 type MouseCaptureFunc func(event MouseEvent) MouseEvent
16 type PasteCaptureFunc func(event PasteEvent) PasteEvent
17
18 type Box struct {
19 border bool
20 borderStyle tcell.Style
21 backgroundColor *tcell.Color
22 keyCapture KeyCaptureFunc
23 mouseCapture MouseCaptureFunc
24 pasteCapture PasteCaptureFunc
25 focusCapture func() bool
26 blurCapture func() bool
27 title string
28 inner Component
29 innerScreen *ProxyScreen
30 focused bool
31 }
32
33 func NewBox(inner Component) *Box {
34 return &Box{
35 border: true,
36 borderStyle: tcell.StyleDefault,
37 backgroundColor: &Styles.PrimitiveBackgroundColor,
38 inner: inner,
39 innerScreen: &ProxyScreen{OffsetX: 1, OffsetY: 1},
40 }
41 }
42
43 func (box *Box) Focus() {
44 box.focused = true
45 if box.focusCapture != nil {
46 if box.focusCapture() {
47 return
48 }
49 }
50 focusable, ok := box.inner.(Focusable)
51 if ok {
52 focusable.Focus()
53 }
54 }
55
56 func (box *Box) Blur() {
57 box.focused = false
58 if box.blurCapture != nil {
59 if box.blurCapture() {
60 return
61 }
62 }
63 focusable, ok := box.inner.(Focusable)
64 if ok {
65 focusable.Blur()
66 }
67 }
68
69 func (box *Box) SetBorder(border bool) *Box {
70 box.border = border
71 if border {
72 box.innerScreen.OffsetY = 1
73 box.innerScreen.OffsetX = 1
74 } else {
75 box.innerScreen.OffsetY = 0
76 box.innerScreen.OffsetX = 0
77 }
78 return box
79 }
80
81 func (box *Box) SetBorderStyle(borderStyle tcell.Style) *Box {
82 box.borderStyle = borderStyle
83 return box
84 }
85
86 func (box *Box) SetTitle(title string) *Box {
87 box.title = title
88 return box
89 }
90
91 func (box *Box) SetInnerComponent(component Component) *Box {
92 box.inner = component
93 return box
94 }
95
96 func (box *Box) SetMouseCaptureFunc(mouseCapture MouseCaptureFunc) *Box {
97 box.mouseCapture = mouseCapture
98 return box
99 }
100
101 func (box *Box) SetKeyCaptureFunc(keyCapture KeyCaptureFunc) *Box {
102 box.keyCapture = keyCapture
103 return box
104 }
105
106 func (box *Box) SetPasteCaptureFunc(pasteCapture PasteCaptureFunc) *Box {
107 box.pasteCapture = pasteCapture
108 return box
109 }
110
111 func (box *Box) SetFocusCaptureFunc(focusCapture func() bool) *Box {
112 box.focusCapture = focusCapture
113 return box
114 }
115
116 func (box *Box) SetBlurCaptureFunc(blurCapture func() bool) *Box {
117 box.blurCapture = blurCapture
118 return box
119 }
120
121 func (box *Box) SetBackgroundColor(color tcell.Color) *Box {
122 box.backgroundColor = &color
123 return box
124 }
125
126 func (box *Box) drawBorder(screen Screen) {
127 width, height := screen.Size()
128 var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune
129 if box.focused {
130 horizontal = Borders.HorizontalFocus
131 vertical = Borders.VerticalFocus
132 topLeft = Borders.TopLeftFocus
133 topRight = Borders.TopRightFocus
134 bottomLeft = Borders.BottomLeftFocus
135 bottomRight = Borders.BottomRightFocus
136 } else {
137 horizontal = Borders.Horizontal
138 vertical = Borders.Vertical
139 topLeft = Borders.TopLeft
140 topRight = Borders.TopRight
141 bottomLeft = Borders.BottomLeft
142 bottomRight = Borders.BottomRight
143 }
144 borderStyle := box.borderStyle
145 if box.backgroundColor != nil {
146 borderStyle = borderStyle.Background(*box.backgroundColor)
147 }
148 for x := 0; x < width; x++ {
149 screen.SetContent(x, 0, horizontal, nil, borderStyle)
150 screen.SetContent(x, height-1, horizontal, nil, borderStyle)
151 }
152 Print(screen, box.title, 1, 0, width-2, AlignCenter, Styles.BorderColor)
153 for y := 0; y < height; y++ {
154 screen.SetContent(0, y, vertical, nil, borderStyle)
155 screen.SetContent(width-1, y, vertical, nil, borderStyle)
156 }
157 screen.SetContent(0, 0, topLeft, nil, borderStyle)
158 screen.SetContent(width-1, 0, topRight, nil, borderStyle)
159 screen.SetContent(0, height-1, bottomLeft, nil, borderStyle)
160 screen.SetContent(width-1, height-1, bottomRight, nil, borderStyle)
161 }
162
163 func (box *Box) Draw(screen Screen) {
164 width, height := screen.Size()
165 border := false
166 if box.backgroundColor != nil {
167 screen.SetStyle(tcell.StyleDefault.Background(*box.backgroundColor))
168 screen.Clear()
169 }
170 if box.border && width >= 2 && height >= 2 {
171 border = true
172 box.drawBorder(screen)
173 }
174
175 if box.inner != nil {
176 if border {
177 box.innerScreen.Width = width - 2
178 box.innerScreen.Height = height - 2
179 } else {
180 box.innerScreen.Width = width
181 box.innerScreen.Height = height
182 }
183 box.innerScreen.Parent = screen
184 box.inner.Draw(box.innerScreen)
185 }
186 }
187
188 func (box *Box) OnKeyEvent(event KeyEvent) bool {
189 if box.keyCapture != nil {
190 event = box.keyCapture(event)
191 if event == nil {
192 return true
193 }
194 }
195 if box.inner != nil {
196 return box.inner.OnKeyEvent(event)
197 }
198 return false
199 }
200
201 func (box *Box) OnPasteEvent(event PasteEvent) bool {
202 if box.pasteCapture != nil {
203 event = box.pasteCapture(event)
204 if event == nil {
205 return true
206 }
207 }
208 if box.inner != nil {
209 return box.inner.OnPasteEvent(event)
210 }
211 return false
212 }
213
214 func (box *Box) OnMouseEvent(event MouseEvent) bool {
215 if box.border {
216 event = OffsetMouseEvent(event, -1, -1)
217 }
218 x, y := event.Position()
219 if x < 0 || y < 0 || x > box.innerScreen.Width || y > box.innerScreen.Height {
220 return false
221 }
222 if box.mouseCapture != nil {
223 event = box.mouseCapture(event)
224 if event == nil {
225 return true
226 }
227 }
228 if box.inner != nil {
229 return box.inner.OnMouseEvent(event)
230 }
231 return false
232 }