diff options
| author | adamdottv <[email protected]> | 2025-06-05 15:44:20 -0500 |
|---|---|---|
| committer | adamdottv <[email protected]> | 2025-06-11 11:43:28 -0500 |
| commit | 95d5e1f2318e0c62f19196122fc2a448f1114cfd (patch) | |
| tree | 75369872d32e10896e9263ddbf32cf36e7e418ac /packages/tui/internal/layout/container.go | |
| parent | 979bad3e64e3fff43d41094a79c73deb31e82ec8 (diff) | |
| download | opencode-95d5e1f2318e0c62f19196122fc2a448f1114cfd.tar.gz opencode-95d5e1f2318e0c62f19196122fc2a448f1114cfd.zip | |
wip: refactoring tui
Diffstat (limited to 'packages/tui/internal/layout/container.go')
| -rw-r--r-- | packages/tui/internal/layout/container.go | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/packages/tui/internal/layout/container.go b/packages/tui/internal/layout/container.go index 00166b1d8..1c12ef6a1 100644 --- a/packages/tui/internal/layout/container.go +++ b/packages/tui/internal/layout/container.go @@ -13,6 +13,8 @@ type Container interface { Bindings Focus() Blur() + MaxWidth() int + Alignment() lipgloss.Position } type container struct { @@ -32,6 +34,9 @@ type container struct { borderLeft bool borderStyle lipgloss.Border + maxWidth int + align lipgloss.Position + focused bool } @@ -51,6 +56,11 @@ func (c *container) View() string { width := c.width height := c.height + // Apply max width constraint if set + if c.maxWidth > 0 && width > c.maxWidth { + width = c.maxWidth + } + style = style.Background(t.Background()) // Apply border if any side is enabled @@ -74,7 +84,7 @@ func (c *container) View() string { if c.focused { style = style.BorderBackground(t.Background()).BorderForeground(t.Primary()) } else { - style = style.BorderBackground(t.Background()).BorderForeground(t.BorderNormal()) + style = style.BorderBackground(t.Background()).BorderForeground(t.Border()) } } style = style. @@ -92,6 +102,12 @@ func (c *container) SetSize(width, height int) tea.Cmd { c.width = width c.height = height + // Apply max width constraint if set + effectiveWidth := width + if c.maxWidth > 0 && width > c.maxWidth { + effectiveWidth = c.maxWidth + } + // If the content implements Sizeable, adjust its size to account for padding and borders if sizeable, ok := c.content.(Sizeable); ok { // Calculate horizontal space taken by padding and borders @@ -113,7 +129,7 @@ func (c *container) SetSize(width, height int) tea.Cmd { } // Set content size with adjusted dimensions - contentWidth := max(0, width-horizontalSpace) + contentWidth := max(0, effectiveWidth-horizontalSpace) contentHeight := max(0, height-verticalSpace) return sizeable.SetSize(contentWidth, contentHeight) } @@ -124,6 +140,14 @@ func (c *container) GetSize() (int, int) { return c.width, c.height } +func (c *container) MaxWidth() int { + return c.maxWidth +} + +func (c *container) Alignment() lipgloss.Position { + return c.align +} + func (c *container) BindingKeys() []key.Binding { if b, ok := c.content.(Bindings); ok { return b.BindingKeys() @@ -228,3 +252,27 @@ func WithThickBorder() ContainerOption { func WithDoubleBorder() ContainerOption { return WithBorderStyle(lipgloss.DoubleBorder()) } + +func WithMaxWidth(maxWidth int) ContainerOption { + return func(c *container) { + c.maxWidth = maxWidth + } +} + +func WithAlign(align lipgloss.Position) ContainerOption { + return func(c *container) { + c.align = align + } +} + +func WithAlignLeft() ContainerOption { + return WithAlign(lipgloss.Left) +} + +func WithAlignCenter() ContainerOption { + return WithAlign(lipgloss.Center) +} + +func WithAlignRight() ContainerOption { + return WithAlign(lipgloss.Right) +} |
