-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Sources/Anima/AnimatablePropertyProvider/PropertyAnimator+Popover.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// | ||
// PropertyAnimator+Popover.swift | ||
// | ||
// | ||
// Created by Florian Zand on 03.02.24. | ||
// | ||
|
||
#if os(macOS) | ||
|
||
import AppKit | ||
|
||
extension NSPopover: AnimatablePropertyProvider { | ||
/** | ||
Provides the animatable content size. | ||
|
||
To animate the property change it's value inside an ``Anima`` animation block, To stop its animation and to change the value imminently, update it outside an animation block. | ||
*/ | ||
public var animator: PopoverAnimator { getAssociatedValue(key: "PropertyAnimator", object: self, initialValue: PopoverAnimator(self)) } | ||
} | ||
|
||
/** | ||
Provides the animatable content size. | ||
|
||
To animate the `contentSize` of a popover, change it's value inside an ``Anima`` animation block: | ||
|
||
```swift | ||
Anima.animate(withSpring: .smooth) { | ||
popover.animator.contentSize = CGSize(width: 200, height: 200) | ||
} | ||
``` | ||
To stop the animation and to change the constant immediately, change it's value outside an animation block: | ||
|
||
```swift | ||
popover.animator.contentSize = CGSize(width: 100, height: 100) | ||
``` | ||
|
||
### Accessing Animation | ||
|
||
To access the animation for `contentSize`, use ``animation(for:)``: | ||
|
||
```swift | ||
if let animation = popover.animator.animation(for: \.contentSize) { | ||
animation.stop() | ||
} | ||
``` | ||
|
||
### Accessing Animation Velocity | ||
|
||
To access the animation velocity for `contentSize`, use ``animationVelocity(for:)``. | ||
|
||
```swift | ||
if let velocity = popover.animator.animation(for: \.contentSize) { | ||
|
||
} | ||
``` | ||
*/ | ||
public class PopoverAnimator: PropertyAnimator<NSPopover> { | ||
/// The content size of the popover. | ||
public var contentSize: CGSize { | ||
get { self[\.contentSize] } | ||
set { self[\.contentSize] = newValue } | ||
} | ||
} | ||
|
||
public extension PopoverAnimator { | ||
/** | ||
The current animation for the property at the specified keypath, or `nil` if the property isn't animated. | ||
|
||
- Parameter keyPath: The keypath to an animatable property. | ||
*/ | ||
func animation<Value: AnimatableProperty>(for keyPath: WritableKeyPath<PopoverAnimator, Value>) -> AnimationProviding? { | ||
lastAccessedPropertyKey = "" | ||
_ = self[keyPath: keyPath] | ||
return animations[lastAccessedPropertyKey != "" ? lastAccessedPropertyKey : keyPath.stringValue] | ||
} | ||
|
||
/** | ||
The current animation velocity for the property at the specified keypath, or `nil` if the property isn't animated or doesn't support velocity values. | ||
|
||
- Parameter keyPath: The keypath to an animatable property. | ||
*/ | ||
func animationVelocity<Value: AnimatableProperty>(for keyPath: WritableKeyPath<PopoverAnimator, Value>) -> Value? { | ||
var velocity: Value? | ||
Anima.updateVelocity { | ||
velocity = self[keyPath: keyPath] | ||
} | ||
return velocity | ||
} | ||
} | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...cumentation/Anima.docc/Extensions/AnimatablePropertyProvider/PopoverAnimator.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# ``PopoverAnimator`` | ||
|
||
## Topics | ||
|
||
### Accessing animations | ||
|
||
- ``PropertyAnimator/animations`` | ||
- ``animation(for:)`` | ||
- ``animationVelocity(for:)`` | ||
|
||
### Animatable properties | ||
|
||
- ``contentSize`` |