forked from fyne-io/fyne
/
hyperlink.go
126 lines (104 loc) · 3.37 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
package widget
import (
"image/color"
"net/url"
"fyne.io/fyne"
"fyne.io/fyne/theme"
)
// 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 {
textProvider
Text string
URL *url.URL
Alignment fyne.TextAlign // The alignment of the Text
TextStyle fyne.TextStyle // The style of the hyperlink text
}
// 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
}
// SetText sets the text of the hyperlink
func (hl *Hyperlink) SetText(text string) {
hl.Text = text
hl.textProvider.SetText(text) // calls refresh
}
// SetURL sets the URL of the hyperlink, taking in a URL type
func (hl *Hyperlink) SetURL(url *url.URL) {
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.URL = u
return nil
}
// textAlign tells the rendering textProvider our alignment
func (hl *Hyperlink) textAlign() fyne.TextAlign {
return hl.Alignment
}
// textStyle tells the rendering textProvider our style
func (hl *Hyperlink) textStyle() fyne.TextStyle {
return hl.TextStyle
}
// textColor tells the rendering textProvider our color
func (hl *Hyperlink) textColor() color.Color {
return theme.HyperlinkColor()
}
// password tells the rendering textProvider if we are a password field
func (hl *Hyperlink) password() bool {
return false
}
// object returns the root object of the widget so it can be referenced
func (hl *Hyperlink) object() fyne.Widget {
return hl
}
// Tapped is called when a pointer tapped event is captured and triggers any change handler
func (hl *Hyperlink) Tapped(*fyne.PointEvent) {
if hl.URL != nil {
fyne.CurrentApp().OpenURL(hl.URL)
}
}
// TappedSecondary is called when a secondary pointer tapped event is captured
func (hl *Hyperlink) TappedSecondary(*fyne.PointEvent) {
}
// CreateRenderer is a private method to Fyne which links this widget to it's renderer
func (hl *Hyperlink) CreateRenderer() fyne.WidgetRenderer {
hl.textProvider = newTextProvider(hl.Text, hl)
return hl.textProvider.CreateRenderer()
}
// Resize sets a new size for a widget.
// 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.resize(size, hl)
}
// Move the widget to a new position, relative to it's parent.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (hl *Hyperlink) Move(pos fyne.Position) {
hl.move(pos, hl)
}
// MinSize returns the smallest size this widget can shrink to
func (hl *Hyperlink) MinSize() fyne.Size {
return hl.minSize(hl)
}
// Show this widget, if it was previously hidden
func (hl *Hyperlink) Show() {
hl.show(hl)
}
// Hide this widget, if it was previously visible
func (hl *Hyperlink) Hide() {
hl.hide(hl)
}