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
Original file line number Diff line number Diff line change
@@ -1,52 +1,167 @@
import SwiftUI
import UniformTypeIdentifiers
import UIKit

/// How a pasted or dropped attachment reached the composer, for naming and analytics (#870).
enum SessionChatAttachmentSource: String {
case paste
case drop
}

/// Makes Session Chat a drop target for images and files (#870). While accepted items are over the
/// chat, `summary` drives the drag-over card; a drag that carries only text shows no card and is
/// left to the system (the prompt field still takes it as text).
struct SessionChatAttachmentDropDelegate: DropDelegate {
/// The drag-over card through one drag: shown while attachable items are over the chat, cleared
/// when the drag leaves, drops or ends. A drag that carries only text shows no card and is left to
/// the system (the prompt field still takes it as text).
struct SessionChatAttachmentDropTracker {
private(set) var summary: AttachmentDropSummary?

/// The drag moved. `inside` is false while it is outside the chat or drops are off.
mutating func update(inside: Bool, items: () -> [AttachmentDropItem], context: () -> AttachmentDropContext) {
guard inside else {
summary = nil
return
}
if summary == nil { summary = AttachmentDropRouting.summary(items(), context: context()) }
}

/// What dropping here would do: `.cancel` when the drag is not the chat's to take.
var operation: UIDropOperation {
guard let summary else { return .cancel }
return summary.rejected ? .forbidden : .copy
}

/// The drag left, was dropped, or ended anywhere, including after the chat declined it (#951).
/// Returns whether a drop should attach.
@discardableResult
mutating func finish() -> Bool {
let accepted = summary.map { !$0.rejected } ?? false
summary = nil
return accepted
}
}

extension View {
/// Makes Session Chat a drop target for images and files (#870). While attachable items are
/// over the chat, `summary` drives the drag-over card.
///
/// Drops go through UIKit: SwiftUI's drop delegate never reports the end of a drag it declined,
/// so the "Can't attach this" card stayed after the drag (#951).
func sessionChatAttachmentDrop(
isEnabled: Bool,
context: @escaping () -> AttachmentDropContext,
summary: Binding<AttachmentDropSummary?>,
onDrop: @escaping ([NSItemProvider]) -> Void
) -> some View {
background(
SessionChatAttachmentDropTarget(
isEnabled: isEnabled,
context: context,
summary: summary,
onDrop: onDrop
)
)
}
}

/// An empty view behind the chat that marks its bounds and puts the drop interaction on the view
/// that hosts it. A drag looks for a drop interaction from the view under it upwards, so only an
/// ancestor of the timeline and composer sees drags over them.
private struct SessionChatAttachmentDropTarget: UIViewRepresentable {
let isEnabled: Bool
let context: () -> AttachmentDropContext
@Binding var summary: AttachmentDropSummary?
let onDrop: ([NSItemProvider]) -> Void

static func items(_ providers: [NSItemProvider]) -> [AttachmentDropItem] {
providers.compactMap { provider in
AttachmentItemProviders.kind(of: provider).map {
AttachmentDropItem(kind: $0, filename: provider.suggestedName)
}
}
}
func makeCoordinator() -> Coordinator { Coordinator() }

func validateDrop(info: DropInfo) -> Bool {
isEnabled && !Self.items(info.itemProviders(for: [.item])).isEmpty
func makeUIView(context: Context) -> AnchorView {
let view = AnchorView()
view.isUserInteractionEnabled = false
view.coordinator = context.coordinator
return view
}

func dropEntered(info: DropInfo) {
summary = AttachmentDropRouting.summary(Self.items(info.itemProviders(for: [.item])), context: context())
func updateUIView(_ view: AnchorView, context: Context) {
let coordinator = context.coordinator
coordinator.isEnabled = isEnabled
coordinator.context = self.context
coordinator.onDrop = onDrop
let summary = $summary
coordinator.setSummary = { if summary.wrappedValue != $0 { summary.wrappedValue = $0 } }
}

func dropUpdated(info: DropInfo) -> DropProposal? {
guard let summary else { return DropProposal(operation: .forbidden) }
return DropProposal(operation: summary.rejected ? .forbidden : .copy)
static func dismantleUIView(_ view: AnchorView, coordinator: Coordinator) {
coordinator.uninstall()
}

func dropExited(info: DropInfo) {
summary = nil
final class AnchorView: UIView {
weak var coordinator: Coordinator?

override func didMoveToWindow() {
super.didMoveToWindow()
coordinator?.install(anchor: self)
}
}

func performDrop(info: DropInfo) -> Bool {
let accepted = summary.map { !$0.rejected } ?? false
summary = nil
guard accepted else { return false }
onDrop(info.itemProviders(for: [.item]))
return true
final class Coordinator: NSObject, UIDropInteractionDelegate {
var isEnabled = false
var context: () -> AttachmentDropContext = { AttachmentDropContext() }
var onDrop: ([NSItemProvider]) -> Void = { _ in }
var setSummary: (AttachmentDropSummary?) -> Void = { _ in }
private var tracker = SessionChatAttachmentDropTracker()
private weak var anchor: UIView?
private lazy var interaction = UIDropInteraction(delegate: self)

func install(anchor: UIView) {
self.anchor = anchor
interaction.view?.removeInteraction(interaction)
// SwiftUI wraps the anchor in a host view of its own size; the view above that hosts the chat.
guard anchor.window != nil, let host = anchor.superview?.superview else { return }
host.addInteraction(interaction)
}

func uninstall() {
interaction.view?.removeInteraction(interaction)
finish()
}

@discardableResult
func finish() -> Bool {
let accepted = tracker.finish()
setSummary(nil)
return accepted
}

func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
isEnabled && !Self.items(session).isEmpty
}

func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
// The hosting view can be wider than the chat (a split view sidebar); only the chat drops.
let inside = anchor.map { $0.bounds.contains(session.location(in: $0)) } ?? false
tracker.update(inside: isEnabled && inside, items: { Self.items(session) }, context: context)
setSummary(tracker.summary)
return UIDropProposal(operation: tracker.operation)
}

func dropInteraction(_ interaction: UIDropInteraction, sessionDidExit session: UIDropSession) {
finish()
}

func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
let providers = session.items.map(\.itemProvider)
if finish() { onDrop(providers) }
}

func dropInteraction(_ interaction: UIDropInteraction, sessionDidEnd session: UIDropSession) {
finish()
}

private static func items(_ session: UIDropSession) -> [AttachmentDropItem] {
session.items.compactMap { item in
AttachmentItemProviders.kind(of: item.itemProvider).map {
AttachmentDropItem(kind: $0, filename: item.itemProvider.suggestedName)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,11 @@ struct SessionChatView: View {
let sheets = sessionChatPickerSheets(sessionChatSheets(sessionChatLayout))
let observed = sessionChatObservers(sessionChatDestinations(sheets))
return sessionChatMoveToWorktree(sessionChatRoutes(sessionChatDialogs(sessionChatLifecycle(observed))))
.onDrop(
of: [.item],
delegate: SessionChatAttachmentDropDelegate(
isEnabled: attachmentDropEnabled,
context: { attachmentDropContext },
summary: $attachmentDropSummary,
onDrop: { attachItemProviders($0, source: .drop) }
)
.sessionChatAttachmentDrop(
isEnabled: attachmentDropEnabled,
context: { attachmentDropContext },
summary: $attachmentDropSummary,
onDrop: { attachItemProviders($0, source: .drop) }
)
.overlay {
if let summary = attachmentDropSummary {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Testing
import UIKit
@testable import OpenCode_Mobile

struct SessionChatAttachmentDropTrackerTests {
private let image = [AttachmentDropItem(kind: .image, filename: "photo.png")]
private let noImages = AttachmentDropContext(imagesSupported: false)

@Test func acceptedDragShowsTheCardAndDropAttaches() {
var tracker = SessionChatAttachmentDropTracker()
tracker.update(inside: true, items: { image }, context: { AttachmentDropContext() })

#expect(tracker.summary?.rejected == false)
#expect(tracker.operation == .copy)
let attaches = tracker.finish()
#expect(attaches)
#expect(tracker.summary == nil)
}

@Test func declinedDragClearsTheCardWhenItEnds() {
var tracker = SessionChatAttachmentDropTracker()
tracker.update(inside: true, items: { image }, context: { noImages })

#expect(tracker.summary?.rejected == true)
#expect(tracker.summary?.subtitle == AttachmentDropRouting.imageUnsupportedMessage)
#expect(tracker.operation == .forbidden)
let attaches = tracker.finish()
#expect(!attaches)
#expect(tracker.summary == nil)
}

@Test func leavingTheChatClearsTheCardAndCancels() {
var tracker = SessionChatAttachmentDropTracker()
tracker.update(inside: true, items: { image }, context: { noImages })
tracker.update(inside: false, items: { image }, context: { noImages })

#expect(tracker.summary == nil)
#expect(tracker.operation == .cancel)
}

@Test func textOnlyDragShowsNoCard() {
var tracker = SessionChatAttachmentDropTracker()
tracker.update(inside: true, items: { [] }, context: { AttachmentDropContext() })

#expect(tracker.summary == nil)
#expect(tracker.operation == .cancel)
let attaches = tracker.finish()
#expect(!attaches)
}

@Test func summaryIsWordedOncePerDrag() {
var tracker = SessionChatAttachmentDropTracker()
var routed = 0
for _ in 0 ..< 3 {
tracker.update(inside: true, items: { routed += 1; return image }, context: { AttachmentDropContext() })
}

#expect(routed == 1)
}
}