gemenon

[ACTIVE] The Safari of the Gemini ecosystem
git clone git://git.figbert.com/gemenon.git
Log | Files | Refs

commit 78d06cd6d15e70ff6cc3b6ebc0dcf18776d41584
parent 76840d1177bb344ef55d366e0dad30bcf5bbe418
Author: FIGBERT <figbert@figbert.com>
Date:   Mon, 17 Oct 2022 21:54:47 -0700

Add support for video and audio responses

Diffstat:
MShared/CapsuleView.swift | 64+++++++++++++++++++++++++++++++++++++++++++++++++---------------
Mtodo | 2+-
2 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/Shared/CapsuleView.swift b/Shared/CapsuleView.swift @@ -6,6 +6,7 @@ // import SwiftUI +import AVKit struct CapsuleView: View { @EnvironmentObject var data: BrowserData @@ -36,27 +37,60 @@ struct CapsuleView: View { struct SuccessView: View { @EnvironmentObject var data: BrowserData + + @State private var player: AVPlayer? + @State private var showMedia = false + var body: some View { - ScrollView { - HStack { - Spacer() - if case .MIME(let mime) = data.tab.response?.header { - if mime.hasPrefix("text/") { - if mime.hasPrefix("text/gemini") { - VStack(alignment: .leading, spacing: 10) { - renderGemtext((data.tab.response?.body?.gemtext)!, url: data.tab.url) + Group { + if case .MIME(let mime) = data.tab.response?.header { + if mime.hasPrefix("text/") { + ScrollView { + HStack { + Spacer() + if mime.hasPrefix("text/gemini") { + VStack(alignment: .leading, spacing: 10) { + renderGemtext((data.tab.response?.body?.gemtext)!, url: data.tab.url) + } + } else { + Text(data.tab.response?.body?.text ?? "error") + .monospaced(true).font(.system(size: 14)) } - } else { - Text(data.tab.response?.body?.text ?? "error") - .monospaced(true).font(.system(size: 14)) + Spacer() + } + .padding(.vertical) + } + } else if mime.hasPrefix("image/") { + Image(nsImage: NSImage(data: data.tab.response?.body?.raw ?? Data()) ?? NSImage()) + } else if mime.hasPrefix("video/") || mime.hasPrefix("audio/") { + if showMedia { + VideoPlayer(player: player!) + .onAppear { player?.play() } + .onDisappear { player?.pause() } + } else { + VStack { + Text("Loading media...") + .font(.system(size: 35, weight: .bold)) + Text("Initializing AVKit player") + .font(.system(size: 15)) + .foregroundColor(.gray) } - } else if mime.hasPrefix("image/") { - Image(nsImage: NSImage(data: data.tab.response?.body?.raw ?? Data()) ?? NSImage()) + .onAppear { + var ext = String(mime.trimmingPrefix("video/").trimmingPrefix("audio/")) + if ext.contains(";") { + ext = String(ext.prefix(upTo: ext.firstIndex(of: ";")!)) + } + + let name = "\(UUID().uuidString).\(ext)" + let file = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent(name) + try! data.tab.response?.body?.raw.write(to: file) + + player = AVPlayer(url: file) + showMedia = true + } } } - Spacer() } - .padding(.vertical) } } diff --git a/todo b/todo @@ -15,7 +15,7 @@ [ ] Sean Conman [ ] Egsam [ ] client certificate support -[ ] non-text mime types +[X] non-text mime types [ ] highlight code based on preformatted alt text [ ] table of contents for pages [ ] search in pages