Fix #142: disable hyperlinks for track artists that are not album artists
This commit is contained in:
@@ -4,14 +4,15 @@ import (
|
|||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
"fyne.io/fyne/v2/container"
|
"fyne.io/fyne/v2/container"
|
||||||
"fyne.io/fyne/v2/layout"
|
"fyne.io/fyne/v2/layout"
|
||||||
|
"fyne.io/fyne/v2/theme"
|
||||||
"fyne.io/fyne/v2/widget"
|
"fyne.io/fyne/v2/widget"
|
||||||
)
|
)
|
||||||
|
|
||||||
type hyperlinkWrapper struct {
|
type hyperlinkWrapper struct {
|
||||||
widget.Hyperlink
|
widget.Hyperlink
|
||||||
|
|
||||||
l *widget.Label
|
textWidthCached float32
|
||||||
MaxWidth float32
|
MaxWidth float32
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHyperlinkWrapper() *hyperlinkWrapper {
|
func newHyperlinkWrapper() *hyperlinkWrapper {
|
||||||
@@ -20,20 +21,24 @@ func newHyperlinkWrapper() *hyperlinkWrapper {
|
|||||||
Text: "",
|
Text: "",
|
||||||
Wrapping: fyne.TextTruncate,
|
Wrapping: fyne.TextTruncate,
|
||||||
},
|
},
|
||||||
l: widget.NewLabel(""),
|
textWidthCached: -1,
|
||||||
}
|
}
|
||||||
h.ExtendBaseWidget(h)
|
h.ExtendBaseWidget(h)
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hyperlinkWrapper) MinSize() fyne.Size {
|
func (h *hyperlinkWrapper) MinSize() fyne.Size {
|
||||||
w := fyne.Min(h.MaxWidth, h.l.MinSize().Width)
|
if h.textWidthCached < 0 {
|
||||||
return fyne.NewSize(w, h.Hyperlink.MinSize().Height)
|
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) {
|
func (h *hyperlinkWrapper) SetText(text string) {
|
||||||
h.Text = text
|
h.Text = text
|
||||||
h.l.SetText(text)
|
h.textWidthCached = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hyperlinkWrapper) TypedKey(e *fyne.KeyEvent) {
|
func (h *hyperlinkWrapper) TypedKey(e *fyne.KeyEvent) {
|
||||||
@@ -47,17 +52,22 @@ func (h *hyperlinkWrapper) TypedKey(e *fyne.KeyEvent) {
|
|||||||
type CustomHyperlink struct {
|
type CustomHyperlink struct {
|
||||||
widget.BaseWidget
|
widget.BaseWidget
|
||||||
h *hyperlinkWrapper
|
h *hyperlinkWrapper
|
||||||
|
l *widget.Label
|
||||||
|
|
||||||
OnTapped func()
|
OnTapped func()
|
||||||
NoTruncate bool
|
NoTruncate bool
|
||||||
|
Disabled bool
|
||||||
|
|
||||||
container *fyne.Container
|
lastDisabled bool
|
||||||
minSize fyne.Size
|
container *fyne.Container
|
||||||
|
minSize fyne.Size
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCustomHyperlink() *CustomHyperlink {
|
func NewCustomHyperlink() *CustomHyperlink {
|
||||||
c := &CustomHyperlink{
|
c := &CustomHyperlink{
|
||||||
h: newHyperlinkWrapper(),
|
h: newHyperlinkWrapper(),
|
||||||
|
l: widget.NewLabel(""),
|
||||||
|
container: container.NewMax(),
|
||||||
}
|
}
|
||||||
c.h.OnTapped = func() {
|
c.h.OnTapped = func() {
|
||||||
if c.OnTapped != nil {
|
if c.OnTapped != nil {
|
||||||
@@ -66,18 +76,22 @@ func NewCustomHyperlink() *CustomHyperlink {
|
|||||||
}
|
}
|
||||||
c.ExtendBaseWidget(c)
|
c.ExtendBaseWidget(c)
|
||||||
c.minSize = c.h.MinSize()
|
c.minSize = c.h.MinSize()
|
||||||
c.container = container.NewHBox(c.h, layout.NewSpacer())
|
c.updateContainer(c.Disabled)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CustomHyperlink) SetText(text string) {
|
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)
|
c.h.SetText(text)
|
||||||
if c.NoTruncate {
|
if c.NoTruncate {
|
||||||
c.minSize = s
|
c.minSize = s
|
||||||
} else {
|
} else {
|
||||||
c.minSize = fyne.NewSize(fyne.Min(c.Size().Width, s.Width), s.Height)
|
c.minSize = fyne.NewSize(fyne.Min(c.Size().Width, s.Width), s.Height)
|
||||||
}
|
}
|
||||||
|
c.l.Wrapping = lastWrapping
|
||||||
c.Refresh()
|
c.Refresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,6 +101,15 @@ func (c *CustomHyperlink) Resize(size fyne.Size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CustomHyperlink) Refresh() {
|
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()
|
c.container.Refresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,3 +120,12 @@ func (c *CustomHyperlink) MinSize() fyne.Size {
|
|||||||
func (c *CustomHyperlink) CreateRenderer() fyne.WidgetRenderer {
|
func (c *CustomHyperlink) CreateRenderer() fyne.WidgetRenderer {
|
||||||
return widget.NewSimpleRenderer(c.container)
|
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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -477,6 +477,7 @@ func (t *TrackRow) Update(tr *subsonic.Child, rowNum int) {
|
|||||||
|
|
||||||
t.name.Segments[0].(*widget.TextSegment).Text = tr.Title
|
t.name.Segments[0].(*widget.TextSegment).Text = tr.Title
|
||||||
t.artist.SetText(tr.Artist)
|
t.artist.SetText(tr.Artist)
|
||||||
|
t.artist.Disabled = tr.ArtistID == ""
|
||||||
t.album.SetText(tr.Album)
|
t.album.SetText(tr.Album)
|
||||||
t.dur.Segments[0].(*widget.TextSegment).Text = util.SecondsToTimeString(float64(tr.Duration))
|
t.dur.Segments[0].(*widget.TextSegment).Text = util.SecondsToTimeString(float64(tr.Duration))
|
||||||
t.year.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(tr.Year)
|
t.year.Segments[0].(*widget.TextSegment).Text = strconv.Itoa(tr.Year)
|
||||||
|
|||||||
Reference in New Issue
Block a user