Работа над стилями, работа над модальными окнами, изменение списка приложений, работа с размерами окон

This commit is contained in:
XeRo 2025-12-29 17:46:48 +05:00
parent 5203032b83
commit ada7db46cf
12 changed files with 1161 additions and 130 deletions

View file

@ -3,67 +3,138 @@ package setup
import (
"bubble/setup/ui"
"bubble/types"
"fmt"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type ListModel struct {
options []string
focus int
type ListApplicationItem struct {
Application types.PackageApplication
}
func NewListModel(applications []types.PackageApplication) *ListModel {
options := make([]string, len(applications))
func (i ListApplicationItem) Title() string {
return fmt.Sprintf("%s, v%s", i.Application.Name, i.Application.Version)
}
func (i ListApplicationItem) Description() string {
return fmt.Sprintf("%s", i.Application.Description)
}
func (i ListApplicationItem) FilterValue() string {
return i.Application.Name
}
type ListApplicationsModel struct {
List list.Model
width int
height int
}
func NewListApplicationsModel(applications []types.PackageApplication, width, height int) *ListApplicationsModel {
items := make([]list.Item, len(applications))
for i, app := range applications {
options[i] = app.Name
items[i] = ListApplicationItem{app}
}
return &ListModel{
options: options,
delegate := list.NewDefaultDelegate()
delegate.Styles.SelectedTitle = ui.ListSelectedStyle
delegate.Styles.SelectedDesc = ui.ListSelectedStyle
delegate.ShowDescription = false
l := list.New(items, delegate, width, height)
l.Title = "Выберите приложение для настройки"
l.Styles.Title = ui.ListTitleStyle
return &ListApplicationsModel{
List: l,
width: width,
height: height,
}
}
func (m *ListModel) Init() tea.Cmd {
func (m *ListApplicationsModel) Init() tea.Cmd {
return nil
}
func (m *ListModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m *ListApplicationsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "up", "w", "shift+tab":
m.focus--
if m.focus < 0 {
m.focus = len(m.options) - 1
}
case "down", "s", "tab":
m.focus++
if m.focus == len(m.options) {
m.focus = 0
}
case "enter":
selected := m.List.SelectedItem().(ListApplicationItem)
return m, func() tea.Msg {
return types.SelectedAppMsg{Index: m.focus}
return types.ShowModalMsg{
Type: types.ModalConfirm,
Title: "Начать установку",
Message: fmt.Sprintf("Вы хотит настроить приложение:\n\n%s\n\nПродолжить?", selected.Title()),
ModalButtons: []types.ModalButton{
{Text: "Да", Value: true},
{Text: "Нет", Value: false},
},
OnResult: func(result any) tea.Cmd {
if result == true {
return func() tea.Msg {
return types.SelectedAppMsg{App: selected.Application}
}
}
return nil
},
}
}
}
}
m.List, cmd = m.List.Update(msg)
return m, cmd
}
func (m *ListModel) View() string {
res := ""
listStyle := lipgloss.NewStyle().Padding(1, 1).Foreground(ui.ColorPurple)
for i := range m.options {
if i == m.focus {
res += "[x] " + m.options[i]
} else {
res += "[ ] " + m.options[i]
}
if i != len(m.options)-1 {
res += "\n"
}
func (m *ListApplicationsModel) View() string {
contentWidth := ui.ContentWidth(m.width)
listWidth := int(float64(contentWidth) * ui.ListWithRatio)
m.List.SetSize(listWidth-6, m.height-10)
listView := m.List.View()
var desc string
if sel := m.List.SelectedItem(); sel != nil {
desc = sel.(ListApplicationItem).Description()
}
return listStyle.Render(res)
descView := ui.ListDescriptionStyle.Width(contentWidth - listWidth - 10).Render(desc)
body := lipgloss.JoinHorizontal(lipgloss.Top, listView, " ", descView)
container := ui.ListContainer.Width(contentWidth).Render(body)
return lipgloss.Place(m.width, m.height,
lipgloss.Center, lipgloss.Center,
container,
lipgloss.WithWhitespaceChars("░"),
lipgloss.WithWhitespaceForeground(ui.ColorGrey),
)
//var desc string
//if selected := m.List.SelectedItem(); selected != nil {
// desc = selected.(ListApplicationItem).Description()
//}
//descView := ui.ListDescriptionStyle.
// Width(contentWidth/2 - 4).
// Render(desc)
//
//body := lipgloss.JoinHorizontal(lipgloss.Top, listView, " ", descView)
//
//container := ui.ListStyle.
// Width(contentWidth).
// Render(body)
//
//return lipgloss.Place(m.width, m.height,
// lipgloss.Center, lipgloss.Center,
// container,
// lipgloss.WithWhitespaceChars("░"),
// lipgloss.WithWhitespaceForeground(ui.ColorGrey),
//)
}