diff --git a/Source/Charts/Animation/Animator.swift b/Source/Charts/Animation/Animator.swift index 406ab93fff..b94b21f496 100644 --- a/Source/Charts/Animation/Animator.swift +++ b/Source/Charts/Animation/Animator.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartAnimatorDelegate) public protocol AnimatorDelegate { diff --git a/Source/Charts/Charts/BarLineChartViewBase.swift b/Source/Charts/Charts/BarLineChartViewBase.swift index 1c7456196b..2ae8256885 100644 --- a/Source/Charts/Charts/BarLineChartViewBase.swift +++ b/Source/Charts/Charts/BarLineChartViewBase.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - /// Base-class of LineChart, BarChart, ScatterChart and CandleStickChart. open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartDataProvider, NSUIGestureRecognizerDelegate { @@ -134,10 +130,10 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD _panGestureRecognizer.isEnabled = _dragXEnabled || _dragYEnabled #if !os(tvOS) - _pinchGestureRecognizer = NSUIPinchGestureRecognizer(target: self, action: #selector(BarLineChartViewBase.pinchGestureRecognized(_:))) - _pinchGestureRecognizer.delegate = self - self.addGestureRecognizer(_pinchGestureRecognizer) - _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled + _pinchGestureRecognizer = NSUIPinchGestureRecognizer(target: self, action: #selector(BarLineChartViewBase.pinchGestureRecognized(_:))) + _pinchGestureRecognizer.delegate = self + self.addGestureRecognizer(_pinchGestureRecognizer) + _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled #endif } @@ -892,13 +888,13 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD else { #if !os(tvOS) - if gestureRecognizer == _pinchGestureRecognizer + if gestureRecognizer == _pinchGestureRecognizer + { + if _data === nil || (!_pinchZoomEnabled && !_scaleXEnabled && !_scaleYEnabled) { - if _data === nil || (!_pinchZoomEnabled && !_scaleXEnabled && !_scaleYEnabled) - { - return false - } + return false } + } #endif } @@ -927,11 +923,11 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD open func gestureRecognizer(_ gestureRecognizer: NSUIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: NSUIGestureRecognizer) -> Bool { #if !os(tvOS) - if ((gestureRecognizer is NSUIPinchGestureRecognizer && otherGestureRecognizer is NSUIPanGestureRecognizer) || - (gestureRecognizer is NSUIPanGestureRecognizer && otherGestureRecognizer is NSUIPinchGestureRecognizer)) - { - return true - } + if ((gestureRecognizer is NSUIPinchGestureRecognizer && otherGestureRecognizer is NSUIPanGestureRecognizer) || + (gestureRecognizer is NSUIPanGestureRecognizer && otherGestureRecognizer is NSUIPinchGestureRecognizer)) + { + return true + } #endif if gestureRecognizer is NSUIPanGestureRecognizer, @@ -1594,7 +1590,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD _scaleXEnabled = enabled _scaleYEnabled = enabled #if !os(tvOS) - _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled + _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled #endif } } @@ -1611,7 +1607,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD { _scaleXEnabled = newValue #if !os(tvOS) - _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled + _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled #endif } } @@ -1629,7 +1625,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD { _scaleYEnabled = newValue #if !os(tvOS) - _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled + _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled #endif } } @@ -1775,7 +1771,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD { _pinchZoomEnabled = newValue #if !os(tvOS) - _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled + _pinchGestureRecognizer.isEnabled = _pinchZoomEnabled || _scaleXEnabled || _scaleYEnabled #endif } } diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index 03350fcbcd..f6e5187841 100644 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -13,10 +13,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc public protocol ChartViewDelegate { @@ -171,7 +167,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate internal func initialize() { #if os(iOS) - self.backgroundColor = NSUIColor.clear + self.backgroundColor = NSUIColor.clear #endif _animator = Animator() diff --git a/Source/Charts/Charts/HorizontalBarChartView.swift b/Source/Charts/Charts/HorizontalBarChartView.swift index 6ea426cf2c..7b6163c8c9 100644 --- a/Source/Charts/Charts/HorizontalBarChartView.swift +++ b/Source/Charts/Charts/HorizontalBarChartView.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - /// BarChart with horizontal bar orientation. In this implementation, x- and y-axis are switched. open class HorizontalBarChartView: BarChartView { diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index 2a86afa40c..f5854662f5 100644 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - /// View that represents a pie chart. Draws cake like slices. open class PieChartView: PieRadarChartViewBase { @@ -387,20 +383,15 @@ open class PieChartView: PieRadarChartViewBase } else { - #if os(OSX) - let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle - paragraphStyle.lineBreakMode = NSLineBreakMode.byTruncatingTail - #else - let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle - paragraphStyle.lineBreakMode = NSLineBreakMode.byTruncatingTail - #endif + let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle + paragraphStyle.lineBreakMode = .byTruncatingTail paragraphStyle.alignment = .center attrString = NSMutableAttributedString(string: newValue!) attrString?.setAttributes([ - NSAttributedString.Key.foregroundColor: NSUIColor.black, - NSAttributedString.Key.font: NSUIFont.systemFont(ofSize: 12.0), - NSAttributedString.Key.paragraphStyle: paragraphStyle + .foregroundColor: NSUIColor.black, + .font: NSUIFont.systemFont(ofSize: 12.0), + .paragraphStyle: paragraphStyle ], range: NSMakeRange(0, attrString!.length)) } self.centerAttributedText = attrString diff --git a/Source/Charts/Charts/PieRadarChartViewBase.swift b/Source/Charts/Charts/PieRadarChartViewBase.swift index c402c8c785..5153609335 100755 --- a/Source/Charts/Charts/PieRadarChartViewBase.swift +++ b/Source/Charts/Charts/PieRadarChartViewBase.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - /// Base class of PieChartView and RadarChartView. open class PieRadarChartViewBase: ChartViewBase { @@ -63,9 +59,9 @@ open class PieRadarChartViewBase: ChartViewBase self.addGestureRecognizer(_tapGestureRecognizer) #if !os(tvOS) - _rotationGestureRecognizer = NSUIRotationGestureRecognizer(target: self, action: #selector(rotationGestureRecognized(_:))) - self.addGestureRecognizer(_rotationGestureRecognizer) - _rotationGestureRecognizer.isEnabled = rotationWithTwoFingers + _rotationGestureRecognizer = NSUIRotationGestureRecognizer(target: self, action: #selector(rotationGestureRecognized(_:))) + self.addGestureRecognizer(_rotationGestureRecognizer) + _rotationGestureRecognizer.isEnabled = rotationWithTwoFingers #endif } @@ -407,7 +403,7 @@ open class PieRadarChartViewBase: ChartViewBase { _rotationWithTwoFingers = newValue #if !os(tvOS) - _rotationGestureRecognizer.isEnabled = _rotationWithTwoFingers + _rotationGestureRecognizer.isEnabled = _rotationWithTwoFingers #endif } } diff --git a/Source/Charts/Components/Description.swift b/Source/Charts/Components/Description.swift index a063499e4f..84990885ad 100644 --- a/Source/Charts/Components/Description.swift +++ b/Source/Charts/Components/Description.swift @@ -12,22 +12,18 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartDescription) open class Description: ComponentBase { public override init() { #if os(tvOS) - // 23 is the smallest recommended font size on the TV - font = NSUIFont.systemFont(ofSize: 23) + // 23 is the smallest recommended font size on the TV + font = .systemFont(ofSize: 23) #elseif os(OSX) - font = NSUIFont.systemFont(ofSize: NSUIFont.systemFontSize) + font = .systemFont(ofSize: NSUIFont.systemFontSize) #else - font = NSUIFont.systemFont(ofSize: 8.0) + font = .systemFont(ofSize: 8.0) #endif super.init() diff --git a/Source/Charts/Components/Legend.swift b/Source/Charts/Components/Legend.swift index 6386fe327d..34e8cc3511 100644 --- a/Source/Charts/Components/Legend.swift +++ b/Source/Charts/Components/Legend.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartLegend) open class Legend: ComponentBase { diff --git a/Source/Charts/Components/LegendEntry.swift b/Source/Charts/Components/LegendEntry.swift index 7b8c88b424..5868137ef9 100644 --- a/Source/Charts/Components/LegendEntry.swift +++ b/Source/Charts/Components/LegendEntry.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartLegendEntry) open class LegendEntry: NSObject { diff --git a/Source/Charts/Components/MarkerImage.swift b/Source/Charts/Components/MarkerImage.swift index 14e4dcfc86..341b1186c0 100644 --- a/Source/Charts/Components/MarkerImage.swift +++ b/Source/Charts/Components/MarkerImage.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartMarkerImage) open class MarkerImage: NSObject, IMarker { diff --git a/Source/Charts/Components/MarkerView.swift b/Source/Charts/Components/MarkerView.swift index 094f4e2e9d..e4d682ce84 100644 --- a/Source/Charts/Components/MarkerView.swift +++ b/Source/Charts/Components/MarkerView.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartMarkerView) open class MarkerView: NSUIView, IMarker { @@ -75,24 +71,24 @@ open class MarkerView: NSUIView, IMarker open class func viewFromXib(in bundle: Bundle = .main) -> MarkerView? { #if !os(OSX) - return bundle.loadNibNamed( - String(describing: self), - owner: nil, - options: nil)?[0] as? MarkerView + return bundle.loadNibNamed( + String(describing: self), + owner: nil, + options: nil)?[0] as? MarkerView #else - - var loadedObjects = NSArray() - let loadedObjectsPointer = AutoreleasingUnsafeMutablePointer(&loadedObjects) - - if bundle.loadNibNamed( - NSNib.Name(String(describing: self)), - owner: nil, - topLevelObjects: loadedObjectsPointer) - { - return loadedObjects[0] as? MarkerView - } - - return nil + + var loadedObjects = NSArray() + let loadedObjectsPointer = AutoreleasingUnsafeMutablePointer(&loadedObjects) + + if bundle.loadNibNamed( + NSNib.Name(String(describing: self)), + owner: nil, + topLevelObjects: loadedObjectsPointer) + { + return loadedObjects[0] as? MarkerView + } + + return nil #endif } diff --git a/Source/Charts/Components/YAxis.swift b/Source/Charts/Components/YAxis.swift index 1d5ef671e2..01e41b51af 100644 --- a/Source/Charts/Components/YAxis.swift +++ b/Source/Charts/Components/YAxis.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - /// Class representing the y-axis labels settings and its entries. /// Be aware that not all features the YLabels class provides are suitable for the RadarChart. diff --git a/Source/Charts/Data/Interfaces/IPieChartDataSet.swift b/Source/Charts/Data/Interfaces/IPieChartDataSet.swift index b85249a292..433c08f262 100644 --- a/Source/Charts/Data/Interfaces/IPieChartDataSet.swift +++ b/Source/Charts/Data/Interfaces/IPieChartDataSet.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc public protocol IPieChartDataSet: IChartDataSet { diff --git a/Source/Charts/Formatters/DefaultFillFormatter.swift b/Source/Charts/Formatters/DefaultFillFormatter.swift index ea9c2a7465..3afadf35fa 100644 --- a/Source/Charts/Formatters/DefaultFillFormatter.swift +++ b/Source/Charts/Formatters/DefaultFillFormatter.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - /// Default formatter that calculates the position of the filled line. @objc(ChartDefaultFillFormatter) open class DefaultFillFormatter: NSObject, IFillFormatter diff --git a/Source/Charts/Jobs/AnimatedMoveViewJob.swift b/Source/Charts/Jobs/AnimatedMoveViewJob.swift index b6ea8c28e5..7a75822161 100644 --- a/Source/Charts/Jobs/AnimatedMoveViewJob.swift +++ b/Source/Charts/Jobs/AnimatedMoveViewJob.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - open class AnimatedMoveViewJob: AnimatedViewPortJob { internal override func animationUpdate() diff --git a/Source/Charts/Jobs/AnimatedViewPortJob.swift b/Source/Charts/Jobs/AnimatedViewPortJob.swift index eaa9fec170..7375456664 100644 --- a/Source/Charts/Jobs/AnimatedViewPortJob.swift +++ b/Source/Charts/Jobs/AnimatedViewPortJob.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - open class AnimatedViewPortJob: ViewPortJob { internal var phase: CGFloat = 1.0 diff --git a/Source/Charts/Jobs/MoveViewJob.swift b/Source/Charts/Jobs/MoveViewJob.swift index acc8165d39..0b6ca320e5 100644 --- a/Source/Charts/Jobs/MoveViewJob.swift +++ b/Source/Charts/Jobs/MoveViewJob.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(MoveChartViewJob) open class MoveViewJob: ViewPortJob { diff --git a/Source/Charts/Jobs/ZoomViewJob.swift b/Source/Charts/Jobs/ZoomViewJob.swift index bdc7e32020..a6a79394da 100644 --- a/Source/Charts/Jobs/ZoomViewJob.swift +++ b/Source/Charts/Jobs/ZoomViewJob.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ZoomChartViewJob) open class ZoomViewJob: ViewPortJob { diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index 56ebc396a0..d16210bdcf 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - open class BarChartRenderer: BarLineScatterCandleBubbleRenderer { /// A nested array of elements ordered logically (i.e not in visual/drawing order) for use with VoiceOver diff --git a/Source/Charts/Renderers/BubbleChartRenderer.swift b/Source/Charts/Renderers/BubbleChartRenderer.swift index 865ce46d93..1a089e9f3e 100644 --- a/Source/Charts/Renderers/BubbleChartRenderer.swift +++ b/Source/Charts/Renderers/BubbleChartRenderer.swift @@ -12,11 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - - open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer { /// A nested array of elements ordered logically (i.e not in visual/drawing order) for use with VoiceOver. diff --git a/Source/Charts/Renderers/CandleStickChartRenderer.swift b/Source/Charts/Renderers/CandleStickChartRenderer.swift index bf813adfd5..aa2cd1b67e 100644 --- a/Source/Charts/Renderers/CandleStickChartRenderer.swift +++ b/Source/Charts/Renderers/CandleStickChartRenderer.swift @@ -12,11 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - - open class CandleStickChartRenderer: LineScatterCandleRadarRenderer { @objc open weak var dataProvider: CandleChartDataProvider? diff --git a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift index 4f493bdeea..e2572ebe7c 100644 --- a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift +++ b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift @@ -12,11 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - - open class HorizontalBarChartRenderer: BarChartRenderer { private class Buffer diff --git a/Source/Charts/Renderers/LegendRenderer.swift b/Source/Charts/Renderers/LegendRenderer.swift index dc98b8b6a4..f0b01d093a 100755 --- a/Source/Charts/Renderers/LegendRenderer.swift +++ b/Source/Charts/Renderers/LegendRenderer.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartLegendRenderer) open class LegendRenderer: Renderer { diff --git a/Source/Charts/Renderers/LineChartRenderer.swift b/Source/Charts/Renderers/LineChartRenderer.swift index f1d81cfef8..ec66ccd504 100644 --- a/Source/Charts/Renderers/LineChartRenderer.swift +++ b/Source/Charts/Renderers/LineChartRenderer.swift @@ -12,11 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - - open class LineChartRenderer: LineRadarRenderer { // TODO: Currently, this nesting isn't necessary for LineCharts. However, it will make it much easier to add a custom rotor diff --git a/Source/Charts/Renderers/PieChartRenderer.swift b/Source/Charts/Renderers/PieChartRenderer.swift index e50106c351..45fc8977f7 100644 --- a/Source/Charts/Renderers/PieChartRenderer.swift +++ b/Source/Charts/Renderers/PieChartRenderer.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - open class PieChartRenderer: DataRenderer { @objc open weak var chart: PieChartView? diff --git a/Source/Charts/Renderers/RadarChartRenderer.swift b/Source/Charts/Renderers/RadarChartRenderer.swift index 0177bb0377..95f553f5af 100644 --- a/Source/Charts/Renderers/RadarChartRenderer.swift +++ b/Source/Charts/Renderers/RadarChartRenderer.swift @@ -12,11 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - - open class RadarChartRenderer: LineRadarRenderer { private lazy var accessibilityXLabels: [String] = { diff --git a/Source/Charts/Renderers/ScatterChartRenderer.swift b/Source/Charts/Renderers/ScatterChartRenderer.swift index 2f5f45da7f..8e03f64f6e 100644 --- a/Source/Charts/Renderers/ScatterChartRenderer.swift +++ b/Source/Charts/Renderers/ScatterChartRenderer.swift @@ -12,11 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - - open class ScatterChartRenderer: LineScatterCandleRadarRenderer { @objc open weak var dataProvider: ScatterChartDataProvider? diff --git a/Source/Charts/Renderers/XAxisRenderer.swift b/Source/Charts/Renderers/XAxisRenderer.swift index 0cca5116b2..a9732e07ab 100644 --- a/Source/Charts/Renderers/XAxisRenderer.swift +++ b/Source/Charts/Renderers/XAxisRenderer.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartXAxisRenderer) open class XAxisRenderer: AxisRendererBase { @@ -172,11 +168,7 @@ open class XAxisRenderer: AxisRendererBase let transformer = self.transformer else { return } - #if os(OSX) - let paraStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle - #else - let paraStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle - #endif + let paraStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle paraStyle.alignment = .center let labelAttrs: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font: xAxis.labelFont, diff --git a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift index 6c3cd98427..9b75d9ef74 100644 --- a/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererHorizontalBarChart.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - open class XAxisRendererHorizontalBarChart: XAxisRenderer { internal weak var chart: BarChartView? diff --git a/Source/Charts/Renderers/XAxisRendererRadarChart.swift b/Source/Charts/Renderers/XAxisRendererRadarChart.swift index 0638f3a19c..c5fcedf54c 100644 --- a/Source/Charts/Renderers/XAxisRendererRadarChart.swift +++ b/Source/Charts/Renderers/XAxisRendererRadarChart.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - open class XAxisRendererRadarChart: XAxisRenderer { @objc open weak var chart: RadarChartView? diff --git a/Source/Charts/Renderers/YAxisRenderer.swift b/Source/Charts/Renderers/YAxisRenderer.swift index f369943842..a6f75b62f0 100644 --- a/Source/Charts/Renderers/YAxisRenderer.swift +++ b/Source/Charts/Renderers/YAxisRenderer.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - @objc(ChartYAxisRenderer) open class YAxisRenderer: AxisRendererBase { diff --git a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift index 2e7bd4d5c2..247cd091bf 100644 --- a/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift +++ b/Source/Charts/Renderers/YAxisRendererHorizontalBarChart.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - open class YAxisRendererHorizontalBarChart: YAxisRenderer { public override init(viewPortHandler: ViewPortHandler, yAxis: YAxis?, transformer: Transformer?) diff --git a/Source/Charts/Renderers/YAxisRendererRadarChart.swift b/Source/Charts/Renderers/YAxisRendererRadarChart.swift index bb3e0b5c04..c98971e904 100644 --- a/Source/Charts/Renderers/YAxisRendererRadarChart.swift +++ b/Source/Charts/Renderers/YAxisRendererRadarChart.swift @@ -12,10 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - open class YAxisRendererRadarChart: YAxisRenderer { private weak var chart: RadarChartView? diff --git a/Source/Charts/Utils/ChartColorTemplates.swift b/Source/Charts/Utils/ChartColorTemplates.swift index 461ef8ab3d..534f9cb880 100644 --- a/Source/Charts/Utils/ChartColorTemplates.swift +++ b/Source/Charts/Utils/ChartColorTemplates.swift @@ -12,11 +12,6 @@ import Foundation import CoreGraphics -#if !os(OSX) - import UIKit -#endif - - open class ChartColorTemplates: NSObject { @objc open class func liberty () -> [NSUIColor] diff --git a/Source/Charts/Utils/Platform.swift b/Source/Charts/Utils/Platform.swift index 3a4258be37..3c11b32a4a 100644 --- a/Source/Charts/Utils/Platform.swift +++ b/Source/Charts/Utils/Platform.swift @@ -1,629 +1,629 @@ import Foundation /** This file provides a thin abstraction layer atop of UIKit (iOS, tvOS) and Cocoa (OS X). The two APIs are very much -alike, and for the chart library's usage of the APIs it is often sufficient to typealias one to the other. The NSUI* -types are aliased to either their UI* implementation (on iOS) or their NS* implementation (on OS X). */ + alike, and for the chart library's usage of the APIs it is often sufficient to typealias one to the other. The NSUI* + types are aliased to either their UI* implementation (on iOS) or their NS* implementation (on OS X). */ #if os(iOS) || os(tvOS) - import UIKit - - public typealias NSUIFont = UIFont - public typealias NSUIColor = UIColor - public typealias NSUIEvent = UIEvent - public typealias NSUITouch = UITouch - public typealias NSUIImage = UIImage - public typealias NSUIScrollView = UIScrollView - public typealias NSUIGestureRecognizer = UIGestureRecognizer +import UIKit + +public typealias NSUIFont = UIFont +public typealias NSUIColor = UIColor +public typealias NSUIEvent = UIEvent +public typealias NSUITouch = UITouch +public typealias NSUIImage = UIImage +public typealias NSUIScrollView = UIScrollView +public typealias NSUIGestureRecognizer = UIGestureRecognizer public typealias NSUIGestureRecognizerState = UIGestureRecognizer.State - public typealias NSUIGestureRecognizerDelegate = UIGestureRecognizerDelegate - public typealias NSUITapGestureRecognizer = UITapGestureRecognizer - public typealias NSUIPanGestureRecognizer = UIPanGestureRecognizer +public typealias NSUIGestureRecognizerDelegate = UIGestureRecognizerDelegate +public typealias NSUITapGestureRecognizer = UITapGestureRecognizer +public typealias NSUIPanGestureRecognizer = UIPanGestureRecognizer #if !os(tvOS) - public typealias NSUIPinchGestureRecognizer = UIPinchGestureRecognizer - public typealias NSUIRotationGestureRecognizer = UIRotationGestureRecognizer +public typealias NSUIPinchGestureRecognizer = UIPinchGestureRecognizer +public typealias NSUIRotationGestureRecognizer = UIRotationGestureRecognizer #endif - public typealias NSUIScreen = UIScreen +public typealias NSUIScreen = UIScreen - public typealias NSUIDisplayLink = CADisplayLink - - extension NSUITapGestureRecognizer +public typealias NSUIDisplayLink = CADisplayLink + +extension NSUITapGestureRecognizer +{ + @objc final func nsuiNumberOfTouches() -> Int { - @objc final func nsuiNumberOfTouches() -> Int + return numberOfTouches + } + + @objc final var nsuiNumberOfTapsRequired: Int + { + get { - return numberOfTouches + return self.numberOfTapsRequired } - - @objc final var nsuiNumberOfTapsRequired: Int + set { - get - { - return self.numberOfTapsRequired - } - set - { - self.numberOfTapsRequired = newValue - } + self.numberOfTapsRequired = newValue } } - - extension NSUIPanGestureRecognizer +} + +extension NSUIPanGestureRecognizer +{ + @objc final func nsuiNumberOfTouches() -> Int { - @objc final func nsuiNumberOfTouches() -> Int - { - return numberOfTouches - } - - @objc final func nsuiLocationOfTouch(_ touch: Int, inView: UIView?) -> CGPoint - { - return super.location(ofTouch: touch, in: inView) - } + return numberOfTouches } - -#if !os(tvOS) - extension NSUIRotationGestureRecognizer + + @objc final func nsuiLocationOfTouch(_ touch: Int, inView: UIView?) -> CGPoint { - @objc final var nsuiRotation: CGFloat + return super.location(ofTouch: touch, in: inView) + } +} + +#if !os(tvOS) +extension NSUIRotationGestureRecognizer +{ + @objc final var nsuiRotation: CGFloat { - get { return rotation } - set { rotation = newValue } - } + get { return rotation } + set { rotation = newValue } } +} #endif - + #if !os(tvOS) - extension NSUIPinchGestureRecognizer - { - @objc final var nsuiScale: CGFloat +extension NSUIPinchGestureRecognizer +{ + @objc final var nsuiScale: CGFloat { - get - { - return scale - } - set - { - scale = newValue - } + get + { + return scale } - - @objc final func nsuiLocationOfTouch(_ touch: Int, inView: UIView?) -> CGPoint + set { - return super.location(ofTouch: touch, in: inView) + scale = newValue } } -#endif - open class NSUIView: UIView + @objc final func nsuiLocationOfTouch(_ touch: Int, inView: UIView?) -> CGPoint { - public final override func touchesBegan(_ touches: Set, with event: NSUIEvent?) - { - self.nsuiTouchesBegan(touches, withEvent: event) - } - - public final override func touchesMoved(_ touches: Set, with event: NSUIEvent?) - { - self.nsuiTouchesMoved(touches, withEvent: event) - } - - public final override func touchesEnded(_ touches: Set, with event: NSUIEvent?) - { - self.nsuiTouchesEnded(touches, withEvent: event) - } - - public final override func touchesCancelled(_ touches: Set, with event: NSUIEvent?) - { - self.nsuiTouchesCancelled(touches, withEvent: event) - } - - @objc open func nsuiTouchesBegan(_ touches: Set, withEvent event: NSUIEvent?) - { - super.touchesBegan(touches, with: event!) - } - - @objc open func nsuiTouchesMoved(_ touches: Set, withEvent event: NSUIEvent?) - { - super.touchesMoved(touches, with: event!) - } - - @objc open func nsuiTouchesEnded(_ touches: Set, withEvent event: NSUIEvent?) - { - super.touchesEnded(touches, with: event!) - } - - @objc open func nsuiTouchesCancelled(_ touches: Set?, withEvent event: NSUIEvent?) - { - super.touchesCancelled(touches!, with: event!) - } - - @objc var nsuiLayer: CALayer? - { - return self.layer - } - } + return super.location(ofTouch: touch, in: inView) + } +} +#endif - extension UIView +open class NSUIView: UIView +{ + public final override func touchesBegan(_ touches: Set, with event: NSUIEvent?) { - @objc final var nsuiGestureRecognizers: [NSUIGestureRecognizer]? - { - return self.gestureRecognizers - } + self.nsuiTouchesBegan(touches, withEvent: event) } - - extension UIScrollView + + public final override func touchesMoved(_ touches: Set, with event: NSUIEvent?) { - @objc var nsuiIsScrollEnabled: Bool - { - get { return isScrollEnabled } - set { isScrollEnabled = newValue } - } + self.nsuiTouchesMoved(touches, withEvent: event) } - - extension UIScreen + + public final override func touchesEnded(_ touches: Set, with event: NSUIEvent?) { - @objc final var nsuiScale: CGFloat - { - return self.scale - } + self.nsuiTouchesEnded(touches, withEvent: event) } - func NSUIGraphicsGetCurrentContext() -> CGContext? + public final override func touchesCancelled(_ touches: Set, with event: NSUIEvent?) { - return UIGraphicsGetCurrentContext() - } + self.nsuiTouchesCancelled(touches, withEvent: event) + } - func NSUIGraphicsGetImageFromCurrentImageContext() -> NSUIImage! + @objc open func nsuiTouchesBegan(_ touches: Set, withEvent event: NSUIEvent?) { - return UIGraphicsGetImageFromCurrentImageContext() - } + super.touchesBegan(touches, with: event!) + } - func NSUIGraphicsPushContext(_ context: CGContext) + @objc open func nsuiTouchesMoved(_ touches: Set, withEvent event: NSUIEvent?) { - UIGraphicsPushContext(context) - } + super.touchesMoved(touches, with: event!) + } - func NSUIGraphicsPopContext() + @objc open func nsuiTouchesEnded(_ touches: Set, withEvent event: NSUIEvent?) { - UIGraphicsPopContext() - } + super.touchesEnded(touches, with: event!) + } - func NSUIGraphicsEndImageContext() + @objc open func nsuiTouchesCancelled(_ touches: Set?, withEvent event: NSUIEvent?) { - UIGraphicsEndImageContext() - } + super.touchesCancelled(touches!, with: event!) + } - func NSUIImagePNGRepresentation(_ image: NSUIImage) -> Data? + @objc var nsuiLayer: CALayer? { - return image.pngData() - } + return self.layer + } +} - func NSUIImageJPEGRepresentation(_ image: NSUIImage, _ quality: CGFloat = 0.8) -> Data? +extension UIView +{ + @objc final var nsuiGestureRecognizers: [NSUIGestureRecognizer]? { - return image.jpegData(compressionQuality: quality) - } + return self.gestureRecognizers + } +} - func NSUIMainScreen() -> NSUIScreen? - { - return NSUIScreen.main - } +extension UIScrollView +{ + @objc var nsuiIsScrollEnabled: Bool + { + get { return isScrollEnabled } + set { isScrollEnabled = newValue } + } +} - func NSUIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat) +extension UIScreen +{ + @objc final var nsuiScale: CGFloat { - UIGraphicsBeginImageContextWithOptions(size, opaque, scale) - } + return self.scale + } +} + +func NSUIGraphicsGetCurrentContext() -> CGContext? +{ + return UIGraphicsGetCurrentContext() +} + +func NSUIGraphicsGetImageFromCurrentImageContext() -> NSUIImage! +{ + return UIGraphicsGetImageFromCurrentImageContext() +} + +func NSUIGraphicsPushContext(_ context: CGContext) +{ + UIGraphicsPushContext(context) +} + +func NSUIGraphicsPopContext() +{ + UIGraphicsPopContext() +} + +func NSUIGraphicsEndImageContext() +{ + UIGraphicsEndImageContext() +} + +func NSUIImagePNGRepresentation(_ image: NSUIImage) -> Data? +{ + return image.pngData() +} + +func NSUIImageJPEGRepresentation(_ image: NSUIImage, _ quality: CGFloat = 0.8) -> Data? +{ + return image.jpegData(compressionQuality: quality) +} + +func NSUIMainScreen() -> NSUIScreen? +{ + return NSUIScreen.main +} + +func NSUIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat) +{ + UIGraphicsBeginImageContextWithOptions(size, opaque, scale) +} #endif #if os(OSX) - import Cocoa - import Quartz - - public typealias NSUIFont = NSFont - public typealias NSUIColor = NSColor - public typealias NSUIEvent = NSEvent - public typealias NSUITouch = NSTouch - public typealias NSUIImage = NSImage - public typealias NSUIScrollView = NSScrollView - public typealias NSUIGestureRecognizer = NSGestureRecognizer - public typealias NSUIGestureRecognizerState = NSGestureRecognizer.State - public typealias NSUIGestureRecognizerDelegate = NSGestureRecognizerDelegate - public typealias NSUITapGestureRecognizer = NSClickGestureRecognizer - public typealias NSUIPanGestureRecognizer = NSPanGestureRecognizer - public typealias NSUIPinchGestureRecognizer = NSMagnificationGestureRecognizer - public typealias NSUIRotationGestureRecognizer = NSRotationGestureRecognizer - public typealias NSUIScreen = NSScreen - - /** On OS X there is no CADisplayLink. Use a 60 fps timer to render the animations. */ - public class NSUIDisplayLink - { - private var timer: Timer? - private var displayLink: CVDisplayLink? - private var _timestamp: CFTimeInterval = 0.0 - - private weak var _target: AnyObject? - private var _selector: Selector - - public var timestamp: CFTimeInterval - { - return _timestamp - } +import Cocoa +import Quartz + +public typealias NSUIFont = NSFont +public typealias NSUIColor = NSColor +public typealias NSUIEvent = NSEvent +public typealias NSUITouch = NSTouch +public typealias NSUIImage = NSImage +public typealias NSUIScrollView = NSScrollView +public typealias NSUIGestureRecognizer = NSGestureRecognizer +public typealias NSUIGestureRecognizerState = NSGestureRecognizer.State +public typealias NSUIGestureRecognizerDelegate = NSGestureRecognizerDelegate +public typealias NSUITapGestureRecognizer = NSClickGestureRecognizer +public typealias NSUIPanGestureRecognizer = NSPanGestureRecognizer +public typealias NSUIPinchGestureRecognizer = NSMagnificationGestureRecognizer +public typealias NSUIRotationGestureRecognizer = NSRotationGestureRecognizer +public typealias NSUIScreen = NSScreen + +/** On OS X there is no CADisplayLink. Use a 60 fps timer to render the animations. */ +public class NSUIDisplayLink +{ + private var timer: Timer? + private var displayLink: CVDisplayLink? + private var _timestamp: CFTimeInterval = 0.0 + + private weak var _target: AnyObject? + private var _selector: Selector + + public var timestamp: CFTimeInterval + { + return _timestamp + } init(target: Any, selector: Selector) + { + _target = target as AnyObject + _selector = selector + + if CVDisplayLinkCreateWithActiveCGDisplays(&displayLink) == kCVReturnSuccess { - _target = target as AnyObject - _selector = selector - - if CVDisplayLinkCreateWithActiveCGDisplays(&displayLink) == kCVReturnSuccess - { - - CVDisplayLinkSetOutputCallback(displayLink!, { (displayLink, inNow, inOutputTime, flagsIn, flagsOut, userData) -> CVReturn in - - let _self = unsafeBitCast(userData, to: NSUIDisplayLink.self) + + CVDisplayLinkSetOutputCallback(displayLink!, { (displayLink, inNow, inOutputTime, flagsIn, flagsOut, userData) -> CVReturn in + + let _self = unsafeBitCast(userData, to: NSUIDisplayLink.self) - _self._timestamp = CFAbsoluteTimeGetCurrent() - _self._target?.performSelector(onMainThread: _self._selector, with: _self, waitUntilDone: false) + _self._timestamp = CFAbsoluteTimeGetCurrent() + _self._target?.performSelector(onMainThread: _self._selector, with: _self, waitUntilDone: false) - return kCVReturnSuccess - }, Unmanaged.passUnretained(self).toOpaque()) - } - else - { - timer = Timer(timeInterval: 1.0 / 60.0, target: target, selector: selector, userInfo: nil, repeats: true) - } - } - - deinit - { - stop() + return kCVReturnSuccess + }, Unmanaged.passUnretained(self).toOpaque()) } - - open func add(to runloop: RunLoop, forMode mode: RunLoop.Mode) + else { - if displayLink != nil - { - CVDisplayLinkStart(displayLink!) - } - else if timer != nil - { - runloop.add(timer!, forMode: mode) - } + timer = Timer(timeInterval: 1.0 / 60.0, target: target, selector: selector, userInfo: nil, repeats: true) + } } - open func remove(from: RunLoop, forMode: RunLoop.Mode) - { - stop() - } - - private func stop() - { - if displayLink != nil - { - CVDisplayLinkStop(displayLink!) - } - if timer != nil - { - timer?.invalidate() - } - } - } + deinit + { + stop() + } - /** The 'tap' gesture is mapped to clicks. */ - extension NSUITapGestureRecognizer + open func add(to runloop: RunLoop, forMode mode: RunLoop.Mode) { - final func nsuiNumberOfTouches() -> Int + if displayLink != nil { - return 1 - } - - final var nsuiNumberOfTapsRequired: Int + CVDisplayLinkStart(displayLink!) + } + else if timer != nil { - get - { - return self.numberOfClicksRequired - } - set - { - self.numberOfClicksRequired = newValue - } - } - } + runloop.add(timer!, forMode: mode) + } + } - extension NSUIPanGestureRecognizer + open func remove(from: RunLoop, forMode: RunLoop.Mode) { - final func nsuiNumberOfTouches() -> Int - { - return 1 - } - - /// FIXME: Currently there are no more than 1 touch in OSX gestures, and not way to create custom touch gestures. - final func nsuiLocationOfTouch(_ touch: Int, inView: NSView?) -> NSPoint - { - return super.location(in: inView) - } + stop() } - - extension NSUIRotationGestureRecognizer + + private func stop() { - /// FIXME: Currently there are no velocities in OSX gestures, and not way to create custom touch gestures. - final var velocity: CGFloat + if displayLink != nil { - return 0.1 + CVDisplayLinkStop(displayLink!) } - - final var nsuiRotation: CGFloat + if timer != nil { - get { return -rotation } - set { rotation = -newValue } + timer?.invalidate() } } - - extension NSUIPinchGestureRecognizer +} + +/** The 'tap' gesture is mapped to clicks. */ +extension NSUITapGestureRecognizer +{ + final func nsuiNumberOfTouches() -> Int { - final var nsuiScale: CGFloat + return 1 + } + + final var nsuiNumberOfTapsRequired: Int + { + get { - get - { - return magnification + 1.0 - } - set - { - magnification = newValue - 1.0 - } + return self.numberOfClicksRequired } - - /// FIXME: Currently there are no more than 1 touch in OSX gestures, and not way to create custom touch gestures. - final func nsuiLocationOfTouch(_ touch: Int, inView view: NSView?) -> NSPoint + set { - return super.location(in: view) + self.numberOfClicksRequired = newValue } } +} - extension NSView +extension NSUIPanGestureRecognizer +{ + final func nsuiNumberOfTouches() -> Int { - final var nsuiGestureRecognizers: [NSGestureRecognizer]? - { - return self.gestureRecognizers - } - } + return 1 + } - extension NSScrollView + /// FIXME: Currently there are no more than 1 touch in OSX gestures, and not way to create custom touch gestures. + final func nsuiLocationOfTouch(_ touch: Int, inView: NSView?) -> NSPoint { - var nsuiIsScrollEnabled: Bool - { - get { return scrollEnabled } - set { scrollEnabled = newValue } - } + return super.location(in: inView) } - - open class NSUIView: NSView - { - /// A private constant to set the accessibility role during initialization. - /// It ensures parity with the iOS element ordering as well as numbered counts of chart components. - /// (See Platform+Accessibility for details) - private let role: NSAccessibility.Role = .list +} - public override init(frame frameRect: NSRect) - { - super.init(frame: frameRect) - setAccessibilityRole(role) - } +extension NSUIRotationGestureRecognizer +{ + /// FIXME: Currently there are no velocities in OSX gestures, and not way to create custom touch gestures. + final var velocity: CGFloat + { + return 0.1 + } - required public init?(coder decoder: NSCoder) + final var nsuiRotation: CGFloat { - super.init(coder: decoder) - setAccessibilityRole(role) - } + get { return -rotation } + set { rotation = -newValue } + } +} - public final override var isFlipped: Bool +extension NSUIPinchGestureRecognizer +{ + final var nsuiScale: CGFloat { - return true - } - - func setNeedsDisplay() + get { - self.setNeedsDisplay(self.bounds) - } - - public final override func touchesBegan(with event: NSEvent) + return magnification + 1.0 + } + set { - self.nsuiTouchesBegan(event.touches(matching: .any, in: self), withEvent: event) - } + magnification = newValue - 1.0 + } + } - public final override func touchesEnded(with event: NSEvent) - { - self.nsuiTouchesEnded(event.touches(matching: .any, in: self), withEvent: event) - } + /// FIXME: Currently there are no more than 1 touch in OSX gestures, and not way to create custom touch gestures. + final func nsuiLocationOfTouch(_ touch: Int, inView view: NSView?) -> NSPoint + { + return super.location(in: view) + } +} - public final override func touchesMoved(with event: NSEvent) - { - self.nsuiTouchesMoved(event.touches(matching: .any, in: self), withEvent: event) - } +extension NSView +{ + final var nsuiGestureRecognizers: [NSGestureRecognizer]? + { + return self.gestureRecognizers + } +} - open override func touchesCancelled(with event: NSEvent) - { - self.nsuiTouchesCancelled(event.touches(matching: .any, in: self), withEvent: event) - } +extension NSScrollView +{ + var nsuiIsScrollEnabled: Bool + { + get { return scrollEnabled } + set { scrollEnabled = newValue } + } +} - open func nsuiTouchesBegan(_ touches: Set, withEvent event: NSUIEvent?) - { - super.touchesBegan(with: event!) - } +open class NSUIView: NSView +{ + /// A private constant to set the accessibility role during initialization. + /// It ensures parity with the iOS element ordering as well as numbered counts of chart components. + /// (See Platform+Accessibility for details) + private let role: NSAccessibility.Role = .list - open func nsuiTouchesMoved(_ touches: Set, withEvent event: NSUIEvent?) - { - super.touchesMoved(with: event!) - } + public override init(frame frameRect: NSRect) + { + super.init(frame: frameRect) + setAccessibilityRole(role) + } - open func nsuiTouchesEnded(_ touches: Set, withEvent event: NSUIEvent?) - { - super.touchesEnded(with: event!) - } + required public init?(coder decoder: NSCoder) + { + super.init(coder: decoder) + setAccessibilityRole(role) + } - open func nsuiTouchesCancelled(_ touches: Set?, withEvent event: NSUIEvent?) - { - super.touchesCancelled(with: event!) - } - - open var backgroundColor: NSUIColor? - { - get - { - return self.layer?.backgroundColor == nil - ? nil - : NSColor(cgColor: self.layer!.backgroundColor!) - } - set - { - self.wantsLayer = true - self.layer?.backgroundColor = newValue == nil ? nil : newValue!.cgColor - } - } + public final override var isFlipped: Bool + { + return true + } - final var nsuiLayer: CALayer? - { - return self.layer - } - } + func setNeedsDisplay() + { + self.setNeedsDisplay(self.bounds) + } - extension NSFont + public final override func touchesBegan(with event: NSEvent) { - var lineHeight: CGFloat - { - // Not sure if this is right, but it looks okay - return self.boundingRectForFont.size.height - } - } + self.nsuiTouchesBegan(event.touches(matching: .any, in: self), withEvent: event) + } - extension NSScreen + public final override func touchesEnded(with event: NSEvent) { - final var nsuiScale: CGFloat - { - return self.backingScaleFactor - } - } + self.nsuiTouchesEnded(event.touches(matching: .any, in: self), withEvent: event) + } - extension NSImage + public final override func touchesMoved(with event: NSEvent) { - var cgImage: CGImage? - { - return self.cgImage(forProposedRect: nil, context: nil, hints: nil) - } - } + self.nsuiTouchesMoved(event.touches(matching: .any, in: self), withEvent: event) + } - extension NSTouch + open override func touchesCancelled(with event: NSEvent) { - /** Touch locations on OS X are relative to the trackpad, whereas on iOS they are actually *on* the view. */ - func locationInView(view: NSView) -> NSPoint - { - let n = self.normalizedPosition - let b = view.bounds - return NSPoint(x: b.origin.x + b.size.width * n.x, y: b.origin.y + b.size.height * n.y) - } - } + self.nsuiTouchesCancelled(event.touches(matching: .any, in: self), withEvent: event) + } - extension NSScrollView + open func nsuiTouchesBegan(_ touches: Set, withEvent event: NSUIEvent?) { - var scrollEnabled: Bool - { - get - { - return true - } - set - { - // FIXME: We can't disable scrolling it on OSX - } - } + super.touchesBegan(with: event!) } - func NSUIGraphicsGetCurrentContext() -> CGContext? + open func nsuiTouchesMoved(_ touches: Set, withEvent event: NSUIEvent?) { - return NSGraphicsContext.current?.cgContext - } + super.touchesMoved(with: event!) + } - func NSUIGraphicsPushContext(_ context: CGContext) + open func nsuiTouchesEnded(_ touches: Set, withEvent event: NSUIEvent?) { - let cx = NSGraphicsContext(cgContext: context, flipped: true) - NSGraphicsContext.saveGraphicsState() - NSGraphicsContext.current = cx - } + super.touchesEnded(with: event!) + } - func NSUIGraphicsPopContext() + open func nsuiTouchesCancelled(_ touches: Set?, withEvent event: NSUIEvent?) { - NSGraphicsContext.restoreGraphicsState() - } + super.touchesCancelled(with: event!) + } - func NSUIImagePNGRepresentation(_ image: NSUIImage) -> Data? + open var backgroundColor: NSUIColor? + { + get + { + return self.layer?.backgroundColor == nil + ? nil + : NSColor(cgColor: self.layer!.backgroundColor!) + } + set + { + self.wantsLayer = true + self.layer?.backgroundColor = newValue == nil ? nil : newValue!.cgColor + } + } + + final var nsuiLayer: CALayer? { - image.lockFocus() - let rep = NSBitmapImageRep(focusedViewRect: NSMakeRect(0, 0, image.size.width, image.size.height)) - image.unlockFocus() - return rep?.representation(using: .png, properties: [:]) - } + return self.layer + } +} - func NSUIImageJPEGRepresentation(_ image: NSUIImage, _ quality: CGFloat = 0.9) -> Data? +extension NSFont +{ + var lineHeight: CGFloat { - image.lockFocus() - let rep = NSBitmapImageRep(focusedViewRect: NSMakeRect(0, 0, image.size.width, image.size.height)) - image.unlockFocus() - return rep?.representation(using: .jpeg, properties: [NSBitmapImageRep.PropertyKey.compressionFactor: quality]) - } + // Not sure if this is right, but it looks okay + return self.boundingRectForFont.size.height + } +} - private var imageContextStack: [CGFloat] = [] +extension NSScreen +{ + final var nsuiScale: CGFloat + { + return self.backingScaleFactor + } +} - func NSUIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat) +extension NSImage +{ + var cgImage: CGImage? { - var scale = scale - if scale == 0.0 - { - scale = NSScreen.main?.backingScaleFactor ?? 1.0 - } + return self.cgImage(forProposedRect: nil, context: nil, hints: nil) + } +} - let width = Int(size.width * scale) - let height = Int(size.height * scale) +extension NSTouch +{ + /** Touch locations on OS X are relative to the trackpad, whereas on iOS they are actually *on* the view. */ + func locationInView(view: NSView) -> NSPoint + { + let n = self.normalizedPosition + let b = view.bounds + return NSPoint(x: b.origin.x + b.size.width * n.x, y: b.origin.y + b.size.height * n.y) + } +} - if width > 0 && height > 0 +extension NSScrollView +{ + var scrollEnabled: Bool + { + get { - imageContextStack.append(scale) - - let colorSpace = CGColorSpaceCreateDeviceRGB() - - guard let ctx = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 4*width, space: colorSpace, bitmapInfo: (opaque ? CGImageAlphaInfo.noneSkipFirst.rawValue : CGImageAlphaInfo.premultipliedFirst.rawValue)) - else { return } - - ctx.concatenate(CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: CGFloat(height))) - ctx.scaleBy(x: scale, y: scale) - NSUIGraphicsPushContext(ctx) - } - } + return true + } + set + { + // FIXME: We can't disable scrolling it on OSX + } + } +} + +func NSUIGraphicsGetCurrentContext() -> CGContext? +{ + return NSGraphicsContext.current?.cgContext +} + +func NSUIGraphicsPushContext(_ context: CGContext) +{ + let cx = NSGraphicsContext(cgContext: context, flipped: true) + NSGraphicsContext.saveGraphicsState() + NSGraphicsContext.current = cx +} + +func NSUIGraphicsPopContext() +{ + NSGraphicsContext.restoreGraphicsState() +} + +func NSUIImagePNGRepresentation(_ image: NSUIImage) -> Data? +{ + image.lockFocus() + let rep = NSBitmapImageRep(focusedViewRect: NSMakeRect(0, 0, image.size.width, image.size.height)) + image.unlockFocus() + return rep?.representation(using: .png, properties: [:]) +} + +func NSUIImageJPEGRepresentation(_ image: NSUIImage, _ quality: CGFloat = 0.9) -> Data? +{ + image.lockFocus() + let rep = NSBitmapImageRep(focusedViewRect: NSMakeRect(0, 0, image.size.width, image.size.height)) + image.unlockFocus() + return rep?.representation(using: .jpeg, properties: [NSBitmapImageRep.PropertyKey.compressionFactor: quality]) +} + +private var imageContextStack: [CGFloat] = [] + +func NSUIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat) +{ + var scale = scale + if scale == 0.0 + { + scale = NSScreen.main?.backingScaleFactor ?? 1.0 + } + + let width = Int(size.width * scale) + let height = Int(size.height * scale) - func NSUIGraphicsGetImageFromCurrentImageContext() -> NSUIImage? + if width > 0 && height > 0 { - if !imageContextStack.isEmpty - { - guard let ctx = NSUIGraphicsGetCurrentContext() - else { return nil } - - let scale = imageContextStack.last! - if let theCGImage = ctx.makeImage() - { - let size = CGSize(width: CGFloat(ctx.width) / scale, height: CGFloat(ctx.height) / scale) - let image = NSImage(cgImage: theCGImage, size: size) - return image - } - } - return nil - } + imageContextStack.append(scale) + + let colorSpace = CGColorSpaceCreateDeviceRGB() + + guard let ctx = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 4*width, space: colorSpace, bitmapInfo: (opaque ? CGImageAlphaInfo.noneSkipFirst.rawValue : CGImageAlphaInfo.premultipliedFirst.rawValue)) + else { return } - func NSUIGraphicsEndImageContext() + ctx.concatenate(CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: CGFloat(height))) + ctx.scaleBy(x: scale, y: scale) + NSUIGraphicsPushContext(ctx) + } +} + +func NSUIGraphicsGetImageFromCurrentImageContext() -> NSUIImage? +{ + if !imageContextStack.isEmpty { - if imageContextStack.last != nil + guard let ctx = NSUIGraphicsGetCurrentContext() + else { return nil } + + let scale = imageContextStack.last! + if let theCGImage = ctx.makeImage() { - imageContextStack.removeLast() - NSUIGraphicsPopContext() - } - } + let size = CGSize(width: CGFloat(ctx.width) / scale, height: CGFloat(ctx.height) / scale) + let image = NSImage(cgImage: theCGImage, size: size) + return image + } + } + return nil +} - func NSUIMainScreen() -> NSUIScreen? +func NSUIGraphicsEndImageContext() +{ + if imageContextStack.last != nil { - return NSUIScreen.main - } - + imageContextStack.removeLast() + NSUIGraphicsPopContext() + } +} + +func NSUIMainScreen() -> NSUIScreen? +{ + return NSUIScreen.main +} + #endif diff --git a/Tests/Charts/Snapshot.swift b/Tests/Charts/Snapshot.swift index 945fe9e57b..0f3be9d452 100644 --- a/Tests/Charts/Snapshot.swift +++ b/Tests/Charts/Snapshot.swift @@ -5,17 +5,14 @@ public struct Snapshot public static let tolerance: CGFloat = 0.01 public static func identifier(_ size: CGSize) -> String { - - let identifier: String - #if os(tvOS) - identifier = "tvOS" + let identifier = "tvOS" #elseif os(iOS) - identifier = "iOS" + let identifier = "iOS" #elseif os(OSX) - identifier = "macOS" + let identifier = "macOS" #else - identifier = "" + let identifier = "" #endif return "\(identifier)_\(size.width)_\(size.height)"