移除透明度调节功能,毛玻璃固定显示
This commit is contained in:
parent
f749771135
commit
6bc4971051
@ -1,7 +1,7 @@
|
|||||||
import AppKit
|
import AppKit
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
/// 悬浮窗口控制器:管理毛玻璃效果 + SwiftUI 视图嵌入
|
/// 悬浮窗口控制器:管理毛玻璃 + SwiftUI 视图嵌入 + 窗口显隐
|
||||||
final class FloatingWindowController: NSWindowController {
|
final class FloatingWindowController: NSWindowController {
|
||||||
private var hostingView: NSHostingView<ContentView>?
|
private var hostingView: NSHostingView<ContentView>?
|
||||||
private var visualEffect: NSVisualEffectView!
|
private var visualEffect: NSVisualEffectView!
|
||||||
@ -13,6 +13,8 @@ final class FloatingWindowController: NSWindowController {
|
|||||||
setupVisualEffect()
|
setupVisualEffect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - 毛玻璃(只做合成锚点,alpha 始终=1)
|
||||||
|
|
||||||
private func setupVisualEffect() {
|
private func setupVisualEffect() {
|
||||||
guard let window = window else { return }
|
guard let window = window else { return }
|
||||||
|
|
||||||
@ -21,6 +23,7 @@ final class FloatingWindowController: NSWindowController {
|
|||||||
ve.material = .hudWindow
|
ve.material = .hudWindow
|
||||||
ve.blendingMode = .behindWindow
|
ve.blendingMode = .behindWindow
|
||||||
ve.state = .active
|
ve.state = .active
|
||||||
|
ve.alphaValue = 1.0 // 始终不透明,维持窗口合成通道
|
||||||
ve.translatesAutoresizingMaskIntoConstraints = false
|
ve.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
ve.layer?.cornerRadius = 16
|
ve.layer?.cornerRadius = 16
|
||||||
@ -42,6 +45,8 @@ final class FloatingWindowController: NSWindowController {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - SwiftUI 嵌入
|
||||||
|
|
||||||
func embed(rootView: ContentView) {
|
func embed(rootView: ContentView) {
|
||||||
guard let window = window, let contentView = window.contentView else { return }
|
guard let window = window, let contentView = window.contentView else { return }
|
||||||
|
|
||||||
@ -93,9 +98,7 @@ final class FloatingWindowController: NSWindowController {
|
|||||||
window?.isVisible ?? false
|
window?.isVisible ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyOpacity(_ value: Double) {
|
// MARK: - 尺寸 & 位置
|
||||||
window?.alphaValue = value
|
|
||||||
}
|
|
||||||
|
|
||||||
func resize(width: CGFloat, height: CGFloat) {
|
func resize(width: CGFloat, height: CGFloat) {
|
||||||
guard let window = window else { return }
|
guard let window = window else { return }
|
||||||
|
|||||||
@ -100,9 +100,6 @@ final class Configuration: ObservableObject {
|
|||||||
@Published var refreshInterval: Double {
|
@Published var refreshInterval: Double {
|
||||||
didSet { save("refreshInterval", refreshInterval) }
|
didSet { save("refreshInterval", refreshInterval) }
|
||||||
}
|
}
|
||||||
@Published var opacity: Double {
|
|
||||||
didSet { save("opacity", opacity) }
|
|
||||||
}
|
|
||||||
@Published var fontSize: Double {
|
@Published var fontSize: Double {
|
||||||
didSet { save("fontSize", fontSize) }
|
didSet { save("fontSize", fontSize) }
|
||||||
}
|
}
|
||||||
@ -192,7 +189,6 @@ final class Configuration: ObservableObject {
|
|||||||
|
|
||||||
processCount = defaults.object(forKey: "processCount") as? Int ?? 5
|
processCount = defaults.object(forKey: "processCount") as? Int ?? 5
|
||||||
refreshInterval = defaults.object(forKey: "refreshInterval") as? Double ?? 1.5
|
refreshInterval = defaults.object(forKey: "refreshInterval") as? Double ?? 1.5
|
||||||
opacity = defaults.object(forKey: "opacity") as? Double ?? 1.0
|
|
||||||
fontSize = defaults.object(forKey: "fontSize") as? Double ?? 12
|
fontSize = defaults.object(forKey: "fontSize") as? Double ?? 12
|
||||||
windowWidth = defaults.object(forKey: "windowWidth") as? Double ?? 250
|
windowWidth = defaults.object(forKey: "windowWidth") as? Double ?? 250
|
||||||
windowHeight = defaults.object(forKey: "windowHeight") as? Double ?? 300
|
windowHeight = defaults.object(forKey: "windowHeight") as? Double ?? 300
|
||||||
|
|||||||
@ -36,14 +36,10 @@ struct ContentView: View {
|
|||||||
.onAppear {
|
.onAppear {
|
||||||
monitor.interval = config.refreshInterval
|
monitor.interval = config.refreshInterval
|
||||||
monitor.topN = config.processCount
|
monitor.topN = config.processCount
|
||||||
applyOpacity()
|
|
||||||
}
|
}
|
||||||
.onChange(of: config.processCount) { _, new in
|
.onChange(of: config.processCount) { _, new in
|
||||||
monitor.topN = new
|
monitor.topN = new
|
||||||
}
|
}
|
||||||
.onChange(of: config.opacity) { _, _ in
|
|
||||||
applyOpacity()
|
|
||||||
}
|
|
||||||
.onReceive(NotificationCenter.default.publisher(for: .refreshIntervalChanged)) { notification in
|
.onReceive(NotificationCenter.default.publisher(for: .refreshIntervalChanged)) { notification in
|
||||||
if let interval = notification.object as? Double {
|
if let interval = notification.object as? Double {
|
||||||
monitor.interval = interval
|
monitor.interval = interval
|
||||||
@ -269,9 +265,4 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func applyOpacity() {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
windowController?.applyOpacity(config.opacity)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -223,22 +223,6 @@ struct SettingsView: View {
|
|||||||
.frame(width: 180)
|
.frame(width: 180)
|
||||||
}
|
}
|
||||||
|
|
||||||
HStack {
|
|
||||||
Text("透明度")
|
|
||||||
.font(.system(size: 12))
|
|
||||||
.frame(width: 52, alignment: .leading)
|
|
||||||
Slider(value: $config.opacity, in: 0.5...1.0, step: 0.05) {
|
|
||||||
Text("")
|
|
||||||
} onEditingChanged: { editing in
|
|
||||||
if !editing { updateWindowOpacity() }
|
|
||||||
}
|
|
||||||
.frame(width: 130)
|
|
||||||
Text(String(format: "%.0f%%", config.opacity * 100))
|
|
||||||
.font(.system(size: 11, design: .monospaced))
|
|
||||||
.foregroundColor(.secondary)
|
|
||||||
.frame(width: 38, alignment: .trailing)
|
|
||||||
}
|
|
||||||
|
|
||||||
HStack {
|
HStack {
|
||||||
Text("字体大小")
|
Text("字体大小")
|
||||||
.font(.system(size: 12))
|
.font(.system(size: 12))
|
||||||
@ -314,12 +298,6 @@ struct SettingsView: View {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func updateWindowOpacity() {
|
|
||||||
if let window = NSApplication.shared.windows.first(where: { $0 is FloatingWindow }) {
|
|
||||||
window.alphaValue = config.opacity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func closeWindow() {
|
private func closeWindow() {
|
||||||
NSApp.keyWindow?.close()
|
NSApp.keyWindow?.close()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user