Improve Button cursor positioning

This commit is contained in:
Trevor Slocum 2021-06-05 10:54:52 -07:00
parent 454d759a42
commit b9d448d0f4
4 changed files with 18 additions and 12 deletions

View File

@ -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)
}
}
}

View File

@ -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

View File

@ -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,

View File

@ -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,