iOS: Add file picker (#747)

* iOS: Add file picker

* Fix lock placement
This commit is contained in:
wheremyfoodat 2025-03-17 02:55:17 +02:00 committed by GitHub
parent 90725252d3
commit fec4428ebf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 130 additions and 11 deletions

View file

@ -3,6 +3,47 @@ import SwiftUI
import MetalKit
import Darwin
var emulatorLock = NSLock()
class DocumentViewController: UIViewController, DocumentDelegate {
var documentPicker: DocumentPicker!
override func viewDidLoad() {
super.viewDidLoad()
/// set up the document picker
documentPicker = DocumentPicker(presentationController: self, delegate: self)
/// When the view loads (ie user opens the app) show the file picker
show()
}
/// callback from the document picker
func didPickDocument(document: Document?) {
if let pickedDoc = document {
let fileURL = pickedDoc.fileURL
print("Loading ROM", fileURL)
emulatorLock.lock()
iosLoadROM(fileURL.path(percentEncoded: false))
emulatorLock.unlock()
}
}
func show() {
documentPicker.displayPicker()
}
}
struct DocumentView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> DocumentViewController {
return DocumentViewController()
}
func updateUIViewController(_ uiViewController: DocumentViewController, context: Context) {
// No update needed
}
}
struct ContentView: UIViewRepresentable {
@State var showFileImporter = true
@ -32,7 +73,9 @@ struct ContentView: UIViewRepresentable {
iosCreateEmulator()
while (true) {
emulatorLock.lock()
iosRunFrame(metalLayer);
emulatorLock.unlock()
}
}
@ -46,6 +89,7 @@ struct ContentView: UIViewRepresentable {
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
DocumentView();
ContentView();
}
}