| 1 |
|
| 2 |
n |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package mauview |
| 9 |
|
| 10 |
import ( |
| 11 |
"sync" |
| 12 |
|
| •13 |
"github.com/gdamore/tcell/v2" |
| 14 |
) |
| 15 |
|
| 16 |
type TextField struct { |
| 17 |
sync.Mutex |
| 18 |
*SimpleEventHandler |
| 19 |
text string |
| 20 |
style tcell.Style |
| 21 |
} |
| 22 |
|
| 23 |
func NewTextField() *TextField { |
| 24 |
return &TextField{ |
| 25 |
SimpleEventHandler: &SimpleEventHandler{}, |
| 26 |
|
| 27 |
text: "", |
| 28 |
style: tcell.StyleDefault.Foreground(Styles.PrimaryTextColor), |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func (tf *TextField) SetText(text string) *TextField { |
| 33 |
tf.Lock() |
| 34 |
tf.text = text |
| 35 |
tf.Unlock() |
| 36 |
return tf |
| 37 |
} |
| 38 |
|
| 39 |
func (tf *TextField) SetTextColor(color tcell.Color) *TextField { |
| 40 |
tf.Lock() |
| 41 |
tf.style = tf.style.Foreground(color) |
| 42 |
tf.Unlock() |
| 43 |
return tf |
| 44 |
} |
| 45 |
|
| 46 |
func (tf *TextField) SetBackgroundColor(color tcell.Color) *TextField { |
| 47 |
tf.Lock() |
| 48 |
tf.style = tf.style.Background(color) |
| 49 |
tf.Unlock() |
| 50 |
return tf |
| 51 |
} |
| 52 |
|
| 53 |
func (tf *TextField) SetStyle(style tcell.Style) *TextField { |
| 54 |
tf.Lock() |
| 55 |
tf.style = style |
| 56 |
tf.Unlock() |
| 57 |
return tf |
| 58 |
} |
| 59 |
|
| 60 |
func (tf *TextField) Draw(screen Screen) { |
| 61 |
tf.Lock() |
| 62 |
width, _ := screen.Size() |
| 63 |
screen.SetStyle(tf.style) |
| 64 |
screen.Clear() |
| 65 |
PrintWithStyle(screen, tf.text, 0, 0, width, AlignLeft, tf.style) |
| 66 |
tf.Unlock() |
| 67 |
} |