2020-06-17 07:06:34 +00:00
|
|
|
package button
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts"
|
2020-07-17 07:26:55 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich"
|
2020-06-17 07:06:34 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
|
|
|
)
|
|
|
|
|
2020-07-19 01:57:57 +00:00
|
|
|
const UnreadColorDefs = `
|
|
|
|
@define-color mentioned rgb(240, 71, 71);
|
|
|
|
`
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
type ToggleButtonImage struct {
|
2020-08-29 01:42:28 +00:00
|
|
|
*rich.ToggleButtonImage
|
2020-06-17 07:06:34 +00:00
|
|
|
|
|
|
|
clicked func(bool)
|
2020-07-17 07:26:55 +00:00
|
|
|
readcss primitives.ClassEnum
|
2020-06-17 07:06:34 +00:00
|
|
|
|
|
|
|
err error
|
2020-07-11 06:48:44 +00:00
|
|
|
icon string // whether or not the button has an icon
|
2020-06-17 07:06:34 +00:00
|
|
|
iconSz int
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ cchat.IconContainer = (*ToggleButtonImage)(nil)
|
|
|
|
|
2020-07-19 01:57:57 +00:00
|
|
|
var serverButtonCSS = primitives.PrepareClassCSS("server-button", `
|
2020-07-18 07:16:47 +00:00
|
|
|
.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)
|
2020-07-17 07:26:55 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func NewToggleButtonImage(content text.Rich) *ToggleButtonImage {
|
|
|
|
b := rich.NewToggleButtonImage(content)
|
2020-08-29 01:42:28 +00:00
|
|
|
return WrapToggleButtonImage(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WrapToggleButtonImage(b *rich.ToggleButtonImage) *ToggleButtonImage {
|
2020-06-17 07:06:34 +00:00
|
|
|
b.Show()
|
|
|
|
|
|
|
|
tb := &ToggleButtonImage{
|
2020-08-29 01:42:28 +00:00
|
|
|
ToggleButtonImage: b,
|
2020-06-17 07:06:34 +00:00
|
|
|
|
|
|
|
clicked: func(bool) {},
|
|
|
|
}
|
2020-07-17 07:26:55 +00:00
|
|
|
tb.Connect("clicked", func() { tb.clicked(tb.GetActive()) })
|
2020-07-19 01:57:57 +00:00
|
|
|
serverButtonCSS(tb)
|
2020-06-17 07:06:34 +00:00
|
|
|
|
|
|
|
return tb
|
|
|
|
}
|
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
func (b *ToggleButtonImage) SetSelected(selected bool) {
|
|
|
|
// Set the clickability the opposite as the boolean.
|
2020-08-29 01:42:28 +00:00
|
|
|
// b.SetSensitive(!selected)
|
|
|
|
b.SetInconsistent(selected)
|
2020-07-18 07:16:47 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
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-06-17 07:06:34 +00:00
|
|
|
|
2020-07-11 06:48:44 +00:00
|
|
|
if b.icon != "" {
|
|
|
|
b.Image.SetPlaceholderIcon(b.icon, b.Image.Size())
|
2020-06-17 07:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ToggleButtonImage) SetLoading() {
|
|
|
|
b.SetLabelUnsafe(b.GetLabel())
|
|
|
|
|
2020-07-11 06:48:44 +00:00
|
|
|
if b.icon != "" {
|
2020-06-17 07:06:34 +00:00
|
|
|
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 != "" {
|
2020-06-17 07:06:34 +00:00
|
|
|
b.Image.SetPlaceholderIcon("computer-fail-symbolic", b.Image.Size())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 07:26:55 +00:00
|
|
|
func (b *ToggleButtonImage) SetUnreadUnsafe(unread, mentioned bool) {
|
|
|
|
switch {
|
2020-07-18 07:16:47 +00:00
|
|
|
// Prioritize mentions over unreads.
|
2020-07-17 07:26:55 +00:00
|
|
|
case mentioned:
|
|
|
|
b.readcss.SetClass(b, "mentioned")
|
2020-07-18 07:16:47 +00:00
|
|
|
case unread:
|
|
|
|
b.readcss.SetClass(b, "unread")
|
2020-07-17 07:26:55 +00:00
|
|
|
default:
|
|
|
|
b.readcss.SetClass(b, "read")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func (b *ToggleButtonImage) SetPlaceholderIcon(iconName string, iconSzPx int) {
|
2020-07-11 06:48:44 +00:00
|
|
|
b.icon = iconName
|
2020-06-17 07:06:34 +00:00
|
|
|
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 = ""
|
2020-06-17 07:06:34 +00:00
|
|
|
b.Image.SetIconUnsafe(url)
|
|
|
|
}
|