Skip to content

Commit

Permalink
Merge pull request #13 from benjaminsage/mac-support
Browse files Browse the repository at this point in the history
Added dots to mac
  • Loading branch information
blsage authored Nov 24, 2020
2 parents 7017d78 + a9f7c76 commit a74d327
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
21 changes: 7 additions & 14 deletions Sources/iPages/PageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import AppKit
struct PageViewController: ControllerRepresentable {
var controllers: [ViewController]
@Binding var currentPage: Int
private var animated: Bool

#if os(iOS)
private var animated: Bool
var wraps: Bool
var navigationOrientation: UIPageViewController.NavigationOrientation
var bounce: Bool
var wraps: Bool
private var interPageSpacing: CGFloat = 0
#endif

Expand Down Expand Up @@ -81,11 +81,9 @@ struct PageViewController: ControllerRepresentable {
}
#else
init(controllers: [ViewController],
currentPage: Binding<Int>,
animated: Bool) {
currentPage: Binding<Int>) {
self.controllers = controllers
self._currentPage = currentPage
self.animated = animated
}

func makeNSViewController(context: Context) -> NSPageController {
Expand All @@ -105,16 +103,11 @@ struct PageViewController: ControllerRepresentable {
func updateNSViewController(_ nsPageController: NSPageController, context: Context) {
context.coordinator.parent = self

if animated {
NSAnimationContext.runAnimationGroup({ NSAnimationContext in
nsPageController.animator().selectedIndex = currentPage
}, completionHandler: {
nsPageController.completeTransition()
})
} else {
nsPageController.selectedIndex = currentPage
NSAnimationContext.runAnimationGroup({ NSAnimationContext in
nsPageController.animator().selectedIndex = currentPage
}, completionHandler: {
nsPageController.completeTransition()
}
})
}
#endif
}
19 changes: 13 additions & 6 deletions Sources/iPages/iPages+ViewModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import UIKit
import AppKit
#endif

#if os(iOS)

public extension iPages {

/// Modifies whether or not the page view should include the standard page control **dots**. (••••)
Expand All @@ -25,6 +23,7 @@ public extension iPages {
return view
}

#if os(iOS)
/// Modifies whether the page dots are hidden when there is only one page. 1️⃣⤵️
/// - Parameter hide: Whether the page dots are hidden when there is only one page
/// - Returns: A page view with the desired dots hiding with one page settings
Expand All @@ -33,6 +32,7 @@ public extension iPages {
view.pageControlHidesForSinglePage = hide
return view
}
#endif

/// Modifies **tint colors** 🟡🟢🔴🟣 to be used for the page dots.
/// - Parameters:
Expand All @@ -42,11 +42,17 @@ public extension iPages {
@available(iOS 14, *)
func dotsTintColors(currentPage: Color, otherPages: Color) -> iPages {
var view = self
#if os(iOS)
view.pageControlCurrentPageIndicatorTintColor = UIColor(currentPage)
view.pageControlPageIndicatorTintColor = UIColor(otherPages)
#else
view.pageControlCurrentPageIndicatorTintColor = currentPage
view.pageControlPageIndicatorTintColor = otherPages
#endif
return view
}

#if os(iOS)
/// Modifies **tint colors** 🟡🟢🔴🟣 to be used for the page dots.
/// - Parameters:
/// - currentPage: The tint color to be used for the current page dot ⬇️
Expand All @@ -69,7 +75,7 @@ public extension iPages {
view.pageControlBackgroundStyle = style
return view
}

/// Modifies the continuous interaction settings of the dots. 🔄
/// - Parameter allowContinuousInteraction: Whether the dots allow continuous interaction
/// - Returns: A page view with the desired continuous interaction settings of the page dots
Expand All @@ -79,7 +85,8 @@ public extension iPages {
view.pageControlAllowsContinuousInteraction = allowContinuousInteraction
return view
}

#endif

/// Modifies the **alignment of the page dots**. 👆 👇
///
/// *Trailing* and *leading* alignments will cause the page dots to rotate vertical
Expand All @@ -91,6 +98,7 @@ public extension iPages {
return view
}

#if os(iOS)
/// Modifies the navigation **orientation** of the page view. ↔️ ↕️
///
/// By default, moves the page dots to the trailing edge
Expand Down Expand Up @@ -139,6 +147,5 @@ public extension iPages {
view.pageViewAnimated = animated
return view
}
#endif
}

#endif
30 changes: 25 additions & 5 deletions Sources/iPages/iPages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ public struct iPages<Content: View>: View {
animated: pageViewAnimated)
#else
return .init(controllers: viewControllers,
currentPage: selection,
animated: pageViewAnimated)
currentPage: selection)
#endif
}

// Page control
var pageControlAlignment: Alignment = .bottom
#if os(iOS)
var showsPageControl: Bool = true
var pageControlHidesForSinglePage: Bool = false
#if os(macOS)
var pageControlCurrentPageIndicatorTintColor: Color?
var pageControlPageIndicatorTintColor: Color?
#endif

#if os(iOS)
var pageControlCurrentPageIndicatorTintColor: UIColor?
var pageControlPageIndicatorTintColor: UIColor?
private var _pageControlBackgroundStyle: Any? = nil
Expand Down Expand Up @@ -81,6 +85,14 @@ public struct iPages<Content: View>: View {
allowsContinuousInteraction: pageControlAllowsContinuousInteraction)
}
}
#else
private var ipageControl: iPageControl {
.init(numberOfPages: viewControllers.count,
currentPage: selection,
hidesForSinglePage: pageControlHidesForSinglePage,
pageIndicatorTintColor: pageControlPageIndicatorTintColor,
currentPageIndicatorTintColor: pageControlCurrentPageIndicatorTintColor)
}
#endif

/// Initializes the page 📃📖 view. 👷‍♀️
Expand All @@ -104,24 +116,32 @@ public struct iPages<Content: View>: View {
public var body: some View {
ZStack(alignment: pageControlAlignment) {
pageViewController
#if os(iOS)
if showsPageControl {
switch pageControlAlignment {
case .leading, .trailing:
VStack {
if pageControlAlignment == .leading { Spacer() }
#if os(iOS)
pageControl
#else
ipageControl
#endif
if pageControlAlignment == .trailing { Spacer() }
}
.aspectRatio(1, contentMode: .fit)
.rotationEffect(.degrees(layoutDirection ~= .leftToRight ? 90 : -90))
default:
#if os(iOS)
pageControl
.fixedSize()
.padding(.vertical)
#else
ipageControl
.fixedSize()
.padding(.vertical)
#endif
}
}
#endif
}
}
}

0 comments on commit a74d327

Please sign in to comment.