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 { func CreateBarChart(parent Control, w, h int, scale int) *BarChart {
c := new(BarChart) c := new(BarChart)
c.BaseControl = NewBaseControl()
if w == AutoSize { if w == AutoSize {
w = 10 w = 10

View File

@ -3,12 +3,14 @@ package clui
import ( import (
term "github.com/nsf/termbox-go" term "github.com/nsf/termbox-go"
"sync" "sync"
"sync/atomic"
) )
// BaseControl is a base for all visible controls. // BaseControl is a base for all visible controls.
// Every new control must inherit it or implement // Every new control must inherit it or implement
// the same set of methods // the same set of methods
type BaseControl struct { type BaseControl struct {
refID int64
title string title string
x, y int x, y int
width, height int width, height int
@ -31,6 +33,22 @@ type BaseControl struct {
mtx sync.RWMutex 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 { func (c *BaseControl) Title() string {
return c.title return c.title
} }
@ -524,3 +542,25 @@ func (c *BaseControl) SetActiveTextColor(clr term.Attribute) {
func (c *BaseControl) SetActiveBackColor(clr term.Attribute) { func (c *BaseControl) SetActiveBackColor(clr term.Attribute) {
c.bgActive = clr 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 { func CreateButton(parent Control, width, height int, title string, scale int) *Button {
b := new(Button) b := new(Button)
b.BaseControl = NewBaseControl()
b.parent = parent b.parent = parent
b.align = AlignCenter 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 { func CreateCheckBox(parent Control, width int, title string, scale int) *CheckBox {
c := new(CheckBox) c := new(CheckBox)
c.BaseControl = NewBaseControl()
c.parent = parent c.parent = parent
if width == AutoSize { 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 // that the control do not want or cannot process the event and the caller sends
// the event to the control parent // the event to the control parent
ProcessEvent(ev Event) bool 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. // control should keep its original size.
func CreateEditField(parent Control, width int, text string, scale int) *EditField { func CreateEditField(parent Control, width int, text string, scale int) *EditField {
e := new(EditField) e := new(EditField)
e.BaseControl = NewBaseControl()
e.onChange = nil e.onChange = nil
e.SetTitle(text) e.SetTitle(text)
e.SetEnabled(true) e.SetEnabled(true)

View File

@ -40,6 +40,7 @@ type EditField struct {
// control should keep its original size. // control should keep its original size.
func CreateEditField(parent Control, width int, text string, scale int) *EditField { func CreateEditField(parent Control, width int, text string, scale int) *EditField {
e := new(EditField) e := new(EditField)
e.BaseControl = NewBaseControl()
e.onChange = nil e.onChange = nil
e.SetTitle(text) e.SetTitle(text)
e.SetEnabled(true) 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 { func CreateFrame(parent Control, width, height int, bs BorderStyle, scale int) *Frame {
f := new(Frame) f := new(Frame)
f.BaseControl = NewBaseControl()
if width == AutoSize { if width == AutoSize {
width = 5 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 { func CreateLabel(parent Control, w, h int, title string, scale int) *Label {
c := new(Label) c := new(Label)
c.BaseControl = NewBaseControl()
if w == AutoSize { if w == AutoSize {
w = xs.Len(title) 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 { func CreateListBox(parent Control, width, height int, scale int) *ListBox {
l := new(ListBox) l := new(ListBox)
l.BaseControl = NewBaseControl()
if height == AutoSize { if height == AutoSize {
height = 3 height = 3

View File

@ -32,6 +32,7 @@ control should keep its original size.
*/ */
func CreateProgressBar(parent Control, width, height int, scale int) *ProgressBar { func CreateProgressBar(parent Control, width, height int, scale int) *ProgressBar {
b := new(ProgressBar) b := new(ProgressBar)
b.BaseControl = NewBaseControl()
if height == AutoSize { if height == AutoSize {
height = 1 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 { func CreateRadio(parent Control, width int, title string, scale int) *Radio {
c := new(Radio) c := new(Radio)
c.BaseControl = NewBaseControl()
if width == AutoSize { if width == AutoSize {
width = xs.Len(title) + 4 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 { func CreateSparkChart(parent Control, w, h int, scale int) *SparkChart {
c := new(SparkChart) c := new(SparkChart)
c.BaseControl = NewBaseControl()
if w == AutoSize { if w == AutoSize {
w = 10 w = 10

View File

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

View File

@ -17,6 +17,7 @@ type TextReader struct {
func CreateTextReader(parent Control, width, height int, scale int) *TextReader { func CreateTextReader(parent Control, width, height int, scale int) *TextReader {
l := new(TextReader) l := new(TextReader)
l.BaseControl = NewBaseControl()
if height == AutoSize { if height == AutoSize {
height = 10 height = 10

View File

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

View File

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