clip.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package internal
  2. import "fyne.io/fyne/v2"
  3. // ClipStack keeps track of the areas that should be clipped when drawing a canvas.
  4. // If no clips are present then adding one will be added as-is.
  5. // Subsequent items pushed will be completely within the previous clip.
  6. type ClipStack struct {
  7. clips []*ClipItem
  8. }
  9. // Pop removes the current top clip and returns it.
  10. func (c *ClipStack) Pop() *ClipItem {
  11. if len(c.clips) == 0 {
  12. return nil
  13. }
  14. top := len(c.clips) - 1
  15. ret := c.clips[top]
  16. c.clips[top] = nil // release memory reference
  17. c.clips = c.clips[:top]
  18. return ret
  19. }
  20. // Length returns the number of items in this clip stack. 0 means no clip.
  21. func (c *ClipStack) Length() int {
  22. return len(c.clips)
  23. }
  24. // Push a new clip onto this stack at position and size specified.
  25. // The returned clip item is the result of calculating the intersection of the requested clip and it's parent.
  26. func (c *ClipStack) Push(p fyne.Position, s fyne.Size) *ClipItem {
  27. outer := c.Top()
  28. inner := outer.Intersect(p, s)
  29. c.clips = append(c.clips, inner)
  30. return inner
  31. }
  32. // Top returns the current clip item - it will always be within the bounds of any parent clips.
  33. func (c *ClipStack) Top() *ClipItem {
  34. if len(c.clips) == 0 {
  35. return nil
  36. }
  37. return c.clips[len(c.clips)-1]
  38. }
  39. // ClipItem represents a single clip in a clip stack, denoted by a size and position.
  40. type ClipItem struct {
  41. pos fyne.Position
  42. size fyne.Size
  43. }
  44. // Rect returns the position and size parameters of the clip.
  45. func (i *ClipItem) Rect() (fyne.Position, fyne.Size) {
  46. return i.pos, i.size
  47. }
  48. // Intersect returns a new clip item that is the intersection of the requested parameters and this clip.
  49. func (i *ClipItem) Intersect(p fyne.Position, s fyne.Size) *ClipItem {
  50. ret := &ClipItem{p, s}
  51. if i == nil {
  52. return ret
  53. }
  54. if ret.pos.X < i.pos.X {
  55. ret.pos.X = i.pos.X
  56. ret.size.Width -= i.pos.X - p.X
  57. }
  58. if ret.pos.Y < i.pos.Y {
  59. ret.pos.Y = i.pos.Y
  60. ret.size.Height -= i.pos.Y - p.Y
  61. }
  62. if p.X+s.Width > i.pos.X+i.size.Width {
  63. ret.size.Width = (i.pos.X + i.size.Width) - ret.pos.X
  64. }
  65. if p.Y+s.Height > i.pos.Y+i.size.Height {
  66. ret.size.Height = (i.pos.Y + i.size.Height) - ret.pos.Y
  67. }
  68. if ret.size.Width < 0 || ret.size.Height < 0 {
  69. ret.size = fyne.NewSize(0, 0)
  70. return ret
  71. }
  72. return ret
  73. }