Files
TUIkit/Sources/TUIKit/Views/Dialog.swift
T
phranck 282585a68b feat: Add Appearance system for consistent UI styling
Appearance controls the visual structure (border style) of UI controls,
while Theme controls colors. Together they create a complete look.

New types:
- Appearance: Defines borderStyle for all controls
- Appearance.ID: Type-safe identifier (no magic strings)
- AppearanceManager: Environment-based cycling (no singleton)
- AppearanceRegistry: Available appearances

Predefined appearances:
- .rounded (default) - Curved corners
- .sharp - 90-degree corners
- .double - Double-line borders
- .heavy - Thick/bold borders
- .ascii - Maximum terminal compatibility (+, -, |)

Updated components to use appearance.borderStyle as default:
- Menu, Button, Panel, Card, Box, Alert, Dialog
- BorderModifier (.border() modifier)

Added 'a' key shortcut to cycle through appearances.

Also added BorderStyle.ascii for ASCII-only terminals.

All 189 tests passing.
2026-01-28 21:44:01 +01:00

179 lines
4.9 KiB
Swift

//
// Dialog.swift
// TUIKit
//
// A modal dialog view with title and custom content.
//
/// A modal dialog view with a title and customizable content.
///
/// `Dialog` is more flexible than `Alert` — it accepts any content,
/// making it suitable for forms, selections, or complex interactions.
///
/// # Example
///
/// ```swift
/// // Simple dialog
/// Dialog(title: "Settings") {
/// Text("Option 1: Enabled")
/// Text("Option 2: Disabled")
/// }
///
/// // Dialog with custom styling
/// Dialog(title: "User Profile", borderStyle: .doubleLine, titleColor: .cyan) {
/// Text("Name: John Doe")
/// Text("Email: john@example.com")
/// Divider()
/// Text("[Edit] [Close]")
/// }
///
/// // Modal overlay pattern
/// mainContent
/// .dimmed()
/// .overlay {
/// Dialog(title: "Confirm Action") {
/// Text("Are you sure you want to proceed?")
/// HStack {
/// Text("[Yes]").foregroundColor(.green)
/// Spacer()
/// Text("[No]").foregroundColor(.red)
/// }
/// }
/// }
/// ```
public struct Dialog<Content: View>: View {
/// The dialog title.
public let title: String
/// The dialog content.
public let content: Content
/// 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 inner padding.
public let padding: EdgeInsets
/// Creates a dialog with the specified options.
///
/// - Parameters:
/// - title: The dialog title.
/// - borderStyle: The border style (default: appearance borderStyle).
/// - borderColor: The border color (default: theme border).
/// - titleColor: The title color (default: theme foreground).
/// - padding: The inner padding (default: horizontal 2, vertical 1).
/// - content: The dialog content.
public init(
title: String,
borderStyle: BorderStyle? = nil,
borderColor: Color? = nil,
titleColor: Color? = nil,
padding: EdgeInsets = EdgeInsets(horizontal: 2, vertical: 1),
@ViewBuilder content: () -> Content
) {
self.title = title
self.borderStyle = borderStyle
self.borderColor = borderColor
self.titleColor = titleColor
self.padding = padding
self.content = content()
}
public var body: some View {
Panel(
title,
borderStyle: borderStyle,
borderColor: borderColor,
titleColor: titleColor,
padding: padding
) {
content
}
}
}
// MARK: - Convenience Extensions
extension Dialog {
/// Creates a dialog with a double-line border style.
///
/// - Parameters:
/// - title: The dialog title.
/// - borderColor: The border color (default: nil).
/// - titleColor: The title color (default: nil).
/// - content: The dialog content.
/// - Returns: A dialog with double-line borders.
public static func doubleLine<C: View>(
title: String,
borderColor: Color? = nil,
titleColor: Color? = nil,
@ViewBuilder content: () -> C
) -> Dialog<C> {
Dialog<C>(
title: title,
borderStyle: .doubleLine,
borderColor: borderColor,
titleColor: titleColor,
content: content
)
}
/// Creates a dialog with a heavy border style.
///
/// - Parameters:
/// - title: The dialog title.
/// - borderColor: The border color (default: nil).
/// - titleColor: The title color (default: nil).
/// - content: The dialog content.
/// - Returns: A dialog with heavy borders.
public static func heavy<C: View>(
title: String,
borderColor: Color? = nil,
titleColor: Color? = nil,
@ViewBuilder content: () -> C
) -> Dialog<C> {
Dialog<C>(
title: title,
borderStyle: .heavy,
borderColor: borderColor,
titleColor: titleColor,
content: content
)
}
}
// MARK: - Modal Presentation Helper
extension View {
/// Presents this view as a modal dialog over dimmed content.
///
/// This is a convenience method that combines `.dimmed()` and `.overlay()`
/// with center alignment.
///
/// # Example
///
/// ```swift
/// mainContent.modal {
/// Dialog(title: "Settings") {
/// Text("Setting 1")
/// Text("Setting 2")
/// }
/// }
/// ```
///
/// - Parameter content: The modal content to display.
/// - Returns: A view with the modal overlay.
public func modal<Modal: View>(
@ViewBuilder content: () -> Modal
) -> some View {
self.dimmed()
.overlay(alignment: .center, content: content)
}
}