Browse Source

【综合】密码窗口优化创建方式

tangchao 10 tháng trước cách đây
mục cha
commit
af5aa593fb

+ 7 - 0
PDF Office/PDF Master/Class/ChromiumTabs/KMBrowserWindowController.swift

@@ -128,10 +128,17 @@ import Cocoa
     }
     
     override func windowShouldClose(_ sender: NSWindow) -> Bool {
+#if DEBUG
+        if self.browser.tabStripModel.count() > 1 {
+            self.browser.windowDidBeginToClose()
+            return false
+        }
+#else
         if let cnt = self.browser?.tabStripModel?.count(), cnt > 1 {
             self.browser?.windowDidBeginToClose()
             return false
         }
+#endif
         return true
     }
     

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/Merge/Controller/KMMergeViewController.swift

@@ -842,7 +842,7 @@ class KMMergeViewController: NSViewController {
             alert.addButton(withTitle: "关闭")
             let response = alert.runModal()
             if response == .alertFirstButtonReturn {
-                let window = KMPasswordInputWindow().window()
+                let window = KMPasswordInputWindow.createWindow()
                 window?.documentURL = documentURL!
                 window?.type = .open
                 

+ 137 - 126
PDF Office/PDF Master/Class/PDFTools/Secure/Window/KMPasswordInputWindow.swift

@@ -22,7 +22,7 @@ typealias KMPasswordInputWindowItemClick = (Int, String) -> ()
 private var passwordInputWindow: KMPasswordInputWindow?
 private var passwordInputWindow_private: KMPasswordInputWindow?
 
-@objcMembers class KMPasswordInputWindow: NSWindow {
+@objcMembers class KMPasswordInputWindow: NSWindow, NibLoadable {
     @IBOutlet weak var titleLabel: NSTextField!
     
     @IBOutlet weak var despLabel: NSTextField!
@@ -100,6 +100,23 @@ private var passwordInputWindow_private: KMPasswordInputWindow?
         KMPrint("KMPasswordInputWindow 已释放了")
     }
     
+    static var nibName: String? {
+        return "KMPasswordInputWindow"
+    }
+    
+    static func createFromNib(in bundle: Bundle) -> Self? {
+        guard let nibName = self.nibName else {
+            return nil
+        }
+        var topLevelArray: NSArray? = nil
+        bundle.loadNibNamed(NSNib.Name(nibName), owner: self, topLevelObjects: &topLevelArray)
+        guard let results = topLevelArray else {
+            return nil
+        }
+        let views = Array<Any>(results).filter { $0 is Self }
+        return views.last as? Self
+    }
+    
     override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
         super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
     }
@@ -107,102 +124,90 @@ private var passwordInputWindow_private: KMPasswordInputWindow?
     override func awakeFromNib() {
         super.awakeFromNib()
         
-        if (titleLabel != nil) {
-            passwordInputWindow_private = self
+        passwordInputWindow_private = self
             
-            titleLabel.stringValue = NSLocalizedString("Permission Password", comment: "")
-            self.titleLabel.textColor = KMAppearance.Layout.h0Color()
-            titleLabel.font = NSFont.SFProTextRegularFont(16)
-//            self.titleLabel.window?.appearance = NSAppearance(named: .aqua)
-        }
+        self.titleLabel.stringValue = NSLocalizedString("Permission Password", comment: "")
+        self.titleLabel.textColor = KMAppearance.Layout.h0Color()
+        self.titleLabel.font = NSFont.SFProTextRegularFont(16)
         
-        if despLabel != nil {
-            despLabel.stringValue = NSLocalizedString("This PDF is password protected. Please enter the password below to access this PDF.", comment: "")
-            self.despLabel.textColor = KMAppearance.Layout.h0Color()
-            despLabel.font = NSFont.SFProTextRegularFont(14)
-            despLabel.isSelectable = false
-            let ps = NSMutableParagraphStyle()
-            ps.lineSpacing = 5
-            despLabel.maximumNumberOfLines = 2
-            despLabel.lineBreakMode = .byTruncatingTail
+        self.despLabel.stringValue = NSLocalizedString("This PDF is password protected. Please enter the password below to access this PDF.", comment: "")
+        self.despLabel.textColor = KMAppearance.Layout.h0Color()
+        self.despLabel.font = NSFont.SFProTextRegularFont(14)
+        self.despLabel.isSelectable = false
+        let ps = NSMutableParagraphStyle()
+        ps.lineSpacing = 5
+        self.despLabel.maximumNumberOfLines = 2
+        self.despLabel.lineBreakMode = .byTruncatingTail
             ps.lineBreakMode = .byTruncatingTail
-            despLabel.attributedStringValue = NSAttributedString(string: despLabel.stringValue, attributes: [.foregroundColor : KMAppearance.Layout.h0Color(), .font : NSFont.SFProTextRegularFont(14), .paragraphStyle : ps])
-        }
+        self.despLabel.attributedStringValue = NSAttributedString(string: despLabel.stringValue, attributes: [.foregroundColor : KMAppearance.Layout.h0Color(), .font : NSFont.SFProTextRegularFont(14), .paragraphStyle : ps])
         
-        if (iconImageView != nil) {
-            self.iconImageView.image = NSImage(named: "KMImageNameSecureIcon")
-        }
+        self.iconImageView.image = NSImage(named: "KMImageNameSecureIcon")
 
-        if secureTextFiled != nil {
-            secureTextFiled.backgroundView.wantsLayer = true
-            secureTextFiled.backgroundView.layer?.borderWidth = 1
-            secureTextFiled.backgroundView.layer?.borderColor = NSColor.buttonBorderColor().cgColor
-            secureTextFiled.backgroundView.layer?.cornerRadius = 4
-            secureTextFiled.placeholderString = NSLocalizedString("Password", comment: "")
+        self.secureTextFiled.backgroundView.wantsLayer = true
+        self.secureTextFiled.backgroundView.layer?.borderWidth = 1
+        self.secureTextFiled.backgroundView.layer?.borderColor = NSColor.buttonBorderColor().cgColor
+        self.secureTextFiled.backgroundView.layer?.cornerRadius = 4
+        self.secureTextFiled.placeholderString = NSLocalizedString("Password", comment: "")
             
-            let rightView = NSView()
-            rightView.frame = NSMakeRect(0, 0, 40, 32);
-            secureTextFiled.rightView = rightView
-            let clearButton = NSButton()
-            rightView.addSubview(clearButton)
-            clearButton.frame = NSMakeRect(10, 6, 20, 20)
-            clearButton.wantsLayer = true
-            clearButton.image = NSImage(named: "KMImageNameSecureClearIcon")
-            clearButton.isBordered = false
-            clearButton.target = self
-            clearButton.action = #selector(clearButtonAction)
-            rightView.isHidden = true
+        let rightView = NSView()
+        rightView.frame = NSMakeRect(0, 0, 40, 32);
+        self.secureTextFiled.rightView = rightView
+        let clearButton = NSButton()
+        rightView.addSubview(clearButton)
+        clearButton.frame = NSMakeRect(10, 6, 20, 20)
+        clearButton.wantsLayer = true
+        clearButton.image = NSImage(named: "KMImageNameSecureClearIcon")
+        clearButton.isBordered = false
+        clearButton.target = self
+        clearButton.action = #selector(clearButtonAction)
+        rightView.isHidden = true
 
-            secureTextFiled.becomeFirstResponderHandler = { [unowned self] securetextFiled in
-                let mySecureTextField: KMSecureTextFiled = securetextFiled as! KMSecureTextFiled
-                mySecureTextField.backgroundView.wantsLayer = true
-                mySecureTextField.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
+        self.secureTextFiled.becomeFirstResponderHandler = { [unowned self] securetextFiled in
+            let mySecureTextField: KMSecureTextFiled = securetextFiled as! KMSecureTextFiled
+            mySecureTextField.backgroundView.wantsLayer = true
+            mySecureTextField.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
                 
-                if mySecureTextField.password().isEmpty {
-                    self.secureTextFiled.rightView?.isHidden = true
-                } else {
-                    self.secureTextFiled.rightView?.isHidden = false
-                }
-                if self.passwordErrorLabel != nil {
-                    self.passwordErrorLabel.isHidden = true
-                }
+            if mySecureTextField.password().isEmpty {
+                self.secureTextFiled.rightView?.isHidden = true
+            } else {
+                self.secureTextFiled.rightView?.isHidden = false
             }
-            secureTextFiled.valueDidChange = { [unowned self] view, string in
-                view.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
-                if self.passwordErrorLabel != nil {
-                    self.passwordErrorLabel.isHidden = true
-                }
-                if string.isEmpty {
-                    view.rightView?.isHidden = true
-                    self.dealConfirmButtonEnabledState(enabled: false)
-                } else {
-                    view.rightView?.isHidden = false
-                    self.dealConfirmButtonEnabledState(enabled: true)
-                }
+            if self.passwordErrorLabel != nil {
+                self.passwordErrorLabel.isHidden = true
             }
-            
-            secureTextFiled.enterAction = { [unowned self] in
-                self.confirmButtonAction()
+        }
+        self.secureTextFiled.valueDidChange = { [unowned self] view, string in
+            view.backgroundView.layer?.borderColor = NSColor.km_init(hex: "#1770F4").cgColor
+            if self.passwordErrorLabel != nil {
+                self.passwordErrorLabel.isHidden = true
+            }
+            if string.isEmpty {
+                view.rightView?.isHidden = true
+                self.dealConfirmButtonEnabledState(enabled: false)
+            } else {
+                view.rightView?.isHidden = false
+                self.dealConfirmButtonEnabledState(enabled: true)
             }
         }
-        
-        if passwordErrorLabel != nil {
-            passwordErrorLabel.stringValue = NSLocalizedString("Incorrect password. Please try again.", comment: "")
-            passwordErrorLabel.font = NSFont.systemFont(ofSize: 12)
-            passwordErrorLabel.wantsLayer = true
-            passwordErrorLabel.textColor = NSColor.km_init(hex: "#F3465B")
-            passwordErrorLabel.isHidden = true
+            
+        self.secureTextFiled.enterAction = { [unowned self] in
+            self.confirmButtonAction()
         }
         
-        if cancelButton != nil {
-            for button in [cancelButton, confirmButton] {
+        self.passwordErrorLabel.stringValue = NSLocalizedString("Incorrect password. Please try again.", comment: "")
+        self.passwordErrorLabel.font = NSFont.systemFont(ofSize: 12)
+        self.passwordErrorLabel.wantsLayer = true
+        self.passwordErrorLabel.textColor = NSColor.km_init(hex: "#F3465B")
+        self.passwordErrorLabel.isHidden = true
+        
+        for button in [cancelButton, confirmButton] {
 //                button?.wantsLayer = true
 //                button?.layer?.cornerRadius = 4
 //                button?.bezelStyle = .roundRect
 //                button?.setButtonType(.momentaryPushIn)
                 
-                button!.target = self
-                if ((button?.isEqual(to: cancelButton))!) {
+            button!.target = self
+            if ((button?.isEqual(to: cancelButton))!) {
 //                    button?.layer?.borderWidth = 1
 //                    button?.layer?.borderColor = NSColor.buttonBorderColor().cgColor
 //                    button?.title = NSLocalizedString("Cancel", comment: "")
@@ -210,68 +215,74 @@ private var passwordInputWindow_private: KMPasswordInputWindow?
 //                    button?.setTitleColor(NSColor.buttonTitleColor())
 //                    button?.font = NSFont.SFProTextRegularFont(14)
 //                    button?.action = #selector(cancelButtonAction)
-                } else {
+            } else {
 //                    button?.title = NSLocalizedString("Open", comment: "")
 //                    button?.attributedTitle = NSMutableAttributedString(string: button!.title, attributes: [.foregroundColor : NSColor.white])
 //                    button?.font = NSFont.SFProTextRegularFont(14)
 //                    button?.action = #selector(confirmButtonAction)
 //                    button?.title = ""
-                }
             }
+        }
             
-            let cancelButtonVC = KMDesignButton(withType: .Text)
+        let cancelButtonVC = KMDesignButton(withType: .Text)
 //            self.cancelButton.addSubview(cancelButtonVC.view)
-            cancelButtonVC.view.frame = self.cancelButton.bounds
-            cancelButtonVC.view.autoresizingMask = [.width, .height]
-            cancelButtonVC.stringValue = NSLocalizedString("Cancel", comment: "")
-            cancelButtonVC.button(type: .Sec_Icon, size: .m)
-            cancelButtonVC.target = self
-            cancelButtonVC.action = #selector(cancelButtonAction)
-            cancelButtonVC.button.keyEquivalent = KMKeyEquivalent.esc.string()
-            self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
-            self.cancelButton.action = #selector(cancelButtonAction)
+        cancelButtonVC.view.frame = self.cancelButton.bounds
+        cancelButtonVC.view.autoresizingMask = [.width, .height]
+        cancelButtonVC.stringValue = NSLocalizedString("Cancel", comment: "")
+        cancelButtonVC.button(type: .Sec_Icon, size: .m)
+        cancelButtonVC.target = self
+        cancelButtonVC.action = #selector(cancelButtonAction)
+        cancelButtonVC.button.keyEquivalent = KMKeyEquivalent.esc.string()
+        self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
+        self.cancelButton.action = #selector(cancelButtonAction)
             
-            let confirmButtonVC = KMDesignButton(withType: .Text)
+        let confirmButtonVC = KMDesignButton(withType: .Text)
 //            self.confirmButton.addSubview(confirmButtonVC.view)
-            confirmButtonVC.view.frame = self.confirmButton.bounds
-            confirmButtonVC.view.autoresizingMask = [.width, .height]
-            confirmButtonVC.stringValue = NSLocalizedString("Open", comment: "")
-            confirmButtonVC.button(type: .Cta, size: .m)
-            confirmButtonVC.target = self
-            confirmButtonVC.action = #selector(confirmButtonAction)
-            self.confirmButtonVC = confirmButtonVC
-            self.confirmButtonVC?.button.keyEquivalent = KMKeyEquivalent.enter
-            self.confirmButton.title = NSLocalizedString("Open", comment: "")
-            self.confirmButton.action = #selector(confirmButtonAction)
-            
-            dealConfirmButtonEnabledState(enabled: false)
-        }
+        confirmButtonVC.view.frame = self.confirmButton.bounds
+        confirmButtonVC.view.autoresizingMask = [.width, .height]
+        confirmButtonVC.stringValue = NSLocalizedString("Open", comment: "")
+        confirmButtonVC.button(type: .Cta, size: .m)
+        confirmButtonVC.target = self
+        confirmButtonVC.action = #selector(confirmButtonAction)
+        self.confirmButtonVC = confirmButtonVC
+        self.confirmButtonVC?.button.keyEquivalent = KMKeyEquivalent.enter
+        self.confirmButton.title = NSLocalizedString("Open", comment: "")
+        self.confirmButton.action = #selector(confirmButtonAction)
+        
+        self.dealConfirmButtonEnabledState(enabled: false)
     }
     
-    func window() -> Self? {
+    class func createWindow() -> Self? {
         KMPasswordInputWindow.canEncrpty = false
         KMPasswordInputWindow.permissionsStatus = .none
         
-        var topLevelArray: NSArray? = nil
-        Bundle.main.loadNibNamed(NSNib.Name("KMPasswordInputWindow"), owner: self, topLevelObjects: &topLevelArray)
-        guard let results = topLevelArray else {
-            return nil
-        }
-        
-        var passwordInputWindow: KMPasswordInputWindow!
-        for object in results {
-            let window: NSObject = object as! NSObject
-            if window.isKind(of: KMPasswordInputWindow.self) {
-                passwordInputWindow = window as! KMPasswordInputWindow?
-            }
-        }
-        
-        guard let myWindow = passwordInputWindow else {
-            return nil
-        }
-        return myWindow as? Self
+        return self.createFromNib(in: MainBundle)
     }
     
+//    func window() -> Self? {
+//        KMPasswordInputWindow.canEncrpty = false
+//        KMPasswordInputWindow.permissionsStatus = .none
+//        
+//        var topLevelArray: NSArray? = nil
+//        Bundle.main.loadNibNamed(NSNib.Name("KMPasswordInputWindow"), owner: self, topLevelObjects: &topLevelArray)
+//        guard let results = topLevelArray else {
+//            return nil
+//        }
+//        
+//        var passwordInputWindow: KMPasswordInputWindow!
+//        for object in results {
+//            let window: NSObject = object as! NSObject
+//            if window.isKind(of: KMPasswordInputWindow.self) {
+//                passwordInputWindow = window as! KMPasswordInputWindow?
+//            }
+//        }
+//        
+//        guard let myWindow = passwordInputWindow else {
+//            return nil
+//        }
+//        return myWindow as? Self
+//    }
+    
     @objc func cancelButtonAction() {
         guard let callback = KMPasswordInputWindow.myItemClick else {
             return
@@ -465,7 +476,7 @@ extension KMPasswordInputWindow {
     }
     
     @objc class func openWindow(window: NSWindow, type: KMPasswordInputWindowType = .open, url: URL, callback: @escaping (KMPasswordInputWindowResult, String?)->Void) -> KMPasswordInputWindow {
-        let passwordWindow = KMPasswordInputWindow().window()
+        let passwordWindow = KMPasswordInputWindow.createWindow()
         passwordWindow?.documentURL = url
         passwordWindow?.type = type
         
@@ -490,7 +501,7 @@ extension KMPasswordInputWindow {
     }
     
     @objc class func success_openWindow(window: NSWindow, type: KMPasswordInputWindowType = .open, url: URL, callback: @escaping (String)->Void) {
-        let passwordWindow = KMPasswordInputWindow().window()
+        let passwordWindow = KMPasswordInputWindow.createWindow()
         passwordWindow?.documentURL = url
         passwordWindow?.type = type
         
@@ -513,7 +524,7 @@ extension KMPasswordInputWindow {
     }
     
     @objc class func openWindow(window: NSWindow, url: URL, needOwner: Bool, callback: @escaping (KMPasswordInputWindowResult, String?)->Void) {
-        let passwordWindow = KMPasswordInputWindow().window()
+        let passwordWindow = KMPasswordInputWindow.createWindow()
         passwordWindow?.documentURL = url
         
         let document = CPDFDocument(url: url)

+ 9 - 0
PDF Office/PDF Master/Class/PDFTools/Secure/Window/KMPasswordInputWindow.xib

@@ -184,6 +184,15 @@ DQ
                     <constraint firstItem="ezx-3i-oVb" firstAttribute="top" secondItem="EiT-Mj-1SZ" secondAttribute="top" id="z4x-Jd-Tt5"/>
                 </constraints>
             </view>
+            <connections>
+                <outlet property="cancelButton" destination="jwm-QT-mwz" id="owI-qe-VM9"/>
+                <outlet property="confirmButton" destination="Dpd-lf-2h1" id="ExL-Jf-SeP"/>
+                <outlet property="despLabel" destination="xRE-aR-QBJ" id="anM-rS-9UT"/>
+                <outlet property="iconImageView" destination="hRH-zQ-1pZ" id="7qd-oa-CLl"/>
+                <outlet property="passwordErrorLabel" destination="gST-4X-s69" id="t8b-EJ-11R"/>
+                <outlet property="secureTextFiled" destination="nlf-dC-L2j" id="omH-Df-hvy"/>
+                <outlet property="titleLabel" destination="X38-4p-51v" id="4qf-4E-g3F"/>
+            </connections>
             <point key="canvasLocation" x="83" y="-5"/>
         </window>
     </objects>