fill.go raw

   1  package gel
   2  
   3  import (
   4  	"image"
   5  	"image/color"
   6  	
   7  	"github.com/p9c/gio/f32"
   8  	l "github.com/p9c/gio/layout"
   9  	"github.com/p9c/gio/op/clip"
  10  	"github.com/p9c/gio/op/paint"
  11  )
  12  
  13  // Filler fills the background of a widget with a specified color and corner
  14  // radius
  15  type Filler struct {
  16  	*Window
  17  	col          string
  18  	w            l.Widget
  19  	dxn          l.Direction
  20  	cornerRadius float32
  21  	corners      int
  22  }
  23  
  24  const (
  25  	NW = 1 << iota
  26  	NE
  27  	SW
  28  	SE
  29  )
  30  
  31  // Fill fills underneath a widget you can put over top of it, dxn sets which
  32  // direction to place a smaller object, cardinal axes and center
  33  func (w *Window) Fill(col string, dxn l.Direction, radius float32, corners int, embed l.Widget) *Filler {
  34  	return &Filler{Window: w, col: col, w: embed, dxn: dxn, cornerRadius: radius, corners: corners}
  35  }
  36  
  37  // Fn renders the fill with the widget inside
  38  func (f *Filler) Fn(gtx l.Context) l.Dimensions {
  39  	gtx1 := CopyContextDimensionsWithMaxAxis(gtx, l.Horizontal)
  40  	// generate the dimensions for all the list elements
  41  	dL := GetDimensionList(gtx1, 1, func(gtx l.Context, index int) l.Dimensions {
  42  		return f.w(gtx)
  43  	})
  44  	fill(gtx, f.Colors.GetNRGBAFromName(f.col), dL[0].Size, f.cornerRadius, f.corners)
  45  	return f.dxn.Layout(gtx, f.w)
  46  }
  47  
  48  func ifDir(radius float32, dir int) float32 {
  49  	if dir != 0 {
  50  		return radius
  51  	}
  52  	return 0
  53  }
  54  
  55  func fill(gtx l.Context, col color.NRGBA, bounds image.Point, radius float32, cnrs int) {
  56  	rect := f32.Rectangle{
  57  		Max: f32.Pt(float32(bounds.X), float32(bounds.Y)),
  58  	}
  59  	clip.RRect{
  60  		Rect: rect,
  61  		NW:   ifDir(radius, cnrs&NW),
  62  		NE:   ifDir(radius, cnrs&NE),
  63  		SW:   ifDir(radius, cnrs&SW),
  64  		SE:   ifDir(radius, cnrs&SE),
  65  	}.Add(gtx.Ops)
  66  	paint.Fill(gtx.Ops, col)
  67  }
  68