diff --git a/gui/buffer.go b/gui/buffer.go index 6414459..e95974b 100644 --- a/gui/buffer.go +++ b/gui/buffer.go @@ -689,7 +689,9 @@ func (b *Buffer) HandleEvent(evt strife.StrifeEvent) { } func (b *Buffer) OnUpdate() bool { - b.HasFocus = true + if !b.HasFocus { + return false + } prev_x := b.curs.x prev_y := b.curs.y diff --git a/gui/close_buffer.go b/gui/close_buffer.go index 84156b5..d1fd364 100644 --- a/gui/close_buffer.go +++ b/gui/close_buffer.go @@ -1,6 +1,8 @@ package gui func CloseBuffer(b *Buffer) bool { + // FIXME + // no parent. this can happen sometimes // for example if we're trying to close // a buffer illegal, e.g. a palette has diff --git a/gui/delete_line.go b/gui/delete_line.go index 40bf1bd..d693ebc 100644 --- a/gui/delete_line.go +++ b/gui/delete_line.go @@ -1,12 +1,25 @@ package gui +import rope "github.com/felixangell/go-rope" + +// FIXME 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) { return false } 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() diff --git a/gui/palette.go b/gui/palette.go index d64326a..60c4509 100644 --- a/gui/palette.go +++ b/gui/palette.go @@ -1,41 +1,38 @@ package gui 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 { BaseComponent - buffer *CommandBuffer + buff *Buffer } -func NewCommandPalette() *CommandPalette { - palette := &CommandPalette{} - palette.buffer = &CommandBuffer{ - Buffer: NewBuffer(nil, nil, 0), +func NewCommandPalette(conf *cfg.TomlConfig) *CommandPalette { + palette := &CommandPalette{ + buff: NewBuffer(conf, nil, 0), } - palette.AddComponent(palette.buffer) + palette.buff.HasFocus = true return palette } -func (p *CommandPalette) OnDispose() {} - -func (p *CommandPalette) OnInit() { -} - -func (c *CommandPalette) OnUpdate() { +func (b *CommandPalette) OnInit() { } -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() { } diff --git a/gui/untitled b/gui/untitled new file mode 100644 index 0000000..e69de29 diff --git a/gui/view.go b/gui/view.go index 13ae0b6..1e0af0e 100644 --- a/gui/view.go +++ b/gui/view.go @@ -8,16 +8,28 @@ import ( type View struct { BaseComponent conf *cfg.TomlConfig + buffers map[int]*Buffer focusedBuff int } 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.Resize(width, height) + view.focusPalette() return view } +func (n *View) focusPalette() { + // clear focus from buffers + for _, buff := range n.buffers { + buff.HasFocus = false + } +} + func sign(dir int) int { if dir > 0 { return 1 @@ -29,7 +41,7 @@ func sign(dir int) int { func (n *View) ChangeFocus(dir int) { // 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 } @@ -49,10 +61,17 @@ func (n *View) OnInit() { } func (n *View) OnUpdate() bool { - if buff := n.components[n.focusedBuff]; buff != nil { - return Update(buff) + dirty := false + 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) {} @@ -60,7 +79,12 @@ func (n *View) OnRender(ctx *strife.Renderer) {} func (n *View) OnDispose() {} func (n *View) AddBuffer() *Buffer { + if buf, ok := n.buffers[n.focusedBuff]; ok { + buf.HasFocus = false + } + c := NewBuffer(n.conf, n, n.NumComponents()) + c.HasFocus = true // work out the size of the buffer and set it // note that we +1 the components because @@ -79,6 +103,7 @@ func (n *View) AddBuffer() *Buffer { } n.AddComponent(c) + n.buffers[c.index] = c n.focusedBuff = c.index // translate all the components accordingly. diff --git a/main.go b/main.go index 10dc91e..8bc8369 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,12 @@ func (n *PhiEditor) init(cfg *cfg.TomlConfig) { mainView.AddBuffer().OpenFile(tempFile.Name()) } + { + palette := gui.NewCommandPalette(cfg) + palette.Resize(1280/3, 72) + // mainView.AddComponent(palette) + } + n.AddComponent(mainView) // TODO put me somewhere else: