-
-
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 all 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,123 @@ | ||
import AppKit | ||
|
||
public class BaseWidget: NSView { | ||
private var leftConstraint: NSLayoutConstraint? | ||
private var topConstraint: NSLayoutConstraint? | ||
private var widthConstraint: NSLayoutConstraint? | ||
private var heightConstraint: NSLayoutConstraint? | ||
|
||
var x = 0 { | ||
didSet { | ||
if x != oldValue { | ||
updateLeftConstraint() | ||
} | ||
} | ||
} | ||
|
||
var y = 0 { | ||
didSet { | ||
if y != oldValue { | ||
updateTopConstraint() | ||
} | ||
} | ||
} | ||
|
||
var width = 0 { | ||
didSet { | ||
if width != oldValue { | ||
updateWidthConstraint() | ||
} | ||
} | ||
} | ||
|
||
var height = 0 { | ||
didSet { | ||
if height != oldValue { | ||
updateHeightConstraint() | ||
} | ||
} | ||
} | ||
|
||
init() { | ||
super.init(frame: .zero) | ||
|
||
self.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
|
||
@available(*, unavailable) | ||
public required init?(coder: NSCoder) { | ||
fatalError("init(coder:) is not used for this view") | ||
} | ||
|
||
private func updateLeftConstraint() { | ||
leftConstraint?.isActive = false | ||
guard let superview else { return } | ||
if #available(macOS 11.0, *) { | ||
leftConstraint = self.leftAnchor.constraint( | ||
equalTo: superview.safeAreaLayoutGuide.leftAnchor, constant: CGFloat(x)) | ||
} else { | ||
leftConstraint = self.leftAnchor.constraint( | ||
equalTo: superview.leftAnchor, constant: CGFloat(x)) | ||
} | ||
leftConstraint!.isActive = true | ||
} | ||
|
||
private func updateTopConstraint() { | ||
topConstraint?.isActive = false | ||
guard let superview else { return } | ||
if #available(macOS 11.0, *) { | ||
topConstraint = self.topAnchor.constraint( | ||
equalTo: superview.safeAreaLayoutGuide.topAnchor, constant: CGFloat(x)) | ||
} else { | ||
topConstraint = self.topAnchor.constraint( | ||
equalTo: superview.topAnchor, constant: CGFloat(x)) | ||
} | ||
topConstraint!.isActive = true | ||
} | ||
|
||
private func updateWidthConstraint() { | ||
widthConstraint?.isActive = false | ||
widthConstraint = self.widthAnchor.constraint(equalToConstant: CGFloat(width)) | ||
widthConstraint!.isActive = true | ||
} | ||
|
||
private func updateHeightConstraint() { | ||
heightConstraint?.isActive = false | ||
heightConstraint = self.heightAnchor.constraint(equalToConstant: CGFloat(height)) | ||
heightConstraint!.isActive = true | ||
} | ||
|
||
public override func viewDidMoveToSuperview() { | ||
super.viewDidMoveToSuperview() | ||
|
||
updateLeftConstraint() | ||
updateTopConstraint() | ||
} | ||
} | ||
|
||
class WrapperWidget<View: NSView>: BaseWidget { | ||
init(child: View) { | ||
super.init() | ||
|
||
self.addSubview(child) | ||
child.translatesAutoresizingMaskIntoConstraints = false | ||
NSLayoutConstraint.activate([ | ||
child.topAnchor.constraint(equalTo: self.topAnchor), | ||
child.leadingAnchor.constraint(equalTo: self.leadingAnchor), | ||
child.bottomAnchor.constraint(equalTo: self.bottomAnchor), | ||
child.trailingAnchor.constraint(equalTo: self.trailingAnchor), | ||
]) | ||
} | ||
|
||
override convenience init() { | ||
self.init(child: View(frame: .zero)) | ||
} | ||
|
||
var child: View { | ||
subviews[0] as! View | ||
} | ||
|
||
override var intrinsicContentSize: CGSize { | ||
child.intrinsicContentSize | ||
} | ||
} |
Oops, something went wrong.
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.