From f028121cb8cad3a1167701693bd0583a41b60ffa Mon Sep 17 00:00:00 2001 From: stk Date: Sun, 12 Mar 2023 09:49:34 +0100 Subject: [PATCH] Make focus reporting an opt-in feature, like mouse reporting --- _demos/mouse.go | 1 + screen.go | 6 ++++++ simulation.go | 6 ++++++ tscreen.go | 19 ++++++++++++++++++- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/_demos/mouse.go b/_demos/mouse.go index 7b41238..1047678 100644 --- a/_demos/mouse.go +++ b/_demos/mouse.go @@ -129,6 +129,7 @@ func main() { s.SetStyle(defStyle) s.EnableMouse() s.EnablePaste() + s.EnableFocus() s.Clear() posfmt := "Mouse: %d, %d " diff --git a/screen.go b/screen.go index 5598a8c..bd35a89 100644 --- a/screen.go +++ b/screen.go @@ -139,6 +139,12 @@ type Screen interface { // DisablePaste disables bracketed paste mode. DisablePaste() + // EnableFocus enables reporting of focus events, if your terminal supports it. + EnableFocus() + + // DisableFocus disables reporting of focus events. + DisableFocus() + // HasMouse returns true if the terminal (apparently) supports a // mouse. Note that the return value of true doesn't guarantee that // a mouse/pointing device is present; a false return definitely diff --git a/simulation.go b/simulation.go index 9ad6131..b18b664 100644 --- a/simulation.go +++ b/simulation.go @@ -325,6 +325,12 @@ func (s *simscreen) DisablePaste() { s.paste = false } +func (s *simscreen) EnableFocus() { +} + +func (s *simscreen) DisableFocus() { +} + func (s *simscreen) Size() (int, int) { s.Lock() w, h := s.back.Size() diff --git a/tscreen.go b/tscreen.go index 63492d7..1228632 100644 --- a/tscreen.go +++ b/tscreen.go @@ -163,6 +163,7 @@ type tScreen struct { wg sync.WaitGroup mouseFlags MouseFlags pasteEnabled bool + focusEnabled bool sync.Mutex } @@ -1056,6 +1057,20 @@ func (t *tScreen) enablePasting(on bool) { } } +func (t *tScreen) EnableFocus() { + t.Lock() + t.focusEnabled = true + t.enableFocusReporting() + t.Unlock() +} + +func (t *tScreen) DisableFocus() { + t.Lock() + t.focusEnabled = false + t.disableFocusReporting() + t.Unlock() +} + func (t *tScreen) enableFocusReporting() { if t.enableFocus != "" { t.TPuts(t.enableFocus) @@ -1866,7 +1881,9 @@ func (t *tScreen) engage() error { t.stopQ = stopQ t.enableMouse(t.mouseFlags) t.enablePasting(t.pasteEnabled) - t.enableFocusReporting() + if t.focusEnabled { + t.enableFocusReporting() + } ti := t.ti t.TPuts(ti.EnterCA)