|
|
@@ -164,14 +164,19 @@ enum PrintService {
|
|
|
])
|
|
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
- window?.makeKeyAndOrderFront(nil)
|
|
|
+ let parentWindow = window ?? NSApp.keyWindow ?? NSApp.mainWindow
|
|
|
+ parentWindow?.makeKeyAndOrderFront(nil)
|
|
|
|
|
|
- let observer = observePrintPanelTrafficLights()
|
|
|
+ let observer = observePrintPanelAppearance(relativeTo: parentWindow)
|
|
|
defer { NotificationCenter.default.removeObserver(observer) }
|
|
|
- _ = operation.run()
|
|
|
+ if let parentWindow {
|
|
|
+ operation.runModal(for: parentWindow, delegate: nil, didRun: nil, contextInfo: nil)
|
|
|
+ } else {
|
|
|
+ _ = operation.run()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private static func observePrintPanelTrafficLights() -> NSObjectProtocol {
|
|
|
+ private static func observePrintPanelAppearance(relativeTo parentWindow: NSWindow?) -> NSObjectProtocol {
|
|
|
NotificationCenter.default.addObserver(
|
|
|
forName: NSWindow.didBecomeKeyNotification,
|
|
|
object: nil,
|
|
|
@@ -179,13 +184,28 @@ enum PrintService {
|
|
|
) { notification in
|
|
|
guard let panelWindow = notification.object as? NSWindow,
|
|
|
isPrintPanelWindow(panelWindow) else { return }
|
|
|
- hideTrafficLights(on: panelWindow)
|
|
|
+ configurePrintPanelWindow(panelWindow, relativeTo: parentWindow)
|
|
|
DispatchQueue.main.async {
|
|
|
- hideTrafficLights(on: panelWindow)
|
|
|
+ configurePrintPanelWindow(panelWindow, relativeTo: parentWindow)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private static func configurePrintPanelWindow(_ panelWindow: NSWindow, relativeTo parentWindow: NSWindow?) {
|
|
|
+ hideTrafficLights(on: panelWindow)
|
|
|
+ if let parentWindow {
|
|
|
+ center(panelWindow, relativeTo: parentWindow)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static func center(_ panelWindow: NSWindow, relativeTo parentWindow: NSWindow) {
|
|
|
+ let parentFrame = parentWindow.frame
|
|
|
+ var panelFrame = panelWindow.frame
|
|
|
+ panelFrame.origin.x = parentFrame.midX - panelFrame.width / 2
|
|
|
+ panelFrame.origin.y = parentFrame.midY - panelFrame.height / 2
|
|
|
+ panelWindow.setFrame(panelFrame, display: true)
|
|
|
+ }
|
|
|
+
|
|
|
private static func isPrintPanelWindow(_ window: NSWindow) -> Bool {
|
|
|
let className = String(describing: type(of: window))
|
|
|
if className.localizedCaseInsensitiveContains("printpanel") {
|