toolbar.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package widget
  2. import (
  3. "fyne.io/fyne/v2"
  4. "fyne.io/fyne/v2/canvas"
  5. "fyne.io/fyne/v2/internal/widget"
  6. "fyne.io/fyne/v2/layout"
  7. "fyne.io/fyne/v2/theme"
  8. )
  9. // ToolbarItem represents any interface element that can be added to a toolbar
  10. type ToolbarItem interface {
  11. ToolbarObject() fyne.CanvasObject
  12. }
  13. // ToolbarAction is push button style of ToolbarItem
  14. type ToolbarAction struct {
  15. Icon fyne.Resource
  16. OnActivated func() `json:"-"`
  17. }
  18. // ToolbarObject gets a button to render this ToolbarAction
  19. func (t *ToolbarAction) ToolbarObject() fyne.CanvasObject {
  20. button := NewButtonWithIcon("", t.Icon, t.OnActivated)
  21. button.Importance = LowImportance
  22. return button
  23. }
  24. // SetIcon updates the icon on a ToolbarItem
  25. //
  26. // Since: 2.2
  27. func (t *ToolbarAction) SetIcon(icon fyne.Resource) {
  28. t.Icon = icon
  29. t.ToolbarObject().Refresh()
  30. }
  31. // NewToolbarAction returns a new push button style ToolbarItem
  32. func NewToolbarAction(icon fyne.Resource, onActivated func()) *ToolbarAction {
  33. return &ToolbarAction{icon, onActivated}
  34. }
  35. // ToolbarSpacer is a blank, stretchable space for a toolbar.
  36. // This is typically used to assist layout if you wish some left and some right aligned items.
  37. // Space will be split evebly amongst all the spacers on a toolbar.
  38. type ToolbarSpacer struct {
  39. }
  40. // ToolbarObject gets the actual spacer object for this ToolbarSpacer
  41. func (t *ToolbarSpacer) ToolbarObject() fyne.CanvasObject {
  42. return layout.NewSpacer()
  43. }
  44. // NewToolbarSpacer returns a new spacer item for a Toolbar to assist with ToolbarItem alignment
  45. func NewToolbarSpacer() *ToolbarSpacer {
  46. return &ToolbarSpacer{}
  47. }
  48. // ToolbarSeparator is a thin, visible divide that can be added to a Toolbar.
  49. // This is typically used to assist visual grouping of ToolbarItems.
  50. type ToolbarSeparator struct {
  51. }
  52. // ToolbarObject gets the visible line object for this ToolbarSeparator
  53. func (t *ToolbarSeparator) ToolbarObject() fyne.CanvasObject {
  54. return canvas.NewRectangle(theme.ForegroundColor())
  55. }
  56. // NewToolbarSeparator returns a new separator item for a Toolbar to assist with ToolbarItem grouping
  57. func NewToolbarSeparator() *ToolbarSeparator {
  58. return &ToolbarSeparator{}
  59. }
  60. // Toolbar widget creates a horizontal list of tool buttons
  61. type Toolbar struct {
  62. BaseWidget
  63. Items []ToolbarItem
  64. }
  65. // CreateRenderer is a private method to Fyne which links this widget to its renderer
  66. func (t *Toolbar) CreateRenderer() fyne.WidgetRenderer {
  67. t.ExtendBaseWidget(t)
  68. r := &toolbarRenderer{toolbar: t, layout: layout.NewHBoxLayout()}
  69. r.resetObjects()
  70. return r
  71. }
  72. // Append a new ToolbarItem to the end of this Toolbar
  73. func (t *Toolbar) Append(item ToolbarItem) {
  74. t.Items = append(t.Items, item)
  75. t.Refresh()
  76. }
  77. // Prepend a new ToolbarItem to the start of this Toolbar
  78. func (t *Toolbar) Prepend(item ToolbarItem) {
  79. t.Items = append([]ToolbarItem{item}, t.Items...)
  80. t.Refresh()
  81. }
  82. // MinSize returns the size that this widget should not shrink below
  83. func (t *Toolbar) MinSize() fyne.Size {
  84. t.ExtendBaseWidget(t)
  85. return t.BaseWidget.MinSize()
  86. }
  87. // NewToolbar creates a new toolbar widget.
  88. func NewToolbar(items ...ToolbarItem) *Toolbar {
  89. t := &Toolbar{Items: items}
  90. t.ExtendBaseWidget(t)
  91. t.Refresh()
  92. return t
  93. }
  94. type toolbarRenderer struct {
  95. widget.BaseRenderer
  96. layout fyne.Layout
  97. items []fyne.CanvasObject
  98. toolbar *Toolbar
  99. }
  100. func (r *toolbarRenderer) MinSize() fyne.Size {
  101. return r.layout.MinSize(r.items)
  102. }
  103. func (r *toolbarRenderer) Layout(size fyne.Size) {
  104. r.layout.Layout(r.items, size)
  105. }
  106. func (r *toolbarRenderer) Refresh() {
  107. r.resetObjects()
  108. for i, item := range r.toolbar.Items {
  109. if _, ok := item.(*ToolbarSeparator); ok {
  110. rect := r.items[i].(*canvas.Rectangle)
  111. rect.FillColor = theme.ForegroundColor()
  112. }
  113. }
  114. canvas.Refresh(r.toolbar)
  115. }
  116. func (r *toolbarRenderer) resetObjects() {
  117. r.items = make([]fyne.CanvasObject, 0, len(r.toolbar.Items))
  118. for _, item := range r.toolbar.Items {
  119. r.items = append(r.items, item.ToolbarObject())
  120. }
  121. r.SetObjects(r.items)
  122. }