Skip to content

Commit 1014bf2

Browse files
committed
Update configuration
1 parent f670a8b commit 1014bf2

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

Sources/FluidPictureInPicture/FluidPictureInPictureController.swift

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import GeometryKit
88
*/
99
open class FluidPictureInPictureController: FluidWrapperViewController {
1010

11+
private let configuration: Configuration
12+
13+
public init(
14+
configuration: Configuration = .init(),
15+
content: Content? = nil
16+
) {
17+
self.configuration = configuration
18+
super.init(content: content)
19+
}
20+
1121
public final var state: State {
1222
customView.state
1323
}
@@ -17,7 +27,7 @@ open class FluidPictureInPictureController: FluidWrapperViewController {
1727
}
1828

1929
public final override func loadView() {
20-
view = View()
30+
view = View(configuration: configuration)
2131
}
2232

2333
open override func viewDidLoad() {
@@ -58,8 +68,22 @@ extension FluidPictureInPictureController {
5868
case hiding
5969
}
6070

71+
/// Layout values used while the content is floating above its background.
6172
public struct Configuration {
6273

74+
/// The fixed size used for the floating content container.
75+
public var sizeForFloating: CGSize
76+
77+
/// The spacing between the floating content and the visible/safe screen edges.
78+
public var floatingContentInset: CGFloat
79+
80+
public init(
81+
sizeForFloating: CGSize = .init(width: 100, height: 140),
82+
floatingContentInset: CGFloat = 12
83+
) {
84+
self.sizeForFloating = sizeForFloating
85+
self.floatingContentInset = floatingContentInset
86+
}
6387
}
6488

6589
public final class ContainerView: UIView {
@@ -103,7 +127,7 @@ extension FluidPictureInPictureController {
103127

104128
let containerView: ContainerView = .init()
105129

106-
let sizeForFloating = CGSize(width: 100, height: 140)
130+
let configuration: Configuration
107131
let safeAreaFinder: SafeAreaFinder
108132

109133
private(set) var state: State = .init() {
@@ -121,10 +145,12 @@ extension FluidPictureInPictureController {
121145
}
122146
}
123147

124-
override init(
125-
frame: CGRect
148+
init(
149+
configuration: Configuration,
150+
frame: CGRect = .zero
126151
) {
127152

153+
self.configuration = configuration
128154
self.safeAreaFinder = .init(windowScene: nil)
129155

130156
super.init(frame: frame)
@@ -262,13 +288,16 @@ extension FluidPictureInPictureController {
262288
for state: State
263289
) -> CGRect {
264290

265-
let containerSize = sizeForFloating
291+
let containerSize = configuration.sizeForFloating
266292
let baseFrame = bounds
267293

268294
let insetFrame =
269295
baseFrame
270296
.inset(by: state.inset)
271-
.insetBy(dx: 12, dy: 12)
297+
.insetBy(
298+
dx: configuration.floatingContentInset,
299+
dy: configuration.floatingContentInset
300+
)
272301

273302
var origin = CGPoint(x: 0, y: 0)
274303

@@ -294,8 +323,9 @@ extension FluidPictureInPictureController {
294323

295324
func setIsHidden(_ isHidden: Bool, animated: Bool) {
296325

297-
let animator = UIViewPropertyAnimator(duration: 0.6, dampingRatio: 0.8) { [self] in
326+
containerView.isUserInteractionEnabled = !isHidden
298327

328+
let updates = { [self] in
299329
if isHidden {
300330
containerView.alpha = 0
301331
containerView.transform = .init(scaleX: 0.8, y: 0.8)
@@ -305,7 +335,12 @@ extension FluidPictureInPictureController {
305335
}
306336
}
307337

308-
animator.startAnimation()
338+
if animated {
339+
let animator = UIViewPropertyAnimator(duration: 0.6, dampingRatio: 0.8, animations: updates)
340+
animator.startAnimation()
341+
} else {
342+
updates()
343+
}
309344

310345
}
311346

0 commit comments

Comments
 (0)