/
tooltip.go
143 lines (131 loc) · 3.85 KB
/
tooltip.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright ©2021-2022 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"strings"
"time"
"github.com/richardwilkes/unison/enums/paintstyle"
)
// DefaultTooltipTheme holds the default TooltipTheme values for Tooltips. Modifying this data will not alter existing
// Tooltips, but will alter any Tooltips created in the future.
var DefaultTooltipTheme = TooltipTheme{
BackgroundInk: TooltipColor,
BaseBorder: NewCompoundBorder(NewLineBorder(ControlEdgeColor, 0, NewUniformInsets(1), false),
NewEmptyBorder(StdInsets())),
Label: defaultToolTipLabelTheme(),
Delay: 1500 * time.Millisecond,
Dismissal: 5 * time.Second,
}
func defaultToolTipLabelTheme() LabelTheme {
theme := DefaultLabelTheme
theme.Font = FieldFont
theme.OnBackgroundInk = OnTooltipColor
return theme
}
// TooltipTheme holds theming data for a Tooltip.
type TooltipTheme struct {
SecondaryTextFont Font
BackgroundInk Ink
BaseBorder Border
Label LabelTheme
Delay time.Duration
Dismissal time.Duration
}
type tooltipSequencer struct {
window *Window
avoid Rect
sequence int
}
// NewTooltipBase returns the base for a tooltip.
func NewTooltipBase() *Panel {
tip := NewPanel()
tip.SetBorder(DefaultTooltipTheme.BaseBorder)
tip.DrawCallback = func(canvas *Canvas, _ Rect) {
r := tip.ContentRect(true)
canvas.DrawRect(r, DefaultTooltipTheme.BackgroundInk.Paint(canvas, r, paintstyle.Fill))
}
return tip
}
// NewTooltipWithText creates a standard text tooltip panel.
func NewTooltipWithText(text string) *Panel {
tip := NewTooltipBase()
tip.SetLayout(&FlexLayout{
Columns: 1,
HSpacing: StdHSpacing,
VSpacing: StdVSpacing,
})
for _, str := range strings.Split(text, "\n") {
l := NewLabel()
l.LabelTheme = DefaultTooltipTheme.Label
l.Text = str
tip.AddChild(l)
}
return tip
}
// NewTooltipWithSecondaryText creates a text tooltip panel containing a primary piece of text along with a secondary
// piece of text in a slightly smaller font.
func NewTooltipWithSecondaryText(primary, secondary string) *Panel {
tip := NewTooltipWithText(primary)
if secondary != "" {
for _, str := range strings.Split(secondary, "\n") {
l := NewLabel()
l.LabelTheme = DefaultTooltipTheme.Label
desc := DefaultTooltipTheme.Label.Font.Descriptor()
desc.Size--
l.LabelTheme.Font = desc.Font()
l.Text = str
tip.AddChild(l)
}
}
return tip
}
func (ts *tooltipSequencer) show() {
if ts.window.tooltipSequence == ts.sequence && ts.window.Focused() {
tip := ts.window.lastTooltip
_, pref, _ := tip.Sizes(Size{})
rect := Rect{Point: Point{X: ts.avoid.X, Y: ts.avoid.Bottom() + 1}, Size: pref}
if rect.X < 0 {
rect.X = 0
}
if rect.Y < 0 {
rect.Y = 0
}
viewSize := ts.window.root.ContentRect(true).Size
if viewSize.Width < rect.Width {
_, pref, _ = tip.Sizes(Size{Width: viewSize.Width})
if viewSize.Width < pref.Width {
rect.X = 0
rect.Width = viewSize.Width
} else {
rect.Width = pref.Width
}
rect.Height = pref.Height
}
if viewSize.Width < rect.X+rect.Width {
rect.X = viewSize.Width - rect.Width
}
if viewSize.Height < rect.Y+rect.Height {
rect.Y = ts.avoid.Y - (rect.Height + 1)
if rect.Y < 0 {
rect.Y = 0
}
}
tip.SetFrameRect(rect)
ts.window.root.setTooltip(tip)
ts.window.lastTooltipShownAt = time.Now()
if !tip.TooltipImmediate {
InvokeTaskAfter(ts.close, DefaultTooltipTheme.Dismissal)
}
}
}
func (ts *tooltipSequencer) close() {
if ts.window.tooltipSequence == ts.sequence {
ts.window.root.setTooltip(nil)
}
}