Files
Bitcoin-Monitoring/test1/AboutWindowView.swift
2025-10-31 06:06:15 +08:00

289 lines
8.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// AboutWindowView.swift
// test1
//
// Created by Mark on 2025/10/31.
//
import SwiftUI
/**
*
* 使 SwiftUI NSAlert
*/
struct AboutWindowView: View {
//
let onClose: () -> Void
//
let currentRefreshInterval: String
//
let appVersion: String
var body: some View {
VStack(spacing: 20) {
//
VStack(spacing: 16) {
//
Image(systemName: "bitcoinsign.circle.fill")
.font(.system(size: 80))
.foregroundColor(.orange)
//
VStack(spacing: 4) {
Text("Bitcoin Monitoring")
.font(.title)
.fontWeight(.bold)
Text("版本 \(appVersion)")
.font(.subheadline)
.foregroundColor(.secondary)
}
}
Divider()
//
VStack(alignment: .leading, spacing: 12) {
Text("功能特性")
.font(.headline)
.fontWeight(.semibold)
VStack(alignment: .leading, spacing: 8) {
FeatureRow(icon: "chart.line.uptrend.xyaxis", title: "实时价格监控", description: "支持 BTC/ETH/BNB/SOL/DOGE")
FeatureRow(icon: "timer", title: "可配置刷新间隔", description: "当前:\(currentRefreshInterval)")
FeatureRow(icon: "exclamationmark.triangle.fill", title: "智能重试机制", description: "网络错误自动恢复")
}
}
Divider()
// 使
VStack(alignment: .leading, spacing: 8) {
Text("使用技巧")
.font(.headline)
.fontWeight(.semibold)
VStack(alignment: .leading, spacing: 6) {
TipRow(text: "• 点击币种名称切换菜单栏显示")
TipRow(text: "• Option + 点击币种名称复制价格")
}
}
Spacer()
.frame(height: 10) //
//
HStack(spacing: 12) {
// GitHub
Button(action: openGitHub) {
HStack {
Image(systemName: "star.circle")
Text("GitHub")
}
}
.buttonStyle(.bordered)
Spacer()
//
Button(action: onClose) {
Text("确定")
.frame(minWidth: 80)
}
.buttonStyle(.borderedProminent)
.keyboardShortcut(.defaultAction)
}
}
.padding(24)
.frame(width: 420, height: 540) //
}
/**
* GitHub
*/
private func openGitHub() {
let githubURL = "https://github.com/jiayouzl/Bitcoin-Monitoring"
// URL
guard let url = URL(string: githubURL) else {
print("❌ 无效的URL: \(githubURL)")
return
}
// 使URL
NSWorkspace.shared.open(url)
print("✅ 已在浏览器中打开GitHub页面: \(githubURL)")
}
}
/**
*
*/
struct FeatureRow: View {
let icon: String
let title: String
let description: String
var body: some View {
HStack(spacing: 12) {
Image(systemName: icon)
.font(.system(size: 16))
.foregroundColor(.blue)
.frame(width: 20)
VStack(alignment: .leading, spacing: 2) {
Text(title)
.font(.body)
.fontWeight(.medium)
Text(description)
.font(.caption)
.foregroundColor(.secondary)
}
Spacer()
}
}
}
/**
* 使
*/
struct TipRow: View {
let text: String
var body: some View {
HStack(alignment: .top, spacing: 8) {
Text(text)
.font(.body)
.foregroundColor(.primary)
Spacer()
}
}
}
/**
*
*
*/
class AboutWindowManager: ObservableObject {
private var aboutWindow: NSWindow?
/**
*
* - Parameters:
* - currentRefreshInterval:
* - appVersion:
*/
func showAboutWindow(currentRefreshInterval: String, appVersion: String) {
//
if let existingWindow = aboutWindow {
existingWindow.makeKeyAndOrderFront(nil)
return
}
//
let aboutView = AboutWindowView(
onClose: { [weak self] in
self?.closeAboutWindow()
},
currentRefreshInterval: currentRefreshInterval,
appVersion: appVersion
)
let hostingView = NSHostingView(rootView: aboutView)
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 420, height: 540), //
styleMask: [.titled, .closable],
backing: .buffered,
defer: false
)
window.title = "关于"
window.contentViewController = NSViewController()
window.contentViewController?.view = hostingView
//
window.layoutIfNeeded()
//
centerWindowInScreen(window)
window.isReleasedWhenClosed = false
window.titlebarAppearsTransparent = false
window.titleVisibility = .visible
//
window.level = .floating
//
self.aboutWindow = window
//
window.makeKeyAndOrderFront(nil)
print("✅ 已显示关于窗口")
}
/**
*
* - Parameter window:
*/
private func centerWindowInScreen(_ window: NSWindow) {
guard let screen = NSScreen.main else {
// 使 center()
window.center()
return
}
// 使 center()
window.center()
//
let currentFrame = window.frame
let screenFrame = screen.visibleFrame
//
let idealCenterY = screenFrame.origin.y + (screenFrame.height - currentFrame.height) / 2
// YY
if abs(currentFrame.origin.y - idealCenterY) > 1 {
var adjustedFrame = currentFrame
adjustedFrame.origin.y = idealCenterY
window.setFrame(adjustedFrame, display: false)
print("✅ 窗口位置已调整到垂直居中")
print("📐 原始Y位置: \(currentFrame.origin.y)")
print("📐 调整后Y位置: \(idealCenterY)")
} else {
print("✅ 窗口已经在垂直居中位置")
}
print("📐 屏幕可见区域: \(screenFrame)")
print("📐 最终窗口位置: \(window.frame)")
}
/**
*
*/
private func closeAboutWindow() {
aboutWindow?.close()
aboutWindow = nil
print("✅ 已关闭关于窗口")
}
}
#Preview {
AboutWindowView(
onClose: {},
currentRefreshInterval: "30秒",
appVersion: "1.0.0"
)
}