mirror of
https://github.com/yJason/ClashX-Dashboard.git
synced 2026-03-01 00:35:19 +08:00
refactor: ProgressButton
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// ProgressButton.swift
|
||||
// ClashX Dashboard
|
||||
//
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import AppKit
|
||||
|
||||
//class ProgressButtonSize: NSObject {
|
||||
// static func width(_ titles: [String]) -> CGFloat {
|
||||
//
|
||||
// let str = titles.max() ?? ""
|
||||
// let w = str.size(withAttributes: [.font: Font.system(size: 13)]).width
|
||||
//
|
||||
// return w + 12 + 8*3
|
||||
// }
|
||||
//}
|
||||
|
||||
struct ProgressButton: View {
|
||||
|
||||
@State var title: String
|
||||
@State var title2: String
|
||||
@State var iconName: String
|
||||
@Binding var inProgress: Bool
|
||||
|
||||
@State var autoWidth = true
|
||||
|
||||
@State var action: () -> Void
|
||||
|
||||
var body: some View {
|
||||
Button() {
|
||||
action()
|
||||
} label: {
|
||||
HStack {
|
||||
VStack {
|
||||
if inProgress {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
} else {
|
||||
Image(systemName: iconName)
|
||||
}
|
||||
}
|
||||
.frame(width: 12)
|
||||
|
||||
Spacer()
|
||||
|
||||
Text(inProgress ? title2 : title)
|
||||
.font(.system(size: 13))
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.animation(.default, value: inProgress)
|
||||
.foregroundColor(inProgress ? .gray : .blue)
|
||||
}
|
||||
.disabled(inProgress)
|
||||
.frame(width: autoWidth ? ProgressButton.width([title, title2]) : nil)
|
||||
}
|
||||
|
||||
static func width(_ titles: [String]) -> CGFloat {
|
||||
let str = titles.max {
|
||||
$0.count < $1.count
|
||||
} ?? ""
|
||||
let w = str.size(withAttributes: [.font: NSFont.systemFont(ofSize: 13)]).width
|
||||
return w + 12 + 45
|
||||
}
|
||||
}
|
||||
|
||||
//struct ProgressButton_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// ProgressButton()
|
||||
// }
|
||||
//}
|
||||
@@ -76,48 +76,33 @@ struct ProviderProxiesView: View {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
var buttonsView: some View {
|
||||
VStack {
|
||||
Button() {
|
||||
startHealthCheck()
|
||||
} label: {
|
||||
HStack {
|
||||
if isTesting {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
.frame(width: 12)
|
||||
} else {
|
||||
Image(systemName: "bolt.fill")
|
||||
.frame(width: 12)
|
||||
}
|
||||
Text(isTesting ? "Testing" : "Health Check")
|
||||
.frame(width: 90)
|
||||
ProgressButton(
|
||||
title: "Health Check",
|
||||
title2: "Testing",
|
||||
iconName: "bolt.fill",
|
||||
inProgress: $isTesting,
|
||||
autoWidth: false) {
|
||||
startHealthCheck()
|
||||
}
|
||||
.foregroundColor(isTesting ? .gray : .blue)
|
||||
}
|
||||
.disabled(isTesting)
|
||||
|
||||
Button() {
|
||||
startUpdate()
|
||||
} label: {
|
||||
HStack {
|
||||
if isUpdating {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
.frame(width: 12)
|
||||
} else {
|
||||
Image(systemName: "arrow.clockwise")
|
||||
.frame(width: 12)
|
||||
}
|
||||
Text(isUpdating ? "Updating" : "Update")
|
||||
.frame(width: 90)
|
||||
ProgressButton(
|
||||
title: "Update",
|
||||
title2: "Updating",
|
||||
iconName: "arrow.clockwise",
|
||||
inProgress: $isUpdating,
|
||||
autoWidth: false) {
|
||||
startUpdate()
|
||||
}
|
||||
.foregroundColor(isUpdating ? .gray : .blue)
|
||||
}
|
||||
.disabled(isTesting)
|
||||
}
|
||||
.frame(width: ProgressButton.width(
|
||||
[
|
||||
"Health Check",
|
||||
"Testing",
|
||||
"Update",
|
||||
"Updating"]
|
||||
))
|
||||
}
|
||||
|
||||
func startHealthCheck() {
|
||||
|
||||
@@ -40,24 +40,12 @@ struct ProxyProvidersRowView: View {
|
||||
listView
|
||||
}
|
||||
} header: {
|
||||
Button {
|
||||
updateAll()
|
||||
} label: {
|
||||
HStack {
|
||||
if isUpdating {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
.frame(width: 12)
|
||||
} else {
|
||||
Image(systemName: "arrow.clockwise")
|
||||
.frame(width: 12)
|
||||
}
|
||||
Text(isUpdating ? "Updating" : "Update All")
|
||||
.frame(width: 80)
|
||||
ProgressButton(
|
||||
title: "Update All",
|
||||
title2: "Updating",
|
||||
iconName: "arrow.clockwise", inProgress: $isUpdating) {
|
||||
updateAll()
|
||||
}
|
||||
.foregroundColor(isUpdating ? .gray : .blue)
|
||||
}
|
||||
.disabled(isUpdating)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
@@ -42,24 +42,13 @@ struct RuleProvidersRowView: View {
|
||||
}
|
||||
}
|
||||
} header: {
|
||||
Button {
|
||||
updateAll()
|
||||
} label: {
|
||||
HStack {
|
||||
if isUpdating {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
.frame(width: 12)
|
||||
} else {
|
||||
Image(systemName: "arrow.clockwise")
|
||||
.frame(width: 12)
|
||||
}
|
||||
Text(isUpdating ? "Updating" : "Update All")
|
||||
.frame(width: 80)
|
||||
ProgressButton(
|
||||
title: "Update All",
|
||||
title2: "Updating",
|
||||
iconName: "arrow.clockwise",
|
||||
inProgress: $isUpdating) {
|
||||
updateAll()
|
||||
}
|
||||
.foregroundColor(isUpdating ? .gray : .blue)
|
||||
}
|
||||
.disabled(isUpdating)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
@@ -82,24 +82,13 @@ struct ProxyGroupView: View {
|
||||
|
||||
Spacer()
|
||||
|
||||
Button() {
|
||||
startBenchmark()
|
||||
} label: {
|
||||
HStack {
|
||||
if isTesting {
|
||||
ProgressView()
|
||||
.controlSize(.small)
|
||||
.frame(width: 12)
|
||||
} else {
|
||||
Image(systemName: "bolt.fill")
|
||||
.frame(width: 12)
|
||||
}
|
||||
Text(isTesting ? "Testing" : (proxyGroup.type == .urltest ? "Retest" : "Benchmark"))
|
||||
.frame(width: 70)
|
||||
ProgressButton(
|
||||
title: proxyGroup.type == .urltest ? "Retest" : "Benchmark",
|
||||
title2: "Testing",
|
||||
iconName: "bolt.fill",
|
||||
inProgress: $isTesting) {
|
||||
startBenchmark()
|
||||
}
|
||||
.foregroundColor(isTesting ? .gray : .blue)
|
||||
}
|
||||
.disabled(isTesting)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user