rectangle.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package canvas
  2. import (
  3. "image/color"
  4. "fyne.io/fyne/v2"
  5. )
  6. // Declare conformity with CanvasObject interface
  7. var _ fyne.CanvasObject = (*Rectangle)(nil)
  8. // Rectangle describes a colored rectangle primitive in a Fyne canvas
  9. type Rectangle struct {
  10. baseObject
  11. FillColor color.Color // The rectangle fill color
  12. StrokeColor color.Color // The rectangle stroke color
  13. StrokeWidth float32 // The stroke width of the rectangle
  14. }
  15. // Hide will set this rectangle to not be visible
  16. func (r *Rectangle) Hide() {
  17. r.baseObject.Hide()
  18. repaint(r)
  19. }
  20. // Move the rectangle to a new position, relative to its parent / canvas
  21. func (r *Rectangle) Move(pos fyne.Position) {
  22. r.baseObject.Move(pos)
  23. repaint(r)
  24. }
  25. // Refresh causes this rectangle to be redrawn with its configured state.
  26. func (r *Rectangle) Refresh() {
  27. Refresh(r)
  28. }
  29. // Resize on a rectangle updates the new size of this object.
  30. // If it has a stroke width this will cause it to Refresh.
  31. func (r *Rectangle) Resize(s fyne.Size) {
  32. if s == r.Size() {
  33. return
  34. }
  35. r.baseObject.Resize(s)
  36. if r.StrokeWidth == 0 {
  37. return
  38. }
  39. Refresh(r)
  40. }
  41. // NewRectangle returns a new Rectangle instance
  42. func NewRectangle(color color.Color) *Rectangle {
  43. return &Rectangle{
  44. FillColor: color,
  45. }
  46. }