Files
TUIkit/Sources/TUIKit/Views/Panel.swift
T
phranck 37cd4b88f5 feat: Add ContainerView with Header/Body/Footer architecture
- Add ContainerView as unified base for container views
- Add ContainerStyle for separator and border configuration
- Add containerBackground and containerHeaderBackground to Theme
- Standard appearances: Title in top border, Footer as separate section
- Block appearance: Separate Header section with darker background
- Migrate Panel, Card, Alert, Dialog to use ContainerView
- All views now support optional footer with separator
2026-01-28 23:24:35 +01:00

189 lines
5.5 KiB
Swift

//
// Panel.swift
// TUIKit
//
// A titled container view with optional footer.
//
/// A bordered container with a title and optional footer.
///
/// `Panel` is useful for grouping content with a visible label,
/// similar to a fieldset in HTML or a group box in desktop UIs.
///
/// ## Behavior by Appearance
///
/// - **Standard appearances** (line, rounded, doubleLine, heavy):
/// Title is rendered IN the top border.
///
/// - **Block appearance**:
/// Title becomes a separate header section with darker background.
///
/// ## Examples
///
/// ```swift
/// // Simple panel
/// Panel("Settings") {
/// Text("Option 1")
/// Text("Option 2")
/// }
///
/// // Panel with footer
/// Panel("User Info") {
/// Text("Name: John")
/// Text("Age: 30")
/// } footer: {
/// ButtonRow {
/// Button("Save") { }
/// Button("Cancel") { }
/// }
/// }
///
/// // Customized panel
/// Panel("Settings", borderStyle: .doubleLine, titleColor: .cyan) {
/// Text("Content")
/// }
/// ```
public struct Panel<Content: View, Footer: View>: View {
/// The title displayed in the header/border.
public let title: String
/// The content of the panel.
public let content: Content
/// The footer content (typically buttons).
public let footer: Footer?
/// The border style (nil uses appearance default).
public let borderStyle: BorderStyle?
/// The border color.
public let borderColor: Color?
/// The title color.
public let titleColor: Color?
/// The padding inside the panel body.
public let padding: EdgeInsets
/// Whether to show a separator before the footer.
public let showFooterSeparator: Bool
/// Creates a panel with content and footer.
///
/// - Parameters:
/// - title: The title to display.
/// - borderStyle: The border style (default: appearance borderStyle).
/// - borderColor: The border color (default: theme border).
/// - titleColor: The title color (default: theme accent).
/// - padding: The inner padding (default: horizontal 1, vertical 0).
/// - showFooterSeparator: Whether to show separator before footer (default: true).
/// - content: The main content of the panel.
/// - footer: The footer content.
public init(
_ title: String,
borderStyle: BorderStyle? = nil,
borderColor: Color? = nil,
titleColor: Color? = nil,
padding: EdgeInsets = EdgeInsets(horizontal: 1, vertical: 0),
showFooterSeparator: Bool = true,
@ViewBuilder content: () -> Content,
@ViewBuilder footer: () -> Footer
) {
self.title = title
self.content = content()
self.footer = footer()
self.borderStyle = borderStyle
self.borderColor = borderColor
self.titleColor = titleColor
self.padding = padding
self.showFooterSeparator = showFooterSeparator
}
public var body: some View {
ContainerView(
title: title,
titleColor: titleColor,
style: ContainerStyle(
showHeaderSeparator: true,
showFooterSeparator: showFooterSeparator,
borderStyle: borderStyle,
borderColor: borderColor
),
padding: padding
) {
content
} footer: {
footer!
}
}
}
// MARK: - Convenience Initializer (no footer)
extension Panel where Footer == EmptyView {
/// Creates a panel without a footer.
///
/// - Parameters:
/// - title: The title to display in the top border.
/// - borderStyle: The border style (default: appearance borderStyle).
/// - borderColor: The border color (default: theme border).
/// - titleColor: The title color (default: same as border).
/// - padding: The inner padding (default: horizontal 1, vertical 0).
/// - content: The content of the panel.
public init(
_ title: String,
borderStyle: BorderStyle? = nil,
borderColor: Color? = nil,
titleColor: Color? = nil,
padding: EdgeInsets = EdgeInsets(horizontal: 1, vertical: 0),
@ViewBuilder content: () -> Content
) {
self.title = title
self.content = content()
self.footer = nil
self.borderStyle = borderStyle
self.borderColor = borderColor
self.titleColor = titleColor
self.padding = padding
self.showFooterSeparator = true
}
}
// MARK: - Panel Rendering
extension Panel: Renderable {
public func renderToBuffer(context: RenderContext) -> FrameBuffer {
// Create the ContainerView and render it
let containerStyle = ContainerStyle(
showHeaderSeparator: true,
showFooterSeparator: showFooterSeparator,
borderStyle: borderStyle,
borderColor: borderColor
)
if let footerView = footer {
let container = ContainerView(
title: title,
titleColor: titleColor,
style: containerStyle,
padding: padding
) {
content
} footer: {
footerView
}
return container.renderToBuffer(context: context)
} else {
let container = ContainerView(
title: title,
titleColor: titleColor,
style: containerStyle,
padding: padding
) {
content
}
return container.renderToBuffer(context: context)
}
}
}