Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
@@ -0,0 +1,190 @@
//
// RangeAnimatedContainer.swift
// GraphTest
//
// Created by Andrei Salavei on 3/12/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
protocol Animatable {
static func valueBetween(start: Self, end: Self, offset: Double) -> Self
}
enum TimeFunction {
case linear
case easeOut
case easeIn
case easeInOut
func profress(time: TimeInterval, duration: TimeInterval) -> TimeInterval {
switch self {
case .linear:
return time / duration
case .easeIn:
return (pow(2, 10 * (time / duration - 1)) - 0.0009765625) * 1.0009775171065499
case .easeOut:
return (-pow(2, -10 * time / duration)) + 1 * 1.0009775171065499
case .easeInOut:
let x = time / duration
if x < 1 / 2 {
return 4 * x * x * x
} else {
let f = 2 * x - 2
return 1 / 2 * f * f * f + 1
}
}
}
}
class AnimationController<AnimatableObject: Animatable> {
private(set) var isAnimating: Bool = false
private(set) var animationDuration: TimeInterval = 0.0
private(set) var currentTime: TimeInterval = 0.0
private(set) var start: AnimatableObject
private(set) var end: AnimatableObject
private(set) var current: AnimatableObject
var timeFunction: TimeFunction = .easeInOut
var refreshClosure: (() -> Void)?
// var updateClosure: ((AnimatableObject) -> Void)?
var completionClosure: (() -> Void)?
init(current: AnimatableObject, refreshClosure: (() -> Void)?) {
self.current = current
self.start = current
self.end = current
self.refreshClosure = refreshClosure
}
func animate(to: AnimatableObject, duration: TimeInterval, timeFunction: TimeFunction = .easeInOut) {
self.timeFunction = timeFunction
currentTime = 0
animationDuration = duration
if animationDuration > 0 {
start = current
end = to
isAnimating = true
DisplayLinkService.shared.add(listner: self)
} else {
start = to
end = to
current = to
isAnimating = false
DisplayLinkService.shared.remove(listner: self)
}
refreshClosure?()
}
func set(current: AnimatableObject) {
self.start = current
self.end = current
self.current = current
animationDuration = 0.0
currentTime = 0.0
// updateClosure?(current)
refreshClosure?()
if isAnimating {
isAnimating = false
DisplayLinkService.shared.remove(listner: self)
}
}
}
extension AnimationController: DisplayLinkListner {
func update(delta: TimeInterval) {
guard isAnimating else {
DisplayLinkService.shared.remove(listner: self)
return
}
currentTime += delta
if currentTime > animationDuration || animationDuration <= 0 {
start = end
current = end
isAnimating = false
animationDuration = 0.0
currentTime = 0.0
// updateClosure?(end)
completionClosure?()
refreshClosure?()
DisplayLinkService.shared.remove(listner: self)
} else {
let offset = timeFunction.profress(time: currentTime, duration: animationDuration)
current = AnimatableObject.valueBetween(start: start, end: end, offset: offset)
// updateClosure?(current)
refreshClosure?()
}
}
}
extension ClosedRange: Animatable where Bound: BinaryFloatingPoint {
static func valueBetween(start: ClosedRange<Bound>, end: ClosedRange<Bound>, offset: Double) -> ClosedRange<Bound> {
let castedOffset = Bound(offset)
return ClosedRange(uncheckedBounds: (lower: start.lowerBound + (end.lowerBound - start.lowerBound) * castedOffset,
upper: start.upperBound + (end.upperBound - start.upperBound) * castedOffset))
}
}
extension CGFloat: Animatable {
static func valueBetween(start: CGFloat, end: CGFloat, offset: Double) -> CGFloat {
return start + (end - start) * CGFloat(offset)
}
}
extension Double: Animatable {
static func valueBetween(start: Double, end: Double, offset: Double) -> Double {
return start + (end - start) * Double(offset)
}
}
extension Int: Animatable {
static func valueBetween(start: Int, end: Int, offset: Double) -> Int {
return start + Int(Double(end - start) * offset)
}
}
extension CGPoint: Animatable {
static func valueBetween(start: CGPoint, end: CGPoint, offset: Double) -> CGPoint {
return CGPoint(x: start.x + (end.x - start.x) * CGFloat(offset),
y: start.y + (end.y - start.y) * CGFloat(offset))
}
}
extension CGRect: Animatable {
static func valueBetween(start: CGRect, end: CGRect, offset: Double) -> CGRect {
return CGRect(x: start.origin.x + (end.origin.x - start.origin.x) * CGFloat(offset),
y: start.origin.y + (end.origin.y - start.origin.y) * CGFloat(offset),
width: start.width + (end.width - start.width) * CGFloat(offset),
height: start.height + (end.height - start.height) * CGFloat(offset))
}
}
struct NSColorContainer: Animatable {
var color: GColor
static func valueBetween(start: NSColorContainer, end: NSColorContainer, offset: Double) -> NSColorContainer {
return NSColorContainer(color: GColor.valueBetween(start: start.color, end: end.color, offset: offset))
}
}
extension GColor {
static func valueBetween(start: GColor, end: GColor, offset: Double) -> GColor {
let offsetF = CGFloat(offset)
let startCIColor = makeCIColor(color: start)
let endCIColor = makeCIColor(color: end)
return GColor(red: startCIColor.red + (endCIColor.red - startCIColor.red) * offsetF,
green: startCIColor.green + (endCIColor.green - startCIColor.green) * offsetF,
blue: startCIColor.blue + (endCIColor.blue - startCIColor.blue) * offsetF,
alpha: startCIColor.alpha + (endCIColor.alpha - startCIColor.alpha) * offsetF)
}
}
@@ -0,0 +1,23 @@
//
// Array+Utils.swift
// GraphTest
//
// Created by Andrei Salavei on 4/7/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
extension Array {
func safeElement(at index: Int) -> Element? {
if index >= 0 && index < count {
return self[index]
}
return nil
}
}
@@ -0,0 +1,21 @@
//
// CGFloat.swift
// GraphTest
//
// Created by Andrei Salavei on 4/11/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
extension CGFloat {
func roundedUpToPixelGrid() -> CGFloat {
return (self * deviceScale).rounded(.up) / deviceScale
}
}
@@ -0,0 +1,224 @@
//
// CGPoint+Extensions.swift
// GraphTest
//
// Created by Andrei Salavei on 4/11/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
extension CGPoint {
public init(vector: CGVector) {
self.init(x: vector.dx, y: vector.dy)
}
public init(angle: CGFloat) {
self.init(x: cos(angle), y: sin(angle))
}
public mutating func offset(dx: CGFloat, dy: CGFloat) -> CGPoint {
x += dx
y += dy
return self
}
public func length() -> CGFloat {
return sqrt(x*x + y*y)
}
public func lengthSquared() -> CGFloat {
return x*x + y*y
}
func normalized() -> CGPoint {
let len = length()
return len>0 ? self / len : CGPoint.zero
}
public mutating func normalize() -> CGPoint {
self = normalized()
return self
}
public func distanceTo(_ point: CGPoint) -> CGFloat {
return (self - point).length()
}
public var angle: CGFloat {
return atan2(y, x)
}
public var cgSize: CGSize {
return CGSize(width: x, height: y)
}
func rotate(origin: CGPoint, angle: CGFloat) -> CGPoint {
let point = self - origin
let s = sin(angle)
let c = cos(angle)
return CGPoint(x: c * point.x - s * point.y,
y: s * point.x + c * point.y) + origin
}
}
extension CGSize {
public var cgPoint: CGPoint {
return CGPoint(x: width, y: height)
}
public init(point: CGPoint) {
self.init(width: point.x, height: point.y)
}
}
public func + (left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x + right.x, y: left.y + right.y)
}
public func += (left: inout CGPoint, right: CGPoint) {
left = left + right
}
public func + (left: CGPoint, right: CGVector) -> CGPoint {
return CGPoint(x: left.x + right.dx, y: left.y + right.dy)
}
public func += (left: inout CGPoint, right: CGVector) {
left = left + right
}
public func - (left: CGPoint, right: CGPoint) -> CGPoint { return CGPoint(x: left.x - right.x, y: left.y - right.y) }
public func - (left: CGSize, right: CGSize) -> CGSize { return CGSize(width: left.width - right.width, height: left.height - right.height) }
public func - (left: CGSize, right: CGPoint) -> CGSize { return CGSize(width: left.width - right.x, height: left.height - right.x) }
public func - (left: CGPoint, right: CGSize) -> CGPoint { return CGPoint(x: left.x - right.width, y: left.y - right.height) }
public func -= (left: inout CGPoint, right: CGPoint) {
left = left - right
}
public func - (left: CGPoint, right: CGVector) -> CGPoint {
return CGPoint(x: left.x - right.dx, y: left.y - right.dy)
}
public func -= (left: inout CGPoint, right: CGVector) {
left = left - right
}
public func *= (left: inout CGPoint, right: CGPoint) {
left = left * right
}
public func * (point: CGPoint, scalar: CGFloat) -> CGPoint { return CGPoint(x: point.x * scalar, y: point.y * scalar) }
public func * (point: CGSize, scalar: CGFloat) -> CGSize { return CGSize(width: point.width * scalar, height: point.height * scalar) }
public func *= (point: inout CGPoint, scalar: CGFloat) { point = point * scalar }
public func * (left: CGPoint, right: CGVector) -> CGPoint {
return CGPoint(x: left.x * right.dx, y: left.y * right.dy)
}
public func *= (left: inout CGPoint, right: CGVector) {
left = left * right
}
public func / (left: CGPoint, right: CGPoint) -> CGPoint { return CGPoint(x: left.x / right.x, y: left.y / right.y) }
public func / (left: CGSize, right: CGSize) -> CGSize { return CGSize(width: left.width / right.width, height: left.height / right.height) }
public func / (left: CGPoint, right: CGSize) -> CGPoint { return CGPoint(x: left.x / right.width, y: left.y / right.height) }
public func / (left: CGSize, right: CGPoint) -> CGSize { return CGSize(width: left.width / right.x, height: left.height / right.y) }
public func /= (left: inout CGPoint, right: CGPoint) { left = left / right }
public func /= (left: inout CGSize, right: CGSize) { left = left / right }
public func /= (left: inout CGSize, right: CGPoint) { left = left / right }
public func /= (left: inout CGPoint, right: CGSize) { left = left / right }
public func / (point: CGPoint, scalar: CGFloat) -> CGPoint { return CGPoint(x: point.x / scalar, y: point.y / scalar) }
public func / (point: CGSize, scalar: CGFloat) -> CGSize { return CGSize(width: point.width / scalar, height: point.height / scalar) }
public func /= (point: inout CGPoint, scalar: CGFloat) {
point = point / scalar
}
public func / (left: CGPoint, right: CGVector) -> CGPoint {
return CGPoint(x: left.x / right.dx, y: left.y / right.dy)
}
public func / (left: CGSize, right: CGVector) -> CGSize {
return CGSize(width: left.width / right.dx, height: left.height / right.dy)
}
public func /= (left: inout CGPoint, right: CGVector) {
left = left / right
}
public func * (left: CGPoint, right: CGPoint) -> CGPoint { return CGPoint(x: left.x * right.x, y: left.y * right.y) }
public func * (left: CGPoint, right: CGSize) -> CGPoint { return CGPoint(x: left.x * right.width, y: left.y * right.height) }
public func *= (left: inout CGPoint, right: CGSize) { left = left * right }
public func * (left: CGSize, right: CGSize) -> CGSize { return CGSize(width: left.width * right.width, height: left.height * right.height) }
public func *= (left: inout CGSize, right: CGSize) { left = left * right }
public func * (left: CGSize, right: CGPoint) -> CGSize { return CGSize(width: left.width * right.x, height: left.height * right.y) }
public func *= (left: inout CGSize, right: CGPoint) { left = left * right }
public func lerp(start: CGPoint, end: CGPoint, t: CGFloat) -> CGPoint {
return start + (end - start) * t
}
public func abs(_ point: CGPoint) -> CGPoint {
return CGPoint(x: abs(point.x), y: abs(point.y))
}
extension CGSize {
var isValid: Bool {
return width > 0 && height > 0 && width != .infinity && height != .infinity && width != .nan && height != .nan
}
var ratio: CGFloat {
return width / height
}
}
extension CGRect {
static var identity: CGRect {
return CGRect(x: 0, y: 0, width: 1, height: 1)
}
var center: CGPoint {
return origin + size.cgPoint / 2
}
var rounded: CGRect {
return CGRect(x: origin.x.rounded(),
y: origin.y.rounded(),
width: width.rounded(.up),
height: height.rounded(.up))
}
var mirroredVertically: CGRect {
return CGRect(x: origin.x,
y: 1.0 - (origin.y + height),
width: width,
height: height)
}
}
extension CGAffineTransform {
func inverted(with size: CGSize) -> CGAffineTransform {
var transform = self
let transformedSize = CGRect(origin: .zero, size: size).applying(transform).size
transform.tx /= transformedSize.width;
transform.ty /= transformedSize.height;
transform = transform.inverted()
transform.tx *= transformedSize.width;
transform.ty *= transformedSize.height;
return transform
}
}
@@ -0,0 +1,20 @@
//
// ClosedRange+Utils.swift
// GraphTest
//
// Created by Andrei Salavei on 3/11/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
extension ClosedRange where Bound: Numeric {
var distance: Bound {
return upperBound - lowerBound
}
}
@@ -0,0 +1,81 @@
//
// DisplayLinkService.swift
// GraphTest
//
// Created by Andrei Salavei on 4/7/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
import CoreGraphics
public protocol DisplayLinkListner: AnyObject {
func update(delta: TimeInterval)
}
// DispatchSource mares refreshes more accurate
class DisplayLinkService {
let listners = NSHashTable<AnyObject>.weakObjects()
static let shared = DisplayLinkService()
public func add(listner: DisplayLinkListner) {
listners.add(listner)
startDisplayLink()
}
public func remove(listner: DisplayLinkListner) {
listners.remove(listner)
if listners.count == 0 {
stopDisplayLink()
}
}
private init() {
dispatchSourceTimer.schedule(deadline: .now() + 1.0 / 60, repeating: 1.0 / 60)
dispatchSourceTimer.setEventHandler {
DispatchQueue.main.sync {
self.fire()
}
}
}
private var dispatchSourceTimer = DispatchSource.makeTimerSource(flags: [], queue: .global(qos: .userInteractive))
private var dispatchSourceTimerStarted: Bool = false
private var previousTickTime = 0.0
private func startDisplayLink() {
guard !dispatchSourceTimerStarted else { return }
dispatchSourceTimerStarted = true
previousTickTime = CACurrentMediaTime()
dispatchSourceTimer.resume()
}
private func stopDisplayLink() {
guard dispatchSourceTimerStarted else { return }
dispatchSourceTimerStarted = false
dispatchSourceTimer.suspend()
}
public func fire() {
let currentTime = CACurrentMediaTime()
let delta = currentTime - previousTickTime
previousTickTime = currentTime
let allListners = listners.allObjects
var hasListners = false
for listner in allListners {
(listner as! DisplayLinkListner).update(delta: delta)
hasListners = true
}
if !hasListners {
stopDisplayLink()
}
}
}
@@ -0,0 +1,12 @@
//
// GlobalHelpers.swift
// TrackingRecorder
//
// Created by Andrew Solovey on 07.09.2018.
// Copyright © 2018 Andrew Solovey. All rights reserved.
//
public func crop<Type>(_ lower: Type, _ val: Type, _ upper: Type) -> Type where Type : Comparable {
assert(lower < upper, "Invalid lover and upper values")
return max(lower, min(upper, val))
}
@@ -0,0 +1,24 @@
//
// NumberFormatter+Utils.swift
// GraphTest
//
// Created by Andrei Salavei on 4/12/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
extension NumberFormatter {
func string(from value: CGFloat) -> String {
return string(from: Double(value))
}
func string(from value: Double) -> String {
return string(from: NSNumber(value: Double(value))) ?? ""
}
}
@@ -0,0 +1,64 @@
//
// ScalesNumberFormatter.swift
// GraphTest
//
// Created by Andrei Salavei on 4/13/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
private let milionsScale = "M"
private let thousandsScale = "K"
class ScalesNumberFormatter: NumberFormatter, @unchecked Sendable {
override func string(from number: NSNumber) -> String? {
let value = number.doubleValue
let pow = log10(value)
if pow >= 6 {
guard let string = super.string(from: NSNumber(value: value / 1_000_000)) else {
return nil
}
return string + milionsScale
} else if pow >= 4 {
guard let string = super.string(from: NSNumber(value: value / 1_000)) else {
return nil
}
return string + thousandsScale
} else {
return super.string(from: number)
}
}
}
class TonNumberFormatter: NumberFormatter, @unchecked Sendable {
override func string(from number: NSNumber) -> String? {
var balanceText = "\(number.intValue)"
let decimalSeparator = self.decimalSeparator ?? "."
while balanceText.count < 10 {
balanceText.insert("0", at: balanceText.startIndex)
}
balanceText.insert(contentsOf: decimalSeparator, at: balanceText.index(balanceText.endIndex, offsetBy: -9))
while true {
if balanceText.hasSuffix("0") {
if balanceText.hasSuffix("\(decimalSeparator)0") {
balanceText.removeLast()
balanceText.removeLast()
break
} else {
balanceText.removeLast()
}
} else {
break
}
}
return balanceText
}
}
@@ -0,0 +1,194 @@
//
// TextUtils.swift
// GraphCore
//
// Created by Mikhail Filimonov on 26.02.2020.
// Copyright © 2020 Telegram. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
#if os(iOS)
typealias NSFont = UIFont
#endif
private let defaultFont:NSFont = NSFont.systemFont(ofSize: 14)
extension NSAttributedString {
var size: CGSize {
return textSize(with: self.string, font: self.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? defaultFont)
}
}
func textSize(with string: String, font: NSFont) -> CGSize {
let attributedString:NSAttributedString = NSAttributedString(string: string, attributes: [.font : font])
let layout = LabelNode.layoutText(attributedString, CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
var size:CGSize = layout.0.size
size.width = ceil(size.width)
size.height = ceil(size.height)
return size
}
private final class LabelNodeLine {
let line: CTLine
let frame: CGRect
init(line: CTLine, frame: CGRect) {
self.line = line
self.frame = frame
}
}
public final class LabelNodeLayout: NSObject {
fileprivate let attributedString: NSAttributedString?
fileprivate let truncationType: CTLineTruncationType
fileprivate let constrainedSize: CGSize
fileprivate let lines: [LabelNodeLine]
let size: CGSize
fileprivate init(attributedString: NSAttributedString?, truncationType: CTLineTruncationType, constrainedSize: CGSize, size: CGSize, lines: [LabelNodeLine]) {
self.attributedString = attributedString
self.truncationType = truncationType
self.constrainedSize = constrainedSize
self.size = size
self.lines = lines
}
var numberOfLines: Int {
return self.lines.count
}
var trailingLineWidth: CGFloat {
if let lastLine = self.lines.last {
return lastLine.frame.width
} else {
return 0.0
}
}
}
class LabelNode: NSObject {
private var currentLayout: LabelNodeLayout?
private class func getlayout(attributedString: NSAttributedString?, truncationType: CTLineTruncationType, constrainedSize: CGSize) -> LabelNodeLayout {
if let attributedString = attributedString {
let font: CTFont
if attributedString.length != 0 {
if let stringFont = attributedString.attribute(NSAttributedString.Key(kCTFontAttributeName as String), at: 0, effectiveRange: nil) {
font = stringFont as! CTFont
} else if let f = attributedString.attribute(.font, at: 0, effectiveRange: nil) as? NSFont {
font = f
} else {
font = defaultFont
}
} else {
font = defaultFont
}
let fontAscent = CTFontGetAscent(font)
let fontDescent = CTFontGetDescent(font)
let fontLineHeight = floor(fontAscent + fontDescent)
let fontLineSpacing = floor(fontLineHeight * 0.12)
var lines: [LabelNodeLine] = []
var maybeTypesetter: CTTypesetter?
maybeTypesetter = CTTypesetterCreateWithAttributedString(attributedString as CFAttributedString)
if maybeTypesetter == nil {
return LabelNodeLayout(attributedString: attributedString, truncationType: truncationType, constrainedSize: constrainedSize, size: CGSize(), lines: [])
}
let typesetter = maybeTypesetter!
var layoutSize = CGSize()
let lineOriginY = floor(layoutSize.height + fontLineHeight - fontLineSpacing * 2.0)
let lastLineCharacterIndex: CFIndex = 0
let coreTextLine: CTLine
let originalLine = CTTypesetterCreateLineWithOffset(typesetter, CFRange(location: lastLineCharacterIndex, length: attributedString.length - lastLineCharacterIndex), 0.0)
if CTLineGetTypographicBounds(originalLine, nil, nil, nil) - CTLineGetTrailingWhitespaceWidth(originalLine) < Double(constrainedSize.width) {
coreTextLine = originalLine
} else {
var truncationTokenAttributes: [NSAttributedString.Key : Any] = [:]
truncationTokenAttributes[NSAttributedString.Key(kCTFontAttributeName as String)] = font
truncationTokenAttributes[NSAttributedString.Key(kCTForegroundColorFromContextAttributeName as String)] = true as NSNumber
let tokenString = "\u{2026}"
let truncatedTokenString = NSAttributedString(string: tokenString, attributes: truncationTokenAttributes)
let truncationToken = CTLineCreateWithAttributedString(truncatedTokenString)
coreTextLine = CTLineCreateTruncatedLine(originalLine, Double(constrainedSize.width), truncationType, truncationToken) ?? truncationToken
}
let lineWidth = ceil(CGFloat(CTLineGetTypographicBounds(coreTextLine, nil, nil, nil) - CTLineGetTrailingWhitespaceWidth(coreTextLine)))
let lineFrame = CGRect(x: 0, y: lineOriginY, width: lineWidth, height: fontLineHeight)
layoutSize.height += fontLineHeight + fontLineSpacing
layoutSize.width = max(layoutSize.width, lineWidth)
lines.append(LabelNodeLine(line: coreTextLine, frame: lineFrame))
return LabelNodeLayout(attributedString: attributedString, truncationType: truncationType, constrainedSize: constrainedSize, size: CGSize(width: ceil(layoutSize.width), height: ceil(layoutSize.height)), lines: lines)
} else {
return LabelNodeLayout(attributedString: attributedString, truncationType: truncationType, constrainedSize: constrainedSize, size: CGSize(), lines: [])
}
}
func draw(_ dirtyRect: CGRect, in ctx: CGContext, backingScaleFactor: CGFloat) {
ctx.saveGState()
ctx.setAllowsFontSubpixelPositioning(true)
ctx.setShouldSubpixelPositionFonts(true)
ctx.setAllowsAntialiasing(true)
ctx.setShouldAntialias(true)
ctx.setAllowsFontSmoothing(backingScaleFactor == 1.0)
ctx.setShouldSmoothFonts(backingScaleFactor == 1.0)
let context:CGContext = ctx
if let layout = self.currentLayout {
let textMatrix = context.textMatrix
let textPosition = context.textPosition
context.textMatrix = CGAffineTransform(scaleX: 1.0, y: -1.0)
for i in 0 ..< layout.lines.count {
let line = layout.lines[i]
context.textPosition = CGPoint(x: dirtyRect.minX, y: line.frame.origin.y + dirtyRect.minY)
CTLineDraw(line.line, context)
}
context.textMatrix = textMatrix
context.textPosition = CGPoint(x: textPosition.x, y: textPosition.y)
}
ctx.restoreGState()
}
class func layoutText(_ attributedString: NSAttributedString?, _ constrainedSize: CGSize, _ truncationType: CTLineTruncationType = .end) -> (LabelNodeLayout, LabelNode) {
let layout: LabelNodeLayout
layout = LabelNode.getlayout(attributedString: attributedString, truncationType: truncationType, constrainedSize: constrainedSize)
let node = LabelNode()
node.currentLayout = layout
return (layout, node)
}
}
@@ -0,0 +1,32 @@
//
// TimeInterval+Utils.swift
// GraphTest
//
// Created by Andrei Salavei on 3/13/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
public extension TimeInterval {
static let minute: TimeInterval = 60
static let hour: TimeInterval = 60 * 60
static let day: TimeInterval = 60 * 60 * 24
static let osXDuration: TimeInterval = 0.25
static let expandAnimationDuration: TimeInterval = 0.25
static var animationDurationMultipler: Double = 1.0
static var defaultDuration: TimeInterval {
return innerDefaultDuration * animationDurationMultipler
}
private static var innerDefaultDuration: TimeInterval = osXDuration
static func setDefaultDuration(_ duration: TimeInterval) {
innerDefaultDuration = duration
}
}
@@ -0,0 +1,41 @@
//
// TimeZone.swift
// GraphTest
//
// Created by Andrei Salavei on 4/9/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
extension TimeZone {
static let utc = TimeZone(secondsFromGMT: 0)!
}
extension Locale {
static let posix = Locale(identifier: "en_US_POSIX")
}
extension Calendar {
static let utc: Calendar = {
var calendar = Calendar.current
calendar.locale = Locale.posix
calendar.timeZone = TimeZone.utc
return calendar
}()
}
extension DateFormatter {
static func utc(format: String = "") -> DateFormatter {
let formatter = DateFormatter()
formatter.calendar = Calendar.utc
formatter.dateFormat = format
formatter.timeZone = .current
return formatter
}
}
@@ -0,0 +1,111 @@
//
// GColor+Utils.swift
// GraphTest
//
// Created by Andrei Salavei on 3/11/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
func makeCIColor(color: GColor) -> CIColor {
#if os(macOS)
return CIColor(color: color)!
#else
return CIColor(color: color)
#endif
}
public extension GColor {
var redValue: CGFloat{ return makeCIColor(color: self).red }
var greenValue: CGFloat{ return makeCIColor(color: self).green }
var blueValue: CGFloat{ return makeCIColor(color: self).blue }
var alphaValue: CGFloat{ return makeCIColor(color: self).alpha }
convenience init?(hexString: String) {
let r, g, b, a: CGFloat
let components = hexString.components(separatedBy: "#")
if let name = components.first, !name.isEmpty {
switch name.lowercased() {
case "red":
self.init(hexString: "#ff3b30")
return
case "green":
self.init(hexString: "#34c759")
return
case "blue":
self.init(hexString: "#007aff")
return
case "golden":
self.init(hexString: "#ffcc00")
return
case "yellow":
self.init(hexString: "#ffcc00")
return
case "lightgreen":
self.init(hexString: "#7ED321")
return
case "lightblue":
self.init(hexString: "#5ac8fa")
return
case "seablue":
self.init(hexString: "#35afdc")
return
case "orange":
self.init(hexString: "#ff9500")
return
case "violet":
self.init(hexString: "#af52de")
return
case "emerald":
self.init(hexString: "#50e3c2")
return
case "pink":
self.init(hexString: "#ff2d55")
return
case "indigo":
self.init(hexString: "#5e5ce6")
return
default:
break
}
}
if let hexColor = components.last {
if hexColor.count == 8 {
let scanner = Scanner(string: hexColor)
var hexNumber: UInt64 = 0
if scanner.scanHexInt64(&hexNumber) {
r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
a = CGFloat(hexNumber & 0x000000ff) / 255
self.init(red: r, green: g, blue: b, alpha: a)
return
}
} else if hexColor.count == 6 {
let scanner = Scanner(string: hexColor)
var hexNumber: UInt64 = 0
if scanner.scanHexInt64(&hexNumber) {
r = CGFloat((hexNumber & 0xff0000) >> 16) / 255
g = CGFloat((hexNumber & 0x00ff00) >> 8) / 255
b = CGFloat((hexNumber & 0x0000ff) >> 0) / 255
self.init(red: r, green: g, blue: b, alpha: 1.0)
return
}
}
}
return nil
}
}
@@ -0,0 +1,91 @@
//
// GImage+Utils.swift
// GraphTest
//
// Created by Andrei Salavei on 4/8/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
#if os(iOS)
public typealias GImage = UIImage
#else
public typealias GImage = NSImage
#endif
#if os(macOS)
internal let deviceColorSpace: CGColorSpace = {
if #available(OSX 10.11.2, *) {
if let colorSpace = CGColorSpace(name: CGColorSpace.displayP3) {
return colorSpace
} else {
return CGColorSpaceCreateDeviceRGB()
}
} else {
return CGColorSpaceCreateDeviceRGB()
}
}()
#else
internal let deviceColorSpace: CGColorSpace = {
if #available(iOSApplicationExtension 9.3, iOS 9.3, *) {
if let colorSpace = CGColorSpace(name: CGColorSpace.displayP3) {
return colorSpace
} else {
return CGColorSpaceCreateDeviceRGB()
}
} else {
return CGColorSpaceCreateDeviceRGB()
}
}()
#endif
var deviceScale: CGFloat {
#if os(macOS)
return NSScreen.main?.backingScaleFactor ?? 1.0
#else
return UIScreen.main.scale
#endif
}
func generateImage(_ size: CGSize, contextGenerator: (CGSize, CGContext) -> Void, opaque: Bool = false, scale: CGFloat? = nil) -> GImage? {
let selectedScale = scale ?? deviceScale
let scaledSize = CGSize(width: size.width * selectedScale, height: size.height * selectedScale)
let bytesPerRow = (4 * Int(scaledSize.width) + 31) & (~31)
let length = bytesPerRow * Int(scaledSize.height)
let bytes = malloc(length)!.assumingMemoryBound(to: Int8.self)
guard let provider = CGDataProvider(dataInfo: bytes, data: bytes, size: length, releaseData: { bytes, _, _ in
free(bytes)
})
else {
return nil
}
let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Little.rawValue | (opaque ? CGImageAlphaInfo.noneSkipFirst.rawValue : CGImageAlphaInfo.premultipliedFirst.rawValue))
guard let context = CGContext(data: bytes, width: Int(scaledSize.width), height: Int(scaledSize.height), bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: deviceColorSpace, bitmapInfo: bitmapInfo.rawValue) else {
return nil
}
context.scaleBy(x: selectedScale, y: selectedScale)
contextGenerator(size, context)
guard let image = CGImage(width: Int(scaledSize.width), height: Int(scaledSize.height), bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: bytesPerRow, space: deviceColorSpace, bitmapInfo: bitmapInfo, provider: provider, decode: nil, shouldInterpolate: false, intent: .defaultIntent)
else {
return nil
}
#if os(macOS)
return GImage(cgImage: image, size: size)
#else
return GImage(cgImage: image, scale: selectedScale, orientation: .up)
#endif
}
@@ -0,0 +1,25 @@
//
// GView+Extensions.swift
// GraphTest
//
// Created by Andrei Salavei on 4/10/19.
// Copyright © 2019 Andrei Salavei. All rights reserved.
//
import Foundation
#if os(macOS)
import Cocoa
#else
import UIKit
#endif
#if os(macOS)
public typealias GView = NSView
#else
public typealias GView = UIView
#endif
public extension GView {
static let oneDevicePixel: CGFloat = (1.0 / max(2, min(1, deviceScale)))
}