Allow pasting and attaching images in question Other fields on iPhone
This commit is contained in:
@@ -84,6 +84,34 @@ struct OtherEntry: View {
|
||||
|
||||
var body: some View {
|
||||
let focused = focus.wrappedValue == otherStop
|
||||
HStack(alignment: .bottom, spacing: 8) {
|
||||
entry(focused)
|
||||
#if os(iOS)
|
||||
ComposerMenu(key: key, photos: true)
|
||||
.disabled(busy)
|
||||
#endif
|
||||
}
|
||||
.onAppear {
|
||||
text = store.drafts?.textAnswer(key) ?? ""
|
||||
}
|
||||
.onChange(of: text) {
|
||||
if text != store.drafts?.textAnswer(key) ?? "" {
|
||||
store.drafts?.setTextAnswer(key, text)
|
||||
}
|
||||
store.paneFocus.setDraft(key, !trimmed(text).isEmpty)
|
||||
}
|
||||
.onChange(of: store.drafts?.textAnswer(key)) { _, new in
|
||||
if (new ?? "") != text {
|
||||
text = new ?? ""
|
||||
}
|
||||
}
|
||||
.onDisappear {
|
||||
store.paneFocus.setDraft(key, false)
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func entry(_ focused: Bool) -> some View {
|
||||
Group {
|
||||
if otherOpen(store, key) {
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
@@ -135,23 +163,6 @@ struct OtherEntry: View {
|
||||
.onHover { hover = $0 }
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
text = store.drafts?.textAnswer(key) ?? ""
|
||||
}
|
||||
.onChange(of: text) {
|
||||
if text != store.drafts?.textAnswer(key) ?? "" {
|
||||
store.drafts?.setTextAnswer(key, text)
|
||||
}
|
||||
store.paneFocus.setDraft(key, !trimmed(text).isEmpty)
|
||||
}
|
||||
.onChange(of: store.drafts?.textAnswer(key)) { _, new in
|
||||
if (new ?? "") != text {
|
||||
text = new ?? ""
|
||||
}
|
||||
}
|
||||
.onDisappear {
|
||||
store.paneFocus.setDraft(key, false)
|
||||
}
|
||||
}
|
||||
|
||||
private func open() {
|
||||
|
||||
@@ -267,12 +267,12 @@ extension View {
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
private struct ComposerMenu: View {
|
||||
struct ComposerMenu: View {
|
||||
let key: String
|
||||
let photos: Bool
|
||||
let models: [String]
|
||||
let selected: String
|
||||
let choose: @MainActor @Sendable (String) -> Void
|
||||
var models: [String] = []
|
||||
var selected = ""
|
||||
var choose: @MainActor @Sendable (String) -> Void = { _ in }
|
||||
@Environment(Store.self) private var store
|
||||
@Environment(Overlays.self) private var overlays
|
||||
@ViewState private var picking = false
|
||||
@@ -286,6 +286,11 @@ private struct ComposerMenu: View {
|
||||
} label: {
|
||||
Label("Attach photos", systemImage: "photo")
|
||||
}
|
||||
Button {
|
||||
paste()
|
||||
} label: {
|
||||
Label("Paste image", systemImage: "doc.on.clipboard")
|
||||
}
|
||||
}
|
||||
if !models.isEmpty {
|
||||
Picker("Model for the next turn", selection: Binding(get: { selected }, set: choose)) {
|
||||
@@ -316,6 +321,19 @@ private struct ComposerMenu: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func paste() {
|
||||
let images = UIPasteboard.general.images ?? []
|
||||
if images.isEmpty {
|
||||
overlays.showAlert("The clipboard has no image.")
|
||||
return
|
||||
}
|
||||
for image in images {
|
||||
if let data = image.jpegData(compressionQuality: 0.85) {
|
||||
store.images.add(key, data, name: nil, overlays)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user