Skip to content

Commit

Permalink
navstack: close the top model when on push
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Miller committed Apr 11, 2024
1 parent 7f549fd commit 745183a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/deeper/artistpaintings/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func (m Model) Init() tea.Cmd {
return nil
}

func (m Model) Close() error {
// This is called when the model is pushed and popped from the navigation stack.
return nil
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

switch msg := msg.(type) {
Expand Down
8 changes: 8 additions & 0 deletions navstack/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ func (m Model) Init() tea.Cmd {
// Push pushes a new navigation item onto the stack.
// The new navigation item is given a tea.WindowSizeMsg to ensure it's view can be presented correctly.
// The item's Init method is called and resulting command is processed by [BubbleTea].
// If top item's model implements the Closable interface the Close method is called.
// This new item will be the top most item on the stack and thus will be rendered.
func (m *Model) Push(item NavigationItem) tea.Cmd {

top := m.Top()
if top != nil {
if c, ok := top.Model.(Closable); ok {
c.Close()
}
}

wmsg := m.window.GetWindowSizeMsg()
nim, cmd := item.Model.Update(wmsg)
item.Model = nim
Expand Down

0 comments on commit 745183a

Please sign in to comment.