Browse Source

Improve Button cursor positioning

textarea
Trevor Slocum 6 months ago
parent
commit
b9d448d0f4
  1. 16
      button.go
  2. 4
      demos/presentation/main.go
  3. 4
      inputfield.go
  4. 4
      slider.go

16
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)
}
_, 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)
}
Print(screen, b.label, x, y, width, AlignCenter, labelColor)
}
}

4
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

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

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

Loading…
Cancel
Save