start of command palette stuff; cleaned up _some_ of the gui component semantics...

still kind of messy though, need to hack away at it on my main pc before i continue
anything properly.
This commit is contained in:
Felix Angell 2018-04-16 20:10:23 +01:00
parent 8b3ec8dda9
commit cececb945b
7 changed files with 75 additions and 30 deletions

View File

@ -689,7 +689,9 @@ func (b *Buffer) HandleEvent(evt strife.StrifeEvent) {
} }
func (b *Buffer) OnUpdate() bool { func (b *Buffer) OnUpdate() bool {
b.HasFocus = true if !b.HasFocus {
return false
}
prev_x := b.curs.x prev_x := b.curs.x
prev_y := b.curs.y prev_y := b.curs.y

View File

@ -1,6 +1,8 @@
package gui package gui
func CloseBuffer(b *Buffer) bool { func CloseBuffer(b *Buffer) bool {
// FIXME
// no parent. this can happen sometimes // no parent. this can happen sometimes
// for example if we're trying to close // for example if we're trying to close
// a buffer illegal, e.g. a palette has // a buffer illegal, e.g. a palette has

View File

@ -1,12 +1,25 @@
package gui package gui
import rope "github.com/felixangell/go-rope"
// FIXME
func DeleteLine(b *Buffer) bool { func DeleteLine(b *Buffer) bool {
if b.curs.y == 0 {
b.contents[b.curs.y] = new(rope.Rope)
return false
}
if b.curs.y >= len(b.contents) { if b.curs.y >= len(b.contents) {
return false return false
} }
prevLineLen := b.contents[b.curs.y].Len() prevLineLen := b.contents[b.curs.y].Len()
b.contents = remove(b.contents[:], b.curs.y) b.contents = remove(b.contents, b.curs.y)
if b.curs.y >= len(b.contents) {
b.moveUp()
return false
}
currLineLen := b.contents[b.curs.y].Len() currLineLen := b.contents[b.curs.y].Len()

View File

@ -1,41 +1,38 @@
package gui package gui
import ( import (
"github.com/veandco/go-sdl2/sdl" "github.com/felixangell/phi-editor/cfg"
"github.com/felixangell/strife"
) )
type CommandBuffer struct {
*Buffer
}
func (b *CommandBuffer) processActionKey() {
// TODO
// b.Buffer.processActionKey()
}
type CommandPalette struct { type CommandPalette struct {
BaseComponent BaseComponent
buffer *CommandBuffer buff *Buffer
} }
func NewCommandPalette() *CommandPalette { func NewCommandPalette(conf *cfg.TomlConfig) *CommandPalette {
palette := &CommandPalette{} palette := &CommandPalette{
palette.buffer = &CommandBuffer{ buff: NewBuffer(conf, nil, 0),
Buffer: NewBuffer(nil, nil, 0),
} }
palette.AddComponent(palette.buffer) palette.buff.HasFocus = true
return palette return palette
} }
func (p *CommandPalette) OnDispose() {} func (b *CommandPalette) OnInit() {
func (p *CommandPalette) OnInit() {
}
func (c *CommandPalette) OnUpdate() {
} }
func (c *CommandPalette) OnRender(ctx *sdl.Renderer) { func (b *CommandPalette) OnUpdate() bool {
return b.buff.OnUpdate()
}
func (b *CommandPalette) OnRender(ctx *strife.Renderer) {
ctx.SetColor(strife.Red)
ctx.Rect(b.x, b.y, b.w, b.h, strife.Fill)
b.buff.OnRender(ctx)
}
func (b *CommandPalette) OnDispose() {
} }

0
gui/untitled Normal file
View File

View File

@ -8,16 +8,28 @@ import (
type View struct { type View struct {
BaseComponent BaseComponent
conf *cfg.TomlConfig conf *cfg.TomlConfig
buffers map[int]*Buffer
focusedBuff int focusedBuff int
} }
func NewView(width, height int, conf *cfg.TomlConfig) *View { func NewView(width, height int, conf *cfg.TomlConfig) *View {
view := &View{conf: conf} view := &View{
conf: conf,
buffers: map[int]*Buffer{},
}
view.Translate(width, height) view.Translate(width, height)
view.Resize(width, height) view.Resize(width, height)
view.focusPalette()
return view return view
} }
func (n *View) focusPalette() {
// clear focus from buffers
for _, buff := range n.buffers {
buff.HasFocus = false
}
}
func sign(dir int) int { func sign(dir int) int {
if dir > 0 { if dir > 0 {
return 1 return 1
@ -29,7 +41,7 @@ func sign(dir int) int {
func (n *View) ChangeFocus(dir int) { func (n *View) ChangeFocus(dir int) {
// remove focus from the curr buffer. // remove focus from the curr buffer.
if buf := n.components[n.focusedBuff].(*Buffer); buf != nil { if buf, ok := n.buffers[n.focusedBuff]; ok {
buf.HasFocus = false buf.HasFocus = false
} }
@ -49,10 +61,17 @@ func (n *View) OnInit() {
} }
func (n *View) OnUpdate() bool { func (n *View) OnUpdate() bool {
if buff := n.components[n.focusedBuff]; buff != nil { dirty := false
return Update(buff) for _, comp := range n.components {
if comp == nil {
continue
}
if Update(comp) {
dirty = true
}
} }
return false return dirty
} }
func (n *View) OnRender(ctx *strife.Renderer) {} func (n *View) OnRender(ctx *strife.Renderer) {}
@ -60,7 +79,12 @@ func (n *View) OnRender(ctx *strife.Renderer) {}
func (n *View) OnDispose() {} func (n *View) OnDispose() {}
func (n *View) AddBuffer() *Buffer { func (n *View) AddBuffer() *Buffer {
if buf, ok := n.buffers[n.focusedBuff]; ok {
buf.HasFocus = false
}
c := NewBuffer(n.conf, n, n.NumComponents()) c := NewBuffer(n.conf, n, n.NumComponents())
c.HasFocus = true
// work out the size of the buffer and set it // work out the size of the buffer and set it
// note that we +1 the components because // note that we +1 the components because
@ -79,6 +103,7 @@ func (n *View) AddBuffer() *Buffer {
} }
n.AddComponent(c) n.AddComponent(c)
n.buffers[c.index] = c
n.focusedBuff = c.index n.focusedBuff = c.index
// translate all the components accordingly. // translate all the components accordingly.

View File

@ -51,6 +51,12 @@ func (n *PhiEditor) init(cfg *cfg.TomlConfig) {
mainView.AddBuffer().OpenFile(tempFile.Name()) mainView.AddBuffer().OpenFile(tempFile.Name())
} }
{
palette := gui.NewCommandPalette(cfg)
palette.Resize(1280/3, 72)
// mainView.AddComponent(palette)
}
n.AddComponent(mainView) n.AddComponent(mainView)
// TODO put me somewhere else: // TODO put me somewhere else: