diff --git a/Stats.xcodeproj/project.pbxproj b/Stats.xcodeproj/project.pbxproj index c75ad661..d8a139a4 100755 --- a/Stats.xcodeproj/project.pbxproj +++ b/Stats.xcodeproj/project.pbxproj @@ -18,11 +18,9 @@ 9A57A19D22A1E3270033E318 /* CPU.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A57A19C22A1E3270033E318 /* CPU.swift */; }; 9A58D1B022C150C800405315 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A58D1AF22C150C800405315 /* Network.swift */; }; 9A58D1B222C150D700405315 /* NetworkReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A58D1B122C150D700405315 /* NetworkReader.swift */; }; - 9A58D1B422C179B200405315 /* NetworkView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A58D1B322C179B200405315 /* NetworkView.swift */; }; 9A5B1CBF229E78F0008B9D3C /* Observable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A5B1CBE229E78F0008B9D3C /* Observable.swift */; }; 9A5B1CC5229E7B40008B9D3C /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A5B1CC4229E7B40008B9D3C /* Extensions.swift */; }; 9A6CFC0122A1C9F5001E782D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9A6CFC0022A1C9F5001E782D /* Assets.xcassets */; }; - 9A74D59422B4315C004FE1FA /* Chart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A74D59322B4315C004FE1FA /* Chart.swift */; }; 9A74D59722B44498004FE1FA /* Mini.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A74D59622B44498004FE1FA /* Mini.swift */; }; 9A79B36A22D3BEE600BF1C3A /* Widget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A79B36922D3BEE600BF1C3A /* Widget.swift */; }; 9A79B36C22D3BEF000BF1C3A /* Module.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A79B36B22D3BEF000BF1C3A /* Module.swift */; }; @@ -32,7 +30,14 @@ 9A7B8F6B22A2C3A700DEB352 /* Disk.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A7B8F6A22A2C3A700DEB352 /* Disk.swift */; }; 9A7B8F6D22A2C3D600DEB352 /* MemoryReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A7B8F6C22A2C3D600DEB352 /* MemoryReader.swift */; }; 9A7B8F6F22A2C57000DEB352 /* DiskReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A7B8F6E22A2C57000DEB352 /* DiskReader.swift */; }; - 9A9696EC22D526D900F967A5 /* BarChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A9696EB22D526D900F967A5 /* BarChart.swift */; }; + 9AF0F31B22DA924000026AE6 /* LineChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF0F31A22DA924000026AE6 /* LineChart.swift */; }; + 9AF0F31D22DA925000026AE6 /* LineChartWithValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF0F31C22DA925000026AE6 /* LineChartWithValue.swift */; }; + 9AF0F31F22DA925700026AE6 /* BarChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF0F31E22DA925700026AE6 /* BarChart.swift */; }; + 9AF0F32122DA92AD00026AE6 /* NetworkDots.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF0F32022DA92AD00026AE6 /* NetworkDots.swift */; }; + 9AF0F32322DA92B900026AE6 /* NetworkArrows.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF0F32222DA92B900026AE6 /* NetworkArrows.swift */; }; + 9AF0F32522DA92C400026AE6 /* NetworkText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF0F32422DA92C400026AE6 /* NetworkText.swift */; }; + 9AF0F32722DA92DD00026AE6 /* NetworkDotsText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF0F32622DA92DD00026AE6 /* NetworkDotsText.swift */; }; + 9AF0F32922DA92E800026AE6 /* NetworkArrowsText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AF0F32822DA92E800026AE6 /* NetworkArrowsText.swift */; }; 9AFA402522AE49A200FE90BC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9AFA402422AE49A200FE90BC /* Assets.xcassets */; }; 9AFA402822AE49A200FE90BC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9AFA402622AE49A200FE90BC /* Main.storyboard */; }; 9AFA402F22AE49AE00FE90BC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AFA402E22AE49AE00FE90BC /* AppDelegate.swift */; }; @@ -69,11 +74,9 @@ 9A57A19C22A1E3270033E318 /* CPU.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CPU.swift; sourceTree = ""; }; 9A58D1AF22C150C800405315 /* Network.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Network.swift; sourceTree = ""; }; 9A58D1B122C150D700405315 /* NetworkReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkReader.swift; sourceTree = ""; }; - 9A58D1B322C179B200405315 /* NetworkView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkView.swift; sourceTree = ""; }; 9A5B1CBE229E78F0008B9D3C /* Observable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Observable.swift; sourceTree = ""; }; 9A5B1CC4229E7B40008B9D3C /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; 9A6CFC0022A1C9F5001E782D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; - 9A74D59322B4315C004FE1FA /* Chart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Chart.swift; sourceTree = ""; }; 9A74D59622B44498004FE1FA /* Mini.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mini.swift; sourceTree = ""; }; 9A79B36922D3BEE600BF1C3A /* Widget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Widget.swift; sourceTree = ""; }; 9A79B36B22D3BEF000BF1C3A /* Module.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Module.swift; sourceTree = ""; }; @@ -83,9 +86,16 @@ 9A7B8F6A22A2C3A700DEB352 /* Disk.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Disk.swift; sourceTree = ""; }; 9A7B8F6C22A2C3D600DEB352 /* MemoryReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryReader.swift; sourceTree = ""; }; 9A7B8F6E22A2C57000DEB352 /* DiskReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiskReader.swift; sourceTree = ""; }; - 9A9696EB22D526D900F967A5 /* BarChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BarChart.swift; sourceTree = ""; }; 9A998CD722A199920087ADE7 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; 9A998CD922A199970087ADE7 /* ServiceManagement.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ServiceManagement.framework; path = System/Library/Frameworks/ServiceManagement.framework; sourceTree = SDKROOT; }; + 9AF0F31A22DA924000026AE6 /* LineChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LineChart.swift; sourceTree = ""; }; + 9AF0F31C22DA925000026AE6 /* LineChartWithValue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LineChartWithValue.swift; sourceTree = ""; }; + 9AF0F31E22DA925700026AE6 /* BarChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BarChart.swift; sourceTree = ""; }; + 9AF0F32022DA92AD00026AE6 /* NetworkDots.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkDots.swift; sourceTree = ""; }; + 9AF0F32222DA92B900026AE6 /* NetworkArrows.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkArrows.swift; sourceTree = ""; }; + 9AF0F32422DA92C400026AE6 /* NetworkText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkText.swift; sourceTree = ""; }; + 9AF0F32622DA92DD00026AE6 /* NetworkDotsText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkDotsText.swift; sourceTree = ""; }; + 9AF0F32822DA92E800026AE6 /* NetworkArrowsText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkArrowsText.swift; sourceTree = ""; }; 9AFA401E22AE49A100FE90BC /* StatsLauncher.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = StatsLauncher.app; sourceTree = BUILT_PRODUCTS_DIR; }; 9AFA402422AE49A200FE90BC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 9AFA402722AE49A200FE90BC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; @@ -203,12 +213,11 @@ 9A74D59522B440D4004FE1FA /* Widgets */ = { isa = PBXGroup; children = ( + 9AF0F31922DA923100026AE6 /* Network */, + 9AF0F31822DA922800026AE6 /* Charts */, 9A09C8A122B3D94D0018426F /* BatteryView.swift */, - 9A74D59322B4315C004FE1FA /* Chart.swift */, 9A74D59622B44498004FE1FA /* Mini.swift */, - 9A58D1B322C179B200405315 /* NetworkView.swift */, 9A79B36922D3BEE600BF1C3A /* Widget.swift */, - 9A9696EB22D526D900F967A5 /* BarChart.swift */, ); path = Widgets; sourceTree = ""; @@ -249,6 +258,28 @@ name = Frameworks; sourceTree = ""; }; + 9AF0F31822DA922800026AE6 /* Charts */ = { + isa = PBXGroup; + children = ( + 9AF0F31A22DA924000026AE6 /* LineChart.swift */, + 9AF0F31C22DA925000026AE6 /* LineChartWithValue.swift */, + 9AF0F31E22DA925700026AE6 /* BarChart.swift */, + ); + path = Charts; + sourceTree = ""; + }; + 9AF0F31922DA923100026AE6 /* Network */ = { + isa = PBXGroup; + children = ( + 9AF0F32022DA92AD00026AE6 /* NetworkDots.swift */, + 9AF0F32222DA92B900026AE6 /* NetworkArrows.swift */, + 9AF0F32422DA92C400026AE6 /* NetworkText.swift */, + 9AF0F32622DA92DD00026AE6 /* NetworkDotsText.swift */, + 9AF0F32822DA92E800026AE6 /* NetworkArrowsText.swift */, + ); + path = Network; + sourceTree = ""; + }; 9AFA401F22AE49A100FE90BC /* StatsLauncher */ = { isa = PBXGroup; children = ( @@ -374,9 +405,10 @@ 9A79B36E22D3BEF900BF1C3A /* Reader.swift in Sources */, 9A7B8F6F22A2C57000DEB352 /* DiskReader.swift in Sources */, 9A7B8F6922A2C3A100DEB352 /* Memory.swift in Sources */, + 9AF0F32522DA92C400026AE6 /* NetworkText.swift in Sources */, 9A7B8F5E22A2A57600DEB352 /* CPUReader.swift in Sources */, - 9A74D59422B4315C004FE1FA /* Chart.swift in Sources */, - 9A58D1B422C179B200405315 /* NetworkView.swift in Sources */, + 9AF0F32322DA92B900026AE6 /* NetworkArrows.swift in Sources */, + 9AF0F31D22DA925000026AE6 /* LineChartWithValue.swift in Sources */, 9A09C89E22B3A7C90018426F /* Battery.swift in Sources */, 9A7B8F6D22A2C3D600DEB352 /* MemoryReader.swift in Sources */, 9A79B36C22D3BEF000BF1C3A /* Module.swift in Sources */, @@ -388,10 +420,14 @@ 9A5B1CBF229E78F0008B9D3C /* Observable.swift in Sources */, 9A7B8F6B22A2C3A700DEB352 /* Disk.swift in Sources */, 9A1410F9229E721100D29793 /* AppDelegate.swift in Sources */, - 9A9696EC22D526D900F967A5 /* BarChart.swift in Sources */, + 9AF0F32722DA92DD00026AE6 /* NetworkDotsText.swift in Sources */, + 9AF0F32922DA92E800026AE6 /* NetworkArrowsText.swift in Sources */, + 9AF0F31F22DA925700026AE6 /* BarChart.swift in Sources */, + 9AF0F31B22DA924000026AE6 /* LineChart.swift in Sources */, 9A74D59722B44498004FE1FA /* Mini.swift in Sources */, 9A5B1CC5229E7B40008B9D3C /* Extensions.swift in Sources */, 9A79B36A22D3BEE600BF1C3A /* Widget.swift in Sources */, + 9AF0F32122DA92AD00026AE6 /* NetworkDots.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Stats/Widgets/BarChart.swift b/Stats/Widgets/Charts/BarChart.swift similarity index 100% rename from Stats/Widgets/BarChart.swift rename to Stats/Widgets/Charts/BarChart.swift diff --git a/Stats/Widgets/Chart.swift b/Stats/Widgets/Charts/LineChart.swift similarity index 58% rename from Stats/Widgets/Chart.swift rename to Stats/Widgets/Charts/LineChart.swift index e161adf9..ff791ce6 100644 --- a/Stats/Widgets/Chart.swift +++ b/Stats/Widgets/Charts/LineChart.swift @@ -1,8 +1,8 @@ // -// CPUView.swift +// LineChart.swift // Stats // -// Created by Serhiy Mytrovtsiy on 14.06.2019. +// Created by Serhiy Mytrovtsiy on 14.07.2019. // Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved. // @@ -137,7 +137,7 @@ class Chart: NSView, Widget { NSAttributedString.Key.foregroundColor: NSColor.labelColor, NSAttributedString.Key.paragraphStyle: style ] - + let letterHeight = (self.frame.size.height - (widgetSize.margin*2)) / 3 let letterWidth: CGFloat = 10.0 @@ -173,108 +173,3 @@ class Chart: NSView, Widget { } } } - -class ChartWithValue: Chart { - var valueLabel: NSTextField = NSTextField() - var color: Bool = false - - override init(frame: NSRect) { - super.init(frame: CGRect(x: 0, y: 0, width: widgetSize.width + 7, height: widgetSize.height)) - self.wantsLayer = true - } - - required init?(coder decoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func Init() { - self.label = defaults.object(forKey: "\(name)_label") != nil ? defaults.bool(forKey: "\(name)_label") : true - self.color = defaults.object(forKey: "\(name)_color") != nil ? defaults.bool(forKey: "\(name)_color") : false - self.initMenu() - - if self.label { - self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width + labelPadding, height: self.frame.size.height) - } - self.drawValue() - } - - override func initMenu() { - let label = NSMenuItem(title: "Label", action: #selector(toggleLabel), keyEquivalent: "") - label.state = self.label ? NSControl.StateValue.on : NSControl.StateValue.off - label.target = self - - let color = NSMenuItem(title: "Color", action: #selector(toggleColor), keyEquivalent: "") - color.state = self.color ? NSControl.StateValue.on : NSControl.StateValue.off - color.target = self - - self.menus.append(label) - self.menus.append(color) - } - - override func setValue(data: [Double]) { - let value: Double = data.first! - - self.valueLabel.stringValue = "\(Int(Float(value.roundTo(decimalPlaces: 2))! * 100))%" - self.valueLabel.textColor = value.usageColor(color: self.color) - - if self.points.count < 50 { - self.points.append(value) - return - } - - for (i, _) in self.points.enumerated() { - if i+1 < self.points.count { - self.points[i] = self.points[i+1] - } else { - self.points[i] = value - } - } - } - - func drawValue () { - for subview in self.subviews { - subview.removeFromSuperview() - } - - valueLabel = NSTextField(frame: NSMakeRect(2, widgetSize.height - 11, self.frame.size.width, 10)) - if label { - valueLabel = NSTextField(frame: NSMakeRect(labelPadding + 2, widgetSize.height - 11, self.frame.size.width, 10)) - } - valueLabel.textColor = NSColor.red - valueLabel.isEditable = false - valueLabel.isSelectable = false - valueLabel.isBezeled = false - valueLabel.wantsLayer = true - valueLabel.textColor = .labelColor - valueLabel.backgroundColor = .controlColor - valueLabel.canDrawSubviewsIntoLayer = true - valueLabel.alignment = .natural - valueLabel.font = NSFont.systemFont(ofSize: 8, weight: .ultraLight) - valueLabel.stringValue = "" - valueLabel.addSubview(NSView()) - - self.height = 7.0 - self.addSubview(valueLabel) - } - - @objc override func toggleLabel(_ sender: NSMenuItem) { - sender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.on - self.defaults.set(sender.state == NSControl.StateValue.on, forKey: "\(self.name)_label") - self.label = (sender.state == NSControl.StateValue.on) - - var width = self.size - if self.label { - width = width + labelPadding - } - self.activeModule << false - self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: width, height: self.frame.size.height) - self.activeModule << true - self.drawValue() - } - - @objc func toggleColor(_ sender: NSMenuItem) { - sender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.on - self.defaults.set(sender.state == NSControl.StateValue.on, forKey: "\(name)_color") - self.color = sender.state == NSControl.StateValue.on - } -} diff --git a/Stats/Widgets/Charts/LineChartWithValue.swift b/Stats/Widgets/Charts/LineChartWithValue.swift new file mode 100644 index 00000000..a8e8d2c7 --- /dev/null +++ b/Stats/Widgets/Charts/LineChartWithValue.swift @@ -0,0 +1,114 @@ +// +// LineChartWithValue.swift +// Stats +// +// Created by Serhiy Mytrovtsiy on 14.07.2019. +// Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved. +// + +import Cocoa + +class ChartWithValue: Chart { + var valueLabel: NSTextField = NSTextField() + var color: Bool = false + + override init(frame: NSRect) { + super.init(frame: CGRect(x: 0, y: 0, width: widgetSize.width + 7, height: widgetSize.height)) + self.wantsLayer = true + } + + required init?(coder decoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func Init() { + self.label = defaults.object(forKey: "\(name)_label") != nil ? defaults.bool(forKey: "\(name)_label") : true + self.color = defaults.object(forKey: "\(name)_color") != nil ? defaults.bool(forKey: "\(name)_color") : false + self.initMenu() + + if self.label { + self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width + labelPadding, height: self.frame.size.height) + } + self.drawValue() + } + + override func initMenu() { + let label = NSMenuItem(title: "Label", action: #selector(toggleLabel), keyEquivalent: "") + label.state = self.label ? NSControl.StateValue.on : NSControl.StateValue.off + label.target = self + + let color = NSMenuItem(title: "Color", action: #selector(toggleColor), keyEquivalent: "") + color.state = self.color ? NSControl.StateValue.on : NSControl.StateValue.off + color.target = self + + self.menus.append(label) + self.menus.append(color) + } + + override func setValue(data: [Double]) { + let value: Double = data.first! + + self.valueLabel.stringValue = "\(Int(Float(value.roundTo(decimalPlaces: 2))! * 100))%" + self.valueLabel.textColor = value.usageColor(color: self.color) + + if self.points.count < 50 { + self.points.append(value) + return + } + + for (i, _) in self.points.enumerated() { + if i+1 < self.points.count { + self.points[i] = self.points[i+1] + } else { + self.points[i] = value + } + } + } + + func drawValue () { + for subview in self.subviews { + subview.removeFromSuperview() + } + + valueLabel = NSTextField(frame: NSMakeRect(2, widgetSize.height - 11, self.frame.size.width, 10)) + if label { + valueLabel = NSTextField(frame: NSMakeRect(labelPadding + 2, widgetSize.height - 11, self.frame.size.width, 10)) + } + valueLabel.textColor = NSColor.red + valueLabel.isEditable = false + valueLabel.isSelectable = false + valueLabel.isBezeled = false + valueLabel.wantsLayer = true + valueLabel.textColor = .labelColor + valueLabel.backgroundColor = .controlColor + valueLabel.canDrawSubviewsIntoLayer = true + valueLabel.alignment = .natural + valueLabel.font = NSFont.systemFont(ofSize: 8, weight: .ultraLight) + valueLabel.stringValue = "" + valueLabel.addSubview(NSView()) + + self.height = 7.0 + self.addSubview(valueLabel) + } + + @objc override func toggleLabel(_ sender: NSMenuItem) { + sender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.on + self.defaults.set(sender.state == NSControl.StateValue.on, forKey: "\(self.name)_label") + self.label = (sender.state == NSControl.StateValue.on) + + var width = self.size + if self.label { + width = width + labelPadding + } + self.activeModule << false + self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: width, height: self.frame.size.height) + self.activeModule << true + self.drawValue() + } + + @objc func toggleColor(_ sender: NSMenuItem) { + sender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.on + self.defaults.set(sender.state == NSControl.StateValue.on, forKey: "\(name)_color") + self.color = sender.state == NSControl.StateValue.on + } +} diff --git a/Stats/Widgets/Network/NetworkArrows.swift b/Stats/Widgets/Network/NetworkArrows.swift new file mode 100644 index 00000000..5905f8ed --- /dev/null +++ b/Stats/Widgets/Network/NetworkArrows.swift @@ -0,0 +1,99 @@ +// +// NetworkArrows.swift +// Stats +// +// Created by Serhiy Mytrovtsiy on 14.07.2019. +// Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved. +// + +import Cocoa + +class NetworkArrowsView: NSView, Widget { + var menus: [NSMenuItem] = [] + var activeModule: Observable = Observable(false) + var size: CGFloat = 8 + var name: String = "" + var shortName: String = "" + + var color: Observable = Observable(false) + var download: Int64 { + didSet { + self.redraw() + } + } + var upload: Int64 { + didSet { + self.redraw() + } + } + + override init(frame: NSRect) { + self.download = 0 + self.upload = 0 + super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) + self.wantsLayer = true + self.addSubview(NSView()) + } + + required init?(coder decoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func Init() {} + + override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + + let arrowAngle = CGFloat(Double.pi / 5) + let pointerLineLength: CGFloat = 3.5 + let workingHeight: CGFloat = (self.frame.size.height - (widgetSize.margin * 2)) + let height: CGFloat = ((workingHeight - widgetSize.margin) / 2) + + let downloadArrow = NSBezierPath() + let downloadStart = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: height + widgetSize.margin) + let downloadEnd = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: widgetSize.margin) + + downloadArrow.addArrow(start: downloadStart, end: downloadEnd, pointerLineLength: pointerLineLength, arrowAngle: arrowAngle) + + if self.download >= 1_024 { + NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).set() + } else { + NSColor.labelColor.set() + } + downloadArrow.lineWidth = 1 + downloadArrow.stroke() + downloadArrow.close() + + let uploadArrow = NSBezierPath() + let uploadStart = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: height + (widgetSize.margin * 2)) + let uploadEnd = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: (widgetSize.margin * 2) + (height * 2)) + + uploadArrow.addArrow(start: uploadStart, end: uploadEnd, pointerLineLength: pointerLineLength, arrowAngle: arrowAngle) + + if self.upload >= 1_024 { + NSColor.red.set() + } else { + NSColor.labelColor.set() + } + uploadArrow.lineWidth = 1 + uploadArrow.stroke() + uploadArrow.close() + } + + func redraw() { + self.needsDisplay = true + setNeedsDisplay(self.frame) + } + + func setValue(data: [Double]) { + let download: Int64 = Int64(data[0]) + let upload: Int64 = Int64(data[1]) + + if self.download != download { + self.download = download + } + if self.upload != upload { + self.upload = upload + } + } +} diff --git a/Stats/Widgets/Network/NetworkArrowsText.swift b/Stats/Widgets/Network/NetworkArrowsText.swift new file mode 100644 index 00000000..82426c4f --- /dev/null +++ b/Stats/Widgets/Network/NetworkArrowsText.swift @@ -0,0 +1,133 @@ +// +// NetworkArrowsText.swift +// Stats +// +// Created by Serhiy Mytrovtsiy on 14.07.2019. +// Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved. +// + +import Cocoa + +class NetworkArrowsTextView: NSView, Widget { + var menus: [NSMenuItem] = [] + var activeModule: Observable = Observable(false) + var size: CGFloat = widgetSize.width + 24 + var name: String = "" + var shortName: String = "" + + var color: Observable = Observable(false) + var download: Int64 { + didSet { + self.redraw() + } + } + var upload: Int64 { + didSet { + self.redraw() + } + } + + var downloadValue: NSTextField = NSTextField() + var uploadValue: NSTextField = NSTextField() + + override init(frame: NSRect) { + self.download = 0 + self.upload = 0 + super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) + self.wantsLayer = true + self.valueView() + } + + required init?(coder decoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func Init() {} + + override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + + let arrowAngle = CGFloat(Double.pi / 5) + let pointerLineLength: CGFloat = 3.5 + let workingHeight: CGFloat = (self.frame.size.height - (widgetSize.margin * 2)) + let height: CGFloat = ((workingHeight - widgetSize.margin) / 2) + + let downloadArrow = NSBezierPath() + let downloadStart = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: height + widgetSize.margin) + let downloadEnd = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: widgetSize.margin) + + downloadArrow.addArrow(start: downloadStart, end: downloadEnd, pointerLineLength: pointerLineLength, arrowAngle: arrowAngle) + + if self.download >= 1_024 { + NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).set() + } else { + NSColor.labelColor.set() + } + downloadArrow.lineWidth = 1 + downloadArrow.stroke() + downloadArrow.close() + + let uploadArrow = NSBezierPath() + let uploadStart = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: height + (widgetSize.margin * 2)) + let uploadEnd = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: (widgetSize.margin * 2) + (height * 2)) + + uploadArrow.addArrow(start: uploadStart, end: uploadEnd, pointerLineLength: pointerLineLength, arrowAngle: arrowAngle) + + if self.upload >= 1_024 { + NSColor.red.set() + } else { + NSColor.labelColor.set() + } + uploadArrow.lineWidth = 1 + uploadArrow.stroke() + uploadArrow.close() + } + + func redraw() { + self.needsDisplay = true + setNeedsDisplay(self.frame) + } + + func setValue(data: [Double]) { + let download: Int64 = Int64(data[0]) + let upload: Int64 = Int64(data[1]) + + if self.download != download { + self.download = download + downloadValue.stringValue = Units(bytes: self.download).getReadableUnit() + } + if self.upload != upload { + self.upload = upload + uploadValue.stringValue = Units(bytes: self.upload).getReadableUnit() + } + } + + func valueView() { + downloadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, widgetSize.margin, self.frame.size.width - widgetSize.margin, 9)) + downloadValue.isEditable = false + downloadValue.isSelectable = false + downloadValue.isBezeled = false + downloadValue.wantsLayer = true + downloadValue.textColor = .labelColor + downloadValue.backgroundColor = .controlColor + downloadValue.canDrawSubviewsIntoLayer = true + downloadValue.alignment = .right + downloadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) + downloadValue.stringValue = "0 KB/s" + + uploadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, self.frame.size.height - 10, self.frame.size.width - widgetSize.margin, 9)) + uploadValue.isEditable = false + uploadValue.isSelectable = false + uploadValue.isBezeled = false + uploadValue.wantsLayer = true + uploadValue.textColor = .labelColor + uploadValue.backgroundColor = .controlColor + uploadValue.canDrawSubviewsIntoLayer = true + uploadValue.alignment = .right + uploadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) + uploadValue.stringValue = "0 KB/s" + + self.addSubview(downloadValue) + self.addSubview(uploadValue) + } +} diff --git a/Stats/Widgets/Network/NetworkDots.swift b/Stats/Widgets/Network/NetworkDots.swift new file mode 100644 index 00000000..4b8c2652 --- /dev/null +++ b/Stats/Widgets/Network/NetworkDots.swift @@ -0,0 +1,85 @@ +// +// NetworkDots.swift +// Stats +// +// Created by Serhiy Mytrovtsiy on 14.07.2019. +// Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved. +// + +import Cocoa + +class NetworkDotsView: NSView, Widget { + var activeModule: Observable = Observable(false) + var size: CGFloat = 12 + var name: String = "" + var shortName: String = "" + var menus: [NSMenuItem] = [] + + var color: Observable = Observable(false) + var download: Int64 { + didSet { + self.redraw() + } + } + var upload: Int64 { + didSet { + self.redraw() + } + } + + override init(frame: NSRect) { + self.download = 0 + self.upload = 0 + super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) + self.wantsLayer = true + self.addSubview(NSView()) + } + + required init?(coder decoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func Init() {} + + override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + + let workingHeight: CGFloat = (self.frame.size.height - (widgetSize.margin * 2)) + let height: CGFloat = ((workingHeight - widgetSize.margin) / 2) - 1 + + var uploadCircle = NSBezierPath() + uploadCircle = NSBezierPath(ovalIn: CGRect(x: widgetSize.margin, y: height + (widgetSize.margin * 2) + 1, width: height, height: height)) + if self.upload >= 1_024 { + NSColor.red.setFill() + } else { + NSColor.labelColor.setFill() + } + uploadCircle.fill() + + var downloadCircle = NSBezierPath() + downloadCircle = NSBezierPath(ovalIn: CGRect(x: widgetSize.margin, y: widgetSize.margin, width: height, height: height)) + if self.download >= 1_024 { + NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).setFill() + } else { + NSColor.labelColor.setFill() + } + downloadCircle.fill() + } + + func redraw() { + self.needsDisplay = true + setNeedsDisplay(self.frame) + } + + func setValue(data: [Double]) { + let download: Int64 = Int64(data[0]) + let upload: Int64 = Int64(data[1]) + + if self.download != download { + self.download = download + } + if self.upload != upload { + self.upload = upload + } + } +} diff --git a/Stats/Widgets/Network/NetworkDotsText.swift b/Stats/Widgets/Network/NetworkDotsText.swift new file mode 100644 index 00000000..ab57aa7b --- /dev/null +++ b/Stats/Widgets/Network/NetworkDotsText.swift @@ -0,0 +1,119 @@ +// +// NetworkDotsText.swift +// Stats +// +// Created by Serhiy Mytrovtsiy on 14.07.2019. +// Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved. +// + +import Cocoa + +class NetworkDotsTextView: NSView, Widget { + var menus: [NSMenuItem] = [] + var activeModule: Observable = Observable(false) + var size: CGFloat = widgetSize.width + 26 + var name: String = "" + var shortName: String = "" + + var color: Observable = Observable(false) + var download: Int64 { + didSet { + self.redraw() + } + } + var upload: Int64 { + didSet { + self.redraw() + } + } + + var downloadValue: NSTextField = NSTextField() + var uploadValue: NSTextField = NSTextField() + + override init(frame: NSRect) { + self.download = 0 + self.upload = 0 + super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) + self.wantsLayer = true + self.valueView() + } + + required init?(coder decoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func Init() {} + + override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + + let workingHeight: CGFloat = (self.frame.size.height - (widgetSize.margin * 2)) + let height: CGFloat = ((workingHeight - widgetSize.margin) / 2) - 1 + + var uploadCircle = NSBezierPath() + uploadCircle = NSBezierPath(ovalIn: CGRect(x: widgetSize.margin, y: height + (widgetSize.margin * 2) + 1, width: height, height: height)) + if self.upload >= 1_024 { + NSColor.red.setFill() + } else { + NSColor.labelColor.setFill() + } + uploadCircle.fill() + + var downloadCircle = NSBezierPath() + downloadCircle = NSBezierPath(ovalIn: CGRect(x: widgetSize.margin, y: widgetSize.margin, width: height, height: height)) + if self.download >= 1_024 { + NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).setFill() + } else { + NSColor.labelColor.setFill() + } + downloadCircle.fill() + } + + func redraw() { + self.needsDisplay = true + setNeedsDisplay(self.frame) + } + + func setValue(data: [Double]) { + let download: Int64 = Int64(data[0]) + let upload: Int64 = Int64(data[1]) + + if self.download != download { + self.download = download + downloadValue.stringValue = Units(bytes: self.download).getReadableUnit() + } + if self.upload != upload { + self.upload = upload + uploadValue.stringValue = Units(bytes: self.upload).getReadableUnit() + } + } + + func valueView() { + downloadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, widgetSize.margin, self.frame.size.width - widgetSize.margin, 9)) + downloadValue.isEditable = false + downloadValue.isSelectable = false + downloadValue.isBezeled = false + downloadValue.wantsLayer = true + downloadValue.textColor = .labelColor + downloadValue.backgroundColor = .controlColor + downloadValue.canDrawSubviewsIntoLayer = true + downloadValue.alignment = .right + downloadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) + downloadValue.stringValue = "0 KB/s" + + uploadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, self.frame.size.height - 10, self.frame.size.width - widgetSize.margin, 9)) + uploadValue.isEditable = false + uploadValue.isSelectable = false + uploadValue.isBezeled = false + uploadValue.wantsLayer = true + uploadValue.textColor = .labelColor + uploadValue.backgroundColor = .controlColor + uploadValue.canDrawSubviewsIntoLayer = true + uploadValue.alignment = .right + uploadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) + uploadValue.stringValue = "0 KB/s" + + self.addSubview(downloadValue) + self.addSubview(uploadValue) + } +} diff --git a/Stats/Widgets/Network/NetworkText.swift b/Stats/Widgets/Network/NetworkText.swift new file mode 100644 index 00000000..45a37525 --- /dev/null +++ b/Stats/Widgets/Network/NetworkText.swift @@ -0,0 +1,79 @@ +// +// NetworkText.swift +// Stats +// +// Created by Serhiy Mytrovtsiy on 14.07.2019. +// Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved. +// + +import Cocoa + +class NetworkTextView: NSView, Widget { + var menus: [NSMenuItem] = [] + var activeModule: Observable = Observable(false) + var size: CGFloat = widgetSize.width + 20 + var name: String = "" + var shortName: String = "" + + var color: Observable = Observable(false) + var downloadValue: NSTextField = NSTextField() + var uploadValue: NSTextField = NSTextField() + + override init(frame: NSRect) { + super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) + self.wantsLayer = true + self.valueView() + } + + required init?(coder decoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func Init() {} + + override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + } + + func redraw() { + self.needsDisplay = true + setNeedsDisplay(self.frame) + } + + func setValue(data: [Double]) { + let download: Int64 = Int64(data[0]) + let upload: Int64 = Int64(data[1]) + + downloadValue.stringValue = Units(bytes: download).getReadableUnit() + uploadValue.stringValue = Units(bytes: upload).getReadableUnit() + } + + func valueView() { + downloadValue = NSTextField(frame: NSMakeRect(widgetSize.width, widgetSize.margin, self.frame.size.width - widgetSize.margin, 9)) + downloadValue.isEditable = false + downloadValue.isSelectable = false + downloadValue.isBezeled = false + downloadValue.wantsLayer = true + downloadValue.textColor = .labelColor + downloadValue.backgroundColor = .controlColor + downloadValue.canDrawSubviewsIntoLayer = true + downloadValue.alignment = .right + downloadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) + downloadValue.stringValue = "0 KB/s" + + uploadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, self.frame.size.height - 10, self.frame.size.width - widgetSize.margin, 9)) + uploadValue.isEditable = false + uploadValue.isSelectable = false + uploadValue.isBezeled = false + uploadValue.wantsLayer = true + uploadValue.textColor = .labelColor + uploadValue.backgroundColor = .controlColor + uploadValue.canDrawSubviewsIntoLayer = true + uploadValue.alignment = .right + uploadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) + uploadValue.stringValue = "0 KB/s" + + self.addSubview(downloadValue) + self.addSubview(uploadValue) + } +} diff --git a/Stats/Widgets/NetworkView.swift b/Stats/Widgets/NetworkView.swift deleted file mode 100644 index 409a2697..00000000 --- a/Stats/Widgets/NetworkView.swift +++ /dev/null @@ -1,479 +0,0 @@ -// -// NetworkView.swift -// Stats -// -// Created by Serhiy Mytrovtsiy on 24.06.2019. -// Copyright © 2019 Serhiy Mytrovtsiy. All rights reserved. -// - -import Cocoa - -class NetworkDotsView: NSView, Widget { - var activeModule: Observable = Observable(false) - var size: CGFloat = 12 - var name: String = "" - var shortName: String = "" - var menus: [NSMenuItem] = [] - - var color: Observable = Observable(false) - var download: Int64 { - didSet { - self.redraw() - } - } - var upload: Int64 { - didSet { - self.redraw() - } - } - - override init(frame: NSRect) { - self.download = 0 - self.upload = 0 - super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) - self.wantsLayer = true - self.addSubview(NSView()) - } - - required init?(coder decoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - func Init() {} - - override func draw(_ dirtyRect: NSRect) { - super.draw(dirtyRect) - - let workingHeight: CGFloat = (self.frame.size.height - (widgetSize.margin * 2)) - let height: CGFloat = ((workingHeight - widgetSize.margin) / 2) - 1 - - var uploadCircle = NSBezierPath() - uploadCircle = NSBezierPath(ovalIn: CGRect(x: widgetSize.margin, y: height + (widgetSize.margin * 2) + 1, width: height, height: height)) - if self.upload >= 1_024 { - NSColor.red.setFill() - } else { - NSColor.labelColor.setFill() - } - uploadCircle.fill() - - var downloadCircle = NSBezierPath() - downloadCircle = NSBezierPath(ovalIn: CGRect(x: widgetSize.margin, y: widgetSize.margin, width: height, height: height)) - if self.download >= 1_024 { - NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).setFill() - } else { - NSColor.labelColor.setFill() - } - downloadCircle.fill() - } - - func redraw() { - self.needsDisplay = true - setNeedsDisplay(self.frame) - } - - func setValue(data: [Double]) { - let download: Int64 = Int64(data[0]) - let upload: Int64 = Int64(data[1]) - - if self.download != download { - self.download = download - } - if self.upload != upload { - self.upload = upload - } - } -} - -class NetworkTextView: NSView, Widget { - var menus: [NSMenuItem] = [] - var activeModule: Observable = Observable(false) - var size: CGFloat = widgetSize.width + 20 - var name: String = "" - var shortName: String = "" - - var color: Observable = Observable(false) - var downloadValue: NSTextField = NSTextField() - var uploadValue: NSTextField = NSTextField() - - override init(frame: NSRect) { - super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) - self.wantsLayer = true - self.valueView() - } - - required init?(coder decoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - func Init() {} - - override func draw(_ dirtyRect: NSRect) { - super.draw(dirtyRect) - } - - func redraw() { - self.needsDisplay = true - setNeedsDisplay(self.frame) - } - - func setValue(data: [Double]) { - let download: Int64 = Int64(data[0]) - let upload: Int64 = Int64(data[1]) - - downloadValue.stringValue = Units(bytes: download).getReadableUnit() - uploadValue.stringValue = Units(bytes: upload).getReadableUnit() - } - - func valueView() { - downloadValue = NSTextField(frame: NSMakeRect(widgetSize.width, widgetSize.margin, self.frame.size.width - widgetSize.margin, 9)) - downloadValue.isEditable = false - downloadValue.isSelectable = false - downloadValue.isBezeled = false - downloadValue.wantsLayer = true - downloadValue.textColor = .labelColor - downloadValue.backgroundColor = .controlColor - downloadValue.canDrawSubviewsIntoLayer = true - downloadValue.alignment = .right - downloadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) - downloadValue.stringValue = "0 KB/s" - - uploadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, self.frame.size.height - 10, self.frame.size.width - widgetSize.margin, 9)) - uploadValue.isEditable = false - uploadValue.isSelectable = false - uploadValue.isBezeled = false - uploadValue.wantsLayer = true - uploadValue.textColor = .labelColor - uploadValue.backgroundColor = .controlColor - uploadValue.canDrawSubviewsIntoLayer = true - uploadValue.alignment = .right - uploadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) - uploadValue.stringValue = "0 KB/s" - - self.addSubview(downloadValue) - self.addSubview(uploadValue) - } -} - -class NetworkArrowsView: NSView, Widget { - var menus: [NSMenuItem] = [] - var activeModule: Observable = Observable(false) - var size: CGFloat = 8 - var name: String = "" - var shortName: String = "" - - var color: Observable = Observable(false) - var download: Int64 { - didSet { - self.redraw() - } - } - var upload: Int64 { - didSet { - self.redraw() - } - } - - override init(frame: NSRect) { - self.download = 0 - self.upload = 0 - super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) - self.wantsLayer = true - self.addSubview(NSView()) - } - - required init?(coder decoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - func Init() {} - - override func draw(_ dirtyRect: NSRect) { - super.draw(dirtyRect) - - let arrowAngle = CGFloat(Double.pi / 5) - let pointerLineLength: CGFloat = 3.5 - let workingHeight: CGFloat = (self.frame.size.height - (widgetSize.margin * 2)) - let height: CGFloat = ((workingHeight - widgetSize.margin) / 2) - - let downloadArrow = NSBezierPath() - let downloadStart = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: height + widgetSize.margin) - let downloadEnd = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: widgetSize.margin) - - downloadArrow.addArrow(start: downloadStart, end: downloadEnd, pointerLineLength: pointerLineLength, arrowAngle: arrowAngle) - - if self.download >= 1_024 { - NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).set() - } else { - NSColor.labelColor.set() - } - downloadArrow.lineWidth = 1 - downloadArrow.stroke() - downloadArrow.close() - - let uploadArrow = NSBezierPath() - let uploadStart = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: height + (widgetSize.margin * 2)) - let uploadEnd = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: (widgetSize.margin * 2) + (height * 2)) - - uploadArrow.addArrow(start: uploadStart, end: uploadEnd, pointerLineLength: pointerLineLength, arrowAngle: arrowAngle) - - if self.upload >= 1_024 { - NSColor.red.set() - } else { - NSColor.labelColor.set() - } - uploadArrow.lineWidth = 1 - uploadArrow.stroke() - uploadArrow.close() - } - - func redraw() { - self.needsDisplay = true - setNeedsDisplay(self.frame) - } - - func setValue(data: [Double]) { - let download: Int64 = Int64(data[0]) - let upload: Int64 = Int64(data[1]) - - if self.download != download { - self.download = download - } - if self.upload != upload { - self.upload = upload - } - } -} - -class NetworkDotsTextView: NSView, Widget { - var menus: [NSMenuItem] = [] - var activeModule: Observable = Observable(false) - var size: CGFloat = widgetSize.width + 26 - var name: String = "" - var shortName: String = "" - - var color: Observable = Observable(false) - var download: Int64 { - didSet { - self.redraw() - } - } - var upload: Int64 { - didSet { - self.redraw() - } - } - - var downloadValue: NSTextField = NSTextField() - var uploadValue: NSTextField = NSTextField() - - override init(frame: NSRect) { - self.download = 0 - self.upload = 0 - super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) - self.wantsLayer = true - self.valueView() - } - - required init?(coder decoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - func Init() {} - - override func draw(_ dirtyRect: NSRect) { - super.draw(dirtyRect) - - let workingHeight: CGFloat = (self.frame.size.height - (widgetSize.margin * 2)) - let height: CGFloat = ((workingHeight - widgetSize.margin) / 2) - 1 - - var uploadCircle = NSBezierPath() - uploadCircle = NSBezierPath(ovalIn: CGRect(x: widgetSize.margin, y: height + (widgetSize.margin * 2) + 1, width: height, height: height)) - if self.upload >= 1_024 { - NSColor.red.setFill() - } else { - NSColor.labelColor.setFill() - } - uploadCircle.fill() - - var downloadCircle = NSBezierPath() - downloadCircle = NSBezierPath(ovalIn: CGRect(x: widgetSize.margin, y: widgetSize.margin, width: height, height: height)) - if self.download >= 1_024 { - NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).setFill() - } else { - NSColor.labelColor.setFill() - } - downloadCircle.fill() - } - - func redraw() { - self.needsDisplay = true - setNeedsDisplay(self.frame) - } - - func setValue(data: [Double]) { - let download: Int64 = Int64(data[0]) - let upload: Int64 = Int64(data[1]) - - if self.download != download { - self.download = download - downloadValue.stringValue = Units(bytes: self.download).getReadableUnit() - } - if self.upload != upload { - self.upload = upload - uploadValue.stringValue = Units(bytes: self.upload).getReadableUnit() - } - } - - func valueView() { - downloadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, widgetSize.margin, self.frame.size.width - widgetSize.margin, 9)) - downloadValue.isEditable = false - downloadValue.isSelectable = false - downloadValue.isBezeled = false - downloadValue.wantsLayer = true - downloadValue.textColor = .labelColor - downloadValue.backgroundColor = .controlColor - downloadValue.canDrawSubviewsIntoLayer = true - downloadValue.alignment = .right - downloadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) - downloadValue.stringValue = "0 KB/s" - - uploadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, self.frame.size.height - 10, self.frame.size.width - widgetSize.margin, 9)) - uploadValue.isEditable = false - uploadValue.isSelectable = false - uploadValue.isBezeled = false - uploadValue.wantsLayer = true - uploadValue.textColor = .labelColor - uploadValue.backgroundColor = .controlColor - uploadValue.canDrawSubviewsIntoLayer = true - uploadValue.alignment = .right - uploadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) - uploadValue.stringValue = "0 KB/s" - - self.addSubview(downloadValue) - self.addSubview(uploadValue) - } -} - -class NetworkArrowsTextView: NSView, Widget { - var menus: [NSMenuItem] = [] - var activeModule: Observable = Observable(false) - var size: CGFloat = widgetSize.width + 24 - var name: String = "" - var shortName: String = "" - - var color: Observable = Observable(false) - var download: Int64 { - didSet { - self.redraw() - } - } - var upload: Int64 { - didSet { - self.redraw() - } - } - - var downloadValue: NSTextField = NSTextField() - var uploadValue: NSTextField = NSTextField() - - override init(frame: NSRect) { - self.download = 0 - self.upload = 0 - super.init(frame: CGRect(x: 0, y: 0, width: self.size, height: widgetSize.height)) - self.wantsLayer = true - self.valueView() - } - - required init?(coder decoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - func Init() {} - - override func draw(_ dirtyRect: NSRect) { - super.draw(dirtyRect) - - let arrowAngle = CGFloat(Double.pi / 5) - let pointerLineLength: CGFloat = 3.5 - let workingHeight: CGFloat = (self.frame.size.height - (widgetSize.margin * 2)) - let height: CGFloat = ((workingHeight - widgetSize.margin) / 2) - - let downloadArrow = NSBezierPath() - let downloadStart = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: height + widgetSize.margin) - let downloadEnd = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: widgetSize.margin) - - downloadArrow.addArrow(start: downloadStart, end: downloadEnd, pointerLineLength: pointerLineLength, arrowAngle: arrowAngle) - - if self.download >= 1_024 { - NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8).set() - } else { - NSColor.labelColor.set() - } - downloadArrow.lineWidth = 1 - downloadArrow.stroke() - downloadArrow.close() - - let uploadArrow = NSBezierPath() - let uploadStart = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: height + (widgetSize.margin * 2)) - let uploadEnd = CGPoint(x: widgetSize.margin + (pointerLineLength/2), y: (widgetSize.margin * 2) + (height * 2)) - - uploadArrow.addArrow(start: uploadStart, end: uploadEnd, pointerLineLength: pointerLineLength, arrowAngle: arrowAngle) - - if self.upload >= 1_024 { - NSColor.red.set() - } else { - NSColor.labelColor.set() - } - uploadArrow.lineWidth = 1 - uploadArrow.stroke() - uploadArrow.close() - } - - func redraw() { - self.needsDisplay = true - setNeedsDisplay(self.frame) - } - - func setValue(data: [Double]) { - let download: Int64 = Int64(data[0]) - let upload: Int64 = Int64(data[1]) - - if self.download != download { - self.download = download - downloadValue.stringValue = Units(bytes: self.download).getReadableUnit() - } - if self.upload != upload { - self.upload = upload - uploadValue.stringValue = Units(bytes: self.upload).getReadableUnit() - } - } - - func valueView() { - downloadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, widgetSize.margin, self.frame.size.width - widgetSize.margin, 9)) - downloadValue.isEditable = false - downloadValue.isSelectable = false - downloadValue.isBezeled = false - downloadValue.wantsLayer = true - downloadValue.textColor = .labelColor - downloadValue.backgroundColor = .controlColor - downloadValue.canDrawSubviewsIntoLayer = true - downloadValue.alignment = .right - downloadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) - downloadValue.stringValue = "0 KB/s" - - uploadValue = NSTextField(frame: NSMakeRect(widgetSize.margin, self.frame.size.height - 10, self.frame.size.width - widgetSize.margin, 9)) - uploadValue.isEditable = false - uploadValue.isSelectable = false - uploadValue.isBezeled = false - uploadValue.wantsLayer = true - uploadValue.textColor = .labelColor - uploadValue.backgroundColor = .controlColor - uploadValue.canDrawSubviewsIntoLayer = true - uploadValue.alignment = .right - uploadValue.font = NSFont.systemFont(ofSize: 9, weight: .light) - uploadValue.stringValue = "0 KB/s" - - self.addSubview(downloadValue) - self.addSubview(uploadValue) - } -}