ficsit-cli-flake/tea/scenes/main_menu.go

133 lines
3.4 KiB
Go
Raw Normal View History

2021-11-05 21:42:49 +00:00
package scenes
import (
2021-12-04 18:02:05 +00:00
"time"
2021-11-05 21:42:49 +00:00
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
2021-12-02 04:00:33 +00:00
"github.com/rs/zerolog/log"
"github.com/satisfactorymodding/ficsit-cli/tea/components"
"github.com/satisfactorymodding/ficsit-cli/tea/utils"
2021-11-05 21:42:49 +00:00
)
2021-12-02 04:00:33 +00:00
var _ tea.Model = (*mainMenu)(nil)
2021-11-05 21:42:49 +00:00
2021-12-02 04:00:33 +00:00
type mainMenu struct {
root components.RootModel
list list.Model
2021-11-05 21:42:49 +00:00
}
2021-12-02 04:00:33 +00:00
func NewMainMenu(root components.RootModel) tea.Model {
model := mainMenu{
root: root,
2021-11-05 21:42:49 +00:00
}
items := []list.Item{
2021-12-02 04:00:33 +00:00
utils.SimpleItem{
2021-12-04 03:42:31 +00:00
ItemTitle: "Installations",
2021-12-02 04:00:33 +00:00
Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) {
newModel := NewInstallations(root, currentModel)
return newModel, newModel.Init()
},
},
utils.SimpleItem{
2021-12-04 03:42:31 +00:00
ItemTitle: "Profiles",
2021-12-02 04:00:33 +00:00
Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) {
newModel := NewProfiles(root, currentModel)
return newModel, newModel.Init()
},
},
utils.SimpleItem{
2021-12-04 03:42:31 +00:00
ItemTitle: "Mods",
2021-12-02 04:00:33 +00:00
Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) {
newModel := NewMods(root, currentModel)
return newModel, newModel.Init()
},
2021-11-05 21:42:49 +00:00
},
2021-12-02 04:00:33 +00:00
utils.SimpleItem{
2021-12-04 03:42:31 +00:00
ItemTitle: "Apply Changes",
2021-12-02 04:00:33 +00:00
Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) {
// TODO Apply changes to all changed profiles
return nil, nil
},
},
utils.SimpleItem{
2021-12-04 03:42:31 +00:00
ItemTitle: "Save",
2021-12-02 04:00:33 +00:00
Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) {
if err := root.GetGlobal().Save(); err != nil {
2021-12-04 18:02:05 +00:00
menu := currentModel.(exitMenu)
log.Error().Err(err).Msg(ErrorFailedAddMod)
cmd := menu.list.NewStatusMessage(ErrorFailedAddMod)
return currentModel, cmd
2021-12-02 04:00:33 +00:00
}
return nil, nil
},
2021-11-05 21:42:49 +00:00
},
2021-12-02 04:00:33 +00:00
utils.SimpleItem{
2021-12-04 03:42:31 +00:00
ItemTitle: "Exit",
2021-12-02 04:00:33 +00:00
Activate: func(msg tea.Msg, currentModel tea.Model) (tea.Model, tea.Cmd) {
newModel := NewExitMenu(root)
return newModel, newModel.Init()
},
2021-11-05 21:42:49 +00:00
},
}
2021-12-04 03:42:31 +00:00
model.list = list.NewModel(items, utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
2021-12-02 04:00:33 +00:00
model.list.SetShowStatusBar(false)
model.list.SetFilteringEnabled(false)
model.list.Title = "Main Menu"
model.list.Styles = utils.ListStyles
model.list.SetSize(model.list.Width(), model.list.Height())
2021-12-04 18:02:05 +00:00
model.list.StatusMessageLifetime = time.Second * 3
model.list.DisableQuitKeybindings()
2021-11-05 21:42:49 +00:00
2021-12-02 04:00:33 +00:00
return model
2021-11-05 21:42:49 +00:00
}
func (m mainMenu) Init() tea.Cmd {
return nil
}
func (m mainMenu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
2021-12-02 04:00:33 +00:00
case KeyControlC:
2021-11-05 21:42:49 +00:00
return m, tea.Quit
2021-12-02 04:00:33 +00:00
case "q":
newModel := NewExitMenu(m.root)
return newModel, newModel.Init()
case KeyEnter:
i, ok := m.list.SelectedItem().(utils.SimpleItem)
2021-11-05 21:42:49 +00:00
if ok {
2021-12-02 04:00:33 +00:00
if i.Activate != nil {
newModel, cmd := i.Activate(msg, m)
if newModel != nil || cmd != nil {
if newModel == nil {
newModel = m
}
return newModel, cmd
}
2021-11-05 21:42:49 +00:00
return m, nil
}
}
2021-12-04 18:02:05 +00:00
return m, nil
2021-11-05 21:42:49 +00:00
default:
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
case tea.WindowSizeMsg:
2021-11-05 22:01:35 +00:00
top, right, bottom, left := lipgloss.NewStyle().Margin(2, 2).GetMargin()
2021-11-05 21:42:49 +00:00
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom)
2021-12-02 04:00:33 +00:00
m.root.SetSize(msg)
2021-11-05 21:42:49 +00:00
}
return m, nil
}
func (m mainMenu) View() string {
2021-12-02 04:00:33 +00:00
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), m.list.View())
2021-11-05 21:42:49 +00:00
}