Merge pull request #90 from dorileo/control-destroy

Control destroy
This commit is contained in:
Vladimir Markelov 2018-03-26 21:01:47 -07:00 committed by GitHub
commit 8d3cd97d8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 71 additions and 5 deletions

View File

@ -79,6 +79,7 @@ control should keep its original size.
*/
func CreateBarChart(parent Control, w, h int, scale int) *BarChart {
c := new(BarChart)
c.BaseControl = NewBaseControl()
if w == AutoSize {
w = 10

View File

@ -3,12 +3,14 @@ package clui
import (
term "github.com/nsf/termbox-go"
"sync"
"sync/atomic"
)
// BaseControl is a base for all visible controls.
// Every new control must inherit it or implement
// the same set of methods
type BaseControl struct {
refID int64
title string
x, y int
width, height int
@ -31,6 +33,22 @@ type BaseControl struct {
mtx sync.RWMutex
}
var (
globalRefId int64
)
func nextRefId() int64 {
return atomic.AddInt64(&globalRefId, 1)
}
func NewBaseControl() BaseControl {
return BaseControl{refID: nextRefId()}
}
func (c *BaseControl) RefID() int64 {
return c.refID
}
func (c *BaseControl) Title() string {
return c.title
}
@ -524,3 +542,25 @@ func (c *BaseControl) SetActiveTextColor(clr term.Attribute) {
func (c *BaseControl) SetActiveBackColor(clr term.Attribute) {
c.bgActive = clr
}
func (c *BaseControl) removeChild(control Control) {
children := []Control{}
for _, child := range c.children {
if child.RefID() == control.RefID() {
continue
}
children = append(children, child)
}
c.children = nil
for _, child := range children {
c.AddChild(child)
}
}
// Destroy removes an object from its parental chain
func (c *BaseControl) Destroy() {
c.parent.removeChild(c)
}

View File

@ -31,6 +31,7 @@ control should keep its original size.
*/
func CreateButton(parent Control, width, height int, title string, scale int) *Button {
b := new(Button)
b.BaseControl = NewBaseControl()
b.parent = parent
b.align = AlignCenter

View File

@ -28,6 +28,7 @@ CheckBox state can be changed using mouse or pressing space on keyboard while th
*/
func CreateCheckBox(parent Control, width int, title string, scale int) *CheckBox {
c := new(CheckBox)
c.BaseControl = NewBaseControl()
c.parent = parent
if width == AutoSize {

View File

@ -147,4 +147,14 @@ type Control interface {
// that the control do not want or cannot process the event and the caller sends
// the event to the control parent
ProcessEvent(ev Event) bool
// RefID returns the controls internal reference id
RefID() int64
// removeChild removes a child from a container
// It's used to "destroy" controls whenever a control is no longer used
// by the user
removeChild(control Control)
// Destroy is the public interface to remove an object from its parental chain
// it implies this control will stop receiving events and will not be drawn nor
// will impact on other objects position and size calculation
Destroy()
}

View File

@ -44,6 +44,7 @@ type EditField struct {
// control should keep its original size.
func CreateEditField(parent Control, width int, text string, scale int) *EditField {
e := new(EditField)
e.BaseControl = NewBaseControl()
e.onChange = nil
e.SetTitle(text)
e.SetEnabled(true)

View File

@ -40,6 +40,7 @@ type EditField struct {
// control should keep its original size.
func CreateEditField(parent Control, width int, text string, scale int) *EditField {
e := new(EditField)
e.BaseControl = NewBaseControl()
e.onChange = nil
e.SetTitle(text)
e.SetEnabled(true)

View File

@ -28,6 +28,7 @@ control should keep its original size.
*/
func CreateFrame(parent Control, width, height int, bs BorderStyle, scale int) *Frame {
f := new(Frame)
f.BaseControl = NewBaseControl()
if width == AutoSize {
width = 5

View File

@ -29,6 +29,7 @@ control should keep its original size.
*/
func CreateLabel(parent Control, w, h int, title string, scale int) *Label {
c := new(Label)
c.BaseControl = NewBaseControl()
if w == AutoSize {
w = xs.Len(title)

View File

@ -38,6 +38,7 @@ control should keep its original size.
*/
func CreateListBox(parent Control, width, height int, scale int) *ListBox {
l := new(ListBox)
l.BaseControl = NewBaseControl()
if height == AutoSize {
height = 3

View File

@ -32,6 +32,7 @@ control should keep its original size.
*/
func CreateProgressBar(parent Control, width, height int, scale int) *ProgressBar {
b := new(ProgressBar)
b.BaseControl = NewBaseControl()
if height == AutoSize {
height = 1

View File

@ -26,6 +26,7 @@ control should keep its original size.
*/
func CreateRadio(parent Control, width int, title string, scale int) *Radio {
c := new(Radio)
c.BaseControl = NewBaseControl()
if width == AutoSize {
width = xs.Len(title) + 4

View File

@ -45,6 +45,7 @@ control should keep its original size.
*/
func CreateSparkChart(parent Control, w, h int, scale int) *SparkChart {
c := new(SparkChart)
c.BaseControl = NewBaseControl()
if w == AutoSize {
w = 10

View File

@ -127,6 +127,7 @@ control should keep its original size.
*/
func CreateTableView(parent Control, width, height int, scale int) *TableView {
l := new(TableView)
l.BaseControl = NewBaseControl()
if height == AutoSize {
height = 3

View File

@ -17,6 +17,7 @@ type TextReader struct {
func CreateTextReader(parent Control, width, height int, scale int) *TextReader {
l := new(TextReader)
l.BaseControl = NewBaseControl()
if height == AutoSize {
height = 10
@ -260,10 +261,10 @@ func (l *TextReader) TopLine() int {
func (l *TextReader) SetTopLine(top int) {
if top < l.lineCount {
l.topLine = top
l.topLine = top
if l.onPositionChanged != nil {
l.onPositionChanged(l.topLine, l.lineCount)
}
}
if l.onPositionChanged != nil {
l.onPositionChanged(l.topLine, l.lineCount)
}
}
}

View File

@ -46,6 +46,7 @@ control should keep its original size.
*/
func CreateTextView(parent Control, width, height int, scale int) *TextView {
l := new(TextView)
l.BaseControl = NewBaseControl()
if height == AutoSize {
height = 3

View File

@ -27,6 +27,8 @@ type Window struct {
func CreateWindow(x, y, w, h int, title string) *Window {
wnd := new(Window)
wnd.BaseControl = NewBaseControl()
if w == AutoSize || w < 1 || w > 1000 {
w = 10
}