-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathHoverableImageView.swift
More file actions
160 lines (145 loc) · 5.88 KB
/
HoverableImageView.swift
File metadata and controls
160 lines (145 loc) · 5.88 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import SwiftUI
import ComposableArchitecture
import Persist
import ConversationServiceProvider
import GitHubCopilotService
import SharedUIComponents
public struct HoverableImageView: View {
@Environment(\.colorScheme) var colorScheme
let image: ImageReference
let chat: StoreOf<Chat>
@State private var isHovered = false
@State private var hoverTask: Task<Void, Never>?
@State private var isSelectedModelSupportVision = AppState.shared.isSelectedModelSupportVision() ?? CopilotModelManager.getDefaultChatModel(scope: AppState.shared.modelScope())?.supportVision ?? false
@State private var showPopover = false
let maxWidth: CGFloat = 330
let maxHeight: CGFloat = 160
private var visionNotSupportedOverlay: some View {
Group {
if !isSelectedModelSupportVision {
ZStack {
Color.clear
.background(.regularMaterial)
.opacity(0.4)
.clipShape(RoundedRectangle(cornerRadius: hoverableImageCornerRadius))
VStack(alignment: .center, spacing: 8) {
Image(systemName: "eye.slash")
.font(.system(size: 14, weight: .semibold))
Text("Vision not supported by current model")
.font(.system(size: 12, weight: .semibold))
.multilineTextAlignment(.center)
.padding(.horizontal, 20)
}
.foregroundColor(colorScheme == .dark ? .primary : .white)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.colorScheme(colorScheme == .dark ? .light : .dark)
}
}
}
private var borderOverlay: some View {
RoundedRectangle(cornerRadius: hoverableImageCornerRadius)
.strokeBorder(Color(nsColor: .separatorColor), lineWidth: 1)
}
private var removeButton: some View {
Button(action: {
chat.send(.removeSelectedImage(image))
}) {
Image(systemName: "xmark")
.foregroundColor(.primary)
.scaledFont(.system(size: 13))
.frame(width: 24, height: 24)
.background(
RoundedRectangle(cornerRadius: hoverableImageCornerRadius)
.fill(Color.contentBackground.opacity(0.72))
.shadow(color: .black.opacity(0.3), radius: 1.5, x: 0, y: 0)
.shadow(color: .black.opacity(0.25), radius: 50, x: 0, y: 36)
)
}
.buttonStyle(.plain)
.padding(1)
.onHover { buttonHovering in
hoverTask?.cancel()
if buttonHovering {
isHovered = true
}
}
}
private var hoverOverlay: some View {
Group {
if isHovered {
VStack {
Spacer()
HStack {
removeButton
Spacer()
}
}
}
}
}
private var baseImageView: some View {
let (image, nsImage) = loadImageFromData(data: image.data)
let imageSize = nsImage?.size ?? CGSize(width: maxWidth, height: maxHeight)
let isWideImage = imageSize.height < 160 && imageSize.width >= maxWidth
return image
.resizable()
.aspectRatio(contentMode: isWideImage ? .fill : .fit)
.blur(radius: !isSelectedModelSupportVision ? 2.5 : 0)
.frame(
width: isWideImage ? min(imageSize.width, maxWidth) : nil,
height: isWideImage ? min(imageSize.height, maxHeight) : maxHeight,
alignment: .leading
)
.clipShape(
RoundedRectangle(cornerRadius: hoverableImageCornerRadius),
style: .init(eoFill: true, antialiased: true)
)
}
private func handleHover(_ hovering: Bool) {
hoverTask?.cancel()
if hovering {
isHovered = true
} else {
// Add a small delay before hiding to prevent flashing
hoverTask = Task {
try? await Task.sleep(nanoseconds: 10_000_000) // 0.01 seconds
if !Task.isCancelled {
isHovered = false
}
}
}
}
private func updateVisionSupport() {
isSelectedModelSupportVision = AppState.shared.isSelectedModelSupportVision() ?? CopilotModelManager.getDefaultChatModel(scope: AppState.shared.modelScope())?.supportVision ?? false
}
public var body: some View {
if NSImage(data: image.data) != nil {
baseImageView
.frame(height: maxHeight, alignment: .leading)
.background(
Color(nsColor: .windowBackgroundColor).opacity(0.5)
)
.overlay(visionNotSupportedOverlay)
.overlay(borderOverlay)
.onHover(perform: handleHover)
.overlay(hoverOverlay)
.onReceive(NotificationCenter.default.publisher(for: .gitHubCopilotSelectedModelDidChange)) { _ in
updateVisionSupport()
}
.onTapGesture {
showPopover.toggle()
}
.popover(isPresented: $showPopover, attachmentAnchor: .rect(.bounds), arrowEdge: .bottom) {
PopoverImageView(data: image.data)
}
}
}
}
public func loadImageFromData(data: Data) -> (image: Image, nsImage: NSImage?) {
if let nsImage = NSImage(data: data) {
return (Image(nsImage: nsImage), nsImage)
} else {
return (Image(systemName: "photo.trianglebadge.exclamationmark"), nil)
}
}