-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from dweymouth/feature/artist-top-tracks
Add Top Tracks view to Artist page
- Loading branch information
Showing
4 changed files
with
207 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package widgets | ||
|
||
import ( | ||
"fyne.io/fyne/v2" | ||
"fyne.io/fyne/v2/container" | ||
"fyne.io/fyne/v2/widget" | ||
) | ||
|
||
type ToggleText struct { | ||
widget.BaseWidget | ||
|
||
OnChanged func(int) | ||
|
||
labels []string | ||
activeLabelIdx int | ||
container *fyne.Container | ||
} | ||
|
||
func NewToggleText(activeLblIdx int, labels []string) *ToggleText { | ||
t := &ToggleText{labels: labels, activeLabelIdx: activeLblIdx} | ||
t.ExtendBaseWidget(t) | ||
t.container = container.NewHBox() | ||
for i, lbl := range labels { | ||
if i == activeLblIdx { | ||
t.container.Add(t.newBoldRichText(lbl)) | ||
} else { | ||
hl := widget.NewHyperlink(lbl, nil) | ||
hl.OnTapped = t.buildOnTapped(i) | ||
t.container.Add(hl) | ||
} | ||
} | ||
return t | ||
} | ||
|
||
func (t *ToggleText) SetActivatedLabel(idx int) { | ||
changed := t.activeLabelIdx != idx | ||
// update old label to hyperlink | ||
hl := widget.NewHyperlink(t.labels[t.activeLabelIdx], nil) | ||
hl.OnTapped = t.buildOnTapped(t.activeLabelIdx) | ||
t.container.Objects[t.activeLabelIdx] = hl | ||
// update activated label to bold text | ||
t.container.Objects[idx] = t.newBoldRichText(t.labels[idx]) | ||
t.activeLabelIdx = idx | ||
if changed { | ||
t.Refresh() | ||
} | ||
} | ||
|
||
func (t *ToggleText) buildOnTapped(i int) func() { | ||
return func() { | ||
t.onActivated(i) | ||
} | ||
} | ||
|
||
func (t *ToggleText) newBoldRichText(text string) *widget.RichText { | ||
return widget.NewRichText(&widget.TextSegment{ | ||
Text: text, | ||
Style: widget.RichTextStyle{TextStyle: fyne.TextStyle{Bold: true}}, | ||
}) | ||
} | ||
|
||
func (t *ToggleText) onActivated(idx int) { | ||
changed := t.activeLabelIdx != idx | ||
t.SetActivatedLabel(idx) | ||
if changed && t.OnChanged != nil { | ||
t.OnChanged(t.activeLabelIdx) | ||
} | ||
} | ||
|
||
func (t *ToggleText) CreateRenderer() fyne.WidgetRenderer { | ||
return widget.NewSimpleRenderer(t.container) | ||
} |