diff --git a/.swiftformat b/.swiftformat new file mode 100644 index 00000000..9de2bffe --- /dev/null +++ b/.swiftformat @@ -0,0 +1,3 @@ +--swiftversion 5 +--exclude Carthage +--nospaceoperators ..<, ... diff --git a/eul.xcodeproj/project.pbxproj b/eul.xcodeproj/project.pbxproj index ebcf86e4..83cb123f 100644 --- a/eul.xcodeproj/project.pbxproj +++ b/eul.xcodeproj/project.pbxproj @@ -131,6 +131,7 @@ 6C7DB6E324E80F9400133B06 /* PreferenceDisplayView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferenceDisplayView.swift; sourceTree = ""; }; 6C7DB6E524E82C5A00133B06 /* Preference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preference.swift; sourceTree = ""; }; 6C7DB6E724E82C7700133B06 /* StringEnum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringEnum.swift; sourceTree = ""; }; + 6C9667D9250E3D6F00E51ABB /* .swiftformat */ = {isa = PBXFileReference; lastKnownFileType = text; path = .swiftformat; sourceTree = ""; }; 6CAEED5324A5DE4700C39597 /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = ""; }; 6CAEED6124A5F51600C39597 /* Text.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Text.swift; sourceTree = ""; }; 6CAEED6424A62DF800C39597 /* SMC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SMC.swift; sourceTree = ""; }; @@ -208,6 +209,7 @@ 6C5F853D249FACE000CF0EF8 /* eul */, 6C5F853C249FACE000CF0EF8 /* Products */, 6C4E880824A649BC00766E15 /* Cartfile */, + 6C9667D9250E3D6F00E51ABB /* .swiftformat */, 6C4E880A24A6545E00766E15 /* Frameworks */, ); sourceTree = ""; @@ -339,6 +341,7 @@ isa = PBXNativeTarget; buildConfigurationList = 6C5F854E249FACE200CF0EF8 /* Build configuration list for PBXNativeTarget "eul" */; buildPhases = ( + 6C9667DA250E3DA200E51ABB /* SwiftFormat */, 6C5F8537249FACE000CF0EF8 /* Sources */, 6C5F8538249FACE000CF0EF8 /* Frameworks */, 6C5F8539249FACE000CF0EF8 /* Resources */, @@ -404,6 +407,24 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ + 6C9667DA250E3DA200E51ABB /* SwiftFormat */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + name = SwiftFormat; + outputFileListPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "if which swiftformat >/dev/null; then\n swiftformat .\nelse\n echo \"warning: SwiftFormat not installed, download from https://github.com/nicklockwood/SwiftFormat\"\nfi\n"; + }; 6CC0798D250CEE96000D7DAC /* ShellScript */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; diff --git a/eul/AppDelegate.swift b/eul/AppDelegate.swift index 9722c6bc..7aef2806 100644 --- a/eul/AppDelegate.swift +++ b/eul/AppDelegate.swift @@ -7,8 +7,8 @@ // import Cocoa -import SwiftUI import Localize_Swift +import SwiftUI @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { @@ -26,12 +26,13 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } - func applicationDidFinishLaunching(_ aNotification: Notification) { + func applicationDidFinishLaunching(_: Notification) { let contentView = ContentView() window = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], - backing: .buffered, defer: false) + backing: .buffered, defer: false + ) window.center() window.setFrameAutosaveName("Eul Preferences") window.contentView = NSHostingView(rootView: contentView) @@ -40,10 +41,10 @@ class AppDelegate: NSObject, NSApplicationDelegate { window.standardWindowButton(.miniaturizeButton)?.isHidden = true window.makeKeyAndOrderFront(nil) SmcControl.shared.start() - self.refreshRepeatedly() + refreshRepeatedly() } - func applicationWillTerminate(_ aNotification: Notification) { + func applicationWillTerminate(_: Notification) { // Insert code here to tear down your application } @@ -56,4 +57,3 @@ class AppDelegate: NSObject, NSApplicationDelegate { NSApplication.shared.terminate(self) } } - diff --git a/eul/Extension/Animation.swift b/eul/Extension/Animation.swift index 03469c51..8872b965 100644 --- a/eul/Extension/Animation.swift +++ b/eul/Extension/Animation.swift @@ -9,7 +9,7 @@ import SwiftUI extension Animation { - static let normal: Animation = Animation.spring(dampingFraction: 1.5).speed(2.5) - static let fast: Animation = Animation.spring(response: 0.3, dampingFraction: 1).speed(2) - static let slow: Animation = Animation.spring(dampingFraction: 1.5).speed(1) + static let normal = Animation.spring(dampingFraction: 1.5).speed(2.5) + static let fast = Animation.spring(response: 0.3, dampingFraction: 1).speed(2) + static let slow = Animation.spring(dampingFraction: 1.5).speed(1) } diff --git a/eul/Extension/Color.swift b/eul/Extension/Color.swift index ca4dc70e..a4e6ff1f 100644 --- a/eul/Extension/Color.swift +++ b/eul/Extension/Color.swift @@ -29,7 +29,7 @@ extension Color { self.init(red: Double(r) / 255, green: Double(g) / 255, blue: Double(b) / 255, opacity: Double(a) / 255) } - static private var isDark: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.darkAqua } + private static var isDark: Bool { NSApp.effectiveAppearance.name == NSAppearance.Name.darkAqua } static let border = Color(NSColor.controlColor) static let controlBackground = Color(NSColor.controlBackgroundColor) static let text = Color(NSColor.textColor) diff --git a/eul/Extension/Text.swift b/eul/Extension/Text.swift index 3c7c4dc0..3b953b05 100644 --- a/eul/Extension/Text.swift +++ b/eul/Extension/Text.swift @@ -10,36 +10,30 @@ import SwiftUI extension Text { func compact() -> some View { - self - .font(.system(size: 8, weight: .regular)) + font(.system(size: 8, weight: .regular)) } func normal() -> some View { - self - .font(.system(size: 12, weight: .regular)) + font(.system(size: 12, weight: .regular)) } func section() -> some View { - self - .font(.headline) + font(.headline) .padding(.top, 8) .padding(.bottom, 4) } func subsection() -> some View { - self - .font(.system(size: 12, weight: .regular)) + font(.system(size: 12, weight: .regular)) .padding(.top, 8) } func inlineSection() -> some View { - self - .font(.system(size: 12, weight: .regular)) + font(.system(size: 12, weight: .regular)) } func menuSection() -> some View { - self - .font(.system(size: 11, weight: .semibold)) + font(.system(size: 11, weight: .semibold)) .padding(.top, 8) .padding(.bottom, 4) .frame(maxWidth: .infinity, alignment: .leading) diff --git a/eul/Extension/View.swift b/eul/Extension/View.swift index 8fdd2192..0e2892e6 100644 --- a/eul/Extension/View.swift +++ b/eul/Extension/View.swift @@ -10,8 +10,7 @@ import SwiftUI extension View { func menuInfo() -> some View { - self - .font(.system(size: 14, weight: .regular)) + font(.system(size: 14, weight: .regular)) .foregroundColor(.info) .padding(.leading, 20) .padding(.trailing, 12) diff --git a/eul/Schema/Preference.swift b/eul/Schema/Preference.swift index 275f42f3..3ce14188 100644 --- a/eul/Schema/Preference.swift +++ b/eul/Schema/Preference.swift @@ -8,7 +8,6 @@ import Foundation - struct Preference { enum TextDisplay: String, StringEnum { case compact diff --git a/eul/Schema/Refreshable.swift b/eul/Schema/Refreshable.swift index daae2404..dbd48cb4 100644 --- a/eul/Schema/Refreshable.swift +++ b/eul/Schema/Refreshable.swift @@ -8,7 +8,7 @@ import Foundation -@objc protocol RefreshableObjC: class { +@objc protocol RefreshableObjC: AnyObject { func refresh() } diff --git a/eul/StatusBar/StatusBarManager.swift b/eul/StatusBar/StatusBarManager.swift index 4b371e15..37165047 100644 --- a/eul/StatusBar/StatusBarManager.swift +++ b/eul/StatusBar/StatusBarManager.swift @@ -6,8 +6,8 @@ // Copyright © 2020 Gao Sun. All rights reserved. // -import SwiftUI import Combine +import SwiftUI class StatusBarManager { @ObservedObject var preferenceStore = PreferenceStore.shared diff --git a/eul/Store/MemoryStore.swift b/eul/Store/MemoryStore.swift index 16714f77..e894499c 100644 --- a/eul/Store/MemoryStore.swift +++ b/eul/Store/MemoryStore.swift @@ -36,6 +36,7 @@ class MemoryStore: ObservableObject, Refreshable { var freeString: String { MemoryStore.memoryUnit(total - used) } + var usedString: String { MemoryStore.memoryUnit(used) } diff --git a/eul/Store/PreferenceStore.swift b/eul/Store/PreferenceStore.swift index 9427eeed..cb9ff2d0 100644 --- a/eul/Store/PreferenceStore.swift +++ b/eul/Store/PreferenceStore.swift @@ -6,8 +6,8 @@ // Copyright © 2020 Gao Sun. All rights reserved. // -import Foundation import Combine +import Foundation import Localize_Swift import SwiftyJSON @@ -38,11 +38,13 @@ class PreferenceStore: ObservableObject { SmcControl.shared.tempUnit = newValue } } + @Published var language = Localize.currentLanguage() { willSet { Localize.setCurrentLanguage(newValue) } } + @Published var textDisplay = Preference.TextDisplay.compact @Published var isActiveComponentToggling = false @Published var activeComponents = EulComponent.allCases @@ -56,7 +58,7 @@ class PreferenceStore: ObservableObject { "language": language, "textDisplay": textDisplay.rawValue, "activeComponents": activeComponents.map { $0.rawValue }, - "availableComponents": availableComponents.map { $0.rawValue } + "availableComponents": availableComponents.map { $0.rawValue }, ]) } @@ -92,7 +94,7 @@ class PreferenceStore: ObservableObject { let url = URL(string: "https://api.github.com/repos/\(repo)/releases/latest") if let url = url { - let task = session.dataTask(with: url) { data, response, error in + let task = session.dataTask(with: url) { data, _, error in DispatchQueue.main.async { if error == nil, @@ -108,8 +110,8 @@ class PreferenceStore: ObservableObject { } task.resume() } else { - self.isUpdateAvailable = false - self.checkUpdateFailed = true + isUpdateAvailable = false + checkUpdateFailed = true } } diff --git a/eul/Utilities/ByteUnit.swift b/eul/Utilities/ByteUnit.swift index 394b6df9..0ca71f94 100644 --- a/eul/Utilities/ByteUnit.swift +++ b/eul/Utilities/ByteUnit.swift @@ -13,15 +13,15 @@ public struct ByteUnit { public let bytes: UInt64 public var kilobytes: Double { - Double(bytes) / 1_024 + Double(bytes) / 1024 } public var megabytes: Double { - kilobytes / 1_024 + kilobytes / 1024 } public var gigabytes: Double { - megabytes / 1_024 + megabytes / 1024 } public init(_ bytes: UInt64) { @@ -34,16 +34,16 @@ public struct ByteUnit { public var readable: String { switch bytes { - case 0..<1_024: - return "\(bytes) bytes" - case 1_024..<(1_024 * 1_024): - return "\(String(format: "%.2f", kilobytes)) kb" - case 1_024..<(1_024 * 1_024 * 1_024): - return "\(String(format: "%.2f", megabytes)) mb" - case (1_024 * 1_024 * 1_024)...UInt64.max: - return "\(String(format: "%.2f", gigabytes)) gb" + case 0..<1024: + return "\(bytes) bytes" + case 1024..<(1024 * 1024): + return "\(String(format: "%.2f", kilobytes)) kb" + case 1024..<(1024 * 1024 * 1024): + return "\(String(format: "%.2f", megabytes)) mb" + case (1024 * 1024 * 1024)...UInt64.max: + return "\(String(format: "%.2f", gigabytes)) gb" default: - return "\(bytes) bytes" + return "\(bytes) bytes" } } } diff --git a/eul/Utilities/Info.swift b/eul/Utilities/Info.swift index a58d3eb9..35cac09c 100644 --- a/eul/Utilities/Info.swift +++ b/eul/Utilities/Info.swift @@ -7,8 +7,8 @@ // import Foundation -import SystemKit import IOKit.ps +import SystemKit struct Info { enum BatteryCondition: String { @@ -37,6 +37,7 @@ struct Info { var currentPercentage: Int { Int(Double(currentCapacity) / Double(maxCapacity) * 100) } + var condition: BatteryCondition = .good var powerSource: PowerSourceState = .unknown var timeToFullCharge = 0 diff --git a/eul/Utilities/SMC.swift b/eul/Utilities/SMC.swift index 3a15a912..126cd761 100644 --- a/eul/Utilities/SMC.swift +++ b/eul/Utilities/SMC.swift @@ -26,12 +26,14 @@ // huge credit to D0miH for new MacBook fan speed compatibility -import IOKit import Foundation +import IOKit + +// ------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ // MARK: Type Aliases -//------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------ // http://stackoverflow.com/a/22383661 @@ -50,12 +52,13 @@ public typealias SMCBytes = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) -//------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ + // MARK: Standard Library Extensions -//------------------------------------------------------------------------------ -extension UInt32 { +// ------------------------------------------------------------------------------ +extension UInt32 { init(fromBytes bytes: (UInt8, UInt8, UInt8, UInt8)) { // TODO: Broken up due to "Expression was too complex" error as of // Swift 4. @@ -70,14 +73,12 @@ extension UInt32 { } extension Bool { - init(fromByte byte: UInt8) { self = byte == 1 ? true : false } } public extension Int { - init(fromFPE2 bytes: FPE2) { self = (Int(bytes.0) << 6) + (Int(bytes.1) >> 2) } @@ -90,30 +91,27 @@ public extension Int { self = Int(resultValue) } - func toFPE2() -> FPE2 { return (UInt8(self >> 6), UInt8((self << 2) ^ ((self >> 6) << 8))) } } extension Double { - init(fromSP78 bytes: SP78) { // FIXME: Handle second byte let sign = bytes.0 & 0x80 == 0 ? 1.0 : -1.0 - self = sign * Double(bytes.0 & 0x7F) // AND to mask sign bit + self = sign * Double(bytes.0 & 0x7F) // AND to mask sign bit } } // Thanks to Airspeed Velocity for the great idea! // http://airspeedvelocity.net/2015/05/22/my-talk-at-swift-summit/ public extension FourCharCode { - init(fromString str: String) { precondition(str.count == 4) self = str.utf8.reduce(0) { sum, character in - return sum << 8 | UInt32(character) + sum << 8 | UInt32(character) } } @@ -134,16 +132,18 @@ public extension FourCharCode { } func toString() -> String { - return String(describing: UnicodeScalar(self >> 24 & 0xff)!) + - String(describing: UnicodeScalar(self >> 16 & 0xff)!) + - String(describing: UnicodeScalar(self >> 8 & 0xff)!) + - String(describing: UnicodeScalar(self & 0xff)!) + return String(describing: UnicodeScalar(self >> 24 & 0xFF)!) + + String(describing: UnicodeScalar(self >> 16 & 0xFF)!) + + String(describing: UnicodeScalar(self >> 8 & 0xFF)!) + + String(describing: UnicodeScalar(self & 0xFF)!) } } -//------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ + // MARK: Defined by AppleSMC.kext -//------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------ /// Defined by AppleSMC.kext /// @@ -162,20 +162,19 @@ public extension FourCharCode { /// /// http://www.opensource.apple.com/source/PowerManagement/PowerManagement-211/ public struct SMCParamStruct { - /// I/O Kit function selector public enum Selector: UInt8 { - case kSMCHandleYPCEvent = 2 - case kSMCReadKey = 5 - case kSMCWriteKey = 6 + case kSMCHandleYPCEvent = 2 + case kSMCReadKey = 5 + case kSMCWriteKey = 6 case kSMCGetKeyFromIndex = 8 - case kSMCGetKeyInfo = 9 + case kSMCGetKeyInfo = 9 } /// Return codes for SMCParamStruct.result property public enum Result: UInt8 { - case kSMCSuccess = 0 - case kSMCError = 1 + case kSMCSuccess = 0 + case kSMCError = 1 case kSMCKeyNotFound = 132 } @@ -237,13 +236,14 @@ public struct SMCParamStruct { UInt8(0), UInt8(0)) } -//------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ + // MARK: SMC Client -//------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------ /// SMC data type information public struct DataTypes { - /// Fan information struct public static let FDS = DataType(type: FourCharCode(fromStaticString: "{fds"), size: 16) public static let Flag = DataType(type: FourCharCode(fromStaticString: "flag"), size: 1) @@ -266,7 +266,7 @@ public struct DataType: Equatable { let size: UInt32 } -public func ==(lhs: DataType, rhs: DataType) -> Bool { +public func == (lhs: DataType, rhs: DataType) -> Bool { return lhs.type == rhs.type && lhs.size == rhs.size } @@ -274,9 +274,7 @@ public func ==(lhs: DataType, rhs: DataType) -> Bool { /// Macs. Works by talking to the AppleSMC.kext (kernel extension), the closed /// source driver for the SMC. public struct SMCKit { - public enum SMCError: Error { - /// AppleSMC driver not found case driverNotFound @@ -339,7 +337,8 @@ public struct SMCKit { /// Get information about the key at index public static func keyInformationAtIndex(_ index: Int) throws -> - FourCharCode { + FourCharCode + { var inputStruct = SMCParamStruct() inputStruct.data8 = SMCParamStruct.Selector.kSMCGetKeyFromIndex.rawValue @@ -377,8 +376,9 @@ public struct SMCKit { /// Make an actual call to the SMC driver public static func callDriver(_ inputStruct: inout SMCParamStruct, - selector: SMCParamStruct.Selector = .kSMCHandleYPCEvent) - throws -> SMCParamStruct { + selector: SMCParamStruct.Selector = .kSMCHandleYPCEvent) + throws -> SMCParamStruct + { assert(MemoryLayout.stride == 80, "SMCParamStruct size is != 80") var outputStruct = SMCParamStruct() @@ -401,23 +401,24 @@ public struct SMCKit { throw SMCError.notPrivileged default: throw SMCError.unknown(kIOReturn: result, - SMCResult: outputStruct.result) + SMCResult: outputStruct.result) } } } -//------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ + // MARK: General -//------------------------------------------------------------------------------ -extension SMCKit { +// ------------------------------------------------------------------------------ +extension SMCKit { /// Get all valid SMC keys for this machine public static func allKeys() throws -> [SMCKey] { let count = try keyCount() var keys = [SMCKey]() - for i in 0 ..< count { + for i in 0.. - [TemperatureSensor] { + [TemperatureSensor] + { var sensors = [TemperatureSensor]() for sensor in TemperatureSensors.all.values { @@ -601,14 +603,16 @@ extension SMCKit { let keys = try allKeys() return keys.filter { $0.code.toString().hasPrefix("T") && - $0.info == DataTypes.SP78 && - TemperatureSensors.all[$0.code] == nil } - .map { TemperatureSensor(name: "Unknown", code: $0.code) } + $0.info == DataTypes.SP78 && + TemperatureSensors.all[$0.code] == nil + } + .map { TemperatureSensor(name: "Unknown", code: $0.code) } } /// Get current temperature of a sensor public static func temperature(_ sensorCode: FourCharCode, - unit: TemperatureUnit = .celius) throws -> Double { + unit: TemperatureUnit = .celius) throws -> Double + { let data = try readData(SMCKey(code: sensorCode, info: DataTypes.SP78)) let temperatureInCelius = Double(fromSP78: (data.0, data.1)) @@ -624,9 +628,11 @@ extension SMCKit { } } -//------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ + // MARK: Fan -//------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------ public struct Fan { // TODO: Should we start the fan id from 1 instead of 0? @@ -637,12 +643,11 @@ public struct Fan { } extension SMCKit { - public static func allFans() throws -> [Fan] { let count = try fanCount() var fans = [Fan]() - for i in 0 ..< count { + for i in 0.. Int { let key = SMCKey(code: FourCharCode(fromStaticString: "FNum"), - info: DataTypes.UInt8) + info: DataTypes.UInt8) let data = try readData(key) return Int(data.0) @@ -668,20 +673,20 @@ extension SMCKit { public static func fanName(_ id: Int) throws -> String { let key = SMCKey(code: FourCharCode(fromString: "F\(id)ID"), - info: DataTypes.FDS) + info: DataTypes.FDS) let data = try readData(key) // The last 12 bytes of '{fds' data type, a custom struct defined by the // AppleSMC.kext that is 16 bytes, contains the fan name - let c1 = String(UnicodeScalar(data.4)) - let c2 = String(UnicodeScalar(data.5)) - let c3 = String(UnicodeScalar(data.6)) - let c4 = String(UnicodeScalar(data.7)) - let c5 = String(UnicodeScalar(data.8)) - let c6 = String(UnicodeScalar(data.9)) - let c7 = String(UnicodeScalar(data.10)) - let c8 = String(UnicodeScalar(data.11)) - let c9 = String(UnicodeScalar(data.12)) + let c1 = String(UnicodeScalar(data.4)) + let c2 = String(UnicodeScalar(data.5)) + let c3 = String(UnicodeScalar(data.6)) + let c4 = String(UnicodeScalar(data.7)) + let c5 = String(UnicodeScalar(data.8)) + let c6 = String(UnicodeScalar(data.9)) + let c7 = String(UnicodeScalar(data.10)) + let c8 = String(UnicodeScalar(data.11)) + let c9 = String(UnicodeScalar(data.12)) let c10 = String(UnicodeScalar(data.13)) let c11 = String(UnicodeScalar(data.14)) let c12 = String(UnicodeScalar(data.15)) @@ -749,9 +754,11 @@ extension SMCKit { } } -//------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ + // MARK: Miscellaneous -//------------------------------------------------------------------------------ + +// ------------------------------------------------------------------------------ public struct batteryInfo { public let batteryCount: Int @@ -762,7 +769,6 @@ public struct batteryInfo { } extension SMCKit { - public static func isOpticalDiskDriveFull() throws -> Bool { // TODO: Should we catch key not found? That just means the machine // doesn't have an ODD. Returning false though is not fully correct. @@ -776,14 +782,14 @@ extension SMCKit { public static func batteryInformation() throws -> batteryInfo { let batteryCountKey = - SMCKey(code: FourCharCode(fromStaticString: "BNum"), - info: DataTypes.UInt8) + SMCKey(code: FourCharCode(fromStaticString: "BNum"), + info: DataTypes.UInt8) let batteryPoweredKey = - SMCKey(code: FourCharCode(fromStaticString: "BATP"), - info: DataTypes.Flag) + SMCKey(code: FourCharCode(fromStaticString: "BATP"), + info: DataTypes.Flag) let batteryInfoKey = - SMCKey(code: FourCharCode(fromStaticString: "BSIn"), - info: DataTypes.UInt8) + SMCKey(code: FourCharCode(fromStaticString: "BSIn"), + info: DataTypes.UInt8) let batteryCountData = try readData(batteryCountKey) let batteryCount = Int(batteryCountData.0) diff --git a/eul/Utilities/Shell.swift b/eul/Utilities/Shell.swift index 2bb73ea8..019c6be9 100644 --- a/eul/Utilities/Shell.swift +++ b/eul/Utilities/Shell.swift @@ -26,7 +26,7 @@ func shell(_ args: String...) -> String? { task.waitUntilExit() - if (task.terminationStatus != 0) { + if task.terminationStatus != 0 { return nil } diff --git a/eul/Utilities/SmcControl.swift b/eul/Utilities/SmcControl.swift index 9d8aeaed..d1fb9ab6 100644 --- a/eul/Utilities/SmcControl.swift +++ b/eul/Utilities/SmcControl.swift @@ -83,9 +83,11 @@ class SmcControl: Refreshable { var cpuProximityTemperature: Double? { sensors.first(where: { $0.sensor.name == "CPU_0_PROXIMITY" })?.temp } + var gpuProximityTemperature: Double? { sensors.first(where: { $0.sensor.name == "GPU_0_PROXIMITY" })?.temp } + var memoryProximityTemperature: Double? { sensors.first(where: { $0.sensor.name == "MEM_SLOTS_PROXIMITY" })?.temp } @@ -107,7 +109,7 @@ class SmcControl: Refreshable { maxSpeed: try SMCKit.fanMaxSpeed($0) ) ) } - } catch let error { + } catch { print("SMC init error", error) } SMCKit.close() @@ -117,7 +119,7 @@ class SmcControl: Refreshable { @objc func refresh() { do { try SMCKit.open() - } catch let error { + } catch { SMCKit.close() print("error while opening SMC", error) return @@ -125,7 +127,7 @@ class SmcControl: Refreshable { for sensor in sensors { do { sensor.temp = try SMCKit.temperature(sensor.sensor.code, unit: tempUnit) - } catch let error { + } catch { sensor.temp = 0 print("error while getting temperature", error) } @@ -133,7 +135,7 @@ class SmcControl: Refreshable { for fan in fans { do { fan.speed = try SMCKit.fanCurrentSpeed(fan.fan.id) - } catch let error { + } catch { fan.speed = 0 print("error while getting fan speed", error) } diff --git a/eul/Utilities/String.swift b/eul/Utilities/String.swift index 9cbdd1af..896a821f 100644 --- a/eul/Utilities/String.swift +++ b/eul/Utilities/String.swift @@ -20,11 +20,10 @@ extension String { } func titleCase() -> String { - self - .replacingOccurrences(of: "([A-Z])", - with: " $1", - options: .regularExpression, - range: range(of: self)) + replacingOccurrences(of: "([A-Z])", + with: " $1", + options: .regularExpression, + range: range(of: self)) .trimmingCharacters(in: .whitespacesAndNewlines) .capitalized } diff --git a/eul/Views/Components/ActivityIndicatorView.swift b/eul/Views/Components/ActivityIndicatorView.swift index 0ec43ec5..2f198470 100644 --- a/eul/Views/Components/ActivityIndicatorView.swift +++ b/eul/Views/Components/ActivityIndicatorView.swift @@ -10,13 +10,13 @@ import SwiftUI struct ActivityIndicatorView: NSViewRepresentable { typealias NSViewType = NSProgressIndicator - var configuration = { (view: NSViewType) in } + var configuration = { (_: NSViewType) in } - func makeNSView(context: NSViewRepresentableContext) -> NSProgressIndicator { + func makeNSView(context _: NSViewRepresentableContext) -> NSProgressIndicator { NSViewType() } - func updateNSView(_ nsView: NSProgressIndicator, context: NSViewRepresentableContext) { + func updateNSView(_ nsView: NSProgressIndicator, context _: NSViewRepresentableContext) { configuration(nsView) } } diff --git a/eul/Views/Components/SectionView.swift b/eul/Views/Components/SectionView.swift index e0f8f7c7..3dd68ad7 100644 --- a/eul/Views/Components/SectionView.swift +++ b/eul/Views/Components/SectionView.swift @@ -20,4 +20,3 @@ struct SectionView: View { } } } - diff --git a/eul/Views/Menu/BatteryMenuView.swift b/eul/Views/Menu/BatteryMenuView.swift index 0b7bc26d..55e60091 100644 --- a/eul/Views/Menu/BatteryMenuView.swift +++ b/eul/Views/Menu/BatteryMenuView.swift @@ -80,4 +80,3 @@ struct BatteryMenuView: SizeChangeView { .background(GeometryReader { self.reportSize($0) }) } } - diff --git a/eul/Views/Preference/PreferenceComponentsView.swift b/eul/Views/Preference/PreferenceComponentsView.swift index fbfc640a..b79d55ff 100644 --- a/eul/Views/Preference/PreferenceComponentsView.swift +++ b/eul/Views/Preference/PreferenceComponentsView.swift @@ -35,7 +35,7 @@ extension Preference { .foregroundColor(Color.gray) } HStack { - ForEach(Array(preference.activeComponents.enumerated()), id: \.element) { (offset, element) in + ForEach(Array(preference.activeComponents.enumerated()), id: \.element) { offset, element in HStack(spacing: 8) { Image(element.rawValue) .resizable() @@ -75,7 +75,7 @@ extension Preference { .zIndex(self.dragging == element ? 1 : 0) .contentShape(Rectangle()) .gesture(DragGesture() - .updating(self.$offsetWidth, body: { (value, state, _) in + .updating(self.$offsetWidth, body: { value, state, _ in state = value.translation.width let currentFrame = self.frames[offset] @@ -100,7 +100,7 @@ extension Preference { } } }) - .onChanged { value in + .onChanged { _ in self.dragging = element } .onEnded { _ in @@ -127,7 +127,7 @@ extension Preference { .foregroundColor(Color.gray) } HStack { - ForEach(Array(preference.availableComponents.enumerated()), id: \.element) { (offset, element) in + ForEach(Array(preference.availableComponents.enumerated()), id: \.element) { offset, element in HStack(spacing: 8) { Image(element.rawValue) .resizable() diff --git a/eul/Views/Preference/PreferenceDisplayView.swift b/eul/Views/Preference/PreferenceDisplayView.swift index 87b55713..e9eaeb5b 100644 --- a/eul/Views/Preference/PreferenceDisplayView.swift +++ b/eul/Views/Preference/PreferenceDisplayView.swift @@ -6,8 +6,8 @@ // Copyright © 2020 Gao Sun. All rights reserved. // -import SwiftUI import Localize_Swift +import SwiftUI extension Preference { struct DisplayView: View { diff --git a/eul/Views/Preference/PreferenceGeneralView.swift b/eul/Views/Preference/PreferenceGeneralView.swift index eee176ec..21177bd2 100644 --- a/eul/Views/Preference/PreferenceGeneralView.swift +++ b/eul/Views/Preference/PreferenceGeneralView.swift @@ -6,8 +6,8 @@ // Copyright © 2020 Gao Sun. All rights reserved. // -import SwiftUI import LaunchAtLogin +import SwiftUI import SwiftyJSON extension Preference { diff --git a/eul/Views/StatusBar/CpuView.swift b/eul/Views/StatusBar/CpuView.swift index 3203f9aa..f6e9f8b7 100644 --- a/eul/Views/StatusBar/CpuView.swift +++ b/eul/Views/StatusBar/CpuView.swift @@ -13,7 +13,7 @@ struct CpuView: SizeChangeView { @ObservedObject var cpuStore = CpuStore.shared var texts: [String] { - [cpuStore.usage, cpuStore.temp.map({ SmcControl.shared.formatTemp($0) })].compactMap { $0 } + [cpuStore.usage, cpuStore.temp.map { SmcControl.shared.formatTemp($0) }].compactMap { $0 } } var body: some View { diff --git a/eul/Views/StatusBar/NetworkView.swift b/eul/Views/StatusBar/NetworkView.swift index 9f3300f0..fddb5f09 100644 --- a/eul/Views/StatusBar/NetworkView.swift +++ b/eul/Views/StatusBar/NetworkView.swift @@ -27,4 +27,3 @@ struct NetworkView: SizeChangeView { .background(GeometryReader { self.reportSize($0) }) } } -