|
1 | 1 | #include "SwiftSurface.h" |
2 | 2 |
|
| 3 | +#include <algorithm> |
| 4 | + |
3 | 5 | namespace Swift { |
4 | 6 |
|
5 | 7 | namespace { |
@@ -42,6 +44,11 @@ SwiftSurface::MemberKind memberKind(SymbolKind kind) { |
42 | 44 |
|
43 | 45 | } // namespace |
44 | 46 |
|
| 47 | +bool isSwiftUIView(const Symbol& symbol) { |
| 48 | + return std::find(symbol.conformances.begin(), symbol.conformances.end(), |
| 49 | + "s:7SwiftUI4ViewP") != symbol.conformances.end(); |
| 50 | +} |
| 51 | + |
45 | 52 | int SwiftSurface::indexOf(const std::string& symbolId) const { |
46 | 53 | auto it = typeIndex.find(symbolId); |
47 | 54 | return it == typeIndex.end() ? -1 : it->second; |
@@ -233,6 +240,53 @@ SwiftSurface describeSurface(const Module& module, |
233 | 240 | member.signature += ">" + signatureCode(surface, member.result); |
234 | 241 | surface.members.push_back(std::move(member)); |
235 | 242 | } |
| 243 | + |
| 244 | + for (const auto& type : surface.types) { |
| 245 | + const Symbol& view = *type.symbol; |
| 246 | + if (type.kind != SwiftSurface::kStruct || !isSwiftUIView(view)) { |
| 247 | + continue; |
| 248 | + } |
| 249 | + bool taken = false; |
| 250 | + for (const auto& member : surface.members) { |
| 251 | + taken |= member.owner == type.index && |
| 252 | + member.capability->jsName == "hostingController"; |
| 253 | + } |
| 254 | + if (taken) { |
| 255 | + continue; |
| 256 | + } |
| 257 | + Symbol symbol; |
| 258 | + symbol.preciseId = view.preciseId; |
| 259 | + symbol.kind = SymbolKind::InstanceMethod; |
| 260 | + symbol.name = "hostingController()"; |
| 261 | + symbol.baseName = "hostingController"; |
| 262 | + symbol.pathComponents = view.pathComponents; |
| 263 | + symbol.pathComponents.push_back(symbol.name); |
| 264 | + symbol.parentId = view.preciseId; |
| 265 | + symbol.module = view.module; |
| 266 | + symbol.docComment = |
| 267 | + "A UIHostingController showing this view (main thread only)."; |
| 268 | + surface.syntheticSymbols.push_back(std::move(symbol)); |
| 269 | + |
| 270 | + Capability capability; |
| 271 | + capability.tier = Tier::SwiftGenerated; |
| 272 | + capability.jsName = "hostingController"; |
| 273 | + capability.isMainActor = true; |
| 274 | + capability.result.bridge = Bridge::ObjCObject; |
| 275 | + capability.result.objcClass = "UIViewController"; |
| 276 | + surface.syntheticCapabilities.push_back(std::move(capability)); |
| 277 | + |
| 278 | + SwiftSurface::Member member; |
| 279 | + member.symbol = &surface.syntheticSymbols.back(); |
| 280 | + member.capability = &surface.syntheticCapabilities.back(); |
| 281 | + member.owner = type.index; |
| 282 | + member.module = type.module; |
| 283 | + member.kind = SwiftSurface::kInstanceMethod; |
| 284 | + member.flags = SwiftSurface::kMainActor; |
| 285 | + member.result = member.capability->result; |
| 286 | + member.signature = ">" + signatureCode(surface, member.result); |
| 287 | + member.hostsView = true; |
| 288 | + surface.members.push_back(std::move(member)); |
| 289 | + } |
236 | 290 | return surface; |
237 | 291 | } |
238 | 292 |
|
|
0 commit comments