Skip to content

Commit

Permalink
send window msg when push/pop the nav stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Miller committed Feb 20, 2024
1 parent 0a7d9da commit ae61793
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/deeper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (m model) View() string {

func main() {
w := window.New(10, 20, 6)
ns := navstack.New()
ns := navstack.New(&w)
artists := data.GetArtists()
choices := make([]menu.Choice, len(artists))
for i, a := range artists {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func main() {
choices := []menu.Choice{red, green, blue}

title := "Colorful Choices"
ns := navstack.New()
w := window.New(10, 20, 6)
ns := navstack.New(&w)
m := model{
menu: menu.New(title, choices, nil, &w),
window: &w,
Expand Down
22 changes: 17 additions & 5 deletions navstack/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ package navstack

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/kevm/bubbleo/window"
)

type Closable interface {
Close() error
}

type Model struct {
stack []NavigationItem
stack []NavigationItem
window *window.Model
}

func New() Model {
func New(w *window.Model) Model {
model := Model{
stack: []NavigationItem{},
stack: []NavigationItem{},
window: w,
}

return model
Expand All @@ -30,8 +33,12 @@ func (m Model) Init() tea.Cmd {
}

func (m *Model) Push(item NavigationItem) tea.Cmd {

nim, cmd := item.Model.Update(tea.WindowSizeMsg{Width: m.window.Width, Height: m.window.Height - m.window.TopOffset})
item.Model = nim

m.stack = append(m.stack, item)
return item.Init()
return tea.Batch(cmd, item.Init())
}

func (m *Model) Pop() tea.Cmd {
Expand All @@ -44,13 +51,18 @@ func (m *Model) Pop() tea.Cmd {
c.Close()
}

cmds := []tea.Cmd{}
nim, cmd := top.Model.Update(tea.WindowSizeMsg{Width: m.window.Width, Height: m.window.Height - m.window.TopOffset})
top.Model = nim
cmds = append(cmds, cmd, top.Init())

m.stack = m.stack[:len(m.stack)-1]
top = m.Top()
if top == nil {
return tea.Quit
}

return top.Init()
return tea.Batch(cmds...)
}

func (m Model) Top() *NavigationItem {
Expand Down

0 comments on commit ae61793

Please sign in to comment.