diff --git a/Fire/AppDelegate.swift b/Fire/AppDelegate.swift index b6c21c3..fb39903 100644 --- a/Fire/AppDelegate.swift +++ b/Fire/AppDelegate.swift @@ -20,7 +20,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { InputSource.shared.activateInputSource() NSApp.terminate(nil) } - + private func commandHandler() { if CommandLine.arguments.count > 1 { print("[Fire] launch argument: \(CommandLine.arguments[1])") diff --git a/Fire/CandidatesView.swift b/Fire/CandidatesView.swift index 5d24c7f..60075c2 100644 --- a/Fire/CandidatesView.swift +++ b/Fire/CandidatesView.swift @@ -87,6 +87,7 @@ struct CandidatesView: View { }) .padding(.horizontal, 10.0) .padding(.vertical, 6) + .fixedSize() .background(Color.white) } } diff --git a/Fire/CandidatesWindow.swift b/Fire/CandidatesWindow.swift index daa1838..acc860b 100644 --- a/Fire/CandidatesWindow.swift +++ b/Fire/CandidatesWindow.swift @@ -9,14 +9,18 @@ import SwiftUI import InputMethodKit +var set = false + class CandidatesWindow: NSWindow { + let hostingView = NSHostingView(rootView: CandidatesView(candidates: [], origin: "")) func setCandidates( candidates: [Candidate], originalString: String, topLeft: NSPoint ) { - self.contentView = NSHostingView(rootView: CandidatesView(candidates: candidates, origin: originalString)) + hostingView.rootView.candidates = candidates + hostingView.rootView.origin = originalString let origin = self.transformTopLeft(originalTopLeft: topLeft) self.setFrameTopLeftPoint(origin) self.orderFront(nil) @@ -31,9 +35,23 @@ class CandidatesWindow: NSWindow { super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag) level = NSWindow.Level(rawValue: NSWindow.Level.RawValue(CGShieldingWindowLevel())) - self.viewsNeedDisplay = true - styleMask = .init(arrayLiteral: .borderless, .fullSizeContentView) + styleMask = .init(arrayLiteral: .fullSizeContentView, .borderless) isReleasedWhenClosed = false + backgroundColor = NSColor.clear + setSizePolicy() + } + + private func setSizePolicy() { + // 窗口大小可根据内容变化 + hostingView.translatesAutoresizingMaskIntoConstraints = false + guard self.contentView != nil else { + return + } + self.contentView?.addSubview(hostingView) + self.contentView?.leftAnchor.constraint(equalTo: hostingView.leftAnchor).isActive = true + self.contentView?.rightAnchor.constraint(equalTo: hostingView.rightAnchor).isActive = true + self.contentView?.topAnchor.constraint(equalTo: hostingView.topAnchor).isActive = true + self.contentView?.bottomAnchor.constraint(equalTo: hostingView.bottomAnchor).isActive = true } private func transformTopLeft(originalTopLeft: NSPoint) -> NSPoint {