ContentView cleanup

This commit is contained in:
Maxim Ananov 2020-12-13 17:01:29 +03:00
parent 72fcf85524
commit 192f36a162
3 changed files with 136 additions and 220 deletions

Binary file not shown.

View File

@ -1,56 +0,0 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"colors" : [
{
"idiom" : "universal",
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "1.000",
"alpha" : "1.000",
"blue" : "1.000",
"green" : "1.000"
}
}
},
{
"idiom" : "universal",
"appearances" : [
{
"appearance" : "luminosity",
"value" : "light"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "0.800",
"alpha" : "1.000",
"blue" : "0.800",
"green" : "0.800"
}
}
},
{
"idiom" : "universal",
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "0.100",
"alpha" : "1.000",
"blue" : "0.100",
"green" : "0.100"
}
}
}
]
}

View File

@ -6,13 +6,10 @@
// Copyright © 2020 David Wernhart. All rights reserved. // Copyright © 2020 David Wernhart. All rights reserved.
// //
import SwiftUI
import ServiceManagement
import Foundation
import LaunchAtLogin import LaunchAtLogin
import Combine import SwiftUI
struct BlueButtonStyle: ButtonStyle { private struct BlueButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View { func makeBody(configuration: Self.Configuration) -> some View {
configuration.label configuration.label
.foregroundColor(configuration.isPressed ? Color.blue : Color.white) .foregroundColor(configuration.isPressed ? Color.blue : Color.white)
@ -22,7 +19,7 @@ struct BlueButtonStyle: ButtonStyle {
} }
} }
struct RedButtonStyle: ButtonStyle { private struct RedButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View { func makeBody(configuration: Self.Configuration) -> some View {
configuration.label configuration.label
.foregroundColor(configuration.isPressed ? Color.red : Color.white) .foregroundColor(configuration.isPressed ? Color.red : Color.white)
@ -32,224 +29,199 @@ struct RedButtonStyle: ButtonStyle {
} }
} }
var osxScale:Bool = true private var osxScale = true
struct settings<Content: View>: View { private struct Settings: View {
@State private var launchAtLogin = LaunchAtLogin.isEnabled
@State private var osxScaleToggle = osxScale
var content: () -> Content @ObservedObject private var presenter = SMCPresenter.shared
@State var launchOnLogin = LaunchAtLogin.isEnabled
@State var osxScaleToggle = osxScale
@ObservedObject var presenter = SMCPresenter.shared
init(@ViewBuilder _ content: @escaping () -> Content) {
self.content = content
//Helper.instance.delegate = self
}
var body: some View { var body: some View {
VStack(alignment: .center){ VStack {
HStack(alignment: .center) { HStack {
Toggle(isOn: Binding( VStack(alignment: .leading) {
get: { Toggle(isOn: Binding(
self.launchOnLogin get: { launchAtLogin },
},
set: {(newValue) in set: { newValue in
self.launchOnLogin = newValue launchAtLogin = newValue
if(newValue){ print("Launch at login turned \(newValue ? "on" : "off")!")
print("launch on login turned on!") LaunchAtLogin.isEnabled = newValue
LaunchAtLogin.isEnabled = true
} }
else{ )) {
print("launch on login turned off!") Text("Launch at login")
LaunchAtLogin.isEnabled = false }
Toggle(isOn: Binding(
get: { osxScaleToggle },
set: { newValue in
osxScaleToggle = newValue
osxScale = newValue
presenter.setValue(value: Float(presenter.value))
} }
)) {
Text("Use macOS battery scale")
} }
)) {
Text("Launch on Login")
}.padding() }.padding()
Toggle(isOn: Binding(
get: {
self.osxScaleToggle
},
set: {(newValue) in
self.osxScaleToggle = newValue
osxScale = newValue
self.presenter.setValue(value: Float(self.presenter.value))
}
)) {
Text("Use macOS battery scale")
}.padding()
Spacer() Spacer()
Button( action: {
Helper.instance.installHelper() Button(action: {
} Helper.instance.installHelper()
) { }) {
Text("Reinstall Helper") Text("Reinstall Helper")
.frame(maxWidth: 120, maxHeight: 30) .frame(maxWidth: 120, maxHeight: 30)
}.buttonStyle(BlueButtonStyle()) }.buttonStyle(BlueButtonStyle())
} }
HStack {
HStack(alignment: .center) {
Spacer() Spacer()
VStack(alignment: .leading){ VStack(alignment: .leading) {
Text("AlDente 1.2 🍝").font(.subheadline) let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
Button(action:{ Text("AlDente \(version ?? "") 🍝").font(.headline)
let url = URL(string: "https://www.github.com/davidwernhart/AlDente")!
if NSWorkspace.shared.open(url) { let address = "github.com/davidwernhart/AlDente"
print("default browser was successfully opened") Button(action: {
} openURL("https://" + address)
}){ }) {
Text("github.com/davidwernhart/AlDente").foregroundColor(Color.blue) Text(address).foregroundColor(Color(.linkColor))
}.buttonStyle(PlainButtonStyle()) }.buttonStyle(PlainButtonStyle())
Text("Created with 🤍 by David Wernhart in 2020") Text("Created with by David Wernhart in 2020")
// Text("AlDente 🍝").font(.title) // Text("AlDente 🍝").font(.title)
// Text("Keep your battery just right").font(.subheadline) // Text("Keep your battery just right").font(.subheadline)
} }
Spacer() Spacer()
Button(action: { Button(action: {
let url = URL(string: "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6PLR7D9ZCZGGC&source=url")! openURL("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6PLR7D9ZCZGGC&source=url")
if NSWorkspace.shared.open(url) {
print("default browser was successfully opened")
}
}) { }) {
Text("Donate") Text("Donate")
.frame(maxWidth: 60, maxHeight: 50) .frame(maxWidth: 60, maxHeight: 50)
} }
.buttonStyle(BlueButtonStyle()) .buttonStyle(BlueButtonStyle())
} }
}.background(Color("SettingsColor")).cornerRadius(5) }
.background(Color(.unemphasizedSelectedContentBackgroundColor))
.cornerRadius(5)
}
private func openURL(_ string: String) {
let url = URL(string: string)!
if NSWorkspace.shared.open(url) {
print("default browser was successfully opened")
}
} }
} }
struct ContentView: View{ struct ContentView: View {
@State var adaptableHeight = CGFloat(100) @State private var adaptableHeight = CGFloat(100)
@State var showSettings = false @State private var showSettings = false
@ObservedObject var presenter = SMCPresenter.shared @ObservedObject private var presenter = SMCPresenter.shared
init() { init() {
Helper.instance.delegate = presenter Helper.instance.delegate = presenter
Helper.instance.readMaxBatteryCharge() Helper.instance.readMaxBatteryCharge()
} }
var body: some View {
let vstack = VStack{
VStack(alignment: .leading) {
//Text("Value is: \(presenter.value)")
HStack(alignment: .center){
Text(" Max. Battery Charge:").padding(.leading)
TextField("Number", value: Binding(
get: {
Float(self.presenter.value)
},
set: {(newValue) in
if(newValue >= 20 && newValue <= 100){
self.presenter.setValue(value: newValue)
}
}
), formatter: NumberFormatter())
.multilineTextAlignment(.center)
.frame(maxWidth:50)
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer() var body: some View {
Button( action: { VStack(alignment: .leading) {
self.showSettings = !self.showSettings
if(self.showSettings){ HStack {
self.adaptableHeight = 235 Text("Max. Battery Charge:").padding(.leading)
TextField("Number", value: Binding(
get: {
Float(presenter.value)
},
set: { newValue in
if newValue >= 20 && newValue <= 100 {
presenter.setValue(value: newValue)
} }
else{ }
self.adaptableHeight = 100 ), formatter: NumberFormatter())
} .multilineTextAlignment(.center)
}) { .frame(maxWidth: 50)
Text("Settings") .textFieldStyle(RoundedBorderTextFieldStyle())
.frame(maxWidth: 70, maxHeight: 30)
}.buttonStyle(BlueButtonStyle()).padding(.leading,-30)
Button( action: {
NSApplication.shared.terminate(self)
}) {
Text("Exit")
.frame(maxWidth: 50, maxHeight: 30)
}.buttonStyle(RedButtonStyle()).padding(.leading,-30)
}
HStack(alignment: .center){
Slider(value: Binding(
get: {
Float(self.presenter.value)
},
set: {(newValue) in
if(newValue >= 20 && newValue <= 100){
self.presenter.setValue(value: newValue)
}
}
), in: 20...100).padding(.horizontal).padding(.top,-20)
}
Spacer() Spacer()
if(self.showSettings){
//settings{Text("")} Button(action: {
settings({Text("")}) showSettings.toggle()
adaptableHeight = showSettings ? 235 : 100
}) {
Text("Settings")
.frame(maxWidth: 70, maxHeight: 30)
}.buttonStyle(BlueButtonStyle()).padding(.leading, -30)
Button(action: {
NSApplication.shared.terminate(self)
}) {
Text("Quit")
.frame(maxWidth: 50, maxHeight: 30)
}.buttonStyle(RedButtonStyle()).padding(.leading, -30)
}
Slider(value: Binding(
get: {
Float(presenter.value)
},
set: { newValue in
if newValue >= 20 && newValue <= 100 {
presenter.setValue(value: newValue)
}
} }
), in: 20...100).padding(.horizontal).padding(.top, -20)
}.frame(width: 400, height: adaptableHeight)
Spacer()
}
if showSettings {
return vstack Settings()
}
}.frame(width: 400, height: adaptableHeight)
} }
} }
class SMCPresenter: ObservableObject, HelperDelegate{ private final class SMCPresenter: ObservableObject, HelperDelegate {
static let shared = SMCPresenter() static let shared = SMCPresenter()
@Published var value: UInt8 = 0 @Published var value: UInt8 = 0
private var timer: Timer? private var timer: Timer?
func OnMaxBatRead(value: UInt8){ func OnMaxBatRead(value: UInt8) {
DispatchQueue.main.async { DispatchQueue.main.async {
if(osxScale){ if osxScale {
self.value = value + 3 self.value = value + 3
} } else {
else{
self.value = value self.value = value
} }
} }
} }
func setValue(value: Float){ func setValue(value: Float) {
DispatchQueue.main.async { DispatchQueue.main.async {
self.value = UInt8(value) self.value = UInt8(value)
} }
self.timer?.invalidate() timer?.invalidate()
self.timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false, block: { timer in timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
var setval = value var setval = value
if(osxScale){ if osxScale {
setval -= 3 setval -= 3
} }
if(setval >= 20 && setval <= 100){ if setval >= 20 && setval <= 100 {
print("Setting Max Battery To: ",setval) print("Setting Max Battery To: ", setval)
Helper.instance.writeMaxBatteryCharge(setVal: UInt8(setval)) Helper.instance.writeMaxBatteryCharge(setVal: UInt8(setval))
Helper.instance.readMaxBatteryCharge() Helper.instance.readMaxBatteryCharge()
self.timer = nil self.timer = nil
} }
}) }
} }
} }