Skip to content

Commit

Permalink
Expose constants to public API
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Jan 18, 2022
1 parent 268e05a commit 5d7d8aa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sources/PopoverContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/PopoverGestureContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 8 additions & 6 deletions Sources/PopoverModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PopoverModel: ObservableObject {
@Published var selectionFrameTags: [String: CGRect] = [:]

/// Force the container view to update.
func update() {
func reload() {
objectWillChange.send()
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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.
Expand All @@ -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))
}
}
Expand Down
22 changes: 22 additions & 0 deletions Sources/Popovers.swift
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 5d7d8aa

Please sign in to comment.