vds_ios/VDS/Protocols/ParentViewProtocol.swift
Matt Bruce b3f602087d refactored into a new file with traversal
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-08-23 08:39:18 -05:00

38 lines
1.0 KiB
Swift

//
// ParentViewProtocol.swift
// VDS
//
// Created by Matt Bruce on 8/23/24.
//
import Foundation
/// This is used in a View or Control to denote subviews of the ViewProtocol
/// type, more or less for composite views/controls.
public protocol ParentViewProtocol: ViewProtocol {
var children: [any ViewProtocol] { get }
}
extension ParentViewProtocol {
/// This will get all of the children for yourself as well as all
/// of the children within the full tree hierarchy.
/// - Returns: All children within the hierachy
public func getAllChildren() -> [any ViewProtocol] {
var allChildren = [any ViewProtocol]()
func traverse(view: any ViewProtocol) {
if let parentView = view as? any ParentViewProtocol {
for child in parentView.children {
allChildren.append(child)
traverse(view: child)
}
}
}
traverse(view: self)
return children
}
}