pages.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package tview
  2. import (
  3. "github.com/gdamore/tcell/v2"
  4. )
  5. // page represents one page of a Pages object.
  6. type page struct {
  7. Name string // The page's name.
  8. Item Primitive // The page's primitive.
  9. Resize bool // Whether or not to resize the page when it is drawn.
  10. Visible bool // Whether or not this page is visible.
  11. }
  12. // Pages is a container for other primitives laid out on top of each other,
  13. // overlapping or not. It is often used as the application's root primitive. It
  14. // allows to easily switch the visibility of the contained primitives.
  15. //
  16. // See https://github.com/rivo/tview/wiki/Pages for an example.
  17. type Pages struct {
  18. *Box
  19. // The contained pages. (Visible) pages are drawn from back to front.
  20. pages []*page
  21. // We keep a reference to the function which allows us to set the focus to
  22. // a newly visible page.
  23. setFocus func(p Primitive)
  24. // An optional handler which is called whenever the visibility or the order of
  25. // pages changes.
  26. changed func()
  27. }
  28. // NewPages returns a new Pages object.
  29. func NewPages() *Pages {
  30. p := &Pages{
  31. Box: NewBox(),
  32. }
  33. return p
  34. }
  35. // SetChangedFunc sets a handler which is called whenever the visibility or the
  36. // order of any visible pages changes. This can be used to redraw the pages.
  37. func (p *Pages) SetChangedFunc(handler func()) *Pages {
  38. p.changed = handler
  39. return p
  40. }
  41. // GetPageCount returns the number of pages currently stored in this object.
  42. func (p *Pages) GetPageCount() int {
  43. return len(p.pages)
  44. }
  45. // AddPage adds a new page with the given name and primitive. If there was
  46. // previously a page with the same name, it is overwritten. Leaving the name
  47. // empty may cause conflicts in other functions so always specify a non-empty
  48. // name.
  49. //
  50. // Visible pages will be drawn in the order they were added (unless that order
  51. // was changed in one of the other functions). If "resize" is set to true, the
  52. // primitive will be set to the size available to the Pages primitive whenever
  53. // the pages are drawn.
  54. func (p *Pages) AddPage(name string, item Primitive, resize, visible bool) *Pages {
  55. hasFocus := p.HasFocus()
  56. for index, pg := range p.pages {
  57. if pg.Name == name {
  58. p.pages = append(p.pages[:index], p.pages[index+1:]...)
  59. break
  60. }
  61. }
  62. p.pages = append(p.pages, &page{Item: item, Name: name, Resize: resize, Visible: visible})
  63. if p.changed != nil {
  64. p.changed()
  65. }
  66. if hasFocus {
  67. p.Focus(p.setFocus)
  68. }
  69. return p
  70. }
  71. // AddAndSwitchToPage calls AddPage(), then SwitchToPage() on that newly added
  72. // page.
  73. func (p *Pages) AddAndSwitchToPage(name string, item Primitive, resize bool) *Pages {
  74. p.AddPage(name, item, resize, true)
  75. p.SwitchToPage(name)
  76. return p
  77. }
  78. // RemovePage removes the page with the given name. If that page was the only
  79. // visible page, visibility is assigned to the last page.
  80. func (p *Pages) RemovePage(name string) *Pages {
  81. var isVisible bool
  82. hasFocus := p.HasFocus()
  83. for index, page := range p.pages {
  84. if page.Name == name {
  85. isVisible = page.Visible
  86. p.pages = append(p.pages[:index], p.pages[index+1:]...)
  87. if page.Visible && p.changed != nil {
  88. p.changed()
  89. }
  90. break
  91. }
  92. }
  93. if isVisible {
  94. for index, page := range p.pages {
  95. if index < len(p.pages)-1 {
  96. if page.Visible {
  97. break // There is a remaining visible page.
  98. }
  99. } else {
  100. page.Visible = true // We need at least one visible page.
  101. }
  102. }
  103. }
  104. if hasFocus {
  105. p.Focus(p.setFocus)
  106. }
  107. return p
  108. }
  109. // HasPage returns true if a page with the given name exists in this object.
  110. func (p *Pages) HasPage(name string) bool {
  111. for _, page := range p.pages {
  112. if page.Name == name {
  113. return true
  114. }
  115. }
  116. return false
  117. }
  118. // ShowPage sets a page's visibility to "true" (in addition to any other pages
  119. // which are already visible).
  120. func (p *Pages) ShowPage(name string) *Pages {
  121. for _, page := range p.pages {
  122. if page.Name == name {
  123. page.Visible = true
  124. if p.changed != nil {
  125. p.changed()
  126. }
  127. break
  128. }
  129. }
  130. if p.HasFocus() {
  131. p.Focus(p.setFocus)
  132. }
  133. return p
  134. }
  135. // HidePage sets a page's visibility to "false".
  136. func (p *Pages) HidePage(name string) *Pages {
  137. for _, page := range p.pages {
  138. if page.Name == name {
  139. page.Visible = false
  140. if p.changed != nil {
  141. p.changed()
  142. }
  143. break
  144. }
  145. }
  146. if p.HasFocus() {
  147. p.Focus(p.setFocus)
  148. }
  149. return p
  150. }
  151. // SwitchToPage sets a page's visibility to "true" and all other pages'
  152. // visibility to "false".
  153. func (p *Pages) SwitchToPage(name string) *Pages {
  154. for _, page := range p.pages {
  155. if page.Name == name {
  156. page.Visible = true
  157. } else {
  158. page.Visible = false
  159. }
  160. }
  161. if p.changed != nil {
  162. p.changed()
  163. }
  164. if p.HasFocus() {
  165. p.Focus(p.setFocus)
  166. }
  167. return p
  168. }
  169. // SendToFront changes the order of the pages such that the page with the given
  170. // name comes last, causing it to be drawn last with the next update (if
  171. // visible).
  172. func (p *Pages) SendToFront(name string) *Pages {
  173. for index, page := range p.pages {
  174. if page.Name == name {
  175. if index < len(p.pages)-1 {
  176. p.pages = append(append(p.pages[:index], p.pages[index+1:]...), page)
  177. }
  178. if page.Visible && p.changed != nil {
  179. p.changed()
  180. }
  181. break
  182. }
  183. }
  184. if p.HasFocus() {
  185. p.Focus(p.setFocus)
  186. }
  187. return p
  188. }
  189. // SendToBack changes the order of the pages such that the page with the given
  190. // name comes first, causing it to be drawn first with the next update (if
  191. // visible).
  192. func (p *Pages) SendToBack(name string) *Pages {
  193. for index, pg := range p.pages {
  194. if pg.Name == name {
  195. if index > 0 {
  196. p.pages = append(append([]*page{pg}, p.pages[:index]...), p.pages[index+1:]...)
  197. }
  198. if pg.Visible && p.changed != nil {
  199. p.changed()
  200. }
  201. break
  202. }
  203. }
  204. if p.HasFocus() {
  205. p.Focus(p.setFocus)
  206. }
  207. return p
  208. }
  209. // GetFrontPage returns the front-most visible page. If there are no visible
  210. // pages, ("", nil) is returned.
  211. func (p *Pages) GetFrontPage() (name string, item Primitive) {
  212. for index := len(p.pages) - 1; index >= 0; index-- {
  213. if p.pages[index].Visible {
  214. return p.pages[index].Name, p.pages[index].Item
  215. }
  216. }
  217. return
  218. }
  219. // HasFocus returns whether or not this primitive has focus.
  220. func (p *Pages) HasFocus() bool {
  221. for _, page := range p.pages {
  222. if page.Item.HasFocus() {
  223. return true
  224. }
  225. }
  226. return p.Box.HasFocus()
  227. }
  228. // Focus is called by the application when the primitive receives focus.
  229. func (p *Pages) Focus(delegate func(p Primitive)) {
  230. if delegate == nil {
  231. return // We cannot delegate so we cannot focus.
  232. }
  233. p.setFocus = delegate
  234. var topItem Primitive
  235. for _, page := range p.pages {
  236. if page.Visible {
  237. topItem = page.Item
  238. }
  239. }
  240. if topItem != nil {
  241. delegate(topItem)
  242. } else {
  243. p.Box.Focus(delegate)
  244. }
  245. }
  246. // Draw draws this primitive onto the screen.
  247. func (p *Pages) Draw(screen tcell.Screen) {
  248. p.Box.DrawForSubclass(screen, p)
  249. for _, page := range p.pages {
  250. if !page.Visible {
  251. continue
  252. }
  253. if page.Resize {
  254. x, y, width, height := p.GetInnerRect()
  255. page.Item.SetRect(x, y, width, height)
  256. }
  257. page.Item.Draw(screen)
  258. }
  259. }
  260. // MouseHandler returns the mouse handler for this primitive.
  261. func (p *Pages) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  262. return p.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
  263. if !p.InRect(event.Position()) {
  264. return false, nil
  265. }
  266. // Pass mouse events along to the last visible page item that takes it.
  267. for index := len(p.pages) - 1; index >= 0; index-- {
  268. page := p.pages[index]
  269. if page.Visible {
  270. consumed, capture = page.Item.MouseHandler()(action, event, setFocus)
  271. if consumed {
  272. return
  273. }
  274. }
  275. }
  276. return
  277. })
  278. }
  279. // InputHandler returns the handler for this primitive.
  280. func (p *Pages) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
  281. return p.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
  282. for _, page := range p.pages {
  283. if page.Item.HasFocus() {
  284. if handler := page.Item.InputHandler(); handler != nil {
  285. handler(event, setFocus)
  286. return
  287. }
  288. }
  289. }
  290. })
  291. }