Skip to content

Commit

Permalink
Inline creation of text provider in hyperlink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Feb 6, 2024
1 parent e8d3f88 commit 30ce1f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 36 deletions.
36 changes: 6 additions & 30 deletions widget/hyperlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Hyperlink struct {

textSize fyne.Size // updated in syncSegments
focused, hovered bool
provider *RichText
provider RichText
}

// NewHyperlink creates a new hyperlink widget with the set text content
Expand All @@ -58,10 +58,7 @@ 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.provider.ExtendBaseWidget(&hl.provider)
hl.syncSegments()

th := hl.Theme()
Expand All @@ -72,7 +69,7 @@ func (hl *Hyperlink) CreateRenderer() fyne.WidgetRenderer {
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}
return &hyperlinkRenderer{hl: hl, objects: []fyne.CanvasObject{&hl.provider, focus, under}, focus: focus, under: under}
}

// Cursor returns the cursor type of this widget
Expand Down Expand Up @@ -139,11 +136,7 @@ func (hl *Hyperlink) focusXPos() float32 {
func (hl *Hyperlink) isPosOverText(pos fyne.Position) bool {
innerPad := theme.InnerPadding()
pad := theme.Padding()
// If not rendered yet provider will be nil
lineCount := float32(1)
if hl.provider != nil {
lineCount = fyne.Max(lineCount, float32(len(hl.provider.rowBounds)))
}
lineCount := fyne.Max(1, float32(len(hl.provider.rowBounds)))

xpos := hl.focusXPos()
return pos.X >= xpos && pos.X <= xpos+hl.focusWidth() &&
Expand All @@ -154,31 +147,21 @@ func (hl *Hyperlink) isPosOverText(pos fyne.Position) bool {
//
// Implements: fyne.Widget
func (hl *Hyperlink) Refresh() {
if hl.provider == nil { // not created until visible
return
}
hl.syncSegments()

hl.provider.Refresh()
hl.BaseWidget.Refresh()
}

// MinSize returns the smallest size this widget can shrink to
func (hl *Hyperlink) MinSize() fyne.Size {
if hl.provider == nil {
hl.CreateRenderer()
}

hl.syncSegments()
return hl.provider.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 hl.provider == nil { // not created until visible
return
}
hl.provider.Resize(size)
}

Expand All @@ -187,9 +170,7 @@ func (hl *Hyperlink) SetText(text string) {
hl.propertyLock.Lock()
hl.Text = text
hl.propertyLock.Unlock()
if hl.provider == nil { // not created until visible
return
}

hl.syncSegments()
hl.provider.Refresh()
}
Expand All @@ -214,11 +195,6 @@ 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(e *fyne.PointEvent) {
// If not rendered yet (hl.provider == nil), register all taps
// in practice this probably only happens in our unit tests
if hl.provider != nil && !hl.isPosOverText(e.Position) {
return
}
hl.invokeAction()
}

Expand Down
10 changes: 5 additions & 5 deletions widget/hyperlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestHyperlink_Alignment(t *testing.T) {
hyperlink := &Hyperlink{Text: "Test", Alignment: fyne.TextAlignTrailing}
hyperlink.CreateRenderer()

assert.Equal(t, fyne.TextAlignTrailing, richTextRenderTexts(hyperlink.provider)[0].Alignment)
assert.Equal(t, fyne.TextAlignTrailing, richTextRenderTexts(&hyperlink.provider)[0].Alignment)
}

func TestHyperlink_Hide(t *testing.T) {
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestHyperlink_SetText(t *testing.T) {
hyperlink.SetText("New")

assert.Equal(t, "New", hyperlink.Text)
assert.Equal(t, "New", richTextRenderTexts(hyperlink.provider)[0].Text)
assert.Equal(t, "New", richTextRenderTexts(&hyperlink.provider)[0].Text)
}

func TestHyperlink_SetUrl(t *testing.T) {
Expand Down Expand Up @@ -183,17 +183,17 @@ func TestHyperlink_Truncate(t *testing.T) {
hyperlink.CreateRenderer()
hyperlink.Resize(fyne.NewSize(100, 20))

texts := richTextRenderTexts(hyperlink.provider)
texts := richTextRenderTexts(&hyperlink.provider)
assert.Equal(t, "TestingWithLongText", texts[0].Text)

hyperlink.Truncation = fyne.TextTruncateClip
hyperlink.Refresh()
texts = richTextRenderTexts(hyperlink.provider)
texts = richTextRenderTexts(&hyperlink.provider)
assert.Equal(t, "TestingWith", texts[0].Text)

hyperlink.Truncation = fyne.TextTruncateEllipsis
hyperlink.Refresh()
texts = richTextRenderTexts(hyperlink.provider)
texts = richTextRenderTexts(&hyperlink.provider)
assert.Equal(t, "TestingWi…", texts[0].Text)
}

Expand Down
2 changes: 1 addition & 1 deletion widget/richtext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ func TestTextRenderer_ApplyTheme(t *testing.T) {
func TestTextProvider_LineSizeToColumn(t *testing.T) {
label := NewLabel("Test")
label.CreateRenderer() // TODO make this a simple refresh call once it's in
provider := label.provider

provider := &label.provider
inPad := theme.InnerPadding()
textSize := theme.TextSize()
fullSize := provider.lineSizeToColumn(4, 0, textSize, inPad)
Expand Down

0 comments on commit 30ce1f8

Please sign in to comment.