ted/session.go

87 lines
2 KiB
Go
Raw Normal View History

package main
2017-08-25 23:48:22 +00:00
import "bitbucket.org/lmika/ted-v2/ui"
// The session is responsible for managing the UI and the model and handling
// the interaction between the two and the user.
type Session struct {
2017-08-25 23:48:22 +00:00
Model Model
Frame *Frame
Commands *CommandMapping
UIManager *ui.Ui
}
2017-08-25 23:48:22 +00:00
func NewSession(uiManager *ui.Ui, frame *Frame, model Model) *Session {
session := &Session{
Model: model,
Frame: frame,
Commands: NewCommandMapping(),
UIManager: uiManager,
}
2017-08-25 23:48:22 +00:00
frame.SetModel(&SessionGridModel{session})
2015-03-24 02:12:40 +00:00
2017-08-25 23:48:22 +00:00
session.Commands.RegisterViewCommands()
session.Commands.RegisterViewKeyBindings()
2017-08-25 23:48:22 +00:00
// Also assign this session with the frame
frame.Session = session
2017-08-25 23:48:22 +00:00
return session
}
// Input from the frame
func (session *Session) KeyPressed(key rune, mod int) {
2017-08-25 23:48:22 +00:00
// Add the mod key modifier
if mod&ui.ModKeyAlt != 0 {
key |= ModAlt
}
cmd := session.Commands.KeyMapping(key)
if cmd != nil {
err := cmd.Do(&CommandContext{session})
if err != nil {
session.Frame.ShowMessage(err.Error())
}
}
}
// The command context used by the session
2017-08-25 23:48:22 +00:00
type CommandContext struct {
session *Session
}
2017-08-25 23:48:22 +00:00
func (scc *CommandContext) Session() *Session {
return scc.session
2015-03-24 02:12:40 +00:00
}
2017-08-25 23:48:22 +00:00
func (scc *CommandContext) Frame() *Frame {
return scc.session.Frame
}
2015-03-24 02:12:40 +00:00
// Session grid model
type SessionGridModel struct {
2017-08-25 23:48:22 +00:00
Session *Session
2015-03-24 02:12:40 +00:00
}
// Returns the size of the grid model (width x height)
func (sgm *SessionGridModel) Dimensions() (int, int) {
2017-08-25 23:48:22 +00:00
rs, cs := sgm.Session.Model.Dimensions()
return cs, rs
2015-03-24 02:12:40 +00:00
}
// Returns the size of the particular column. If the size is 0, this indicates that the column is hidden.
func (sgm *SessionGridModel) ColWidth(int) int {
2017-08-25 23:48:22 +00:00
return 24
2015-03-24 02:12:40 +00:00
}
// Returns the size of the particular row. If the size is 0, this indicates that the row is hidden.
func (sgm *SessionGridModel) RowHeight(int) int {
2017-08-25 23:48:22 +00:00
return 1
2015-03-24 02:12:40 +00:00
}
// Returns the value of the cell a position X, Y
func (sgm *SessionGridModel) CellValue(x int, y int) string {
2017-08-25 23:48:22 +00:00
return sgm.Session.Model.CellValue(y, x)
2015-03-24 02:12:40 +00:00
}