Repository navigation
Expand file tree
/
Copy pathnotify.go
More file actions
95 lines (83 loc) · 2.61 KB
/
Copy pathnotify.go
File metadata and controls
95 lines (83 loc) · 2.61 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package view
import (
"webtyp.com/fmt"
"webtyp.com/lang"
"webtyp.com/msgtype"
)
// Duration is how long a notification stays before dismissing itself.
type Duration struct {
millis func(msg string) int // nil = persistent
auto bool // built by Auto(): the caller left the length to the chassis
}
// IsAuto reports whether the duration was created with Auto().
func (d Duration) IsAuto() bool { return d.auto }
// HasTimer reports whether the duration auto-dismisses after a positive number of milliseconds.
func (d Duration) HasTimer() bool { return d.millis != nil }
// Millis computes the duration in milliseconds for msg, or 0 if persistent.
func (d Duration) Millis(msg string) int {
if d.millis == nil {
return 0
}
return d.millis(msg)
}
// Auto sizes the duration to the message (~350ms per word plus 2.5s, floored at 4s and capped at 10s).
func Auto() Duration {
return Duration{auto: true, millis: func(msg string) int {
words := fmt.Count(msg, " ") + 1
ms := 2500 + words*350
if ms < 4000 {
return 4000
}
if ms > 10000 {
return 10000
}
return ms
}}
}
// Persistent keeps the notification until the user dismisses it.
func Persistent() Duration { return Duration{} }
// For pins an exact duration in milliseconds.
func For(ms int) Duration {
return Duration{millis: func(string) int { return ms }}
}
// Notifier raises a notification in the chassis.
type Notifier interface {
Notify(t msgtype.Type, msg string, d Duration)
}
// UsesNotifier is a screen component that tells the user «saved» or an error through the chassis.
//
// func (r *RootView) UseNotifier(n view.Notifier) { r.notifier = n }
type UsesNotifier interface {
UseNotifier(n Notifier)
}
// ErrorMessage turns an operation error (including transport errors that expose
// a clean Message() without status prefixes) into a translated user-facing string.
func ErrorMessage(err error) string {
if err == nil {
return ""
}
var raw string
if m, ok := err.(interface{ Message() string }); ok {
raw = m.Message()
}
if raw == "" {
raw = err.Error()
}
return lang.Translate(raw).String()
}
// NotifySave reports the outcome of a persistence operation through n:
// when err != nil it shows the translated ErrorMessage(err) and returns false;
// when err == nil it shows translated "Saved" and returns true.
// Using NotifySave guarantees no mutation can finish silently.
func NotifySave(n Notifier, err error) bool {
if err != nil {
if n != nil {
n.Notify(msgtype.Error, ErrorMessage(err), Auto())
}
return false
}
if n != nil {
n.Notify(msgtype.Success, lang.Translate("Saved").String(), Auto())
}
return true
}