cchat-gtk/internal/ui/ui.go

109 lines
2.6 KiB
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package ui
import (
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/gts"
"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-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"))
}
// constraints for the left panel
const (
leftMinWidth = 200
leftCurrentWidth = 250
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
// used to keep track of what row to disconnect before switching
lastDeactivator func()
2020-05-26 06:51:06 +00:00
}
var (
2020-06-04 23:00:41 +00:00
_ gts.Windower = (*App)(nil)
_ gts.Headerer = (*App)(nil)
_ service.Controller = (*App)(nil)
2020-05-26 06:51:06 +00:00
)
2020-06-04 23:00:41 +00:00
func NewApplication() *App {
app := &App{
2020-05-26 06:51:06 +00:00
window: newWindow(),
header: newHeader(),
}
// 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-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-06-07 07:06:13 +00:00
app.window.Services.AddService(svc, app)
2020-06-07 04:27:28 +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-04 23:00:41 +00:00
func (app *App) MessageRowSelected(ses *session.Row, srv *server.Row, smsg cchat.ServerMessage) {
// Is there an old row that we should deactivate?
if app.lastDeactivator != nil {
app.lastDeactivator()
2020-06-04 23:00:41 +00:00
}
// Set the new row.
app.lastDeactivator = srv.Deactivate
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
// Assert that server is also a list, then join the server.
app.window.MessageView.JoinServer(ses.Session, smsg.(messages.ServerMessage))
2020-06-04 23:00:41 +00:00
}
func (app *App) AuthenticateSession(container *service.Container, svc cchat.Service) {
auth.NewDialog(svc.Name(), svc.Authenticate(), func(ses cchat.Session) {
container.AddSession(ses)
})
2020-05-26 06:51:06 +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
}