ctlNonModelAlertWindow // Guard-let the window in show().

This commit is contained in:
ShikiSuen 2022-06-02 01:05:54 +08:00
parent c30cc6697f
commit 376a97ea30
1 changed files with 10 additions and 9 deletions

View File

@ -44,7 +44,8 @@ class ctlNonModalAlertWindow: NSWindowController {
title: String, content: String, confirmButtonTitle: String, cancelButtonTitle: String?, title: String, content: String, confirmButtonTitle: String, cancelButtonTitle: String?,
cancelAsDefault: Bool, delegate: ctlNonModalAlertWindowDelegate? cancelAsDefault: Bool, delegate: ctlNonModalAlertWindowDelegate?
) { ) {
if window?.isVisible == true { guard let window = window else { return }
if window.isVisible == true {
self.delegate?.ctlNonModalAlertWindowDidCancel(self) self.delegate?.ctlNonModalAlertWindowDidCancel(self)
} }
@ -76,13 +77,13 @@ class ctlNonModalAlertWindow: NSWindowController {
if cancelButtonTitle != nil { if cancelButtonTitle != nil {
if cancelAsDefault { if cancelAsDefault {
window?.defaultButtonCell = cancelButton.cell as? NSButtonCell window.defaultButtonCell = cancelButton.cell as? NSButtonCell
} else { } else {
cancelButton.keyEquivalent = " " cancelButton.keyEquivalent = " "
window?.defaultButtonCell = confirmButton.cell as? NSButtonCell window.defaultButtonCell = confirmButton.cell as? NSButtonCell
} }
} else { } else {
window?.defaultButtonCell = confirmButton.cell as? NSButtonCell window.defaultButtonCell = confirmButton.cell as? NSButtonCell
} }
titleTextField.stringValue = title titleTextField.stringValue = title
@ -103,12 +104,12 @@ class ctlNonModalAlertWindow: NSWindowController {
newFrame.origin.y -= (newFrame.size.height - oldFrame.size.height) newFrame.origin.y -= (newFrame.size.height - oldFrame.size.height)
contentTextField.frame = newFrame contentTextField.frame = newFrame
var windowFrame = window?.frame ?? NSRect.zero var windowFrame = window.frame
windowFrame.size.height += (newFrame.size.height - oldFrame.size.height) windowFrame.size.height += (newFrame.size.height - oldFrame.size.height)
window?.level = NSWindow.Level(Int(CGShieldingWindowLevel()) + 1) window.level = NSWindow.Level(Int(CGShieldingWindowLevel()) + 1)
window?.setFrame(windowFrame, display: true) window.setFrame(windowFrame, display: true)
window?.center() window.center()
window?.makeKeyAndOrderFront(self) window.makeKeyAndOrderFront(self)
NSApp.activate(ignoringOtherApps: true) NSApp.activate(ignoringOtherApps: true)
} }