Steamrolled errors under panic() in Combobox/Listbox.Append()/InsertBefore() because screw Windows being different.

This commit is contained in:
Pietro Gagliardi 2014-03-09 16:02:17 -04:00
parent a7ec908ebd
commit b9f0ad90ec
6 changed files with 30 additions and 38 deletions

View File

@ -36,25 +36,23 @@ func NewEditableCombobox(items ...string) *Combobox {
}
// Append adds items to the end of the Combobox's list.
func (c *Combobox) Append(what ...string) (err error) {
// 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 i, s := range what {
err := c.sysData.append(s)
if err != nil {
return fmt.Errorf("error adding element %d in Combobox.Append() (%q): %v", i, s, err)
}
c.sysData.append(s)
}
return nil
return
}
c.initItems = append(c.initItems, what...)
return nil
}
// 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.
func (c *Combobox) InsertBefore(what string, before int) (err error) {
// 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()
@ -64,7 +62,8 @@ func (c *Combobox) InsertBefore(what string, before int) (err error) {
if before < 0 || before >= c.sysData.len() {
goto badrange
}
return c.sysData.insertBefore(what, before)
c.sysData.insertBefore(what, before)
return
}
if before < 0 || before >= len(c.initItems) {
goto badrange
@ -73,7 +72,7 @@ func (c *Combobox) InsertBefore(what string, before int) (err error) {
m = append(m, c.initItems[:before]...)
m = append(m, what)
c.initItems = append(m, c.initItems[before:]...)
return nil
return
badrange:
panic(fmt.Errorf("index %d out of range in Combobox.InsertBefore()", before))
}

View File

@ -28,25 +28,23 @@ func NewListbox(multiple bool, items ...string) (l *Listbox) {
}
// Append adds items to the end of the Listbox's list.
func (l *Listbox) Append(what ...string) (err error) {
// 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 i, s := range what {
err := l.sysData.append(s)
if err != nil {
return fmt.Errorf("error adding element %d in Listbox.Append() (%q): %v", i, s, err)
}
l.sysData.append(s)
}
return nil
return
}
l.initItems = append(l.initItems, what...)
return nil
}
// 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.
func (l *Listbox) InsertBefore(what string, before int) (err error) {
// 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()
@ -56,7 +54,8 @@ func (l *Listbox) InsertBefore(what string, before int) (err error) {
if before < 0 || before >= l.sysData.len() {
goto badrange
}
return l.sysData.insertBefore(what, before)
l.sysData.insertBefore(what, before)
return
}
if before < 0 || before >= len(l.initItems) {
goto badrange
@ -65,7 +64,7 @@ func (l *Listbox) InsertBefore(what string, before int) (err error) {
m = append(m, l.initItems[:before]...)
m = append(m, what)
l.initItems = append(m, l.initItems[before:]...)
return nil
return
badrange:
panic(fmt.Errorf("index %d out of range in Listbox.InsertBefore()", before))
}

View File

@ -40,10 +40,10 @@ func (c *cSysData) isChecked() bool {
func (c *cSysData) text() string {
panic(runtime.GOOS + " sysData does not define text()")
}
func (c *cSysData) append(string) error {
func (c *cSysData) append(string) {
panic(runtime.GOOS + " sysData does not define append()")
}
func (c *cSysData) insertBefore(string, int) error {
func (c *cSysData) insertBefore(string, int) {
panic(runtime.GOOS + " sysData does not define insertBefore()")
}
func (c *cSysData) selectedIndex() int {

View File

@ -387,7 +387,7 @@ func (s *sysData) text() string {
return <-ret
}
func (s *sysData) append(what string) error {
func (s *sysData) append(what string) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
@ -395,10 +395,9 @@ func (s *sysData) append(what string) error {
ret <- struct{}{}
}
<-ret
return nil
}
func (s *sysData) insertBefore(what string, before int) error {
func (s *sysData) insertBefore(what string, before int) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
@ -406,7 +405,6 @@ func (s *sysData) insertBefore(what string, before int) error {
ret <- struct{}{}
}
<-ret
return nil
}
func (s *sysData) selectedIndex() int {

View File

@ -216,7 +216,7 @@ func (s *sysData) text() string {
return <-ret
}
func (s *sysData) append(what string) error {
func (s *sysData) append(what string) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
@ -224,10 +224,9 @@ func (s *sysData) append(what string) error {
ret <- struct{}{}
}
<-ret
return nil
}
func (s *sysData) insertBefore(what string, before int) error {
func (s *sysData) insertBefore(what string, before int) {
ret := make(chan struct{})
defer close(ret)
uitask <- func() {
@ -235,7 +234,6 @@ func (s *sysData) insertBefore(what string, before int) error {
ret <- struct{}{}
}
<-ret
return nil
}
func (s *sysData) selectedIndex() int {

View File

@ -317,7 +317,7 @@ func (s *sysData) text() (str string) {
return syscall.UTF16ToString(tc)
}
func (s *sysData) append(what string) (err error) {
func (s *sysData) append(what string) {
ret := make(chan uiret)
defer close(ret)
uitask <- &uimsg{
@ -332,14 +332,13 @@ func (s *sysData) append(what string) (err error) {
}
r := <-ret
if r.ret == uintptr(classTypes[s.ctype].addSpaceErr) {
return fmt.Errorf("out of space adding item to combobox/listbox (last error: %v)", r.err)
panic(fmt.Errorf("out of space adding item to combobox/listbox (last error: %v)", r.err))
} else if r.ret == uintptr(classTypes[s.ctype].selectedIndexErr) {
return fmt.Errorf("failed to add item to combobox/listbox (last error: %v)", r.err)
panic(fmt.Errorf("failed to add item to combobox/listbox (last error: %v)", r.err))
}
return nil
}
func (s *sysData) insertBefore(what string, index int) (err error) {
func (s *sysData) insertBefore(what string, index int) {
ret := make(chan uiret)
defer close(ret)
uitask <- &uimsg{
@ -354,11 +353,10 @@ func (s *sysData) insertBefore(what string, index int) (err error) {
}
r := <-ret
if r.ret == uintptr(classTypes[s.ctype].addSpaceErr) {
return fmt.Errorf("out of space adding item to combobox/listbox (last error: %v)", r.err)
panic(fmt.Errorf("out of space adding item to combobox/listbox (last error: %v)", r.err))
} else if r.ret == uintptr(classTypes[s.ctype].selectedIndexErr) {
return fmt.Errorf("failed to add item to combobox/listbox (last error: %v)", r.err)
panic(fmt.Errorf("failed to add item to combobox/listbox (last error: %v)", r.err))
}
return nil
}
func (s *sysData) selectedIndex() int {