~gioverse/chat

ref: 67742c9d042c4a206eead8dc911801abdb907117 chat/widget/material/image.go -rw-r--r-- 1.0 KiB
67742c9dChris Waldon example/carousel: adapt to new Loader signature 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package material

import (
	"image"

	"gioui.org/f32"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/clip"
	"gioui.org/unit"
	"gioui.org/widget"
)

// Image lays out an image with optionally rounded corners.
type Image struct {
	widget.Image
	widget.Clickable
	// Radii specifies the amount of rounding.
	Radii unit.Value
	// Width and Height specify respective dimensions.
	// If left empty, dimensions will be unconstrained.
	Width, Height unit.Value
}

// Layout the image.
func (img Image) Layout(gtx layout.Context) layout.Dimensions {
	if img.Width.V > 0 {
		gtx.Constraints.Max.X = gtx.Constraints.Constrain(image.Pt(gtx.Px(img.Width), 0)).X
	}
	if img.Height.V > 0 {
		gtx.Constraints.Max.Y = gtx.Constraints.Constrain(image.Pt(0, gtx.Px(img.Height))).Y
	}
	defer op.Save(gtx.Ops).Load()
	macro := op.Record(gtx.Ops)
	dims := img.Image.Layout(gtx)
	call := macro.Stop()
	r := float32(gtx.Px(img.Radii))
	clip.RRect{
		Rect: f32.Rectangle{Max: layout.FPt(dims.Size)},
		NE:   r, NW: r, SE: r, SW: r,
	}.Add(gtx.Ops)
	call.Add(gtx.Ops)
	return dims
}