2020-06-04 23:00:41 +00:00
|
|
|
package rich
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: parser
|
|
|
|
|
|
|
|
type Labeler interface {
|
|
|
|
cchat.LabelContainer // thread-safe
|
|
|
|
GetLabel() text.Rich // not thread-safe
|
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))
|
2020-06-04 23:00:41 +00:00
|
|
|
label.SetHAlign(gtk.ALIGN_START)
|
|
|
|
return &Label{*label, content}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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}
|
|
|
|
}
|