33// OpenSwiftUICore
44//
55// Audited for iOS 18.0
6- // Status: WIP
6+ // Status: Blocked by TreeElement
77// ID: 5A14269649C60F846422EA0FA4C5E535 (SwiftUI)
88// ID: 43DA1754B0518AF1D72B90677BF266DB (SwiftUICore)
99
@@ -260,49 +260,56 @@ extension _ViewDebug.Data: Encodable {
260260 try container. encode ( serializedProperties ( ) , forKey: . properties)
261261 try container. encode ( childData, forKey: . children)
262262 }
263-
263+
264264 private func serializedProperties( ) -> [ SerializedProperty ] {
265265 data. compactMap { key, value -> SerializedProperty ? in
266- if key == . value {
267- if let attribute = serializedAttribute ( for: value, label: nil , reflectionDepth: 6 ) {
268- return SerializedProperty ( id: key. rawValue, attribute: attribute)
269- } else {
270- return nil
271- }
272- } else if key == . type {
273- let type = value as? Any . Type ?? type ( of: value)
274- let attribute = SerializedAttribute ( type: type)
275- return SerializedProperty ( id: 0 , attribute: attribute)
276- } else {
277- if let attribute = serializedAttribute ( for: value, label: nil , reflectionDepth: 4 ) {
278- return SerializedProperty ( id: key. rawValue, attribute: attribute)
279- } else {
280- return nil
281- }
266+ let attribute : SerializedAttribute ? = switch key {
267+ case . type: SerializedAttribute ( type: value as? Any . Type ?? type ( of: value) )
268+ case . value: serializedAttribute ( for: value, label: nil , reflectionDepth: 6 )
269+ default : serializedAttribute ( for: value, label: nil , reflectionDepth: 4 )
282270 }
271+ guard let attribute else { return nil }
272+ return SerializedProperty ( id: key. rawValue, attribute: attribute)
283273 }
284274 }
285-
286- // TODO
287- // Mirror API
275+
288276 private func serializedAttribute( for value: Any , label: String ? , reflectionDepth depth: Int ) -> SerializedAttribute ? {
289- // let unwrapped = unwrapped(value)
290-
291- return nil
292-
293- // let mirror = Mirror(reflecting: value)
294- // mirror.displayStyle = .tuple
295-
277+ guard let unwrappedValue = unwrapped ( value) else {
278+ return nil
279+ }
280+ if unwrappedValue is Encodable || unwrappedValue is CustomViewDebugValueConvertible || depth == 0 {
281+ return SerializedAttribute ( value: unwrappedValue, serializeValue: true , label: label, subattributes: nil )
282+ } else if let mirror = effectiveMirror ( for: unwrappedValue) {
283+ guard !mirror. children. isEmpty else {
284+ return SerializedAttribute ( value: unwrappedValue, serializeValue: true , label: label, subattributes: nil )
285+ }
286+ let depth = depth - 1
287+ let subattributes = mirror. children. compactMap { child in
288+ serializedAttribute ( for: child. value, label: child. label, reflectionDepth: depth)
289+ }
290+ return SerializedAttribute ( value: unwrappedValue, serializeValue: false , label: label, subattributes: subattributes)
291+ } else {
292+ return SerializedAttribute ( value: unwrappedValue, serializeValue: false , label: label, subattributes: nil )
293+ }
296294 }
297295
298296 private func unwrapped( _ value: Any ) -> Any ? {
299- if let value = value as? ValueWrapper {
300- return value . wrappedValue
297+ if let valueWrapper = value as? ValueWrapper {
298+ return valueWrapper . wrappedValue
301299 } else {
302- return nil
300+ return value
303301 }
304302 }
305303
304+ private func effectiveMirror( for value: Any ) -> Mirror ? {
305+ if case let customized as CustomViewDebugReflectable = value {
306+ customized. customViewDebugMirror
307+ } else if case let customized as CustomReflectable = value {
308+ customized. customMirror
309+ } else {
310+ Mirror ( reflecting: value)
311+ }
312+ }
306313}
307314
308315// MARK: _ViewDebug.Data.SerializedProperty
@@ -328,57 +335,21 @@ extension _ViewDebug.Data {
328335// MARK: _ViewDebug.Data.SerializedAttribute
329336
330337extension _ViewDebug . Data {
331- // Size: 0x60
332338 private struct SerializedAttribute : Encodable {
333- // TODO:
334- static func serialize( value _: Any ) -> Any ? {
335- // Mirror API
336- nil
337- }
338-
339- init ( type anyType: Any . Type ) {
340- name = nil
341- type = String ( reflecting: anyType)
342- readableType = OGTypeID ( anyType) . description
343- flags = [
344- conformsToProtocol ( anyType, _OpenSwiftUI_viewProtocolDescriptor ( ) ) ? . view : [ ] ,
345- conformsToProtocol ( anyType, _OpenSwiftUI_viewModifierProtocolDescriptor ( ) ) ? . viewModifier : [ ] ,
346- ]
347- value = nil
348- subattributes = nil
349- }
350-
351- init ( value inputValue: Any , serializeValue: Bool , label: String ? , subattributes inputSubattributes: [ SerializedAttribute ] ) {
352- name = label
353- let anyType = Swift . type ( of: inputValue)
354- type = String ( reflecting: anyType)
355- readableType = OGTypeID ( anyType) . description
356- flags = [
357- conformsToProtocol ( anyType, _OpenSwiftUI_viewProtocolDescriptor ( ) ) ? . view : [ ] ,
358- conformsToProtocol ( anyType, _OpenSwiftUI_viewModifierProtocolDescriptor ( ) ) ? . viewModifier : [ ] ,
359- ]
360- if serializeValue {
361- value = SerializedAttribute . serialize ( value: inputValue)
362- } else {
363- value = nil
364- }
365- subattributes = inputSubattributes
366- }
367-
368- struct Flags : OptionSet , Encodable {
369- let rawValue : Int
370-
371- static let view = Flags ( rawValue: 1 << 0 )
372- static let viewModifier = Flags ( rawValue: 1 << 1 )
373- }
374-
375339 let name : String ?
376340 let type : String
377341 let readableType : String
378342 let flags : Flags
379343 let value : Any ?
380344 let subattributes : [ SerializedAttribute ] ?
381345
346+ struct Flags : OptionSet , Encodable {
347+ let rawValue : Int
348+
349+ static let view = Flags ( rawValue: 1 << 0 )
350+ static let viewModifier = Flags ( rawValue: 1 << 1 )
351+ }
352+
382353 enum CodingKeys : CodingKey {
383354 case name
384355 case type
@@ -399,6 +370,52 @@ extension _ViewDebug.Data {
399370 }
400371 try container. encodeIfPresent ( subattributes, forKey: . subattributes)
401372 }
373+
374+ static func serialize( value: Any ) -> Any ? {
375+ let viewDebugValue : Any
376+ if let customValue = value as? CustomViewDebugValueConvertible {
377+ viewDebugValue = customValue. viewDebugValue
378+ } else {
379+ viewDebugValue = value
380+ }
381+ if let encodable = viewDebugValue as? Encodable {
382+ return encodable
383+ } else if let customDebugStringConvertible = viewDebugValue as? CustomDebugStringConvertible {
384+ return customDebugStringConvertible. debugDescription
385+ } else {
386+ let mirror = Mirror ( reflecting: viewDebugValue)
387+ if let displayStyle = mirror. displayStyle, displayStyle == . enum {
388+ return String ( describing: viewDebugValue)
389+ } else {
390+ return nil
391+ }
392+ }
393+ }
394+
395+ init ( type anyType: Any . Type ) {
396+ self . name = nil
397+ self . type = String ( reflecting: anyType)
398+ self . readableType = OGTypeID ( anyType) . description
399+ self . flags = [
400+ conformsToProtocol ( anyType, _OpenSwiftUI_viewProtocolDescriptor ( ) ) ? . view : [ ] ,
401+ conformsToProtocol ( anyType, _OpenSwiftUI_viewModifierProtocolDescriptor ( ) ) ? . viewModifier : [ ] ,
402+ ]
403+ self . value = nil
404+ self . subattributes = nil
405+ }
406+
407+ init ( value: Any , serializeValue: Bool , label: String ? , subattributes: [ SerializedAttribute ] ? ) {
408+ self . name = label
409+ let anyType = Swift . type ( of: value)
410+ self . type = String ( reflecting: anyType)
411+ self . readableType = OGTypeID ( anyType) . description
412+ self . flags = [
413+ conformsToProtocol ( anyType, _OpenSwiftUI_viewProtocolDescriptor ( ) ) ? . view : [ ] ,
414+ conformsToProtocol ( anyType, _OpenSwiftUI_viewModifierProtocolDescriptor ( ) ) ? . viewModifier : [ ] ,
415+ ]
416+ self . value = serializeValue ? SerializedAttribute . serialize ( value: value) : nil
417+ self . subattributes = subattributes
418+ }
402419 }
403420}
404421
0 commit comments