Skip to content

Commit

Permalink
Make focus reporting an opt-in feature, like mouse reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
stk committed Jun 4, 2023
1 parent 37ad113 commit 8fc93f2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions _demos/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func main() {
s.SetStyle(defStyle)
s.EnableMouse()
s.EnablePaste()
s.EnableFocus()
s.Clear()

posfmt := "Mouse: %d, %d "
Expand Down
6 changes: 6 additions & 0 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 18 additions & 1 deletion tscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ type tScreen struct {
wg sync.WaitGroup
mouseFlags MouseFlags
pasteEnabled bool
focusEnabled bool

sync.Mutex
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1864,7 +1879,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)
Expand Down

0 comments on commit 8fc93f2

Please sign in to comment.