Skip to content

Commit

Permalink
add locking to Hyperlink
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Feb 1, 2024
1 parent 3ad2fa0 commit 77e922d
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions widget/hyperlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func NewHyperlinkWithStyle(text string, url *url.URL, alignment fyne.TextAlign,
// CreateRenderer is a private method to Fyne which links this widget to its renderer
func (hl *Hyperlink) CreateRenderer() fyne.WidgetRenderer {
hl.ExtendBaseWidget(hl)
hl.propertyLock.RLock()
hl.provider = NewRichTextWithText(hl.Text)
hl.propertyLock.RUnlock()
hl.provider.ExtendBaseWidget(hl.provider)
hl.syncSegments()

Expand Down Expand Up @@ -179,7 +181,9 @@ func (hl *Hyperlink) Resize(size fyne.Size) {

// SetText sets the text of the hyperlink
func (hl *Hyperlink) SetText(text string) {
hl.propertyLock.Lock()
hl.Text = text
hl.propertyLock.Unlock()
if hl.provider == nil { // not created until visible
return
}
Expand All @@ -189,6 +193,9 @@ func (hl *Hyperlink) SetText(text string) {

// 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
}

Expand All @@ -198,7 +205,7 @@ func (hl *Hyperlink) SetURLFromString(str string) error {
if err != nil {
return err
}
hl.URL = u
hl.SetURL(u)
return nil
}

Expand All @@ -213,8 +220,12 @@ func (hl *Hyperlink) Tapped(e *fyne.PointEvent) {
}

func (hl *Hyperlink) invokeAction() {
if hl.OnTapped != nil {
hl.OnTapped()
hl.propertyLock.RLock()
onTapped := hl.OnTapped
hl.propertyLock.RUnlock()

if onTapped != nil {
onTapped()
return
}
hl.openURL()
Expand All @@ -232,15 +243,22 @@ func (hl *Hyperlink) TypedKey(ev *fyne.KeyEvent) {
}

func (hl *Hyperlink) openURL() {
if hl.URL != nil {
err := fyne.CurrentApp().OpenURL(hl.URL)
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() {
hl.propertyLock.RLock()
defer hl.propertyLock.RUnlock()

hl.provider.Wrapping = hl.Wrapping
hl.provider.Truncation = hl.Truncation
hl.provider.Segments = []RichTextSegment{&TextSegment{
Expand Down

0 comments on commit 77e922d

Please sign in to comment.