forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomScrollView.swift
More file actions
66 lines (60 loc) · 2.03 KB
/
CustomScrollView.swift
File metadata and controls
66 lines (60 loc) · 2.03 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
import AppKit
import Combine
import Preferences
import SwiftUI
public struct CustomScrollViewHeightPreferenceKey: SwiftUI.PreferenceKey {
public static var defaultValue: Double = 0
public static func reduce(value: inout Double, nextValue: () -> Double) {
value = nextValue() + value
}
}
public struct CustomScrollViewUpdateHeightModifier: ViewModifier {
public func body(content: Content) -> some View {
content
.background {
GeometryReader { proxy in
Color.clear
.preference(
key: CustomScrollViewHeightPreferenceKey.self,
value: proxy.size.height
)
}
}
}
}
/// Used to workaround a SwiftUI bug. https://github.com/intitni/CopilotForXcode/issues/122
public struct CustomScrollView<Content: View>: View {
@ViewBuilder var content: () -> Content
@State var height: Double = 10
@AppStorage(\.useCustomScrollViewWorkaround) var useNSScrollViewWrapper
public init(content: @escaping () -> Content) {
self.content = content
}
public var body: some View {
if useNSScrollViewWrapper {
List {
content()
.listRowInsets(EdgeInsets(top: 0, leading: -8, bottom: 0, trailing: -8))
.modifier(CustomScrollViewUpdateHeightModifier())
}
.listStyle(.plain)
.modify { view in
if #available(macOS 13.0, *) {
view.listRowSeparator(.hidden).listSectionSeparator(.hidden)
} else {
view
}
}
.frame(idealHeight: max(10, height))
.onPreferenceChange(CustomScrollViewHeightPreferenceKey.self) { newHeight in
Task { @MainActor in
height = newHeight
}
}
} else {
ScrollView {
content()
}
}
}
}