2020-06-06 00:47:28 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
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-gtk/internal/ui/service/breadcrumb"
|
2020-06-17 07:06:34 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/button"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/loading"
|
2020-06-17 07:06:34 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/menu"
|
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-06-06 00:47:28 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ChildrenMargin = 24
|
2020-07-11 06:48:44 +00:00
|
|
|
const IconSize = 32
|
2020-06-06 00:47:28 +00:00
|
|
|
|
|
|
|
type Controller interface {
|
2020-06-17 07:06:34 +00:00
|
|
|
RowSelected(*ServerRow, cchat.ServerMessage)
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Row struct {
|
|
|
|
*gtk.Box
|
2020-06-17 07:06:34 +00:00
|
|
|
Button *button.ToggleButtonImage
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
parentcrumb breadcrumb.Breadcrumber
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
children *Children
|
|
|
|
serverList cchat.ServerList
|
|
|
|
loaded bool
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func NewRow(parent breadcrumb.Breadcrumber, name text.Rich) *Row {
|
|
|
|
button := button.NewToggleButtonImage(name)
|
2020-06-06 00:47:28 +00:00
|
|
|
button.Box.SetHAlign(gtk.ALIGN_START)
|
|
|
|
button.SetRelief(gtk.RELIEF_NONE)
|
|
|
|
button.Show()
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-06-06 00:47:28 +00:00
|
|
|
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
|
2020-06-17 07:06:34 +00:00
|
|
|
box.SetMarginStart(ChildrenMargin)
|
2020-06-06 00:47:28 +00:00
|
|
|
box.PackStart(button, false, false, 0)
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
row := &Row{
|
|
|
|
Box: box,
|
|
|
|
Button: button,
|
|
|
|
parentcrumb: parent,
|
|
|
|
}
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
return row
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Row) Breadcrumb() breadcrumb.Breadcrumb {
|
|
|
|
return breadcrumb.Try(r.parentcrumb, r.Button.GetText())
|
|
|
|
}
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func (r *Row) SetLabelUnsafe(name text.Rich) {
|
|
|
|
r.Button.SetLabelUnsafe(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Row) SetIconer(v interface{}) {
|
|
|
|
if iconer, ok := v.(cchat.Icon); ok {
|
2020-07-11 06:48:44 +00:00
|
|
|
r.Button.Image.SetSize(IconSize)
|
2020-06-17 07:06:34 +00:00
|
|
|
r.Button.Image.AsyncSetIconer(iconer, "Error getting server icon URL")
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
2020-06-17 07:06:34 +00:00
|
|
|
}
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
// SetServerList sets the row to a server list.
|
|
|
|
func (r *Row) SetServerList(list cchat.ServerList, ctrl Controller) {
|
|
|
|
r.Button.SetClicked(func(active bool) {
|
|
|
|
r.SetRevealChild(active)
|
|
|
|
})
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
r.children = NewChildren(r, ctrl)
|
|
|
|
r.children.Show()
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
r.Box.PackStart(r.children, false, false, 0)
|
|
|
|
r.serverList = list
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset clears off all children servers. It's a no-op if there are none.
|
|
|
|
func (r *Row) Reset() {
|
|
|
|
if r.children != nil {
|
|
|
|
// Remove everything from the children container.
|
|
|
|
r.children.Reset()
|
|
|
|
|
|
|
|
// Remove the children container itself.
|
|
|
|
r.Box.Remove(r.children)
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
2020-06-30 07:20:13 +00:00
|
|
|
|
|
|
|
// Reset the state.
|
|
|
|
r.loaded = false
|
|
|
|
r.serverList = nil
|
|
|
|
r.children = nil
|
2020-06-17 07:06:34 +00:00
|
|
|
}
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
// SetLoading is called by the parent struct.
|
|
|
|
func (r *Row) SetLoading() {
|
|
|
|
r.Button.SetLoading()
|
|
|
|
r.SetSensitive(false)
|
|
|
|
}
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
// SetFailed is shared between the parent struct and the children list. This is
|
|
|
|
// because both of those errors share the same appearance, just different
|
|
|
|
// callbacks.
|
|
|
|
func (r *Row) SetFailed(err error, retry func()) {
|
|
|
|
r.SetSensitive(true)
|
|
|
|
r.SetTooltipText(err.Error())
|
|
|
|
r.Button.SetFailed(err, retry)
|
|
|
|
r.Button.Label.SetMarkup(rich.MakeRed(r.Button.GetLabel()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDone is shared between the parent struct and the children list. This is
|
|
|
|
// because both will use the same SetFailed.
|
|
|
|
func (r *Row) SetDone() {
|
|
|
|
r.Button.SetNormal()
|
|
|
|
r.SetSensitive(true)
|
|
|
|
r.SetTooltipText("")
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func (r *Row) SetNormalExtraMenu(items []menu.Item) {
|
|
|
|
r.Button.SetNormalExtraMenu(items)
|
|
|
|
r.SetSensitive(true)
|
|
|
|
r.SetTooltipText("")
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func (r *Row) childrenFailed(err error) {
|
|
|
|
// If the user chooses to retry, the list will automatically expand.
|
|
|
|
r.SetFailed(err, func() { r.SetRevealChild(true) })
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func (r *Row) childrenDone() {
|
|
|
|
r.loaded = true
|
2020-07-11 06:48:44 +00:00
|
|
|
|
|
|
|
// I don't think this is supposed to be called here...
|
|
|
|
// r.SetDone()
|
2020-06-17 07:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSelected is used for highlighting the current message server.
|
|
|
|
func (r *Row) SetSelected(selected bool) {
|
|
|
|
// Set the clickability the opposite as the boolean.
|
|
|
|
r.Button.SetSensitive(!selected)
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
// Some special edge case that I forgot.
|
|
|
|
if !selected {
|
|
|
|
r.Button.SetActive(false)
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func (r *Row) GetActive() bool {
|
|
|
|
return r.Button.GetActive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRevealChild reveals the list of servers. It does nothing if there are no
|
|
|
|
// servers, meaning if Row does not represent a ServerList.
|
|
|
|
func (r *Row) SetRevealChild(reveal bool) {
|
|
|
|
// Do the above noop check.
|
|
|
|
if r.children == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually reveal the children.
|
|
|
|
r.children.SetRevealChild(reveal)
|
|
|
|
|
|
|
|
// If this isn't a reveal, then we don't need to load.
|
|
|
|
if !reveal {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't loaded yet and we're still not loading, then load.
|
|
|
|
if !r.loaded && r.children.load == nil {
|
|
|
|
r.Load()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads the row without uncollapsing it.
|
|
|
|
func (r *Row) Load() {
|
|
|
|
// Safeguard.
|
|
|
|
if r.children == nil || r.serverList == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set that we're now loading.
|
|
|
|
r.children.setLoading()
|
|
|
|
r.SetSensitive(false)
|
|
|
|
|
|
|
|
// Load the list of servers if we're still in loading mode.
|
|
|
|
go func() {
|
|
|
|
err := r.serverList.Servers(r.children)
|
|
|
|
gts.ExecAsync(func() {
|
|
|
|
// We're not loading anymore, so remove the loading circle.
|
|
|
|
r.children.setNotLoading()
|
|
|
|
// Restore clickability.
|
|
|
|
r.SetSensitive(true)
|
|
|
|
|
|
|
|
// Use the childrenX method instead of SetX.
|
|
|
|
if err != nil {
|
|
|
|
r.childrenFailed(errors.Wrap(err, "Failed to get servers"))
|
|
|
|
} else {
|
|
|
|
r.childrenDone()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRevealChild returns whether or not the server list is expanded, or always
|
|
|
|
// false if there is no server list.
|
|
|
|
func (r *Row) GetRevealChild() bool {
|
|
|
|
if r.children != nil {
|
|
|
|
return r.children.GetRevealChild()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerRow struct {
|
|
|
|
*Row
|
|
|
|
Server cchat.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServerRow(p breadcrumb.Breadcrumber, server cchat.Server, ctrl Controller) *ServerRow {
|
|
|
|
row := NewRow(p, server.Name())
|
|
|
|
row.Show()
|
|
|
|
row.SetIconer(server)
|
|
|
|
primitives.AddClass(row, "server")
|
|
|
|
|
|
|
|
var serverRow = &ServerRow{Row: row, Server: server}
|
|
|
|
|
|
|
|
switch server := server.(type) {
|
|
|
|
case cchat.ServerList:
|
|
|
|
row.SetServerList(server, ctrl)
|
|
|
|
primitives.AddClass(row, "server-list")
|
|
|
|
|
|
|
|
case cchat.ServerMessage:
|
|
|
|
row.Button.SetClickedIfTrue(func() { ctrl.RowSelected(serverRow, server) })
|
|
|
|
primitives.AddClass(row, "server-message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return serverRow
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Children is a children server with a reference to the parent.
|
|
|
|
type Children struct {
|
|
|
|
*gtk.Revealer
|
|
|
|
Main *gtk.Box
|
|
|
|
|
|
|
|
rowctrl Controller
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
load *loading.Button // only not nil while loading
|
|
|
|
|
|
|
|
Rows []*ServerRow
|
2020-06-06 00:47:28 +00:00
|
|
|
Parent breadcrumb.Breadcrumber
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func NewChildren(p breadcrumb.Breadcrumber, ctrl Controller) *Children {
|
2020-06-06 00:47:28 +00:00
|
|
|
main, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
|
|
|
|
main.Show()
|
|
|
|
|
|
|
|
rev, _ := gtk.RevealerNew()
|
|
|
|
rev.SetRevealChild(false)
|
|
|
|
rev.Add(main)
|
|
|
|
|
2020-06-07 07:06:13 +00:00
|
|
|
return &Children{
|
2020-06-06 00:47:28 +00:00
|
|
|
Revealer: rev,
|
|
|
|
Main: main,
|
|
|
|
rowctrl: ctrl,
|
2020-06-17 07:06:34 +00:00
|
|
|
Parent: p,
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
2020-06-07 07:06:13 +00:00
|
|
|
}
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
// setLoading shows the loading circle as a list child.
|
|
|
|
func (c *Children) setLoading() {
|
|
|
|
// Exit if we're already loading.
|
2020-06-14 18:19:06 +00:00
|
|
|
if c.load != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
// Clear everything.
|
|
|
|
c.Reset()
|
|
|
|
|
|
|
|
// Set the loading circle and stuff.
|
2020-06-14 18:19:06 +00:00
|
|
|
c.load = loading.NewButton()
|
|
|
|
c.load.Show()
|
|
|
|
c.Main.Add(c.load)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Children) Reset() {
|
|
|
|
// Remove old servers from the list.
|
|
|
|
for _, row := range c.Rows {
|
|
|
|
c.Main.Remove(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wipe the list empty.
|
|
|
|
c.Rows = nil
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
// setNotLoading removes the loading circle, if any. This is not in Reset()
|
|
|
|
// anymore, since the backend may not necessarily call SetServers.
|
|
|
|
func (c *Children) setNotLoading() {
|
|
|
|
// Do we have the spinning circle button? If yes, remove it.
|
|
|
|
if c.load != nil {
|
|
|
|
// Stop the loading mode. The reset function should do everything for us.
|
|
|
|
c.Main.Remove(c.load)
|
|
|
|
c.load = nil
|
|
|
|
}
|
2020-06-06 00:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Children) SetServers(servers []cchat.Server) {
|
|
|
|
gts.ExecAsync(func() {
|
|
|
|
// Save the current state.
|
|
|
|
var oldID string
|
|
|
|
for _, row := range c.Rows {
|
|
|
|
if row.GetActive() {
|
|
|
|
oldID = row.Server.ID()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 18:19:06 +00:00
|
|
|
// Reset before inserting new servers.
|
|
|
|
c.Reset()
|
2020-06-06 00:47:28 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
c.Rows = make([]*ServerRow, len(servers))
|
2020-06-06 00:47:28 +00:00
|
|
|
|
|
|
|
for i, server := range servers {
|
2020-06-17 07:06:34 +00:00
|
|
|
row := NewServerRow(c, server, c.rowctrl)
|
2020-06-06 00:47:28 +00:00
|
|
|
c.Rows[i] = row
|
|
|
|
c.Main.Add(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update parent reference? Only if it's activated.
|
|
|
|
if oldID != "" {
|
|
|
|
for _, row := range c.Rows {
|
|
|
|
if row.Server.ID() == oldID {
|
|
|
|
row.Button.SetActive(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Children) Breadcrumb() breadcrumb.Breadcrumb {
|
|
|
|
return breadcrumb.Try(c.Parent)
|
|
|
|
}
|