123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- //
- // KMConvertPageRangeSettingItemView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/12/6.
- //
- import Cocoa
- import KMComponentLibrary
- typealias KMConvertPageRangeSettingItemViewTextDidEndEditing = (_ textString: String) -> ()
- class KMConvertPageRangeSettingItemView: KMConvertBaseView {
-
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var pageSizeComboBox: NSComboBox!
- @IBOutlet weak var pageSizeBox: NSBox!
- @IBOutlet weak var pagesizeBoxHeight: NSLayoutConstraint!
-
- var pageSizeVC: KMDesignSelect?
- var pageRangeValue: String = ""
-
- var itemClick: KMCommonClickBlock?
- var textDidEndEditingCallback: KMConvertPageRangeSettingItemViewTextDidEndEditing?
-
- var isTextDidEndCallback: Bool = false
- var pageCount = 0
-
- private lazy var pageRangeView_: ComponentSelect = {
- let view = ComponentSelect()
-
- view.properties = ComponentSelectProperties(size: .s,
- state: .normal,
- isDisabled: false,
- isError: false,
- leftIcon: false,
- placeholder: nil,
- errorText: nil,
- creatable: false,
- text: KMLocalizedString("Odd Pages Only", comment: ""))
- view.delegate = self
- return view
- }()
-
- override func initSubViews() {
- super.initSubViews()
-
- self.pageSizeBox.contentView = self.pageRangeView_
-
- let strings = [KMTools.pageRangeTypeString(pageRange: .all),
- KMTools.pageRangeTypeString(pageRange: .current),
- KMTools.pageRangeTypeString(pageRange: .odd),
- KMTools.pageRangeTypeString(pageRange: .even),
- KMTools.pageRangeTypeString(pageRange: .custom)]
- var menuItemArr: [ComponentMenuitemProperty] = []
- for str in strings {
- let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
- itemSelected: false,
- isDisabled: false,
- keyEquivalent: nil,
- text: str)
- menuItemArr.append(itemProperty)
- }
-
- pageRangeView_.updateMenuItemsArr(menuItemArr)
-
- pageRangeView_.selectItemAtIndex(0)
- pageRangeView_.properties.creatable = false
- pageRangeView_.properties.text = menuItemArr.first?.text
- pageRangeView_.reloadData()
- }
-
- override func initDefaultVlaue() {
- super.initDefaultVlaue()
-
- self.pageSizeBox.fillColor = .clear
-
- self.titleLabel.stringValue = NSLocalizedString("Page Range", comment: "")
- self.titleLabel.textColor = NSColor.km_init(hex: "#616469")
- self.titleLabel.font = .SFProTextSemiboldFont(12)
- }
- }
- extension KMConvertPageRangeSettingItemView: KMSelectPopButtonDelegate {
- func km_controlTextDidChange(_ obj: KMDesignSelect) {
- if let vc = self.pageSizeVC {
- self.pageRangeValue = vc.stringValue
- }
- }
-
- func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
- guard let vc = self.pageSizeVC else {
- return
- }
-
- if (!self.isTextDidEndCallback) {
- return
- }
- if (vc.indexOfSelectedItem != (vc.numberOfItems-1)) {
- /// 其他item切换
- return
- }
- guard let callback = self.textDidEndEditingCallback else {
- return
- }
- callback(vc.stringValue)
- }
-
- func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
- guard let vc = self.pageSizeVC else {
- return
- }
- let index = vc.indexOfSelectedItem
- if index < 0 {
- return
- }
- if index == vc.numberOfItems-1 {
- self.pageSizeVC?.editable = true
- DispatchQueue.main.async {
- self.pageSizeVC?.stringValue = ""
- }
- self.window?.makeFirstResponder(self.pageSizeVC?.textField)
- } else {
- self.window?.makeFirstResponder(nil)
- self.pageSizeVC?.editable = false
- }
- guard let callback = self.itemClick else {
- return
- }
- callback(index)
- }
-
- func km_SelectPopoverWillShow(_ obj: KMDesignSelect) {
- if (self.pageCount <= 1) {
- self.pageSizeVC?.disItems = [KMTools.pageRangeTypeString(pageRange: .even)]
- } else {
- self.pageSizeVC?.disItems = []
- }
- }
- }
- extension KMConvertPageRangeSettingItemView: ComponentSelectDelegate {
- func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
- if(view == pageRangeView_) {
- let pageRangeSelectIndex = pageRangeView_.indexOfSelect()
- var isEditIndex:Int = 4
- if (pageRangeSelectIndex == isEditIndex) {
- pageRangeView_.properties.text = ""
- pageRangeView_.properties.placeholder = KMLocalizedString("e.g. 1,3-5,10")
- pageRangeView_.properties.creatable = true
- } else {
- pageRangeView_.properties.placeholder = ""
- pageRangeView_.properties.creatable = false
- }
- pageRangeView_.reloadData()
-
- guard let callback = self.itemClick else {
- return
- }
- callback(pageRangeSelectIndex)
- }
- }
-
- func componentSelectTextDidEndEditing(_ view: ComponentSelect) {
- guard let callback = self.textDidEndEditingCallback else {
- return
- }
- callback(view.properties.text ?? "")
- }
- }
|