line.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package canvas
  2. import (
  3. "image/color"
  4. "math"
  5. "fyne.io/fyne/v2"
  6. )
  7. // Declare conformity with CanvasObject interface
  8. var _ fyne.CanvasObject = (*Line)(nil)
  9. // Line describes a colored line primitive in a Fyne canvas.
  10. // Lines are special as they can have a negative width or height to indicate
  11. // an inverse slope (i.e. slope up vs down).
  12. type Line struct {
  13. Position1 fyne.Position // The current top-left position of the Line
  14. Position2 fyne.Position // The current bottom-right position of the Line
  15. Hidden bool // Is this Line currently hidden
  16. StrokeColor color.Color // The line stroke color
  17. StrokeWidth float32 // The stroke width of the line
  18. }
  19. // Size returns the current size of bounding box for this line object
  20. func (l *Line) Size() fyne.Size {
  21. return fyne.NewSize(float32(math.Abs(float64(l.Position2.X)-float64(l.Position1.X))),
  22. float32(math.Abs(float64(l.Position2.Y)-float64(l.Position1.Y))))
  23. }
  24. // Resize sets a new bottom-right position for the line object, then it will then be refreshed.
  25. func (l *Line) Resize(size fyne.Size) {
  26. if size == l.Size() {
  27. return
  28. }
  29. if l.Position1.X <= l.Position2.X {
  30. l.Position2.X = l.Position1.X + size.Width
  31. } else {
  32. l.Position1.X = l.Position2.X + size.Width
  33. }
  34. if l.Position1.Y <= l.Position2.Y {
  35. l.Position2.Y = l.Position1.Y + size.Height
  36. } else {
  37. l.Position1.Y = l.Position2.Y + size.Height
  38. }
  39. Refresh(l)
  40. }
  41. // Position gets the current top-left position of this line object, relative to its parent / canvas
  42. func (l *Line) Position() fyne.Position {
  43. return fyne.NewPos(fyne.Min(l.Position1.X, l.Position2.X), fyne.Min(l.Position1.Y, l.Position2.Y))
  44. }
  45. // Move the line object to a new position, relative to its parent / canvas
  46. func (l *Line) Move(pos fyne.Position) {
  47. oldPos := l.Position()
  48. deltaX := pos.X - oldPos.X
  49. deltaY := pos.Y - oldPos.Y
  50. l.Position1 = l.Position1.Add(fyne.NewPos(deltaX, deltaY))
  51. l.Position2 = l.Position2.Add(fyne.NewPos(deltaX, deltaY))
  52. repaint(l)
  53. }
  54. // MinSize for a Line simply returns Size{1, 1} as there is no
  55. // explicit content
  56. func (l *Line) MinSize() fyne.Size {
  57. return fyne.NewSize(1, 1)
  58. }
  59. // Visible returns true if this line// Show will set this circle to be visible is visible, false otherwise
  60. func (l *Line) Visible() bool {
  61. return !l.Hidden
  62. }
  63. // Show will set this line to be visible
  64. func (l *Line) Show() {
  65. l.Hidden = false
  66. l.Refresh()
  67. }
  68. // Hide will set this line to not be visible
  69. func (l *Line) Hide() {
  70. l.Hidden = true
  71. repaint(l)
  72. }
  73. // Refresh causes this line to be redrawn with its configured state.
  74. func (l *Line) Refresh() {
  75. Refresh(l)
  76. }
  77. // NewLine returns a new Line instance
  78. func NewLine(color color.Color) *Line {
  79. return &Line{
  80. StrokeColor: color,
  81. StrokeWidth: 1,
  82. }
  83. }