From b9d448d0f4b83bc6f93eed6b1fa4b54b713a0f84 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Sat, 5 Jun 2021 10:54:52 -0700 Subject: [PATCH] Improve Button cursor positioning --- button.go | 18 ++++++++++++------ demos/presentation/main.go | 4 ++-- inputfield.go | 4 ++-- slider.go | 4 ++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/button.go b/button.go index 11ac130..e360085 100644 --- a/button.go +++ b/button.go @@ -4,7 +4,6 @@ import ( "sync" "github.com/gdamore/tcell/v2" - "github.com/mattn/go-runewidth" ) // Button is labeled box that triggers an action when selected. @@ -154,12 +153,19 @@ func (b *Button) Draw(screen tcell.Screen) { labelColor := b.labelColor if b.focus.HasFocus() { labelColor = b.labelColorFocused - // Draw cursor. - if b.cursorRune != 0 { - Print(screen, []byte(string(b.cursorRune)), x+width-(width-runewidth.StringWidth(string(b.label)))/2+1, y, width, AlignLeft, labelColor) - } } - Print(screen, b.label, x, y, width, AlignCenter, labelColor) + _, pw := Print(screen, b.label, x, y, width, AlignCenter, labelColor) + + // Draw cursor. + if b.focus.HasFocus() && b.cursorRune != 0 { + cursorX := x + int(float64(width)/2+float64(pw)/2) + if cursorX > x+width-1 { + cursorX = x + width - 1 + } else if cursorX < x+width { + cursorX++ + } + Print(screen, []byte(string(b.cursorRune)), cursorX, y, width, AlignLeft, labelColor) + } } } diff --git a/demos/presentation/main.go b/demos/presentation/main.go index 13f6a0e..aa80a3e 100644 --- a/demos/presentation/main.go +++ b/demos/presentation/main.go @@ -25,12 +25,12 @@ import ( ) const ( - appInfo = "Next slide: Ctrl-N Previous: Ctrl-P Exit: Ctrl-C (Navigate with your keyboard or mouse)" + appInfo = "Next slide: Ctrl-N Previous: Ctrl-P Exit: Ctrl-C (Navigate with your keyboard and mouse)" listInfo = "Next item: J, Down Previous item: K, Up Open context menu: Alt+Enter" textViewInfo = "Scroll down: J, Down, PageDown Scroll up: K, Up, PageUp" sliderInfo = "Decrease: H, J, Left, Down Increase: K, L, Right, Up" formInfo = "Next field: Tab Previous field: Shift+Tab Select: Enter" - windowInfo = "Windows may be dragged an resized using the mouse." + windowInfo = "Windows may be dragged and resized using the mouse." ) // Slide is a function which returns the slide's title, any applicable diff --git a/inputfield.go b/inputfield.go index 717089b..ccf35a7 100644 --- a/inputfield.go +++ b/inputfield.go @@ -144,8 +144,8 @@ func NewInputField() *InputField { return &InputField{ Box: NewBox(), labelColor: Styles.SecondaryTextColor, - fieldBackgroundColor: Styles.ContrastBackgroundColor, - fieldBackgroundColorFocused: Styles.MoreContrastBackgroundColor, + fieldBackgroundColor: Styles.MoreContrastBackgroundColor, + fieldBackgroundColorFocused: Styles.ContrastBackgroundColor, fieldTextColor: Styles.PrimaryTextColor, fieldTextColorFocused: Styles.PrimaryTextColor, placeholderTextColor: Styles.ContrastSecondaryTextColor, diff --git a/slider.go b/slider.go index f72a8a8..663bf3e 100644 --- a/slider.go +++ b/slider.go @@ -64,8 +64,8 @@ func NewSlider() *Slider { ProgressBar: NewProgressBar(), increment: 10, labelColor: Styles.SecondaryTextColor, - fieldBackgroundColor: Styles.ContrastBackgroundColor, - fieldBackgroundColorFocused: Styles.MoreContrastBackgroundColor, + fieldBackgroundColor: Styles.MoreContrastBackgroundColor, + fieldBackgroundColorFocused: Styles.ContrastBackgroundColor, fieldTextColor: Styles.PrimaryTextColor, labelColorFocused: ColorUnset, fieldTextColorFocused: ColorUnset,