Files
LazyPM/pkg/tui/views/root.go
2026-03-18 14:31:36 +01:00

89 lines
2.6 KiB
Go

package views
import (
"charm.land/bubbletea/v2"
"github.com/LazyBachelor/LazyPM/internal/app"
"github.com/LazyBachelor/LazyPM/internal/models"
"github.com/LazyBachelor/LazyPM/pkg/tui/msgs"
"github.com/LazyBachelor/LazyPM/pkg/tui/views/dashboard"
"github.com/LazyBachelor/LazyPM/pkg/tui/views/kanban"
)
type RootModel struct {
currentView tea.Model
app *app.App
feedbackChan chan models.ValidationFeedback
quitChan chan bool
submitChan chan<- struct{}
lastSize tea.WindowSizeMsg
hasSize bool
}
func NewRootView(app *app.App, feedbackChan chan models.ValidationFeedback, quitChan chan bool, submitChan chan<- struct{}) *RootModel {
initialView := dashboard.NewDashboard(app, feedbackChan, quitChan, submitChan)
return &RootModel{
currentView: initialView,
app: app,
feedbackChan: feedbackChan,
quitChan: quitChan,
submitChan: submitChan,
}
}
func (r *RootModel) Init() tea.Cmd {
return r.currentView.Init()
}
func (r *RootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// switch dashboards based on msg
switch m := msg.(type) {
case tea.WindowSizeMsg:
// remember the latest window size and forward it to the current view.
r.hasSize = true
r.lastSize = m
var cmd tea.Cmd
r.currentView, cmd = r.currentView.Update(msg)
return r, cmd
case msgs.SwitchToDashboardMsg:
// switch back to dashboard 1 and apply the last known size.
r.currentView = dashboard.NewDashboard(r.app, r.feedbackChan, r.quitChan, r.submitChan)
var cmds []tea.Cmd
if r.hasSize {
// check if there is a size, and then update it
var sizeCmd tea.Cmd
// set size of dashboard1 to the size before switching
r.currentView, sizeCmd = r.currentView.Update(r.lastSize)
if sizeCmd != nil {
cmds = append(cmds, sizeCmd)
}
}
cmds = append(cmds, tea.ClearScreen, r.currentView.Init())
return r, tea.Batch(cmds...)
case msgs.SwitchToKanbanBoardMsg:
// switch to kanban board and apply the last known size.
r.currentView = kanban.NewDashboard(r.app, r.feedbackChan, r.quitChan, r.submitChan)
var cmds []tea.Cmd
if r.hasSize {
// check if there is a size, and then update it
var sizeCmd tea.Cmd
// set size of kanban board to the size before switching
r.currentView, sizeCmd = r.currentView.Update(r.lastSize)
if sizeCmd != nil {
cmds = append(cmds, sizeCmd)
}
}
cmds = append(cmds, tea.ClearScreen, r.currentView.Init())
return r, tea.Batch(cmds...)
}
var cmd tea.Cmd
r.currentView, cmd = r.currentView.Update(msg)
return r, cmd
}
func (r *RootModel) View() tea.View {
v := r.currentView.View()
v.AltScreen = true
return v
}