term-kit-backend/Sources/TermKitBackend/View/Label.swift

54 lines
1.4 KiB
Swift
Raw Normal View History

2024-07-10 14:41:11 +02:00
//
// Label.swift
// TermKitBackend
//
// Created by david-swift on 07.07.2024.
//
import TermKit
/// A simple label view.
public struct Label: TermKitWidget {
/// The label.
var label: String
/// Initialize a label.
/// - Parameter label: The label.
public init(_ label: String) {
self.label = label
}
/// The view storage.
/// - Parameters:
/// - modifiers: Modify views before being updated.
/// - type: The type of the app storage.
2024-07-18 16:07:37 +02:00
/// - Returns: The view storage.
public func container<Data>(
2024-07-10 14:41:11 +02:00
modifiers: [(any AnyView) -> any AnyView],
2024-07-18 16:07:37 +02:00
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
2024-07-10 14:41:11 +02:00
let button = TermKit.Label(label)
return .init(button)
}
/// Update the stored content.
/// - Parameters:
/// - storage: The storage to update.
/// - modifiers: Modify views before being updated
/// - updateProperties: Whether to update the view's properties.
/// - type: The type of the app storage.
2024-07-18 16:07:37 +02:00
public func update<Data>(
2024-07-10 14:41:11 +02:00
_ storage: ViewStorage,
modifiers: [(any AnyView) -> any AnyView],
updateProperties: Bool,
2024-07-18 16:07:37 +02:00
type: Data.Type
) where Data: ViewRenderData {
2024-07-10 14:41:11 +02:00
guard let storage = storage.pointer as? TermKit.Label, updateProperties else {
return
}
storage.text = label
}
}