Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Update all NS* reference to use the common CoreGraphics CG* functions…
Browse files Browse the repository at this point in the history
… to help with AppKit / UIKit compatibility
  • Loading branch information
martindufort committed Dec 29, 2023
1 parent e5fb876 commit fdba61d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
5 changes: 3 additions & 2 deletions Sources/PillboxView/NSUI+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
//

import CoreGraphics
import Foundation

// ---
Expand All @@ -15,12 +16,12 @@ internal extension NSUIView {
/// Changing the window size will not recalculate the origin.
func originForCenter(inRelationTo parentView: NSUIView) -> CGPoint {
guard
parentView.frame != NSZeroRect
parentView.frame != CGRect.zero
else {
fatalError("Your parentView must have a non-zero size")
}

let midPoint = NSMidX(parentView.frame)
let midPoint = CGRectGetMidX(parentView.frame)

// Now get the half the width of our view and substract than from the midPoint
let selfMidPoint = self.frame.width / 2
Expand Down
54 changes: 30 additions & 24 deletions Sources/PillboxView/PillView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Foundation

#if canImport(AppKit)
import AppKit
#elseif canImport(UIKit)
import UIKit
#endif

// ---
Expand Down Expand Up @@ -110,11 +112,11 @@ public class PillView: NSUIView {
///
/// Use the other initializers to set fields/values of the ``PillboxView/PillView``. While you could modify some of the fields/properties with default values, some of them cannot be mutated.
public init() {
super.init(frame: NSRect(origin: NSZeroPoint, size: NSSize(width: Self.defaultWidth, height: Self.defaultHeight)))
super.init(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: Self.defaultWidth, height: Self.defaultHeight)))
self._internalInit()
}

public override init(frame frameRect: NSRect) {
public override init(frame frameRect: CGRect) {
super.init(frame: frameRect)
self._internalInit()
}
Expand Down Expand Up @@ -226,7 +228,7 @@ public class PillView: NSUIView {
}

imageView.image = state ? self.successSymbol : self.failureSymbol
imageView.tintColor(color: state ? NSUIColor.systemGreen : NSUIColor.systemRed)
imageView.tintColor = state ? NSUIColor.systemGreen : NSUIColor.systemRed
imageView.isHidden = true

#if os(macOS)
Expand All @@ -240,7 +242,7 @@ public class PillView: NSUIView {
viewAnimation.start()
#else
UIView.transition(from: self.activityIndicator, to: imageView, duration: 0.25, options: .transitionCrossDissolve) { _ in
self.activityIndicator.isSpinning = false
self.activityIndicator.stopAnimating()
self.activityIndicator.isHidden = true
imageView.isHidden = false
}
Expand Down Expand Up @@ -282,7 +284,7 @@ public class PillView: NSUIView {
self.showType = .ongoingTask

// titleLabel which should be centered within the superview
self.titleLabel = NSUILabel(frame: NSRect(x: 0,
self.titleLabel = NSUILabel(frame: CGRect(x: 0,
y: 6,
width: self.frame.width - 40,
height: 23))
Expand All @@ -299,7 +301,7 @@ public class PillView: NSUIView {
self.centerVertically(subview: self.titleLabel, horizontalAlignment: .leading)

// Setup activityIndicator
self.activityIndicator = NSUIActivityIndicatorView(frame: NSRect(x: self.titleLabel.frame.maxX,
self.activityIndicator = NSUIActivityIndicatorView(frame: CGRect(x: self.titleLabel.frame.maxX,
y: 10,
width: 16,
height: 16))
Expand All @@ -311,13 +313,13 @@ public class PillView: NSUIView {
self.centerVertically(subview: self.activityIndicator, horizontalAlignment: .trailing)

// Setup completed task imageView
let imageView = NSUIImageView(frame: NSRect(x: self.titleLabel.frame.maxX,
let imageView = NSUIImageView(frame: CGRect(x: self.titleLabel.frame.maxX,
y: 10,
width: 16,
height: 16))

imageView.isHidden = true
imageView.tintColor(color: tintColor)
imageView.tintColor = tintColor
imageView.tag = 2
self.centerVertically(subview: imageView, horizontalAlignment: .trailing)

Expand All @@ -339,11 +341,11 @@ public class PillView: NSUIView {
}
#else
NSUIView.animate(withDuration: 1) {
self.pillView.frame = UXRect(x: Int(vcView.frame.midX),
y: UIDevice.current.hasNotch ? 45: 25 + (self.isNavigationControllerPresent ? 40 : 0),
width: self.width, height: self.height)
self.frame = CGRect(x: vcView.frame.midX,
y: UIDevice.current.hasNotch ? 45: 25 + (self.isNavigationControllerPresent ? 40 : 0),
width: self.width, height: self.height)

self.pillView.center.x = vcView.center.x
self.center.x = vcView.center.x

if let completionHandler = completionHandler { completionHandler() }
}
Expand Down Expand Up @@ -390,7 +392,7 @@ public class PillView: NSUIView {
self.showType = .error

// titleLabel
self.titleLabel = NSUILabel(frame: NSRect(x: 0, y: 6, width: self.frame.width - 40, height: 23))
self.titleLabel = NSUILabel(frame: CGRect(x: 0, y: 6, width: self.frame.width - 40, height: 23))
self.titleLabel.text = message
self.titleLabel.textAlignment = .center
self.titleLabel.font = self.font
Expand All @@ -403,13 +405,13 @@ public class PillView: NSUIView {
self.titleLabel.tag = 3

// imageView
let imageView = NSUIImageView(frame: NSRect(x: titleLabel.frame.maxX,
let imageView = NSUIImageView(frame: CGRect(x: titleLabel.frame.maxX,
y: 6,
width: (self.frame.width - 15) - titleLabel.frame.maxX,
height: 23))

imageView.image = errorSymbol
imageView.tintColor(color: tintColor!)
imageView.tintColor = tintColor!
imageView.tag = 4

// moving/adding into frame
Expand All @@ -434,11 +436,11 @@ public class PillView: NSUIView {
}
#else
UIView.animate(withDuration: 1) {
self.pillView.frame = UXRect(x: 100,
y: UIDevice.current.hasNotch ? 45: 25 + (self.isNavigationControllerPresent ? 40 : 0),
width: self.width,
height: self.height)
self.pillView.center.x = vcView.center.x
self.frame = CGRect(x: 100,
y: UIDevice.current.hasNotch ? 45: 25 + (self.isNavigationControllerPresent ? 40 : 0),
width: self.width,
height: self.height)
self.center.x = vcView.center.x
}
#endif

Expand Down Expand Up @@ -481,19 +483,23 @@ public class PillView: NSUIView {
self.layer!.borderColor = NSUIColor.lightGray.cgColor
self.layer!.borderWidth = 0.2

// And add resizing mask so both the left and right side have flexible margins
// and pill is horizontally centered into containing view when resizing
self.autoresizingMask = [.minXMargin, .maxXMargin]

#else
let layer = self.layer
layer.backgroundColor = NSUIColor.PillboxBackgroundColor
layer.backgroundColor = NSUIColor.PillboxBackgroundColor.cgColor
layer.cornerRadius = 20
layer.shadowOpacity = 0.1
layer.shadowOffset = .zero
layer.shadowColor = NSUIColor.black.cgColor
layer.shadowRadius = 10
#endif

// And add resizing mask so both the left and right side have flexible margins
// and pill is horizontally centered into containing view when resizing
self.autoresizingMask = [.minXMargin, .maxXMargin]
self.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin]
#endif

// Add it to the hierarchy and retain parent
parentView.addSubview(self)
Expand All @@ -507,7 +513,7 @@ public class PillView: NSUIView {
// TODO: This function is not doign anything useful yet...
let originX = subview.frame.origin.x
let originY = subview.frame.origin.y
subview.frame = NSRect(origin: CGPoint(x: originX, y: originY), size: subview.frame.size)
subview.frame = CGRect(origin: CGPoint(x: originX, y: originY), size: subview.frame.size)
}
}

Expand Down
19 changes: 11 additions & 8 deletions Sources/PillboxView/VisualTransitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

#if canImport(AppKit)
import AppKit
#elseif canImport(UIKit)
import UIKit
#endif

import Foundation

extension PillView {
Expand Down Expand Up @@ -41,10 +44,10 @@ extension PillView {
})
#else
UIView.animate(withDuration: 1, delay: 0.25) {
self.pillView.frame = CGRect(x: self.pillView.frame.minX,
y: -300,
width: self.pillView.frame.width,
height: self.pillView.frame.height)
self.frame = CGRect(x: self.frame.minX,
y: -300,
width: self.frame.width,
height: self.frame.height)

if let completionHandler = completionHandler { completionHandler() }
}
Expand Down Expand Up @@ -73,10 +76,10 @@ extension PillView {
}
#else
UIView.animate(withDuration: 1, delay: 0.25) {
self.pillView.frame = CGRect(x: self.pillView.frame.minX,
y: UIDevice.current.hasNotch ? 45: 25 + (self.isNavigationControllerPresent ? 40 : 0),
width: self.pillView.frame.width,
height: self.pillView.frame.height)
self.frame = CGRect(x: self.frame.minX,
y: UIDevice.current.hasNotch ? 45: 25 + (self.isNavigationControllerPresent ? 40 : 0),
width: self.frame.width,
height: self.frame.height)

if let completionHandler = completionHandler { completionHandler() }
}
Expand Down

0 comments on commit fdba61d

Please sign in to comment.