diff --git a/Sources/UIComponent/Components/Layout/Other/DynamicPager.swift b/Sources/UIComponent/Components/Layout/Other/DynamicPager.swift index 73cbb8aa..f322cb95 100644 --- a/Sources/UIComponent/Components/Layout/Other/DynamicPager.swift +++ b/Sources/UIComponent/Components/Layout/Other/DynamicPager.swift @@ -22,7 +22,7 @@ public struct DynamicHPager: Component { /// - count: The total number of pages. /// - alignItems: Defines how child components are aligned along the cross axis. /// - content: A closure that provides the content for a given page index. - public init(count: Int, alignItems: CrossAxisAlignment, content: @escaping (Int) -> any Component) { + public init(count: Int, alignItems: CrossAxisAlignment = .start, content: @escaping (Int) -> any Component) { self.count = count self.alignItems = alignItems self.content = content @@ -52,7 +52,7 @@ public struct DynamicVPager: Component { /// - count: The total number of pages. /// - alignItems: Defines how child components are aligned along the cross axis. /// - content: A closure that provides the content for a given page index. - public init(count: Int, alignItems: CrossAxisAlignment, content: @escaping (Int) -> any Component) { + public init(count: Int, alignItems: CrossAxisAlignment = .start, content: @escaping (Int) -> any Component) { self.count = count self.alignItems = alignItems self.content = content diff --git a/Sources/UIComponent/Components/Layout/Other/Offset.swift b/Sources/UIComponent/Components/Layout/Other/Offset.swift index 13dc3522..0b3c18c4 100644 --- a/Sources/UIComponent/Components/Layout/Other/Offset.swift +++ b/Sources/UIComponent/Components/Layout/Other/Offset.swift @@ -58,8 +58,4 @@ struct OffsetRenderNode: RenderNode { var positions: [CGPoint] { [offset] } - - func visibleIndexes(in frame: CGRect) -> any Collection { - [0] - } } diff --git a/Sources/UIComponent/Components/Layout/Stack/StackRenderNode.swift b/Sources/UIComponent/Components/Layout/Stack/StackRenderNode.swift index 1d030169..0681fd72 100644 --- a/Sources/UIComponent/Components/Layout/Stack/StackRenderNode.swift +++ b/Sources/UIComponent/Components/Layout/Stack/StackRenderNode.swift @@ -3,7 +3,7 @@ import UIKit -/// A base render node protocol for a stack that provide implementation for ``visibleIndexes(in:)`` +/// A base render node protocol for a stack that provide implementation for ``visibleChildren(in:)`` public protocol StackRenderNode: RenderNode, BaseLayoutProtocol { /// The size of the render node. var size: CGSize { get } @@ -16,14 +16,16 @@ public protocol StackRenderNode: RenderNode, BaseLayoutProtocol { } extension StackRenderNode { - public func visibleIndexes(in frame: CGRect) -> any Collection { + public func visibleChildren(in frame: CGRect) -> [RenderNodeChild] { guard let start = firstVisibleIndex(in: frame) else { return [] } var index = start let endPoint = main(frame.origin) + main(frame.size) while index < positions.count, main(positions[index]) < endPoint { index += 1 } - return IndexSet(start.. Int? { @@ -96,18 +98,19 @@ public struct SlowRenderNode: RenderNode { self.positions = positions } - public func visibleIndexes(in frame: CGRect) -> any Collection { - var result = [Int]() + public func visibleChildren(in frame: CGRect) -> [RenderNodeChild] { + var result = [RenderNodeChild]() - for (i, origin) in positions.enumerated() { + for (i, position) in positions.enumerated() { let child = children[i] - let childFrame = CGRect(origin: origin, size: child.size) + let childFrame = CGRect(origin: position, size: child.size) if frame.intersects(childFrame) { - result.append(i) + let nodeChild = RenderNodeChild(renderNode: child, position: position, index: i) + result.append(nodeChild) } } - return IndexSet(result) + return result } } diff --git a/Sources/UIComponent/Components/Layout/Utility/VisibleBoundsObserverComponent.swift b/Sources/UIComponent/Components/Layout/Utility/VisibleBoundsObserverComponent.swift index 83d61aaf..6ca388af 100644 --- a/Sources/UIComponent/Components/Layout/Utility/VisibleBoundsObserverComponent.swift +++ b/Sources/UIComponent/Components/Layout/Utility/VisibleBoundsObserverComponent.swift @@ -45,8 +45,8 @@ public struct VisibleBoundsObserverRenderNode: RenderNodeWr self.onVisibleBoundsChanged = onVisibleBoundsChanged } - public func visibleIndexes(in frame: CGRect) -> any Collection { + public func visibleChildren(in frame: CGRect) -> [RenderNodeChild] { onVisibleBoundsChanged(size, frame) - return content.visibleIndexes(in: frame) + return content.visibleChildren(in: frame) } } diff --git a/Sources/UIComponent/Core/Model/RenderNode/AnyRenderNode.swift b/Sources/UIComponent/Core/Model/RenderNode/AnyRenderNode.swift index f4dbac78..24efb799 100644 --- a/Sources/UIComponent/Core/Model/RenderNode/AnyRenderNode.swift +++ b/Sources/UIComponent/Core/Model/RenderNode/AnyRenderNode.swift @@ -40,9 +40,6 @@ public struct AnyRenderNode: RenderNode { public var shouldRenderView: Bool { erasing.shouldRenderView } - public func visibleIndexes(in frame: CGRect) -> any Collection { - erasing.visibleIndexes(in: frame) - } public func visibleChildren(in frame: CGRect) -> [RenderNodeChild] { erasing.visibleChildren(in: frame) } @@ -95,9 +92,6 @@ public struct AnyRenderNodeOfView: RenderNode { public var shouldRenderView: Bool { erasing.shouldRenderView } - public func visibleIndexes(in frame: CGRect) -> any Collection { - erasing.visibleIndexes(in: frame) - } public func visibleChildren(in frame: CGRect) -> [RenderNodeChild] { erasing.visibleChildren(in: frame) } diff --git a/Sources/UIComponent/Core/Model/RenderNode/RenderNode.swift b/Sources/UIComponent/Core/Model/RenderNode/RenderNode.swift index 171bb11f..9d1305da 100644 --- a/Sources/UIComponent/Core/Model/RenderNode/RenderNode.swift +++ b/Sources/UIComponent/Core/Model/RenderNode/RenderNode.swift @@ -33,25 +33,18 @@ public protocol RenderNode { /// The child render nodes of this node. var children: [any RenderNode] { get } - /// Returns the indexes of the children that are visible within the given frame. - /// - /// - Parameter frame: The frame within which to determine visibility of children. - /// - Returns: The indexes of the children that are visible within the given frame. - /// - /// This method is used in the default implementation of `visibleRenderables(in:)`. - /// It won't be called if `visibleRenderables(in:)` is overwritten. - /// The default implementation for this methods is not optmized and will return all indexes regardless of the frame. - func visibleIndexes(in frame: CGRect) -> any Collection - /// Returns the render nodes that are visible within the given frame. /// /// - Parameter frame: The frame within which to determine visibility of renderables. /// - Returns: The render nodes that are visible within the given frame. The objects to return are `RenderNodeChild` which contains /// the child render node, the position relative to the parent, and the index of the child (for structure identity only, not used to access `children` or `positions`). /// - /// The default implementation calls ``RenderNode/visibleIndexes(in:)-1jtpe`` to get the visible childrens. And return the corresponding RenderNodeChild by combining data from `children` and `positions`. + /// The default implementation return all children. And return the corresponding RenderNodeChild by combining data from `children` and `positions`. func visibleChildren(in frame: CGRect) -> [RenderNodeChild] + /// Method called before `visibleChildren(in:)` to adjust the visible frame. + func adjustVisibleFrame(frame: CGRect) -> CGRect + /// Creates a new view instance for this render node. func makeView() -> View @@ -59,10 +52,6 @@ public protocol RenderNode { /// /// - Parameter view: The view to update. func updateView(_ view: View) - - - /// Method called before `visibleChildren(in:)` to adjust the visible frame. - func adjustVisibleFrame(frame: CGRect) -> CGRect } // MARK: - Helper methods @@ -129,12 +118,8 @@ extension RenderNode { public var children: [any RenderNode] { [] } public var positions: [CGPoint] { [] } - public func visibleIndexes(in frame: CGRect) -> any Collection { - IndexSet(0.. [RenderNodeChild] { - visibleIndexes(in: frame).map { + (0.. any Collection { - content.visibleIndexes(in: frame) - } public func visibleChildren(in frame: CGRect) -> [RenderNodeChild] { content.visibleChildren(in: frame) } diff --git a/Sources/UIComponent/Documentation.docc/RenderNode.md b/Sources/UIComponent/Documentation.docc/RenderNode.md index 710947cf..ea15be3a 100644 --- a/Sources/UIComponent/Documentation.docc/RenderNode.md +++ b/Sources/UIComponent/Documentation.docc/RenderNode.md @@ -26,7 +26,7 @@ To create a custom ``RenderNode``, you need to write a struct that conforms to ` - ``RenderNode/size`` - ``RenderNode/positions-6f59e`` - ``RenderNode/children-42h1l`` - - ``RenderNode/visibleIndexes(in:)-65qgr`` + - ``RenderNode/visibleChildren(in:)-4nvhh`` - ``RenderNode/shouldRenderView(in:)-yvrd`` - ``RenderNode/makeView()-8w5z2`` - ``RenderNode/updateView(_:)-2xjz4``