Files
phranck db8ea40c0a Refactor: Fix SwiftLint warnings and refactor StatusBar to _StatusBarCore pattern
- 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)
2026-02-15 02:35:18 +01:00

50 lines
1.7 KiB
Swift
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 🖥 TUIKit Terminal UI Kit for Swift
// Environment.swift
//
// Created by LAYERED.work
// License: MIT
import TUIkitCore
// MARK: - Environment Modifier
/// A modifier that injects a value into the environment for child views.
///
/// `EnvironmentModifier` conforms to both `View` and ``Renderable``.
/// Because ``renderToBuffer(_:context:)`` checks `Renderable` first,
/// the `body` property below is **never called during rendering**.
/// It exists only to satisfy the `View` protocol requirement.
/// All actual work happens in `renderToBuffer(context:)`.
public struct EnvironmentModifier<Content: View, V>: View {
/// The content view.
public let content: Content
/// The key path to modify.
public let keyPath: WritableKeyPath<EnvironmentValues, V>
/// The value to inject.
public let value: V
/// Creates a new environment modifier.
public init(content: Content, keyPath: WritableKeyPath<EnvironmentValues, V>, value: V) {
self.content = content
self.keyPath = keyPath
self.value = value
}
/// Not used during rendering ``Renderable`` conformance takes priority.
public var body: some View {
content
}
}
extension EnvironmentModifier: Renderable {
public func renderToBuffer(context: RenderContext) -> FrameBuffer {
// Create modified environment and render content with it.
// The modified context carries the environment through the render tree
// no global state sync needed.
let modifiedEnvironment = context.environment.setting(keyPath, to: value)
let modifiedContext = context.withEnvironment(modifiedEnvironment)
return TUIkitView.renderToBuffer(content, context: modifiedContext)
}
}