From 72f9543e759c7a7c10e332a8d2ad7b10486430bb Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 3 May 2018 16:08:52 -0700 Subject: [PATCH] Can dismiss help modal from the keyboard --- todo/widget.go | 8 +++++++- wtf/billboard_modal.go | 23 +++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/todo/widget.go b/todo/widget.go index 21832cde..2231ec7e 100644 --- a/todo/widget.go +++ b/todo/widget.go @@ -218,7 +218,13 @@ func (widget *Widget) persist() { } func (widget *Widget) showHelp() { - modal := wtf.NewBillboardModal(helpText) + closeFunc := func() { + widget.pages.RemovePage("help") + widget.app.SetFocus(widget.View) + widget.display() + } + + modal := wtf.NewBillboardModal(helpText, closeFunc) widget.pages.AddPage("help", modal, false, true) widget.app.SetFocus(modal) diff --git a/wtf/billboard_modal.go b/wtf/billboard_modal.go index eb7dde99..d8128b04 100644 --- a/wtf/billboard_modal.go +++ b/wtf/billboard_modal.go @@ -9,15 +9,34 @@ const offscreen = -1000 const modalWidth = 80 const modalHeight = 22 -func NewBillboardModal(text string) *tview.Frame { +func NewBillboardModal(text string, closeFunc func()) *tview.Frame { + keyboardIntercept := func(event *tcell.EventKey) *tcell.EventKey { + switch string(event.Rune()) { + case "h": + closeFunc() + return nil + } + + switch event.Key() { + case tcell.KeyEsc: + closeFunc() + return nil + case tcell.KeyTab: + return nil + default: + return event + } + } + textView := tview.NewTextView() textView.SetWrap(true) textView.SetText(text) textView.SetBackgroundColor(tview.Styles.ContrastBackgroundColor) + textView.SetInputCapture(keyboardIntercept) thing := tview.NewFrame(textView) - thing.SetRect(offscreen, offscreen, modalWidth, modalHeight) // First draw it offscreen and then reposition below + thing.SetRect(offscreen, offscreen, modalWidth, modalHeight) drawFunc := func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) { w, h := screen.Size()