Skip to content

Commit

Permalink
Allow hyperlink to override default behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jun 2, 2022
1 parent 41496dc commit 156e43e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions widget/hyperlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type Hyperlink struct {
Wrapping fyne.TextWrap // The wrapping of the Text
TextStyle fyne.TextStyle // The style of the hyperlink text

// OnTapped overrides the default `fyne.OpenURL` call when the link is tapped
//
// Since: 2.2
OnTapped func() `json:"-"`

focused, hovered bool
provider *RichText
}
Expand Down Expand Up @@ -152,6 +157,11 @@ func (hl *Hyperlink) SetURLFromString(str string) error {

// Tapped is called when a pointer tapped event is captured and triggers any change handler
func (hl *Hyperlink) Tapped(*fyne.PointEvent) {
if hl.OnTapped != nil {
hl.OnTapped()
return
}

hl.openURL()
}

Expand Down
13 changes: 13 additions & 0 deletions widget/hyperlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ func TestHyperlink_Focus(t *testing.T) {
test.AssertImageMatches(t, "hyperlink/initial.png", w.Canvas().Capture())
}

func TestHyperlink_OnTapped(t *testing.T) {
tapped := 0
link := &Hyperlink{Text: "Test"}
test.Tap(link)
assert.Equal(t, 0, tapped)

link.OnTapped = func() {
tapped++
}
test.Tap(link)
assert.Equal(t, 1, tapped)
}

func TestHyperlink_Resize(t *testing.T) {
hyperlink := &Hyperlink{Text: "Test"}
hyperlink.CreateRenderer()
Expand Down

0 comments on commit 156e43e

Please sign in to comment.