1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-16 11:12:44 +00:00
cchat-gtk/internal/ui/rich/rich.go

112 lines
2.3 KiB
Go
Raw Normal View History

2020-06-04 23:00:41 +00:00
package rich
import (
2020-06-07 04:27:28 +00:00
"html"
2020-06-04 23:00:41 +00:00
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/gts"
"github.com/diamondburned/cchat-gtk/internal/log"
2020-06-04 23:00:41 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
2020-06-06 00:47:28 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser"
2020-06-04 23:00:41 +00:00
"github.com/diamondburned/cchat/text"
"github.com/gotk3/gotk3/gtk"
"github.com/gotk3/gotk3/pango"
"github.com/pkg/errors"
2020-06-04 23:00:41 +00:00
)
func Small(text string) string {
return `<span size="small" color="#808080">` + text + "</span>"
}
2020-06-04 23:00:41 +00:00
2020-06-07 04:27:28 +00:00
func MakeRed(content text.Rich) string {
return `<span color="red">` + html.EscapeString(content.Content) + `</span>`
}
2020-06-04 23:00:41 +00:00
type Labeler interface {
2020-06-07 04:27:28 +00:00
// thread-safe
2020-06-04 23:00:41 +00:00
cchat.LabelContainer // thread-safe
2020-06-07 04:27:28 +00:00
// not thread-safe
SetLabelUnsafe(text.Rich)
GetLabel() text.Rich
2020-06-06 00:47:28 +00:00
GetText() string
2020-06-04 23:00:41 +00:00
}
type Label struct {
gtk.Label
current text.Rich
}
var (
_ gtk.IWidget = (*Label)(nil)
_ Labeler = (*Label)(nil)
)
func NewLabel(content text.Rich) *Label {
2020-06-06 00:47:28 +00:00
label, _ := gtk.LabelNew("")
label.SetMarkup(parser.RenderMarkup(content))
label.SetXAlign(0) // left align
label.SetEllipsize(pango.ELLIPSIZE_END)
2020-06-04 23:00:41 +00:00
return &Label{*label, content}
}
2020-06-07 07:06:13 +00:00
// Reset wipes the state to be just after construction.
func (l *Label) Reset() {
l.current = text.Rich{}
l.Label.SetText("")
}
func (l *Label) AsyncSetLabel(fn func(cchat.LabelContainer) error, info string) {
go func() {
if err := fn(l); err != nil {
log.Error(errors.Wrap(err, info))
}
}()
}
2020-06-04 23:00:41 +00:00
// SetLabel is thread-safe.
func (l *Label) SetLabel(content text.Rich) {
gts.ExecAsync(func() {
2020-06-06 00:47:28 +00:00
l.SetLabelUnsafe(content)
2020-06-04 23:00:41 +00:00
})
}
2020-06-06 00:47:28 +00:00
// SetLabelUnsafe sets the label in the current thread, meaning it's not
// thread-safe.
func (l *Label) SetLabelUnsafe(content text.Rich) {
l.current = content
l.SetMarkup(parser.RenderMarkup(content))
}
2020-06-04 23:00:41 +00:00
// GetLabel is NOT thread-safe.
func (l *Label) GetLabel() text.Rich {
return l.current
}
2020-06-06 00:47:28 +00:00
// GetText is NOT thread-safe.
func (l *Label) GetText() string {
return l.current.Content
}
2020-06-04 23:00:41 +00:00
type ToggleButton struct {
gtk.ToggleButton
Label
}
var (
_ gtk.IWidget = (*ToggleButton)(nil)
_ cchat.LabelContainer = (*ToggleButton)(nil)
)
func NewToggleButton(content text.Rich) *ToggleButton {
l := NewLabel(content)
l.Show()
b, _ := gtk.ToggleButtonNew()
primitives.BinLeftAlignLabel(b)
b.Add(l)
return &ToggleButton{*b, *l}
}