ted/frame.go

138 lines
3.1 KiB
Go
Raw Normal View History

package main
import (
2017-08-25 23:48:22 +00:00
"bitbucket.org/lmika/ted-v2/ui"
)
2017-08-25 23:48:22 +00:00
type Mode int
const (
2017-08-25 23:48:22 +00:00
NilMode Mode = iota
// The grid is selectable
GridMode
// EntryMode is when the text entry is selected
EntryMode
)
// A frame is a UI instance.
type Frame struct {
2017-08-25 23:48:22 +00:00
Session *Session
mode Mode
uiManager *ui.Ui
clientArea *ui.RelativeLayout
grid *ui.Grid
messageView *ui.TextView
textEntry *ui.TextEntry
statusBar *ui.StatusBar
textEntrySwitch *ui.ProxyLayout
}
// Creates the UI and returns a new frame
func NewFrame(uiManager *ui.Ui) *Frame {
2017-08-25 23:48:22 +00:00
frame := &Frame{
uiManager: uiManager,
}
frame.grid = ui.NewGrid(nil)
frame.messageView = &ui.TextView{"Hello"}
frame.statusBar = &ui.StatusBar{"Test", "Status"}
frame.textEntrySwitch = &ui.ProxyLayout{frame.messageView}
frame.textEntry = &ui.TextEntry{}
// Build the UI frame
statusLayout := &ui.VertLinearLayout{}
statusLayout.Append(frame.statusBar)
statusLayout.Append(frame.textEntrySwitch)
frame.clientArea = &ui.RelativeLayout{Client: frame.grid, South: statusLayout}
return frame
}
// Returns the root component of the frame
func (frame *Frame) RootComponent() ui.UiComponent {
2017-08-25 23:48:22 +00:00
return frame.clientArea
}
2015-03-24 02:12:40 +00:00
// Sets the current model of the frame
func (frame *Frame) SetModel(model ui.GridModel) {
2017-08-25 23:48:22 +00:00
frame.grid.SetModel(model)
2015-03-24 02:12:40 +00:00
}
// Returns the grid component
func (frame *Frame) Grid() *ui.Grid {
2017-08-25 23:48:22 +00:00
return frame.grid
}
// Enter the specific mode.
func (frame *Frame) enterMode(mode Mode) {
switch mode {
case GridMode:
frame.uiManager.SetFocusedComponent(frame)
case EntryMode:
frame.textEntrySwitch.Component = frame.textEntry
frame.uiManager.SetFocusedComponent(frame.textEntry)
}
}
// Exit the specific mode.
func (frame *Frame) exitMode(mode Mode) {
switch mode {
case EntryMode:
frame.textEntrySwitch.Component = frame.messageView
}
}
2017-08-25 23:48:22 +00:00
func (frame *Frame) setMode(mode Mode) {
frame.exitMode(frame.mode)
frame.mode = mode
frame.enterMode(frame.mode)
}
// Message sets the message view's message
func (frame *Frame) Message(s string) {
frame.messageView.Text = s
}
// Prompt the user for input. This switches the mode to entry mode.
func (frame *Frame) Prompt(prompt string, callback func(res string)) {
frame.textEntry.Prompt = prompt
frame.textEntry.SetValue("")
frame.textEntry.OnCancel = frame.exitEntryMode
2017-08-25 23:48:22 +00:00
frame.textEntry.OnEntry = func(res string) {
frame.exitEntryMode()
2017-08-25 23:48:22 +00:00
callback(res)
}
frame.setMode(EntryMode)
}
func (frame *Frame) exitEntryMode() {
frame.textEntry.OnEntry = nil
frame.setMode(GridMode)
}
// Show a message. This will switch the bottom to the messageView and select the frame
func (frame *Frame) ShowMessage(msg string) {
2017-08-25 23:48:22 +00:00
frame.messageView.Text = msg
frame.textEntrySwitch.Component = frame.messageView
//frame.EnterMode(GridMode)
}
// Shows the value of the currently select grid cell
func (frame *Frame) ShowCellValue() {
2017-08-25 23:48:22 +00:00
displayValue := frame.grid.CurrentCellDisplayValue()
frame.ShowMessage(displayValue)
}
// Handle the main grid input as this is the "component" that handles command input.
func (frame *Frame) KeyPressed(key rune, mod int) {
2017-08-25 23:48:22 +00:00
if frame.Session != nil {
frame.Session.KeyPressed(key, mod)
}
}