Debian Codegraph @

frame.go

raw symbols SCIP
1 package tviewscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/
2
3 import (
4 "github.com/gdamore/tcell/v2scip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/"
5 )
6
7 // frameText holds information about a line of text shown in the frame.
8 type frameTextscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText# struct {
9 Textscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Text. string // The text to be displayed.
10 Headerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Header. bool // true = place in header, false = place in footer.
11 Alignscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Align. int // One of the Align constants.
12 Colorscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Color. tcellscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/.Colorscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/Color# // The text color.
13 }
14
15 // Frame is a wrapper which adds space around another primitive. In addition,
16 // the top area (header) and the bottom area (footer) may also contain text.
17 //
18 // See https://github.com/rivo/tview/wiki/Frame for an example.
19 type Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame# struct {
20 *Boxscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#Box.
21
22 // The contained primitive. May be nil.
23 primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive. Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#
24
25 // The lines of text to be displayed.
26 textscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#text. []*frameTextscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#
27
28 // Border spacing.
29 topscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#top., bottomscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#bottom., headerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#header., footerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#footer., leftscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#left., rightscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#right. int
30
31 // Keep a reference in case we need it when we change the primitive.
32 setFocusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#setFocus. func(p Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#)
33 }
34
35 // NewFrame returns a new frame around the given primitive. The primitive's
36 // size will be changed to fit within this frame. The primitive may be nil, in
37 // which case no other primitive is embedded in the frame.
38 func NewFramescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/NewFrame().(primitive Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#) *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame# {
39 box := NewBoxscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/NewBox().()
40
41 f := &Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#{
42 Boxscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#Box.: box,
43 primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive.: primitive,
44 topscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#top.: 1,
45 bottomscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#bottom.: 1,
46 headerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#header.: 1,
47 footerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#footer.: 1,
48 leftscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#left.: 1,
49 rightscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#right.: 1,
50 }
51
52 return f
53 }
54
55 // SetPrimitive replaces the contained primitive with the given one. To remove
56 // a primitive, set it to nil.
57 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) SetPrimitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#SetPrimitive().(p Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#) *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame# {
58 var hasFocus bool
59 if f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive. != nil {
60 hasFocus = f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive..HasFocusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#HasFocus.()
61 }
62 f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive. = p
63 if hasFocus && f.setFocusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#setFocus. != nil {
64 f.setFocusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#setFocus.(p) // Restore focus.
65 }
66 return f
67 }
68
69 // GetPrimitive returns the primitive contained in this frame.
70 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) GetPrimitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#GetPrimitive().() Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive# {
71 return f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive.
72 }
73
74 // AddText adds text to the frame. Set "header" to true if the text is to appear
75 // in the header, above the contained primitive. Set it to false for it to
76 // appear in the footer, below the contained primitive. "align" must be one of
77 // the Align constants. Rows in the header are printed top to bottom, rows in
78 // the footer are printed bottom to top. Note that long text can overlap as
79 // different alignments will be placed on the same row.
80 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) AddTextscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#AddText().(text string, header bool, align int, color tcellscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/.Colorscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/Color#) *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame# {
81 f.textscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#text. = append(f.textscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#text., &frameTextscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#{
82 Textscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Text.: text,
83 Headerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Header.: header,
84 Alignscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Align.: align,
85 Colorscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Color.: color,
86 })
87 return f
88 }
89
90 // Clear removes all text from the frame.
91 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) Clearscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#Clear().() *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame# {
92 f.textscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#text. = nil
93 return f
94 }
95
96 // SetBorders sets the width of the frame borders as well as "header" and
97 // "footer", the vertical space between the header and footer text and the
98 // contained primitive (does not apply if there is no text).
99 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) SetBordersscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#SetBorders().(top, bottom, header, footer, left, right int) *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame# {
100 f.topscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#top., f.bottomscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#bottom., f.headerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#header., f.footerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#footer., f.leftscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#left., f.rightscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#right. = top, bottom, header, footer, left, right
101 return f
102 }
103
104 // Draw draws this primitive onto the screen.
105 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) Drawscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#Draw().(screen tcellscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/.Screenscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/Screen#) {
106 f.Boxscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#Box..DrawForSubclassscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Box#DrawForSubclass().(screen, f)
107
108 // Calculate start positions.
109 x, top, width, height := f.GetInnerRectscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Box#GetInnerRect().()
110 bottom := top + height - 1
111 x += f.leftscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#left.
112 top += f.topscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#top.
113 bottom -= f.bottomscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#bottom.
114 width -= f.leftscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#left. + f.rightscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#right.
115 if width <= 0 || top >= bottom {
116 return // No space left.
117 }
118
119 // Draw text.
120 var rows [6]int // top-left, top-center, top-right, bottom-left, bottom-center, bottom-right.
121 topMax := top
122 bottomMin := bottom
123 for _, text := range f.textscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#text. {
124 // Where do we place this text?
125 var y int
126 if text.Headerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Header. {
127 y = top + rows[text.Alignscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Align.]
128 rows[text.Alignscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Align.]++
129 if y >= bottomMin {
130 continue
131 }
132 if y+1 > topMax {
133 topMax = y + 1
134 }
135 } else {
136 y = bottom - rows[3+text.Alignscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Align.]
137 rows[3+text.Alignscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Align.]++
138 if y <= topMax {
139 continue
140 }
141 if y-1 < bottomMin {
142 bottomMin = y - 1
143 }
144 }
145
146 // Draw text.
147 Printscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Print().(screen, text.Textscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Text., x, y, width, text.Alignscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Align., text.Colorscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/frameText#Color.)
148 }
149
150 // Set the size of the contained primitive.
151 if f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive. != nil {
152 if topMax > top {
153 top = topMax + f.headerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#header.
154 }
155 if bottomMin < bottom {
156 bottom = bottomMin - f.footerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#footer.
157 }
158 if top > bottom {
159 return // No space for the primitive.
160 }
161 f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive..SetRectscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#SetRect.(x, top, width, bottom+1-top)
162
163 // Finally, draw the contained primitive.
164 f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive..Drawscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#Draw.(screen)
165 }
166 }
167
168 // Focus is called when this primitive receives focus.
169 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) Focusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#Focus().(delegate func(p Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#)) {
170 f.setFocusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#setFocus. = delegate
171 if f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive. != nil {
172 delegate(f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive.)
173 } else {
174 f.Boxscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#Box..Focusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Box#Focus().(delegate)
175 }
176 }
177
178 // HasFocus returns whether or not this primitive has focus.
179 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) HasFocusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#HasFocus().() bool {
180 if f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive. == nil {
181 return f.Boxscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#Box..HasFocusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Box#HasFocus().()
182 }
183 return f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive..HasFocusscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#HasFocus.()
184 }
185
186 // MouseHandler returns the mouse handler for this primitive.
187 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) MouseHandlerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#MouseHandler().() func(action MouseActionscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/MouseAction#, event *tcellscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/.EventMousescip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/EventMouse#, setFocus func(p Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#)) (consumed bool, capture Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#) {
188 return f.WrapMouseHandlerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Box#WrapMouseHandler().(func(action MouseActionscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/MouseAction#, event *tcellscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/.EventMousescip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/EventMouse#, setFocus func(p Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#)) (consumed bool, capture Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#) {
189 if !f.InRectscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Box#InRect().(event.Positionscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/EventMouse#Position().()) {
190 return false, nil
191 }
192
193 // Pass mouse events on to contained primitive.
194 if f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive. != nil {
195 consumed, capture = f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive..MouseHandlerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#MouseHandler.()(action, event, setFocus)
196 if consumed {
197 return true, capture
198 }
199 }
200
201 // Clicking on the frame parts.
202 if action == MouseLeftDownscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/MouseLeftDown. {
203 setFocus(f)
204 consumed = true
205 }
206
207 return
208 })
209 }
210
211 // InputHandler returns the handler for this primitive.
212 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) InputHandlerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#InputHandler().() func(event *tcellscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/.EventKeyscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/EventKey#, setFocus func(p Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#)) {
213 return f.WrapInputHandlerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Box#WrapInputHandler().(func(event *tcellscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/.EventKeyscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/EventKey#, setFocus func(p Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#)) {
214 if f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive. == nil {
215 return
216 }
217 if handler := f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive..InputHandlerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#InputHandler.(); handler != nil {
218 handler(event, setFocus)
219 return
220 }
221 })
222 }
223
224 // PasteHandler returns the handler for this primitive.
225 func (f *Framescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#) PasteHandlerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#PasteHandler().() func(pastedText string, setFocus func(p Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#)) {
226 return f.WrapPasteHandlerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Box#WrapPasteHandler().(func(pastedText string, setFocus func(p Primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#)) {
227 if f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive. == nil {
228 return
229 }
230 if handler := f.primitivescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Frame#primitive..PasteHandlerscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Primitive#PasteHandler.(); handler != nil {
231 handler(pastedText, setFocus)
232 return
233 }
234 })
235 }