tabbar.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. // TabBar is a panel which can contain other panels organized
  6. // as horizontal tabs.
  7. type TabBar struct {
  8. Panel // Embedded panel
  9. tabs []*Tab // Array of tabs
  10. selected int // Index of the selected tab
  11. }
  12. // Tab describes and individual tab from the TabBar
  13. type Tab struct {
  14. header Panel // Tab header
  15. label *Label // Tab optional label
  16. icon *Label // Tab optional icon
  17. img *Image // Tab optional image
  18. content Panel // User content panel
  19. }
  20. // NewTabBar creates and returns a pointer to a new TabBar widget
  21. // with the specified width and height
  22. func NewTabBar(width, height float32) *TabBar {
  23. return nil
  24. }
  25. // AddTab creates and adds a new tab with the specified text
  26. // at end of this TabBar list of tabs.
  27. func (tb *TabBar) AddTab(text string) *Tab {
  28. return tb.InsertTab(text, len(tb.tabs))
  29. }
  30. // InsertTab creates and inserts a new tab at the specified position
  31. // from left to right.
  32. // Return nil if the position is invalid
  33. func (tb *TabBar) InsertTab(text string, pos int) *Tab {
  34. if pos < 0 || pos > len(tb.tabs) {
  35. return nil
  36. }
  37. tab := new(Tab)
  38. tb.tabs = append(tb.tabs, nil)
  39. copy(tb.tabs[pos+1:], tb.tabs[pos:])
  40. tb.tabs[pos] = tab
  41. return tab
  42. }
  43. // RemoveTab removes the tab at the specified position in the TabBar.
  44. // Returns the pointer of the removed tab or nil if the position is invalid.
  45. func (tb *TabBar) RemoveTab(pos int) *Tab {
  46. if pos < 0 || pos >= len(tb.tabs) {
  47. return nil
  48. }
  49. tab := tb.tabs[pos]
  50. // Remove tab from array
  51. copy(tb.tabs[pos:], tb.tabs[pos+1:])
  52. tb.tabs[len(tb.tabs)-1] = nil
  53. tb.tabs = tb.tabs[:len(tb.tabs)-1]
  54. // Checks if tab was selected
  55. if tb.selected == pos {
  56. }
  57. return tab
  58. }
  59. // TabCount returns the current number of tabs
  60. func (tb *TabBar) TabCount() int {
  61. return len(tb.tabs)
  62. }
  63. // TabAt returns the pointer of the Tab object at the specified index.
  64. // Return nil if the index is invalid
  65. func (tb *TabBar) TabAt(idx int) *Tab {
  66. if idx < 0 || idx >= len(tb.tabs) {
  67. return nil
  68. }
  69. return tb.tabs[idx]
  70. }
  71. // SetSelected sets the selected tab of the TabBar to the tab with the specified position.
  72. // Returns the pointer of the selected tab or nil if the position is invalid.
  73. func (tb *TabBar) SetSelected(pos int) *Tab {
  74. if pos < 0 || pos >= len(tb.tabs) {
  75. return nil
  76. }
  77. tb.selected = pos
  78. return tb.tabs[pos]
  79. }
  80. // recalc recalculates and updates the positions of all tabs
  81. func (tb *TabBar) recalc() {
  82. }
  83. //
  84. // Tab methods
  85. //
  86. // SetText sets the text of the tab header
  87. func (tab *Tab) SetText(text string) *Tab {
  88. return tab
  89. }
  90. // SetIcon sets the icon of the tab header
  91. func (tab *Tab) SetIcon(icon string) *Tab {
  92. return tab
  93. }
  94. // Panel returns a pointer to the specified tab content panel
  95. func (tab *Tab) Panel() *Panel {
  96. return &tab.content
  97. }