2020-05-26 06:51:06 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/log"
|
2020-05-28 19:26:55 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
2020-06-04 23:00:41 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich"
|
2020-06-06 00:47:28 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/breadcrumb"
|
2020-05-26 06:51:06 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/session/server"
|
2020-06-04 23:00:41 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-05-26 06:51:06 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
const IconSize = 32
|
|
|
|
|
|
|
|
// Controller extends server.RowController to add session.
|
|
|
|
type Controller interface {
|
|
|
|
MessageRowSelected(*Row, *server.Row, cchat.ServerMessage)
|
|
|
|
}
|
|
|
|
|
2020-05-26 06:51:06 +00:00
|
|
|
type Row struct {
|
|
|
|
*gtk.Box
|
2020-06-04 23:00:41 +00:00
|
|
|
Button *rich.ToggleButtonImage
|
2020-05-26 06:51:06 +00:00
|
|
|
Session cchat.Session
|
|
|
|
|
|
|
|
Servers *server.Children
|
2020-06-04 23:00:41 +00:00
|
|
|
|
2020-06-06 00:47:28 +00:00
|
|
|
ctrl Controller
|
|
|
|
parent breadcrumb.Breadcrumber
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 00:47:28 +00:00
|
|
|
func New(parent breadcrumb.Breadcrumber, ses cchat.Session, ctrl Controller) *Row {
|
2020-06-04 23:00:41 +00:00
|
|
|
row := &Row{
|
|
|
|
Session: ses,
|
|
|
|
ctrl: ctrl,
|
2020-06-06 00:47:28 +00:00
|
|
|
parent: parent,
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
2020-06-06 00:47:28 +00:00
|
|
|
row.Servers = server.NewChildren(row, ses, row)
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
row.Button = rich.NewToggleButtonImage(text.Rich{}, "")
|
|
|
|
row.Button.Box.SetHAlign(gtk.ALIGN_START)
|
|
|
|
row.Button.SetRelief(gtk.RELIEF_NONE)
|
|
|
|
row.Button.Show()
|
|
|
|
// On click, toggle reveal.
|
|
|
|
row.Button.Connect("clicked", func() {
|
|
|
|
revealed := !row.Servers.GetRevealChild()
|
|
|
|
row.Servers.SetRevealChild(revealed)
|
|
|
|
row.Button.SetActive(revealed)
|
|
|
|
})
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
primitives.SetImageIcon(&row.Button.Image, "user-available-symbolic", IconSize)
|
2020-05-28 19:26:55 +00:00
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
if err := ses.Name(row.Button); err != nil {
|
|
|
|
log.Error(errors.Wrap(err, "Failed to get the username"))
|
|
|
|
row.Button.SetLabel(text.Rich{Content: "Unknown"})
|
|
|
|
}
|
2020-05-28 19:26:55 +00:00
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
row.Box, _ = gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
|
|
|
|
row.Box.SetMarginStart(server.ChildrenMargin)
|
|
|
|
row.Box.PackStart(row.Button, false, false, 0)
|
|
|
|
row.Box.PackStart(row.Servers, false, false, 0)
|
|
|
|
row.Box.Show()
|
2020-05-28 19:26:55 +00:00
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
primitives.AddClass(row.Box, "session")
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
return row
|
|
|
|
}
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
func (r *Row) MessageRowSelected(server *server.Row, smsg cchat.ServerMessage) {
|
|
|
|
r.ctrl.MessageRowSelected(r, server, smsg)
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
2020-06-06 00:47:28 +00:00
|
|
|
|
|
|
|
func (r *Row) Breadcrumb() breadcrumb.Breadcrumb {
|
|
|
|
return breadcrumb.Try(r.parent, r.Button.GetLabel().Content)
|
|
|
|
}
|