1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-01 03:54:16 +00:00
cchat-gtk/internal/ui/service/session/server/button/button.go

153 lines
3.4 KiB
Go
Raw Normal View History

package button
import (
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/gts"
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
"github.com/diamondburned/cchat-gtk/internal/ui/rich"
"github.com/diamondburned/cchat/text"
)
2020-07-19 01:57:57 +00:00
const UnreadColorDefs = `
@define-color mentioned rgb(240, 71, 71);
`
type ToggleButtonImage struct {
*rich.ToggleButtonImage
clicked func(bool)
readcss primitives.ClassEnum
err error
2020-07-11 06:48:44 +00:00
icon string // whether or not the button has an icon
iconSz int
}
var _ cchat.IconContainer = (*ToggleButtonImage)(nil)
2020-07-19 01:57:57 +00:00
var serverButtonCSS = primitives.PrepareClassCSS("server-button", `
.selected-server {
border-left: 2px solid mix(@theme_base_color, @theme_fg_color, 0.1);
background-color: mix(@theme_base_color, @theme_fg_color, 0.1);
color: @theme_fg_color;
}
.read {
color: alpha(@theme_fg_color, 0.5);
border-left: 2px solid transparent;
}
.unread {
color: @theme_fg_color;
border-left: 2px solid alpha(@theme_fg_color, 0.75);
background-color: alpha(@theme_fg_color, 0.05);
}
.mentioned {
color: @mentioned;
border-left: 2px solid alpha(@mentioned, 0.75);
background-color: alpha(@mentioned, 0.05);
}
2020-07-19 01:57:57 +00:00
`+UnreadColorDefs)
func NewToggleButtonImage(content text.Rich) *ToggleButtonImage {
b := rich.NewToggleButtonImage(content)
return WrapToggleButtonImage(b)
}
func WrapToggleButtonImage(b *rich.ToggleButtonImage) *ToggleButtonImage {
b.Show()
tb := &ToggleButtonImage{
ToggleButtonImage: b,
clicked: func(bool) {},
}
tb.Connect("clicked", func() { tb.clicked(tb.GetActive()) })
2020-07-19 01:57:57 +00:00
serverButtonCSS(tb)
return tb
}
func (b *ToggleButtonImage) SetSelected(selected bool) {
// Set the clickability the opposite as the boolean.
// b.SetSensitive(!selected)
b.SetInconsistent(selected)
if selected {
primitives.AddClass(b, "selected-server")
} else {
primitives.RemoveClass(b, "selected-server")
}
// Some special edge case that I forgot.
if !selected {
b.SetActive(false)
}
}
func (b *ToggleButtonImage) SetClicked(clicked func(bool)) {
b.clicked = clicked
}
func (b *ToggleButtonImage) SetClickedIfTrue(clickedIfTrue func()) {
b.clicked = func(clicked bool) {
if clicked {
clickedIfTrue()
}
}
}
func (b *ToggleButtonImage) SetNormal() {
b.SetLabelUnsafe(b.GetLabel())
2020-10-26 02:28:29 +00:00
// b.menu.SetItems(b.extraMenu)
2020-07-11 06:48:44 +00:00
if b.icon != "" {
b.Image.SetPlaceholderIcon(b.icon, b.Image.Size())
}
}
func (b *ToggleButtonImage) SetLoading() {
b.SetLabelUnsafe(b.GetLabel())
2020-07-11 06:48:44 +00:00
if b.icon != "" {
b.Image.SetPlaceholderIcon("content-loading-symbolic", b.Image.Size())
}
}
func (b *ToggleButtonImage) SetFailed(err error, retry func()) {
b.Label.SetMarkup(rich.MakeRed(b.GetLabel()))
// If we have an icon set, then we can use the failed icon.
2020-07-11 06:48:44 +00:00
if b.icon != "" {
b.Image.SetPlaceholderIcon("computer-fail-symbolic", b.Image.Size())
}
}
func (b *ToggleButtonImage) SetUnreadUnsafe(unread, mentioned bool) {
switch {
// Prioritize mentions over unreads.
case mentioned:
b.readcss.SetClass(b, "mentioned")
case unread:
b.readcss.SetClass(b, "unread")
default:
b.readcss.SetClass(b, "read")
}
}
func (b *ToggleButtonImage) SetPlaceholderIcon(iconName string, iconSzPx int) {
2020-07-11 06:48:44 +00:00
b.icon = iconName
b.Image.SetPlaceholderIcon(iconName, iconSzPx)
}
func (b *ToggleButtonImage) SetIcon(url string) {
gts.ExecAsync(func() { b.SetIconUnsafe(url) })
}
func (b *ToggleButtonImage) SetIconUnsafe(url string) {
2020-07-11 06:48:44 +00:00
b.icon = ""
b.Image.SetIconUnsafe(url)
}