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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer s.scr.Fini()
|
defer s.scr.Fini()
|
||||||
|
//defer close(s.redrawCh)
|
||||||
|
|
||||||
s.scr.EnableMouse()
|
s.scr.EnableMouse()
|
||||||
return <-s.stopCh
|
return <-s.stopCh
|
||||||
@ -63,15 +65,14 @@ func (s *Screen) StopWithError(err error) {
|
|||||||
|
|
||||||
func (s *Screen) onKeyPressed(event *KeyEvent) {
|
func (s *Screen) onKeyPressed(event *KeyEvent) {
|
||||||
if s.KeyPressed == nil || !s.KeyPressed(event) {
|
if s.KeyPressed == nil || !s.KeyPressed(event) {
|
||||||
go s.Root.OnKeyPressed(event)
|
s.Root.OnKeyPressed(event)
|
||||||
}
|
}
|
||||||
s.Redraw()
|
s.Redraw()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Screen) onMouseEvent(event *MouseEvent) {
|
func (s *Screen) onMouseEvent(event *MouseEvent) {
|
||||||
if s.MouseEvent == nil || !s.MouseEvent(event) {
|
if s.MouseEvent == nil || !s.MouseEvent(event) {
|
||||||
go s.Root.OnMouseEvent(event)
|
s.Root.OnMouseEvent(event)
|
||||||
// TODO redraw in same thread
|
|
||||||
}
|
}
|
||||||
if event.Button != MouseButtonNone {
|
if event.Button != MouseButtonNone {
|
||||||
s.Redraw()
|
s.Redraw()
|
||||||
@ -99,15 +100,22 @@ func (s *Screen) eventloop() {
|
|||||||
case *tcell.EventResize:
|
case *tcell.EventResize:
|
||||||
s.Redraw()
|
s.Redraw()
|
||||||
case *tcell.EventKey:
|
case *tcell.EventKey:
|
||||||
s.onKeyPressed(event)
|
s.startPanicSafeThread("panicked while handling key event", func() { s.onKeyPressed(event) })
|
||||||
case *tcell.EventMouse:
|
case *tcell.EventMouse:
|
||||||
s.onMouseEvent(convertMouseEvent(event))
|
s.startPanicSafeThread("panicked while handling mouse event", func() { s.onMouseEvent(convertMouseEvent(event)) })
|
||||||
default:
|
default:
|
||||||
s.StopWithError(fmt.Errorf("%#v", event))
|
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() {
|
func (s *Screen) drawloop() {
|
||||||
defer s.handlePanic("panicked while redrawing")
|
defer s.handlePanic("panicked while redrawing")
|
||||||
|
|
||||||
|
@ -285,13 +285,6 @@ func TestBorderLayout(t *testing.T) {
|
|||||||
return
|
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) {
|
screen.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
|
||||||
if event.Button == tui.MouseButtonLeft {
|
if event.Button == tui.MouseButtonLeft {
|
||||||
b := new(strings.Builder)
|
b := new(strings.Builder)
|
||||||
|
Loading…
Reference in New Issue
Block a user