feat: add mouse capture toggle keybinding with 15s auto-restore countdown

This commit is contained in:
hunchulchoi 2026-06-04 17:45:01 +09:00
parent 9e4735d48a
commit 540d275fdc
2 changed files with 91 additions and 0 deletions

View file

@ -2,6 +2,8 @@ package gui
import (
"math"
"sync"
"time"
"github.com/jesseduffield/gocui"
)
@ -111,3 +113,84 @@ func (gui *Gui) handleMainClick() error {
return gui.switchFocus(gui.Views.Main)
}
var (
mouseTimerMutex sync.Mutex
mouseTimer *time.Timer
mouseTicker *time.Ticker
mouseTicksLeft int
)
func (gui *Gui) handleToggleMouse() error {
mouseTimerMutex.Lock()
defer mouseTimerMutex.Unlock()
if gui.g.Mouse {
// Disable Mouse
gui.g.Mouse = false
gocui.Screen.DisableMouse()
if mouseTimer != nil {
mouseTimer.Stop()
}
if mouseTicker != nil {
mouseTicker.Stop()
}
mouseTicksLeft = 15
mouseTimer = time.AfterFunc(15*time.Second, func() {
gui.g.Update(func(g *gocui.Gui) error {
mouseTimerMutex.Lock()
defer mouseTimerMutex.Unlock()
if !gui.g.Mouse {
gui.g.Mouse = true
gocui.Screen.EnableMouse()
if mouseTicker != nil {
mouseTicker.Stop()
}
_ = gui.renderString(gui.g, "information", gui.getInformationContent())
}
return nil
})
})
mouseTicker = time.NewTicker(1 * time.Second)
go func() {
for range mouseTicker.C {
gui.g.Update(func(g *gocui.Gui) error {
mouseTimerMutex.Lock()
defer mouseTimerMutex.Unlock()
if gui.g.Mouse {
return nil
}
mouseTicksLeft--
if mouseTicksLeft <= 0 {
if mouseTicker != nil {
mouseTicker.Stop()
}
return nil
}
_ = gui.renderString(gui.g, "information", gui.getInformationContent())
return nil
})
}
}()
} else {
// Enable Mouse manually
gui.g.Mouse = true
gocui.Screen.EnableMouse()
if mouseTimer != nil {
mouseTimer.Stop()
}
if mouseTicker != nil {
mouseTicker.Stop()
}
}
_ = gui.renderString(gui.g, "information", gui.getInformationContent())
return nil
}

View file

@ -1,6 +1,7 @@
package gui
import (
"fmt"
"os"
"github.com/fatih/color"
@ -195,6 +196,13 @@ func (gui *Gui) setInitialViewContent() error {
func (gui *Gui) getInformationContent() string {
informationStr := gui.Config.Version
mouseStatus := "Mouse: ON"
if !gui.g.Mouse {
mouseStatus = fmt.Sprintf("Mouse: OFF (%ds left)", mouseTicksLeft)
}
informationStr = mouseStatus + " | " + informationStr
if !gui.g.Mouse {
return informationStr
}