diff --git a/CHANGELOG b/CHANGELOG index 5ea7307..32419be 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/FORK.md b/FORK.md index 4878c95..087a3e3 100644 --- a/FORK.md +++ b/FORK.md @@ -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) diff --git a/box.go b/box.go index 728734a..3d04042 100644 --- a/box.go +++ b/box.go @@ -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() diff --git a/button.go b/button.go index 955c4f2..4ba6303 100644 --- a/button.go +++ b/button.go @@ -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() diff --git a/checkbox.go b/checkbox.go index 9d15713..8af1e3c 100644 --- a/checkbox.go +++ b/checkbox.go @@ -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() diff --git a/flex.go b/flex.go index 92db2ef..841511c 100644 --- a/flex.go +++ b/flex.go @@ -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() diff --git a/form.go b/form.go index 830d4a5..5250ea0 100644 --- a/form.go +++ b/form.go @@ -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() diff --git a/frame.go b/frame.go index cf0233e..9292513 100644 --- a/frame.go +++ b/frame.go @@ -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() diff --git a/grid.go b/grid.go index c9627ff..7957a30 100644 --- a/grid.go +++ b/grid.go @@ -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() diff --git a/inputfield.go b/inputfield.go index 11763a5..c768095 100644 --- a/inputfield.go +++ b/inputfield.go @@ -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() diff --git a/list.go b/list.go index 2ed0977..17faa7f 100644 --- a/list.go +++ b/list.go @@ -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() diff --git a/modal.go b/modal.go index 4fea670..12ad872 100644 --- a/modal.go +++ b/modal.go @@ -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() diff --git a/panels.go b/panels.go index 812117c..e2d74a3 100644 --- a/panels.go +++ b/panels.go @@ -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() diff --git a/primitive.go b/primitive.go index a12b990..1022b28 100644 --- a/primitive.go +++ b/primitive.go @@ -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. // diff --git a/progressbar.go b/progressbar.go index f0cac0a..eaebb63 100644 --- a/progressbar.go +++ b/progressbar.go @@ -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() diff --git a/slider.go b/slider.go index 7163f80..3143446 100644 --- a/slider.go +++ b/slider.go @@ -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() diff --git a/tabbedpanels.go b/tabbedpanels.go index a593457..1d84302 100644 --- a/tabbedpanels.go +++ b/tabbedpanels.go @@ -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() diff --git a/table.go b/table.go index f2a060d..b47f1c7 100644 --- a/table.go +++ b/table.go @@ -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() diff --git a/textview.go b/textview.go index e5e1c68..98e2b03 100644 --- a/textview.go +++ b/textview.go @@ -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() diff --git a/treeview.go b/treeview.go index 5c6e3f3..ebdd6fc 100644 --- a/treeview.go +++ b/treeview.go @@ -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() diff --git a/window.go b/window.go index 3cb9f87..6625dcb 100644 --- a/window.go +++ b/window.go @@ -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() diff --git a/windowmanager.go b/windowmanager.go index 742fabf..3b77805 100644 --- a/windowmanager.go +++ b/windowmanager.go @@ -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)