Trigger focus actions synchronously

This commit is contained in:
Junegunn Choi 2023-12-31 15:53:53 +09:00
parent 5da606a9ac
commit 863a12562b
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

View File

@ -288,6 +288,7 @@ type Terminal struct {
executing *util.AtomicBool executing *util.AtomicBool
termSize tui.TermSize termSize tui.TermSize
lastAction actionType lastAction actionType
lastFocus int32
} }
type selectedItem struct { type selectedItem struct {
@ -751,7 +752,8 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
tui: renderer, tui: renderer,
initFunc: func() { renderer.Init() }, initFunc: func() { renderer.Init() },
executing: util.NewAtomicBool(false), executing: util.NewAtomicBool(false),
lastAction: actStart} lastAction: actStart,
lastFocus: minItem.Index()}
t.prompt, t.promptLen = t.parsePrompt(opts.Prompt) t.prompt, t.promptLen = t.parsePrompt(opts.Prompt)
t.pointer, t.pointerLen = t.processTabs([]rune(opts.Pointer), 0) t.pointer, t.pointerLen = t.processTabs([]rune(opts.Pointer), 0)
t.marker, t.markerLen = t.processTabs([]rune(opts.Marker), 0) t.marker, t.markerLen = t.processTabs([]rune(opts.Marker), 0)
@ -2762,6 +2764,13 @@ func (t *Terminal) pwindowSize() tui.TermSize {
return size return size
} }
func (t *Terminal) currentIndex() int32 {
if currentItem := t.currentItem(); currentItem != nil {
return currentItem.Index()
}
return minItem.Index()
}
// Loop is called to start Terminal I/O // Loop is called to start Terminal I/O
func (t *Terminal) Loop() { func (t *Terminal) Loop() {
// prof := profile.Start(profile.ProfilePath("/tmp/")) // prof := profile.Start(profile.ProfilePath("/tmp/"))
@ -3046,17 +3055,14 @@ func (t *Terminal) Loop() {
t.printInfo() t.printInfo()
case reqList: case reqList:
t.printList() t.printList()
var currentIndex int32 = minItem.Index() currentIndex := t.currentIndex()
currentItem := t.currentItem()
if currentItem != nil {
currentIndex = currentItem.Index()
}
focusChanged := focusedIndex != currentIndex focusChanged := focusedIndex != currentIndex
if focusChanged && t.track == trackCurrent { if focusChanged && t.track == trackCurrent {
t.track = trackDisabled t.track = trackDisabled
t.printInfo() t.printInfo()
} }
if onFocus, prs := t.keymap[tui.Focus.AsEvent()]; prs && focusChanged { if onFocus, prs := t.keymap[tui.Focus.AsEvent()]; prs && focusChanged && currentIndex != t.lastFocus {
t.lastFocus = focusedIndex
t.serverInputChan <- onFocus t.serverInputChan <- onFocus
} }
if focusChanged || version != t.version { if focusChanged || version != t.version {
@ -3246,12 +3252,21 @@ func (t *Terminal) Loop() {
} }
var doAction func(*action) bool var doAction func(*action) bool
doActions := func(actions []*action) bool { var doActions func(actions []*action) bool
doActions = func(actions []*action) bool {
currentIndex := t.currentIndex()
for _, action := range actions { for _, action := range actions {
if !doAction(action) { if !doAction(action) {
return false return false
} }
} }
if onFocus, prs := t.keymap[tui.Focus.AsEvent()]; prs {
if newIndex := t.currentIndex(); newIndex != currentIndex {
t.lastFocus = newIndex
return doActions(onFocus)
}
}
return true return true
} }
doAction = func(a *action) bool { doAction = func(a *action) bool {