phi/gui/view.go
2018-04-13 20:04:10 +01:00

96 lines
1.8 KiB
Go

package gui
import (
"github.com/felixangell/phi-editor/cfg"
"github.com/felixangell/strife"
)
type View struct {
BaseComponent
conf *cfg.TomlConfig
focusedBuff int
}
func NewView(width, height int, conf *cfg.TomlConfig) *View {
view := &View{conf: conf}
view.Translate(width, height)
view.Resize(width, height)
return view
}
func sign(dir int) int {
if dir > 0 {
return 1
} else if dir < 0 {
return -1
}
return 0
}
func (n *View) ChangeFocus(dir int) {
// remove focus from the curr buffer.
if buf := n.components[n.focusedBuff].(*Buffer); buf != nil {
buf.HasFocus = false
}
newIndex := n.focusedBuff + sign(dir)
if newIndex >= n.NumComponents() {
newIndex = 0
} else if newIndex < 0 {
newIndex = n.NumComponents() - 1
}
if buff := n.components[newIndex]; buff != nil {
n.focusedBuff = newIndex
}
}
func (n *View) OnInit() {
}
func (n *View) OnUpdate() bool {
if buff := n.components[n.focusedBuff]; buff != nil {
return Update(buff)
}
return false
}
func (n *View) OnRender(ctx *strife.Renderer) {}
func (n *View) OnDispose() {}
func (n *View) AddBuffer() *Buffer {
c := NewBuffer(n.conf, n, n.NumComponents())
// work out the size of the buffer and set it
// note that we +1 the components because
// we haven't yet added the panel
var bufferWidth int
// NOTE: because we're ADDING a component
// here we add 1 to the components since
// we want to calculate the sizes _after_
// we've added this component.
numComponents := n.NumComponents() + 1
if numComponents > 0 {
bufferWidth = n.w / numComponents
} else {
bufferWidth = n.w
}
n.AddComponent(c)
n.focusedBuff = c.index
// translate all the components accordingly.
for i, p := range n.components {
if p == nil {
continue
}
p.Resize(bufferWidth, n.h)
p.SetPosition(bufferWidth*i, 0)
}
return c
}