Skip to content

Commit 743a488

Browse files
committed
Made it work for non identifiables
StressTestExample is broken
1 parent d582598 commit 743a488

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

Sources/SwiftCrossUI/Views/Button.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
/// A control that initiates an action.
24
public struct Button: Sendable {
35
/// The label to show on the button.

Sources/SwiftCrossUI/Views/ForEach.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ extension ForEach: TypeSafeView, View where Child: View {
255255
// a huge issue, but definitely something to keep an eye on.
256256
var layoutableChildren: [LayoutSystem.LayoutableChild] = []
257257

258-
let oldNodes = children.nodes
259258
let oldMap = children.nodeIdentifierMap
260259
let oldIdentifiers = children.identifiers
261260
let identifiersStart = oldIdentifiers.startIndex
@@ -269,11 +268,11 @@ extension ForEach: TypeSafeView, View where Child: View {
269268
// rendered in the correct order.
270269
var requiresOngoingReinsertion = false
271270

272-
for (i, element) in elements.enumerated() {
271+
for element in elements {
273272
let childContent = child(element)
274273
let node: AnyViewGraphNode<Child>
275274

276-
if let oldNode = oldMap[element.id] {
275+
if let oldNode = oldMap[element[keyPath: idKeyPath]] {
277276
node = oldNode
278277

279278
// Checks if there is a preceding item that was not preceding in
@@ -284,7 +283,8 @@ extension ForEach: TypeSafeView, View where Child: View {
284283
requiresOngoingReinsertion
285284
|| {
286285
guard
287-
let ownOldIndex = oldIdentifiers.firstIndex(of: element.id)
286+
let ownOldIndex = oldIdentifiers.firstIndex(
287+
of: element[keyPath: idKeyPath])
288288
else { return false }
289289

290290
let subset = oldIdentifiers[identifiersStart..<ownOldIndex]
@@ -297,7 +297,7 @@ extension ForEach: TypeSafeView, View where Child: View {
297297
}
298298
} else {
299299
// New Items need ongoing reinsertion to get
300-
// displayed at the correct location.
300+
// displayed at the correct locat ion.
301301
requiresOngoingReinsertion = true
302302
node = AnyViewGraphNode(
303303
for: childContent,

Sources/SwiftCrossUI/Views/Menu.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public struct Menu: Sendable {
2626
ResolvedMenu(
2727
items: items.map { item in
2828
switch item {
29-
case .button(let button):
29+
case .button(let button, _):
3030
.button(button.label, button.action)
31-
case .text(let text):
31+
case .text(let text, _):
3232
.button(text.string, nil)
33-
case .submenu(let submenu):
33+
case .submenu(let submenu, _):
3434
.submenu(submenu.resolve())
3535
}
3636
}
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
/// An item of a ``Menu`` or ``CommandMenu``.
22
public enum MenuItem: Sendable {
3-
case button(Button)
4-
case text(Text)
5-
case submenu(Menu)
3+
case button(Button, Int? = nil)
4+
case text(Text, Int? = nil)
5+
case submenu(Menu, Int? = nil)
6+
}
7+
8+
extension MenuItem: Hashable {
9+
public static func == (lhs: MenuItem, rhs: MenuItem) -> Bool {
10+
lhs.hashValue == rhs.hashValue
11+
}
12+
13+
public func hash(into hasher: inout Hasher) {
14+
hasher.combine(
15+
{
16+
switch self {
17+
case .button(_, let int), .text(_, let int), .submenu(_, let int):
18+
return int ?? 0
19+
}
20+
}())
21+
}
22+
23+
package func addingIDIfNeeded(id: Int) -> Self {
24+
switch self {
25+
case .button(let button, let int):
26+
return .button(button, int ?? id)
27+
case .text(let text, let int):
28+
return .text(text, int ?? id)
29+
case .submenu(let menu, let int):
30+
return .submenu(menu, int ?? id)
31+
}
32+
}
633
}

0 commit comments

Comments
 (0)