Files
Bitcoin-Monitoring/test1/PreferencesWindowManager.swift

132 lines
3.7 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.
//
// PreferencesWindowManager.swift
// Bitcoin Monitoring
//
// Created by Mark on 2025/10/31.
//
import SwiftUI
/**
*
*
*/
class PreferencesWindowManager: ObservableObject {
private var preferencesWindow: NSWindow?
private var appSettings: AppSettings
init(appSettings: AppSettings) {
self.appSettings = appSettings
}
/**
*
*/
func showPreferencesWindow() {
//
if let existingWindow = preferencesWindow {
existingWindow.makeKeyAndOrderFront(nil)
return
}
//
let preferencesView = PreferencesWindowView(
appSettings: appSettings,
onClose: { [weak self] in
self?.closePreferencesWindow()
}
)
let hostingView = NSHostingView(rootView: preferencesView)
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 620),
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.preferencesWindow = window
//
window.makeKeyAndOrderFront(nil)
print("✅ 已显示偏好设置窗口")
}
/**
*
*/
private func closePreferencesWindow() {
preferencesWindow?.close()
preferencesWindow = 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("✅ 偏好设置窗口位置已调整到垂直居中")
} else {
print("✅ 偏好设置窗口已经在垂直居中位置")
}
}
/**
*
* - Returns:
*/
func isWindowVisible() -> Bool {
return preferencesWindow?.isVisible ?? false
}
/**
*
*
*/
func closeWindow() {
closePreferencesWindow()
}
}