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/service/session/session.go

129 lines
3.4 KiB
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package session
import (
"github.com/diamondburned/cchat"
2020-06-07 04:27:28 +00:00
"github.com/diamondburned/cchat-gtk/internal/keyring"
"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-06-07 04:27:28 +00:00
"github.com/diamondburned/imgutil"
2020-05-26 06:51:06 +00:00
"github.com/gotk3/gotk3/gtk"
2020-06-07 04:27:28 +00:00
"github.com/pkg/errors"
2020-05-26 06:51:06 +00:00
)
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-06-07 04:27:28 +00:00
RestoreSession(*Row, cchat.SessionRestorer) // async
2020-06-04 23:00:41 +00:00
}
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-07 04:27:28 +00:00
row := new(parent, ctrl)
row.SetSession(ses)
return row
}
func NewLoading(parent breadcrumb.Breadcrumber, name string, ctrl Controller) *Row {
row := new(parent, ctrl)
row.Button.SetLabelUnsafe(text.Rich{Content: name})
row.Button.Image.SetPlaceholderIcon("content-loading-symbolic", IconSize)
row.SetSensitive(false)
return row
}
func new(parent breadcrumb.Breadcrumber, ctrl Controller) *Row {
2020-06-04 23:00:41 +00:00
row := &Row{
2020-06-07 04:27:28 +00:00
ctrl: ctrl,
parent: parent,
2020-05-26 06:51:06 +00:00
}
2020-06-06 07:44:36 +00:00
row.Button = rich.NewToggleButtonImage(text.Rich{})
2020-06-04 23:00:41 +00:00
row.Button.Box.SetHAlign(gtk.ALIGN_START)
2020-06-07 04:27:28 +00:00
row.Button.Image.AddProcessors(imgutil.Round(true))
// Set the loading icon.
2020-06-04 23:00:41 +00:00
row.Button.SetRelief(gtk.RELIEF_NONE)
// On click, toggle reveal.
row.Button.Connect("clicked", func() {
revealed := !row.Servers.GetRevealChild()
row.Servers.SetRevealChild(revealed)
row.Button.SetActive(revealed)
})
2020-06-06 07:44:36 +00:00
row.Button.Show()
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.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-07 04:27:28 +00:00
// KeyringSession returns a keyring session, or nil if the session cannot be
// saved. This function is not cached, as I'd rather not keep the map in memory.
func (r *Row) KeyringSession() *keyring.Session {
// Is the session saveable?
saver, ok := r.Session.(cchat.SessionSaver)
if !ok {
return nil
}
ks := keyring.Session{
ID: r.Session.ID(),
Name: r.Button.GetText(),
}
s, err := saver.Save()
if err != nil {
log.Error(errors.Wrapf(err, "Failed to save session ID %s (%s)", ks.ID, ks.Name))
return nil
}
ks.Data = s
return &ks
}
func (r *Row) SetSession(ses cchat.Session) {
r.Session = ses
r.Servers = server.NewChildren(r, ses, r)
r.Button.Image.SetPlaceholderIcon("user-available-symbolic", IconSize)
r.Box.PackStart(r.Servers, false, false, 0)
r.SetSensitive(true)
// Set the session's name to the button.
r.Button.Try(ses, "session")
}
func (r *Row) SetFailed(err error) {
r.SetTooltipText(err.Error())
// TODO: setting the label directly here is kind of shitty, as it screws up
// the getter. Fix?
r.Button.Label.SetMarkup(rich.MakeRed(r.Button.GetLabel()))
}
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)
}