progressbar.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package widget
  2. import (
  3. "image/color"
  4. "strconv"
  5. "fyne.io/fyne/v2"
  6. "fyne.io/fyne/v2/canvas"
  7. "fyne.io/fyne/v2/data/binding"
  8. "fyne.io/fyne/v2/internal/cache"
  9. col "fyne.io/fyne/v2/internal/color"
  10. "fyne.io/fyne/v2/internal/widget"
  11. "fyne.io/fyne/v2/theme"
  12. )
  13. type progressRenderer struct {
  14. widget.BaseRenderer
  15. background, bar *canvas.Rectangle
  16. label *canvas.Text
  17. progress *ProgressBar
  18. }
  19. // MinSize calculates the minimum size of a progress bar.
  20. // This is simply the "100%" label size plus padding.
  21. func (p *progressRenderer) MinSize() fyne.Size {
  22. var tsize fyne.Size
  23. if text := p.progress.TextFormatter; text != nil {
  24. tsize = fyne.MeasureText(text(), p.label.TextSize, p.label.TextStyle)
  25. } else {
  26. tsize = fyne.MeasureText("100%", p.label.TextSize, p.label.TextStyle)
  27. }
  28. return fyne.NewSize(tsize.Width+theme.InnerPadding()*2, tsize.Height+theme.InnerPadding()*2)
  29. }
  30. func (p *progressRenderer) updateBar() {
  31. if p.progress.Value < p.progress.Min {
  32. p.progress.Value = p.progress.Min
  33. }
  34. if p.progress.Value > p.progress.Max {
  35. p.progress.Value = p.progress.Max
  36. }
  37. delta := float32(p.progress.Max - p.progress.Min)
  38. ratio := float32(p.progress.Value-p.progress.Min) / delta
  39. if text := p.progress.TextFormatter; text != nil {
  40. p.label.Text = text()
  41. } else {
  42. p.label.Text = strconv.Itoa(int(ratio*100)) + "%"
  43. }
  44. size := p.progress.Size()
  45. p.bar.Resize(fyne.NewSize(size.Width*ratio, size.Height))
  46. }
  47. // Layout the components of the check widget
  48. func (p *progressRenderer) Layout(size fyne.Size) {
  49. p.background.Resize(size)
  50. p.label.Resize(size)
  51. p.updateBar()
  52. }
  53. // applyTheme updates the progress bar to match the current theme
  54. func (p *progressRenderer) applyTheme() {
  55. p.background.FillColor = progressBackgroundColor()
  56. p.bar.FillColor = theme.PrimaryColor()
  57. p.label.Color = theme.BackgroundColor()
  58. p.label.TextSize = theme.TextSize()
  59. }
  60. func (p *progressRenderer) Refresh() {
  61. p.applyTheme()
  62. p.updateBar()
  63. p.background.Refresh()
  64. p.bar.Refresh()
  65. canvas.Refresh(p.progress.super())
  66. }
  67. // ProgressBar widget creates a horizontal panel that indicates progress
  68. type ProgressBar struct {
  69. BaseWidget
  70. Min, Max, Value float64
  71. // TextFormatter can be used to have a custom format of progress text.
  72. // If set, it overrides the percentage readout and runs each time the value updates.
  73. //
  74. // Since: 1.4
  75. TextFormatter func() string `json:"-"`
  76. binder basicBinder
  77. }
  78. // Bind connects the specified data source to this ProgressBar.
  79. // The current value will be displayed and any changes in the data will cause the widget to update.
  80. //
  81. // Since: 2.0
  82. func (p *ProgressBar) Bind(data binding.Float) {
  83. p.binder.SetCallback(p.updateFromData)
  84. p.binder.Bind(data)
  85. }
  86. // SetValue changes the current value of this progress bar (from p.Min to p.Max).
  87. // The widget will be refreshed to indicate the change.
  88. func (p *ProgressBar) SetValue(v float64) {
  89. p.Value = v
  90. p.Refresh()
  91. }
  92. // MinSize returns the size that this widget should not shrink below
  93. func (p *ProgressBar) MinSize() fyne.Size {
  94. p.ExtendBaseWidget(p)
  95. return p.BaseWidget.MinSize()
  96. }
  97. // CreateRenderer is a private method to Fyne which links this widget to its renderer
  98. func (p *ProgressBar) CreateRenderer() fyne.WidgetRenderer {
  99. p.ExtendBaseWidget(p)
  100. if p.Min == 0 && p.Max == 0 {
  101. p.Max = 1.0
  102. }
  103. background := canvas.NewRectangle(progressBackgroundColor())
  104. bar := canvas.NewRectangle(theme.PrimaryColor())
  105. label := canvas.NewText("0%", theme.ForegroundColor())
  106. label.Alignment = fyne.TextAlignCenter
  107. return &progressRenderer{widget.NewBaseRenderer([]fyne.CanvasObject{background, bar, label}), background, bar, label, p}
  108. }
  109. // Unbind disconnects any configured data source from this ProgressBar.
  110. // The current value will remain at the last value of the data source.
  111. //
  112. // Since: 2.0
  113. func (p *ProgressBar) Unbind() {
  114. p.binder.Unbind()
  115. }
  116. // NewProgressBar creates a new progress bar widget.
  117. // The default Min is 0 and Max is 1, Values set should be between those numbers.
  118. // The display will convert this to a percentage.
  119. func NewProgressBar() *ProgressBar {
  120. p := &ProgressBar{Min: 0, Max: 1}
  121. cache.Renderer(p).Layout(p.MinSize())
  122. return p
  123. }
  124. // NewProgressBarWithData returns a progress bar connected with the specified data source.
  125. //
  126. // Since: 2.0
  127. func NewProgressBarWithData(data binding.Float) *ProgressBar {
  128. p := NewProgressBar()
  129. p.Bind(data)
  130. return p
  131. }
  132. func progressBackgroundColor() color.Color {
  133. r, g, b, a := col.ToNRGBA(theme.PrimaryColor())
  134. faded := uint8(a) / 2
  135. return &color.NRGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: faded}
  136. }
  137. func (p *ProgressBar) updateFromData(data binding.DataItem) {
  138. if data == nil {
  139. return
  140. }
  141. floatSource, ok := data.(binding.Float)
  142. if !ok {
  143. return
  144. }
  145. val, err := floatSource.Get()
  146. if err != nil {
  147. fyne.LogError("Error getting current data value", err)
  148. return
  149. }
  150. p.SetValue(val)
  151. }