-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathImageReferenceItemView.swift
More file actions
70 lines (64 loc) · 2.2 KB
/
ImageReferenceItemView.swift
File metadata and controls
70 lines (64 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import ConversationServiceProvider
import SwiftUI
import Foundation
import SharedUIComponents
struct ImageReferenceItemView: View {
let item: ImageReference
@State private var showPopover = false
private func getImageTitle() -> String {
switch item.source {
case .file:
if let fileUrl = item.fileUrl {
return fileUrl.lastPathComponent
} else {
return "Attached Image"
}
case .pasted:
return "Pasted Image"
case .screenshot:
return "Screenshot"
}
}
var body: some View {
HStack(alignment: .center, spacing: 4) {
let image = loadImageFromData(data: item.data).image
image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 28, height: 28)
.clipShape(RoundedRectangle(cornerRadius: 1.72))
.overlay(
RoundedRectangle(cornerRadius: 1.72)
.inset(by: 0.21)
.stroke(Color(nsColor: .separatorColor), lineWidth: 0.43)
)
let text = getImageTitle()
let font = NSFont.systemFont(ofSize: 12)
let attributes = [NSAttributedString.Key.font: font]
let size = (text as NSString).size(withAttributes: attributes)
let textWidth = min(size.width, 105)
Text(text)
.lineLimit(1)
.scaledFont(size: 12)
.foregroundColor(.primary.opacity(0.85))
.truncationMode(.middle)
.frame(width: textWidth, alignment: .leading)
}
.padding(4)
.background(
Color(nsColor: .windowBackgroundColor).opacity(0.5)
)
.cornerRadius(4)
.overlay(
RoundedRectangle(cornerRadius: 4)
.inset(by: 0.5)
.stroke(Color(nsColor: .separatorColor), lineWidth: 1)
)
.popover(isPresented: $showPopover, arrowEdge: .bottom) {
PopoverImageView(data: item.data)
}
.onTapGesture {
self.showPopover = true
}
}
}