box.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package tview
  2. import (
  3. "github.com/gdamore/tcell"
  4. )
  5. // Box implements the Primitive interface with an empty background and optional
  6. // elements such as a border and a title. Box itself does not hold any content
  7. // but serves as the superclass of all other primitives. Subclasses add their
  8. // own content, typically (but not necessarily) keeping their content within the
  9. // box's rectangle.
  10. //
  11. // Box provides a number of utility functions available to all primitives.
  12. //
  13. // See https://github.com/rivo/tview/wiki/Box for an example.
  14. type Box struct {
  15. // The position of the rect.
  16. x, y, width, height int
  17. // The inner rect reserved for the box's content.
  18. innerX, innerY, innerWidth, innerHeight int
  19. // Border padding.
  20. paddingTop, paddingBottom, paddingLeft, paddingRight int
  21. // The box's background color.
  22. backgroundColor tcell.Color
  23. // Whether or not a border is drawn, reducing the box's space for content by
  24. // two in width and height.
  25. border bool
  26. // The color of the border.
  27. borderColor tcell.Color
  28. // The style attributes of the border.
  29. borderAttributes tcell.AttrMask
  30. // The title. Only visible if there is a border, too.
  31. title string
  32. // The color of the title.
  33. titleColor tcell.Color
  34. // The alignment of the title.
  35. titleAlign int
  36. // Provides a way to find out if this box has focus. We always go through
  37. // this interface because it may be overridden by implementing classes.
  38. focus Focusable
  39. // Whether or not this box has focus.
  40. hasFocus bool
  41. // An optional capture function which receives a key event and returns the
  42. // event to be forwarded to the primitive's default input handler (nil if
  43. // nothing should be forwarded).
  44. inputCapture func(event *tcell.EventKey) *tcell.EventKey
  45. // An optional function which is called before the box is drawn.
  46. draw func(screen tcell.Screen, x, y, width, height int) (int, int, int, int)
  47. // An optional capture function which receives a mouse event and returns the
  48. // event to be forwarded to the primitive's default mouse event handler (at
  49. // least one nil if nothing should be forwarded).
  50. mouseCapture func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse)
  51. }
  52. // NewBox returns a Box without a border.
  53. func NewBox() *Box {
  54. b := &Box{
  55. width: 15,
  56. height: 10,
  57. innerX: -1, // Mark as uninitialized.
  58. backgroundColor: Styles.PrimitiveBackgroundColor,
  59. borderColor: Styles.BorderColor,
  60. titleColor: Styles.TitleColor,
  61. titleAlign: AlignCenter,
  62. }
  63. b.focus = b
  64. return b
  65. }
  66. // SetBorderPadding sets the size of the borders around the box content.
  67. func (b *Box) SetBorderPadding(top, bottom, left, right int) *Box {
  68. b.paddingTop, b.paddingBottom, b.paddingLeft, b.paddingRight = top, bottom, left, right
  69. return b
  70. }
  71. // GetRect returns the current position of the rectangle, x, y, width, and
  72. // height.
  73. func (b *Box) GetRect() (int, int, int, int) {
  74. return b.x, b.y, b.width, b.height
  75. }
  76. // GetInnerRect returns the position of the inner rectangle (x, y, width,
  77. // height), without the border and without any padding. Width and height values
  78. // will clamp to 0 and thus never be negative.
  79. func (b *Box) GetInnerRect() (int, int, int, int) {
  80. if b.innerX >= 0 {
  81. return b.innerX, b.innerY, b.innerWidth, b.innerHeight
  82. }
  83. x, y, width, height := b.GetRect()
  84. if b.border {
  85. x++
  86. y++
  87. width -= 2
  88. height -= 2
  89. }
  90. x, y, width, height = x+b.paddingLeft,
  91. y+b.paddingTop,
  92. width-b.paddingLeft-b.paddingRight,
  93. height-b.paddingTop-b.paddingBottom
  94. if width < 0 {
  95. width = 0
  96. }
  97. if height < 0 {
  98. height = 0
  99. }
  100. return x, y, width, height
  101. }
  102. // SetRect sets a new position of the primitive. Note that this has no effect
  103. // if this primitive is part of a layout (e.g. Flex, Grid) or if it was added
  104. // like this:
  105. //
  106. // application.SetRoot(b, true)
  107. func (b *Box) SetRect(x, y, width, height int) {
  108. b.x = x
  109. b.y = y
  110. b.width = width
  111. b.height = height
  112. b.innerX = -1 // Mark inner rect as uninitialized.
  113. }
  114. // SetDrawFunc sets a callback function which is invoked after the box primitive
  115. // has been drawn. This allows you to add a more individual style to the box
  116. // (and all primitives which extend it).
  117. //
  118. // The function is provided with the box's dimensions (set via SetRect()). It
  119. // must return the box's inner dimensions (x, y, width, height) which will be
  120. // returned by GetInnerRect(), used by descendent primitives to draw their own
  121. // content.
  122. func (b *Box) SetDrawFunc(handler func(screen tcell.Screen, x, y, width, height int) (int, int, int, int)) *Box {
  123. b.draw = handler
  124. return b
  125. }
  126. // GetDrawFunc returns the callback function which was installed with
  127. // SetDrawFunc() or nil if no such function has been installed.
  128. func (b *Box) GetDrawFunc() func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
  129. return b.draw
  130. }
  131. // WrapInputHandler wraps an input handler (see InputHandler()) with the
  132. // functionality to capture input (see SetInputCapture()) before passing it
  133. // on to the provided (default) input handler.
  134. //
  135. // This is only meant to be used by subclassing primitives.
  136. func (b *Box) WrapInputHandler(inputHandler func(*tcell.EventKey, func(p Primitive))) func(*tcell.EventKey, func(p Primitive)) {
  137. return func(event *tcell.EventKey, setFocus func(p Primitive)) {
  138. if b.inputCapture != nil {
  139. event = b.inputCapture(event)
  140. }
  141. if event != nil && inputHandler != nil {
  142. inputHandler(event, setFocus)
  143. }
  144. }
  145. }
  146. // InputHandler returns nil.
  147. func (b *Box) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
  148. return b.WrapInputHandler(nil)
  149. }
  150. // SetInputCapture installs a function which captures key events before they are
  151. // forwarded to the primitive's default key event handler. This function can
  152. // then choose to forward that key event (or a different one) to the default
  153. // handler by returning it. If nil is returned, the default handler will not
  154. // be called.
  155. //
  156. // Providing a nil handler will remove a previously existing handler.
  157. //
  158. // Note that this function will not have an effect on primitives composed of
  159. // other primitives, such as Form, Flex, or Grid. Key events are only captured
  160. // by the primitives that have focus (e.g. InputField) and only one primitive
  161. // can have focus at a time. Composing primitives such as Form pass the focus on
  162. // to their contained primitives and thus never receive any key events
  163. // themselves. Therefore, they cannot intercept key events.
  164. func (b *Box) SetInputCapture(capture func(event *tcell.EventKey) *tcell.EventKey) *Box {
  165. b.inputCapture = capture
  166. return b
  167. }
  168. // GetInputCapture returns the function installed with SetInputCapture() or nil
  169. // if no such function has been installed.
  170. func (b *Box) GetInputCapture() func(event *tcell.EventKey) *tcell.EventKey {
  171. return b.inputCapture
  172. }
  173. // WrapMouseHandler wraps a mouse event handler (see MouseHandler()) with the
  174. // functionality to capture mouse events (see SetMouseCapture()) before passing
  175. // them on to the provided (default) event handler.
  176. //
  177. // This is only meant to be used by subclassing primitives.
  178. func (b *Box) WrapMouseHandler(mouseHandler func(MouseAction, *tcell.EventMouse, func(p Primitive)) (bool, Primitive)) func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  179. return func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  180. if b.mouseCapture != nil {
  181. action, event = b.mouseCapture(action, event)
  182. }
  183. if event != nil && mouseHandler != nil {
  184. consumed, capture = mouseHandler(action, event, setFocus)
  185. }
  186. return
  187. }
  188. }
  189. // MouseHandler returns nil.
  190. func (b *Box) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  191. return b.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  192. if action == MouseLeftClick && b.InRect(event.Position()) {
  193. setFocus(b)
  194. consumed = true
  195. }
  196. return
  197. })
  198. }
  199. // SetMouseCapture sets a function which captures mouse events (consisting of
  200. // the original tcell mouse event and the semantic mouse action) before they are
  201. // forwarded to the primitive's default mouse event handler. This function can
  202. // then choose to forward that event (or a different one) by returning it or
  203. // returning a nil mouse event, in which case the default handler will not be
  204. // called.
  205. //
  206. // Providing a nil handler will remove a previously existing handler.
  207. func (b *Box) SetMouseCapture(capture func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse)) *Box {
  208. b.mouseCapture = capture
  209. return b
  210. }
  211. // InRect returns true if the given coordinate is within the bounds of the box's
  212. // rectangle.
  213. func (b *Box) InRect(x, y int) bool {
  214. rectX, rectY, width, height := b.GetRect()
  215. return x >= rectX && x < rectX+width && y >= rectY && y < rectY+height
  216. }
  217. // GetMouseCapture returns the function installed with SetMouseCapture() or nil
  218. // if no such function has been installed.
  219. func (b *Box) GetMouseCapture() func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse) {
  220. return b.mouseCapture
  221. }
  222. // SetBackgroundColor sets the box's background color.
  223. func (b *Box) SetBackgroundColor(color tcell.Color) *Box {
  224. b.backgroundColor = color
  225. return b
  226. }
  227. // SetBorder sets the flag indicating whether or not the box should have a
  228. // border.
  229. func (b *Box) SetBorder(show bool) *Box {
  230. b.border = show
  231. return b
  232. }
  233. // SetBorderColor sets the box's border color.
  234. func (b *Box) SetBorderColor(color tcell.Color) *Box {
  235. b.borderColor = color
  236. return b
  237. }
  238. // SetBorderAttributes sets the border's style attributes. You can combine
  239. // different attributes using bitmask operations:
  240. //
  241. // box.SetBorderAttributes(tcell.AttrUnderline | tcell.AttrBold)
  242. func (b *Box) SetBorderAttributes(attr tcell.AttrMask) *Box {
  243. b.borderAttributes = attr
  244. return b
  245. }
  246. // GetBorderAttributes returns the border's style attributes.
  247. func (b *Box) GetBorderAttributes() tcell.AttrMask {
  248. return b.borderAttributes
  249. }
  250. // GetBorderColor returns the box's border color.
  251. func (b *Box) GetBorderColor() tcell.Color {
  252. return b.borderColor
  253. }
  254. // GetBackgroundColor returns the box's background color.
  255. func (b *Box) GetBackgroundColor() tcell.Color {
  256. return b.backgroundColor
  257. }
  258. // SetTitle sets the box's title.
  259. func (b *Box) SetTitle(title string) *Box {
  260. b.title = title
  261. return b
  262. }
  263. // GetTitle returns the box's current title.
  264. func (b *Box) GetTitle() string {
  265. return b.title
  266. }
  267. // SetTitleColor sets the box's title color.
  268. func (b *Box) SetTitleColor(color tcell.Color) *Box {
  269. b.titleColor = color
  270. return b
  271. }
  272. // SetTitleAlign sets the alignment of the title, one of AlignLeft, AlignCenter,
  273. // or AlignRight.
  274. func (b *Box) SetTitleAlign(align int) *Box {
  275. b.titleAlign = align
  276. return b
  277. }
  278. // Draw draws this primitive onto the screen.
  279. func (b *Box) Draw(screen tcell.Screen) {
  280. // Don't draw anything if there is no space.
  281. if b.width <= 0 || b.height <= 0 {
  282. return
  283. }
  284. def := tcell.StyleDefault
  285. // Fill background.
  286. background := def.Background(b.backgroundColor)
  287. if b.backgroundColor != tcell.ColorDefault {
  288. for y := b.y; y < b.y+b.height; y++ {
  289. for x := b.x; x < b.x+b.width; x++ {
  290. screen.SetContent(x, y, ' ', nil, background)
  291. }
  292. }
  293. }
  294. // Draw border.
  295. if b.border && b.width >= 2 && b.height >= 2 {
  296. border := background.Foreground(b.borderColor) | tcell.Style(b.borderAttributes)
  297. var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune
  298. if b.focus.HasFocus() {
  299. horizontal = Borders.HorizontalFocus
  300. vertical = Borders.VerticalFocus
  301. topLeft = Borders.TopLeftFocus
  302. topRight = Borders.TopRightFocus
  303. bottomLeft = Borders.BottomLeftFocus
  304. bottomRight = Borders.BottomRightFocus
  305. } else {
  306. horizontal = Borders.Horizontal
  307. vertical = Borders.Vertical
  308. topLeft = Borders.TopLeft
  309. topRight = Borders.TopRight
  310. bottomLeft = Borders.BottomLeft
  311. bottomRight = Borders.BottomRight
  312. }
  313. for x := b.x + 1; x < b.x+b.width-1; x++ {
  314. screen.SetContent(x, b.y, horizontal, nil, border)
  315. screen.SetContent(x, b.y+b.height-1, horizontal, nil, border)
  316. }
  317. for y := b.y + 1; y < b.y+b.height-1; y++ {
  318. screen.SetContent(b.x, y, vertical, nil, border)
  319. screen.SetContent(b.x+b.width-1, y, vertical, nil, border)
  320. }
  321. screen.SetContent(b.x, b.y, topLeft, nil, border)
  322. screen.SetContent(b.x+b.width-1, b.y, topRight, nil, border)
  323. screen.SetContent(b.x, b.y+b.height-1, bottomLeft, nil, border)
  324. screen.SetContent(b.x+b.width-1, b.y+b.height-1, bottomRight, nil, border)
  325. // Draw title.
  326. if b.title != "" && b.width >= 4 {
  327. printed, _ := Print(screen, b.title, b.x+1, b.y, b.width-2, b.titleAlign, b.titleColor)
  328. if len(b.title)-printed > 0 && printed > 0 {
  329. _, _, style, _ := screen.GetContent(b.x+b.width-2, b.y)
  330. fg, _, _ := style.Decompose()
  331. Print(screen, string(SemigraphicsHorizontalEllipsis), b.x+b.width-2, b.y, 1, AlignLeft, fg)
  332. }
  333. }
  334. }
  335. // Call custom draw function.
  336. if b.draw != nil {
  337. b.innerX, b.innerY, b.innerWidth, b.innerHeight = b.draw(screen, b.x, b.y, b.width, b.height)
  338. } else {
  339. // Remember the inner rect.
  340. b.innerX = -1
  341. b.innerX, b.innerY, b.innerWidth, b.innerHeight = b.GetInnerRect()
  342. }
  343. }
  344. // Focus is called when this primitive receives focus.
  345. func (b *Box) Focus(delegate func(p Primitive)) {
  346. b.hasFocus = true
  347. }
  348. // Blur is called when this primitive loses focus.
  349. func (b *Box) Blur() {
  350. b.hasFocus = false
  351. }
  352. // HasFocus returns whether or not this primitive has focus.
  353. func (b *Box) HasFocus() bool {
  354. return b.hasFocus
  355. }
  356. // GetFocusable returns the item's Focusable.
  357. func (b *Box) GetFocusable() Focusable {
  358. return b.focus
  359. }