2020-08-29 01:42:28 +00:00
|
|
|
package roundimage
|
|
|
|
|
|
|
|
import (
|
2020-12-20 09:48:52 +00:00
|
|
|
"context"
|
|
|
|
|
2020-08-29 01:42:28 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts/httputil"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
2020-12-30 06:30:41 +00:00
|
|
|
"github.com/gotk3/gotk3/cairo"
|
2020-08-29 01:42:28 +00:00
|
|
|
"github.com/gotk3/gotk3/gdk"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StaticImage is an image that only plays a GIF if it's hovered on top of.
|
|
|
|
type StaticImage struct {
|
|
|
|
*Image
|
2020-12-20 09:48:52 +00:00
|
|
|
animating bool
|
2020-08-29 01:42:28 +00:00
|
|
|
animation *gdk.PixbufAnimation
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ Imager = (*StaticImage)(nil)
|
|
|
|
_ Connector = (*StaticImage)(nil)
|
|
|
|
_ httputil.ImageContainer = (*StaticImage)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewStaticImage(parent primitives.Connector, radius float64) (*StaticImage, error) {
|
|
|
|
i, err := NewImage(radius)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-20 09:48:52 +00:00
|
|
|
var s = &StaticImage{i, false, nil}
|
2020-08-29 01:42:28 +00:00
|
|
|
if parent != nil {
|
|
|
|
s.ConnectHandlers(parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StaticImage) ConnectHandlers(connector primitives.Connector) {
|
2020-12-30 06:30:41 +00:00
|
|
|
connector.Connect("enter-notify-event", func(interface{}) {
|
2020-12-20 09:48:52 +00:00
|
|
|
if s.animation != nil && !s.animating {
|
|
|
|
s.animating = true
|
2020-08-29 01:42:28 +00:00
|
|
|
s.Image.SetFromAnimation(s.animation)
|
|
|
|
}
|
|
|
|
})
|
2020-12-30 06:30:41 +00:00
|
|
|
connector.Connect("leave-notify-event", func(interface{}) {
|
2020-12-20 09:48:52 +00:00
|
|
|
if s.animation != nil && s.animating {
|
|
|
|
s.animating = false
|
2020-08-29 01:42:28 +00:00
|
|
|
s.Image.SetFromPixbuf(s.animation.GetStaticImage())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-20 09:48:52 +00:00
|
|
|
func (s *StaticImage) SetImageURL(url string) {
|
|
|
|
// No dynamic sizing support; yolo.
|
|
|
|
httputil.AsyncImage(context.Background(), s, url, s.Image.procs...)
|
|
|
|
}
|
|
|
|
|
2020-08-29 01:42:28 +00:00
|
|
|
func (s *StaticImage) SetFromPixbuf(pb *gdk.Pixbuf) {
|
|
|
|
s.animation = nil
|
|
|
|
s.Image.SetFromPixbuf(pb)
|
|
|
|
}
|
|
|
|
|
2020-12-30 06:30:41 +00:00
|
|
|
func (s *StaticImage) SetFromSurface(sf *cairo.Surface) {
|
|
|
|
s.animation = nil
|
|
|
|
s.Image.SetFromSurface(sf)
|
|
|
|
}
|
|
|
|
|
2020-08-29 01:42:28 +00:00
|
|
|
func (s *StaticImage) SetFromAnimation(anim *gdk.PixbufAnimation) {
|
|
|
|
s.animation = anim
|
|
|
|
s.Image.SetFromPixbuf(anim.GetStaticImage())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StaticImage) GetAnimation() *gdk.PixbufAnimation {
|
|
|
|
return s.animation
|
|
|
|
}
|