From 02e51e5aa26a8e0571ff941e9a4fca05c80d42b6 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Mon, 24 Apr 2023 19:13:41 -0700 Subject: [PATCH] Fix #142: disable hyperlinks for track artists that are not album artists --- ui/widgets/customhyperlink.go | 54 ++++++++++++++++++++++++++++------- ui/widgets/tracklist.go | 1 + 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/ui/widgets/customhyperlink.go b/ui/widgets/customhyperlink.go index a9045c1..0380871 100644 --- a/ui/widgets/customhyperlink.go +++ b/ui/widgets/customhyperlink.go @@ -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())) + } +} diff --git a/ui/widgets/tracklist.go b/ui/widgets/tracklist.go index faf74fb..bbc2621 100644 --- a/ui/widgets/tracklist.go +++ b/ui/widgets/tracklist.go @@ -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)