Skip to content

Commit

Permalink
Modifiers to enable/disable digital Crown rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoya committed Apr 8, 2022
1 parent a545aa8 commit 6e4170e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
20 changes: 20 additions & 0 deletions Sources/SwiftUIPager/Pager+Buildable.swift
Expand Up @@ -173,6 +173,26 @@ extension Pager: Buildable {
mutating(keyPath: \.onDigitalCrownRotated, value: callback)
}

/// Disables any interaction with the digital crown
@available(iOS, unavailable)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS 7.0, *)
public func disableDigitalCrownRotation() -> Self {
mutating(keyPath: \.allowsDigitalCrownRotation, value: false)
}

/// Sets whether the `Pager` interacts with the _watchOS_ digital crown
///
/// - Parameter value: `true` if allowed, `false`, otherwise. Defaults to `true`
@available(iOS, unavailable)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS 7.0, *)
public func allowsDigitalCrownRotation(_ value: Bool = true) -> Self {
mutating(keyPath: \.allowsDigitalCrownRotation, value: value)
}

/// Sets the explicit animation to be used. Defaults to `.standard`
///
/// - Parameter value: animation to use while dragging and to page
Expand Down
11 changes: 8 additions & 3 deletions Sources/SwiftUIPager/Pager.swift
Expand Up @@ -111,6 +111,9 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
/// `true` if `Pager` can be dragged
var allowsDragging: Bool = true

/// `true` if `Pager`interacts with the digital crown
var allowsDigitalCrownRotation: Bool = true

/// `true` if pages should have a 3D rotation effect
var shouldRotate: Bool = false

Expand Down Expand Up @@ -153,8 +156,8 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
/// Callback invoked when a new page is set
var onPageChanged: ((Int) -> Void)?

/// Callback for a dragging began event
var onDraggingBegan: (() -> Void)?
/// Callback for a dragging began event
var onDraggingBegan: (() -> Void)?

/// Callback for a dragging changed event
var onDraggingChanged: ((Double) -> Void)?
Expand Down Expand Up @@ -232,7 +235,9 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:

#if os(watchOS)
if #available(watchOS 7.0, *) {
pagerContent = pagerContent.onDigitalCrownRotated(onDigitalCrownRotated)
pagerContent = pagerContent
.onDigitalCrownRotated(onDigitalCrownRotated)
.allowsDigitalCrownRotation(allowsDigitalCrownRotation)
}
#endif

Expand Down
11 changes: 11 additions & 0 deletions Sources/SwiftUIPager/PagerContent+Buildable.swift
Expand Up @@ -170,6 +170,17 @@ extension Pager.PagerContent: Buildable {
mutating(keyPath: \.onDigitalCrownRotated, value: callback)
}

/// Sets whether the `Pager` interacts with the _watchOS_ digital crown
///
/// - Parameter value: `true` if allowed, `false`, otherwise. Defaults to `true`
@available(iOS, unavailable)
@available(macOS, unavailable)
@available(tvOS, unavailable)
@available(watchOS 7.0, *)
public func allowsDigitalCrownRotation(_ value: Bool = true) -> Self {
mutating(keyPath: \.allowsDigitalCrownRotation, value: value)
}

/// Sets the explicit animation to be used. Defaults to `.standard`
///
/// - Parameter value: animation to use while dragging and to page
Expand Down
5 changes: 4 additions & 1 deletion Sources/SwiftUIPager/PagerContent.swift
Expand Up @@ -92,6 +92,9 @@ extension Pager {
/// `true` if `Pager` can be dragged
var allowsDragging: Bool = true

/// `true` if `Pager`interacts with the digital crown
var allowsDigitalCrownRotation: Bool = true

/// `true` if pages should have a 3D rotation effect
var shouldRotate: Bool = false

Expand Down Expand Up @@ -245,7 +248,7 @@ extension Pager {
#endif

#if os(watchOS)
if #available(watchOS 7.0, *) {
if #available(watchOS 7.0, *), allowsDigitalCrownRotation {
resultView = resultView
.focusable()
.digitalCrownRotation(
Expand Down

0 comments on commit 6e4170e

Please sign in to comment.