~gioverse/chat

ref: ff42a2f8b59287707042461aa1ca1347fb8250eb chat/widget/material/bubble.go -rw-r--r-- 1.2 KiB
ff42a2f8Chris Waldon list: update Loader to return if more elements 1 year, 10 days 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
package material

import (
	"image/color"

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

// BubbleStyle defines a colored surface with (optionally) rounded corners.
type BubbleStyle struct {
	// The radius of the corners of the surface.
	// Non-rounded rectangles can just provide a zero.
	CornerRadius unit.Value
	Color        color.NRGBA
}

// Bubble creates a Bubble style for the provided theme with the theme
// background color and rounded corners.
func Bubble(th *material.Theme) BubbleStyle {
	return BubbleStyle{
		CornerRadius: unit.Dp(12),
		Color:        th.Bg,
	}
}

// Layout renders the BubbleStyle, beneath the provided widget.
func (c BubbleStyle) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
	return layout.Stack{}.Layout(gtx,
		layout.Expanded(func(gtx layout.Context) layout.Dimensions {
			surface := clip.UniformRRect(f32.Rectangle{
				Max: layout.FPt(gtx.Constraints.Min),
			}, float32(gtx.Px(c.CornerRadius)))
			paint.FillShape(gtx.Ops, c.Color, surface.Op(gtx.Ops))
			return layout.Dimensions{Size: gtx.Constraints.Min}
		}),
		layout.Stacked(w),
	)
}