Skip to content

feat: hide cursor when process loses focus #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/opencode-ai/opencode/internal/logging"
"github.com/opencode-ai/opencode/internal/pubsub"
"github.com/opencode-ai/opencode/internal/tui"
"github.com/opencode-ai/opencode/internal/tui/util"
"github.com/opencode-ai/opencode/internal/version"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -125,6 +126,14 @@ to assist developers in writing, debugging, and understanding code directly from
// Setup the subscriptions, this will send services events to the TUI
ch, cancelSubs := setupSubscriptions(app, ctx)

// Start focus tracking
focusTracker := util.NewFocusTracker(program)
if err := focusTracker.Start(ctx); err != nil {
logging.Warn("Failed to start focus tracking", "error", err)
} else {
logging.Info("Started focus tracking")
}

// Create a context for the TUI message handler
tuiCtx, tuiCancel := context.WithCancel(ctx)
var tuiWg sync.WaitGroup
Expand Down
14 changes: 14 additions & 0 deletions internal/tui/components/chat/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"unicode"

"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -156,6 +157,15 @@ func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.session = msg
}
return m, nil
case util.FocusMsg:
if msg.Focused {
// Show blinking cursor when pane is focused
cmd = m.textarea.Cursor.SetMode(cursor.CursorBlink)
} else {
// Hide cursor when pane is not focused
cmd = m.textarea.Cursor.SetMode(cursor.CursorHide)
}
return m, cmd
case dialog.AttachmentAddedMsg:
if len(m.attachments) >= maxAttachments {
logging.ErrorPersist(fmt.Sprintf("cannot add more than %d images", maxAttachments))
Expand Down Expand Up @@ -306,6 +316,10 @@ func CreateTextArea(existing *textarea.Model) textarea.Model {
}

ta.Focus()

// Set initial cursor mode to blinking (default to focused)
ta.Cursor.SetMode(cursor.CursorBlink)

return ta
}

Expand Down
11 changes: 11 additions & 0 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
s, _ := a.status.Update(msg)
a.status = s.(core.StatusCmp)

// Focus tracking
case util.FocusMsg:
// Forward focus messages to the current page
a.pages[a.currentPage], cmd = a.pages[a.currentPage].Update(msg)
cmds = append(cmds, cmd)

// Permission
case pubsub.Event[permission.PermissionRequest]:
a.showPermissions = true
Expand Down Expand Up @@ -443,6 +449,11 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return a, nil

case tea.KeyMsg:
// Handle focus events from terminal escape sequences
if ok, cmd := util.ParseFocusMessage(msg); ok {
return a, util.CmdHandler(cmd)
}

// If multi-arguments dialog is open, let it handle the key press first
if a.showMultiArgumentsDialog {
args, cmd := a.multiArgumentsDialog.Update(msg)
Expand Down
64 changes: 64 additions & 0 deletions internal/tui/util/focus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package util

import (
"context"
"fmt"

tea "github.com/charmbracelet/bubbletea"
)

// FocusTracker manages terminal focus tracking using ANSI escape sequences
type FocusTracker struct {
program *tea.Program
focused bool
}

// NewFocusTracker creates a new focus tracker
func NewFocusTracker(program *tea.Program) *FocusTracker {
return &FocusTracker{
program: program,
focused: true, // Default to focused
}
}

// Start enables focus tracking and starts monitoring
func (ft *FocusTracker) Start(ctx context.Context) error {
// Enable focus tracking with ANSI escape sequence
fmt.Print("\x1b[?1004h")

// Start a goroutine to handle focus events
go func() {
<-ctx.Done()
// Disable focus tracking when context is cancelled
fmt.Print("\x1b[?1004l")
}()

return nil
}

// HandleFocusEvent processes focus in/out events from terminal
func (ft *FocusTracker) HandleFocusEvent(focused bool) {
if ft.focused != focused {
ft.focused = focused
if ft.program != nil {
ft.program.Send(FocusMsg{Focused: focused})
}
}
}

// IsFocused returns the current focus state
func (ft *FocusTracker) IsFocused() bool {
return ft.focused
}

// ParseFocusMessage takes an input key event and checks if it matches
// the ANSI escape codes for focus in or out.
func ParseFocusMessage(input tea.KeyMsg) (bool, FocusMsg) {
switch input.String() {
case "\x1b[I": // Focus in
return true, FocusMsg{Focused: true}
case "\x1b[O": // Focus out
return true, FocusMsg{Focused: false}
}
return false, FocusMsg{}
}
3 changes: 3 additions & 0 deletions internal/tui/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type (
TTL time.Duration
}
ClearStatusMsg struct{}
FocusMsg struct {
Focused bool
}
)

func Clamp(v, low, high int) int {
Expand Down