// // KMPDFMergePageRangeTabelViewCell.swift // PDF Master // // Created by tangchao on 2023/10/11. // import Cocoa @objcMembers class KMPDFMergePageRangeTabelViewCell: NSTableCellView { var pageCount: Int = 0 var callback: ((Int) -> Void)? var textDidChange: ((String) -> Void)? var textDidEndEdit: ((String, KMPDFMergePageRangeTabelViewCell) -> Void)? var comboBox: KMMergeSelect? override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.initSubviews() } required init?(coder: NSCoder) { super.init(coder: coder) self.initSubviews() } override var isFlipped: Bool { return true } func initSubviews() { let comboBox = KMMergeSelect(withType: .Combox) self.addSubview(comboBox.view) self.comboBox = comboBox self.comboBox?.removeAllItems() self.comboBox?.addItems(withObjectValues: [KMLocalizedString("All Pages", nil),KMLocalizedString("Odd Pages", nil),KMLocalizedString("Even Pages", nil),KMLocalizedString("Customized", nil)]) self.comboBox?.stringValue = KMLocalizedString("All Pages", nil) self.comboBox?.editable = false self.comboBox?.delete = self self.comboBox?.placeholderString = KMLocalizedString("eg .1,3-5,10", nil) } override func layout() { super.layout() let width = NSWidth(self.bounds) let height = NSHeight(self.bounds) let comboBoxH = 26.0 self.comboBox?.view.frame = NSMakeRect(10, (height-comboBoxH)*0.5, width-10, comboBoxH) } func updatePageRange(_ pageRange: KMPageRange, pageStrings: String, isFirstResponser: Bool = true) { if (pageRange == .all) { self.comboBox?.stringValue = KMLocalizedString("All Pages", nil) self.comboBox?.editable = false self.comboBox?.alert = false } else if (pageRange == .odd) { self.comboBox?.stringValue = KMLocalizedString("Odd Pages", nil) self.comboBox?.editable = false self.comboBox?.alert = false } else if (pageRange == .even) { self.comboBox?.stringValue = KMLocalizedString("Even Pages", nil) self.comboBox?.editable = false self.comboBox?.alert = false } else { self.comboBox?.stringValue = pageStrings self.comboBox?.editable = true if (pageStrings.isEmpty) { self.comboBox?.alert = true } else { self.comboBox?.alert = false } if (isFirstResponser || pageStrings.isEmpty) { if let data = self.window?.firstResponder, data.isEqual(to: self.comboBox?.textField) == false { if (Thread.isMainThread) { self.window?.makeFirstResponder(self.comboBox?.textField) } else { DispatchQueue.main.async { self.window?.makeFirstResponder(self.comboBox?.textField) } } } } } } /* - (void)updateRageRangeState:(NSInteger)state { if (state == 9) { self.comboBox.state = KMDesignTokenStateError_def; } else if (state == 10) { self.comboBox.state = KMDesignTokenStateError_focus; } } */ } extension KMPDFMergePageRangeTabelViewCell: KMSelectPopButtonDelegate { func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) { guard let data = self.callback else { return } var index = obj.indexOfSelectedItem if (index < 0) { index = 0 } if (index == obj.numberOfItems-1) { self.comboBox?.editable = true DispatchQueue.main.async { self.comboBox?.stringValue = "" } self.window?.makeFirstResponder(obj.textField) } else { self.comboBox?.editable = false self.comboBox?.alert = false } data(index) } func km_controlTextDidChange(_ obj: KMDesignSelect) { guard let data = self.textDidChange else { return } data(obj.stringValue) } func km_controlTextDidEndEditing(_ obj: KMDesignSelect) { guard let data = self.textDidEndEdit else { return } data(obj.stringValue, self) } func km_SelectPopoverWillShow(_ obj: KMDesignSelect) { if (obj.isEqual(to: self.comboBox)) { var disableItems: [String] = [] if (self.pageCount <= 1) { disableItems.append(KMLocalizedString("Even Pages", nil)) } self.comboBox?.disItems = disableItems } } }