Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit ba588b4

Browse files
committed
Initial commit
0 parents  commit ba588b4

File tree

9 files changed

+326
-0
lines changed

9 files changed

+326
-0
lines changed

.gitignore

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Xcode
2+
#
3+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4+
5+
## Build generated
6+
build/
7+
DerivedData/
8+
9+
## Various settings
10+
*.pbxuser
11+
!default.pbxuser
12+
*.mode1v3
13+
!default.mode1v3
14+
*.mode2v3
15+
!default.mode2v3
16+
*.perspectivev3
17+
!default.perspectivev3
18+
xcuserdata/
19+
20+
## Other
21+
*.moved-aside
22+
*.xccheckout
23+
*.xcscmblueprint
24+
25+
## Obj-C/Swift specific
26+
*.hmap
27+
*.ipa
28+
*.dSYM.zip
29+
*.dSYM
30+
31+
## Playgrounds
32+
timeline.xctimeline
33+
playground.xcworkspace
34+
35+
# Swift Package Manager
36+
#
37+
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
38+
# Packages/
39+
# Package.pins
40+
# Package.resolved
41+
.build/
42+
43+
# CocoaPods
44+
#
45+
# We recommend against adding the Pods directory to your .gitignore. However
46+
# you should judge for yourself, the pros and cons are mentioned at:
47+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
48+
#
49+
# Pods/
50+
51+
# Carthage
52+
#
53+
# Add this line if you want to avoid checking in source code from Carthage dependencies.
54+
# Carthage/Checkouts
55+
56+
Carthage/Build
57+
58+
# fastlane
59+
#
60+
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
61+
# screenshots whenever they are needed.
62+
# For more information about the recommended setup visit:
63+
# https://docs.fastlane.tools/best-practices/source-control/#source-control
64+
65+
fastlane/report.xml
66+
fastlane/Preview.html
67+
fastlane/screenshots/**/*.png
68+
fastlane/test_output

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
BSD 2-Clause License
2+
3+
Copyright (c) 2019, Tim Donnelly
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Package.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// swift-tools-version:5.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "DisplayLink",
8+
platforms: [
9+
.iOS(.v13),
10+
.tvOS(.v13)
11+
],
12+
products: [
13+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
14+
.library(
15+
name: "DisplayLink",
16+
targets: ["DisplayLink"]),
17+
],
18+
dependencies: [
19+
// Dependencies declare other packages that this package depends on.
20+
// .package(url: /* package url */, from: "1.0.0"),
21+
],
22+
targets: [
23+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
24+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
25+
.target(
26+
name: "DisplayLink",
27+
dependencies: []),
28+
.testTarget(
29+
name: "DisplayLinkTests",
30+
dependencies: ["DisplayLink"]),
31+
]
32+
)

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# DisplayLink
2+
3+
A Combine publisher with SwiftUI integration for `CADisplayLink`.
4+
5+
SwiftUI does not currently provide any API to perform actions on a per-frame basis. This tiny
6+
library simplifies the work of bridging between `CADisplayLink` and SwiftUI:
7+
8+
```swift
9+
import DisplayLink
10+
11+
struct MyView: View {
12+
13+
@State var offset: CGFloat = 0.0
14+
15+
var body: some View {
16+
Color
17+
.red
18+
.frame(width: 40, height: 40)
19+
.offset(x: offset, y: offset)
20+
.onFrame { frame in
21+
self.offset += (frame.duration * 20.0)
22+
}
23+
}
24+
}
25+
```
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import Foundation
2+
import QuartzCore
3+
import Combine
4+
5+
6+
// A publisher that emits new values when the system is about to update the display.
7+
public final class DisplayLink: Publisher {
8+
public typealias Output = Frame
9+
public typealias Failure = Never
10+
11+
private let platformDisplayLink: PlatformDisplayLink
12+
13+
private var subscribers: [CombineIdentifier:AnySubscriber<Frame, Never>] = [:] {
14+
didSet {
15+
dispatchPrecondition(condition: .onQueue(.main))
16+
platformDisplayLink.isPaused = subscribers.isEmpty
17+
}
18+
}
19+
20+
fileprivate init(platformDisplayLink: PlatformDisplayLink) {
21+
dispatchPrecondition(condition: .onQueue(.main))
22+
self.platformDisplayLink = platformDisplayLink
23+
self.platformDisplayLink.onFrame = { [weak self] frame in
24+
self?.send(frame: frame)
25+
}
26+
}
27+
28+
public func receive<S>(subscriber: S) where S : Subscriber, S.Failure == Never, S.Input == Frame {
29+
dispatchPrecondition(condition: .onQueue(.main))
30+
31+
let typeErased = AnySubscriber(subscriber)
32+
let identifier = typeErased.combineIdentifier
33+
let subscription = Subscription(onCancel: { [weak self] in
34+
self?.cancelSubscription(for: identifier)
35+
})
36+
subscribers[identifier] = typeErased
37+
subscriber.receive(subscription: subscription)
38+
}
39+
40+
private func cancelSubscription(for identifier: CombineIdentifier) {
41+
dispatchPrecondition(condition: .onQueue(.main))
42+
subscribers.removeValue(forKey: identifier)
43+
}
44+
45+
private func send(frame: Frame) {
46+
dispatchPrecondition(condition: .onQueue(.main))
47+
let subscribers = self.subscribers.values
48+
subscribers.forEach {
49+
_ = $0.receive(frame) // Ignore demand
50+
}
51+
}
52+
53+
}
54+
55+
extension DisplayLink {
56+
57+
// Represents a frame that is about to be drawn
58+
public struct Frame {
59+
60+
// The system timestamp for the frame to be drawn
61+
public var timestamp: TimeInterval
62+
63+
// The duration between each display update
64+
public var duration: TimeInterval
65+
}
66+
67+
}
68+
69+
extension DisplayLink {
70+
71+
@available(iOS 13.0, tvOS 13.0, *)
72+
public convenience init() {
73+
self.init(platformDisplayLink: CADisplayLinkPlatformDisplayLink())
74+
75+
}
76+
77+
}
78+
79+
extension DisplayLink {
80+
public static let shared = DisplayLink()
81+
}
82+
83+
extension DisplayLink {
84+
85+
fileprivate final class Subscription: Combine.Subscription {
86+
87+
var onCancel: () -> Void
88+
89+
init(onCancel: @escaping () -> Void) {
90+
self.onCancel = onCancel
91+
}
92+
93+
func request(_ demand: Subscribers.Demand) {
94+
// Do nothing – subscribers can't impact how often the system draws frames.
95+
}
96+
97+
func cancel() {
98+
onCancel()
99+
}
100+
}
101+
102+
}
103+
104+
fileprivate protocol PlatformDisplayLink: class {
105+
var onFrame: (DisplayLink.Frame) -> Void { get set }
106+
var isPaused: Bool { get set }
107+
}
108+
109+
110+
@available(iOS 13.0, tvOS 13.0, *)
111+
final class CADisplayLinkPlatformDisplayLink: PlatformDisplayLink {
112+
113+
private var displayLink: CADisplayLink!
114+
115+
var onFrame: (DisplayLink.Frame) -> Void = { _ in }
116+
117+
var isPaused: Bool {
118+
get { displayLink.isPaused }
119+
set { displayLink.isPaused = newValue }
120+
}
121+
122+
init() {
123+
displayLink = CADisplayLink(target: self, selector: #selector(displayLinkDidFire(_:)))
124+
displayLink.add(to: RunLoop.main, forMode: .common)
125+
displayLink.isPaused = true
126+
}
127+
128+
@objc private func displayLinkDidFire(_ link: CADisplayLink) {
129+
let frame = DisplayLink.Frame(
130+
timestamp: link.timestamp,
131+
duration: link.duration)
132+
onFrame(frame)
133+
}
134+
}

Sources/DisplayLink/OnFrame.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import SwiftUI
2+
import Combine
3+
4+
extension SwiftUI.View {
5+
6+
public func onFrame(isActive: Bool = true, displayLink: DisplayLink = .shared, _ action: @escaping (DisplayLink.Frame) -> Void) -> some View {
7+
let publisher = isActive ? displayLink.eraseToAnyPublisher() : Empty<DisplayLink.Frame, Never>().eraseToAnyPublisher()
8+
return SubscriptionView(content: self, publisher: publisher, action: action)
9+
}
10+
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import XCTest
2+
@testable import DisplayLink
3+
4+
final class DisplayLinkTests: XCTestCase {
5+
func testExample() {
6+
// This is an example of a functional test case.
7+
// Use XCTAssert and related functions to verify your tests produce the correct
8+
// results.
9+
XCTAssertEqual("Hello, World!", "Hello, World!")
10+
}
11+
12+
static var allTests = [
13+
("testExample", testExample),
14+
]
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import XCTest
2+
3+
#if !canImport(ObjectiveC)
4+
public func allTests() -> [XCTestCaseEntry] {
5+
return [
6+
testCase(DisplayLinkTests.allTests),
7+
]
8+
}
9+
#endif

Tests/LinuxMain.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import XCTest
2+
3+
import DisplayLinkTests
4+
5+
var tests = [XCTestCaseEntry]()
6+
tests += DisplayLinkTests.allTests()
7+
XCTMain(tests)

0 commit comments

Comments
 (0)