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)
50 lines
1.7 KiB
Swift
50 lines
1.7 KiB
Swift
// 🖥️ 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)
|
||
}
|
||
}
|