2020-07-14 07:24:55 +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/service/loading"
|
2020-07-18 07:16:47 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/session/server/traverse"
|
2020-07-14 07:24:55 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Controller interface {
|
|
|
|
RowSelected(*ServerRow, cchat.ServerMessage)
|
|
|
|
}
|
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
// Children is a children server with a reference to the parent. By default, a
|
|
|
|
// children will contain hollow rows. They are rows that do not yet have any
|
|
|
|
// widgets. This changes as soon as Row's Load is called.
|
2020-07-14 07:24:55 +00:00
|
|
|
type Children struct {
|
|
|
|
*gtk.Box
|
2020-07-18 07:16:47 +00:00
|
|
|
|
|
|
|
load *loading.Button // only not nil while loading
|
|
|
|
loading bool
|
2020-07-14 07:24:55 +00:00
|
|
|
|
|
|
|
Rows []*ServerRow
|
|
|
|
|
2020-07-17 18:17:21 +00:00
|
|
|
Parent traverse.Breadcrumber
|
2020-07-14 07:24:55 +00:00
|
|
|
rowctrl Controller
|
2020-07-18 07:16:47 +00:00
|
|
|
|
|
|
|
// Unreadable state for children rows to use. The parent row that has this
|
|
|
|
// Children will bind a handler to this.
|
|
|
|
traverse.Unreadable
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var childrenCSS = primitives.PrepareClassCSS("server-children", `
|
2020-07-17 00:21:14 +00:00
|
|
|
.server-children {
|
|
|
|
margin: 0;
|
|
|
|
margin-top: 3px;
|
|
|
|
border-radius: 0;
|
|
|
|
}
|
2020-07-14 07:24:55 +00:00
|
|
|
`)
|
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
// NewHollowChildren creates a hollow children, which is a children without any
|
|
|
|
// widgets.
|
|
|
|
func NewHollowChildren(p traverse.Breadcrumber, ctrl Controller) *Children {
|
2020-07-14 07:24:55 +00:00
|
|
|
return &Children{
|
|
|
|
Parent: p,
|
|
|
|
rowctrl: ctrl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
// NewChildren creates a hollow children then immediately unhollows it.
|
|
|
|
func NewChildren(p traverse.Breadcrumber, ctrl Controller) *Children {
|
|
|
|
c := NewHollowChildren(p, ctrl)
|
|
|
|
c.Init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Children) IsHollow() bool {
|
|
|
|
return c.Box == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init ensures that the children container is not hollow. It does nothing after
|
|
|
|
// the first call. It does not actually populate the list with widgets. This is
|
|
|
|
// done for lazy loading. To load everything, call LoadAll after this.
|
|
|
|
//
|
|
|
|
// Nothing but ServerRow should call this method.
|
|
|
|
func (c *Children) Init() {
|
|
|
|
if c.IsHollow() {
|
|
|
|
c.Box, _ = gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
|
|
|
|
c.Box.SetMarginStart(ChildrenMargin)
|
2020-08-29 01:42:28 +00:00
|
|
|
c.Box.SetHExpand(true)
|
2020-07-18 07:16:47 +00:00
|
|
|
childrenCSS(c.Box)
|
|
|
|
|
|
|
|
// Check if we're still loading. This is effectively restoring the
|
|
|
|
// state that was set before we had widgets.
|
|
|
|
if c.loading {
|
|
|
|
c.setLoading()
|
|
|
|
} else {
|
|
|
|
c.setNotLoading()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset ensures that the children container is no longer hollow, then reset all
|
|
|
|
// states.
|
|
|
|
func (c *Children) Reset() {
|
|
|
|
// If the children container isn't hollow, then we have to remove the known
|
|
|
|
// rows from the container box.
|
|
|
|
if c.Box != nil {
|
|
|
|
// Remove old servers from the list.
|
|
|
|
for _, row := range c.Rows {
|
2020-08-20 23:53:13 +00:00
|
|
|
if row.IsHollow() {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-18 07:16:47 +00:00
|
|
|
c.Box.Remove(row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wipe the list empty.
|
|
|
|
c.Rows = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setLoading shows the loading circle as a list child. If hollow, this function
|
|
|
|
// will only update the state.
|
2020-07-14 07:24:55 +00:00
|
|
|
func (c *Children) setLoading() {
|
2020-07-18 07:16:47 +00:00
|
|
|
c.loading = true
|
|
|
|
|
|
|
|
// Don't do the rest if we're still hollow.
|
|
|
|
if c.IsHollow() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// Exit if we're already loading.
|
|
|
|
if c.load != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear everything.
|
|
|
|
c.Reset()
|
|
|
|
|
|
|
|
// Set the loading circle and stuff.
|
|
|
|
c.load = loading.NewButton()
|
|
|
|
c.load.Show()
|
|
|
|
c.Box.Add(c.load)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2020-07-18 07:16:47 +00:00
|
|
|
c.loading = false
|
|
|
|
|
|
|
|
// Don't call the rest if we're still hollow.
|
|
|
|
if c.IsHollow() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// 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.Box.Remove(c.load)
|
|
|
|
c.load = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetServers is reserved for cchat.ServersContainer.
|
|
|
|
func (c *Children) SetServers(servers []cchat.Server) {
|
|
|
|
gts.ExecAsync(func() {
|
2020-07-18 07:16:47 +00:00
|
|
|
// Save the current state (if any) if the children container is not
|
|
|
|
// hollow.
|
|
|
|
if !c.IsHollow() {
|
|
|
|
restore := c.saveSelectedRow()
|
|
|
|
defer restore()
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset before inserting new servers.
|
|
|
|
c.Reset()
|
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
// Insert hollow servers.
|
2020-07-14 07:24:55 +00:00
|
|
|
c.Rows = make([]*ServerRow, len(servers))
|
|
|
|
for i, server := range servers {
|
2020-07-18 07:16:47 +00:00
|
|
|
c.Rows[i] = NewHollowServer(c, server, c.rowctrl)
|
|
|
|
}
|
2020-07-14 07:24:55 +00:00
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
// We should not unhollow everything here, but rather on uncollapse.
|
|
|
|
// Since the root node is always unhollow, calls to this function will
|
|
|
|
// pass the hollow test and unhollow its children nodes. That should not
|
|
|
|
// happen.
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadAll forces all children rows to be unhollowed (initialized). It does
|
|
|
|
// NOT check if the children container itself is hollow.
|
|
|
|
func (c *Children) LoadAll() {
|
|
|
|
AssertUnhollow(c)
|
|
|
|
|
|
|
|
for _, row := range c.Rows {
|
2020-07-19 01:57:57 +00:00
|
|
|
if row.IsHollow() {
|
|
|
|
row.Init() // this is the alloc-heavy method
|
|
|
|
row.Show()
|
|
|
|
c.Box.Add(row)
|
|
|
|
}
|
2020-07-18 07:16:47 +00:00
|
|
|
}
|
2020-08-29 01:42:28 +00:00
|
|
|
|
|
|
|
// Check if we have icons.
|
|
|
|
var hasIcon bool
|
|
|
|
|
|
|
|
for _, row := range c.Rows {
|
|
|
|
if row.HasIcon() {
|
|
|
|
hasIcon = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an icon, then show all other possibly empty icons. HdyAvatar
|
|
|
|
// will generate a placeholder.
|
|
|
|
if hasIcon {
|
|
|
|
for _, row := range c.Rows {
|
|
|
|
row.UseEmptyIcon()
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 07:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// saveSelectedRow saves the current selected row and returns a callback that
|
|
|
|
// restores the selection.
|
|
|
|
func (c *Children) saveSelectedRow() (restore func()) {
|
|
|
|
// Save the current state.
|
|
|
|
var oldID string
|
|
|
|
for _, row := range c.Rows {
|
|
|
|
if row.GetActive() {
|
|
|
|
oldID = row.Server.ID()
|
|
|
|
break
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
2020-07-18 07:16:47 +00:00
|
|
|
}
|
2020-07-14 07:24:55 +00:00
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
return func() {
|
2020-07-14 07:24:55 +00:00
|
|
|
if oldID != "" {
|
|
|
|
for _, row := range c.Rows {
|
|
|
|
if row.Server.ID() == oldID {
|
2020-08-29 01:42:28 +00:00
|
|
|
row.Init()
|
2020-07-14 07:24:55 +00:00
|
|
|
row.Button.SetActive(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 07:16:47 +00:00
|
|
|
|
|
|
|
// TODO Update parent reference? Only if it's activated.
|
|
|
|
}
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 18:17:21 +00:00
|
|
|
func (c *Children) Breadcrumb() traverse.Breadcrumb {
|
|
|
|
return traverse.TryBreadcrumb(c.Parent)
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|