summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/components/modal/modal.go
blob: 09989d8ece1333b4b3e62396d179f282f6d2aea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package modal

import (
	"strings"

	"github.com/charmbracelet/lipgloss/v2"
	"github.com/sst/opencode/internal/layout"
	"github.com/sst/opencode/internal/styles"
	"github.com/sst/opencode/internal/theme"
)

// CloseModalMsg is a message to signal that the active modal should be closed.
type CloseModalMsg struct{}

// Modal is a reusable modal component that handles frame rendering and overlay placement
type Modal struct {
	width      int
	height     int
	title      string
	maxWidth   int
	maxHeight  int
	fitContent bool
}

// ModalOption is a function that configures a Modal
type ModalOption func(*Modal)

// WithTitle sets the modal title
func WithTitle(title string) ModalOption {
	return func(m *Modal) {
		m.title = title
	}
}

// WithMaxWidth sets the maximum width
func WithMaxWidth(width int) ModalOption {
	return func(m *Modal) {
		m.maxWidth = width
		m.fitContent = false
	}
}

// WithMaxHeight sets the maximum height
func WithMaxHeight(height int) ModalOption {
	return func(m *Modal) {
		m.maxHeight = height
	}
}

func WithFitContent(fit bool) ModalOption {
	return func(m *Modal) {
		m.fitContent = fit
	}
}

// New creates a new Modal with the given options
func New(opts ...ModalOption) *Modal {
	m := &Modal{
		maxWidth:   0,
		maxHeight:  0,
		fitContent: true,
	}

	for _, opt := range opts {
		opt(m)
	}

	return m
}

func (m *Modal) SetTitle(title string) {
	m.title = title
}

// Render renders the modal centered on the screen
func (m *Modal) Render(contentView string, background string) string {
	t := theme.CurrentTheme()

	outerWidth := layout.Current.Container.Width - 8
	if m.maxWidth > 0 && outerWidth > m.maxWidth {
		outerWidth = m.maxWidth
	}

	if m.fitContent {
		titleWidth := lipgloss.Width(m.title)
		contentWidth := lipgloss.Width(contentView)
		largestWidth := max(titleWidth+2, contentWidth)
		outerWidth = largestWidth + 6
	}

	innerWidth := outerWidth - 4

	baseStyle := styles.NewStyle().Foreground(t.TextMuted()).Background(t.BackgroundPanel())

	var finalContent string
	if m.title != "" {
		titleStyle := baseStyle.
			Foreground(t.Text()).
			Bold(true).
			Padding(0, 1)

		escStyle := baseStyle.Foreground(t.TextMuted())
		escText := escStyle.Render("esc")

		// Calculate position for esc text
		titleWidth := lipgloss.Width(m.title)
		escWidth := lipgloss.Width(escText)
		spacesNeeded := max(0, innerWidth-titleWidth-escWidth-2)
		spacer := strings.Repeat(" ", spacesNeeded)
		titleLine := m.title + spacer + escText
		titleLine = titleStyle.Render(titleLine)

		finalContent = strings.Join([]string{titleLine, "", contentView}, "\n")
	} else {
		finalContent = contentView
	}

	modalStyle := baseStyle.
		PaddingTop(1).
		PaddingBottom(1).
		PaddingLeft(2).
		PaddingRight(2)

	modalView := modalStyle.
		Width(outerWidth).
		Render(finalContent)

	// Calculate position for centering
	bgHeight := lipgloss.Height(background)
	bgWidth := lipgloss.Width(background)
	modalHeight := lipgloss.Height(modalView)
	modalWidth := lipgloss.Width(modalView)

	row := (bgHeight - modalHeight) / 2
	col := (bgWidth - modalWidth) / 2

	return layout.PlaceOverlay(
		col-1, // TODO: whyyyyy
		row,
		modalView,
		background,
		layout.WithOverlayBorder(),
		layout.WithOverlayBorderColor(t.BorderActive()),
	)
}