~gioverse/chat

ref: ff42a2f8b59287707042461aa1ca1347fb8250eb chat/widget/material/separator.go -rw-r--r-- 1.8 KiB
ff42a2f8Chris Waldon list: update Loader to return if more elements 1 year, 15 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package material

import (
	"image"
	"time"

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

// SeparatorStyle configures the presentation of the unread indicator.
type SeparatorStyle struct {
	Message    material.LabelStyle
	TextMargin layout.Inset
	LineMargin layout.Inset
	LineWidth  unit.Value
}

// UnreadSeparator fills in a SeparatorStyle with sensible defaults.
func UnreadSeparator(th *material.Theme) SeparatorStyle {
	us := SeparatorStyle{
		Message:    material.Body1(th, "New Messages"),
		TextMargin: layout.UniformInset(unit.Dp(8)),
		LineMargin: layout.UniformInset(unit.Dp(8)),
		LineWidth:  unit.Dp(2),
	}
	us.Message.Color = th.ContrastBg
	return us
}

// DateSeparator makes a SeparatorStyle with indicating the transition to
// the date provided in the time.Time.
func DateSeparator(th *material.Theme, date time.Time) SeparatorStyle {
	return SeparatorStyle{
		Message:    material.Body1(th, date.Format("Mon Jan 2, 2006")),
		TextMargin: layout.UniformInset(unit.Dp(8)),
		LineMargin: layout.UniformInset(unit.Dp(8)),
		LineWidth:  unit.Dp(2),
	}
}

// Layout the Separator.
func (u SeparatorStyle) Layout(gtx layout.Context) layout.Dimensions {
	layoutLine := func(gtx layout.Context) layout.Dimensions {
		return u.LineMargin.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
			size := image.Point{
				X: gtx.Constraints.Max.X,
				Y: gtx.Px(u.LineWidth),
			}
			paint.FillShape(gtx.Ops, u.Message.Color, clip.Rect(image.Rectangle{Max: size}).Op())
			return layout.Dimensions{Size: size}
		})
	}
	return layout.Flex{
		Alignment: layout.Middle,
	}.Layout(gtx,
		layout.Flexed(.5, layoutLine),
		layout.Rigid(func(gtx layout.Context) layout.Dimensions {
			return u.TextMargin.Layout(gtx, u.Message.Layout)
		}),
		layout.Flexed(.5, layoutLine),
	)
}