// // KMCompressContentView.swift // PDF Reader Pro // // Created by lizhe on 2024/11/18. // import Cocoa import KMComponentLibrary typealias KMBatchOperateCompressViewSelectTypeBlcok = (_ view: KMCompressContentView, _ model: KMCompressSettingModel) -> Void typealias KMBatchOperateCompressViewCompressAction = (_ view: KMCompressContentView, _ model: KMCompressSettingModel, _ showView: NSView) -> Void class KMCompressContentView: BaseXibView { @IBOutlet weak var standardButton: ComponentRadio! @IBOutlet weak var mobileButton: ComponentRadio! @IBOutlet weak var customButton: ComponentRadio! @IBOutlet weak var tipButton: ComponentToolTipsHelp! @IBOutlet weak var settingButton: NSButton! @IBOutlet weak var mobileWidthConstraint: NSLayoutConstraint! @IBOutlet weak var customButtonWidthConstraint: NSLayoutConstraint! var isDisable: Bool = false { didSet { self.reloadData() } } var compressSettingViewWindow: NSWindow? var faterWindow: NSWindow? var compressSettingViewController: KMCompressSettingViewController? var compressAction: KMBatchOperateCompressViewCompressAction? var popOver: NSPopover! var selectTypeAction: KMBatchOperateCompressViewSelectTypeBlcok? var model: KMCompressSettingModel = KMCompressSettingModel(modelsType: .standard) { didSet { self.reloadData() } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } override func setup() { updateUI() updateUILanguage() reloadData() } override func updateUILanguage() { standardButton.properties.text = KMLocalizedString("Standard (Recommend)", comment: "") mobileButton.properties.text = KMLocalizedString("Mobile", comment: "") customButton.properties.text = KMLocalizedString("Custom", comment: "") settingButton.stringValue = KMLocalizedString("Setting", comment: "") tipButton.toolTip = KMLocalizedString("This mode is specifically tuned for mobile devices to improve compression quality and ensure clear viewing on mobile devices.", comment: "") } override func updateUIThemeColor() { settingButton.setTitleColor(NSColor(hex: "#1EB3F9")) } func updateUI() { standardButton.properties = ComponentCheckBoxProperty(size: .s, text: KMLocalizedString("Standard (Recommend)", comment: ""), checkboxType: .normal) standardButton.setTarget(self, action: #selector(standardButtonAction(_:))) mobileButton.properties = ComponentCheckBoxProperty(size: .s, text: KMLocalizedString("Mobile", comment: ""), checkboxType: .normal) mobileButton.setTarget(self, action: #selector(mobileButtonAction(_:))) mobileWidthConstraint.constant = mobileButton.properties.propertyInfo.viewWidth customButton.properties = ComponentCheckBoxProperty(size: .s, text: KMLocalizedString("Custom", comment: ""), checkboxType: .normal) customButton.setTarget(self, action: #selector(customButtonAction(_:))) customButtonWidthConstraint.constant = mobileButton.properties.propertyInfo.viewWidth } func reloadData() { self.updateType(type: model.modelsType) self.standardButton.properties.isDisabled = self.isDisable self.standardButton.reloadData() self.mobileButton.properties.isDisabled = self.isDisable self.mobileButton.reloadData() self.customButton.properties.isDisabled = self.isDisable self.customButton.reloadData() self.settingButton.isEnabled = !self.isDisable } func updateType(type: KMCompressModelsType) { self.standardButton.properties.checkboxType = .normal self.mobileButton.properties.checkboxType = .normal self.customButton.properties.checkboxType = .normal switch type { case .standard: self.standardButton.properties.checkboxType = .selected case .custom: self.customButton.properties.checkboxType = .selected case .mobile: self.mobileButton.properties.checkboxType = .selected } self.standardButton.reloadData() self.mobileButton.reloadData() self.customButton.reloadData() } } extension KMCompressContentView { func standardButtonAction(_ sender: Any) { self.updateType(type: .standard) self.model = KMCompressSettingModel(modelsType: .standard) guard let callBack = selectTypeAction else { return } callBack(self, model) } func mobileButtonAction(_ sender: Any) { self.updateType(type: .mobile) self.model = KMCompressSettingModel(modelsType: .mobile) guard let callBack = selectTypeAction else { return } callBack(self, model) } func customButtonAction(_ sender: Any) { self.updateType(type: .custom) self.model = KMCompressSettingModel(modelsType: .custom) if !KMCompressSettingModel.isSaveCustomData() { self.settingButtonAction(self.settingButton) } guard let callBack = selectTypeAction else { return } callBack(self, model) } @IBAction func settingButtonAction(_ sender: Any) { compressSettingViewController = KMCompressSettingViewController(nibName: "KMCompressSettingViewController", bundle: nil) let window = NSWindow(contentViewController: compressSettingViewController!) window.minSize = CGSizeMake(584, 640) window.maxSize = CGSizeMake(584, 640) window.styleMask.remove(.resizable) compressSettingViewWindow = window let currentWindow = NSWindow.currentWindow() faterWindow = currentWindow currentWindow.beginSheet(window) compressSettingViewController?.model = self.model compressSettingViewController?.cancelAction = { resultControler in currentWindow.endSheet(window) } compressSettingViewController?.doneAction = { [unowned self] resultControler in guard let callBack = compressAction else { return } callBack(self, self.model, resultControler.view) } compressSettingViewController?.modelChangeAction = { [unowned self] resultControler, tModel in self.updateType(type: tModel.modelsType) guard let callBack = selectTypeAction else { return } callBack(self, tModel) } } func closeSettingWindow() { guard let window = compressSettingViewWindow else { return } guard let faterWindow = faterWindow else { return } faterWindow.endSheet(window) } }