mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-427 Use new keyboard implementation for widgets. Closes #427
This commit is contained in:
18
modules/hackernews/keyboard.go
Normal file
18
modules/hackernews/keyboard.go
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
package hackernews
|
||||
|
||||
import "github.com/gdamore/tcell"
|
||||
|
||||
func (widget *Widget) initializeKeyboardControls() {
|
||||
widget.SetKeyboardChar("/", widget.ShowHelp)
|
||||
widget.SetKeyboardChar("j", widget.next)
|
||||
widget.SetKeyboardChar("k", widget.prev)
|
||||
widget.SetKeyboardChar("o", widget.openStory)
|
||||
widget.SetKeyboardChar("r", widget.Refresh)
|
||||
widget.SetKeyboardChar("c", widget.openComments)
|
||||
|
||||
widget.SetKeyboardKey(tcell.KeyDown, widget.next)
|
||||
widget.SetKeyboardKey(tcell.KeyEnter, widget.openStory)
|
||||
widget.SetKeyboardKey(tcell.KeyEsc, widget.unselect)
|
||||
widget.SetKeyboardKey(tcell.KeyUp, widget.prev)
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
@@ -28,6 +27,7 @@ const HelpText = `
|
||||
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.KeyboardWidget
|
||||
wtf.TextWidget
|
||||
|
||||
app *tview.Application
|
||||
@@ -38,19 +38,23 @@ type Widget struct {
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
KeyboardWidget: wtf.NewKeyboardWidget(),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
app: app,
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
|
||||
widget.unselect()
|
||||
|
||||
widget.View.SetScrollable(true)
|
||||
widget.View.SetRegions(true)
|
||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
|
||||
return &widget
|
||||
}
|
||||
@@ -171,39 +175,3 @@ func (widget *Widget) unselect() {
|
||||
widget.selected = -1
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch string(event.Rune()) {
|
||||
case "/":
|
||||
widget.ShowHelp()
|
||||
case "j":
|
||||
widget.next()
|
||||
return nil
|
||||
case "k":
|
||||
widget.prev()
|
||||
return nil
|
||||
case "r":
|
||||
widget.Refresh()
|
||||
return nil
|
||||
case "c":
|
||||
widget.openComments()
|
||||
return nil
|
||||
}
|
||||
|
||||
switch event.Key() {
|
||||
case tcell.KeyDown:
|
||||
widget.next()
|
||||
return nil
|
||||
case tcell.KeyEnter:
|
||||
widget.openStory()
|
||||
return nil
|
||||
case tcell.KeyEsc:
|
||||
widget.unselect()
|
||||
return event
|
||||
case tcell.KeyUp:
|
||||
widget.prev()
|
||||
return nil
|
||||
default:
|
||||
return event
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user