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

146 lines
3.9 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
error *components.ErrorComponent
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{
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Installations",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
2021-12-02 04:00:33 +00:00
newModel := NewInstallations(root, currentModel)
return newModel, newModel.Init()
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Profiles",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
2021-12-02 04:00:33 +00:00
newModel := NewProfiles(root, currentModel)
return newModel, newModel.Init()
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Mods",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
2021-12-02 04:00:33 +00:00
newModel := NewMods(root, currentModel)
return newModel, newModel.Init()
},
2021-11-05 21:42:49 +00:00
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Apply Changes",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
if err := root.GetGlobal().Save(); err != nil {
log.Error().Err(err).Msg(ErrorFailedAddMod)
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
currentModel.error = errorComponent
return currentModel, cmd
}
newModel := NewApply(root, currentModel)
return newModel, newModel.Init()
2021-12-02 04:00:33 +00:00
},
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Save",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
2021-12-02 04:00:33 +00:00
if err := root.GetGlobal().Save(); err != nil {
2021-12-04 18:02:05 +00:00
log.Error().Err(err).Msg(ErrorFailedAddMod)
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
currentModel.error = errorComponent
2021-12-04 18:02:05 +00:00
return currentModel, cmd
2021-12-02 04:00:33 +00:00
}
return nil, nil
},
2021-11-05 21:42:49 +00:00
},
2022-04-14 01:27:39 +00:00
utils.SimpleItem[mainMenu]{
2021-12-04 03:42:31 +00:00
ItemTitle: "Exit",
2022-04-14 01:27:39 +00:00
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
return nil, tea.Quit
2021-12-02 04:00:33 +00:00
},
2021-11-05 21:42:49 +00:00
},
}
2022-04-14 01:27:39 +00:00
model.list = list.New(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
2021-12-04 18:02:05 +00:00
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":
2022-04-14 01:27:39 +00:00
return m, tea.Quit
2021-12-02 04:00:33 +00:00
case KeyEnter:
2022-04-14 01:27:39 +00:00
i, ok := m.list.SelectedItem().(utils.SimpleItem[mainMenu])
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)
case components.ErrorComponentTimeoutMsg:
m.error = nil
2021-11-05 21:42:49 +00:00
}
return m, nil
}
func (m mainMenu) View() string {
if m.error != nil {
err := (*m.error).View()
m.list.SetSize(m.list.Width(), m.root.Size().Height-m.root.Height()-lipgloss.Height(err))
return lipgloss.JoinVertical(lipgloss.Left, m.root.View(), err, m.list.View())
}
m.list.SetSize(m.list.Width(), m.root.Size().Height-m.root.Height())
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
}