border.go 800 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. package widget
  3. import (
  4. "image"
  5. "image/color"
  6. "gioui.org/layout"
  7. "gioui.org/op/clip"
  8. "gioui.org/op/paint"
  9. "gioui.org/unit"
  10. )
  11. // Border lays out a widget and draws a border inside it.
  12. type Border struct {
  13. Color color.NRGBA
  14. CornerRadius unit.Dp
  15. Width unit.Dp
  16. }
  17. func (b Border) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
  18. dims := w(gtx)
  19. sz := dims.Size
  20. rr := gtx.Dp(b.CornerRadius)
  21. width := gtx.Dp(b.Width)
  22. whalf := (width + 1) / 2
  23. sz.X -= whalf * 2
  24. sz.Y -= whalf * 2
  25. r := image.Rectangle{Max: sz}
  26. r = r.Add(image.Point{X: whalf, Y: whalf})
  27. paint.FillShape(gtx.Ops,
  28. b.Color,
  29. clip.Stroke{
  30. Path: clip.UniformRRect(r, rr).Path(gtx.Ops),
  31. Width: float32(width),
  32. }.Op(),
  33. )
  34. return dims
  35. }