Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed May 21, 2024
1 parent 691f7a5 commit a6f6f3b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ extension PageControllerView {
public class Coordinator: NSObject, NSPageControllerDelegate {
/**
* Parent
* - Fixme: ⚠️️ add description
*/
var parent: PageControllerView
/**
* Set parent
* - Fixme: ⚠️️ add reasoning
*/
public init(_ pageControllerView: PageControllerView) {
self.parent = pageControllerView
Expand All @@ -33,7 +35,7 @@ extension PageControllerView.Coordinator {
* - Remark: This could be based on the object's position in an array or its properties
*/
public func pageController(_ pageController: NSPageController, identifierFor object: Any) -> NSPageController.ObjectIdentifier { // String
Swift.print("Coordinator.identifierFor: \(object)")
// Swift.print("Coordinator.identifierFor: \(object)")
return String(describing: object) // Return identifier
}
/**
Expand All @@ -42,13 +44,14 @@ extension PageControllerView.Coordinator {
* - Remark: This could involve looking up the identifier in a dictionary or storyboard
* - Remark: Displays the `ViewController` in the page-controller
* - Remark: We can also use NSViewcontroller and a NSHostingView
* - Fixme: ⚠️️ doc each line, use copilot
* - Parameters:
* - pageController: current page controller
* - identifier: page id
* - Returns: current page
*/
public func pageController(_ pageController: NSPageController, viewControllerForIdentifier identifier: NSPageController.ObjectIdentifier) -> NSViewController {
Swift.print("viewControllerForIdentifier \(identifier)")
// Swift.print("viewControllerForIdentifier \(identifier)")
let rootView: some View = AnyView(parent.makeView(identifier)) // ⚠️️ We can probably find a better way than using AnyView
let hostingController = NSHostingController(rootView: rootView)
hostingController.view.autoresizingMask = [.height, .width] // this is the key to make the swiftuiu view work on init and when window is resized
Expand All @@ -58,7 +61,7 @@ extension PageControllerView.Coordinator {
* Delegate callback
* - Description: Ends the transition when the user swipes left or right
* - Fixme: ⚠️️ We can add callback here to update `PageIndicator` (or use WillStartLiveTransition, if we want more instant UX etc)
* - Parameter pageController: pagecontroller that the live transition ended in
* - Parameter pageController: The pagecontroller that the live transition ended in
*/
public func pageControllerDidEndLiveTransition(_ pageController: NSPageController) {
pageController.completeTransition() // Hide the transition view used for animation and show the selectedViewController.view. Generally, this is called during pageControllerDidEndLiveTransition: in the delegate when the new contents of view is ready to be displayed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import SwiftUI
import AppKit
/**
* NSViewControllerRepresentable extension
* `NSViewControllerRepresentable` extension
*/
extension PageControllerView {
/**
* This method is part of the NSViewControllerRepresentable protocol in SwiftUI for macOS.
* It's responsible for creating an instance of Coordinator, which is a helper class that can manage and coordinate with the NSPageController.
* The Coordinator can handle tasks such as responding to user input, managing the current page index, and managing the view controllers.
* - Note: It's responsible for creating an instance of Coordinator, which is a helper class that can manage and coordinate with the NSPageController.
* - Note: The Coordinator can handle tasks such as responding to user input, managing the current page index, and managing the view controllers.
* - Returns A new instance of Coordinator.
*/
public func makeCoordinator() -> Coordinator {
Expand Down Expand Up @@ -37,14 +37,14 @@ extension PageControllerView {
}
/**
* This method is part of the NSViewControllerRepresentable protocol in SwiftUI for macOS.
* It's called whenever the SwiftUI view's state changes and the changes need to be reflected in the corresponding NSViewController.
* In this specific case, the method is currently printing the frame of the NSPageController's view to the console.
* However, it's also where you would update the NSPageController based on changes to the SwiftUI view's state.
* For example, if the SwiftUI view had a @State variable that the NSPageController needed to reflect, you would update that here.
* - Fixme: ⚠️️ we might want to keep selection value in sync with page controller here
*
* - Parameter: nsViewController The NSPageController that needs to be updated.
* - Parameter: context The context in which the NSViewControllerRepresentable is being used.
* - Description: It's called whenever the SwiftUI view's state changes and the changes need to be reflected in the corresponding NSViewController.
* In this specific case, the method is currently printing the frame of the NSPageController's view to the console.
* However, it's also where you would update the NSPageController based on changes to the SwiftUI view's state.
* For example, if the SwiftUI view had a @State variable that the NSPageController needed to reflect, you would update that here.
* - Fixme: ⚠️️ we might want to keep selection value in sync with page controller here
* - Parameters:
* - nsViewController: The NSPageController that needs to be updated.
* - context: The context in which the NSViewControllerRepresentable is being used.
*/
public func updateNSViewController(_ nsViewController: NSPageController, context: Context) {
// Update the NSPageController when the SwiftUI view's state changes
Expand Down
8 changes: 5 additions & 3 deletions Sources/PageControllerView/PageControllerView+Preview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import AppKit
* Preview
* - Important: ⚠️️ Preview doesn't work properly with swiping, so run the app to see the swipe work properly
* - Note: Because of github action we can't currently use more modern: #Preview(traits: .fixedLayout(width: 300, height: 300)) {
* - Fixme: ⚠️️ Add darkmode to the preview
* - Fixme: ⚠️️ Use modern preview syntax once github allows it
*/
struct PageControl_Previews: PreviewProvider {
struct ContentView: View {
@State var curPageIndex: Int = 0
var body: some View {
PageControllerView(dataSource: ["green", "blue", "red"], currentPage: $curPageIndex ) { identifier in // ["View 1", "View 2", "View 3"]
let dataSource = ["green", "blue", "red"]
return PageControllerView(dataSource: dataSource, currentPage: $curPageIndex ) { identifier in // ["View 1", "View 2", "View 3"]
var rootView: Color
switch identifier {
case "green":
Expand All @@ -30,7 +33,6 @@ struct PageControl_Previews: PreviewProvider {
static var previews: some View {
return ContentView()
.frame(width: 300, height: 300)

}
}
#endif
#endif
9 changes: 5 additions & 4 deletions Sources/PageControllerView/PageControllerView+Setter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import AppKit
extension PageControllerView {
/**
* Updates "page-flipper"
* - Parameter pageController: reference to pageController
* - Parameter index: current index
* - Fixme: ⚠️️ we could make animation optional
* - Fixme: ⚠️️ remove param pageController, figure out how to reach it without
* - Fixme: ⚠️️ We could make animation optional
* - Fixme: ⚠️️ Remove param pageController, figure out how to reach it without
* - Parameters:
* - pageController: reference to pageController
* - index: current index
*/
func goToPage(pageController: NSPageController, index: Int) {
NSAnimationContext.runAnimationGroup({ _ /*context*/ in // To animate a selectedIndex change:
Expand Down
6 changes: 4 additions & 2 deletions Sources/PageControllerView/PageControllerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ public struct PageControllerView: NSViewControllerRepresentable { // PageControl
public let dataSource: [NSPageController.ObjectIdentifier] // Replace with your data source
/**
* This should change page from the caller
* Use this to get callbacks when page changes, and also use it to set pages
* - Description: Use this to get callbacks when page changes, and also use it to set pages
*/
@Binding public var currentPage: Int
/**
* - Fixme: ⚠️️ we could try use some View somehow
* - Fixme: ⚠️️ move into extension?
*/
public typealias MakeView = (_ id: NSPageController.ObjectIdentifier) -> AnyView
/**
Expand All @@ -31,7 +32,8 @@ public struct PageControllerView: NSViewControllerRepresentable { // PageControl
*/
public var makeView: MakeView = { id in Swift.print("default makeView - id: \(id)"); return AnyView(EmptyView()) }
/**
* Initializes a new PageControllerView.
* Initializes a new PageControllerView
* - Fixme: ⚠️️ doc each line, use copilot
* - Parameters:
* - dataSource: An array of identifiers for the pages. Each identifier is used to create the corresponding page.
* - currentPage: A binding to a property that tracks the currently selected page. The PageControllerView updates the property as the user interacts with the interface.
Expand Down
7 changes: 5 additions & 2 deletions Sources/PageControllerView/util/EffectView+MacOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import AppKit
open class EffectView: NSVisualEffectView {
/**
* Initializes a new instance of the `EffectView` with the given material, blending mode, and frame.
* - Fixme: ⚠️️ doc each line, use copilot
* - Parameters:
* - material: The translucent material to use as a background.
* - blendingMode: The blending mode to use.
* - frame: The frame rectangle for the view.
*/
public init(material: NSVisualEffectView.Material = .headerView, blendingMode: NSVisualEffectView.BlendingMode = .withinWindow, frame frameRect: NSRect) {
super.init(frame: frameRect)
self.config(material: material, blendingMode: blendingMode)
self.config(
material: material,
blendingMode: blendingMode
)
}
/**
* Required initializer that is not implemented.
Expand All @@ -44,5 +48,4 @@ extension EffectView {
self.blendingMode = blendingMode
}
}

#endif
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ struct MacApp: App {
- [iPages](https://github.com/benjaminsage/iPages/)
- [BigUIPaging](https://github.com/notsobigcompany/BigUIPaging/)

## Todo:
- Add dedicated UITests

0 comments on commit a6f6f3b

Please sign in to comment.