mirror of
https://github.com/phranck/TUIkit.git
synced 2026-05-21 09:50:35 +00:00
5ad97132b8
- Replace custom Observable protocol and @Published with Apple's @Observable macro - Add withObservationTracking in renderToBuffer for automatic per-property dependency tracking - Add type-based @Environment(Type.self) and .environment(object) for observable objects - Add ObjectEnvironmentModifier for injecting observable objects into the environment - Add needsCacheClear flag to AppState for thread-safe cache invalidation - Add cross-platform test script (scripts/test-linux.sh) for Docker-based Linux verification - Add DemoAppHeader with system info display (OS, version, architecture) - Consolidate Example App: extract ImageDemoHelpers, KeyboardHelpSection, ValueDisplayRow - Add pre-push verification rule to CLAUDE.md - Verified on both macOS and Linux (swift:6.0 container), 1155 tests passing
54 lines
1.6 KiB
Swift
54 lines
1.6 KiB
Swift
// 🖥️ TUIKit — Terminal UI Kit for Swift
|
||
// ObjectEnvironmentModifier.swift
|
||
//
|
||
// Created by LAYERED.work
|
||
// License: MIT
|
||
|
||
import Observation
|
||
import TUIkitCore
|
||
|
||
// MARK: - Object Environment Modifier
|
||
|
||
/// A modifier that injects an observable object into the environment by type.
|
||
///
|
||
/// The object is stored by its type, enabling retrieval via
|
||
/// `@Environment(MyModel.self)`. Only one object per type can be
|
||
/// stored at each level of the view hierarchy; inner modifiers
|
||
/// override outer ones for the same type.
|
||
///
|
||
/// This is framework infrastructure. Use the `.environment(_:)` modifier
|
||
/// on `View` instead.
|
||
public struct ObjectEnvironmentModifier<Content: View, T: Observable>: View {
|
||
/// The content view.
|
||
public let content: Content
|
||
|
||
/// The observable object to inject.
|
||
public let object: T
|
||
|
||
/// Creates a new object environment modifier.
|
||
///
|
||
/// - Parameters:
|
||
/// - content: The child view tree.
|
||
/// - object: The observable object to make available.
|
||
public init(content: Content, object: T) {
|
||
self.content = content
|
||
self.object = object
|
||
}
|
||
|
||
/// Not used during rendering. ``Renderable`` conformance takes priority.
|
||
public var body: some View {
|
||
content
|
||
}
|
||
}
|
||
|
||
// MARK: - Renderable
|
||
|
||
extension ObjectEnvironmentModifier: Renderable {
|
||
public func renderToBuffer(context: RenderContext) -> FrameBuffer {
|
||
var modifiedEnvironment = context.environment
|
||
modifiedEnvironment[observable: T.self] = object
|
||
let modifiedContext = context.withEnvironment(modifiedEnvironment)
|
||
return TUIkitView.renderToBuffer(content, context: modifiedContext)
|
||
}
|
||
}
|