From b843ff4cbf5cbbf25f98eb79a384dfcf8ded45b7 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Fri, 13 Nov 2020 13:57:33 +0100 Subject: [PATCH] - add total upload/download stats to the Network widget (#153) --- ModuleKit/Widgets/Mini.swift | 6 ++--- Modules/Net/main.swift | 3 +++ Modules/Net/popup.swift | 27 ++++++++++++++----- Modules/Net/readers.swift | 3 +++ .../en.lproj/Localizable.strings | 3 +++ .../pl.lproj/Localizable.strings | 3 +++ .../ru.lproj/Localizable.strings | 5 +++- .../uk.lproj/Localizable.strings | 5 +++- 8 files changed, 44 insertions(+), 11 deletions(-) diff --git a/ModuleKit/Widgets/Mini.swift b/ModuleKit/Widgets/Mini.swift index f5196ea9..fa651df9 100644 --- a/ModuleKit/Widgets/Mini.swift +++ b/ModuleKit/Widgets/Mini.swift @@ -156,15 +156,15 @@ public class Mini: Widget, CALayerDelegate { let layer = CALayer() layer.frame = CGRect(x: 0, y: 0, width: self.width, height: self.frame.height) - let label = CAText(fontSize: 8, weight: .regular) - label.frame = CGRect(x: origin.x, y: 12, width: self.width - (Constants.Widget.margin*2), height: 8) + let label = CAText(fontSize: 7, weight: .regular) + label.frame = CGRect(x: origin.x, y: 12, width: self.width - (Constants.Widget.margin*2), height: 7) label.string = self.title label.alignmentMode = .left label.foregroundColor = NSColor.labelColor.cgColor label.isHidden = !self.labelState let value = CAText(fontSize: valueSize) - value.frame = CGRect(x: origin.x, y: origin.y-2, width: self.width - (Constants.Widget.margin*2), height: valueSize+1) + value.frame = CGRect(x: origin.x, y: origin.y-1, width: self.width - (Constants.Widget.margin*2), height: valueSize+1) value.font = NSFont.systemFont(ofSize: valueSize, weight: .medium) value.string = "\(Int(self.value.rounded(toPlaces: 2) * 100))%" value.fontSize = valueSize diff --git a/Modules/Net/main.swift b/Modules/Net/main.swift index 8b68952e..5f032165 100644 --- a/Modules/Net/main.swift +++ b/Modules/Net/main.swift @@ -31,6 +31,9 @@ public struct Network_Usage: value_t { var download: Int64 = 0 var upload: Int64 = 0 + var totalDownload: Int64 = 0 + var totalUpload: Int64 = 0 + var laddr: String? = nil // local ip var raddr: String? = nil // remote ip diff --git a/Modules/Net/popup.swift b/Modules/Net/popup.swift index 02331619..34a3a304 100644 --- a/Modules/Net/popup.swift +++ b/Modules/Net/popup.swift @@ -21,7 +21,7 @@ internal class Popup: NSView, Popup_p { private let dashboardHeight: CGFloat = 90 private let chartHeight: CGFloat = 90 + Constants.Popup.separatorHeight - private let detailsHeight: CGFloat = 110 + Constants.Popup.separatorHeight + private let detailsHeight: CGFloat = (22*7) + Constants.Popup.separatorHeight private let processHeight: CGFloat = 22 private var dashboardView: NSView? = nil @@ -43,6 +43,8 @@ internal class Popup: NSView, Popup_p { private var interfaceField: ValueField? = nil private var ssidField: ValueField? = nil private var macAdressField: ValueField? = nil + private var totalUploadField: ValueField? = nil + private var totalDownloadField: ValueField? = nil private var initialized: Bool = false private var processesInitialized: Bool = false @@ -172,6 +174,9 @@ internal class Popup: NSView, Popup_p { let separator = SeparatorView(LocalizedString("Details"), origin: NSPoint(x: 0, y: self.detailsHeight-Constants.Popup.separatorHeight), width: self.frame.width) let container: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: separator.frame.origin.y)) + self.totalUploadField = PopupWithColorRow(container, color: NSColor.systemRed, n: 6, title: "\(LocalizedString("Total upload")):", value: "") + self.totalDownloadField = PopupWithColorRow(container, color: NSColor.systemBlue, n: 5, title: "\(LocalizedString("Total download")):", value: "") + self.publicIPField = PopupRow(container, n: 4, title: "\(LocalizedString("Public IP")):", value: "") self.localIPField = PopupRow(container, n: 3, title: "\(LocalizedString("Local IP")):", value: "") self.interfaceField = PopupRow(container, n: 2, title: "\(LocalizedString("Interface")):", value: "") @@ -180,17 +185,14 @@ internal class Popup: NSView, Popup_p { self.publicIPField?.addTracking() self.localIPField?.addTracking() - self.ssidField?.addTracking() self.macAdressField?.addTracking() self.publicIPField?.toolTip = LocalizedString("Click to copy public IP address") self.localIPField?.toolTip = LocalizedString("Click to copy local IP address") - self.ssidField?.toolTip = LocalizedString("Click to copy wifi name") self.macAdressField?.toolTip = LocalizedString("Click to copy mac address") self.publicIPField?.isSelectable = true self.localIPField?.isSelectable = true - self.ssidField?.isSelectable = true self.macAdressField?.isSelectable = true view.addSubview(separator) @@ -224,7 +226,12 @@ internal class Popup: NSView, Popup_p { let unitWidth = "KB/s".widthOfString(usingFont: .systemFont(ofSize: 13, weight: .light)) + 5 let topPartWidth = valueWidth + unitWidth - let topView: NSView = NSView(frame: NSRect(x: (view.frame.width-topPartWidth)/2, y: (view.frame.height - topHeight - titleHeight)/2 + titleHeight, width: topPartWidth, height: topHeight)) + let topView: NSView = NSView(frame: NSRect( + x: (view.frame.width-topPartWidth)/2, + y: (view.frame.height - topHeight - titleHeight)/2 + titleHeight, + width: topPartWidth, + height: topHeight + )) let valueField = LabelField(frame: NSRect(x: 0, y: 0, width: valueWidth, height: 30), "0") valueField.font = NSFont.systemFont(ofSize: 26, weight: .light) @@ -239,7 +246,12 @@ internal class Popup: NSView, Popup_p { let titleWidth: CGFloat = title.widthOfString(usingFont: NSFont.systemFont(ofSize: 12, weight: .regular))+8 let iconSize: CGFloat = 12 let bottomWidth: CGFloat = titleWidth+iconSize - let bottomView: NSView = NSView(frame: NSRect(x: (view.frame.width-bottomWidth)/2, y: topView.frame.origin.y - titleHeight, width: bottomWidth, height: titleHeight)) + let bottomView: NSView = NSView(frame: NSRect( + x: (view.frame.width-bottomWidth)/2, + y: topView.frame.origin.y - titleHeight, + width: bottomWidth, + height: titleHeight + )) let colorBlock: ColorView = ColorView(frame: NSRect(x: 0, y: 1, width: iconSize, height: iconSize), color: color, radius: 4) let titleField = LabelField(frame: NSRect(x: iconSize, y: 0, width: titleWidth, height: titleHeight), title) @@ -298,6 +310,9 @@ internal class Popup: NSView, Popup_p { self.downloadValue = value.download self.setUploadDownloadFields() + self.totalUploadField?.stringValue = Units(bytes: value.totalUpload).getReadableMemory() + self.totalDownloadField?.stringValue = Units(bytes: value.totalDownload).getReadableMemory() + if let interface = value.interface { self.interfaceField?.stringValue = "\(interface.displayName) (\(interface.BSDName))" self.macAdressField?.stringValue = interface.address diff --git a/Modules/Net/readers.swift b/Modules/Net/readers.swift index 5a64ac92..69b66824 100644 --- a/Modules/Net/readers.swift +++ b/Modules/Net/readers.swift @@ -105,6 +105,9 @@ internal class UsageReader: Reader { self.usage.download = 0 } + self.usage.totalUpload += self.usage.upload + self.usage.totalDownload += self.usage.download + self.callback(self.usage) self.usage.upload = upload diff --git a/Stats/Supporting Files/en.lproj/Localizable.strings b/Stats/Supporting Files/en.lproj/Localizable.strings index 95ee009f..fe2d0b7f 100644 --- a/Stats/Supporting Files/en.lproj/Localizable.strings +++ b/Stats/Supporting Files/en.lproj/Localizable.strings @@ -30,6 +30,7 @@ "Characters" = "Character"; "Short" = "Short"; "Long" = "Long"; +"Statistics" = "Statistics"; // Alerts "New version available" = "New version available"; @@ -133,6 +134,8 @@ "Click to copy mac address" = "Click to copy mac address"; "No connection" = "No connection"; "Network interface" = "Network interface"; +"Total download" = "Total download"; +"Total upload" = "Total upload"; // Battery "Level" = "Level"; diff --git a/Stats/Supporting Files/pl.lproj/Localizable.strings b/Stats/Supporting Files/pl.lproj/Localizable.strings index efbff9e4..8e677130 100644 --- a/Stats/Supporting Files/pl.lproj/Localizable.strings +++ b/Stats/Supporting Files/pl.lproj/Localizable.strings @@ -30,6 +30,7 @@ "Characters" = "Litery"; "Short" = "Krótki"; "Long" = "Długi"; +"Statistics" = "Statystyki"; // Alerts "New version available" = "Nowa wersja dostępna"; @@ -133,6 +134,8 @@ "Click to copy mac address" = "Kliknij aby skopiować adres fizyczny"; "No connection" = "Brak połączenia"; "Network interface" = "Interfejs sieciowy"; +"Total download" = "Całkowicie pobrano"; +"Total upload" = "Całkowicie przesłano"; // Battery "Level" = "Poziom naładowania"; diff --git a/Stats/Supporting Files/ru.lproj/Localizable.strings b/Stats/Supporting Files/ru.lproj/Localizable.strings index a204ff4f..bb9c9614 100644 --- a/Stats/Supporting Files/ru.lproj/Localizable.strings +++ b/Stats/Supporting Files/ru.lproj/Localizable.strings @@ -30,6 +30,7 @@ "Characters" = "Символы"; "Short" = "Короткий"; "Long" = "Длинный"; +"Statistics" = "Статистика"; // Alerts "New version available" = "Доступна новая версия"; @@ -120,7 +121,7 @@ "Fahrenheit" = "Фаренгейта"; // Network -"Uploading" = "Висилання"; +"Uploading" = "Выгрузка"; "Downloading" = "Загрузка"; "Public IP" = "Публичный IP"; "Local IP" = "Локальный IP"; @@ -133,6 +134,8 @@ "Click to copy mac address" = "Нажмите, чтобы скопировать физический адрес"; "No connection" = "Нет соединения"; "Network interface" = "Сетевой интерфейс"; +"Total download" = "Всего скачано"; +"Total upload" = "Всего выгружено"; // Battery "Level" = "Уровень заряда"; diff --git a/Stats/Supporting Files/uk.lproj/Localizable.strings b/Stats/Supporting Files/uk.lproj/Localizable.strings index 3b414b5a..5888895d 100644 --- a/Stats/Supporting Files/uk.lproj/Localizable.strings +++ b/Stats/Supporting Files/uk.lproj/Localizable.strings @@ -30,6 +30,7 @@ "Characters" = "Літери"; "Short" = "Короткий"; "Long" = "Довгий"; +"Statistics" = "Статистика"; // Alerts "New version available" = "Доступна нова версія"; @@ -120,7 +121,7 @@ "Fahrenheit" = "Фаренгейта"; // Network -"Uploading" = "Вислання"; +"Uploading" = "Висилання"; "Downloading" = "Завантаження"; "Public IP" = "Публічний IP"; "Local IP" = "Локальний IP"; @@ -133,6 +134,8 @@ "Click to copy mac address" = "Натисніть, щоб скопіювати фізичний адрес"; "No connection" = "Немає з'єднання"; "Network interface" = "Мережевий інтерфейс"; +"Total download" = "Загально завантажено"; +"Total upload" = "Загально вислано"; // Battery "Level" = "Рівень заряду";