Skip to content
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

Added support for AppKit #14

Open
wants to merge 9 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: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// swift-tools-version:5.2
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SlidingRuler",
platforms: [.iOS(.v13)],
platforms: [.iOS(.v15),
.macOS(.v14),
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
Expand Down
5 changes: 4 additions & 1 deletion Sources/SlidingRuler/HorizontalPanGesture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import SwiftUI
import CoreGeometry

#if canImport(UIKit)
struct HorizontalDragGestureValue {
let state: UIGestureRecognizer.State
let translation: CGSize
Expand All @@ -38,7 +39,7 @@ struct HorizontalDragGestureValue {
let location: CGPoint
}

protocol HorizontalPanGestureReceiverViewDelegate: class {
protocol HorizontalPanGestureReceiverViewDelegate: AnyObject {
func viewTouchedWithoutPan(_ view: UIView)
}

Expand Down Expand Up @@ -140,3 +141,5 @@ extension HorizontalPanGesture.Coordinator: UIPointerInteractionDelegate {
.init(shape: .path(Pointers.standard), constrainedAxes: .vertical)
}
}

#endif
57 changes: 49 additions & 8 deletions Sources/SlidingRuler/Mechanic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,83 @@
// SOFTWARE.
//

import UIKit.UIScrollView
#if canImport(UIKit)
import UIKit
typealias ScrollView = UIScrollView
#elseif canImport(AppKit)
import AppKit
typealias ScrollView = NSScrollView
#endif
import CoreGraphics

enum Mechanic {

enum Mechanic {

#if canImport(UIKit)
enum Inertia {
private static let epsilon: CGFloat = 0.6

/// Velocity at time `t` of the initial velocity `v0` decelerated by the given deceleration rate.
static func velocity(atTime t: TimeInterval, v0: CGFloat, decelerationRate rate: UIScrollView.DecelerationRate) -> CGFloat {
static func velocity(atTime t: TimeInterval, v0: CGFloat, decelerationRate rate: ScrollView.DecelerationRate) -> CGFloat {
v0 * pow(rate.rawValue, (1000 * CGFloat(t)))
}

/// Travelled distance at time `t` for the initial velocity `v0` decelerated by the given deceleration rate.
static func distance(atTime t: TimeInterval, v0: CGFloat, decelerationRate rate: UIScrollView.DecelerationRate) -> CGFloat {
static func distance(atTime t: TimeInterval, v0: CGFloat, decelerationRate rate: ScrollView.DecelerationRate) -> CGFloat {
v0 * (pow(rate.rawValue, 1000 * CGFloat(t)) - 1) / (coef(rate))
}

/// Total distance travelled for he initial velocity `v0` decelerated by the given deceleration rate before being completely still.
static func totalDistance(forVelocity v0: CGFloat, decelerationRate rate: UIScrollView.DecelerationRate) -> CGFloat {
static func totalDistance(forVelocity v0: CGFloat, decelerationRate rate: ScrollView.DecelerationRate) -> CGFloat {
distance(atTime: duration(forVelocity: v0, decelerationRate: rate), v0: v0, decelerationRate: rate)
}

/// Total time ellapsed before the motion become completely still for the initial velocity `v0` decelerated by the given deceleration rate.
static func duration(forVelocity v0: CGFloat, decelerationRate rate: UIScrollView.DecelerationRate) -> TimeInterval {
static func duration(forVelocity v0: CGFloat, decelerationRate rate: ScrollView.DecelerationRate) -> TimeInterval {
TimeInterval((log((-1000 * epsilon * log(rate.rawValue)) / abs(v0))) / coef(rate))
}

static func time(toReachDistance x: CGFloat, forVelocity v0: CGFloat, decelerationRate rate: UIScrollView.DecelerationRate) -> TimeInterval {
static func time(toReachDistance x: CGFloat, forVelocity v0: CGFloat, decelerationRate rate: ScrollView.DecelerationRate) -> TimeInterval {
TimeInterval(log(1 + coef(rate) * x / v0) / coef(rate))
}

static func coef(_ rate: UIScrollView.DecelerationRate) -> CGFloat {
static func coef(_ rate: ScrollView.DecelerationRate) -> CGFloat {
1000 * log(rate.rawValue)
}
}
#elseif canImport(AppKit)
enum Inertia {
private static let epsilon: CGFloat = 0.6

/// Velocity at time `t` of the initial velocity `v0` decelerated by the given deceleration rate.
static func velocity(atTime t: TimeInterval, v0: CGFloat, decelerationRate rate: CGFloat) -> CGFloat {
v0 * pow(rate, (1000 * CGFloat(t)))
}

/// Travelled distance at time `t` for the initial velocity `v0` decelerated by the given deceleration rate.
static func distance(atTime t: TimeInterval, v0: CGFloat, decelerationRate rate: CGFloat) -> CGFloat {
v0 * (pow(rate, 1000 * CGFloat(t)) - 1) / (coef(rate))
}

/// Total distance travelled for he initial velocity `v0` decelerated by the given deceleration rate before being completely still.
static func totalDistance(forVelocity v0: CGFloat, decelerationRate rate: CGFloat) -> CGFloat {
distance(atTime: duration(forVelocity: v0, decelerationRate: rate), v0: v0, decelerationRate: rate)
}

/// Total time ellapsed before the motion become completely still for the initial velocity `v0` decelerated by the given deceleration rate.
static func duration(forVelocity v0: CGFloat, decelerationRate rate: CGFloat) -> TimeInterval {
TimeInterval((log((-1000 * epsilon * log(rate)) / abs(v0))) / coef(rate))
}

static func time(toReachDistance x: CGFloat, forVelocity v0: CGFloat, decelerationRate rate: CGFloat) -> TimeInterval {
TimeInterval(log(1 + coef(rate) * x / v0) / coef(rate))
}

static func coef(_ rate: CGFloat) -> CGFloat {
1000 * log(rate)
}
}
#endif

enum Spring {
private static var e: CGFloat { CGFloat(M_E) }
Expand Down
73 changes: 70 additions & 3 deletions Sources/SlidingRuler/Pointers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,74 @@
//


import UIKit.UIBezierPath
#if canImport(UIKit)
import UIKit
typealias BezierPath = UIBezierPath
#elseif canImport(AppKit)
import AppKit
typealias BezierPath = NSBezierPath
#endif

#if os(OSX)
import AppKit

public extension NSBezierPath {

var cgPath: CGPath {
get {
let path = CGMutablePath()
let points = NSPointArray.allocate(capacity: 3)

for i in 0 ..< self.elementCount {
let type = self.element(at: i, associatedPoints: points)
switch type {
case .moveTo:
path.move(to: points[0])
case .lineTo:
path.addLine(to: points[0])
case .curveTo:
path.addCurve(to: points[2], control1: points[0], control2: points[1])
case .closePath:
path.closeSubpath()
case .cubicCurveTo:
fatalError("Encountered an unknown element type in NSBezierPath")
case .quadraticCurveTo:
// TODO: Complete
fatalError("Encountered an unknown element type in NSBezierPath")
@unknown default:
fatalError("Encountered an unknown element type in NSBezierPath")
}
}
return path
}
}

func addLine(to point: NSPoint) {
self.line(to: point)
}

func addCurve(to point: NSPoint, controlPoint1: NSPoint, controlPoint2: NSPoint) {
self.curve(to: point, controlPoint1: controlPoint1, controlPoint2: controlPoint2)
}

func addQuadCurve(to point: NSPoint, controlPoint: NSPoint) {
self.curve(to: point,
controlPoint1: NSPoint(
x: (controlPoint.x - self.currentPoint.x) * (2.0 / 3.0) + self.currentPoint.x,
y: (controlPoint.y - self.currentPoint.y) * (2.0 / 3.0) + self.currentPoint.y),
controlPoint2: NSPoint(
x: (controlPoint.x - point.x) * (2.0 / 3.0) + point.x,
y: (controlPoint.y - point.y) * (2.0 / 3.0) + point.y))
}



}
#endif

enum Pointers {
static var standard: UIBezierPath {
let path = UIBezierPath()
static var standard: BezierPath {
let path = BezierPath()

path.move(to: CGPoint(x: 18.78348, y: 1.134168))
path.addCurve(to: CGPoint(x: 19, y: 2.051366), controlPoint1: CGPoint(x: 18.925869, y: 1.418949), controlPoint2: CGPoint(x: 19, y: 1.732971))
Expand Down Expand Up @@ -65,7 +128,11 @@ enum Pointers {
path.addCurve(to: CGPoint(x: 24.5, y: 6), controlPoint1: CGPoint(x: 21, y: 7.567003), controlPoint2: CGPoint(x: 22.567003, y: 6))
path.close()

#if canImport(UIKit)
path.apply(.init(translationX: -24.5, y: 0))
#elseif canImport(AppKit)
path.transform(using: AffineTransform(translationByX: -24.5, byY: 0))
#endif

return path
}
Expand Down
17 changes: 9 additions & 8 deletions Sources/SlidingRuler/Ruler/Ruler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
// SOFTWARE.
//


import SwiftUI

struct Ruler: View, Equatable {
@Environment(\.slidingRulerStyle) private var style

let cells: [RulerCell]
let step: CGFloat
let markOffset: CGFloat
let bounds: ClosedRange<CGFloat>
let formatter: NumberFormatter?

var body: some View {
HStack(spacing: 0) {
ForEach(self.cells) { cell in
Expand All @@ -46,21 +45,23 @@ struct Ruler: View, Equatable {
}
.animation(nil)
}

private func configuration(forCell cell: RulerCell) -> SlidingRulerStyleConfiguation {
return .init(mark: (cell.mark + markOffset) * step, bounds: bounds, step: step, formatter: formatter)
}

static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.step == rhs.step &&
lhs.cells.count == rhs.cells.count &&
(!StaticSlidingRulerStyleEnvironment.hasMarks || lhs.markOffset == rhs.markOffset)
lhs.cells.count == rhs.cells.count &&
(lhs.markOffset == rhs.markOffset)
// causing "Accessing Environment's value outside of being installed on a View" errors when running so commenting out
// (!StaticSlidingRulerStyleEnvironment.hasMarks || lhs.markOffset == rhs.markOffset)
}
}

struct Ruler_Previews: PreviewProvider {
static var previews: some View {
Ruler(cells: [.init(CGFloat(0))],
step: 1.0, markOffset: 0, bounds: -1...1, formatter: nil)
step: 1.0, markOffset: 0, bounds: -1 ... 1, formatter: nil)
}
}
Loading