From 30ce1f8dda8ba0069c1a724461749116010dc747 Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 1 Feb 2024 12:12:43 +0100 Subject: [PATCH] Inline creation of text provider in hyperlink --- widget/hyperlink.go | 36 ++++++------------------------------ widget/hyperlink_test.go | 10 +++++----- widget/richtext_test.go | 2 +- 3 files changed, 12 insertions(+), 36 deletions(-) diff --git a/widget/hyperlink.go b/widget/hyperlink.go index 83e5f93138..2eb3b76335 100644 --- a/widget/hyperlink.go +++ b/widget/hyperlink.go @@ -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 @@ -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() @@ -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 @@ -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() && @@ -154,21 +147,14 @@ 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() } @@ -176,9 +162,6 @@ func (hl *Hyperlink) MinSize() fyne.Size { // 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) } @@ -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() } @@ -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() } diff --git a/widget/hyperlink_test.go b/widget/hyperlink_test.go index 3b7a528557..55f17f0fe7 100644 --- a/widget/hyperlink_test.go +++ b/widget/hyperlink_test.go @@ -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) { @@ -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) { @@ -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) } diff --git a/widget/richtext_test.go b/widget/richtext_test.go index eaff83bd0b..19ed6cfb12 100644 --- a/widget/richtext_test.go +++ b/widget/richtext_test.go @@ -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)