command palette fixes

This commit is contained in:
Felix Angell 2018-04-29 18:32:03 +01:00
parent d092aa78db
commit ac351ec9db
7 changed files with 98 additions and 59 deletions

View File

@ -4,10 +4,10 @@ import "os"
type BufferAction struct {
name string
proc func(*Buffer) bool
proc func(*View) bool
}
func NewBufferAction(name string, proc func(*Buffer) bool) BufferAction {
func NewBufferAction(name string, proc func(*View) bool) BufferAction {
return BufferAction{
name: name,
proc: proc,
@ -20,7 +20,7 @@ var actions = map[string]BufferAction{
"close_buffer": NewBufferAction("close_buffer", CloseBuffer),
"paste": NewBufferAction("paste", Paste),
"show_palette": NewBufferAction("show_palette", ShowPalette),
"exit": NewBufferAction("exit", func(*Buffer) bool {
"exit": NewBufferAction("exit", func(*View) bool {
// TODO do this properly lol
os.Exit(0)
return false

View File

@ -222,6 +222,25 @@ var altAlternative = map[rune]rune{
'\\': '«',
}
func (b *Buffer) deleteLine() {
if len(b.contents) > 1 {
b.contents = remove(b.contents, b.curs.y)
} else {
// we are on the first line
// and there is nothing else to delete
// so we just clear the line
b.contents[b.curs.y] = new(rope.Rope)
}
if b.curs.y >= len(b.contents) {
if b.curs.y > 0 {
b.moveUp()
}
}
b.moveToEndOfLine()
}
func (b *Buffer) processTextInput(r rune) bool {
if ALT_DOWN && r == '\t' {
// nop, we dont want to
@ -245,23 +264,11 @@ func (b *Buffer) processTextInput(r rune) bool {
}
}
if CONTROL_DOWN {
actionName, actionExists := cfg.Shortcuts.Controls[string(unicode.ToLower(r))]
if actionExists {
if action, ok := actions[actionName]; ok {
log.Println("Executing action '" + actionName + "'")
return action.proc(b)
}
} else {
log.Println("warning, unimplemented shortcut ctrl +", string(unicode.ToLower(r)), actionName)
}
}
if SUPER_DOWN {
actionName, actionExists := cfg.Shortcuts.Supers[string(unicode.ToLower(r))]
if actionExists {
if action, ok := actions[actionName]; ok {
return action.proc(b)
return action.proc(b.parent)
}
} else {
log.Println("warning, unimplemented shortcut ctrl+", unicode.ToLower(r), actionName)
@ -807,10 +814,10 @@ var lastCursorDraw = time.Now()
var renderFlashingCursor = true
func (b *Buffer) OnUpdate() bool {
return b.doUpdate(nil)
return false
}
func (b *Buffer) doUpdate(pred func(r int) bool) bool {
func (b *Buffer) processInput(pred func(r int) bool) bool {
if !b.HasFocus() {
return false
}

View File

@ -1,8 +1,16 @@
package gui
func CloseBuffer(b *Buffer) bool {
view := b.parent
view.ChangeFocus(-1)
view.removeBuffer(b.index)
func CloseBuffer(v *View) bool {
b := v.getCurrentBuff()
if b == nil {
return false
}
if len(v.buffers) > 1 {
v.ChangeFocus(-1)
}
v.removeBuffer(b.index)
return false
}

View File

@ -1,23 +1,11 @@
package gui
import rope "github.com/felixangell/go-rope"
func DeleteLine(b *Buffer) bool {
if len(b.contents) > 1 {
b.contents = remove(b.contents, b.curs.y)
} else {
// we are on the first line
// and there is nothing else to delete
// so we just clear the line
b.contents[b.curs.y] = new(rope.Rope)
func DeleteLine(v *View) bool {
b := v.getCurrentBuff()
if b == nil {
return false
}
if b.curs.y >= len(b.contents) {
if b.curs.y > 0 {
b.moveUp()
}
}
b.moveToEndOfLine()
b.deleteLine()
return true
}

View File

@ -23,6 +23,7 @@ type CommandPalette struct {
buff *Buffer
parentBuff *Buffer
conf *cfg.TomlConfig
parent *View
suggestionIndex int
recentSuggestions *[]suggestion
@ -82,7 +83,8 @@ func NewCommandPalette(conf cfg.TomlConfig, view *View) *CommandPalette {
conf.Editor.Highlight_Line = false
palette := &CommandPalette{
conf: &conf,
conf: &conf,
parent: view,
buff: NewBuffer(&conf, BufferConfig{
conf.Theme.Palette.Background,
conf.Theme.Palette.Foreground,
@ -128,7 +130,7 @@ func (b *CommandPalette) processCommand() {
return
}
action.proc(b.parentBuff)
action.proc(b.parent)
}
func (b *CommandPalette) calculateSuggestions() {
@ -167,7 +169,7 @@ func (b *CommandPalette) scrollSuggestion(dir int) {
}
func (b *CommandPalette) clearInput() {
actions["delete_line"].proc(b.buff)
b.buff.deleteLine()
}
func (b *CommandPalette) setToSuggested() {
@ -215,16 +217,17 @@ func (b *CommandPalette) OnUpdate() bool {
return true
}
fallthrough
b.processCommand()
break
case sdl.K_ESCAPE:
break
}
b.processCommand()
b.parentBuff.parent.hidePalette()
b.parent.hidePalette()
return true
}
return b.buff.doUpdate(override)
return b.buff.processInput(override)
}
func (b *CommandPalette) OnRender(ctx *strife.Renderer) {

View File

@ -7,13 +7,19 @@ import (
"log"
)
func ShowPalette(b *Buffer) bool {
b.parent.UnfocusBuffers()
b.parent.focusPalette(b)
func ShowPalette(v *View) bool {
b := v.getCurrentBuff()
v.UnfocusBuffers()
v.focusPalette(b)
return true
}
func Paste(b *Buffer) bool {
func Paste(v *View) bool {
b := v.getCurrentBuff()
if b == nil {
return false
}
str, err := clipboard.ReadAll()
if err == nil {
@ -32,7 +38,11 @@ func Paste(b *Buffer) bool {
// if the buffer is modified it will be
// re-rendered.
func Save(b *Buffer) bool {
func Save(v *View) bool {
b := v.getCurrentBuff()
if b == nil {
return false
}
var buffer bytes.Buffer
for idx, line := range b.contents {

View File

@ -1,10 +1,11 @@
package gui
import (
"fmt"
"github.com/felixangell/phi/cfg"
"github.com/felixangell/strife"
"github.com/veandco/go-sdl2/sdl"
"log"
"unicode"
)
// View is an array of buffers basically.
@ -39,7 +40,10 @@ func (n *View) hidePalette() {
// set focus to the buffer
// that invoked the cmd palette
p.parentBuff.SetFocus(true)
if p.parentBuff != nil {
p.parentBuff.SetFocus(true)
n.focusedBuff = p.parentBuff.index
}
// remove focus from palette
p.buff.SetFocus(false)
@ -112,13 +116,36 @@ func (n *View) ChangeFocus(dir int) {
}
}
func (n *View) getCurrentBuff() *Buffer {
if buff, ok := n.buffers[n.focusedBuff]; ok {
return buff
}
return nil
}
func (n *View) OnInit() {
}
func (n *View) OnUpdate() bool {
dirty := false
CONTROL_DOWN = strife.KeyPressed(sdl.K_LCTRL) || strife.KeyPressed(sdl.K_RCTRL)
if CONTROL_DOWN && strife.PollKeys() {
r := rune(strife.PopKey())
actionName, actionExists := cfg.Shortcuts.Controls[string(unicode.ToLower(r))]
if actionExists {
if action, ok := actions[actionName]; ok {
log.Println("Executing action '" + actionName + "'")
return action.proc(n)
}
} else {
log.Println("warning, unimplemented shortcut ctrl +", string(unicode.ToLower(r)), actionName)
}
}
if buff, ok := n.buffers[n.focusedBuff]; ok {
buff.processInput(nil)
buff.OnUpdate()
}
@ -128,10 +155,8 @@ func (n *View) OnUpdate() bool {
}
func (n *View) OnRender(ctx *strife.Renderer) {
for idx, buffer := range n.buffers {
for _, buffer := range n.buffers {
buffer.OnRender(ctx)
ctx.String(fmt.Sprintf("idx %d", idx), (buffer.x+buffer.w)-150, (buffer.y+buffer.h)-150)
}
n.commandPalette.OnRender(ctx)
@ -140,9 +165,7 @@ 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.SetFocus(false)
}
n.UnfocusBuffers()
cfg := n.conf
c := NewBuffer(cfg, BufferConfig{