-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
searchentry.go
99 lines (83 loc) · 1.96 KB
/
searchentry.go
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
96
97
98
99
package widgets
import (
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/dweymouth/supersonic/ui/util"
)
// SearchEntry is a search entry widget that will issue a search command
// (aka call OnSearched) when a short span of time has elapsed since
// the user typed into the widget.
type SearchEntry struct {
widget.Entry
OnSearched func(string)
}
func NewSearchEntry() *SearchEntry {
sf := &SearchEntry{}
sf.ExtendBaseWidget(sf)
sf.Init()
return sf
}
// For use only by extending widgets
func (sf *SearchEntry) Init() {
sf.PlaceHolder = "Search"
sf.ActionItem = NewClearTextButton(func() {
sf.SetText("")
})
debounceFunc := util.NewDebouncer(200*time.Millisecond, func() {
sf.sendSearch(sf.Entry.Text)
})
sf.Entry.OnChanged = func(_ string) {
if sf.updateActionButton() {
sf.ActionItem.Refresh()
}
debounceFunc()
}
}
func (s *SearchEntry) TypedKey(e *fyne.KeyEvent) {
if e.Name == fyne.KeyEscape {
s.SetText("")
fyne.CurrentApp().Driver().CanvasForObject(s).Unfocus()
return
}
s.Entry.TypedKey(e)
}
func (s *SearchEntry) Refresh() {
s.updateActionButton()
s.Entry.Refresh()
}
func (s *SearchEntry) MinSize() fyne.Size {
return fyne.NewSize(200, s.Entry.MinSize().Height)
}
func (s *SearchEntry) updateActionButton() bool {
btn := s.ActionItem.(*clearTextButton)
oldResouce := btn.Resource
if s.Text == "" {
btn.Resource = theme.SearchIcon()
} else {
btn.Resource = theme.ContentClearIcon()
}
return oldResouce != btn.Resource
}
var _ fyne.Tappable = (*clearTextButton)(nil)
type clearTextButton struct {
widget.Icon
OnTapped func()
}
func (s *SearchEntry) sendSearch(text string) {
if s.OnSearched != nil {
s.OnSearched(text)
}
}
func NewClearTextButton(onTapped func()) *clearTextButton {
c := &clearTextButton{OnTapped: onTapped}
c.ExtendBaseWidget(c)
c.Resource = theme.SearchIcon()
return c
}
func (c *clearTextButton) Tapped(*fyne.PointEvent) {
if c.OnTapped != nil {
c.OnTapped()
}
}