소스 검색

【综合】整理代码

tangchao 3 달 전
부모
커밋
c7621bce64

+ 129 - 72
PDF Office/PDF Master/KMClass/Left/Outline/Controller/KMOutlineEditViewController.swift

@@ -37,6 +37,8 @@ class KMOutlineEditViewController: KMNBaseViewController {
     
     var pageCount = 0
     
+    var itemClick: KMCommonClickBlock?
+    
     private lazy var inputView_: ComponentTextarea = {
         let view = ComponentTextarea()
         view.properties = ComponentTextareaProperty(size: .m, state: .pressed, isError: false, placeholderString: "", totalCount: -1, text: "", isDisabled: false)
@@ -49,9 +51,20 @@ class KMOutlineEditViewController: KMNBaseViewController {
         return view
     }()
     
-    private lazy var pageInput_: ComponentInputAddon = {
-        let view = ComponentInputAddon()
-        view.properties = ComponentInputAddonProperty(size: .s, state: .normal, isDisabled: false, addOnBefore: false, onlyRead: true, addonType: .textWithColor, iconImage: nil, text: KMLocalizedString("11"))
+    private lazy var pageInput_: ComponentInput = {
+        let view = ComponentInput()
+//        view.properties = ComponentInputAddonProperty(size: .s, state: .normal, isDisabled: false, addOnBefore: false, onlyRead: true, addonType: .textWithColor, iconImage: nil, text: KMLocalizedString("11"))
+        let inputWithAddonProperty = ComponentInputProperty(size: .s,
+                                                            state: .normal,
+                                                            isError: false,
+                                                            showPrefix: false,
+                                                            showSuffix: false,
+                                                            showClear: false,
+                                                            isDisabled: true,
+                                                            placeholder: "",
+                                                            text: "",
+                                                            creatable: true)
+        view.properties = inputWithAddonProperty
         return view
     }()
     
@@ -96,6 +109,42 @@ class KMOutlineEditViewController: KMNBaseViewController {
         return view
     }()
     
+    var pageRadio: ComponentRadio {
+        get {
+            return pageRadio_
+        }
+    }
+    
+    var pageInput: ComponentInput {
+        get {
+            return pageInput_
+        }
+    }
+    
+    var webRaido: ComponentRadio {
+        get {
+            return webRadio_
+        }
+    }
+    
+    var webInput: ComponentInput {
+        get {
+            return webInput_
+        }
+    }
+    
+    var emailRadio: ComponentRadio {
+        get {
+            return emailRadio_
+        }
+    }
+    
+    var emailInput: ComponentInput {
+        get {
+            return emailInput_
+        }
+    }
+    
     convenience init(outline: CPDFOutline?, document: CPDFView?) {
         self.init()
         
@@ -109,6 +158,8 @@ class KMOutlineEditViewController: KMNBaseViewController {
         inputView_.frame = inputScrollView.contentView.bounds
         inputView_.autoresizingMask = [.width, .height]
         inputScrollView.documentView = inputView_
+        inputView_.properties.text = outline?.label ?? ""
+        inputView_.reloadData()
         
         pageButton.isHidden = true
         totalPageCountLabel.isHidden = true
@@ -123,12 +174,13 @@ class KMOutlineEditViewController: KMNBaseViewController {
         pageInput_.km_add_leading_constraint(equalTo: pageRadio_, attribute: .trailing, constant: 0)
         pageInput_.km_add_centerY_constraint()
         pageInput_.km_add_height_constraint(constant: 32)
-        pageInput_.km_add_width_constraint(constant: 165)
+        pageInput_.km_add_width_constraint(constant: 80)
         pageLabel.km_add_leading_constraint(equalTo: pageInput_, attribute: .trailing, constant: 8)
         pageLabel.km_add_centerY_constraint()
         pageLabel.stringValue = "/\(pageCount)"
         pageInput_.properties.text = "11"
         pageInput_.reloadData()
+        pageRadio_.setTarget(self, action: #selector(buttonClicked_GotoAction))
         
         urlButton.isHidden = true
         outlineURLTextField.isHidden = true
@@ -143,6 +195,7 @@ class KMOutlineEditViewController: KMNBaseViewController {
         webInput_.km_add_centerY_constraint()
         webInput_.km_add_height_constraint(constant: 32)
         webInput_.km_add_trailing_constraint()
+        webRadio_.setTarget(self, action: #selector(buttonClicked_URLAction))
         
         mailButton.isHidden = true
         mailAddressTextField.isHidden = true
@@ -157,24 +210,25 @@ class KMOutlineEditViewController: KMNBaseViewController {
         emailInput_.km_add_centerY_constraint()
         emailInput_.km_add_height_constraint(constant: 32)
         emailInput_.km_add_trailing_constraint()
+        emailRadio_.setTarget(self, action: #selector(buttonClicked_MailAction))
         
         bottomBox.borderWidth = 0
         leftButtonBox.borderWidth = 0
         leftButtonBox.contentView = cancelButton_
         rightButtonBox.borderWidth = 0
         rightButtonBox.contentView = okButton_
-
+        cancelButton_.setTarget(self, action: #selector(_cancelAction))
+        okButton_.setTarget(self, action: #selector(_okAction))
+        
         outlineNameLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
         pageLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
         
-        self.localizedLanguage()
-        
-        self.setONButton(button: self.pageButton)
-        self.enableTextField(textField: self.outlineTargetPageIndexTextField)
+        setONButton(button: pageRadio_)
+        enableTextField(textField: pageInput_)
         
-        self.setDelegateAndFormatter()
-        if let action = self.outline?.action {
-            self.adjustUIWithAction(action: action)
+        setDelegateAndFormatter()
+        if let action = outline?.action {
+            adjustUIWithAction(action: action)
         }
 
     }
@@ -200,54 +254,48 @@ class KMOutlineEditViewController: KMNBaseViewController {
     
     //MARK: - Private Methods
     
-    private func localizedLanguage () {
-        
-        self.pageButton.title = "\(NSLocalizedString("Page", comment: ""))"
-        self.urlButton.title = "\(NSLocalizedString("URL:", comment: ""))"
-        self.outlineURLTextField.placeholderString = "\(NSLocalizedString("https://www.pdfreaderpro.com", comment: ""))"
-        self.mailButton.title = "Email:"
-        self.mailAddressTextField.placeholderString = "\(NSLocalizedString("support@pdfreaderpro.com", comment: ""))"
-        self.outlineNameTextView.string = self.outline?.label ?? ""
-    }
-    
     private func adjustUIWithAction(action:CPDFAction!) {
         guard let doc = self.pdfView?.document else {
             return
         }
         
         if action == nil {
-            self.totalPageCountLabel.stringValue = " \(doc.pageCount)"
-            self.outlineTargetPageIndexTextField.stringValue = " \(self.currentPageIndex + 1)"
+            pageLabel.stringValue = " \(doc.pageCount)"
+            pageInput_.properties.text = " \(currentPageIndex + 1)"
+            pageInput_.reloadData()
         } else {
-            guard let outL  = self.outline else {
+            guard let outL  = outline else {
                 return
             }
             if action.isKind(of: CPDFURLAction.self) {
-                var newAction : CPDFURLAction = self.outline?.action as! CPDFURLAction
+                var newAction : CPDFURLAction = outline?.action as! CPDFURLAction
                 var urlString = newAction.url() ?? ""
-                self.originalURLString = urlString
+                originalURLString = urlString
                 
                 if urlString.hasPrefix("mailto:") {
-                    self.setONButton(button: self.mailButton)
-                    self.enableTextField(textField: self.mailAddressTextField)
+                    setONButton(button: emailRadio_)
+                    enableTextField(textField: emailInput_)
                     urlString = String(urlString.suffix(from: "mailto:".endIndex))
-                    self.mailAddressTextField.stringValue = urlString
+                    emailInput_.properties.text = urlString
+                    emailInput_.reloadData()
                 } else {
-                    self.setONButton(button: self.urlButton)
-                    self.enableTextField(textField: self.outlineURLTextField)
-                    self.outlineURLTextField.stringValue = urlString
+                    setONButton(button: webRadio_)
+                    enableTextField(textField: webInput_)
+                    webInput_.properties.text = urlString
+                    webInput_.reloadData()
                 }
-                self.currentPageIndex = Int(doc.index(for: doc.page(at: UInt(self.pdfView!.currentPageIndex))))
+                currentPageIndex = Int(doc.index(for: doc.page(at: UInt(pdfView?.currentPageIndex ?? 0))))
             } else {
-                self.setONButton(button: self.pageButton)
-                self.enableTextField(textField: self.outlineTargetPageIndexTextField)
-                self.currentPageIndex = Int(doc.index(for: doc.page(at: UInt(outL.destination.pageIndex))))
-                self.originalDestination = outL.destination
-                self.originalPageIndex = self.originalDestination?.pageIndex ?? -1
+                setONButton(button: pageRadio_)
+                enableTextField(textField: pageInput_)
+                currentPageIndex = Int(doc.index(for: doc.page(at: UInt(outL.destination.pageIndex))))
+                originalDestination = outL.destination
+                originalPageIndex = originalDestination?.pageIndex ?? -1
             }
-            self.originalLabel = outL.label ?? ""
-            self.totalPageCountLabel.stringValue = " /\(doc.pageCount)"
-            self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)"
+            originalLabel = outL.label ?? ""
+            pageLabel.stringValue = " /\(doc.pageCount)"
+            pageInput_.properties.text = "\(self.currentPageIndex + 1)"
+            pageInput_.reloadData()
         }
     }
     
@@ -262,59 +310,68 @@ class KMOutlineEditViewController: KMNBaseViewController {
         self.outlineTargetPageIndexTextField.formatter = formatter
     }
     
-    private func setONButton(button:NSButton) {
-        self.pageButton.state = NSControl.StateValue.off
-        self.urlButton.state = NSControl.StateValue.off
-        self.mailButton.state = NSControl.StateValue.off
-        
-        button.state = NSControl.StateValue.on
+    private func setONButton(button sender: ComponentRadio) {
+        for radio in [pageRadio_, webRadio_, emailRadio_] {
+            if radio.isEqual(to: sender) {
+                radio.properties.checkboxType = .selected
+            } else {
+                radio.properties.checkboxType = .normal
+            }
+            radio.reloadData()
+        }
     }
     
-    private func enableTextField(textField:NSTextField) {
-        self.outlineTargetPageIndexTextField.isEditable = false
-        self.outlineTargetPageIndexTextField.isSelectable = false
-        self.outlineURLTextField.isEditable = false
-        self.outlineURLTextField.isSelectable = false
-        self.mailAddressTextField.isEditable = false
-        self.mailAddressTextField.isSelectable = false
-        
-        self.outlineTargetPageIndexTextField.textColor = KMAppearance.Layout.h0Color()
-        self.outlineURLTextField.textColor = KMAppearance.Layout.h0Color()
-        self.mailAddressTextField.textColor = KMAppearance.Layout.h0Color()
-        
-        textField.isEditable = true
-        textField.isSelectable = true
-        textField.textColor = KMAppearance.Layout.h0Color()
+    private func enableTextField(textField sender: ComponentInput) {
+        for input in [pageInput_, webInput_, emailInput_] {
+            if sender.isEqual(to: input) {
+                input.properties.isDisabled = false
+                input.properties.creatable = true
+            } else {
+                input.properties.isDisabled = true
+                input.properties.creatable = false
+            }
+            
+            input.reloadData()
+        }
     }
     
     //MARK: Button Action
+    
     @IBAction func buttonClicked_GotoAction(_ sender: Any) {
-        self.setONButton(button: self.pageButton)
-        self.enableTextField(textField: self.outlineTargetPageIndexTextField)
+        self.setONButton(button: pageRadio_)
+        self.enableTextField(textField: pageInput_)
+//        self.view.window?.makeFirstResponder(self.outlineTargetPageIndexTextField)
         
-        self.view.window?.makeFirstResponder(self.outlineTargetPageIndexTextField)
     }
     
     @IBAction func buttonClicked_URLAction(_ sender: Any) {
-        self.setONButton(button: self.urlButton)
-        self.enableTextField(textField: self.outlineURLTextField)
+        self.setONButton(button: self.webRadio_)
+        self.enableTextField(textField: webInput_)
+//        self.view.window?.makeFirstResponder(self.outlineURLTextField)
+        
         
-        self.view.window?.makeFirstResponder(self.outlineURLTextField)
         if (self.outlineTargetPageIndexTextField.stringValue == "0" || self.outlineTargetPageIndexTextField.stringValue.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) < 1){
             self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)"
         }
     }
     
     @IBAction func buttonClicked_MailAction(_ sender: Any) {
-        self.setONButton(button: self.mailButton)
-        self.enableTextField(textField: self.mailAddressTextField)
+        self.setONButton(button: self.emailRadio_)
+        self.enableTextField(textField: emailInput_)
+//        self.view.window?.makeFirstResponder(self.mailAddressTextField)
         
-        self.view.window?.makeFirstResponder(self.mailAddressTextField)
         if (self.outlineTargetPageIndexTextField.stringValue == "0" || self.outlineTargetPageIndexTextField.stringValue.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) < 1){
             self.outlineTargetPageIndexTextField.stringValue = "\(self.currentPageIndex + 1)"
         }
     }
     
+    @objc private func _cancelAction() {
+        itemClick?(1)
+    }
+    
+    @objc private func _okAction() {
+        itemClick?(2, self)
+    }
 }
 
 extension KMOutlineEditViewController : NSTextFieldDelegate,NSTextViewDelegate {

+ 49 - 24
PDF Office/PDF Master/KMClass/Left/Outline/Controller/KMOutlineViewController.swift

@@ -34,6 +34,8 @@ class KMOutlineViewController: KMNBaseViewController {
     
     var handdler = KMNOutlineHanddler()
     
+    private weak var popover_: NSPopover?
+    
     private lazy var searchButton_: ComponentButton = {
         let view = ComponentButton()
         view.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, state: .normal, isDisable: false, onlyIcon: true)
@@ -300,20 +302,21 @@ class KMOutlineViewController: KMNBaseViewController {
     }
     
     func editOutlineUI(editVC : KMOutlineEditViewController!) {
-        if editVC.pageButton.state == NSControl.StateValue.on {
-            let index = Int(editVC.outlineTargetPageIndexTextField.stringValue)!
-            if editVC.originalDestination?.pageIndex != index {
-                let page = editVC.pdfView?.document.page(at: UInt(index))
+        if editVC.pageRadio.properties.checkboxType == .selected {
+            let index = Int(editVC.pageInput.properties.text) ?? 0
+            let pageIndex = max(0, index-1)
+            if editVC.originalDestination?.pageIndex != pageIndex {
+                let page = editVC.pdfView?.document.page(at: UInt(pageIndex))
                 if page != nil {
-                    let destination = CPDFDestination.init(document: editVC.pdfView!.document, pageIndex: index)
+                    let destination = CPDFDestination.init(document: editVC.pdfView!.document, pageIndex: pageIndex)
                     editVC.outline?.destination = destination
                 } else {
                     __NSBeep()
                 }
             }
-        } else if editVC.urlButton.state == NSControl.StateValue.on {
-            if editVC.originalURLString != editVC.outlineURLTextField.stringValue {
-                var urlString = editVC.outlineURLTextField.stringValue
+        } else if editVC.webRaido.properties.checkboxType == .selected {
+            if editVC.originalURLString != editVC.webInput.properties.text {
+                var urlString = editVC.webInput.properties.text
                 let tLowerUrl = urlString.lowercased()
                 if !tLowerUrl.hasPrefix("https://") && !tLowerUrl.hasPrefix("pf]://")  && !urlString.hasPrefix("https://") &&
                     urlString.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) > 0 {
@@ -322,8 +325,8 @@ class KMOutlineViewController: KMNBaseViewController {
                 let action = CPDFURLAction.init(url: urlString)
                 editVC.outline?.action = action
             }
-        } else if editVC.mailButton.state == NSControl.StateValue.on {
-            var mailString = editVC.mailAddressTextField.stringValue
+        } else if editVC.emailRadio.properties.checkboxType == .selected {
+            var mailString = editVC.emailInput.properties.text
             let tLowerStr = mailString.lowercased()
             if !tLowerStr.hasPrefix("mailto:") {
                 mailString = "mailto:\(mailString)"
@@ -545,21 +548,37 @@ extension KMOutlineViewController {
     }
     
     @objc func editItemAction() {
-        if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
-            if self.BOTAOutlineView.outlineView.rowView(atRow: self.BOTAOutlineView.outlineView.clickedRow, makeIfNecessary: true) != nil {
-                let cell = self.BOTAOutlineView.outlineView.rowView(atRow: self.BOTAOutlineView.outlineView.clickedRow, makeIfNecessary: true)
-                let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
-                let vc = KMOutlineEditViewController.init(outline: outlineItem.outline, document: self.handdler.pdfView)
-                vc.pageCount = handdler.pageCount()
-                let popover = NSPopover()
-                popover.delegate = self
-                popover.contentViewController = vc
-                popover.animates = true
-                popover.behavior = .applicationDefined
-                popover.show(relativeTo: cell!.bounds, of: cell!, preferredEdge: .minX)
+        let clickedRow = BOTAOutlineView.outlineView.clickedRow
+        if clickedRow < 0 {
+            NSSound.beep()
+            return
+        }
+        if let rowView = self.BOTAOutlineView.outlineView.rowView(atRow: clickedRow, makeIfNecessary: true) {
+            let item = self.BOTAOutlineView.outlineView.item(atRow: clickedRow) as? KMBOTAOutlineItem
+            let vc = KMOutlineEditViewController.init(outline: item?.outline, document: self.handdler.pdfView)
+            vc.pageCount = handdler.pageCount()
+            vc.itemClick = { [weak self] idx, params in
+                if idx == 1 {
+                    self?.popover_?.close()
+                } else if idx == 2 {
+                    self?.popover_?.close()
+                    
+                    if let viewC = params.first as? KMOutlineEditViewController {
+                        let resp = self?._showAlert(style: .informational, message: KMLocalizedString("Are you sure you want to apply edits to this outline?"), info: "", buttons: [KMLocalizedString("Apply"), KMLocalizedString("Cancel")])
+                        if resp == .alertFirstButtonReturn {
+                            self?.editOutlineUI(editVC: viewC)
+                        }
+                    }
+                }
             }
-        } else {
-            __NSBeep()
+            let popover = NSPopover()
+            popover_ = popover
+            popover.delegate = self
+            popover.contentViewController = vc
+            popover.animates = true
+            popover.behavior = .transient
+            popover.setValue(true, forKey: "shouldHideAnchor")
+            popover.show(relativeTo: rowView.bounds, of: rowView, preferredEdge: .minX)
         }
     }
     
@@ -895,6 +914,12 @@ extension KMOutlineViewController: NSPopoverDelegate {
             
         }
     }
+    
+    func popoverDidClose(_ notification: Notification) {
+        if popover_ == (notification.object as? NSPopover) {
+           popover_ = nil
+        }
+    }
 }
 
 //MARK: - NSMenuItemValidation