Skip to content

Commit

Permalink
#283: Prevent artist bio from overflowing header space
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Nov 22, 2023
1 parent 2946fa5 commit 6445320
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
18 changes: 6 additions & 12 deletions ui/browsing/artistpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package browsing
import (
"log"
"strconv"
"strings"

"github.com/dweymouth/supersonic/backend"
"github.com/dweymouth/supersonic/backend/mediaprovider"
Expand Down Expand Up @@ -280,7 +279,7 @@ type ArtistPageHeader struct {
artistPage *ArtistPage
artistImage *widgets.ImagePlaceholder
titleDisp *widget.RichText
biographyDisp *widget.RichText
biographyDisp *widgets.MaxRowsLabel
similarArtists *fyne.Container
favoriteBtn *widgets.FavoriteButton
playBtn *widget.Button
Expand All @@ -294,7 +293,7 @@ func NewArtistPageHeader(page *ArtistPage) *ArtistPageHeader {
a := &ArtistPageHeader{
artistPage: page,
titleDisp: widget.NewRichTextWithText(""),
biographyDisp: widget.NewRichTextWithText(artistBioNotAvailableStr),
biographyDisp: widgets.NewMaxRowsLabel(5, artistBioNotAvailableStr),
similarArtists: container.NewHBox(),
}
a.titleDisp.Segments[0].(*widget.TextSegment).Style = widget.RichTextStyle{
Expand All @@ -312,6 +311,7 @@ func NewArtistPageHeader(page *ArtistPage) *ArtistPageHeader {
})
a.playRadioBtn = widget.NewButtonWithIcon(" Play Artist Radio", myTheme.ShuffleIcon, a.artistPage.playArtistRadio)
a.biographyDisp.Wrapping = fyne.TextWrapWord
a.biographyDisp.Truncation = fyne.TextTruncateEllipsis
a.ExtendBaseWidget(a)
a.createContainer()
return a
Expand All @@ -321,7 +321,7 @@ func (a *ArtistPageHeader) Clear() {
a.artistID = ""
a.favoriteBtn.IsFavorited = false
a.titleDisp.Segments[0].(*widget.TextSegment).Text = ""
a.biographyDisp.Segments[0].(*widget.TextSegment).Text = artistBioNotAvailableStr
a.biographyDisp.Text = artistBioNotAvailableStr
for _, obj := range a.similarArtists.Objects {
obj.Hide()
}
Expand Down Expand Up @@ -352,14 +352,8 @@ func (a *ArtistPageHeader) UpdateInfo(info *mediaprovider.ArtistInfo) {
return
}

if info.Biography != "" {
segs := util.RichTextSegsFromHTMLString(info.Biography)
if len(segs) > 0 {
if ts, ok := segs[0].(*widget.TextSegment); ok && strings.TrimSpace(ts.Text) != "" {
a.biographyDisp.Segments = segs
a.biographyDisp.Refresh()
}
}
if text := util.PlaintextFromHTMLString(info.Biography); text != "" {
a.biographyDisp.SetText(text)
}

if len(a.similarArtists.Objects) == 0 {
Expand Down
9 changes: 4 additions & 5 deletions ui/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func NewDebouncer(dur time.Duration, callOnDone func()) func() {
}
}

func RichTextSegsFromHTMLString(s string) []widget.RichTextSegment {
func PlaintextFromHTMLString(s string) string {
tokr := html.NewTokenizer(strings.NewReader(s))
var segs []widget.RichTextSegment

var text string
var isLink bool
var done bool
for !done {
Expand All @@ -102,12 +102,11 @@ func RichTextSegsFromHTMLString(s string) []widget.RichTextSegment {
t := tokr.Token()
// for now, skip displaying Navidrome's "Read more on Last.FM" link
if !isLink {
segs = append(segs, &widget.TextSegment{Text: t.Data})
text = text + t.Data
}
}
}

return segs
return text
}

func NewRatingSubmenu(onSetRating func(int)) *fyne.MenuItem {
Expand Down
36 changes: 36 additions & 0 deletions ui/widgets/maxrowslabel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package widgets

import (
"strings"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
)

type MaxRowsLabel struct {
widget.Label

maxHeight float32
}

func NewMaxRowsLabel(maxRows int, text string) *MaxRowsLabel {
if maxRows < 1 {
maxRows = 1
}
m := &MaxRowsLabel{
Label: widget.Label{
Text: text,
},
}
m.ExtendBaseWidget(m)

maxHeightText := strings.Repeat("W\n", maxRows)
maxHeightText = maxHeightText[:len(maxHeightText)-1]
m.maxHeight = widget.NewLabel(maxHeightText).MinSize().Height

return m
}

func (m *MaxRowsLabel) MinSize() fyne.Size {
return fyne.NewSize(m.Label.MinSize().Width, m.maxHeight)
}

0 comments on commit 6445320

Please sign in to comment.