2020-05-26 06:51:06 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts"
|
2020-06-20 07:28:47 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/log"
|
2020-06-20 04:40:34 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/config/preferences"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/messages"
|
2020-06-04 23:00:41 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/auth"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/session"
|
2020-05-28 19:26:55 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/session/server"
|
2020-05-26 06:51:06 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
2020-06-06 00:47:28 +00:00
|
|
|
"github.com/markbates/pkger"
|
2020-06-20 07:28:47 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-05-26 06:51:06 +00:00
|
|
|
)
|
|
|
|
|
2020-06-06 00:47:28 +00:00
|
|
|
func init() {
|
|
|
|
// Load the local CSS.
|
|
|
|
gts.LoadCSS(pkger.Include("/internal/ui/style.css"))
|
|
|
|
}
|
|
|
|
|
2020-06-13 21:51:03 +00:00
|
|
|
// constraints for the left panel
|
|
|
|
const (
|
|
|
|
leftMinWidth = 200
|
2020-07-14 07:24:55 +00:00
|
|
|
leftCurrentWidth = 275
|
2020-06-13 21:51:03 +00:00
|
|
|
leftMaxWidth = 400
|
|
|
|
)
|
|
|
|
|
|
|
|
func clamp(n, min, max int) int {
|
|
|
|
switch {
|
|
|
|
case n > max:
|
|
|
|
return max
|
|
|
|
case n < min:
|
|
|
|
return min
|
|
|
|
default:
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
type App struct {
|
2020-05-26 06:51:06 +00:00
|
|
|
window *window
|
|
|
|
header *header
|
2020-06-04 23:00:41 +00:00
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// used to keep track of what row to disconnect before switching
|
2020-06-17 07:06:34 +00:00
|
|
|
lastSelector func(bool)
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-06-20 04:40:34 +00:00
|
|
|
_ gts.WindowHeaderer = (*App)(nil)
|
2020-06-04 23:00:41 +00:00
|
|
|
_ service.Controller = (*App)(nil)
|
2020-05-26 06:51:06 +00:00
|
|
|
)
|
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
func NewApplication() *App {
|
2020-07-14 07:24:55 +00:00
|
|
|
app := &App{}
|
|
|
|
app.window = newWindow(app)
|
|
|
|
app.header = newHeader()
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-06-13 21:51:03 +00:00
|
|
|
// Resize the left-side header w/ the left-side pane.
|
|
|
|
app.window.Services.Connect("size-allocate", func(wv gtk.IWidget) {
|
|
|
|
// Get the current width of the left sidebar.
|
|
|
|
var width = app.window.GetPosition()
|
|
|
|
// Set the left-side header's size.
|
|
|
|
app.header.left.SetSizeRequest(width, -1)
|
|
|
|
})
|
|
|
|
|
2020-06-20 04:40:34 +00:00
|
|
|
// Bind the preferences action for our GAction button in the header popover.
|
|
|
|
// The action name for this is "app.preferences".
|
|
|
|
gts.AddAppAction("preferences", preferences.SpawnPreferenceDialog)
|
|
|
|
|
2020-05-26 06:51:06 +00:00
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
func (app *App) AddService(svc cchat.Service) {
|
2020-07-14 07:24:55 +00:00
|
|
|
app.window.Services.AddService(svc)
|
2020-06-07 04:27:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// OnSessionRemove resets things before the session is removed.
|
|
|
|
func (app *App) OnSessionRemove(id string) {
|
|
|
|
// Reset the message view if it's what we're showing.
|
|
|
|
if app.window.MessageView.SessionID() == id {
|
|
|
|
app.window.MessageView.Reset()
|
|
|
|
app.header.SetBreadcrumb(nil)
|
|
|
|
}
|
2020-05-28 19:26:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 18:19:06 +00:00
|
|
|
func (app *App) OnSessionDisconnect(id string) {
|
|
|
|
// We're basically doing the same thing as removing a session. Check
|
|
|
|
// OnSessionRemove above.
|
|
|
|
app.OnSessionRemove(id)
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
func (app *App) SessionSelected(svc *service.Service, ses *session.Row) {
|
|
|
|
// TODO: restore last message box
|
|
|
|
app.window.MessageView.Reset()
|
|
|
|
app.header.SetBreadcrumb(nil)
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
func (app *App) RowSelected(ses *session.Row, srv *server.ServerRow, smsg cchat.ServerMessage) {
|
2020-06-13 07:29:32 +00:00
|
|
|
// Is there an old row that we should deactivate?
|
2020-06-17 07:06:34 +00:00
|
|
|
if app.lastSelector != nil {
|
|
|
|
app.lastSelector(false)
|
2020-06-04 23:00:41 +00:00
|
|
|
}
|
2020-06-17 07:06:34 +00:00
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Set the new row.
|
2020-06-17 07:06:34 +00:00
|
|
|
app.lastSelector = srv.SetSelected
|
|
|
|
app.lastSelector(true)
|
2020-06-04 23:00:41 +00:00
|
|
|
|
2020-06-06 00:47:28 +00:00
|
|
|
app.header.SetBreadcrumb(srv.Breadcrumb())
|
2020-06-04 23:00:41 +00:00
|
|
|
|
2020-06-17 07:06:34 +00:00
|
|
|
// Disable the server list because we don't want the user to switch around
|
|
|
|
// while we're loading.
|
|
|
|
app.window.Services.SetSensitive(false)
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Assert that server is also a list, then join the server.
|
2020-06-17 07:06:34 +00:00
|
|
|
app.window.MessageView.JoinServer(ses.Session, smsg.(messages.ServerMessage), func() {
|
|
|
|
// Re-enable the server list.
|
|
|
|
app.window.Services.SetSensitive(true)
|
|
|
|
})
|
2020-06-04 23:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
func (app *App) AuthenticateSession(list *service.List, ssvc *service.Service) {
|
|
|
|
var svc = ssvc.Service()
|
2020-06-04 23:00:41 +00:00
|
|
|
auth.NewDialog(svc.Name(), svc.Authenticate(), func(ses cchat.Session) {
|
2020-07-14 07:24:55 +00:00
|
|
|
ssvc.AddSession(ses)
|
2020-06-04 23:00:41 +00:00
|
|
|
})
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 07:28:47 +00:00
|
|
|
// Close is called when the application finishes gracefully.
|
|
|
|
func (app *App) Close() {
|
|
|
|
// Disconnect everything. This blocks the main thread, so by the time we're
|
|
|
|
// done, the application would exit immediately. There's no need to update
|
|
|
|
// the GUI.
|
2020-07-14 07:24:55 +00:00
|
|
|
for _, s := range app.window.AllServices() {
|
|
|
|
var service = s.Service().Name()
|
|
|
|
|
|
|
|
for _, session := range s.BodyList.Sessions() {
|
2020-06-20 07:28:47 +00:00
|
|
|
if session.Session == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
log.Printlnf("Disconnecting %s session %s", service, session.ID())
|
2020-06-20 07:28:47 +00:00
|
|
|
|
|
|
|
if err := session.Session.Disconnect(); err != nil {
|
|
|
|
log.Error(errors.Wrap(err, "Failed to disconnect "+session.ID()))
|
|
|
|
}
|
2020-06-20 04:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
func (app *App) Header() gtk.IWidget {
|
2020-05-26 06:51:06 +00:00
|
|
|
return app.header
|
|
|
|
}
|
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
func (app *App) Window() gtk.IWidget {
|
2020-05-26 06:51:06 +00:00
|
|
|
return app.window
|
|
|
|
}
|