diff --git a/Sources/PopoverContainerView.swift b/Sources/PopoverContainerView.swift index 9d1a997..695c555 100644 --- a/Sources/PopoverContainerView.swift +++ b/Sources/PopoverContainerView.swift @@ -87,7 +87,7 @@ struct PopoverContainerView: View { /// Add the drag gesture. .simultaneousGesture( /// `minimumDistance: 2` is enough to allow scroll views to scroll, if one is contained in the popover. - DragGesture(minimumDistance: 2) + DragGesture(minimumDistance: Popovers.minimumDragDistance) .onChanged { value in func update() { diff --git a/Sources/PopoverGestureContainer.swift b/Sources/PopoverGestureContainer.swift index d45a6ad..dc6c6dc 100644 --- a/Sources/PopoverGestureContainer.swift +++ b/Sources/PopoverGestureContainer.swift @@ -25,7 +25,7 @@ class PopoverGestureContainer: UIView { super.layoutSubviews() /// Orientation or screen bounds changed, so update popover frames. - popoverModel.updateFrames() + popoverModel.updateFramesAfterBoundsChange() } override func didMoveToWindow() { diff --git a/Sources/PopoverModel.swift b/Sources/PopoverModel.swift index 36a973c..c845473 100644 --- a/Sources/PopoverModel.swift +++ b/Sources/PopoverModel.swift @@ -33,7 +33,7 @@ class PopoverModel: ObservableObject { @Published var selectionFrameTags: [String: CGRect] = [:] /// Force the container view to update. - func update() { + func reload() { objectWillChange.send() } @@ -49,10 +49,10 @@ class PopoverModel: ObservableObject { } /// Update all popovers. - update() + reload() } - /// Adds a `Popover` to this model/ + /// Adds a `Popover` to this model. func add(_ popover: Popover) { popovers.append(popover) } @@ -82,7 +82,7 @@ class PopoverModel: ObservableObject { This is called when the device rotates or has a bounds change. */ - func updateFrames() { + func updateFramesAfterBoundsChange() { /** First, update all popovers anyway. @@ -91,10 +91,12 @@ class PopoverModel: ObservableObject { for popover in popovers { popover.updateFrame(with: popover.context.size) } - update() + + /// Reload the container view. + reload() /// Some other popovers need to wait until the rotation has completed before updating. - DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + DispatchQueue.main.asyncAfter(deadline: .now() + Popovers.frameUpdateDelayAfterBoundsChange) { self.refresh(with: Transaction(animation: .default)) } } diff --git a/Sources/Popovers.swift b/Sources/Popovers.swift new file mode 100644 index 0000000..7860ec8 --- /dev/null +++ b/Sources/Popovers.swift @@ -0,0 +1,22 @@ +// +// Popovers.swift +// Popovers +// +// Created by A. Zheng (github.com/aheze) on 1/17/22. +// Copyright © 2022 A. Zheng. All rights reserved. +// + + +import SwiftUI + +/** + A collection of constants. + */ +public enum Popovers { + + /// The minimum distance a popover needs to be dragged before it starts getting offset. + public static var minimumDragDistance = CGFloat(2) + + /// The delay after a bounds change before recalculating popover frames. + public static var frameUpdateDelayAfterBoundsChange = CGFloat(0.6) +}