refactor + fix добавление проекта
This commit is contained in:
21
tg_bot/screens/ui/buttons.go
Normal file
21
tg_bot/screens/ui/buttons.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package ui
|
||||
|
||||
import "github.com/NicoNex/echotron/v3"
|
||||
|
||||
// BuildGrid builds button rows from items using a callback builder.
|
||||
func BuildGrid[T any](items []T, itemsPerRow, itemsPerPage, totalPages int, itemFn func(T) (title string, callback string)) [][]echotron.InlineKeyboardButton {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
buttons := make([]echotron.InlineKeyboardButton, 0, len(items))
|
||||
for _, item := range items {
|
||||
title, callback := itemFn(item)
|
||||
buttons = append(buttons, echotron.InlineKeyboardButton{
|
||||
Text: title,
|
||||
CallbackData: callback,
|
||||
})
|
||||
}
|
||||
|
||||
return BuildPageRows(buttons, itemsPerRow, itemsPerPage, totalPages)
|
||||
}
|
||||
@@ -173,3 +173,47 @@ func BuildElementRows(config ElementLayoutConfig, allItems []echotron.InlineKeyb
|
||||
|
||||
return rows
|
||||
}
|
||||
|
||||
// BuildPageRows раскладывает элементы текущей страницы без собственной пагинации.
|
||||
// Автоматически заполняет пустыми кнопками если страниц > 1 (для консистентности высоты).
|
||||
func BuildPageRows(pageItems []echotron.InlineKeyboardButton, itemsPerRow, itemsPerPage, totalPages int) [][]echotron.InlineKeyboardButton {
|
||||
if len(pageItems) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Параметры по умолчанию
|
||||
if itemsPerPage <= 0 {
|
||||
itemsPerPage = 6
|
||||
}
|
||||
if itemsPerRow <= 0 {
|
||||
itemsPerRow = 2
|
||||
}
|
||||
|
||||
// Кнопка-заполнитель по умолчанию
|
||||
emptyButton := echotron.InlineKeyboardButton{Text: " ", CallbackData: "empty"}
|
||||
|
||||
// Если страниц >= 2, заполняем до ItemsPerPage пустыми кнопками
|
||||
if totalPages >= 2 {
|
||||
for len(pageItems) < itemsPerPage {
|
||||
pageItems = append(pageItems, emptyButton)
|
||||
}
|
||||
}
|
||||
|
||||
// Раскладываем элементы по рядам
|
||||
var rows [][]echotron.InlineKeyboardButton
|
||||
for i := 0; i < len(pageItems); i += itemsPerRow {
|
||||
var row []echotron.InlineKeyboardButton
|
||||
|
||||
for j := 0; j < itemsPerRow && i+j < len(pageItems); j++ {
|
||||
row = append(row, pageItems[i+j])
|
||||
}
|
||||
|
||||
for len(row) < itemsPerRow {
|
||||
row = append(row, emptyButton)
|
||||
}
|
||||
|
||||
rows = append(rows, row)
|
||||
}
|
||||
|
||||
return rows
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user