panic safe threads
This commit is contained in:
parent
b6b6668d0e
commit
d29f48896e
18
screen.go
18
screen.go
@ -47,7 +47,9 @@ func (s *Screen) Start() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer s.scr.Fini()
|
||||
//defer close(s.redrawCh)
|
||||
|
||||
s.scr.EnableMouse()
|
||||
return <-s.stopCh
|
||||
@ -63,15 +65,14 @@ func (s *Screen) StopWithError(err error) {
|
||||
|
||||
func (s *Screen) onKeyPressed(event *KeyEvent) {
|
||||
if s.KeyPressed == nil || !s.KeyPressed(event) {
|
||||
go s.Root.OnKeyPressed(event)
|
||||
s.Root.OnKeyPressed(event)
|
||||
}
|
||||
s.Redraw()
|
||||
}
|
||||
|
||||
func (s *Screen) onMouseEvent(event *MouseEvent) {
|
||||
if s.MouseEvent == nil || !s.MouseEvent(event) {
|
||||
go s.Root.OnMouseEvent(event)
|
||||
// TODO redraw in same thread
|
||||
s.Root.OnMouseEvent(event)
|
||||
}
|
||||
if event.Button != MouseButtonNone {
|
||||
s.Redraw()
|
||||
@ -99,15 +100,22 @@ func (s *Screen) eventloop() {
|
||||
case *tcell.EventResize:
|
||||
s.Redraw()
|
||||
case *tcell.EventKey:
|
||||
s.onKeyPressed(event)
|
||||
s.startPanicSafeThread("panicked while handling key event", func() { s.onKeyPressed(event) })
|
||||
case *tcell.EventMouse:
|
||||
s.onMouseEvent(convertMouseEvent(event))
|
||||
s.startPanicSafeThread("panicked while handling mouse event", func() { s.onMouseEvent(convertMouseEvent(event)) })
|
||||
default:
|
||||
s.StopWithError(fmt.Errorf("%#v", event))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Screen) startPanicSafeThread(errorMessage string, f func()) {
|
||||
go func() {
|
||||
defer s.handlePanic(errorMessage)
|
||||
f()
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *Screen) drawloop() {
|
||||
defer s.handlePanic("panicked while redrawing")
|
||||
|
||||
|
@ -285,13 +285,6 @@ func TestBorderLayout(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
screen.KeyPressed = func(event *tui.KeyEvent) (consumed bool) {
|
||||
if event.Key() == tcell.KeyCtrlC {
|
||||
screen.StopWithError(errors.New(fmt.Sprintf("key: %#v | rune: %s", event.Key(), string(event.Rune()))))
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
screen.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
|
||||
if event.Button == tui.MouseButtonLeft {
|
||||
b := new(strings.Builder)
|
||||
|
Loading…
Reference in New Issue
Block a user