Minor code cleanup.

Linted with SwiftLint.
This commit is contained in:
Maxim Ananov 2020-12-13 17:09:32 +03:00
parent 192f36a162
commit 0dbbbeefcd
9 changed files with 107 additions and 124 deletions

View File

@ -6,14 +6,12 @@
// Copyright © 2020 David Wernhart. All rights reserved. // Copyright © 2020 David Wernhart. All rights reserved.
// //
import Cocoa import AppKit
import SwiftUI import SwiftUI
import ServiceManagement
import Foundation
import LaunchAtLogin import LaunchAtLogin
@NSApplicationMain @NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate { final class AppDelegate: NSObject, NSApplicationDelegate {
//var window: NSWindow! //var window: NSWindow!
var statusBarItem: NSStatusItem! var statusBarItem: NSStatusItem!
@ -29,30 +27,25 @@ class AppDelegate: NSObject, NSApplicationDelegate {
popover.behavior = .transient popover.behavior = .transient
popover.contentViewController = NSHostingController(rootView: contentView) popover.contentViewController = NSHostingController(rootView: contentView)
self.popover = popover self.popover = popover
let statusBar = NSStatusBar.system let statusBar = NSStatusBar.system
statusBarItem = statusBar.statusItem( statusBarItem = statusBar.statusItem(withLength: NSStatusItem.squareLength)
withLength: NSStatusItem.squareLength)
statusBarItem.button?.image = NSImage(named: "menubaricon")! statusBarItem.button?.image = NSImage(named: "menubaricon")!
//statusBarItem.button?.title = "🍝" statusBarItem.button?.action = #selector(togglePopover(_:))
if let button = self.statusBarItem.button {
button.action = #selector(togglePopover(_:))
}
Helper.instance.checkHelperVersion() Helper.instance.checkHelperVersion()
LaunchAtLogin.isEnabled = true LaunchAtLogin.isEnabled = true
} }
@objc func togglePopover(_ sender: AnyObject?) { @objc func togglePopover(_ sender: AnyObject?) {
self.popover.contentViewController?.view.window?.becomeKey() popover.contentViewController?.view.window?.becomeKey()
if let button = self.statusBarItem.button { if let button = self.statusBarItem.button {
if self.popover.isShown { if popover.isShown {
self.popover.performClose(sender) popover.performClose(sender)
} else { } else {
self.popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY) popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
} }
} }
} }
@ -60,8 +53,5 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ aNotification: Notification) { func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application // Insert code here to tear down your application
} }
} }

View File

@ -2,60 +2,58 @@
"images" : [ "images" : [
{ {
"idiom" : "mac", "idiom" : "mac",
"size" : "16x16", "scale" : "1x",
"scale" : "1x" "size" : "16x16"
}, },
{ {
"idiom" : "mac", "idiom" : "mac",
"size" : "16x16", "scale" : "2x",
"scale" : "2x" "size" : "16x16"
}, },
{ {
"idiom" : "mac", "idiom" : "mac",
"size" : "32x32", "scale" : "1x",
"scale" : "1x" "size" : "32x32"
}, },
{ {
"idiom" : "mac", "idiom" : "mac",
"size" : "32x32", "scale" : "2x",
"scale" : "2x" "size" : "32x32"
}, },
{ {
"idiom" : "mac", "idiom" : "mac",
"size" : "128x128", "scale" : "1x",
"scale" : "1x" "size" : "128x128"
}, },
{ {
"idiom" : "mac", "idiom" : "mac",
"size" : "128x128", "scale" : "2x",
"scale" : "2x" "size" : "128x128"
}, },
{ {
"size" : "256x256",
"idiom" : "mac", "idiom" : "mac",
"filename" : "spaghetti-2.png", "scale" : "1x",
"scale" : "1x" "size" : "256x256"
}, },
{ {
"size" : "256x256",
"idiom" : "mac", "idiom" : "mac",
"filename" : "spaghetti-1.png", "scale" : "2x",
"scale" : "2x" "size" : "256x256"
}, },
{ {
"size" : "512x512",
"idiom" : "mac",
"filename" : "spaghetti.png", "filename" : "spaghetti.png",
"scale" : "1x" "idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
}, },
{ {
"idiom" : "mac", "idiom" : "mac",
"size" : "512x512", "scale" : "2x",
"scale" : "2x" "size" : "512x512"
} }
], ],
"info" : { "info" : {
"version" : 1, "author" : "xcode",
"author" : "xcode" "version" : 1
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB

View File

@ -6,21 +6,19 @@
// Copyright © 2020 David Wernhart. All rights reserved. // Copyright © 2020 David Wernhart. All rights reserved.
// //
import Cocoa
import SwiftUI
import ServiceManagement
import Foundation import Foundation
import ServiceManagement
protocol HelperDelegate { protocol HelperDelegate {
func OnMaxBatRead(value: UInt8) func OnMaxBatRead(value: UInt8)
} }
class Helper{ final class Helper {
static let instance = Helper() static let instance = Helper()
public var delegate: HelperDelegate? public var delegate: HelperDelegate?
// var receiveMessage = "" { // var receiveMessage = "" {
// didSet { // didSet {
// DispatchQueue.main.async { // DispatchQueue.main.async {
@ -33,15 +31,15 @@ class Helper{
// } // }
// } // }
// } // }
lazy var helperToolConnection: NSXPCConnection = { lazy var helperToolConnection: NSXPCConnection = {
let connection = NSXPCConnection(machServiceName: "com.davidwernhart.Helper.mach", options: .privileged) let connection = NSXPCConnection(machServiceName: "com.davidwernhart.Helper.mach", options: .privileged)
connection.remoteObjectInterface = NSXPCInterface(with: HelperToolProtocol.self) connection.remoteObjectInterface = NSXPCInterface(with: HelperToolProtocol.self)
connection.resume() connection.resume()
return connection return connection
}() }()
@objc func installHelper() { @objc func installHelper() {
print("trying to install helper!") print("trying to install helper!")
var status = noErr var status = noErr
@ -72,57 +70,55 @@ class Helper{
} }
} }
@objc func writeMaxBatteryCharge(setVal: UInt8) {
@objc func writeMaxBatteryCharge(setVal: UInt8){
SMCWriteByte(key: "BCLM", value: setVal) SMCWriteByte(key: "BCLM", value: setVal)
} }
@objc func readMaxBatteryCharge(){ @objc func readMaxBatteryCharge() {
SMCReadByte(key:"BCLM",withReply: { (value) in SMCReadByte(key: "BCLM") { value in
print(String(value)) print(String(value))
self.delegate?.OnMaxBatRead(value: value) self.delegate?.OnMaxBatRead(value: value)
}) }
} }
@objc func checkHelperVersion() { @objc func checkHelperVersion() {
print("checking helper version") print("checking helper version")
if let helper = helperToolConnection.remoteObjectProxyWithErrorHandler({ (error) in let helper = helperToolConnection.remoteObjectProxyWithErrorHandler {
let e = error as NSError let e = $0 as NSError
print("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")") print("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")")
self.installHelper() self.installHelper()
//self.receiveMessage.append("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")") //self.receiveMessage.append("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")")
}) as? HelperToolProtocol { } as? HelperToolProtocol
helper.getVersion(withReply: { (version) in
print("helperVersion:", helperVersion, " version from helper:",version)
if(!helperVersion.elementsEqual(version)){
self.installHelper()
}
//self.receiveMessage.append("Version: \(version)\n")
})
}
}
@objc func SMCReadByte(key:String, withReply reply: @escaping (UInt8) -> Void){ helper?.getVersion { version in
if let helper = helperToolConnection.remoteObjectProxyWithErrorHandler({ (error) in print("helperVersion:", helperVersion, " version from helper:", version)
let e = error as NSError if !helperVersion.elementsEqual(version) {
print("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")") self.installHelper()
//self.receiveMessage.append("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")")
}) as? HelperToolProtocol {
helper.readSMCByte(key: key) { (value) in
reply (value)
} }
//self.receiveMessage.append("Version: \(version)\n")
} }
} }
@objc func SMCWriteByte(key:String,value:UInt8){ @objc func SMCReadByte(key: String, withReply reply: @escaping (UInt8) -> Void) {
if let helper = helperToolConnection.remoteObjectProxyWithErrorHandler({ (error) in let helper = helperToolConnection.remoteObjectProxyWithErrorHandler {
let e = error as NSError let e = $0 as NSError
print("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")") print("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")")
//self.receiveMessage.append("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")") //self.receiveMessage.append("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")")
}) as? HelperToolProtocol { } as? HelperToolProtocol
helper.setSMCByte(key: key,value: value)
helper?.readSMCByte(key: key) {
reply($0)
} }
} }
@objc func SMCWriteByte(key: String, value: UInt8) {
let helper = helperToolConnection.remoteObjectProxyWithErrorHandler {
let e = $0 as NSError
print("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")")
//self.receiveMessage.append("Remote proxy error \(e.code): \(e.localizedDescription) \(e.localizedRecoverySuggestion ?? "---")")
} as? HelperToolProtocol
helper?.setSMCByte(key: key, value: value)
}
} }

View File

@ -13,9 +13,9 @@ let helperVersion: String = "4" //for some reason the integrated version check d
@objc(HelperToolProtocol) protocol HelperToolProtocol { @objc(HelperToolProtocol) protocol HelperToolProtocol {
//protocol HelperToolProtocol { //protocol HelperToolProtocol {
func getVersion(withReply reply: @escaping (String) -> Void) func getVersion(withReply reply: @escaping (String) -> Void)
//TODO: more functions for other data types, altough this is sufficient for battery max charge level //TODO: more functions for other data types, altough this is sufficient for battery max charge level
func setSMCByte(key: String, value: UInt8) func setSMCByte(key: String, value: UInt8)
func readSMCByte(key: String, withReply reply: @escaping (UInt8) -> Void) func readSMCByte(key: String, withReply reply: @escaping (UInt8) -> Void)
} }

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
class HelperTool: NSObject, HelperToolProtocol { final class HelperTool: NSObject, HelperToolProtocol {
func getVersion(withReply reply: (String) -> Void) { func getVersion(withReply reply: (String) -> Void) {
// let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString" as String) as? String ?? "(unknown version)" // let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString" as String) as? String ?? "(unknown version)"
// let build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String ?? "(unknown build)" // let build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String ?? "(unknown build)"

View File

@ -528,35 +528,35 @@ public struct TemperatureSensors {
public static let THUNDERBOLT_1 = TemperatureSensor(name: "THUNDERBOLT_1", public static let THUNDERBOLT_1 = TemperatureSensor(name: "THUNDERBOLT_1",
code: FourCharCode(fromStaticString: "TI1P")) code: FourCharCode(fromStaticString: "TI1P"))
public static let all = [AMBIENT_AIR_0.code : AMBIENT_AIR_0, public static let all = [AMBIENT_AIR_0.code: AMBIENT_AIR_0,
AMBIENT_AIR_1.code : AMBIENT_AIR_1, AMBIENT_AIR_1.code: AMBIENT_AIR_1,
CPU_0_DIE.code : CPU_0_DIE, CPU_0_DIE.code: CPU_0_DIE,
CPU_0_DIODE.code : CPU_0_DIODE, CPU_0_DIODE.code: CPU_0_DIODE,
CPU_0_HEATSINK.code : CPU_0_HEATSINK, CPU_0_HEATSINK.code: CPU_0_HEATSINK,
CPU_0_PROXIMITY.code : CPU_0_PROXIMITY, CPU_0_PROXIMITY.code: CPU_0_PROXIMITY,
ENCLOSURE_BASE_0.code : ENCLOSURE_BASE_0, ENCLOSURE_BASE_0.code: ENCLOSURE_BASE_0,
ENCLOSURE_BASE_1.code : ENCLOSURE_BASE_1, ENCLOSURE_BASE_1.code: ENCLOSURE_BASE_1,
ENCLOSURE_BASE_2.code : ENCLOSURE_BASE_2, ENCLOSURE_BASE_2.code: ENCLOSURE_BASE_2,
ENCLOSURE_BASE_3.code : ENCLOSURE_BASE_3, ENCLOSURE_BASE_3.code: ENCLOSURE_BASE_3,
GPU_0_DIODE.code : GPU_0_DIODE, GPU_0_DIODE.code: GPU_0_DIODE,
GPU_0_HEATSINK.code : GPU_0_HEATSINK, GPU_0_HEATSINK.code: GPU_0_HEATSINK,
GPU_0_PROXIMITY.code : GPU_0_PROXIMITY, GPU_0_PROXIMITY.code: GPU_0_PROXIMITY,
HDD_PROXIMITY.code : HDD_PROXIMITY, HDD_PROXIMITY.code: HDD_PROXIMITY,
HEATSINK_0.code : HEATSINK_0, HEATSINK_0.code: HEATSINK_0,
HEATSINK_1.code : HEATSINK_1, HEATSINK_1.code: HEATSINK_1,
HEATSINK_2.code : HEATSINK_2, HEATSINK_2.code: HEATSINK_2,
MEM_SLOT_0.code : MEM_SLOT_0, MEM_SLOT_0.code: MEM_SLOT_0,
MEM_SLOTS_PROXIMITY.code: MEM_SLOTS_PROXIMITY, MEM_SLOTS_PROXIMITY.code: MEM_SLOTS_PROXIMITY,
PALM_REST.code : PALM_REST, PALM_REST.code: PALM_REST,
LCD_PROXIMITY.code : LCD_PROXIMITY, LCD_PROXIMITY.code: LCD_PROXIMITY,
MISC_PROXIMITY.code : MISC_PROXIMITY, MISC_PROXIMITY.code: MISC_PROXIMITY,
NORTHBRIDGE.code : NORTHBRIDGE, NORTHBRIDGE.code: NORTHBRIDGE,
NORTHBRIDGE_DIODE.code : NORTHBRIDGE_DIODE, NORTHBRIDGE_DIODE.code: NORTHBRIDGE_DIODE,
NORTHBRIDGE_PROXIMITY.code : NORTHBRIDGE_PROXIMITY, NORTHBRIDGE_PROXIMITY.code: NORTHBRIDGE_PROXIMITY,
ODD_PROXIMITY.code : ODD_PROXIMITY, ODD_PROXIMITY.code: ODD_PROXIMITY,
PWR_SUPPLY_PROXIMITY.code : PWR_SUPPLY_PROXIMITY, PWR_SUPPLY_PROXIMITY.code: PWR_SUPPLY_PROXIMITY,
THUNDERBOLT_0.code : THUNDERBOLT_0, THUNDERBOLT_0.code: THUNDERBOLT_0,
THUNDERBOLT_1.code : THUNDERBOLT_1] THUNDERBOLT_1.code: THUNDERBOLT_1]
} }
public struct TemperatureSensor { public struct TemperatureSensor {

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
class HelperDelegate: NSObject, NSXPCListenerDelegate { final class HelperDelegate: NSObject, NSXPCListenerDelegate {
func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool { func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
newConnection.exportedInterface = NSXPCInterface(with: HelperToolProtocol.self) newConnection.exportedInterface = NSXPCInterface(with: HelperToolProtocol.self)
newConnection.exportedObject = HelperTool() newConnection.exportedObject = HelperTool()
@ -22,4 +22,3 @@ let listener = NSXPCListener(machServiceName: "com.davidwernhart.Helper.mach")
listener.delegate = delegate listener.delegate = delegate
listener.resume() listener.resume()
RunLoop.current.run() RunLoop.current.run()