2020-06-06 07:44:36 +00:00
|
|
|
package rich
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
import "log"
|
2020-08-17 00:13:47 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// ImageURLSetter describes an image that can be set a URL.
|
|
|
|
type ImageURLSetter interface {
|
2020-12-20 09:48:52 +00:00
|
|
|
SetImageURL(url string)
|
2020-08-17 00:13:47 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
type tooltipSetter interface {
|
|
|
|
SetTooltipText(text string)
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// BindRoundImage binds a round image to a rich label state store.
|
|
|
|
func BindRoundImage(img ImageURLSetter, state LabelStateStorer, tooltip bool) {
|
|
|
|
var setTooltip func(string)
|
2020-06-06 07:44:36 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
if tooltip {
|
|
|
|
tooltipper, ok := img.(tooltipSetter)
|
|
|
|
if !ok {
|
|
|
|
log.Panicf("img of type %T is not tooltipSetter", img)
|
|
|
|
}
|
2020-06-06 07:44:36 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
setTooltip = tooltipper.SetTooltipText
|
2020-12-20 08:18:23 +00:00
|
|
|
}
|
2020-06-17 07:06:34 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
state.OnUpdate(func() {
|
|
|
|
image := state.Image()
|
|
|
|
img.SetImageURL(image.URL)
|
2020-08-29 01:42:28 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
if setTooltip != nil {
|
|
|
|
setTooltip(state.Label().String())
|
2020-12-20 09:48:52 +00:00
|
|
|
}
|
2020-06-17 07:06:34 +00:00
|
|
|
})
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|