Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ios/Xum/Kit/Attachments.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CoreTransferable
import Foundation
import ImageIO
import UniformTypeIdentifiers
Expand All @@ -10,6 +11,34 @@ nonisolated struct PickedFile: Sendable {
var mediaType: String?
}

/// An image pasted into the composer's field. Text-only items carry none: the field pastes
/// those itself.
nonisolated struct PastedImage: Transferable, Equatable {
var data: Data
var type: UTType

/// The first representation the item has wins: PNG keeps a screenshot lossless, so it goes
/// before the JPEG copy some apps put beside it.
static var transferRepresentation: some TransferRepresentation {
DataRepresentation(importedContentType: .png) { PastedImage(data: $0, type: .png) }
DataRepresentation(importedContentType: .jpeg) { PastedImage(data: $0, type: .jpeg) }
DataRepresentation(importedContentType: .image) { PastedImage(data: $0, type: sniffedType($0)) }
}

/// An item that only says "image" (HEIC from Photos, for one) is named by its bytes.
static func sniffedType(_ data: Data) -> UTType {
CGImageSourceCreateWithData(data as CFData, nil).flatMap(CGImageSourceGetType).flatMap { UTType($0 as String) } ?? .png
}

static func files(_ images: [PastedImage]) -> [PickedFile] {
images.enumerated().map { i, image in
let name = images.count == 1 ? "image" : "image-\(i + 1)"
return PickedFile(data: image.data, filename: "\(name).\(image.type.preferredFilenameExtension ?? "png")",
mediaType: image.type.preferredMIMEType)
}
}
}

/// A provider file part (`FilePartSchema`): a `data:` URL sent inline with the message.
nonisolated struct FilePart: Hashable, Sendable {
var url: String
Expand Down
23 changes: 23 additions & 0 deletions ios/Xum/Screens/Chat/Composer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct Composer: View {
.padding(.horizontal, 2)
.focused($focused)
.frame(minHeight: oneRow ? ComposerMetrics.roundButton : nil)
.modifier(PasteImages(enabled: onPick != nil, attach: attach))
}

private static func load(_ items: [PhotosPickerItem]) async -> [PickedFile] {
Expand All @@ -138,6 +139,12 @@ struct Composer: View {
return PickedFile(data: data, filename: url.lastPathComponent, mediaType: UTType(filenameExtension: url.pathExtension)?.preferredMIMEType)
}

private func attach(_ images: [PastedImage]) {
guard !images.isEmpty else { return }
let files = PastedImage.files(images)
onPick?(files.count) { files }
}

private func pasteImage() {
let board = UIPasteboard.general
if let png = board.data(forPasteboardType: UTType.png.identifier) {
Expand Down Expand Up @@ -259,3 +266,19 @@ struct Composer: View {
}
}
}

/// The field takes pasted images (Mike), on iOS 27, the first release with a paste destination
/// on iPhone; before it the paperclip's Paste Image stays the way in.
private struct PasteImages: ViewModifier {
/// Fixed for a composer: the new chat's composer has no attachments.
var enabled: Bool
var attach: ([PastedImage]) -> Void

func body(content: Content) -> some View {
if #available(iOS 27.0, *), enabled {
content.pasteDestination(for: PastedImage.self, action: attach)
} else {
content
}
}
}
59 changes: 59 additions & 0 deletions ios/XumTests/PastedImageTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import ImageIO
import Testing
import UIKit
import UniformTypeIdentifiers
@testable import Xum

private let image = UIGraphicsImageRenderer(size: CGSize(width: 4, height: 4)).image { ctx in
UIColor.red.setFill()
ctx.fill(CGRect(x: 0, y: 0, width: 4, height: 4))
}

private func provider(_ items: [(UTType, Data)]) -> NSItemProvider {
let p = NSItemProvider()
for (type, data) in items {
p.registerDataRepresentation(for: type) { done in done(data, nil); return nil }
}
return p
}

/// What a paste of the item hands the composer: the item's image, imported as the field does.
private func load(_ provider: NSItemProvider) async -> PastedImage? {
await withCheckedContinuation { done in
_ = provider.loadTransferable(type: PastedImage.self) { done.resume(returning: try? $0.get()) }
}
}

private func tiff() -> Data {
let out = NSMutableData()
let dest = CGImageDestinationCreateWithData(out, UTType.tiff.identifier as CFString, 1, nil)!
CGImageDestinationAddImage(dest, image.cgImage!, nil)
CGImageDestinationFinalize(dest)
return out as Data
}

struct PastedImageTests {
@Test func aScreenshotPastesAsItsLosslessCopy() async {
let png = image.pngData()!, jpeg = image.jpegData(compressionQuality: 0.8)!
#expect(await load(provider([(.jpeg, jpeg), (.png, png)])) == PastedImage(data: png, type: .png))
#expect(await load(provider([(.jpeg, jpeg)])) == PastedImage(data: jpeg, type: .jpeg))
}

@Test func anImageOfAnotherFormatKeepsItsFormat() async {
let data = tiff()
#expect(await load(provider([(.tiff, data)])) == PastedImage(data: data, type: .tiff))
}

@Test func textIsNoImage() async {
#expect(await load(provider([(.utf8PlainText, Data("hello".utf8))])) == nil)
}

@Test func eachPastedImageBecomesAFileOfItsType() {
let one = PastedImage.files([PastedImage(data: Data([1]), type: .png)])
#expect(one.map(\.filename) == ["image.png"])
let two = PastedImage.files([PastedImage(data: Data([1]), type: .png), PastedImage(data: Data([2]), type: .jpeg)])
#expect(two.map(\.filename) == ["image-1.png", "image-2.jpeg"])
#expect(two.map(\.mediaType) == ["image/png", "image/jpeg"])
#expect(two.map(\.data) == [Data([1]), Data([2])])
}
}
Loading