simplify SearchEntry and extract a debouncer util func

This commit is contained in:
Drew Weymouth
2023-05-10 21:05:17 -07:00
parent b94ef8da97
commit 5634dfcfc1
8 changed files with 58 additions and 85 deletions
+18
View File
@@ -6,6 +6,8 @@ import (
"math"
"strconv"
"strings"
"sync"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/layout"
@@ -60,6 +62,22 @@ func ImageAspect(im image.Image) float32 {
return float32(b.Max.X-b.Min.X) / float32(b.Max.Y-b.Min.Y)
}
// Debouncer returns a function that will call callOnDone when
// it has not been invoked since the last dur interval.
func NewDebouncer(dur time.Duration, callOnDone func()) func() {
var mu sync.Mutex
var timer *time.Timer
return func() {
mu.Lock()
defer mu.Unlock()
if timer != nil {
timer.Stop()
}
timer = time.AfterFunc(dur, callOnDone)
}
}
func RichTextSegsFromHTMLString(s string) []widget.RichTextSegment {
tokr := html.NewTokenizer(strings.NewReader(s))
var segs []widget.RichTextSegment