Fix focus event not triggered in certain cases

This commit is contained in:
Junegunn Choi 2024-01-07 15:43:17 +09:00
parent b92a843c5f
commit e47dc758c9
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 12 additions and 5 deletions

View File

@ -251,6 +251,7 @@ type Terminal struct {
borderWidth int borderWidth int
count int count int
progress int progress int
hasFocusActions bool
hasLoadActions bool hasLoadActions bool
triggerLoad bool triggerLoad bool
reading bool reading bool
@ -730,6 +731,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
ellipsis: opts.Ellipsis, ellipsis: opts.Ellipsis,
ansi: opts.Ansi, ansi: opts.Ansi,
tabstop: opts.Tabstop, tabstop: opts.Tabstop,
hasFocusActions: false,
hasLoadActions: false, hasLoadActions: false,
triggerLoad: false, triggerLoad: false,
reading: true, reading: true,
@ -757,7 +759,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
killChan: make(chan int), killChan: make(chan int),
serverInputChan: make(chan []*action, 10), serverInputChan: make(chan []*action, 10),
serverOutputChan: make(chan string), serverOutputChan: make(chan string),
eventChan: make(chan tui.Event, 3), // load / zero|one | GetChar eventChan: make(chan tui.Event, 4), // (load + zero|one) | (focus) | (GetChar)
tui: renderer, tui: renderer,
initFunc: func() { renderer.Init() }, initFunc: func() { renderer.Init() },
executing: util.NewAtomicBool(false), executing: util.NewAtomicBool(false),
@ -801,6 +803,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
} }
} }
_, t.hasFocusActions = t.keymap[tui.Focus.AsEvent()]
_, t.hasLoadActions = t.keymap[tui.Load.AsEvent()] _, t.hasLoadActions = t.keymap[tui.Load.AsEvent()]
if t.listenAddr != nil { if t.listenAddr != nil {
@ -3070,9 +3073,9 @@ func (t *Terminal) Loop() {
t.track = trackDisabled t.track = trackDisabled
t.printInfo() t.printInfo()
} }
if onFocus, prs := t.keymap[tui.Focus.AsEvent()]; prs && focusChanged && currentIndex != t.lastFocus { if t.hasFocusActions && focusChanged && currentIndex != t.lastFocus {
t.lastFocus = focusedIndex t.lastFocus = currentIndex
t.serverInputChan <- onFocus t.eventChan <- tui.Focus.AsEvent()
} }
if focusChanged || version != t.version { if focusChanged || version != t.version {
version = t.version version = t.version
@ -3186,7 +3189,7 @@ func (t *Terminal) Loop() {
} }
select { select {
case event = <-t.eventChan: case event = <-t.eventChan:
needBarrier = !event.Is(tui.Load, tui.One, tui.Zero) needBarrier = !event.Is(tui.Load, tui.Focus, tui.One, tui.Zero)
case serverActions := <-t.serverInputChan: case serverActions := <-t.serverInputChan:
event = tui.Invalid.AsEvent() event = tui.Invalid.AsEvent()
if t.listenAddr == nil || t.listenAddr.IsLocal() || t.listenUnsafe { if t.listenAddr == nil || t.listenAddr.IsLocal() || t.listenUnsafe {

View File

@ -2711,6 +2711,10 @@ class TestGoFZF < TestBase
tmux.until { |lines| assert_includes(lines[-1], '[[2]]') } tmux.until { |lines| assert_includes(lines[-1], '[[2]]') }
tmux.send_keys :X tmux.send_keys :X
tmux.until { |lines| assert_includes(lines[-1], '[[]]') } tmux.until { |lines| assert_includes(lines[-1], '[[]]') }
tmux.send_keys :BSpace
tmux.until { |lines| assert_includes(lines[-1], '[[1]]') }
tmux.send_keys :X
tmux.until { |lines| assert_includes(lines[-1], '[[]]') }
tmux.send_keys '?' tmux.send_keys '?'
tmux.send_keys :BSpace tmux.send_keys :BSpace
tmux.until { |lines| assert_equal 100, lines.match_count } tmux.until { |lines| assert_equal 100, lines.match_count }