base.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Package canvas contains all of the primitive CanvasObjects that make up a Fyne GUI.
  2. //
  3. // The types implemented in this package are used as building blocks in order
  4. // to build higher order functionality. These types are designed to be
  5. // non-interactive, by design. If additional functionality is required,
  6. // it's usually a sign that this type should be used as part of a custom
  7. // widget.
  8. package canvas // import "fyne.io/fyne/v2/canvas"
  9. import (
  10. "sync"
  11. "fyne.io/fyne/v2"
  12. )
  13. type baseObject struct {
  14. size fyne.Size // The current size of the canvas object
  15. position fyne.Position // The current position of the object
  16. Hidden bool // Is this object currently hidden
  17. min fyne.Size // The minimum size this object can be
  18. propertyLock sync.RWMutex
  19. }
  20. // Hide will set this object to not be visible.
  21. func (o *baseObject) Hide() {
  22. o.propertyLock.Lock()
  23. defer o.propertyLock.Unlock()
  24. o.Hidden = true
  25. }
  26. // MinSize returns the specified minimum size, if set, or {1, 1} otherwise.
  27. func (o *baseObject) MinSize() fyne.Size {
  28. o.propertyLock.RLock()
  29. defer o.propertyLock.RUnlock()
  30. if o.min.Width == 0 && o.min.Height == 0 {
  31. return fyne.NewSize(1, 1)
  32. }
  33. return o.min
  34. }
  35. // Move the object to a new position, relative to its parent.
  36. func (o *baseObject) Move(pos fyne.Position) {
  37. o.propertyLock.Lock()
  38. defer o.propertyLock.Unlock()
  39. o.position = pos
  40. }
  41. // Position gets the current position of this canvas object, relative to its parent.
  42. func (o *baseObject) Position() fyne.Position {
  43. o.propertyLock.RLock()
  44. defer o.propertyLock.RUnlock()
  45. return o.position
  46. }
  47. // Resize sets a new size for the canvas object.
  48. func (o *baseObject) Resize(size fyne.Size) {
  49. o.propertyLock.Lock()
  50. defer o.propertyLock.Unlock()
  51. o.size = size
  52. }
  53. // SetMinSize specifies the smallest size this object should be.
  54. func (o *baseObject) SetMinSize(size fyne.Size) {
  55. o.propertyLock.Lock()
  56. defer o.propertyLock.Unlock()
  57. o.min = size
  58. }
  59. // Show will set this object to be visible.
  60. func (o *baseObject) Show() {
  61. o.propertyLock.Lock()
  62. defer o.propertyLock.Unlock()
  63. o.Hidden = false
  64. }
  65. // Size returns the current size of this canvas object.
  66. func (o *baseObject) Size() fyne.Size {
  67. o.propertyLock.RLock()
  68. defer o.propertyLock.RUnlock()
  69. return o.size
  70. }
  71. // Visible returns true if this object is visible, false otherwise.
  72. func (o *baseObject) Visible() bool {
  73. o.propertyLock.RLock()
  74. defer o.propertyLock.RUnlock()
  75. return !o.Hidden
  76. }