Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
adjusting how things render
Browse files Browse the repository at this point in the history
  • Loading branch information
andylin2004 committed Jan 24, 2024
1 parent 83d050a commit 6c578e2
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 8 deletions.
81 changes: 81 additions & 0 deletions Acknowledgements.plist
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ SOFTWARE.
<key>license</key>
<string>MIT License
Copyright (c) 2022 Steffan Andrews - https://github.com/orchetect
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
</string>
<key>title</key>
<string>MacControlCenterUI</string>
</dict>
<dict>
<key>license</key>
<string>MIT License
Copyright (c) 2021 Josh Kaplan (trilemma.dev)
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -102,6 +129,33 @@ SOFTWARE.
<key>license</key>
<string>MIT License
Copyright (c) 2023 Steffan Andrews - https://github.com/orchetect
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
</string>
<key>title</key>
<string>MenuBarExtraAccess</string>
</dict>
<dict>
<key>license</key>
<string>MIT License
Copyright (c) 2021 Josh Kaplan (trilemma.dev)
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -153,5 +207,32 @@ Software.)
<key>title</key>
<string>SwiftClient</string>
</dict>
<dict>
<key>license</key>
<string>MIT License
Copyright (c) 2023 Steffan Andrews - https://github.com/orchetect
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
</string>
<key>title</key>
<string>SettingsAccess</string>
</dict>
</array>
</plist>
102 changes: 100 additions & 2 deletions Low-Power-Mode-Toggler/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import SecureXPC
struct ContentView: View {
@State var lowPowerModeEnabled = false
@State var shortcutInstalled = false
@State var statusItem: NSStatusItem!

var body: some View {
VStack{
VStack(spacing: 0) {
HStack{
Text("Low Power Mode")
.bold()
Expand All @@ -22,9 +23,69 @@ struct ContentView: View {
.toggleStyle(.switch)
.disabled(!lowPowerModeSupported() || !shortcutInstalled)
}
.padding(.horizontal, 9)
.padding(.vertical, 5)
.padding(.bottom, 2.5)
Divider()
.padding(.horizontal, 9)
.padding(.bottom, 2.5)
if #available(macOS 14, *) {
SettingsLink {
Text("Settings")
.padding(.horizontal, 10)
.padding(.vertical, 3)
}
.buttonStyle(ControlCenterButtonStyle(statusItem: statusItem))
} else {
Button {
Task {
try await Task.sleep(nanoseconds:100000000)
NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
NSApp.windows.last?.level = NSWindow.Level.normal + 1
NSApp.windows.last?.center()
NSApp.windows.last?.orderFrontRegardless()
}
} label: {
Text("Settings")
.padding(.horizontal, 10)
.padding(.vertical, 3)
}
.buttonStyle(ControlCenterButtonStyle(statusItem: statusItem))}
Button {
Task {
try await Task.sleep(nanoseconds:100000000)
for window in NSApp.windows {
if window.title == "About" {
window.close()
}
}
let aboutWindow = NSWindow(contentViewController: NSHostingController(rootView: AboutThisAppView()))
aboutWindow.title = "About"
aboutWindow.titleVisibility = .hidden
aboutWindow.standardWindowButton(.miniaturizeButton)?.isEnabled = false
aboutWindow.standardWindowButton(.zoomButton)?.isEnabled = false
aboutWindow.center()
aboutWindow.makeKeyAndOrderFront(nil)
}
} label: {
Text("About Low Power Mode Toggler")
.padding(.horizontal, 10)
.padding(.vertical, 3)
}
.buttonStyle(ControlCenterButtonStyle(statusItem: statusItem))
Button {
Task {
try await Task.sleep(nanoseconds:100000000)
NSApplication.shared.terminate(self)
}
} label: {
Text("Quit")
.padding(.horizontal, 10)
.padding(.vertical, 3)
}
.buttonStyle(ControlCenterButtonStyle(statusItem: statusItem))
}
.padding(.horizontal, 15)
.padding(5)
.onChange(of: lowPowerModeEnabled){ isLowPowerEnabled in
toggleShortcut(enable: isLowPowerEnabled)
}
Expand All @@ -37,6 +98,43 @@ struct ContentView: View {
}
}

struct ControlCenterButtonStyle: ButtonStyle {
@State var hovering = false
@State var clickAnim = false
@State var flashIndicatorOn = true
@State var task: Task<(), Error>?
@State var statusItem: NSStatusItem!
@Environment(\.colorScheme) var scheme

func makeBody(configuration: Configuration) -> some View {
HStack {
configuration.label
.foregroundStyle(scheme == .light ? .black : .white)
Spacer()
}
.background(.quaternary.opacity(hovering && ((clickAnim && flashIndicatorOn) || !clickAnim) ? 1 : 0), in: RoundedRectangle(cornerRadius: 5))
.onChange(of: configuration.isPressed) { pressed in
if pressed {
task?.cancel()
task = Task(priority: .userInitiated) {
clickAnim = true
flashIndicatorOn = false
try await Task.sleep(nanoseconds: 100000000)
flashIndicatorOn = true
clickAnim = false

DispatchQueue.main.async {
statusItem.menu?.cancelTracking()
}
}
}
}
.onHover { hover in
hovering = hover
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
Expand Down
17 changes: 11 additions & 6 deletions Low-Power-Mode-Toggler/Low_Power_Mode_TogglerApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Low_Power_Mode_TogglerApp: App {
}

class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
private var statusItem: NSStatusItem!
var statusItem: NSStatusItem!
var isLowPowerEnabled = ProcessInfo.processInfo.isLowPowerModeEnabled
var batteryPercentage = 0.0
let menu = NSMenu()
Expand Down Expand Up @@ -76,11 +76,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
}
})

let view = NSHostingView(rootView: ContentView())

view.frame = NSRect(x: 0, y: 0, width: 250, height: 40)
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if #available(macOS 13.0, *) {
let view = NSHostingView(rootView: ContentView(statusItem: statusItem))

view.frame = NSRect(x: 0, y: 0, width: 250, height: 105)
menuItem.view = view
} else {
menuItem.state = isLowPowerEnabled ? .on : .off
Expand All @@ -104,9 +105,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
quitButton.title = "Quit"
quitButton.action = #selector(quitApp(_:))
menu.addItem(menuItem)
menu.addItem(settingsButton)
menu.addItem(aboutButton)
menu.addItem(quitButton)

if #unavailable(macOS 13){
menu.addItem(settingsButton)
menu.addItem(aboutButton)
menu.addItem(quitButton)
}

statusItem.menu = menu
statusItem.menu?.autoenablesItems = false

Expand Down

0 comments on commit 6c578e2

Please sign in to comment.