1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-10-31 19:44:23 +00:00
cchat-gtk/internal/ui/messages/input/username.go

142 lines
3.6 KiB
Go
Raw Normal View History

2020-06-07 04:27:28 +00:00
package input
import (
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/gts"
2020-06-20 04:40:34 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/config"
2020-06-07 04:27:28 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/rich"
"github.com/diamondburned/cchat/text"
"github.com/diamondburned/imgutil"
"github.com/gotk3/gotk3/gtk"
)
const AvatarSize = 24
2020-06-07 04:27:28 +00:00
2020-06-20 04:40:34 +00:00
var showUser = true
var currentRevealer = func(bool) {} // noop by default
func init() {
// Bind this revealer in settings.
config.AppearanceAdd("Show Username in Input", config.Switch(
&showUser,
func(b bool) { currentRevealer(b) },
))
}
2020-06-07 04:27:28 +00:00
type usernameContainer struct {
*gtk.Revealer
main *gtk.Box
2020-06-07 04:27:28 +00:00
avatar *rich.Icon
label *rich.Label
}
var (
_ cchat.LabelContainer = (*usernameContainer)(nil)
_ cchat.IconContainer = (*usernameContainer)(nil)
)
func newUsernameContainer() *usernameContainer {
avatar := rich.NewIcon(AvatarSize, imgutil.Round(true))
avatar.SetPlaceholderIcon("user-available-symbolic", AvatarSize)
avatar.Show()
label := rich.NewLabel(text.Rich{})
label.SetMaxWidthChars(35)
label.Show()
box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 5)
box.PackStart(avatar, false, false, 0)
box.PackStart(label, false, false, 0)
box.SetMarginStart(10)
box.SetMarginEnd(10)
box.SetMarginTop(inputmargin)
box.SetMarginBottom(inputmargin)
box.SetVAlign(gtk.ALIGN_START)
box.Show()
rev, _ := gtk.RevealerNew()
rev.SetRevealChild(false)
rev.SetTransitionType(gtk.REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
rev.SetTransitionDuration(50)
rev.Add(box)
2020-06-20 04:40:34 +00:00
// Bind the current global revealer to this revealer for settings. This
// operation should be thread-safe, as everything is being done in the main
// thread.
currentRevealer = rev.SetRevealChild
2020-06-07 04:27:28 +00:00
return &usernameContainer{
Revealer: rev,
main: box,
avatar: avatar,
label: label,
}
}
2020-06-20 04:40:34 +00:00
func (u *usernameContainer) SetRevealChild(reveal bool) {
// Only reveal if showUser is true.
u.Revealer.SetRevealChild(reveal && showUser)
}
// shouldReveal returns whether or not the container should reveal.
func (u *usernameContainer) shouldReveal() bool {
return !u.label.GetLabel().Empty() && showUser
}
2020-06-07 07:06:13 +00:00
func (u *usernameContainer) Reset() {
u.SetRevealChild(false)
u.avatar.Reset()
u.label.Reset()
}
2020-06-07 04:27:28 +00:00
// Update is not thread-safe.
func (u *usernameContainer) Update(session cchat.Session, sender cchat.ServerMessageSender) {
// Set the fallback username.
u.label.SetLabelUnsafe(session.Name())
// Reveal the name if it's not empty.
2020-06-20 04:40:34 +00:00
u.SetRevealChild(u.shouldReveal())
2020-06-07 04:27:28 +00:00
// Does sender (aka Server) implement ServerNickname? If yes, use it.
if nicknamer, ok := sender.(cchat.ServerNickname); ok {
u.label.AsyncSetLabel(nicknamer.Nickname, "Error fetching server nickname")
2020-06-07 04:27:28 +00:00
}
// Does session implement an icon? Update if yes.
2020-06-07 04:27:28 +00:00
if iconer, ok := session.(cchat.Icon); ok {
2020-06-30 03:39:42 +00:00
u.avatar.AsyncSetIconer(iconer, "Error fetching session icon URL")
2020-06-07 04:27:28 +00:00
}
}
// GetLabel is not thread-safe.
func (u *usernameContainer) GetLabel() text.Rich {
return u.label.GetLabel()
}
// SetLabel is thread-safe.
func (u *usernameContainer) SetLabel(content text.Rich) {
gts.ExecAsync(func() {
u.label.SetLabelUnsafe(content)
// Reveal if the name is not empty.
2020-06-20 04:40:34 +00:00
u.SetRevealChild(u.shouldReveal())
2020-06-07 04:27:28 +00:00
})
}
// SetIcon is thread-safe.
func (u *usernameContainer) SetIcon(url string) {
gts.ExecAsync(func() {
u.avatar.SetIconUnsafe(url)
// Reveal if the icon URL is not empty. We don't touch anything if the
// URL is empty, as the name might not be.
if url != "" {
u.SetRevealChild(true)
}
})
}
2020-06-07 07:06:13 +00:00
// GetIconURL is not thread-safe.
func (u *usernameContainer) GetIconURL() string {
return u.avatar.URL()
}