dynamo-browse/internal/dynamo-browse/ui/teamodels/frame/frame.go

60 lines
1.2 KiB
Go
Raw Normal View History

package frame
import (
2022-03-27 21:58:41 +00:00
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/lmika/dynamo-browse/internal/dynamo-browse/ui/teamodels/utils"
)
var (
2022-03-27 00:40:32 +00:00
inactiveHeaderStyle = lipgloss.NewStyle().
2022-03-30 11:52:26 +00:00
Foreground(lipgloss.Color("#000000")).
Background(lipgloss.Color("#d1d1d1"))
)
// Frame is a frame that appears in the
2022-03-27 21:58:41 +00:00
type FrameTitle struct {
header string
active bool
style Style
width int
}
type Style struct {
ActiveTitle lipgloss.Style
InactiveTitle lipgloss.Style
}
func NewFrameTitle(header string, active bool, style Style) FrameTitle {
return FrameTitle{header, active, style, 0}
}
2022-03-27 21:58:41 +00:00
func (f *FrameTitle) SetTitle(title string) {
f.header = title
}
2022-03-27 21:58:41 +00:00
func (f FrameTitle) View() string {
return f.headerView()
}
2022-03-27 21:58:41 +00:00
func (f *FrameTitle) Resize(w, h int) {
f.width = w
}
2022-03-27 21:58:41 +00:00
func (f FrameTitle) HeaderHeight() int {
return lipgloss.Height(f.headerView())
}
2022-03-27 21:58:41 +00:00
func (f FrameTitle) headerView() string {
style := f.style.InactiveTitle
2022-03-27 00:40:32 +00:00
if f.active {
style = f.style.ActiveTitle
2022-03-27 00:40:32 +00:00
}
titleText := f.header
2022-03-27 00:40:32 +00:00
title := style.Render(titleText)
line := style.Render(strings.Repeat(" ", utils.Max(0, f.width-lipgloss.Width(title))))
return lipgloss.JoinHorizontal(lipgloss.Left, title, line)
}