From 24342eb05d70af48d609a224351237eb1f3d41d2 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 18 Mar 2014 11:50:56 -0400 Subject: [PATCH] Restored the locks everywhere except on resizing calculations. I'll run under the assumption that uitask cannot process any requests while a resize occurs, which means preferredSize() and Stack/Grid.setRect() are inherently safe... let's hope I'm right... --- button.go | 10 ++++++---- checkbox.go | 15 +++++++++------ combobox.go | 30 ++++++++++++++++++------------ label.go | 10 ++++++---- lineedit.go | 10 ++++++---- listbox.go | 30 ++++++++++++++++++------------ progressbar.go | 5 +++-- 7 files changed, 66 insertions(+), 44 deletions(-) diff --git a/button.go b/button.go index 380fdd1..9a5f93c 100644 --- a/button.go +++ b/button.go @@ -29,22 +29,24 @@ func NewButton(text string) (b *Button) { // SetText sets the button's text. func (b *Button) SetText(text string) { + b.lock.Lock() + defer b.lock.Unlock() + if b.created { b.sysData.setText(text) return } - b.lock.Lock() - defer b.lock.Unlock() b.initText = text } // Text returns the button's text. func (b *Button) Text() string { + b.lock.Lock() + defer b.lock.Unlock() + if b.created { return b.sysData.text() } - b.lock.Lock() - defer b.lock.Unlock() return b.initText } diff --git a/checkbox.go b/checkbox.go index c5f0217..fe46023 100644 --- a/checkbox.go +++ b/checkbox.go @@ -27,32 +27,35 @@ func NewCheckbox(text string) (c *Checkbox) { // SetText sets the checkbox's text. func (c *Checkbox) SetText(text string) { + c.lock.Lock() + defer c.lock.Unlock() + if c.created { c.sysData.setText(text) return } - c.lock.Lock() - defer c.lock.Unlock() c.initText = text } // Text returns the checkbox's text. func (c *Checkbox) Text() string { + c.lock.Lock() + defer c.lock.Unlock() + if c.created { return c.sysData.text() } - c.lock.Lock() - defer c.lock.Unlock() return c.initText } // Checked() returns whether or not the checkbox has been checked. func (c *Checkbox) Checked() bool { + c.lock.Lock() + defer c.lock.Unlock() + if c.created { return c.sysData.isChecked() } - c.lock.Lock() - defer c.lock.Unlock() return false } diff --git a/combobox.go b/combobox.go index 2b50a72..41b24c0 100644 --- a/combobox.go +++ b/combobox.go @@ -39,20 +39,24 @@ func NewEditableCombobox(items ...string) *Combobox { // Append adds items to the end of the Combobox's list. // Append will panic if something goes wrong on platforms that do not abort themselves. func (c *Combobox) Append(what ...string) { + c.lock.Lock() + defer c.lock.Unlock() + if c.created { for _, s := range what { c.sysData.append(s) } return } - c.lock.Lock() - defer c.lock.Unlock() c.initItems = append(c.initItems, what...) } // InsertBefore inserts a new item in the Combobox before the item at the given position. It panics if the given index is out of bounds. // InsertBefore will also panic if something goes wrong on platforms that do not abort themselves. func (c *Combobox) InsertBefore(what string, before int) { + c.lock.Lock() + defer c.lock.Unlock() + var m []string if c.created { @@ -62,8 +66,6 @@ func (c *Combobox) InsertBefore(what string, before int) { c.sysData.insertBefore(what, before) return } - c.lock.Lock() - defer c.lock.Unlock() if before < 0 || before >= len(c.initItems) { goto badrange } @@ -78,6 +80,9 @@ badrange: // Delete removes the given item from the Combobox. It panics if the given index is out of bounds. func (c *Combobox) Delete(index int) { + c.lock.Lock() + defer c.lock.Unlock() + if c.created { if index < 0 || index >= c.sysData.len() { goto badrange @@ -85,8 +90,6 @@ func (c *Combobox) Delete(index int) { c.sysData.delete(index) return } - c.lock.Lock() - defer c.lock.Unlock() if index < 0 || index >= len(c.initItems) { goto badrange } @@ -98,21 +101,23 @@ badrange: // Selection returns the current selection. func (c *Combobox) Selection() string { + c.lock.Lock() + defer c.lock.Unlock() + if c.created { return c.sysData.text() } - c.lock.Lock() - defer c.lock.Unlock() return "" } // SelectedIndex returns the index of the current selection in the Combobox. It returns -1 either if no selection was made or if text was manually entered in an editable Combobox. func (c *Combobox) SelectedIndex() int { + c.lock.Lock() + defer c.lock.Unlock() + if c.created { return c.sysData.selectedIndex() } - c.lock.Lock() - defer c.lock.Unlock() return -1 } @@ -120,11 +125,12 @@ func (c *Combobox) SelectedIndex() int { // // On platforms for which this function may return an error, it panics if one is returned. func (c *Combobox) Len() int { + c.lock.Lock() + defer c.lock.Unlock() + if c.created { return c.sysData.len() } - c.lock.Lock() - defer c.lock.Unlock() return len(c.initItems) } diff --git a/label.go b/label.go index 6d6fc9d..c4bcc4e 100644 --- a/label.go +++ b/label.go @@ -24,22 +24,24 @@ func NewLabel(text string) *Label { // SetText sets the Label's text. func (l *Label) SetText(text string) { + l.lock.Lock() + defer l.lock.Unlock() + if l.created { l.sysData.setText(text) return } - l.lock.Lock() - defer l.lock.Unlock() l.initText = text } // Text returns the Label's text. func (l *Label) Text() string { + l.lock.Lock() + defer l.lock.Unlock() + if l.created { return l.sysData.text() } - l.lock.Lock() - defer l.lock.Unlock() return l.initText } diff --git a/lineedit.go b/lineedit.go index 4ad6889..43ec60c 100644 --- a/lineedit.go +++ b/lineedit.go @@ -35,22 +35,24 @@ func NewPasswordEdit() *LineEdit { // SetText sets the LineEdit's text. func (l *LineEdit) SetText(text string) { + l.lock.Lock() + defer l.lock.Unlock() + if l.created { l.sysData.setText(text) return } - l.lock.Lock() - defer l.lock.Unlock() l.initText = text } // Text returns the LineEdit's text. func (l *LineEdit) Text() string { + l.lock.Lock() + defer l.lock.Unlock() + if l.created { return l.sysData.text() } - l.lock.Lock() - defer l.lock.Unlock() return l.initText } diff --git a/listbox.go b/listbox.go index 764bfad..9a1d756 100644 --- a/listbox.go +++ b/listbox.go @@ -40,20 +40,24 @@ func NewMultiSelListbox(items ...string) *Listbox { // Append adds items to the end of the Listbox's list. // Append will panic if something goes wrong on platforms that do not abort themselves. func (l *Listbox) Append(what ...string) { + l.lock.Lock() + defer l.lock.Unlock() + if l.created { for _, s := range what { l.sysData.append(s) } return } - l.lock.Lock() - defer l.lock.Unlock() l.initItems = append(l.initItems, what...) } // InsertBefore inserts a new item in the Listbox before the item at the given position. It panics if the given index is out of bounds. // InsertBefore will also panic if something goes wrong on platforms that do not abort themselves. func (l *Listbox) InsertBefore(what string, before int) { + l.lock.Lock() + defer l.lock.Unlock() + var m []string if l.created { @@ -63,8 +67,6 @@ func (l *Listbox) InsertBefore(what string, before int) { l.sysData.insertBefore(what, before) return } - l.lock.Lock() - defer l.lock.Unlock() if before < 0 || before >= len(l.initItems) { goto badrange } @@ -79,6 +81,9 @@ badrange: // Delete removes the given item from the Listbox. It panics if the given index is out of bounds. func (l *Listbox) Delete(index int) { + l.lock.Lock() + defer l.lock.Unlock() + if l.created { if index < 0 || index >= l.sysData.len() { goto badrange @@ -86,8 +91,6 @@ func (l *Listbox) Delete(index int) { l.sysData.delete(index) return } - l.lock.Lock() - defer l.lock.Unlock() if index < 0 || index >= len(l.initItems) { goto badrange } @@ -99,21 +102,23 @@ badrange: // Selection returns a list of strings currently selected in the Listbox, or an empty list if none have been selected. This list will have at most one item on a single-selection Listbox. func (l *Listbox) Selection() []string { + l.lock.Lock() + defer l.lock.Unlock() + if l.created { return l.sysData.selectedTexts() } - l.lock.Lock() - defer l.lock.Unlock() return nil } // SelectedIndices returns a list of the currently selected indexes in the Listbox, or an empty list if none have been selected. This list will have at most one item on a single-selection Listbox. func (l *Listbox) SelectedIndices() []int { + l.lock.Lock() + defer l.lock.Unlock() + if l.created { return l.sysData.selectedIndices() } - l.lock.Lock() - defer l.lock.Unlock() return nil } @@ -121,11 +126,12 @@ func (l *Listbox) SelectedIndices() []int { // // On platforms for which this function may return an error, it panics if one is returned. func (l *Listbox) Len() int { + l.lock.Lock() + defer l.lock.Unlock() + if l.created { return l.sysData.len() } - l.lock.Lock() - defer l.lock.Unlock() return len(l.initItems) } diff --git a/progressbar.go b/progressbar.go index 80debdc..6ef3f84 100644 --- a/progressbar.go +++ b/progressbar.go @@ -30,6 +30,9 @@ func NewProgressBar() *ProgressBar { // Otherwise, SetProgress panics. // TODO what happens if you repeatedly call SetProgress(-1)? func (p *ProgressBar) SetProgress(percent int) { + p.lock.Lock() + defer p.lock.Unlock() + if percent < -1 || percent > 100 { panic("percent value out of range") } @@ -37,8 +40,6 @@ func (p *ProgressBar) SetProgress(percent int) { p.sysData.setProgress(percent) return } - p.lock.Lock() - defer p.lock.Unlock() p.initProg = percent }