diff --git a/cfg/config.go b/cfg/config.go index e54d9cc..6c71299 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -62,6 +62,7 @@ type RenderConfig struct { Aliased bool Accelerated bool Throttle_Cpu_Usage bool + Always_Render bool } // todo make this more extendable... diff --git a/cfg/linuxconfig.go b/cfg/linuxconfig.go index 6fe77f0..8e346f3 100644 --- a/cfg/linuxconfig.go +++ b/cfg/linuxconfig.go @@ -12,6 +12,7 @@ highlight_line = true aliased = true accelerated = true throttle_cpu_usage = true +always_render = false [theme] background = 0x002649 diff --git a/cfg/macconfig.go b/cfg/macconfig.go index d847c18..5616ef2 100644 --- a/cfg/macconfig.go +++ b/cfg/macconfig.go @@ -12,6 +12,7 @@ highlight_line = true aliased = true accelerated = true throttle_cpu_usage = true +always_render = false [theme] background = 0x002649 diff --git a/cfg/windowsconfig.go b/cfg/windowsconfig.go index 0632479..7667ad7 100644 --- a/cfg/windowsconfig.go +++ b/cfg/windowsconfig.go @@ -12,6 +12,7 @@ highlight_line = true aliased = true accelerated = true throttle_cpu_usage = true +always_render = false [theme] background = 0x002649 diff --git a/gui/buffer.go b/gui/buffer.go index 780e8d8..2318d85 100644 --- a/gui/buffer.go +++ b/gui/buffer.go @@ -29,6 +29,7 @@ type camera struct { type Buffer struct { BaseComponent + HasFocus bool index int parent *View font *strife.Font @@ -307,17 +308,39 @@ func (b *Buffer) moveRight() { } func (b *Buffer) moveUp() { - if b.curs.x > 0 { + if b.curs.y > 0 { b.curs.move(0, -1) } } func (b *Buffer) moveDown() { - if b.curs.x < len(b.contents) { + if b.curs.y < len(b.contents) { b.curs.move(0, 1) } } +func (b *Buffer) swapLineUp() bool { + if b.curs.y > 0 { + currLine := b.contents[b.curs.y] + prevLine := b.contents[b.curs.y-1] + b.contents[b.curs.y-1] = currLine + b.contents[b.curs.y] = prevLine + b.moveUp() + } + return true +} + +func (b *Buffer) swapLineDown() bool { + if b.curs.y < len(b.contents) { + currLine := b.contents[b.curs.y] + nextLine := b.contents[b.curs.y+1] + b.contents[b.curs.y+1] = currLine + b.contents[b.curs.y] = nextLine + b.moveDown() + } + return true +} + func (b *Buffer) scrollUp() { // TODO move the cursor down 45 lines // IF the buffer exceeds the window size. @@ -399,6 +422,11 @@ func (b *Buffer) processActionKey(key int) bool { case sdl.K_RIGHT: currLineLength := b.contents[b.curs.y].Len() + if CONTROL_DOWN && b.parent != nil { + b.parent.ChangeFocus(1) + return true + } + if SUPER_DOWN { for b.curs.x < currLineLength { b.curs.move(1, 0) @@ -427,6 +455,11 @@ func (b *Buffer) processActionKey(key int) bool { b.moveRight() return true case sdl.K_LEFT: + if CONTROL_DOWN && b.parent != nil { + b.parent.ChangeFocus(-1) + return true + } + if SUPER_DOWN { // TODO go to the nearest \t // if no \t (i.e. start of line) go to @@ -459,15 +492,12 @@ func (b *Buffer) processActionKey(key int) bool { b.moveLeft() return true case sdl.K_UP: - if SUPER_DOWN { - // go to the start of the file + if ALT_DOWN { + return b.swapLineUp() } - // as well as normally moving - // upwards, this moves the cursor - // to the start of the line - if ALT_DOWN { - b.curs.gotoStart() + if SUPER_DOWN { + // go to the start of the file } if b.curs.y > 0 { @@ -481,22 +511,14 @@ func (b *Buffer) processActionKey(key int) bool { } return true case sdl.K_DOWN: + if ALT_DOWN { + return b.swapLineDown() + } + if SUPER_DOWN { // go to the end of the file } - // FIXME this doesnt work properly - if ALT_DOWN { - currLineLength := b.contents[b.curs.y].Len() - // in sublime this goes to the end - // of every line - for b.curs.x < currLineLength { - b.curs.move(1, 0) - } - } - - log.Println(b.curs.y, " .. ", b.curs.y-b.cam.y) - if b.curs.y < len(b.contents)-1 { offs := 0 nextLineLen := b.contents[b.curs.y+1].Len() @@ -589,6 +611,8 @@ func (b *Buffer) makeTab() string { } func (b *Buffer) OnUpdate() bool { + b.HasFocus = true + prev_x := b.curs.x prev_y := b.curs.y @@ -645,13 +669,13 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { ctx.SetColor(strife.HexRGB(b.cfg.Theme.Background)) ctx.Rect(b.x, b.y, b.w, b.h, strife.Fill) - if b.cfg.Editor.Highlight_Line { + if b.cfg.Editor.Highlight_Line && b.HasFocus { ctx.SetColor(strife.Black) // highlight_line_col? ctx.Rect(rx, (ry + b.curs.ry*last_h), b.w, last_h, strife.Fill) } // render the ol' cursor - if should_draw && b.cfg.Cursor.Draw { + if should_draw && b.cfg.Cursor.Draw && b.HasFocus { cursorWidth := b.cfg.Cursor.GetCaretWidth() if cursorWidth == -1 { cursorWidth = last_w @@ -711,7 +735,8 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { // if we're currently over a character then set // the font colour to something else - if b.curs.x+1 == x_col && b.curs.y == y_col && should_draw { + // ONLY SET THE COLOUR IF WE HAVE FOCUS ALSO! + if b.HasFocus && b.curs.x+1 == x_col && b.curs.y == y_col && should_draw { ctx.SetColor(strife.HexRGB(b.cfg.Theme.Cursor_Invert)) } diff --git a/gui/close_buffer.go b/gui/close_buffer.go index da7b28c..15b7d0e 100644 --- a/gui/close_buffer.go +++ b/gui/close_buffer.go @@ -9,6 +9,8 @@ func CloseBuffer(b *Buffer) bool { return false } + // remove focus + b.HasFocus = false b.parent.DeleteComponent(b.index) // we need to re-calculate the sizes of everything @@ -25,8 +27,16 @@ func CloseBuffer(b *Buffer) bool { bufferWidth = b.parent.w } + // TODO track buffer visit history on a stack + // when we remove a buffer we pop it from the stack + // then we can focus on the top of the stack instead. + // for now let's just focus on the most recently + // added buffer + // translate all the components accordingly. i := 0 + + var lastBuffer *Buffer for _, p := range b.parent.components { if p == nil { continue @@ -36,7 +46,9 @@ func CloseBuffer(b *Buffer) bool { p.SetPosition(bufferWidth*i, 0) i = i + 1 + lastBuffer = p.(*Buffer) } + lastBuffer.HasFocus = true return true } diff --git a/gui/component.go b/gui/component.go index 42886b5..20ad83c 100644 --- a/gui/component.go +++ b/gui/component.go @@ -100,7 +100,6 @@ func Update(c Component) bool { if child == nil { continue } - dirty := Update(child) if dirty { needsRender = true diff --git a/gui/view.go b/gui/view.go index cd2151b..13ae0b6 100644 --- a/gui/view.go +++ b/gui/view.go @@ -7,7 +7,8 @@ import ( type View struct { BaseComponent - conf *cfg.TomlConfig + conf *cfg.TomlConfig + focusedBuff int } func NewView(width, height int, conf *cfg.TomlConfig) *View { @@ -17,10 +18,40 @@ func NewView(width, height int, conf *cfg.TomlConfig) *View { 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 } @@ -47,12 +78,8 @@ func (n *View) AddBuffer() *Buffer { bufferWidth = n.w } - // setup and add the panel for the buffer - panel := NewPanel(n.inputHandler) - c.SetInputHandler(n.inputHandler) - - panel.AddComponent(c) - n.AddComponent(panel) + n.AddComponent(c) + n.focusedBuff = c.index // translate all the components accordingly. for i, p := range n.components { diff --git a/main.go b/main.go index 02cdf31..223058b 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,7 @@ func (n *PhiEditor) dispose() { func (n *PhiEditor) update() bool { needsRender := false for _, comp := range n.GetComponents() { - dirty := gui.Update(comp) + dirty := comp.OnUpdate() if dirty { needsRender = true } @@ -135,7 +135,7 @@ func main() { break } - if editor.update() { + if editor.update() || config.Render.Always_Render { ctx.Clear() editor.render(ctx) diff --git a/phi-editor b/phi-editor index 043997f..b14fae0 100755 Binary files a/phi-editor and b/phi-editor differ