1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-10-05 16:57:12 +03:00

Added search by predicate

This commit is contained in:
Anton Poltoratskyi 2020-07-14 12:54:45 +03:00
parent cfb97d6f73
commit 7d6a3b964b
2 changed files with 22 additions and 14 deletions

View File

@ -26,28 +26,28 @@ open class Group: Node {
}
// Searching
override public func nodeBy(tag: String) -> Node? {
if let node = super.nodeBy(tag: tag) {
override public func nodeBy(predicate: (String) -> Bool) -> Node? {
if let node = super.nodeBy(predicate: predicate) {
return node
}
for child in contents {
if let node = child.nodeBy(tag: tag) {
if let node = child.nodeBy(predicate: predicate) {
return node
}
}
return .none
}
override public func nodesBy(tag: String) -> [Node] {
override public func nodesBy(predicate: (String) -> Bool) -> [Node] {
var result = [Node]()
contents.forEach { child in
result.append(contentsOf: child.nodesBy(tag: tag))
result.append(contentsOf: child.nodesBy(predicate: predicate))
}
if let node = super.nodeBy(tag: tag) {
if let node = super.nodeBy(predicate: predicate) {
result.append(node)
}

View File

@ -42,16 +42,24 @@ open class Node: Drawable {
// MARK: - Searching
public func nodeBy(tag: String) -> Node? {
if self.tag.contains(tag) {
return self
}
return .none
return nodeBy(predicate: { $0 == tag })
}
public func nodesBy(tag: String) -> [Node] {
return [nodeBy(tag: tag)].compactMap { $0 }
return nodesBy(predicate: { $0 == tag })
}
public func nodeBy(predicate: (String) -> Bool) -> Node? {
if self.tag.contains(where: predicate) {
return self
}
return .none
}
public func nodesBy(predicate: (String) -> Bool) -> [Node] {
return [nodeBy(predicate: predicate)].compactMap { $0 }
}
// MARK: - Events
internal var animationObservers = [AnimationObserver]()