refactor(ui): pass event handlers in main func

This commit is contained in:
ayntgl 2021-09-14 14:28:29 +04:00
parent fa8b590d9c
commit 7a9d0e066e
3 changed files with 16 additions and 20 deletions

View file

@ -33,15 +33,19 @@ func main() {
config = util.NewConfig()
tview.Styles = config.Theme
app = tview.NewApplication().
EnableMouse(config.Mouse).
SetInputCapture(onAppInputCapture)
guildsView = ui.NewGuildsView(onGuildsTreeViewSelected)
messagesView = ui.NewMessagesView(
app,
onMessagesTextViewInputCapture,
)
messageInputField = ui.NewMessageInputField(onMessageInputFieldInputCapture)
app = tview.NewApplication()
app.EnableMouse(config.Mouse)
app.SetInputCapture(onAppInputCapture)
guildsView = ui.NewGuildsView()
guildsView.SetSelectedFunc(onGuildsTreeViewSelected)
messagesView = ui.NewMessagesView(app)
messagesView.SetInputCapture(onMessagesViewInputCapture)
messageInputField = ui.NewMessageInputField()
messageInputField.SetInputCapture(onMessageInputFieldInputCapture)
mainFlex = ui.NewMainFlex(
guildsView,
messagesView,
@ -99,7 +103,7 @@ func findByMessageID(ms []*discordgo.Message, mID string) (int, *discordgo.Messa
return -1, nil
}
func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
if selectedChannel == nil {
return nil
}

View file

@ -7,14 +7,11 @@ import (
)
// NewGuildsView creates and returns a new guilds treeview.
func NewGuildsView(
onGuildsTreeViewSelected func(*tview.TreeNode),
) *tview.TreeView {
func NewGuildsView() *tview.TreeView {
v := tview.NewTreeView()
v.
SetTopLevel(1).
SetRoot(tview.NewTreeNode("")).
SetSelectedFunc(onGuildsTreeViewSelected).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0).
SetTitle("Guilds").

View file

@ -8,7 +8,6 @@ import (
// NewMessagesView creates and returns a new messages textview.
func NewMessagesView(
app *tview.Application,
onMessagesTextViewInputCapture func(*tcell.EventKey) *tcell.EventKey,
) *tview.TextView {
v := tview.NewTextView()
v.
@ -19,7 +18,6 @@ func NewMessagesView(
SetChangedFunc(func() {
app.Draw()
}).
SetInputCapture(onMessagesTextViewInputCapture).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0).
SetTitleAlign(tview.AlignLeft)
@ -28,15 +26,12 @@ func NewMessagesView(
}
// NewMessageInputField creates and returns a new message inputfield.
func NewMessageInputField(
onMessageInputFieldInputCapture func(*tcell.EventKey) *tcell.EventKey,
) *tview.InputField {
func NewMessageInputField() *tview.InputField {
i := tview.NewInputField()
i.
SetPlaceholder("Message...").
SetPlaceholderTextColor(tcell.ColorWhite).
SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
SetInputCapture(onMessageInputFieldInputCapture).
SetBorder(true).
SetBorderPadding(0, 0, 1, 0).
SetTitleAlign(tview.AlignLeft)