diff --git a/Shared/Favorites/FavoritesView.swift b/Shared/Favorites/FavoritesView.swift index 9135f9d7..faf747fd 100644 --- a/Shared/Favorites/FavoritesView.swift +++ b/Shared/Favorites/FavoritesView.swift @@ -29,6 +29,9 @@ struct FavoritesView: View { #else ForEach(favorites) { item in FavoriteItemView(item: item, dragging: $dragging) + #if os(macOS) + .workaroundForVerticalScrollingBug() + #endif } #endif } diff --git a/macOS/VerticalScrollingFix.swift b/macOS/VerticalScrollingFix.swift new file mode 100644 index 00000000..49e0b9eb --- /dev/null +++ b/macOS/VerticalScrollingFix.swift @@ -0,0 +1,42 @@ +// source: https://stackoverflow.com/a/65002837 + +import SwiftUI + +// we need this workaround only for macOS +// this is the NSView that implements proper `wantsForwardedScrollEvents` method +final class VerticalScrollingFixHostingView: NSHostingView where Content: View { + override func wantsForwardedScrollEvents(for axis: NSEvent.GestureAxis) -> Bool { + axis == .vertical + } +} + +// this is the SwiftUI wrapper for our NSView +struct VerticalScrollingFixViewRepresentable: NSViewRepresentable where Content: View { + let content: Content + + func makeNSView(context _: Context) -> NSHostingView { + VerticalScrollingFixHostingView(rootView: content) + } + + func updateNSView(_: NSHostingView, context _: Context) {} +} + +// this is the SwiftUI wrapper that makes it easy to insert the view +// into the existing SwiftUI view builders structure +struct VerticalScrollingFixWrapper: View where Content: View { + let content: () -> Content + + init(@ViewBuilder content: @escaping () -> Content) { + self.content = content + } + + var body: some View { + VerticalScrollingFixViewRepresentable(content: self.content()) + } +} + +extension View { + @ViewBuilder func workaroundForVerticalScrollingBug() -> some View { + VerticalScrollingFixWrapper { self } + } +}