Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to enable/disable keyboard control, fixes issue of unwanted focus ring in macOS 14+ #330

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Sources/SwiftUIPager/Pager+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,11 @@ extension Pager: Buildable {
return mutating(keyPath: \.sideInsets, value: length ?? 8)
}

/// Sets whether the page can be controlled by the keyboard
///
/// - Parameter value: `true` if keyboard control is allowed, `false`, otherwise. Defaults to `true`
public func allowsKeyboardControl(_ value: Bool = true) -> Self {
mutating(keyPath: \.allowsKeyboardControl, value: value)
}

}

8 changes: 6 additions & 2 deletions Sources/SwiftUIPager/Pager.swift
Original file line number Diff line number Diff line change
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` can be controlled by the keyboard commands
var allowsKeyboardControl: Bool = true

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

Expand Down Expand Up @@ -215,7 +218,8 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
.padding(sideInsets)
.pagingAnimation(pagingAnimation)
.partialPagination(pageRatio)

.allowsKeyboardControl(allowsKeyboardControl)

#if !os(tvOS)
pagerContent = pagerContent
.swipeInteractionArea(swipeInteractionArea)
Expand Down Expand Up @@ -245,7 +249,7 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
pagerContent = isHorizontal ? pagerContent.horizontal(horizontalSwipeDirection) : pagerContent.vertical(verticalSwipeDirection)

if let preferredItemSize = preferredItemSize {
pagerContent = pagerContent.preferredItemSize(preferredItemSize, alignment: itemAlignment)
pagerContent = pagerContent.preferredItemSize(preferredItemSize)
}

return pagerContent
Expand Down
7 changes: 7 additions & 0 deletions Sources/SwiftUIPager/PagerContent+Buildable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ extension Pager.PagerContent: Buildable {
mutating(keyPath: \.pageRatio, value: ratio)
}

/// Sets whether the page can be controlled by the keyboard
///
/// - Parameter value: `true` if keyboard control is allowed, `false`, otherwise. Defaults to `true`
func allowsKeyboardControl(_ allow: Bool) -> Self {
mutating(keyPath: \.allowsKeyboardControl, value: allow)
}

#if !os(tvOS)

/// User can only swipe forward so in one direction
Expand Down
13 changes: 6 additions & 7 deletions Sources/SwiftUIPager/PagerContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ extension Pager {
/// `true` if `Pager` can be dragged
var allowsDragging: Bool = true

/// `true` if `Pager` can be controlled by the keyboard commands
var allowsKeyboardControl: Bool = true

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

Expand Down Expand Up @@ -210,7 +213,7 @@ extension Pager {
#endif

#if os(macOS)
wrappedView = wrappedView
wrappedView = !allowsKeyboardControl ? wrappedView : wrappedView
.focusable()
.onMoveCommand(perform: self.onMoveCommandSent)
.eraseToAny()
Expand Down Expand Up @@ -395,14 +398,10 @@ extension Pager.PagerContent {
}
withAnimation(animation) {
self.pagerModel.draggingOffset = 0
if pageIncrement != 0 {
self.pagerModel.pageIncrement = pageIncrement
}
if page != newPage {
self.pagerModel.index = newPage
}
self.pagerModel.pageIncrement = pageIncrement
self.pagerModel.draggingVelocity = 0
self.pagerModel.lastDraggingValue = nil
self.pagerModel.index = newPage
self.pagerModel.lastDigitalCrownPageOffset = CGFloat(pagerModel.index)
self.pagerModel.objectWillChange.send()
#if os(watchOS)
Expand Down
2 changes: 1 addition & 1 deletion SwiftUIPager.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SwiftUIPager"
s.version = ENV['LIB_VERSION'] # pod trunk me --verbose
s.version = '2.5.1'
s.summary = "Native pager for SwiftUI. Easily to use, easy to customize."

s.description = <<-DESC
Expand Down