1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-17 03:32:56 +00:00
cchat-gtk/internal/ui/primitives/roundimage/button.go

60 lines
1.2 KiB
Go

package roundimage
import (
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
"github.com/gotk3/gotk3/gtk"
)
// TODO: move roundimage.Button to rich.ImageButton.
// Button implements a rounded button with a rounded image. This widget only
// supports a full circle for rounding.
type Button struct {
*gtk.Button
Image Imager
}
var roundButtonCSS = primitives.PrepareClassCSS("round-button", `
.round-button {
padding: 0;
border-radius: 50%;
}
`)
func NewButton() (*Button, error) {
image := NewImage(0)
image.Show()
b := NewEmptyButton()
b.SetImage(image)
return b, nil
}
func NewEmptyButton() *Button {
b, _ := gtk.ButtonNew()
b.SetRelief(gtk.RELIEF_NONE)
roundButtonCSS(b)
return &Button{Button: b}
}
// NewCustomButton creates a new rounded button with the given Imager. If the
// given Imager implements the Connector interface (aka *StillImage), then the
// function will implicitly connect its handlers to the button.
func NewCustomButton(img Imager) (*Button, error) {
b := NewEmptyButton()
b.SetImage(img)
if connector, ok := img.(Connector); ok {
connector.ConnectHandlers(b)
}
return b, nil
}
func (b *Button) SetImage(img Imager) {
b.Image = img
b.Button.SetImage(img)
}