Add SetVisible and GetVisible to all widgets

This commit is contained in:
Trevor Slocum 2020-10-19 19:05:03 -07:00
parent 5a360ad87e
commit d7f1e2ab2a
22 changed files with 122 additions and 18 deletions

View File

@ -3,6 +3,7 @@ v1.5.1 (WIP)
- Add Slider
- Add TabbedPanels
- Add Application.GetScreen and Application.GetScreenSize
- Add SetVisible and GetVisible to all widgets
- Add TextView.SetBytes and TextView.GetBytes
- Add TableCell.SetBytes, TableCell.GetBytes and TableCell.GetText
- Fix List.Transform not calling handler set via SetChangedFunc

15
FORK.md
View File

@ -42,6 +42,21 @@ tview [is not thread-safe](https://godoc.org/github.com/rivo/tview#hdr-Concurren
tview [blocks until the queued function returns](https://github.com/rivo/tview/blob/fe3052019536251fd145835dbaa225b33b7d3088/application.go#L510).
## `Primitive` has two additional fields, `SetVisible` and `GetVisible`
Widgets embedding `Box` require the addition of the following at the beginning
of their `Draw` routine to handle visibility changes.
```go
func (w *Widget) Draw(screen tcell.Screen) {
if !w.GetVisible() {
return
}
// ...
}
```
## Setting a primitive's background color to `tcell.ColorDefault` does not result in transparency
Call [Box.SetBackgroundTransparent](https://docs.rocketnine.space/gitlab.com/tslocum/cview/#Box.SetBackgroundTransparent)

26
box.go
View File

@ -16,6 +16,9 @@ type Box struct {
// The inner rect reserved for the box's content.
innerX, innerY, innerWidth, innerHeight int
// Whether or not the box is visible.
visible bool
// Border padding.
paddingTop, paddingBottom, paddingLeft, paddingRight int
@ -79,6 +82,7 @@ func NewBox() *Box {
width: 15,
height: 10,
innerX: -1, // Mark as uninitialized.
visible: true,
backgroundColor: Styles.PrimitiveBackgroundColor,
borderColor: Styles.BorderColor,
titleColor: Styles.TitleColor,
@ -163,6 +167,22 @@ func (b *Box) SetRect(x, y, width, height int) {
b.innerX = -1 // Mark inner rect as uninitialized.
}
// SetVisible sets the flag indicating whether or not the box is visible.
func (b *Box) SetVisible(v bool) {
b.l.Lock()
defer b.l.Unlock()
b.visible = v
}
// GetVisible returns a value indicating whether or not the box is visible.
func (b *Box) GetVisible() bool {
b.l.RLock()
defer b.l.RUnlock()
return b.visible
}
// SetDrawFunc sets a callback function which is invoked after the box primitive
// has been drawn. This allows you to add a more individual style to the box
// (and all primitives which extend it).
@ -398,6 +418,12 @@ func (b *Box) SetTitleAlign(align int) {
func (b *Box) Draw(screen tcell.Screen) {
b.l.Lock()
// Don't draw anything if the box is hidden
if !b.visible {
b.l.Unlock()
return
}
// Don't draw anything if there is no space.
if b.width <= 0 || b.height <= 0 {
b.l.Unlock()

View File

@ -112,6 +112,10 @@ func (b *Button) SetBlurFunc(handler func(key tcell.Key)) {
// Draw draws this primitive onto the screen.
func (b *Button) Draw(screen tcell.Screen) {
if !b.GetVisible() {
return
}
b.Lock()
defer b.Unlock()

View File

@ -239,6 +239,10 @@ func (c *CheckBox) SetFinishedFunc(handler func(key tcell.Key)) {
// Draw draws this primitive onto the screen.
func (c *CheckBox) Draw(screen tcell.Screen) {
if !c.GetVisible() {
return
}
c.Box.Draw(screen)
c.Lock()

View File

@ -155,6 +155,10 @@ func (f *Flex) ResizeItem(p Primitive, fixedSize, proportion int) {
// Draw draws this primitive onto the screen.
func (f *Flex) Draw(screen tcell.Screen) {
if !f.GetVisible() {
return
}
f.Box.Draw(screen)
f.Lock()

View File

@ -641,6 +641,10 @@ func (f *Form) getAttributes() *FormItemAttributes {
// Draw draws this primitive onto the screen.
func (f *Form) Draw(screen tcell.Screen) {
if !f.GetVisible() {
return
}
f.Box.Draw(screen)
f.Lock()

View File

@ -90,6 +90,10 @@ func (f *Frame) SetBorders(top, bottom, header, footer, left, right int) {
// Draw draws this primitive onto the screen.
func (f *Frame) Draw(screen tcell.Screen) {
if !f.GetVisible() {
return
}
f.Box.Draw(screen)
f.Lock()

View File

@ -331,6 +331,10 @@ func (g *Grid) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
// Draw draws this primitive onto the screen.
func (g *Grid) Draw(screen tcell.Screen) {
if !g.GetVisible() {
return
}
g.Box.Draw(screen)
g.Lock()

View File

@ -537,6 +537,10 @@ func (i *InputField) SetFinishedFunc(handler func(key tcell.Key)) {
// Draw draws this primitive onto the screen.
func (i *InputField) Draw(screen tcell.Screen) {
if !i.GetVisible() {
return
}
i.Box.Draw(screen)
i.Lock()

View File

@ -853,6 +853,10 @@ func (l *List) updateOffset() {
// Draw draws this primitive onto the screen.
func (l *List) Draw(screen tcell.Screen) {
if !l.GetVisible() {
return
}
l.Box.Draw(screen)
hasFocus := l.GetFocusable().HasFocus()

View File

@ -189,6 +189,10 @@ func (m *Modal) HasFocus() bool {
// Draw draws this primitive onto the screen.
func (m *Modal) Draw(screen tcell.Screen) {
if !m.GetVisible() {
return
}
formItemCount := m.form.GetFormItemCount()
m.Lock()

View File

@ -333,6 +333,10 @@ func (p *Panels) Focus(delegate func(p Primitive)) {
// Draw draws this primitive onto the screen.
func (p *Panels) Draw(screen tcell.Screen) {
if !p.GetVisible() {
return
}
p.Box.Draw(screen)
p.Lock()

View File

@ -16,6 +16,12 @@ type Primitive interface {
// SetRect sets a new position of the primitive.
SetRect(x, y, width, height int)
// GetVisible returns whether or not the primitive is visible.
GetVisible() bool
// GetVisible sets whether or not the primitive is visible.
SetVisible(visibility bool)
// InputHandler returns a handler which receives key events when it has focus.
// It is called by the Application class.
//

View File

@ -150,6 +150,10 @@ func (p *ProgressBar) Complete() bool {
// Draw draws this primitive onto the screen.
func (p *ProgressBar) Draw(screen tcell.Screen) {
if !p.GetVisible() {
return
}
p.Box.Draw(screen)
p.Lock()

View File

@ -198,6 +198,10 @@ func (s *Slider) SetFinishedFunc(handler func(key tcell.Key)) {
// Draw draws this primitive onto the screen.
func (s *Slider) Draw(screen tcell.Screen) {
if !s.GetVisible() {
return
}
s.Box.Draw(screen)
hasFocus := s.GetFocusable().HasFocus()

View File

@ -320,6 +320,10 @@ func (t *TabbedPanels) updateAll() {
// Draw draws this primitive onto the screen.
func (t *TabbedPanels) Draw(screen tcell.Screen) {
if !t.GetVisible() {
return
}
t.Box.Draw(screen)
_, _, t.width, _ = t.GetInnerRect()

View File

@ -804,6 +804,10 @@ func (t *Table) Sort(column int, descending bool) {
// Draw draws this primitive onto the screen.
func (t *Table) Draw(screen tcell.Screen) {
if !t.GetVisible() {
return
}
t.Box.Draw(screen)
t.Lock()

View File

@ -953,6 +953,10 @@ func (t *TextView) reindexBuffer(width int) {
// Draw draws this primitive onto the screen.
func (t *TextView) Draw(screen tcell.Screen) {
if !t.GetVisible() {
return
}
t.Box.Draw(screen)
t.Lock()

View File

@ -740,6 +740,10 @@ func (t *TreeView) process() {
// Draw draws this primitive onto the screen.
func (t *TreeView) Draw(screen tcell.Screen) {
if !t.GetVisible() {
return
}
t.Box.Draw(screen)
t.Lock()

View File

@ -16,7 +16,6 @@ type Window struct {
x, y int
width, height int
fullscreen bool
hidden bool
dragX, dragY int
dragWX, dragWY int
@ -36,21 +35,6 @@ func NewWindow(primitive Primitive) *Window {
return w
}
// Show the window.
func (w *Window) Show() {
w.hidden = false
}
// Hide the window.
func (w *Window) Hide() {
w.hidden = true
}
// Visible returns whether or not the window is visible.
func (w *Window) Visible() bool {
return !w.hidden
}
// SetPosition sets the position of the window.
func (w *Window) SetPosition(x, y int) {
w.Lock()
@ -111,6 +95,10 @@ func (w *Window) HasFocus() bool {
// Draw draws this primitive onto the screen.
func (w *Window) Draw(screen tcell.Screen) {
if !w.GetVisible() {
return
}
w.RLock()
defer w.RUnlock()

View File

@ -66,6 +66,10 @@ func (wm *WindowManager) HasFocus() bool {
// Draw draws this primitive onto the screen.
func (wm *WindowManager) Draw(screen tcell.Screen) {
if !wm.GetVisible() {
return
}
wm.RLock()
defer wm.RUnlock()
@ -73,7 +77,7 @@ func (wm *WindowManager) Draw(screen tcell.Screen) {
var hasFullScreen bool
for _, w := range wm.windows {
if !w.fullscreen || !w.Visible() {
if !w.fullscreen || !w.GetVisible() {
continue
}
@ -88,7 +92,7 @@ func (wm *WindowManager) Draw(screen tcell.Screen) {
}
for _, w := range wm.windows {
if !w.Visible() {
if !w.GetVisible() {
continue
}
w.SetBorder(true)