-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathhyperlink.go
353 lines (294 loc) · 9.07 KB
/
hyperlink.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package widget
import (
"image/color"
"net/url"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/theme"
)
var _ fyne.Focusable = (*Hyperlink)(nil)
var _ fyne.Widget = (*Hyperlink)(nil)
// Hyperlink widget is a text component with appropriate padding and layout.
// When clicked, the default web browser should open with a URL
type Hyperlink struct {
BaseWidget
Text string
URL *url.URL
Alignment fyne.TextAlign // The alignment of the Text
Wrapping fyne.TextWrap // The wrapping of the Text
TextStyle fyne.TextStyle // The style of the hyperlink text
// The truncation mode of the hyperlink
//
// Since: 2.5
Truncation fyne.TextTruncation
// The theme size name for the text size of the hyperlink
//
// Since: 2.5
SizeName fyne.ThemeSizeName
// OnTapped overrides the default `fyne.OpenURL` call when the link is tapped
//
// Since: 2.2
OnTapped func() `json:"-"`
textSize fyne.Size // updated in syncSegments
focused, hovered bool
provider RichText
}
// NewHyperlink creates a new hyperlink widget with the set text content
func NewHyperlink(text string, url *url.URL) *Hyperlink {
return NewHyperlinkWithStyle(text, url, fyne.TextAlignLeading, fyne.TextStyle{})
}
// NewHyperlinkWithStyle creates a new hyperlink widget with the set text content
func NewHyperlinkWithStyle(text string, url *url.URL, alignment fyne.TextAlign, style fyne.TextStyle) *Hyperlink {
hl := &Hyperlink{
Text: text,
URL: url,
Alignment: alignment,
TextStyle: style,
}
return hl
}
// CreateRenderer is a private method to Fyne which links this widget to its renderer
func (hl *Hyperlink) CreateRenderer() fyne.WidgetRenderer {
hl.ExtendBaseWidget(hl)
hl.provider.ExtendBaseWidget(&hl.provider)
hl.syncSegments()
th := hl.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()
focus := canvas.NewRectangle(color.Transparent)
focus.StrokeColor = th.Color(theme.ColorNameFocus, v)
focus.StrokeWidth = 2
focus.Hide()
under := canvas.NewRectangle(th.Color(theme.ColorNameHyperlink, v))
under.Hide()
return &hyperlinkRenderer{hl: hl, objects: []fyne.CanvasObject{&hl.provider, focus, under}, focus: focus, under: under}
}
// Cursor returns the cursor type of this widget
func (hl *Hyperlink) Cursor() desktop.Cursor {
if hl.hovered {
return desktop.PointerCursor
}
return desktop.DefaultCursor
}
// FocusGained is a hook called by the focus handling logic after this object gained the focus.
func (hl *Hyperlink) FocusGained() {
hl.focused = true
hl.BaseWidget.Refresh()
}
// FocusLost is a hook called by the focus handling logic after this object lost the focus.
func (hl *Hyperlink) FocusLost() {
hl.focused = false
hl.BaseWidget.Refresh()
}
// MouseIn is a hook that is called if the mouse pointer enters the element.
func (hl *Hyperlink) MouseIn(e *desktop.MouseEvent) {
hl.MouseMoved(e)
}
// MouseMoved is a hook that is called if the mouse pointer moved over the element.
func (hl *Hyperlink) MouseMoved(e *desktop.MouseEvent) {
oldHovered := hl.hovered
hl.hovered = hl.isPosOverText(e.Position)
if hl.hovered != oldHovered {
hl.BaseWidget.Refresh()
}
}
// MouseOut is a hook that is called if the mouse pointer leaves the element.
func (hl *Hyperlink) MouseOut() {
changed := hl.hovered
hl.hovered = false
if changed {
hl.BaseWidget.Refresh()
}
}
func (hl *Hyperlink) focusWidth() float32 {
th := hl.themeWithLock()
innerPad := th.Size(theme.SizeNameInnerPadding)
return fyne.Min(hl.size.Load().Width, hl.textSize.Width+innerPad+th.Size(theme.SizeNamePadding)*2) - innerPad
}
func (hl *Hyperlink) focusXPos() float32 {
innerPad := hl.themeWithLock().Size(theme.SizeNameInnerPadding)
switch hl.Alignment {
case fyne.TextAlignLeading:
return innerPad / 2
case fyne.TextAlignCenter:
return (hl.size.Load().Width - hl.focusWidth()) / 2
case fyne.TextAlignTrailing:
return (hl.size.Load().Width - hl.focusWidth()) - innerPad/2
default:
return 0 // unreached
}
}
func (hl *Hyperlink) isPosOverText(pos fyne.Position) bool {
th := hl.Theme()
innerPad := th.Size(theme.SizeNameInnerPadding)
pad := th.Size(theme.SizeNamePadding)
lineCount := fyne.Max(1, float32(len(hl.provider.rowBounds)))
xpos := hl.focusXPos()
return pos.X >= xpos && pos.X <= xpos+hl.focusWidth() &&
pos.Y >= innerPad/2 && pos.Y <= hl.textSize.Height*lineCount+pad*2+innerPad/2
}
// Refresh triggers a redraw of the hyperlink.
//
// Implements: fyne.Widget
func (hl *Hyperlink) Refresh() {
if len(hl.provider.Segments) == 0 {
return // Not initialized yet.
}
hl.syncSegments()
hl.provider.Refresh()
hl.BaseWidget.Refresh()
}
// MinSize returns the smallest size this widget can shrink to
func (hl *Hyperlink) MinSize() fyne.Size {
hl.ExtendBaseWidget(hl)
return hl.BaseWidget.MinSize()
}
// Resize sets a new size for the hyperlink.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (hl *Hyperlink) Resize(size fyne.Size) {
hl.BaseWidget.Resize(size)
if len(hl.provider.Segments) == 0 {
return // Not initialized yet.
}
hl.provider.Resize(size)
}
// SetText sets the text of the hyperlink
func (hl *Hyperlink) SetText(text string) {
hl.propertyLock.Lock()
hl.Text = text
hl.propertyLock.Unlock()
if len(hl.provider.Segments) == 0 {
return // Not initialized yet.
}
hl.syncSegments()
hl.provider.Refresh()
}
// SetURL sets the URL of the hyperlink, taking in a URL type
func (hl *Hyperlink) SetURL(url *url.URL) {
hl.propertyLock.Lock()
defer hl.propertyLock.Unlock()
hl.URL = url
}
// SetURLFromString sets the URL of the hyperlink, taking in a string type
func (hl *Hyperlink) SetURLFromString(str string) error {
u, err := url.Parse(str)
if err != nil {
return err
}
hl.SetURL(u)
return nil
}
// Tapped is called when a pointer tapped event is captured and triggers any change handler
func (hl *Hyperlink) Tapped(e *fyne.PointEvent) {
if len(hl.provider.Segments) != 0 && !hl.isPosOverText(e.Position) {
return // tapped outside text area
}
hl.invokeAction()
}
func (hl *Hyperlink) invokeAction() {
hl.propertyLock.RLock()
onTapped := hl.OnTapped
hl.propertyLock.RUnlock()
if onTapped != nil {
onTapped()
return
}
hl.openURL()
}
// TypedRune is a hook called by the input handling logic on text input events if this object is focused.
func (hl *Hyperlink) TypedRune(rune) {
}
// TypedKey is a hook called by the input handling logic on key events if this object is focused.
func (hl *Hyperlink) TypedKey(ev *fyne.KeyEvent) {
if ev.Name == fyne.KeySpace {
hl.invokeAction()
}
}
func (hl *Hyperlink) openURL() {
hl.propertyLock.RLock()
url := hl.URL
hl.propertyLock.RUnlock()
if url != nil {
err := fyne.CurrentApp().OpenURL(url)
if err != nil {
fyne.LogError("Failed to open url", err)
}
}
}
func (hl *Hyperlink) syncSegments() {
th := hl.Theme()
hl.propertyLock.RLock()
defer hl.propertyLock.RUnlock()
hl.provider.Wrapping = hl.Wrapping
hl.provider.Truncation = hl.Truncation
if len(hl.provider.Segments) == 0 {
hl.provider.Scroll = widget.ScrollNone
hl.provider.Segments = []RichTextSegment{
&TextSegment{
Style: RichTextStyle{
Alignment: hl.Alignment,
ColorName: theme.ColorNameHyperlink,
Inline: true,
TextStyle: hl.TextStyle,
},
Text: hl.Text,
},
}
} else {
segment := hl.provider.Segments[0].(*TextSegment)
segment.Style.Alignment = hl.Alignment
segment.Style.TextStyle = hl.TextStyle
segment.Text = hl.Text
}
sizeName := hl.SizeName
if sizeName == "" {
sizeName = theme.SizeNameText
}
hl.provider.Segments[0].(*TextSegment).Style.SizeName = sizeName
hl.textSize = fyne.MeasureText(hl.Text, th.Size(sizeName), hl.TextStyle)
}
var _ fyne.WidgetRenderer = (*hyperlinkRenderer)(nil)
type hyperlinkRenderer struct {
hl *Hyperlink
focus *canvas.Rectangle
under *canvas.Rectangle
objects []fyne.CanvasObject
}
func (r *hyperlinkRenderer) Destroy() {
}
func (r *hyperlinkRenderer) Layout(s fyne.Size) {
th := r.hl.Theme()
r.hl.propertyLock.RLock()
textSize := r.hl.textSize
innerPad := th.Size(theme.SizeNameInnerPadding)
w := r.hl.focusWidth()
xposFocus := r.hl.focusXPos()
r.hl.propertyLock.RUnlock()
xposUnderline := xposFocus + innerPad/2
lineCount := float32(len(r.hl.provider.rowBounds))
r.hl.provider.Resize(s)
r.focus.Move(fyne.NewPos(xposFocus, innerPad/2))
r.focus.Resize(fyne.NewSize(w, textSize.Height*lineCount+innerPad))
r.under.Move(fyne.NewPos(xposUnderline, textSize.Height*lineCount+th.Size(theme.SizeNamePadding)*2))
r.under.Resize(fyne.NewSize(w-innerPad, 1))
}
func (r *hyperlinkRenderer) MinSize() fyne.Size {
return r.hl.provider.MinSize()
}
func (r *hyperlinkRenderer) Objects() []fyne.CanvasObject {
return r.objects
}
func (r *hyperlinkRenderer) Refresh() {
r.hl.provider.Refresh()
th := r.hl.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()
r.hl.propertyLock.RLock()
defer r.hl.propertyLock.RUnlock()
r.focus.StrokeColor = th.Color(theme.ColorNameFocus, v)
r.focus.Hidden = !r.hl.focused
r.focus.Refresh()
r.under.FillColor = th.Color(theme.ColorNameHyperlink, v)
r.under.Hidden = !r.hl.hovered
r.under.Refresh()
}