2015-01-06 11:34:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2018-09-01 02:02:01 +00:00
|
|
|
"github.com/lmika/ted/ui"
|
2015-01-06 11:34:06 +00:00
|
|
|
)
|
|
|
|
|
|
2017-08-25 23:48:22 +00:00
|
|
|
type Mode int
|
2015-01-06 11:34:06 +00:00
|
|
|
|
|
|
|
|
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
|
2015-01-06 11:34:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2015-01-06 11:34:06 +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
|
|
|
|
|
}
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
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("")
|
|
|
|
|
|
2018-09-01 01:27:34 +00:00
|
|
|
frame.textEntry.OnCancel = frame.exitEntryMode
|
2017-08-25 23:48:22 +00:00
|
|
|
frame.textEntry.OnEntry = func(res string) {
|
2018-09-01 01:27:34 +00:00
|
|
|
frame.exitEntryMode()
|
2017-08-25 23:48:22 +00:00
|
|
|
callback(res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frame.setMode(EntryMode)
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-01 01:27:34 +00:00
|
|
|
func (frame *Frame) exitEntryMode() {
|
|
|
|
|
frame.textEntry.OnEntry = nil
|
|
|
|
|
frame.setMode(GridMode)
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 11:34:06 +00:00
|
|
|
// 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)
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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)
|
2015-01-06 11:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
}
|