ted/frame.go

153 lines
3.4 KiB
Go
Raw Normal View History

package main
import (
2018-09-01 02:02:01 +00:00
"github.com/lmika/ted/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)
2018-09-01 02:02:01 +00:00
frame.messageView = &ui.TextView{""}
frame.statusBar = &ui.StatusBar{"Test", ""}
2017-08-25 23:48:22 +00:00
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:
2018-09-01 02:02:01 +00:00
frame.statusBar.Left = frame.Session.Source.String()
2017-08-25 23:48:22 +00:00
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
}
func (frame *Frame) Error(err error) {
if err != nil {
frame.messageView.Text = err.Error()
}
}
type PromptOptions struct {
Prompt string
InitialValue string
}
2017-08-25 23:48:22 +00:00
// Prompt the user for input. This switches the mode to entry mode.
func (frame *Frame) Prompt(options PromptOptions, callback func(res string) error) {
frame.textEntry.Prompt = options.Prompt
frame.textEntry.SetValue(options.InitialValue)
2017-08-25 23:48:22 +00:00
frame.textEntry.OnCancel = frame.exitEntryMode
2017-08-25 23:48:22 +00:00
frame.textEntry.OnEntry = func(res string) {
frame.exitEntryMode()
if err := callback(res); err != nil {
frame.Error(err)
}
2017-08-25 23:48:22 +00:00
}
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)
}
}