-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoast.go
More file actions
64 lines (58 loc) · 1.56 KB
/
Copy pathtoast.go
File metadata and controls
64 lines (58 loc) · 1.56 KB
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
package proton
import (
"image"
"sync"
"time"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget/material"
)
// ToastState holds notification state. Declare one in your UI struct.
//
// u.toast.Show("Saved!", 2*time.Second)
// proton.Toast(win, &u.toast) // call last so it renders on top
type ToastState struct {
msg string
until time.Time
mu sync.Mutex
}
func (t *ToastState) Show(msg string, duration time.Duration) {
t.mu.Lock()
t.msg = msg
t.until = time.Now().Add(duration)
t.mu.Unlock()
}
func (t *ToastState) active() string {
t.mu.Lock()
defer t.mu.Unlock()
if time.Now().Before(t.until) {
return t.msg
}
return ""
}
// Toast draws a pill notification. Call last in your draw function.
func Toast(win *Win, state *ToastState) {
msg := state.active()
if msg == "" {
return
}
win.Invalidate()
win.add(func(gtx layout.Context) layout.Dimensions {
return layout.Stack{Alignment: layout.Center}.Layout(gtx,
layout.Expanded(func(gtx layout.Context) layout.Dimensions {
w, h := gtx.Constraints.Min.X, gtx.Constraints.Min.Y
r := gtx.Dp(unit.Dp(h/2 + 1))
rrect := clip.RRect{Rect: image.Rect(0, 0, w, h), NW: r, NE: r, SE: r, SW: r}
paint.FillShape(gtx.Ops, win.th.Palette.ContrastBg, rrect.Op(gtx.Ops))
return layout.Dimensions{Size: image.Pt(w, h)}
}),
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
lbl := material.Body2(win.th, msg)
lbl.Color = win.th.Palette.ContrastFg
return layout.UniformInset(unit.Dp(10)).Layout(gtx, lbl.Layout)
}),
)
})
}