From 4f53e2be28baf0eb7ef55369e432c9432dff4b10 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Sat, 13 Apr 2024 13:46:15 +0200 Subject: [PATCH] feat: redesigned Dashboard to new style --- Stats/Views/Dashboard.swift | 454 ++++++++++++++---------------------- 1 file changed, 175 insertions(+), 279 deletions(-) diff --git a/Stats/Views/Dashboard.swift b/Stats/Views/Dashboard.swift index 855ed626..c92309fd 100644 --- a/Stats/Views/Dashboard.swift +++ b/Stats/Views/Dashboard.swift @@ -13,22 +13,182 @@ import Cocoa import Kit class Dashboard: NSStackView { - private var uptimeField: NSTextField? = nil + private var processorValue: String { + guard let cpu = SystemKit.shared.device.info.cpu, cpu.name != nil || cpu.physicalCores != nil || cpu.logicalCores != nil else { + return localizedString("Unknown") + } + + var value = "" + + if let name = cpu.name { + value += name + } + + if cpu.physicalCores != nil || cpu.logicalCores != nil { + if !value.isEmpty { + value += "\n" + } + + var mini = "" + if let cores = cpu.physicalCores { + mini += localizedString("Number of cores", "\(cores)") + } + if let threads = cpu.logicalCores { + if mini != "" { + mini += ", " + } + mini += localizedString("Number of threads", "\(threads)") + } + value += "\(mini)" + } + + if cpu.eCores != nil || cpu.pCores != nil { + if !value.isEmpty { + value += "\n" + } + + var mini = "" + if let eCores = cpu.eCores { + mini += localizedString("Number of e-cores", "\(eCores)") + } + if let pCores = cpu.pCores { + if mini != "" { + mini += "\n" + } + mini += localizedString("Number of p-cores", "\(pCores)") + } + value += "\(mini)" + } + + return value + } + private var memoryValue: String { + guard let dimms = SystemKit.shared.device.info.ram?.dimms else { + return localizedString("Unknown") + } + + let sizeFormatter = ByteCountFormatter() + sizeFormatter.allowedUnits = [.useGB] + sizeFormatter.countStyle = .memory + + var value = "" + for i in 0.. NSView { + private func deviceView() -> NSView { let container: NSGridView = NSGridView() container.rowSpacing = 0 container.yPlacement = .center container.xPlacement = .center let deviceImageView: NSImageView = NSImageView(image: SystemKit.shared.device.model.icon) + deviceImageView.widthAnchor.constraint(equalToConstant: 140).isActive = true + deviceImageView.heightAnchor.constraint(equalToConstant: 140).isActive = true let deviceNameField: NSTextField = TextView() deviceNameField.alignment = .center - deviceNameField.font = NSFont.systemFont(ofSize: 14, weight: .regular) + deviceNameField.font = NSFont.systemFont(ofSize: 17, weight: .semibold) deviceNameField.stringValue = SystemKit.shared.device.model.name deviceNameField.isSelectable = true deviceNameField.toolTip = SystemKit.shared.device.model.id @@ -74,274 +236,8 @@ class Dashboard: NSStackView { return container } - private func specs() -> NSView { - let view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: 0)) - let grid: NSGridView = NSGridView(frame: NSRect(x: 0, y: 0, width: view.frame.width, height: 0)) - grid.rowSpacing = 10 - grid.columnSpacing = 20 - grid.xPlacement = .trailing - grid.rowAlignment = .firstBaseline - grid.translatesAutoresizingMaskIntoConstraints = false - - let separator = NSBox() - separator.boxType = .separator - - grid.addRow(with: self.processor()) - grid.addRow(with: self.ram()) - grid.addRow(with: self.gpu()) - grid.addRow(with: self.disk()) - grid.addRow(with: self.serialNumber()) - - grid.addRow(with: [separator]) - grid.row(at: 5).mergeCells(in: NSRange(location: 0, length: 2)) - grid.row(at: 5).topPadding = 5 - grid.row(at: 5).bottomPadding = 5 - - grid.addRow(with: self.upTime()) - - view.addSubview(grid) - - var height: CGFloat = (CGFloat(grid.numberOfRows)-2) * grid.rowSpacing - for i in 0.. [NSView] { - var value = "" - - if let cpu = SystemKit.shared.device.info.cpu, cpu.name != nil || cpu.physicalCores != nil || cpu.logicalCores != nil { - if let name = cpu.name { - value += name - } - - if cpu.physicalCores != nil || cpu.logicalCores != nil { - if !value.isEmpty { - value += "\n" - } - - var mini = "" - if let cores = cpu.physicalCores { - mini += localizedString("Number of cores", "\(cores)") - } - if let threads = cpu.logicalCores { - if mini != "" { - mini += ", " - } - mini += localizedString("Number of threads", "\(threads)") - } - value += "\(mini)" - } - - if cpu.eCores != nil || cpu.pCores != nil { - if !value.isEmpty { - value += "\n" - } - - var mini = "" - if let eCores = cpu.eCores { - mini += localizedString("Number of e-cores", "\(eCores)") - } - if let pCores = cpu.pCores { - if mini != "" { - mini += "\n" - } - mini += localizedString("Number of p-cores", "\(pCores)") - } - value += "\(mini)" - } - } else { - value = localizedString("Unknown") - } - - return [ - self.titleView("\(localizedString("Processor")):"), - self.valueView(value) - ] - } - - private func ram() -> [NSView] { - let sizeFormatter = ByteCountFormatter() - sizeFormatter.allowedUnits = [.useGB] - sizeFormatter.countStyle = .memory - - var value = "" - if let dimms = SystemKit.shared.device.info.ram?.dimms { - for i in 0.. [NSView] { - var value = "" - if let gpus = SystemKit.shared.device.info.gpu { - for i in 0.. [NSView] { - var text = "\(SystemKit.shared.device.info.disk?.model ?? SystemKit.shared.device.info.disk?.name ?? localizedString("Unknown"))" - - if let size = SystemKit.shared.device.info.disk?.size, size != 0 { - text += " (\(DiskSize(size).getReadableMemory()))" - } - - return [ - self.titleView("\(localizedString("Disk")):"), - self.valueView(text) - ] - } - - private func serialNumber() -> [NSView] { - return [ - self.titleView("\(localizedString("Serial number")):"), - self.valueView("\(SystemKit.shared.device.serialNumber ?? localizedString("Unknown"))") - ] - } - - private func upTime() -> [NSView] { - let form = DateComponentsFormatter() - form.maximumUnitCount = 2 - form.unitsStyle = .full - form.allowedUnits = [.day, .hour, .minute] - - var value = localizedString("Unknown") - if let bootDate = SystemKit.shared.device.bootDate { - if let duration = form.string(from: bootDate, to: Date()) { - value = duration - } - } - - let valueView = self.valueView(value) - self.uptimeField = valueView - - return [ - self.titleView("\(localizedString("Uptime")):"), - valueView - ] - } - - // MARK: - Helpers - - private func titleView(_ value: String) -> NSTextField { - let field: NSTextField = TextView(frame: NSRect(x: 0, y: 0, width: 120, height: 17)) - field.font = NSFont.systemFont(ofSize: 13, weight: .light) - field.textColor = .labelColor - field.stringValue = value - - return field - } - - private func valueView(_ value: String) -> NSTextField { - let field: NSTextField = TextView(frame: NSRect(x: 0, y: 0, width: 0, height: 17)) - field.font = NSFont.systemFont(ofSize: 13, weight: .regular) - field.textColor = .labelColor - field.alignment = .right - field.stringValue = value - field.isSelectable = true - - return field + guard notification.userInfo?["module"] as? String == "Dashboard" else { return } + self.uptimeField?.stringValue = self.uptimeValue } }