mirror of
https://github.com/phranck/TUIkit.git
synced 2026-05-21 09:50:35 +00:00
db8ea40c0a
- Fix 80 SwiftLint warnings (159 -> 79): vertical_whitespace, prefer_self_in_static_references, modifier_order, trailing_newline, trailing_whitespace, prefer_for_where, unneeded_synthesized_initializer, redundant_type_annotation, implicit_optional_initialization, superfluous_disable_command, shorthand_optional_binding, syntactic_sugar, empty_string, vertical_whitespace_closing_braces, identifier_name in BadgeModifier - Refactor StatusBar from direct Renderable to _StatusBarCore pattern (public View with real body wrapping private Renderable core)
96 lines
2.6 KiB
Swift
96 lines
2.6 KiB
Swift
// 🖥️ TUIKit — Terminal UI Kit for Swift
|
||
// ViewBuilder.swift
|
||
//
|
||
// Created by LAYERED.work
|
||
// License: MIT
|
||
|
||
import TUIkitCore
|
||
/// A result builder for View hierarchies.
|
||
///
|
||
/// The `@ViewBuilder` enables a declarative syntax similar to SwiftUI:
|
||
///
|
||
/// ```swift
|
||
/// VStack {
|
||
/// Text("Line 1")
|
||
/// Text("Line 2")
|
||
/// if showMore {
|
||
/// Text("Line 3")
|
||
/// }
|
||
/// }
|
||
/// ```
|
||
///
|
||
/// The builder supports:
|
||
/// - Single views
|
||
/// - Multiple views (unlimited, via Parameter Packs)
|
||
/// - Conditionals (`if`, `if-else`)
|
||
/// - Optional views (`if let`)
|
||
/// - Arrays of views (`for-in`)
|
||
@MainActor
|
||
@resultBuilder
|
||
public struct ViewBuilder {
|
||
|
||
// MARK: - Single View
|
||
|
||
/// Builds a single view.
|
||
public static func buildBlock<Content: View>(_ content: Content) -> Content {
|
||
content
|
||
}
|
||
|
||
// MARK: - Multiple Views (Parameter Pack)
|
||
|
||
/// Builds multiple views into a TupleView using Swift Parameter Packs.
|
||
///
|
||
/// This single overload replaces the previous 9 arity-specific `buildBlock`
|
||
/// overloads (`TupleView2` through `TupleView10`), removing the 10-child limit.
|
||
public static func buildBlock<each C: View>(
|
||
_ content: repeat each C
|
||
) -> TupleView<repeat each C> {
|
||
TupleView(repeat each content)
|
||
}
|
||
|
||
// MARK: - Conditionals
|
||
|
||
/// Supports the true branch of an if-else.
|
||
public static func buildEither<TrueContent: View, FalseContent: View>(
|
||
first content: TrueContent
|
||
) -> ConditionalView<TrueContent, FalseContent> {
|
||
.trueContent(content)
|
||
}
|
||
|
||
/// Supports the false branch of an if-else.
|
||
public static func buildEither<TrueContent: View, FalseContent: View>(
|
||
second content: FalseContent
|
||
) -> ConditionalView<TrueContent, FalseContent> {
|
||
.falseContent(content)
|
||
}
|
||
|
||
/// Supports optional views (if let, if without else).
|
||
public static func buildOptional<Content: View>(_ content: Content?) -> Content? {
|
||
content
|
||
}
|
||
|
||
/// Supports availability limiting.
|
||
public static func buildLimitedAvailability<Content: View>(_ content: Content) -> Content {
|
||
content
|
||
}
|
||
|
||
// MARK: - Arrays
|
||
|
||
/// Supports for-in loops.
|
||
public static func buildArray<Content: View>(_ components: [Content]) -> ViewArray<Content> {
|
||
ViewArray(components)
|
||
}
|
||
|
||
// MARK: - Expression
|
||
|
||
/// Converts a single expression into a view.
|
||
public static func buildExpression<Content: View>(_ expression: Content) -> Content {
|
||
expression
|
||
}
|
||
|
||
/// Supports optional expressions.
|
||
public static func buildExpression<Content: View>(_ expression: Content?) -> Content? {
|
||
expression
|
||
}
|
||
}
|