// // KMConvertImageSettingView.swift // PDF Reader Pro // // Created by tangchao on 2022/12/6. // import KMComponentLibrary typealias KMConvertImageSettingViewImageTypeAction = (_ selectedIndex: Int) -> Void typealias KMConvertImageSettingViewResolutionAction = (_ selectedIndex: Int) -> Void class KMConvertImageSettingView: KMConvertSettingView { @IBOutlet weak var fileTypeTitleLabel: NSTextField! @IBOutlet weak var formatBox: NSBox! @IBOutlet weak var pageRangeBox: NSBox! @IBOutlet weak var dpiTitleLabel: NSTextField! @IBOutlet weak var dpiSelectBox: NSBox! var selectedFormatIndex = 0 var selectedResolutionIndex = 3 var imageTypeAction: KMConvertImageSettingViewImageTypeAction? var resolutionAction: KMConvertImageSettingViewResolutionAction? private lazy var formatSelect_: ComponentSelect = { let view = ComponentSelect() view.properties = ComponentSelectProperties(size: .s, state: .normal, isDisabled: false, isError: false, leftIcon: false, placeholder: nil, errorText: nil, creatable: false, text: NSLocalizedString("", comment: "")) view.delegate = self return view }() private lazy var dpiSelect_: ComponentSelect = { let view = ComponentSelect() view.properties = ComponentSelectProperties(size: .s, state: .normal, isDisabled: false, isError: false, leftIcon: false, placeholder: nil, errorText: nil, creatable: false, text: NSLocalizedString("", comment: "")) view.delegate = self return view }() override func initSubViews() { super.initSubViews() self.formatBox.contentView = self.formatSelect_ var menuItemArr: [ComponentMenuitemProperty] = [] for string in ["JPEG","JPG","PNG","GIF","TIFF","TGA","BMP","JPEG-2000"] { let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: string) menuItemArr.append(itemProperty) } formatSelect_.updateMenuItemsArr(menuItemArr) formatSelect_.selectItemAtIndex(0) formatSelect_.properties.creatable = false formatSelect_.properties.text = "JPEG" formatSelect_.reloadData() self.dpiSelectBox.contentView = self.dpiSelect_ var menuItemArr2: [ComponentMenuitemProperty] = [] for string in ["50 dpi", "72 dpi", "96 dpi", "150 dpi", "300 dpi", "600 dpi"] { let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false, itemSelected: false, isDisabled: false, keyEquivalent: nil, text: string) menuItemArr2.append(itemProperty) } dpiSelect_.updateMenuItemsArr(menuItemArr2) dpiSelect_.selectItemAtIndex(3) dpiSelect_.properties.creatable = false dpiSelect_.properties.text = "150 dpi" dpiSelect_.reloadData() self.tipView?.km_add_left_constraint(constant: 8) self.tipView?.km_add_top_constraint(equalTo: self.pageRangeBox, attribute: .bottom, constant: -5) self.tipView?.km_add_right_constraint() self.tipView?.km_add_height_constraint(constant: 16+20+16) } override func initDefaultVlaue() { self.formatBox.fillColor = .clear self.fileTypeTitleLabel.stringValue = NSLocalizedString("Format", comment: "") self.fileTypeTitleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium") self.formatBox.fillColor = .clear } override func viewDidMoveToWindow() { super.viewDidMoveToWindow() if (self.pageRangeItemView == nil) { self.pageRangeItemView = KMConvertPageRangeSettingItemView.createFromNib() self.pageRangeBox.contentView = self.pageRangeItemView self.pageRangeItemView?.titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium") self.updateViewColor() } self.pageRangeItemView?.pageCount = self.pageCount self.pageRangeItemView?.isHidden = self.isBatch } override func getPageRangeString() -> String { if let view = self.pageRangeItemView { return view.pageRangeValue } return "" } override func updateViewColor() { super.updateViewColor() KMMainThreadExecute { self.fileTypeTitleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") self.pageRangeItemView?.titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2") } } override func reloadData() { formatSelect_.properties.isDisabled = self.isDisable dpiSelect_.properties.isDisabled = self.isDisable } func selectConvertType(convertType: KMPDFConvertType) { if convertType == .jpeg { self.selectedFormatIndex = 0 } else if convertType == .jpg { self.selectedFormatIndex = 1 } else if convertType == .png { self.selectedFormatIndex = 2 } else if convertType == .gif { self.selectedFormatIndex = 3 } else if convertType == .tiff { self.selectedFormatIndex = 4 } else if convertType == .tga { self.selectedFormatIndex = 5 } else if convertType == .bmp { self.selectedFormatIndex = 6 } else if convertType == .jp2 { self.selectedFormatIndex = 7 } formatSelect_.selectItemAtIndex(self.selectedFormatIndex) } } extension KMConvertImageSettingView: ComponentSelectDelegate { func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) { guard let index = view?.indexOfSelect(), index >= 0 else { return } if self.formatSelect_.isEqual(to: view) { self.selectedFormatIndex = index imageTypeAction?(self.selectedFormatIndex) } else if self.dpiSelect_.isEqual(to: view) { self.selectedResolutionIndex = max(index, 0) resolutionAction?(self.selectedResolutionIndex) } } }