// // KMBackgroundPropertyController.swift // PDF Master // // Created by tangchao on 2022/12/23. // import Cocoa protocol KMBackgroundPropertyDelegate: KMWatermarkAdjectivePropertyDelegate { } typealias KMBackgroundPropertyTemplateDidSelected = (_ model: KMBackgroundModel,_ isSelected: Bool)->() typealias KMBackgroundPropertyTemplateMenuItemClick = (_ itemIndex: Int, _ model: KMBackgroundModel)->() class KMBackgroundPropertyController: KMWatermarkAdjectivePropertyBaseController { var templateDidSelected: KMBackgroundPropertyTemplateDidSelected! var menuItemClick: KMBackgroundPropertyTemplateMenuItemClick! override func viewDidLoad() { super.viewDidLoad() self.showBack(show: false) self.titleLabel.stringValue = NSLocalizedString("Background", comment: "") self.segementControl.items = [NSLocalizedString("Color", comment: ""),NSLocalizedString("File", comment: "")] self.segementControl.itemClick = { [self] (index: Int) in self.tabView.selectTabViewItem(at: index) } let item = NSTabViewItem(identifier: "tabID") item.label = "tab1"; let controller = KMBackgroundListController() item.viewController = controller controller.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new background.", comment: "") controller.delegate = self let item2 = NSTabViewItem(identifier: "tablID2") item2.label = "tab2"; let controller2 = KMBackgroundListController() item2.viewController = controller2 controller2.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new background.", comment: "") controller2.delegate = self self.tabView.addTabViewItem(item) self.tabView.addTabViewItem(item2) childViewController.append(controller) childViewController.append(controller2) reloadDatas() } func findTemplateModel(in controller: KMBackgroundListController, index: Int) -> KMBackgroundModel? { if (index >= controller.datas.count) { return nil } return (controller.datas[index] as! KMBackgroundModel) } func deleteTemplateModel(_ templateModel: KMBackgroundModel) -> Bool { return KMBackgroundManager.defaultManager.deleteTemplate(model: templateModel) } func deleteTemplateModel(in controller: KMBackgroundListController, index: Int) { if (index >= controller.datas.count) { return } let templateModel: KMBackgroundModel = controller.datas[index] as! KMBackgroundModel if (deleteTemplateModel(templateModel)) { /// 删除成功,刷新数据 reloadDatas() } } func deleteAllTemplateModel(in controller: KMBackgroundListController) { if (controller.isEqual(to: childViewController.first)) { if (KMBackgroundManager.defaultManager.deleteAllColorTemplates()) { reloadDatas() } } else { if (KMBackgroundManager.defaultManager.deleteAllFileTemplates()) { reloadDatas() } } } func reloadDatas () { var colorModels: Array = [] var fileModels: Array = [] for model in KMBackgroundManager.defaultManager.datas { if (model.type == .file) { /// 文件 fileModels.append(model) } else { /// 颜色 colorModels.append(model) } } var count: Int = 0 for controller in childViewController { let listController: KMBackgroundListController = controller as! KMBackgroundListController if (count == 0) { listController.initDatas(datas: colorModels) } else { listController.initDatas(datas: fileModels) } count += 1 } } } extension KMBackgroundPropertyController: KMWatermarkAdjectiveListControllerDelegate { func controller(controller: KMWatermarkAdjectiveListController, shouldSelectAtRow: Int) -> Bool { return true } func controller(controller: KMWatermarkAdjectiveListController, didSelectAtRow: Int) { guard let callback = templateDidSelected else { return } let isSelected = controller.selectedTemplateID.isEmpty == false callback(findTemplateModel(in: controller as! KMBackgroundListController, index: didSelectAtRow)!, isSelected) } func controller(controller: KMWatermarkAdjectiveListController, didRemoveAtRow: Int) { deleteTemplateModel(in: controller as! KMBackgroundListController, index: didRemoveAtRow) } func controller(controller: KMWatermarkAdjectiveListController, didEditAtRow: Int) { guard let callback = menuItemClick else { return } callback(2, findTemplateModel(in: controller as! KMBackgroundListController, index: didEditAtRow)!) } func controller(controller: KMWatermarkAdjectiveListController, item: NSMenuItem, itemIndex: Int) { if (itemIndex == 1) { /// 删除 deleteTemplateModel(in: controller as! KMBackgroundListController, index: item.tag) return } if (itemIndex == 2) { /// 删除全部 let alert = NSAlert() alert.messageText = "确认删除全部模板吗?" alert.addButton(withTitle: "删除") alert.addButton(withTitle: "取消") let result = alert.runModal() if (result == .alertSecondButtonReturn) { /// 取消 return } deleteAllTemplateModel(in: controller as! KMBackgroundListController) return } /// 编辑 guard let callback = menuItemClick else { return } callback(2, findTemplateModel(in: controller as! KMBackgroundListController, index: item.tag)!) } }