circle.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package canvas
  2. import (
  3. "image/color"
  4. "fyne.io/fyne/v2"
  5. )
  6. // Declare conformity with CanvasObject interface
  7. var _ fyne.CanvasObject = (*Circle)(nil)
  8. // Circle describes a colored circle primitive in a Fyne canvas
  9. type Circle struct {
  10. Position1 fyne.Position // The current top-left position of the Circle
  11. Position2 fyne.Position // The current bottomright position of the Circle
  12. Hidden bool // Is this circle currently hidden
  13. FillColor color.Color // The circle fill color
  14. StrokeColor color.Color // The circle stroke color
  15. StrokeWidth float32 // The stroke width of the circle
  16. }
  17. // NewCircle returns a new Circle instance
  18. func NewCircle(color color.Color) *Circle {
  19. return &Circle{
  20. FillColor: color,
  21. }
  22. }
  23. // Hide will set this circle to not be visible
  24. func (c *Circle) Hide() {
  25. c.Hidden = true
  26. repaint(c)
  27. }
  28. // MinSize for a Circle simply returns Size{1, 1} as there is no
  29. // explicit content
  30. func (c *Circle) MinSize() fyne.Size {
  31. return fyne.NewSize(1, 1)
  32. }
  33. // Move the circle object to a new position, relative to its parent / canvas
  34. func (c *Circle) Move(pos fyne.Position) {
  35. size := c.Size()
  36. c.Position1 = pos
  37. c.Position2 = fyne.NewPos(c.Position1.X+size.Width, c.Position1.Y+size.Height)
  38. repaint(c)
  39. }
  40. // Position gets the current top-left position of this circle object, relative to its parent / canvas
  41. func (c *Circle) Position() fyne.Position {
  42. return c.Position1
  43. }
  44. // Refresh causes this object to be redrawn with its configured state.
  45. func (c *Circle) Refresh() {
  46. Refresh(c)
  47. }
  48. // Resize sets a new bottom-right position for the circle object
  49. // If it has a stroke width this will cause it to Refresh.
  50. func (c *Circle) Resize(size fyne.Size) {
  51. if size == c.Size() {
  52. return
  53. }
  54. c.Position2 = fyne.NewPos(c.Position1.X+size.Width, c.Position1.Y+size.Height)
  55. Refresh(c)
  56. }
  57. // Show will set this circle to be visible
  58. func (c *Circle) Show() {
  59. c.Hidden = false
  60. c.Refresh()
  61. }
  62. // Size returns the current size of bounding box for this circle object
  63. func (c *Circle) Size() fyne.Size {
  64. return fyne.NewSize(c.Position2.X-c.Position1.X, c.Position2.Y-c.Position1.Y)
  65. }
  66. // Visible returns true if this circle is visible, false otherwise
  67. func (c *Circle) Visible() bool {
  68. return !c.Hidden
  69. }