Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button disable and label alignment option, label attrs setter #824

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
4 changes: 4 additions & 0 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ type Box struct {
mouseCapture func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse)
}

func (b *Box) SetDontClear(dontClear bool) {
b.dontClear = dontClear
}

// NewBox returns a Box without a border.
func NewBox() *Box {
b := &Box{
Expand Down
73 changes: 70 additions & 3 deletions button.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type Button struct {
// key is provided indicating which key was pressed to leave (tab or
// backtab).
exit func(tcell.Key)

// Label's alignment, by default AlignCenter
align int
}

// NewButton returns a new input field.
Expand All @@ -41,9 +44,11 @@ func NewButton(label string) *Button {
return &Button{
Box: box,
text: label,
align: AlignCenter,
disabled: false,
style: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.PrimaryTextColor),
activatedStyle: tcell.StyleDefault.Background(Styles.PrimaryTextColor).Foreground(Styles.InverseTextColor),
disabledStyle: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.ContrastSecondaryTextColor),
disabledStyle: tcell.StyleDefault.Background(Styles.DisabledBackgroundColor).Foreground(Styles.DisabledTextColor),
}
}

Expand All @@ -64,6 +69,13 @@ func (b *Button) SetLabelColor(color tcell.Color) *Button {
return b
}

// SetLabelAlign sets the label alignment within the button. This must be
// either AlignLeft, AlignCenter, or AlignRight.
func (b *Button) SetLabelAlign(align int) *Button {
b.align = align
return b
}

// SetStyle sets the style of the button used when it is not focused.
func (b *Button) SetStyle(style tcell.Style) *Button {
b.style = style
Expand All @@ -77,6 +89,13 @@ func (b *Button) SetLabelColorActivated(color tcell.Color) *Button {
return b
}

// SetBackgroundColor sets the background color of the button text when
// the button is not in focus. Overrides embeddedBox method.
func (b *Button) SetBackgroundColor(color tcell.Color) *Button {
b.style = b.style.Background(color)
return b
}

// SetBackgroundColorActivated sets the background color of the button text when
// the button is in focus.
func (b *Button) SetBackgroundColorActivated(color tcell.Color) *Button {
Expand Down Expand Up @@ -111,6 +130,47 @@ func (b *Button) IsDisabled() bool {
return b.disabled
}

// SetLabelColorDisabled sets the color of the button text when the button is
// disabled.
func (b *Button) SetLabelColorDisabled(color tcell.Color) *Button {
b.disabledStyle = b.disabledStyle.Foreground(color)
return b
}

// SetBackgroundColorDisabled sets the background color of the button text when
// the button is disabled.
func (b *Button) SetBackgroundColorDisabled(color tcell.Color) *Button {
b.disabledStyle = b.disabledStyle.Background(color)
return b
}

// SetStyleAttrs sets the label's style attributes. You can combine
// different attributes using bitmask operations:
//
// button.SetStyleAttrs(tcell.AttrUnderline | tcell.AttrBold)
func (b *Button) SetStyleAttrs(attrs tcell.AttrMask) *Button {
b.style = b.style.Attributes(attrs)
return b
}

// SetActivatedStyleAttrs sets the label's activatedStyle attributes. You can combine
// different attributes using bitmask operations:
//
// button.SetActivatedStyleAttrs(tcell.AttrUnderline | tcell.AttrBold)
func (b *Button) SetActivatedStyleAttrs(attrs tcell.AttrMask) *Button {
b.activatedStyle = b.activatedStyle.Attributes(attrs)
return b
}

// SetDisabledStyleAttrs sets the label's disabledStyle attributes. You can combine
// different attributes using bitmask operations:
//
// button.SetDisabledStyleAttrs(tcell.AttrUnderline | tcell.AttrBold)
func (b *Button) SetDisabledStyleAttrs(attrs tcell.AttrMask) *Button {
b.disabledStyle = b.disabledStyle.Attributes(attrs)
return b
}

// SetSelectedFunc sets a handler which is called when the button was selected.
func (b *Button) SetSelectedFunc(handler func()) *Button {
b.selected = handler
Expand All @@ -129,6 +189,13 @@ func (b *Button) SetExitFunc(handler func(key tcell.Key)) *Button {
return b
}


// SetEnabled sets the flag that, if false, button is available for interactions
func (b *Button) SetEnabled() *Button {
b.disabled = false
return b
}

// Draw draws this primitive onto the screen.
func (b *Button) Draw(screen tcell.Screen) {
// Draw the box.
Expand All @@ -148,14 +215,14 @@ func (b *Button) Draw(screen tcell.Screen) {
b.SetBorderColor(borderColor)
}()
}
b.SetBackgroundColor(backgroundColor)
b.Box.SetBackgroundColor(backgroundColor)
b.Box.DrawForSubclass(screen, b)

// Draw label.
x, y, width, height := b.GetInnerRect()
if width > 0 && height > 0 {
y = y + height/2
printWithStyle(screen, b.text, x, y, 0, width, AlignCenter, style, true)
printWithStyle(screen, b.text, x, y, 0, width, b.align, style, true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/rivo/tview
module github.com/censync/tview

go 1.18

Expand Down
4 changes: 4 additions & 0 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Theme struct {
TertiaryTextColor tcell.Color // Tertiary text (e.g. subtitles, notes).
InverseTextColor tcell.Color // Text on primary-colored backgrounds.
ContrastSecondaryTextColor tcell.Color // Secondary text on ContrastBackgroundColor-colored backgrounds.
DisabledBackgroundColor tcell.Color // Background color for disabled elements
DisabledTextColor tcell.Color // Primary text for disabled elements
}

// Styles defines the theme for applications. The default is for a black
Expand All @@ -31,5 +33,7 @@ var Styles = Theme{
SecondaryTextColor: tcell.ColorYellow,
TertiaryTextColor: tcell.ColorGreen,
InverseTextColor: tcell.ColorBlue,
DisabledBackgroundColor: tcell.ColorDarkSlateGray,
DisabledTextColor: tcell.ColorLightGray,
ContrastSecondaryTextColor: tcell.ColorNavy,
}