rectangle.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // The radius of the rectangle corners
  15. //
  16. // Since: 2.4
  17. CornerRadius float32
  18. }
  19. // Hide will set this rectangle to not be visible
  20. func (r *Rectangle) Hide() {
  21. r.baseObject.Hide()
  22. repaint(r)
  23. }
  24. // Move the rectangle to a new position, relative to its parent / canvas
  25. func (r *Rectangle) Move(pos fyne.Position) {
  26. r.baseObject.Move(pos)
  27. repaint(r)
  28. }
  29. // Refresh causes this rectangle to be redrawn with its configured state.
  30. func (r *Rectangle) Refresh() {
  31. Refresh(r)
  32. }
  33. // Resize on a rectangle updates the new size of this object.
  34. // If it has a stroke width this will cause it to Refresh.
  35. func (r *Rectangle) Resize(s fyne.Size) {
  36. if s == r.Size() {
  37. return
  38. }
  39. r.baseObject.Resize(s)
  40. if r.StrokeWidth == 0 {
  41. return
  42. }
  43. Refresh(r)
  44. }
  45. // NewRectangle returns a new Rectangle instance
  46. func NewRectangle(color color.Color) *Rectangle {
  47. return &Rectangle{
  48. FillColor: color,
  49. }
  50. }