Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Jan 18, 2022
1 parent 5d7d8aa commit 927afee
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 26 deletions.
Binary file removed Examples/PopoversPlaygroundsApp.swiftpm.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct AccessibilityView: View {

Text("By default, a \(Image(systemName: "xmark.circle.fill")) button will appear next to popovers when VoiceOver is on. You can customize this with `attributes.accessibility.dismissButtonLabel`.")
}

HStack {
ExampleImage.tip

Expand Down
Binary file removed Examples/PopoversXcodeApp.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct AccessibilityView: View {

Text("By default, a \(Image(systemName: "xmark.circle.fill")) button will appear next to popovers when VoiceOver is on. You can customize this with `attributes.accessibility.dismissButtonLabel`.")
}

HStack {
ExampleImage.tip

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ struct PopoverView: View {
<br>

## Customization
| [🔖](https://github.com/aheze/Popovers#tag--string)| [💠](https://github.com/aheze/Popovers#position--position)| [](https://github.com/aheze/Popovers#source-frame-----cgrect)| [🔲](https://github.com/aheze/Popovers#source-frame-inset--uiedgeinsets)| [](https://github.com/aheze/Popovers#screen-edge-padding--uiedgeinsets)| [🟩](https://github.com/aheze/Popovers#presentation--presentation)| [🟥](https://github.com/aheze/Popovers#dismissal--dismissal)| [🎾](https://github.com/aheze/Popovers#rubber-banding-mode--rubberbandingmode)| [🛑](https://github.com/aheze/Popovers#blocks-background-touches--bool)| [👓](https://github.com/aheze/Popovers#blocks-background-touches--bool)| [👉](https://github.com/aheze/Popovers#on-tap-outside-----void)| [🎈](https://github.com/aheze/Popovers#on-dismiss-----void)| [🔰](https://github.com/aheze/Popovers#on-context-change--context---void)|
| [🔖](https://github.com/aheze/Popovers#tag--string)| [💠](https://github.com/aheze/Popovers#position--position)| [](https://github.com/aheze/Popovers#source-frame-----cgrect)| [🔲](https://github.com/aheze/Popovers#source-frame-inset--uiedgeinsets)| [](https://github.com/aheze/Popovers#screen-edge-padding--uiedgeinsets)| [🟩](https://github.com/aheze/Popovers#presentation--presentation)| [🟥](https://github.com/aheze/Popovers#dismissal--dismissal)| [🎾](https://github.com/aheze/Popovers#rubber-banding-mode--rubberbandingmode)| [🛑](https://github.com/aheze/Popovers#blocks-background-touches--bool)| [👓](https://github.com/aheze/Popovers#accessibility--accessibility--v120)| [👉](https://github.com/aheze/Popovers#on-tap-outside-----void)| [🎈](https://github.com/aheze/Popovers#on-dismiss-----void)| [🔰](https://github.com/aheze/Popovers#on-context-change--context---void)|
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |

Customize popovers through the `Attributes` struct. Pretty much everything is customizable, including positioning, animations, and dismissal behavior.
Expand Down
6 changes: 3 additions & 3 deletions Sources/Popover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ public struct Popover: Identifiable {
.background(Color.black.opacity(0.25))
.cornerRadius(18)
)
.accessibilityElement()
.accessibility(label: Text("Close"))
.accessibility(hint: Text("Dismiss this popover."))
.accessibilityElement()
.accessibility(label: Text("Close"))
.accessibility(hint: Text("Dismiss this popover."))
)
}
}
Expand Down
13 changes: 5 additions & 8 deletions Sources/PopoverContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ struct PopoverContainerView: View {
/// `minimumDistance: 2` is enough to allow scroll views to scroll, if one is contained in the popover.
DragGesture(minimumDistance: Popovers.minimumDragDistance)
.onChanged { value in

func update() {
/// Apply the offset.
applyDraggingOffset(popover: popover, translation: value.translation)

/// Update the visual frame to account for the dragging offset.
popover.context.frame = CGRect(
origin: popover.context.staticFrame.origin + CGPoint(
Expand All @@ -103,17 +103,15 @@ struct PopoverContainerView: View {
size: popover.context.size ?? .zero
)
}

/// Select the popover for dragging.
if selectedPopover == nil {

/// Apply an animation to make up for the `minimumDistance`.
withAnimation(.spring()) {
selectedPopover = popover
update()
}
} else {

/// The user is already dragging, so update the frames immediately.
update()
}
Expand Down Expand Up @@ -180,9 +178,8 @@ struct PopoverContainerView: View {

/// Apply the additional offset needed if a popover is dragged.
func applyDraggingOffset(popover: Popover, translation: CGSize) {

var selectedPopoverOffset = CGSize.zero

/// If `.dragDown` or `.dragUp` is in the popover's dismissal mode, then apply rubber banding.
func applyVerticalOffset(dragDown: Bool) {
let condition = dragDown ? translation.height <= 0 : translation.height >= 0
Expand Down Expand Up @@ -219,7 +216,7 @@ struct PopoverContainerView: View {
selectedPopoverOffset = translation
}
}

self.selectedPopoverOffset = selectedPopoverOffset
}

Expand Down
7 changes: 3 additions & 4 deletions Sources/PopoverGestureContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PopoverGestureContainer: UIView {

override func layoutSubviews() {
super.layoutSubviews()

/// Orientation or screen bounds changed, so update popover frames.
popoverModel.updateFramesAfterBoundsChange()
}
Expand Down Expand Up @@ -111,7 +111,6 @@ class PopoverGestureContainer: UIView {
if popover.attributes.dismissal.mode.contains(.tapOutside) {
let excludedFrames = popover.attributes.dismissal.excludedFrames()
if excludedFrames.contains(where: { $0.contains(point) }) {

/**
The touch hit an excluded view, so don't dismiss it.
However, if the touch hit another popover, block it from passing through.
Expand All @@ -131,13 +130,13 @@ class PopoverGestureContainer: UIView {
/// The touch did not hit any popover, so pass it through to the hit testing target.
return nil
}

/// Dismiss all popovers if the accessibility escape gesture was performed.
override func accessibilityPerformEscape() -> Bool {
for popover in popoverModel.popovers {
popover.dismiss()
}

return true
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/PopoverModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ class PopoverModel: ObservableObject {
func updateFramesAfterBoundsChange() {
/**
First, update all popovers anyway.
For some reason, relative positioning + `.center` doesn't need the rotation animation to complete before having a size change.
*/
for popover in popovers {
popover.updateFrame(with: popover.context.size)
}

/// Reload the container view.
reload()

/// Some other popovers need to wait until the rotation has completed before updating.
DispatchQueue.main.asyncAfter(deadline: .now() + Popovers.frameUpdateDelayAfterBoundsChange) {
self.refresh(with: Transaction(animation: .default))
Expand Down
3 changes: 1 addition & 2 deletions Sources/PopoverUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public extension View {

/**
Read a view's size. The closure is called whenever the size itself changes, or the transaction changes (in the event of a screen rotation.)
From https://stackoverflow.com/a/66822461/14351818
*/
func sizeReader(transaction: Transaction?, size: @escaping (CGSize) -> Void) -> some View {
Expand Down Expand Up @@ -74,7 +74,6 @@ extension Transaction: Equatable {
}
}


struct ContentFrameReaderPreferenceKey: PreferenceKey {
static var defaultValue: CGRect { return CGRect() }
static func reduce(value: inout CGRect, nextValue: () -> CGRect) { value = nextValue() }
Expand Down
4 changes: 1 addition & 3 deletions Sources/Popovers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
// 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 927afee

Please sign in to comment.