更改关于的显示方式

This commit is contained in:
ZhangLei
2025-10-31 06:06:15 +08:00
parent 938ba0ecbb
commit 21c5890709
2 changed files with 298 additions and 22 deletions

288
test1/AboutWindowView.swift Normal file
View File

@@ -0,0 +1,288 @@
//
// 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"
)
}

View File

@@ -18,6 +18,9 @@ class BTCMenuBarApp: NSObject, ObservableObject {
private let priceManager: PriceManager
private var cancellables = Set<AnyCancellable>()
//
private let aboutWindowManager = AboutWindowManager()
override init() {
let settings = AppSettings()
self.appSettings = settings
@@ -404,31 +407,16 @@ class BTCMenuBarApp: NSObject, ObservableObject {
print("✅ 刷新间隔已更新为: \(interval.displayText)")
}
//
//
@objc private func showAbout() {
let currentInterval = priceManager.getCurrentRefreshInterval()
//
let version = getAppVersion()
let alert = NSAlert()
alert.messageText = "BTC价格监控器 v\(version)"
alert.informativeText = """
🚀 一款 macOS 原生菜单栏应用,用于实时显示主流币种价格
✨ 功能特性:
• 实时显示主流币种/USDT价格BTC/ETH/BNB/SOL/DOGE
• 可配置刷新间隔(当前:\(currentInterval.displayText)
• 支持手动刷新 (Cmd+R)
• 智能错误重试机制
• 优雅的SF Symbols图标
💡 TIPS
• 点击币种名称为切换主菜单栏显示
• Option + 鼠标左键复制价格
"""
alert.alertStyle = .informational
alert.addButton(withTitle: "确定")
alert.runModal()
// 使 NSAlert
aboutWindowManager.showAboutWindow(
currentRefreshInterval: currentInterval.displayText,
appVersion: version
)
}
// Debug