forked from richardwilkes/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tooltips.go
66 lines (60 loc) · 1.62 KB
/
tooltips.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
package window
import (
"time"
"github.com/richardwilkes/toolbox/xmath/geom"
"github.com/richardwilkes/ui"
"github.com/richardwilkes/ui/layout"
)
var (
// TooltipDelay holds the delay before a tooltip will be shown.
TooltipDelay = 1500 * time.Millisecond
// TooltipDismissal holds the delay before a tooltip will be dismissed.
TooltipDismissal = 3 * time.Second
)
type tooltipSequencer struct {
window *Window
avoid geom.Rect
sequence int
}
func (ts *tooltipSequencer) show() {
if ts.window.tooltipSequence == ts.sequence {
tip := ts.window.lastToolTip
_, pref, _ := ui.Sizes(tip, layout.NoHintSize)
bounds := geom.Rect{Point: geom.Point{X: ts.avoid.X, Y: ts.avoid.Y + ts.avoid.Height + 1}, Size: pref}
if bounds.X < 0 {
bounds.X = 0
}
if bounds.Y < 0 {
bounds.Y = 0
}
viewSize := ts.window.root.Size()
if viewSize.Width < bounds.Width {
_, pref, _ := ui.Sizes(tip, geom.Size{Width: viewSize.Width, Height: layout.NoHint})
if viewSize.Width < pref.Width {
bounds.X = 0
bounds.Width = viewSize.Width
} else {
bounds.Width = pref.Width
}
bounds.Height = pref.Height
}
if viewSize.Width < bounds.X+bounds.Width {
bounds.X = viewSize.Width - bounds.Width
}
if viewSize.Height < bounds.Y+bounds.Height {
bounds.Y = ts.avoid.Y - (bounds.Height + 1)
if bounds.Y < 0 {
bounds.Y = 0
}
}
tip.SetBounds(bounds)
ts.window.root.SetTooltip(tip)
ts.window.lastTooltipShownAt = time.Now()
ts.window.InvokeAfter(ts.close, TooltipDismissal)
}
}
func (ts *tooltipSequencer) close() {
if ts.window.tooltipSequence == ts.sequence {
ts.window.root.SetTooltip(nil)
}
}