Skip to content

AppKitNavigation - Navigation #213

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

Open
wants to merge 17 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
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ let package = Package(
.target(
name: "AppKitNavigation",
dependencies: [
"SwiftNavigation"
"SwiftNavigation",
"AppKitNavigationShim",
]
),
.target(
name: "AppKitNavigationShim"
),
.testTarget(
name: "UIKitNavigationTests",
dependencies: [
Expand Down
4 changes: 4 additions & 0 deletions Package@swift-6.0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ let package = Package(
name: "AppKitNavigation",
dependencies: [
"SwiftNavigation",
"AppKitNavigationShim",
]
),
.target(
name: "AppKitNavigationShim"
),
.testTarget(
name: "UIKitNavigationTests",
dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/AppKitNavigation/AppKitAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
return try result!._rethrowGet()

case let .swiftUI(animation):
var result: Swift.Result<Result, Error>?
#if swift(>=6)
var result: Swift.Result<Result, Error>?
if #available(macOS 15, *) {
NSAnimationContext.animate(animation) {
result = Swift.Result(catching: body)
Expand Down
36 changes: 36 additions & 0 deletions Sources/AppKitNavigation/Internal/AssociatedKeys.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#if canImport(AppKit) && !targetEnvironment(macCatalyst)

import AppKit

struct AssociatedKeys {
var keys: [AnyHashableMetatype: UnsafeMutableRawPointer] = [:]

mutating func key<T>(of type: T.Type) -> UnsafeMutableRawPointer {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell this is a helper that sets up an associated object. Is it doing anything else? If not, I'm inclined to be consistent with the pattern used in UIKitNavigation, where we define dedicated local keys where needed instead of leveraging a dynamic helper. How's that sound?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again because there is more than one type of object that can navigate, this is mainly used for Observer generic types like Sheet, the classes that can execute it are NSWindow, NSSavePanel, NSAlert, NSPrintPanel.... They are not necessarily of the same type, and each class has a different way of executing a sheet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because associated keys are local to each object, does the type need to be encoded into the operation? Wouldn't it work the same to use the same static key instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private func sheetObserver<Content: SheetContent>() -> SheetObserver<Self, Content> {
        if let observer = objc_getAssociatedObject(self, sheetObserverKeys.key(of: Content.self)) as? SheetObserver<Self, Content> {
            return observer
        } else {
            let observer = SheetObserver<Self, Content>(owner: self)
            objc_setAssociatedObject(self, sheetObserverKeys.key(of: Content.self), observer, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            return observer
        }
    }

Because of the one-to-many relationship, a class that executes a sheet may have more than one observer (this class is mainly used to store the object that has been executed sheet), the object that is executed by the sheet is indeterminate, it may be NSAlert or NSWindow. Currently, storing the object that is executed by the sheet is mainly to perform cleanup work, if you have a If you have a better idea, let me know! 😄

let key = AnyHashableMetatype(type)
if let associatedKey = keys[key] {
return associatedKey
} else {
let associatedKey = malloc(1)!
keys[key] = associatedKey
return associatedKey
}
}
}

struct AnyHashableMetatype: Hashable {
static func == (lhs: AnyHashableMetatype, rhs: AnyHashableMetatype) -> Bool {
return lhs.base == rhs.base
}

let base: Any.Type

init(_ base: Any.Type) {
self.base = base
}

func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(base))
}
}

#endif
Loading
Loading