Add Modal.SetButtonsAlign and Modal.SetTextAlign

Also, do not customize Modal window styling by default. Use GetForm and GetFrame to customize.

Resolves #71.
This commit is contained in:
Trevor Slocum 2021-07-17 12:41:44 -07:00
parent 3b49c99661
commit 605d0123c1
4 changed files with 28 additions and 8 deletions

View file

@ -1,5 +1,7 @@
v1.5.7 (WIP)
- Add Modal.SetButtonsAlign and Modal.SetTextAlign
- Fix TextView.GetRegionText error when text contains color tags
- Do not customize Modal window styling by default (use GetForm and GetFrame to customize)
v1.5.6 (2020-07-08)
- Add TrueColorTags option and do not use TrueColor tag values by default

2
go.mod
View file

@ -4,7 +4,7 @@ go 1.12
require (
code.rocketnine.space/tslocum/cbind v0.1.5
github.com/gdamore/tcell/v2 v2.3.11
github.com/gdamore/tcell/v2 v2.4.0
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-runewidth v0.0.13
github.com/rivo/uniseg v0.2.0

4
go.sum
View file

@ -3,8 +3,8 @@ code.rocketnine.space/tslocum/cbind v0.1.5/go.mod h1:LtfqJTzM7qhg88nAvNhx+VnTjZ0
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.2.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
github.com/gdamore/tcell/v2 v2.3.11 h1:ECO6WqHGbKZ3HrSL7bG/zArMCmLaNr5vcjjMVnLHpzc=
github.com/gdamore/tcell/v2 v2.3.11/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
github.com/gdamore/tcell/v2 v2.4.0 h1:W6dxJEmaxYvhICFoTY3WrLLEXsQ11SaFnKGVEXW57KM=
github.com/gdamore/tcell/v2 v2.4.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=

View file

@ -27,6 +27,9 @@ type Modal struct {
// The text color.
textColor tcell.Color
// The text alignment.
textAlign int
// The optional callback for when the user clicked one of the buttons. It
// receives the index of the clicked button and the button's label.
done func(buttonIndex int, buttonLabel string)
@ -39,13 +42,11 @@ func NewModal() *Modal {
m := &Modal{
Box: NewBox(),
textColor: Styles.PrimaryTextColor,
textAlign: AlignCenter,
}
m.form = NewForm()
m.form.SetButtonsAlign(AlignCenter)
m.form.SetButtonBackgroundColor(Styles.PrimitiveBackgroundColor)
m.form.SetButtonTextColor(Styles.PrimaryTextColor)
m.form.SetBackgroundColor(Styles.ContrastBackgroundColor)
m.form.SetPadding(0, 0, 0, 0)
m.form.SetCancelFunc(func() {
if m.done != nil {
@ -56,7 +57,6 @@ func NewModal() *Modal {
m.frame = NewFrame(m.form)
m.frame.SetBorder(true)
m.frame.SetBorders(0, 0, 1, 0, 0, 0)
m.frame.SetBackgroundColor(Styles.ContrastBackgroundColor)
m.frame.SetPadding(1, 1, 1, 1)
m.focus = m
@ -96,6 +96,15 @@ func (m *Modal) SetButtonTextColor(color tcell.Color) {
m.form.SetButtonTextColor(color)
}
// SetButtonsAlign sets the horizontal alignment of the buttons. This must be
// either AlignLeft, AlignCenter (the default), or AlignRight.
func (m *Modal) SetButtonsAlign(align int) {
m.Lock()
defer m.Unlock()
m.form.SetButtonsAlign(align)
}
// SetDoneFunc sets a handler which is called when one of the buttons was
// pressed. It receives the index of the button as well as its label text. The
// handler is also called when the user presses the Escape key. The index will
@ -117,6 +126,15 @@ func (m *Modal) SetText(text string) {
m.text = text
}
// SetTextAlign sets the horizontal alignment of the text. This must be either
// AlignLeft, AlignCenter (the default), or AlignRight.
func (m *Modal) SetTextAlign(align int) {
m.Lock()
defer m.Unlock()
m.textAlign = align
}
// GetForm returns the Form embedded in the window. The returned Form may be
// modified to include additional elements (e.g. AddInputField, AddFormItem).
func (m *Modal) GetForm() *Form {
@ -215,7 +233,7 @@ func (m *Modal) Draw(screen tcell.Screen) {
m.frame.Clear()
lines := WordWrap(m.text, width)
for _, line := range lines {
m.frame.AddText(line, true, AlignCenter, m.textColor)
m.frame.AddText(line, true, m.textAlign, m.textColor)
}
// Set the Modal's position and size.