From 6639676b9322a858ca5d51f05ebb14f67d1c1725 Mon Sep 17 00:00:00 2001 From: x88 Date: Mon, 13 Mar 2023 19:25:45 +0300 Subject: [PATCH 1/5] feat: button disable, label alignment option, label attrs setter --- button.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 5 deletions(-) diff --git a/button.go b/button.go index b54c49ea..bbd80685 100644 --- a/button.go +++ b/button.go @@ -19,6 +19,9 @@ type Button struct { // The button's style (when activated). activatedStyle tcell.Style + // The button's style (when disabled) + disabledStyle tcell.Style + // An optional function which is called when the button was selected. selected func() @@ -26,6 +29,12 @@ 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 + + // An optional attribute, when button is disabled + disabled bool } // NewButton returns a new input field. @@ -35,8 +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(tcell.ColorDarkSlateGray).Foreground(tcell.ColorLightGray), } } @@ -57,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 @@ -83,6 +102,53 @@ func (b *Button) SetActivatedStyle(style tcell.Style) *Button { return b } +// 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 +} + +// SetDisabledStyle sets the style of the button used when it is disabled. +func (b *Button) SetDisabledStyle(style tcell.Style) *Button { + b.disabledStyle = style + 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 @@ -101,10 +167,27 @@ func (b *Button) SetExitFunc(handler func(key tcell.Key)) *Button { return b } +// SetDisabled sets the flag that, if true, button is not available for interactions +func (b *Button) SetDisabled() *Button { + b.disabled = true + 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) { + var style tcell.Style // Draw the box. - style := b.style + if !b.disabled { + style = b.style + } else { + style = b.disabledStyle + } _, backgroundColor, _ := style.Decompose() if b.HasFocus() { style = b.activatedStyle @@ -124,7 +207,7 @@ func (b *Button) Draw(screen tcell.Screen) { 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) } } @@ -134,7 +217,7 @@ func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Prim // Process key event. switch key := event.Key(); key { case tcell.KeyEnter: // Selected. - if b.selected != nil { + if b.selected != nil && !b.disabled { b.selected() } case tcell.KeyBacktab, tcell.KeyTab, tcell.KeyEscape: // Leave. No action. @@ -153,11 +236,11 @@ func (b *Button) MouseHandler() func(action MouseAction, event *tcell.EventMouse } // Process mouse event. - if action == MouseLeftDown { + if action == MouseLeftDown && !b.disabled { setFocus(b) consumed = true } else if action == MouseLeftClick { - if b.selected != nil { + if b.selected != nil && !b.disabled { b.selected() } consumed = true From ffdcdafdc15ce8157559f21e8f304a6f9e31d55e Mon Sep 17 00:00:00 2001 From: x88 Date: Mon, 13 Mar 2023 19:55:09 +0300 Subject: [PATCH 2/5] feat: added styles for disabled elements --- button.go | 2 +- styles.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/button.go b/button.go index bbd80685..1cc05a3c 100644 --- a/button.go +++ b/button.go @@ -48,7 +48,7 @@ func NewButton(label string) *Button { disabled: false, style: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.PrimaryTextColor), activatedStyle: tcell.StyleDefault.Background(Styles.PrimaryTextColor).Foreground(Styles.InverseTextColor), - disabledStyle: tcell.StyleDefault.Background(tcell.ColorDarkSlateGray).Foreground(tcell.ColorLightGray), + disabledStyle: tcell.StyleDefault.Background(Styles.DisabledBackgroundColor).Foreground(Styles.DisabledTextColor), } } diff --git a/styles.go b/styles.go index 9f8beae8..c7acd7a7 100644 --- a/styles.go +++ b/styles.go @@ -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 @@ -32,4 +34,6 @@ var Styles = Theme{ TertiaryTextColor: tcell.ColorGreen, InverseTextColor: tcell.ColorBlue, ContrastSecondaryTextColor: tcell.ColorDarkBlue, + DisabledBackgroundColor: tcell.ColorDarkSlateGray, + DisabledTextColor: tcell.ColorLightGray, } From d021dc8325543afef860f29d40a762dc3d509909 Mon Sep 17 00:00:00 2001 From: x88 Date: Tue, 14 Mar 2023 02:08:29 +0300 Subject: [PATCH 3/5] fix: calling SetBackgroundColor from embedded struct Box --- button.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/button.go b/button.go index 1cc05a3c..c5ef9667 100644 --- a/button.go +++ b/button.go @@ -89,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 { @@ -200,7 +207,7 @@ func (b *Button) Draw(screen tcell.Screen) { b.SetBorderColor(borderColor) }() } - b.SetBackgroundColor(backgroundColor) + b.Box.SetBackgroundColor(backgroundColor) b.Box.DrawForSubclass(screen, b) // Draw label. From e1d902e59f82b53f6e5cf1d913e25cd2aba25f1a Mon Sep 17 00:00:00 2001 From: Dmitry M Date: Sun, 30 Jul 2023 13:57:03 +0300 Subject: [PATCH 4/5] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 39a54170..4391b6ce 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/rivo/tview +module github.com/censync/tview go 1.18 From 1c7795b1187d81bcaaee768599d310d17c099828 Mon Sep 17 00:00:00 2001 From: x88 Date: Sun, 17 Sep 2023 19:06:34 +0300 Subject: [PATCH 5/5] added SetDontClear method --- box.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/box.go b/box.go index bd82a6b4..80e76a47 100644 --- a/box.go +++ b/box.go @@ -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{