Fix #142: disable hyperlinks for track artists that are not album artists

This commit is contained in:
Drew Weymouth
2023-04-24 19:13:41 -07:00
parent 25ca42a31e
commit 02e51e5aa2
2 changed files with 44 additions and 11 deletions
+43 -11
View File
@@ -4,14 +4,15 @@ import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type hyperlinkWrapper struct {
widget.Hyperlink
l *widget.Label
MaxWidth float32
textWidthCached float32
MaxWidth float32
}
func newHyperlinkWrapper() *hyperlinkWrapper {
@@ -20,20 +21,24 @@ func newHyperlinkWrapper() *hyperlinkWrapper {
Text: "",
Wrapping: fyne.TextTruncate,
},
l: widget.NewLabel(""),
textWidthCached: -1,
}
h.ExtendBaseWidget(h)
return h
}
func (h *hyperlinkWrapper) MinSize() fyne.Size {
w := fyne.Min(h.MaxWidth, h.l.MinSize().Width)
return fyne.NewSize(w, h.Hyperlink.MinSize().Height)
if h.textWidthCached < 0 {
s := fyne.MeasureText(h.Text, theme.TextSize(), h.TextStyle)
// the 2.7 factor is a bit of a magic number but it works ¯\_(ツ)_/¯
h.textWidthCached = s.Width + theme.Padding()*2.7
}
return fyne.NewSize(fyne.Min(h.MaxWidth, h.textWidthCached), h.Hyperlink.MinSize().Height)
}
func (h *hyperlinkWrapper) SetText(text string) {
h.Text = text
h.l.SetText(text)
h.textWidthCached = -1
}
func (h *hyperlinkWrapper) TypedKey(e *fyne.KeyEvent) {
@@ -47,17 +52,22 @@ func (h *hyperlinkWrapper) TypedKey(e *fyne.KeyEvent) {
type CustomHyperlink struct {
widget.BaseWidget
h *hyperlinkWrapper
l *widget.Label
OnTapped func()
NoTruncate bool
Disabled bool
container *fyne.Container
minSize fyne.Size
lastDisabled bool
container *fyne.Container
minSize fyne.Size
}
func NewCustomHyperlink() *CustomHyperlink {
c := &CustomHyperlink{
h: newHyperlinkWrapper(),
h: newHyperlinkWrapper(),
l: widget.NewLabel(""),
container: container.NewMax(),
}
c.h.OnTapped = func() {
if c.OnTapped != nil {
@@ -66,18 +76,22 @@ func NewCustomHyperlink() *CustomHyperlink {
}
c.ExtendBaseWidget(c)
c.minSize = c.h.MinSize()
c.container = container.NewHBox(c.h, layout.NewSpacer())
c.updateContainer(c.Disabled)
return c
}
func (c *CustomHyperlink) SetText(text string) {
s := widget.NewLabel(text).MinSize()
c.l.Text = text
lastWrapping := c.l.Wrapping
c.l.Wrapping = fyne.TextWrapOff
s := c.l.MinSize()
c.h.SetText(text)
if c.NoTruncate {
c.minSize = s
} else {
c.minSize = fyne.NewSize(fyne.Min(c.Size().Width, s.Width), s.Height)
}
c.l.Wrapping = lastWrapping
c.Refresh()
}
@@ -87,6 +101,15 @@ func (c *CustomHyperlink) Resize(size fyne.Size) {
}
func (c *CustomHyperlink) Refresh() {
if c.NoTruncate {
c.l.Wrapping = fyne.TextWrapOff
} else {
c.l.Wrapping = fyne.TextTruncate
}
if c.lastDisabled != c.Disabled {
c.updateContainer(c.Disabled)
c.lastDisabled = c.Disabled
}
c.container.Refresh()
}
@@ -97,3 +120,12 @@ func (c *CustomHyperlink) MinSize() fyne.Size {
func (c *CustomHyperlink) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(c.container)
}
func (c *CustomHyperlink) updateContainer(linkDisabled bool) {
c.container.RemoveAll()
if linkDisabled {
c.container.Add(c.l)
} else {
c.container.Add(container.NewHBox(c.h, layout.NewSpacer()))
}
}
+1
View File
@@ -477,6 +477,7 @@ func (t *TrackRow) Update(tr *subsonic.Child, rowNum int) {
t.name.Segments[0].(*widget.TextSegment).Text = tr.Title
t.artist.SetText(tr.Artist)
t.artist.Disabled = tr.ArtistID == ""
t.album.SetText(tr.Album)
t.dur.Segments[0].(*widget.TextSegment).Text = util.SecondsToTimeString(float64(tr.Duration))
t.year.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(tr.Year)