Skip to content

Commit c55f0e0

Browse files
committed
login helper
1 parent aece378 commit c55f0e0

File tree

8 files changed

+287
-6
lines changed

8 files changed

+287
-6
lines changed

AMD Power Gadget/AppDelegate.swift

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import Cocoa
10+
import ServiceManagement
1011

1112
@NSApplicationMain
1213
class AppDelegate: NSObject, NSApplicationDelegate {
@@ -15,6 +16,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
1516

1617
@IBOutlet weak var appearanceToggle: NSMenuItem!
1718
@IBOutlet weak var statusbarToggle: NSMenuItem!
19+
@IBOutlet weak var startAtLoginToggle: NSMenuItem!
1820

1921

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

59+
@IBAction func startAtLogin(_ sender: Any) {
60+
applyStartAtLogin(enabled: startAtLoginToggle.state == .off)
61+
}
62+
5763
@IBAction func sysmonitor(_ sender: Any) {
5864
SystemMonitorViewController.launch()
5965
}
6066

6167
func applicationDidFinishLaunching(_ aNotification: Notification) {
62-
UserDefaults.standard.register(defaults: ["usetranslucency" : false,
63-
"statusbarenabled": true])
68+
69+
let keyDefaults = [
70+
"usetranslucency" : false,
71+
"statusbarenabled": true,
72+
"startAtLogin": false,
73+
"startAtLoginAsked": false
74+
]
75+
76+
UserDefaults.standard.register(defaults: keyDefaults)
6477

6578
let useTran = UserDefaults.standard.bool(forKey: "usetranslucency")
6679
let sb = UserDefaults.standard.bool(forKey: "statusbarenabled")
80+
let sl = UserDefaults.standard.bool(forKey: "startAtLogin")
81+
82+
if !UserDefaults.standard.bool(forKey: "startAtLoginAsked") {
83+
askStartup()
84+
UserDefaults.standard.set(true, forKey: "startAtLoginAsked")
85+
} else { applyStartAtLogin(enabled: sl) }
6786

6887
applyStatusBarSwitch(enabled: sb)
6988
applyAppearanceSwitch(translucency: useTran)
7089

90+
7191
if !sb {
7292
ViewController.launch()
7393
}
94+
95+
}
96+
97+
func askStartup() {
98+
let alert = NSAlert()
99+
alert.messageText = "Startup at login?"
100+
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."
101+
alert.alertStyle = .critical
102+
alert.addButton(withTitle: "Yes")
103+
alert.addButton(withTitle: "No")
104+
let res = alert.runModal()
105+
106+
if res == .alertFirstButtonReturn {
107+
applyStartAtLogin(enabled: true)
108+
}
109+
110+
if res == .alertSecondButtonReturn {
111+
applyStartAtLogin(enabled: false)
112+
}
74113
}
75114

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

101140
UserDefaults.standard.set(enabled, forKey: "statusbarenabled")
102141
}
142+
143+
func applyStartAtLogin(enabled: Bool) {
144+
startAtLoginToggle.state = enabled ? .on : .off
145+
UserDefaults.standard.set(enabled, forKey: "startAtLogin")
146+
SMLoginItemSetEnabled("wtf.spinach.APGLaunchHelper" as CFString, enabled)
147+
}
103148
}
104149

AMD Power Gadget/Base.lproj/Main.storyboard

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@
110110
<action selector="toggleStatusBar:" target="Voe-Tx-rLC" id="F1t-uf-hDf"/>
111111
</connections>
112112
</menuItem>
113+
<menuItem title="Launch in menu bar at login" id="UJh-jc-2fi">
114+
<modifierMask key="keyEquivalentModifierMask"/>
115+
<connections>
116+
<action selector="startAtLogin:" target="Voe-Tx-rLC" id="73L-hw-2nN"/>
117+
</connections>
118+
</menuItem>
113119
</items>
114120
</menu>
115121
</menuItem>
@@ -159,6 +165,7 @@
159165
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="AMD_Power_Gadget" customModuleProvider="target">
160166
<connections>
161167
<outlet property="appearanceToggle" destination="fV4-Ra-Fu1" id="QrG-wZ-zSC"/>
168+
<outlet property="startAtLoginToggle" destination="UJh-jc-2fi" id="XYm-oE-KaU"/>
162169
<outlet property="statusbarToggle" destination="goc-Nc-vnb" id="xNu-fJ-KhX"/>
163170
</connections>
164171
</customObject>
@@ -1352,7 +1359,7 @@
13521359
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
13531360
<textFieldCell key="cell" allowsUndo="NO" alignment="left" id="AIb-f9-EZB">
13541361
<font key="font" size="12" name="HelveticaNeue"/>
1355-
<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>
1362+
<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>
13561363
<color key="textColor" red="1" green="1" blue="1" alpha="0.54901960780000003" colorSpace="custom" customColorSpace="sRGB"/>
13571364
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
13581365
</textFieldCell>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>com.apple.security.app-sandbox</key>
6+
<true/>
7+
<key>com.apple.security.files.user-selected.read-only</key>
8+
<true/>
9+
</dict>
10+
</plist>

APGLaunchHelper/AppDelegate.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// AppDelegate.swift
3+
// APGLaunchHelper
4+
//
5+
// Created by Qi HaoYan on 7/30/21.
6+
// Copyright © 2021 trulyspinach. All rights reserved.
7+
//
8+
9+
import Cocoa
10+
11+
class AppDelegate: NSObject, NSApplicationDelegate {
12+
13+
func applicationDidFinishLaunching(_ aNotification: Notification) {
14+
let runningApps = NSWorkspace.shared.runningApplications
15+
print("hello world")
16+
let isRunning = runningApps.contains {
17+
$0.bundleIdentifier == "wtf.spinach.AMD-Power-Gadget"
18+
}
19+
20+
if !isRunning {
21+
var path = URL(fileURLWithPath: Bundle.main.bundlePath as String)
22+
23+
for _ in 1...4 {
24+
path = path.deletingLastPathComponent()
25+
}
26+
27+
let config = NSWorkspace.OpenConfiguration()
28+
NSWorkspace.shared.openApplication(at: path, configuration: config) { instance, e in
29+
exit(0)
30+
}
31+
}
32+
33+
}
34+
35+
}
36+

APGLaunchHelper/Info.plist

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIconFile</key>
10+
<string></string>
11+
<key>CFBundleIdentifier</key>
12+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
13+
<key>CFBundleInfoDictionaryVersion</key>
14+
<string>6.0</string>
15+
<key>CFBundleName</key>
16+
<string>$(PRODUCT_NAME)</string>
17+
<key>CFBundlePackageType</key>
18+
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>1.0</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
<key>LSMinimumSystemVersion</key>
24+
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
25+
<key>NSHumanReadableCopyright</key>
26+
<string>Copyright © 2021 trulyspinach. All rights reserved.</string>
27+
<key>NSPrincipalClass</key>
28+
<string>NSApplication</string>
29+
<key>LSBackgroundOnly</key>
30+
<true/>
31+
</dict>
32+
</plist>

APGLaunchHelper/main.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// main.swift
3+
// APGLaunchHelper
4+
//
5+
// Created by Qi HaoYan on 7/30/21.
6+
// Copyright © 2021 trulyspinach. All rights reserved.
7+
//
8+
9+
import Cocoa
10+
11+
let delegate = AppDelegate()
12+
NSApplication.shared.delegate = delegate
13+
NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

0 commit comments

Comments
 (0)