fixed some focus issues; change focus behaviour is now mostly sound!

in addition, the command palette shows all of the open buffers and you can switch to a buffer using the command palette.
This commit is contained in:
Felix Angell 2018-05-12 20:43:26 +01:00
parent c20675b2a6
commit 8ad459755d
3 changed files with 101 additions and 34 deletions

View File

@ -34,6 +34,11 @@ func NewBufferPane(buff *Buffer) *BufferPane {
var lastWidth int
func (b *BufferPane) SetFocus(focus bool) {
b.Buff.SetFocus(focus)
b.BaseComponent.SetFocus(focus)
}
func (b *BufferPane) renderMetaPanel(ctx *strife.Renderer) {
conf := b.Buff.cfg.Theme.Palette

View File

@ -26,10 +26,17 @@ type CommandPalette struct {
conf *cfg.TomlConfig
parent *View
pathToIndex map[string]int
suggestionIndex int
recentSuggestions *[]suggestion
}
func (p *CommandPalette) SetFocus(focus bool) {
p.buff.SetFocus(focus)
p.BaseComponent.SetFocus(focus)
}
var suggestionBoxHeight, suggestionBoxWidth = 48, 0
type suggestion struct {
@ -123,22 +130,42 @@ func NewCommandPalette(conf cfg.TomlConfig, view *View) *CommandPalette {
}
func (b *CommandPalette) processCommand() {
tokenizedLine := strings.Split(b.buff.contents[0].String(), " ")
command := tokenizedLine[0]
input := b.buff.contents[0].String()
input = strings.TrimSpace(input)
tokenizedLine := strings.Split(input, " ")
log.Println("command palette: ", tokenizedLine)
// command
if strings.Compare(tokenizedLine[0], "!") == 0 {
tokenizedLine := strings.Split(input, " ")
log.Println("COMMAND TING '", input, "', ", tokenizedLine)
command := tokenizedLine[0]
action, exists := actions[command]
if !exists {
log.Println("command palette: ", tokenizedLine)
action, exists := actions[command]
if !exists {
return
}
action.proc(b.parent, tokenizedLine[1:])
return
}
action.proc(b.parent, tokenizedLine[1:])
if index, ok := b.pathToIndex[input]; ok {
b.parent.setFocusTo(index)
}
}
func (b *CommandPalette) calculateSuggestions() {
tokenizedLine := strings.Split(b.buff.contents[0].String(), " ")
command := tokenizedLine[0]
func (b *CommandPalette) calculateCommandSuggestions() {
input := b.buff.contents[0].String()
input = strings.TrimSpace(input)
tokenizedLine := strings.Split(input, " ")
if strings.Compare(tokenizedLine[0], "!") == 0 {
return
}
command := tokenizedLine[1]
if command == "" {
b.recentSuggestions = nil
@ -159,6 +186,47 @@ func (b *CommandPalette) calculateSuggestions() {
b.recentSuggestions = &suggestions
}
func (b *CommandPalette) calculateSuggestions() {
input := b.buff.contents[0].String()
input = strings.TrimSpace(input)
if len(input) == 0 {
return
}
if input[0] == '!' {
b.calculateCommandSuggestions()
return
}
// fill it with the currently open files!
openFiles := make([]string, len(b.parent.buffers))
b.pathToIndex = map[string]int{}
for i, pane := range b.parent.buffers {
path := pane.Buff.filePath
openFiles[i] = path
b.pathToIndex[path] = i
}
ranks := fuzzy.RankFind(input, openFiles)
suggestions := []suggestion{}
for _, r := range ranks {
pane := b.parent.buffers[r.Index]
if pane != nil {
sugg := suggestion{
b,
pane.Buff.filePath,
}
suggestions = append(suggestions, sugg)
}
}
b.recentSuggestions = &suggestions
}
func (b *CommandPalette) scrollSuggestion(dir int) {
if b.recentSuggestions != nil {
b.suggestionIndex += dir

View File

@ -40,13 +40,6 @@ func (n *View) hidePalette() {
p.clearInput()
p.SetFocus(false)
// set focus to the buffer
// that invoked the cmd palette
if p.parentBuff != nil {
p.parentBuff.SetFocus(true)
n.focusedBuff = p.parentBuff.index
}
// remove focus from palette
p.buff.SetFocus(false)
}
@ -55,9 +48,6 @@ func (n *View) focusPalette(buff *Buffer) {
p := n.commandPalette
p.SetFocus(true)
// focus the command palette
p.buff.SetFocus(true)
// remove focus from the buffer
// that invoked the command palette
p.parentBuff = buff
@ -66,7 +56,7 @@ func (n *View) focusPalette(buff *Buffer) {
func (n *View) UnfocusBuffers() {
// clear focus from buffers
for _, buffPane := range n.buffers {
buffPane.Buff.SetFocus(false)
buffPane.SetFocus(false)
}
}
@ -108,6 +98,15 @@ func (n *View) removeBuffer(index int) {
n.ChangeFocus(dir)
}
func (n *View) setFocusTo(index int) {
log.Println("Moving focus from ", n.focusedBuff, " to ", index)
n.UnfocusBuffers()
n.focusedBuff = index
buff := n.getCurrentBuffPane()
buff.SetFocus(true)
}
// FIXME
func (n *View) ChangeFocus(dir int) {
// we cant change focus if there are no
@ -116,27 +115,22 @@ func (n *View) ChangeFocus(dir int) {
return
}
prevBuff := n.buffers[n.focusedBuff]
newFocus := n.focusedBuff
if dir == -1 {
n.focusedBuff--
newFocus--
} else if dir == 1 {
n.focusedBuff++
newFocus++
}
if n.focusedBuff < 0 {
n.focusedBuff = len(n.buffers) - 1
} else if n.focusedBuff >= len(n.buffers) {
n.focusedBuff = 0
if newFocus < 0 {
newFocus = len(n.buffers) - 1
} else if newFocus >= len(n.buffers) {
newFocus = 0
}
if prevBuff != nil {
prevBuff.Buff.SetFocus(false)
}
if buffPane := n.getCurrentBuffPane(); buffPane != nil {
buffPane.Buff.SetFocus(true)
}
n.UnfocusBuffers()
n.setFocusTo(newFocus)
}
func (n *View) getCurrentBuffPane() *BufferPane {