-
-
Notifications
You must be signed in to change notification settings - Fork 48
Add NSViewRepresentable
for AppKit & support Swift 6 concurrency.
#105
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
067e78e
AppKit: Implement NSViewRepresentable.
furby-tm be74a9e
AppKit: specify main actors on NSViewRepresentable.
furby-tm 09bb095
UIKit: add main actors.
furby-tm c63d3ef
Make UIKit implementation agnostic of iOS/visionOS.
furby-tm 457c421
bump swift-image-formats for dependency centralization.
furby-tm 0d34485
Remove BaseWidget for AppKit backend.
furby-tm db85feb
UIKit: add support for swift 6 concurrency.
furby-tm 499a745
AppKit: Add asWidget and update methods back.
furby-tm f377e83
appkit: revert to swift 6 compatible but lopsided support.
furby-tm f4cf5ef
remove an additional, no longer needed MainActor
furby-tm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import SwiftCrossUI | ||
import AppKit | ||
|
||
public struct NSViewRepresentableContext<Coordinator> { | ||
public let coordinator: Coordinator | ||
public internal(set) var environment: EnvironmentValues | ||
} | ||
|
||
public protocol NSViewRepresentable: View where Content == Never { | ||
associatedtype NSViewType: NSView | ||
associatedtype Coordinator = Void | ||
|
||
/// Create the initial NSView instance. | ||
@MainActor | ||
func makeNSView(context: NSViewRepresentableContext<Coordinator>) -> NSViewType | ||
|
||
/// Update the view with new values. | ||
/// - Parameters: | ||
/// - nsView: The view to update. | ||
/// - context: The context, including the coordinator and potentially new environment | ||
/// values. | ||
/// - Note: This may be called even when `context` has not changed. | ||
@MainActor | ||
func updateNSView(_ nsView: NSViewType, context: NSViewRepresentableContext<Coordinator>) | ||
|
||
/// Make the coordinator for this view. | ||
/// | ||
/// The coordinator is used when the view needs to communicate changes to the rest of | ||
/// the view hierarchy (i.e. through bindings), and is often the view's delegate. | ||
@MainActor | ||
func makeCoordinator() -> Coordinator | ||
|
||
/// Compute the view's size. | ||
/// - Parameters: | ||
/// - proposal: The proposed frame for the view to render in. | ||
/// - nsVIew: The view being queried for its preferred size. | ||
/// - context: The context, including the coordinator and environment values. | ||
/// - Returns: Information about the view's size. The ``SwiftCrossUI/ViewSize/size`` | ||
/// property is what frame the view will actually be rendered with if the current layout | ||
/// pass is not a dry run, while the other properties are used to inform the layout engine | ||
/// how big or small the view can be. The ``SwiftCrossUI/ViewSize/idealSize`` property | ||
/// should not vary with the `proposal`, and should only depend on the view's contents. | ||
/// Pass `nil` for the maximum width/height if the view has no maximum size (and therefore | ||
/// may occupy the entire screen). | ||
/// | ||
/// The default implementation uses `nsView.intrinsicContentSize` and `nsView.sizeThatFits(_:)` | ||
/// to determine the return value. | ||
@MainActor | ||
func determineViewSize( | ||
for proposal: SIMD2<Int>, nsView: NSViewType, | ||
context: NSViewRepresentableContext<Coordinator> | ||
) -> ViewSize | ||
|
||
/// Called to clean up the view when it's removed. | ||
/// - Parameters: | ||
/// - nsVIew: The view being dismantled. | ||
/// - coordinator: The coordinator. | ||
/// | ||
/// This method is called after all AppKit lifecycle methods, such as | ||
/// `nsView.didMoveToSuperview()`. | ||
/// | ||
/// The default implementation does nothing. | ||
static func dismantleNSView(_ nsView: NSViewType, coordinator: Coordinator) | ||
} | ||
|
||
extension NSViewRepresentable { | ||
public static func dismantleNSView(_: NSViewType, coordinator _: Coordinator) { | ||
// no-op | ||
} | ||
|
||
public func determineViewSize( | ||
for proposal: SIMD2<Int>, nsView: NSViewType, | ||
context _: NSViewRepresentableContext<Coordinator> | ||
) -> ViewSize { | ||
let intrinsicSize = nsView.intrinsicContentSize | ||
let sizeThatFits = nsView.fittingSize | ||
|
||
let roundedSizeThatFits = SIMD2( | ||
Int(sizeThatFits.width.rounded(.up)), | ||
Int(sizeThatFits.height.rounded(.up))) | ||
let roundedIntrinsicSize = SIMD2( | ||
Int(intrinsicSize.width.rounded(.awayFromZero)), | ||
Int(intrinsicSize.height.rounded(.awayFromZero))) | ||
|
||
return ViewSize( | ||
size: SIMD2( | ||
intrinsicSize.width < 0.0 ? proposal.x : roundedSizeThatFits.x, | ||
intrinsicSize.height < 0.0 ? proposal.y : roundedSizeThatFits.y | ||
), | ||
// The 10 here is a somewhat arbitrary constant value so that it's always the same. | ||
// See also `Color` and `Picker`, which use the same constant. | ||
idealSize: SIMD2( | ||
intrinsicSize.width < 0.0 ? 10 : roundedIntrinsicSize.x, | ||
intrinsicSize.height < 0.0 ? 10 : roundedIntrinsicSize.y | ||
), | ||
minimumWidth: max(0, roundedIntrinsicSize.x), | ||
minimumHeight: max(0, roundedIntrinsicSize.x), | ||
maximumWidth: nil, | ||
maximumHeight: nil | ||
) | ||
} | ||
} | ||
|
||
extension View where Self: NSViewRepresentable { | ||
public var body: Never { | ||
preconditionFailure("This should never be called") | ||
} | ||
|
||
public func children<Backend: AppBackend>( | ||
backend _: Backend, | ||
snapshots _: [ViewGraphSnapshotter.NodeSnapshot]?, | ||
environment _: EnvironmentValues | ||
) -> any ViewGraphNodeChildren { | ||
EmptyViewChildren() | ||
} | ||
|
||
public func layoutableChildren<Backend: AppBackend>( | ||
backend _: Backend, | ||
children _: any ViewGraphNodeChildren | ||
) -> [LayoutSystem.LayoutableChild] { | ||
[] | ||
} | ||
} | ||
|
||
extension NSViewRepresentable where Coordinator == Void { | ||
public func makeCoordinator() { | ||
return () | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.