Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
key msg handled by state view only
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 15, 2022
1 parent ffe1487 commit 685f7a0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
9 changes: 6 additions & 3 deletions tui/confirm_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ func (v *ConfirmView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
v.width, v.height = msg.Width, msg.Height
case ConfirmMessage:
v.tipLabel.Text = msg.Message
v.confirmButton.OnClick = msg.ConfirmAction
case tea.KeyMsg:

}
Expand All @@ -68,3 +65,9 @@ func (v *ConfirmView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, cmd)
return v, tea.Batch(cmds...)
}

func (v *ConfirmView) Reset(tip string, confirmOnClick, cancelOnClick func() tea.Cmd) {
v.tipLabel.Text = tip
v.confirmButton.OnClick = confirmOnClick
v.cancelButton.OnClick = cancelOnClick
}
3 changes: 0 additions & 3 deletions tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case AppliedNewHostContent:
v.SetHostNode(v.hostNode)
case tea.KeyMsg:
if v.model.state != StateEditorView {
return v, nil
}
switch {
case key.Matches(m, keys.Esc):
return v, nil
Expand Down
22 changes: 17 additions & 5 deletions tui/main_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,25 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.switchState(m.preState)
m.helpView.helpView.ShowAll = false
}
default:
switch m.state {
case StateTreeView:
m.updateView(msg, &cmds, m.treeView)
case StateEditorView:
m.updateView(msg, &cmds, m.editorView)
case StateNodeView:
m.updateView(msg, &cmds, m.nodeView)
case StateConfirmView:
m.updateView(msg, &cmds, m.confirmView)
}
}
default:
m.updateView(msg, &cmds, m.editorView)
m.updateView(msg, &cmds, m.nodeView)
m.updateView(msg, &cmds, m.treeView)
m.updateView(msg, &cmds, m.confirmView)
m.updateView(msg, &cmds, m.helpView)
}
m.updateView(msg, &cmds, m.confirmView)
m.updateView(msg, &cmds, m.editorView)
m.updateView(msg, &cmds, m.nodeView)
m.updateView(msg, &cmds, m.treeView)
m.updateView(msg, &cmds, m.helpView)
return m, tea.Batch(cmds...)
}

Expand Down
6 changes: 0 additions & 6 deletions tui/msg.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package tui

import tea "github.com/charmbracelet/bubbletea"

type RefreshTreeViewItems struct {
}

type AppliedNewHostContent struct {
}

type ConfirmMessage struct {
Message string
ConfirmAction func() tea.Cmd
}
25 changes: 11 additions & 14 deletions tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ func NewNodeView(model *Model) *NodeView {
confirmButton.OnClick = func() tea.Cmd {
// Check inputs
if nodeTypeChoices.SelectedItem() == nil {
model.switchState(StateConfirmView)
return func() tea.Msg {
return ConfirmMessage{
Message: "Please select node type",
ConfirmAction: func() tea.Cmd {
// Go back to previous state
model.setState(model.preState)
return nil
},
}
}
model.confirmView.Reset("Please select node type",
func() tea.Cmd {
model.setState(StateNodeView)
return nil
},
func() tea.Cmd {
model.setState(StateNodeView)
return nil
})
model.setState(StateConfirmView)
return nil
}

// Get parent group
Expand Down Expand Up @@ -136,9 +136,6 @@ func (v *NodeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
log.Debug(fmt.Sprintf("node view w %d h %d", m.Width, m.Height))
case tea.KeyMsg:
if v.model.state != StateNodeView {
return v, nil
}
return v.Form.Update(msg)
}
_, cmd := v.Form.Update(msg)
Expand Down
3 changes: 0 additions & 3 deletions tui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ func (v *TreeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case RefreshTreeViewItems, AppliedNewHostContent:
v.RefreshTreeNodes()
case tea.KeyMsg:
if v.model.state != StateTreeView {
return v, nil
}
selectedNode := v.SelectedNode()
switch {
case key.Matches(m, keys.Esc):
Expand Down

0 comments on commit 685f7a0

Please sign in to comment.