ted/ui/manager.go

89 lines
1.8 KiB
Go
Raw Normal View History

// The UI manager. This manages the components that make up the UI and dispatches
// events.
package ui
// The UI manager
type Ui struct {
2017-08-25 23:48:22 +00:00
// The root component
rootComponent UiComponent
focusedComponent FocusableComponent
2017-08-25 23:48:22 +00:00
drawContext *DrawContext
driver Driver
shutdown bool
}
// Creates a new UI context. This also initializes the UI state.
// Returns the context and an error.
func NewUI() (*Ui, error) {
2017-08-25 23:48:22 +00:00
driver := &TermboxDriver{}
err := driver.Init()
2017-08-25 23:48:22 +00:00
if err != nil {
return nil, err
}
2017-08-25 23:48:22 +00:00
drawContext := &DrawContext{driver: driver}
ui := &Ui{drawContext: drawContext, driver: driver}
2017-08-25 23:48:22 +00:00
return ui, nil
}
// Closes the UI context.
func (ui *Ui) Close() {
2017-08-25 23:48:22 +00:00
ui.driver.Close()
}
// Sets the root component
func (ui *Ui) SetRootComponent(comp UiComponent) {
2017-08-25 23:48:22 +00:00
ui.rootComponent = comp
ui.Remeasure()
}
// Sets the focused component
func (ui *Ui) SetFocusedComponent(newFocused FocusableComponent) {
2017-08-25 23:48:22 +00:00
ui.focusedComponent = newFocused
}
// Remeasures the UI
func (ui *Ui) Remeasure() {
2017-08-25 23:48:22 +00:00
ui.drawContext.X = 0
ui.drawContext.Y = 0
ui.drawContext.W, ui.drawContext.H = ui.driver.Size()
2017-08-25 23:48:22 +00:00
ui.rootComponent.Remeasure(ui.drawContext.W, ui.drawContext.H)
}
// Redraws the UI.
func (ui *Ui) Redraw() {
2017-08-25 23:48:22 +00:00
ui.Remeasure()
2017-08-25 23:48:22 +00:00
ui.rootComponent.Redraw(ui.drawContext)
ui.driver.Sync()
}
2017-08-25 23:48:22 +00:00
// Quit indicates to the UI that it should shutdown
func (ui *Ui) Shutdown() {
ui.shutdown = true
}
// Enter the UI loop
func (ui *Ui) Loop() {
2017-08-25 23:48:22 +00:00
for !ui.shutdown {
ui.Redraw()
event := ui.driver.WaitForEvent()
// TODO: If the event is a key-press, do something.
if event.Type == EventKeyPress {
if ui.focusedComponent != nil {
ui.focusedComponent.KeyPressed(event.Ch, event.Par)
}
} else if event.Type == EventResize {
// HACK: Find another way to refresh the size of the screen to prevent a full redraw.
ui.driver.Sync()
}
}
}