Skip to content

Commit

Permalink
login helper
Browse files Browse the repository at this point in the history
  • Loading branch information
trulyspinach committed Jul 30, 2021
1 parent aece378 commit c55f0e0
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 6 deletions.
49 changes: 47 additions & 2 deletions AMD Power Gadget/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Cocoa
import ServiceManagement

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
Expand All @@ -15,6 +16,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var appearanceToggle: NSMenuItem!
@IBOutlet weak var statusbarToggle: NSMenuItem!
@IBOutlet weak var startAtLoginToggle: NSMenuItem!


@IBAction func openPage(_ sender: Any) {
Expand Down Expand Up @@ -54,23 +56,60 @@ class AppDelegate: NSObject, NSApplicationDelegate {
applyStatusBarSwitch(enabled: statusbarToggle.state == .off)
}

@IBAction func startAtLogin(_ sender: Any) {
applyStartAtLogin(enabled: startAtLoginToggle.state == .off)
}

@IBAction func sysmonitor(_ sender: Any) {
SystemMonitorViewController.launch()
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
UserDefaults.standard.register(defaults: ["usetranslucency" : false,
"statusbarenabled": true])

let keyDefaults = [
"usetranslucency" : false,
"statusbarenabled": true,
"startAtLogin": false,
"startAtLoginAsked": false
]

UserDefaults.standard.register(defaults: keyDefaults)

let useTran = UserDefaults.standard.bool(forKey: "usetranslucency")
let sb = UserDefaults.standard.bool(forKey: "statusbarenabled")
let sl = UserDefaults.standard.bool(forKey: "startAtLogin")

if !UserDefaults.standard.bool(forKey: "startAtLoginAsked") {
askStartup()
UserDefaults.standard.set(true, forKey: "startAtLoginAsked")
} else { applyStartAtLogin(enabled: sl) }

applyStatusBarSwitch(enabled: sb)
applyAppearanceSwitch(translucency: useTran)


if !sb {
ViewController.launch()
}

}

func askStartup() {
let alert = NSAlert()
alert.messageText = "Startup at login?"
alert.informativeText = "Do you want AMD Power Gadget to start in menu bar at login? \n\n This will only be asked once. You can change this setting later under Appearance menu."
alert.alertStyle = .critical
alert.addButton(withTitle: "Yes")
alert.addButton(withTitle: "No")
let res = alert.runModal()

if res == .alertFirstButtonReturn {
applyStartAtLogin(enabled: true)
}

if res == .alertSecondButtonReturn {
applyStartAtLogin(enabled: false)
}
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down Expand Up @@ -100,5 +139,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {

UserDefaults.standard.set(enabled, forKey: "statusbarenabled")
}

func applyStartAtLogin(enabled: Bool) {
startAtLoginToggle.state = enabled ? .on : .off
UserDefaults.standard.set(enabled, forKey: "startAtLogin")
SMLoginItemSetEnabled("wtf.spinach.APGLaunchHelper" as CFString, enabled)
}
}

9 changes: 8 additions & 1 deletion AMD Power Gadget/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
<action selector="toggleStatusBar:" target="Voe-Tx-rLC" id="F1t-uf-hDf"/>
</connections>
</menuItem>
<menuItem title="Launch in menu bar at login" id="UJh-jc-2fi">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="startAtLogin:" target="Voe-Tx-rLC" id="73L-hw-2nN"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
Expand Down Expand Up @@ -159,6 +165,7 @@
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="AMD_Power_Gadget" customModuleProvider="target">
<connections>
<outlet property="appearanceToggle" destination="fV4-Ra-Fu1" id="QrG-wZ-zSC"/>
<outlet property="startAtLoginToggle" destination="UJh-jc-2fi" id="XYm-oE-KaU"/>
<outlet property="statusbarToggle" destination="goc-Nc-vnb" id="xNu-fJ-KhX"/>
</connections>
</customObject>
Expand Down Expand Up @@ -1352,7 +1359,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" allowsUndo="NO" alignment="left" id="AIb-f9-EZB">
<font key="font" size="12" name="HelveticaNeue"/>
<mutableString key="title">When enabled, your processor speed will increase automatically on demand, and decrease after a short period of low activity. Lower amount of energy will be consumed when processor running at a lower speed; AMDRyzenCPUPowerManagement will always optimze how your processor idle to save energy.
This option is enabled by default since release version 0.6.</mutableString>
<string key="title">When enabled, your processor speed will increase automatically on demand, and decrease after a short period of low activity. Lower amount of energy will be consumed when processor running at a lower speed; AMDRyzenCPUPowerManagement will always optimze how your processor idle to save energy.
This option is enabled by default since release version 0.6.</string>
<color key="textColor" red="1" green="1" blue="1" alpha="0.54901960780000003" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
Expand Down
10 changes: 10 additions & 0 deletions APGLaunchHelper/APGLaunchHelper.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
36 changes: 36 additions & 0 deletions APGLaunchHelper/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// AppDelegate.swift
// APGLaunchHelper
//
// Created by Qi HaoYan on 7/30/21.
// Copyright © 2021 trulyspinach. All rights reserved.
//

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
let runningApps = NSWorkspace.shared.runningApplications
print("hello world")
let isRunning = runningApps.contains {
$0.bundleIdentifier == "wtf.spinach.AMD-Power-Gadget"
}

if !isRunning {
var path = URL(fileURLWithPath: Bundle.main.bundlePath as String)

for _ in 1...4 {
path = path.deletingLastPathComponent()
}

let config = NSWorkspace.OpenConfiguration()
NSWorkspace.shared.openApplication(at: path, configuration: config) { instance, e in
exit(0)
}
}

}

}

32 changes: 32 additions & 0 deletions APGLaunchHelper/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2021 trulyspinach. All rights reserved.</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>LSBackgroundOnly</key>
<true/>
</dict>
</plist>
13 changes: 13 additions & 0 deletions APGLaunchHelper/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// main.swift
// APGLaunchHelper
//
// Created by Qi HaoYan on 7/30/21.
// Copyright © 2021 trulyspinach. All rights reserved.
//

import Cocoa

let delegate = AppDelegate()
NSApplication.shared.delegate = delegate
NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
Loading

0 comments on commit c55f0e0

Please sign in to comment.