KMBatesPropertyController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. //
  2. // KMBatesPropertyController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMBatesPropertyControllerDelegate: AnyObject {
  10. //水印数据有更新
  11. @objc optional func batesPropertyControllerDidUpdate(_ controller: KMBatesPropertyController)
  12. //切换到模板界面
  13. @objc optional func batesPropertyControllerDidChangetoTemplate(_ controller: KMBatesPropertyController)
  14. //成功保存到模板
  15. @objc optional func batesPropertyControllerSaveTemplateSuccess(_ controller: KMBatesPropertyController, _ data: KMBatesModel)
  16. //取消修改模板信息
  17. @objc optional func batesPropertyControllerCancelTemplateEdit(_ controller: KMBatesPropertyController)
  18. @objc optional func batesPropertyControllerFinishTemplateEdit(_ controller: KMBatesPropertyController)
  19. }
  20. class KMBatesPropertyController: NSViewController {
  21. @IBOutlet var contendView: NSView!
  22. @IBOutlet var infoScrollView: KMScrollView!
  23. @IBOutlet var infoContendBottomConst: NSLayoutConstraint!
  24. @IBOutlet var infoView: NSView!
  25. @IBOutlet var titleLabel: NSTextField!
  26. @IBOutlet var templateButton: ComponentButton!
  27. @IBOutlet var leftTopButton: ComponentButton!
  28. //Font
  29. @IBOutlet var fontBGView: NSView!
  30. @IBOutlet var fontLabel: NSTextField!
  31. @IBOutlet var fontNameSelect: ComponentSelect!
  32. @IBOutlet var fontSizeSelect: ComponentSelect!
  33. @IBOutlet var fontColorView: ComponentCColorCustom!
  34. //Margin
  35. @IBOutlet var marginBGView: NSView!
  36. @IBOutlet var marginLabel: NSTextField!
  37. @IBOutlet var marginLeftInput: ComponentInputNumber!
  38. @IBOutlet var marginTopInput: ComponentInputNumber!
  39. @IBOutlet var marginRightInput: ComponentInputNumber!
  40. @IBOutlet var marginBottomInput: ComponentInputNumber!
  41. //Display
  42. @IBOutlet var displayBGView: NSView!
  43. @IBOutlet var displayLabel: NSTextField!
  44. @IBOutlet var prefixLabel: NSTextField!
  45. @IBOutlet var prefixInput: ComponentInput!
  46. @IBOutlet var suffixLabel: NSTextField!
  47. @IBOutlet var suffixInput: ComponentInput!
  48. @IBOutlet var digitsLabel: NSTextField!
  49. @IBOutlet var digitsInputNumber: ComponentInputNumber!
  50. @IBOutlet var startPageLabel: NSTextField!
  51. @IBOutlet var startPageSelect: ComponentSelect!
  52. //Layout&Content
  53. @IBOutlet var layoutBGView: NSView!
  54. @IBOutlet var layoutLabel: NSTextField!
  55. @IBOutlet var layoutInsertButton: ComponentButton!
  56. @IBOutlet var layoutPosition: ComponentCPosition!
  57. @IBOutlet var layoutTextarea: ComponentTextarea!
  58. //Save
  59. @IBOutlet var saveTemplateBGView: NSView!
  60. @IBOutlet var saveButton: ComponentButton!
  61. var groupView: ComponentGroup!
  62. private var familyNames = CPDFFont.familyNames
  63. var positionSelRow: Int = 1
  64. var positionSelColumn: Int = 0
  65. var batesModel: KMBatesModel = KMBatesModel()
  66. var totalPDFCount: Int = 1
  67. var originalDataDict: Dictionary<String, Any>?
  68. public var editSubType: KMPDFEditSubModeType = .none
  69. public var isInBatchMode: Bool = false
  70. weak open var delegate: KMBatesPropertyControllerDelegate?
  71. override func viewDidLoad() {
  72. super.viewDidLoad()
  73. // Do view setup here.
  74. infoScrollView.contentViewSize = CGSizeMake(264, 648)
  75. infoScrollView.scrollerStyle = .overlay
  76. infoScrollView.documentView = infoView
  77. infoScrollView.layout()
  78. infoScrollView.scrollToTop()
  79. setupProperty()
  80. }
  81. func setupProperty() {
  82. contendView.wantsLayer = true
  83. contendView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  84. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  85. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  86. titleLabel.stringValue = KMLocalizedString("Add Bates Numbers")
  87. templateButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "icon_wm_template"), keepPressState: false)
  88. templateButton.setTarget(self, action: #selector(componentButtonClicked(_:)))
  89. leftTopButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "watermark_arrowLeft"), keepPressState: false)
  90. leftTopButton.setTarget(self, action: #selector(leftTopButtonClicked(_:)))
  91. //Font
  92. fontLabel.stringValue = KMLocalizedString("Font")
  93. fontLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  94. fontLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  95. fontNameSelect.properties = ComponentSelectProperties(size: .s,
  96. state: .normal,
  97. text: "")
  98. var menuItemArr: [ComponentMenuitemProperty] = []
  99. for string in familyNames {
  100. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  101. menuItemArr.append(item)
  102. }
  103. fontNameSelect.updateMenuItemsArr(menuItemArr)
  104. fontNameSelect.delegate = self
  105. fontSizeSelect.properties = ComponentSelectProperties(size: .s,
  106. state: .normal,
  107. creatable: true,
  108. text: "12 pt",
  109. regexString: "0123456789 pt")
  110. if true {
  111. var sizeItemArr: [ComponentMenuitemProperty] = []
  112. for string in KMHeaderFooterManager.getFontSize() {
  113. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  114. sizeItemArr.append(item)
  115. }
  116. fontSizeSelect.updateMenuItemsArr(sizeItemArr)
  117. }
  118. fontSizeSelect.delegate = self
  119. fontColorView.properties = ComponentCColorProperty(colorType: .colorPanel, state: .normal, isCustom: true, color: NSColor.black)
  120. //Margin
  121. marginLabel.stringValue = KMLocalizedString("Margin (mm)")
  122. marginLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  123. marginLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  124. marginLeftInput.properties = ComponentInputNumberProperty(alignment: .left,
  125. size: .s,
  126. minSize: 0,
  127. maxSize: 1000,
  128. text: "0")
  129. marginLeftInput.delegate = self
  130. marginTopInput.properties = ComponentInputNumberProperty(alignment: .left,
  131. size: .s,
  132. minSize: 0,
  133. maxSize: 1000,
  134. text: "0")
  135. marginTopInput.delegate = self
  136. marginRightInput.properties = ComponentInputNumberProperty(alignment: .left,
  137. size: .s,
  138. minSize: 0,
  139. maxSize: 1000,
  140. text: "0")
  141. marginRightInput.delegate = self
  142. marginBottomInput.properties = ComponentInputNumberProperty(alignment: .left,
  143. size: .s,
  144. minSize: 0,
  145. maxSize: 1000,
  146. text: "0")
  147. marginBottomInput.delegate = self
  148. //Display
  149. displayLabel.stringValue = KMLocalizedString("Display")
  150. displayLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  151. displayLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  152. prefixLabel.stringValue = KMLocalizedString("Prefix: ")
  153. prefixLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-nor")
  154. prefixLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  155. prefixInput.properties = ComponentInputProperty(size: .s, text: "")
  156. prefixInput.delegate = self
  157. suffixLabel.stringValue = KMLocalizedString("Suffix: ")
  158. suffixLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-nor")
  159. suffixLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  160. suffixInput.properties = ComponentInputProperty(size: .s, text: "")
  161. suffixInput.delegate = self
  162. digitsLabel.stringValue = KMLocalizedString("Numbers of Digits: ")
  163. digitsLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-nor")
  164. digitsLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  165. digitsInputNumber.properties = ComponentInputNumberProperty(alignment: .left, size: .s, minSize: 0, maxSize: 99, valueType: .intType)
  166. digitsInputNumber.delegate = self
  167. startPageLabel.stringValue = KMLocalizedString("Start Page:")
  168. startPageLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("comp-field/colorText-filled-nor")
  169. startPageLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  170. startPageSelect.properties = ComponentSelectProperties(size: .s,
  171. state: .normal,
  172. creatable: false,
  173. text: "1")
  174. startPageSelect.delegate = self
  175. //Layout&Content
  176. layoutLabel.stringValue = KMLocalizedString("Layout & Content")
  177. layoutLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  178. layoutLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  179. layoutInsertButton.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, onlyIcon: true, icon: NSImage(named: "watermark_template_add"), keepPressState: false)
  180. layoutInsertButton.setTarget(self, action: #selector(componentButtonClicked(_:)))
  181. layoutPosition.properties = ComponentCPositionProperty(rowCount: 2, columnCount: 3, dash: true)
  182. layoutPosition.delegate = self
  183. layoutTextarea.properties = ComponentTextareaProperty(totalCount: -1)
  184. layoutTextarea.delegate = self
  185. //Save
  186. saveTemplateBGView.wantsLayer = true
  187. saveTemplateBGView.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  188. saveButton.properties = ComponentButtonProperty(type: .default_tertiary, size: .m, showLeftIcon: true, buttonText: KMLocalizedString("Save as Template"), icon: NSImage(named: "icon_wm_template_save"), keepPressState: false)
  189. saveButton.setTarget(self, action: #selector(componentButtonClicked(_:)))
  190. }
  191. func clearData() {
  192. positionSelRow = 1
  193. positionSelColumn = 0
  194. totalPDFCount = 1
  195. }
  196. func reloadData() {
  197. titleLabel.stringValue = KMLocalizedString("Add Bates")
  198. leftTopButton.isHidden = true
  199. saveTemplateBGView.isHidden = false
  200. templateButton.properties.icon = NSImage(named: "icon_wm_template")
  201. if editSubType == .edit {
  202. titleLabel.stringValue = KMLocalizedString("Edit Bates")
  203. leftTopButton.isHidden = false
  204. saveTemplateBGView.isHidden = true
  205. templateButton.properties.icon = NSImage(named: "edit_save")
  206. }
  207. templateButton.reloadData()
  208. if self.isInBatchMode {
  209. infoContendBottomConst.constant = 0
  210. saveTemplateBGView.isHidden = true
  211. } else {
  212. infoContendBottomConst.constant = 72
  213. saveTemplateBGView.isHidden = false
  214. }
  215. //Font
  216. if let index = familyNames.firstIndex(of: batesModel.fontName){
  217. fontNameSelect.selectItemAtIndex(index)
  218. }
  219. fontNameSelect.properties.text = batesModel.fontName
  220. fontNameSelect.reloadData()
  221. let fontSizeStr = String(format: "%.0f", batesModel.fontsize)
  222. if let index = KMHeaderFooterManager.getFontSize().firstIndex(of: fontSizeStr) {
  223. fontSizeSelect.selectItemAtIndex(index)
  224. }
  225. fontSizeSelect.properties.text = String(format: "%.0f", batesModel.fontsize) + " pt"
  226. fontSizeSelect.reloadData()
  227. fontColorView.properties?.color = batesModel.color
  228. fontColorView.reloadData()
  229. //Margin
  230. marginLeftInput.properties.text = String(format: "%d", batesModel.leftMargin)
  231. marginLeftInput.reloadData()
  232. marginTopInput.properties.text = String(format: "%d", batesModel.topMargin)
  233. marginTopInput.reloadData()
  234. marginRightInput.properties.text = String(format: "%d", batesModel.rightMargin)
  235. marginRightInput.reloadData()
  236. marginBottomInput.properties.text = String(format: "%d", batesModel.bottomMargin)
  237. marginBottomInput.reloadData()
  238. //Display
  239. prefixInput.properties.text = batesModel.prefixString
  240. suffixInput.properties.text = batesModel.suffixString
  241. digitsInputNumber.properties.text = String(format: "%d", batesModel.digits)
  242. digitsInputNumber.reloadData()
  243. if true {
  244. var sizeItemArr: [ComponentMenuitemProperty] = []
  245. var indexArr: [String] = []
  246. for index in 0...totalPDFCount {
  247. indexArr.append(String(format: "%ld", index))
  248. }
  249. for string in indexArr {
  250. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  251. sizeItemArr.append(item)
  252. }
  253. startPageSelect.updateMenuItemsArr(sizeItemArr)
  254. }
  255. if let index = KMHeaderFooterManager.getPageFormats().firstIndex(of: batesModel.startString) {
  256. startPageSelect.selectItemAtIndex(index)
  257. }
  258. startPageSelect.properties.text = batesModel.startString
  259. startPageSelect.reloadData()
  260. //Layout
  261. layoutPosition.properties.selRow = positionSelRow
  262. layoutPosition.properties.selColumn = positionSelColumn
  263. layoutPosition.reloadData()
  264. layoutTextarea.properties.text = ""
  265. layoutTextarea.reloadData()
  266. updateTextAreaText()
  267. //Save
  268. if batesModel.topLeftString.count == 0 && batesModel.topCenterString.count == 0 && batesModel.topRightString.count == 0
  269. && batesModel.bottomLeftString.count == 0 && batesModel.bottomCenterString.count == 0 && batesModel.bottomRightString.count == 0 {
  270. saveButton.properties.isDisabled = true
  271. } else {
  272. saveButton.properties.isDisabled = false
  273. }
  274. saveButton.reloadData()
  275. }
  276. func updateTextAreaText() {
  277. if positionSelRow == 0 && positionSelColumn == 0 {
  278. //左下角
  279. layoutTextarea.properties.text = batesModel.bottomLeftString
  280. } else if positionSelRow == 0 && positionSelColumn == 1 {
  281. layoutTextarea.properties.text = batesModel.bottomCenterString
  282. } else if positionSelRow == 0 && positionSelColumn == 2 {
  283. layoutTextarea.properties.text = batesModel.bottomRightString
  284. } else if positionSelRow == 1 && positionSelColumn == 0 {
  285. layoutTextarea.properties.text = batesModel.topLeftString
  286. } else if positionSelRow == 1 && positionSelColumn == 1 {
  287. layoutTextarea.properties.text = batesModel.topCenterString
  288. } else if positionSelRow == 1 && positionSelColumn == 2 {
  289. layoutTextarea.properties.text = batesModel.topRightString
  290. }
  291. layoutTextarea.reloadData()
  292. layoutPosition.setString(batesModel.bottomLeftString, to: 0, 0)
  293. layoutPosition.setString(batesModel.bottomCenterString, to: 0, 1)
  294. layoutPosition.setString(batesModel.bottomRightString, to: 0, 2)
  295. layoutPosition.setString(batesModel.topLeftString, to: 1, 0)
  296. layoutPosition.setString(batesModel.topCenterString, to: 1, 1)
  297. layoutPosition.setString(batesModel.topRightString, to: 1, 2)
  298. }
  299. //MARK: - Action
  300. @objc func leftTopButtonClicked(_ sender: ComponentButton) {
  301. if sender == leftTopButton {
  302. var didChanged = false
  303. if let dict = self.originalDataDict {
  304. didChanged = KMBatesManager.compareIsChangedModel(batesModel, withDict: dict as NSDictionary)
  305. }
  306. if didChanged {
  307. let alert = NSAlert()
  308. alert.alertStyle = .critical
  309. alert.messageText = KMLocalizedString("Save template changes?")
  310. alert.informativeText = KMLocalizedString("Cancel and they will not be saved.")
  311. alert.addButton(withTitle: KMLocalizedString("Save"))
  312. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  313. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  314. if response == NSApplication.ModalResponse.alertFirstButtonReturn {
  315. let _ = KMBatesManager.defaultManager.updateTemplate(self.batesModel)
  316. self.delegate?.batesPropertyControllerFinishTemplateEdit?(self)
  317. } else {
  318. if let dict = self.originalDataDict {
  319. KMBatesManager.defaultManager.updateModel(self.batesModel, with: dict as NSDictionary)
  320. let _ = KMBatesManager.defaultManager.updateTemplate(self.batesModel)
  321. }
  322. self.delegate?.batesPropertyControllerCancelTemplateEdit?(self)
  323. }
  324. }
  325. } else {
  326. delegate?.batesPropertyControllerCancelTemplateEdit?(self)
  327. }
  328. }
  329. }
  330. @objc func componentButtonClicked(_ sender: ComponentButton) {
  331. if sender == templateButton {
  332. delegate?.batesPropertyControllerDidChangetoTemplate?(self)
  333. } else if sender == layoutInsertButton {
  334. var menuItemArr: [ComponentMenuitemProperty] = []
  335. let items: [(String, String)] = [("Insert Bates", "InsertBatesKey")]
  336. for (i, value) in items {
  337. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  338. itemSelected: false,
  339. isDisabled: false,
  340. keyEquivalent: nil,
  341. text: KMLocalizedString(i),
  342. identifier: value)
  343. menuItemArr.append(properties_Menuitem)
  344. }
  345. if groupView == nil {
  346. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  347. }
  348. groupView.groupDelegate = self
  349. groupView?.frame = CGRectMake(0, 0, 160, 44)
  350. groupView.updateGroupInfo(menuItemArr)
  351. if let point: CGPoint = sender.superview?.convert(sender.frame.origin, to: self.view.window?.contentView) {
  352. groupView.showWithPoint(CGPoint(x: point.x, y: point.y - CGRectGetHeight(groupView.frame)-4), relativeTo: self.view)
  353. }
  354. } else if sender == saveButton {
  355. self.saveAction()
  356. }
  357. }
  358. func saveAction() {
  359. batesModel.prefixString = prefixInput.properties.text
  360. batesModel.suffixString = suffixInput.properties.text
  361. let saveWindow: KMWatermarkSaveWindow = KMWatermarkSaveWindow(windowNibName: "KMWatermarkSaveWindow")
  362. saveWindow.nameValue = KMBatesManager.defaultManager.fetchBatesAvailableName()
  363. saveWindow.saveHandler = {[weak self] string in
  364. guard let weakSelf = self else { return }
  365. DispatchQueue.main.async {
  366. weakSelf.batesModel.name = string ?? ""
  367. if KMBatesManager.defaultManager.addTemplate(weakSelf.batesModel){
  368. weakSelf.delegate?.batesPropertyControllerSaveTemplateSuccess?(weakSelf, weakSelf.batesModel)
  369. }
  370. }
  371. }
  372. saveWindow.own_beginSheetModal(for: view.window) { string in
  373. }
  374. }
  375. //MARK: - Mouse
  376. override func mouseDown(with event: NSEvent) {
  377. super.mouseDown(with: event)
  378. view.window?.makeFirstResponder(nil)
  379. }
  380. }
  381. //MARK: - ComponentInputNumberDelegate
  382. extension KMBatesPropertyController: ComponentInputNumberDelegate {
  383. func componentInputNumberDidValueChanged(inputNumber: ComponentInputNumber?) {
  384. if inputNumber == marginLeftInput {
  385. if let text = inputNumber?.properties.text {
  386. batesModel.leftMargin = Int(text) ?? 30
  387. }
  388. } else if inputNumber == marginTopInput {
  389. if let text = inputNumber?.properties.text {
  390. batesModel.topMargin = Int(text) ?? 30
  391. }
  392. } else if inputNumber == marginRightInput {
  393. if let text = inputNumber?.properties.text {
  394. batesModel.rightMargin = Int(text) ?? 30
  395. }
  396. } else if inputNumber == marginBottomInput {
  397. if let text = inputNumber?.properties.text {
  398. batesModel.bottomMargin = Int(text) ?? 30
  399. }
  400. } else if inputNumber == digitsInputNumber {
  401. if let text = inputNumber?.properties.text {
  402. batesModel.digits = Int(text) ?? 1
  403. }
  404. }
  405. reloadData()
  406. delegate?.batesPropertyControllerDidUpdate?(self)
  407. }
  408. }
  409. //MARK: - ComponentInputDelegate
  410. extension KMBatesPropertyController: ComponentInputDelegate {
  411. }
  412. //MARK: - ComponentSelectDelegate
  413. extension KMBatesPropertyController: ComponentSelectDelegate {
  414. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  415. if view == fontNameSelect {
  416. batesModel.fontName = menuItemProperty?.text ?? ""
  417. } else if view == fontSizeSelect {
  418. if let text = menuItemProperty?.text {
  419. let result = text.stringByDeleteCharString(" pt")
  420. batesModel.fontsize = result.stringToCGFloat()
  421. }
  422. } else if view == startPageSelect {
  423. if let text = menuItemProperty?.text {
  424. batesModel.startString = text
  425. }
  426. }
  427. reloadData()
  428. delegate?.batesPropertyControllerDidUpdate?(self)
  429. }
  430. }
  431. //MARK: - ComponentGroupDelegate
  432. extension KMBatesPropertyController: ComponentGroupDelegate {
  433. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  434. if menuItemProperty?.identifier == "InsertBatesKey" {
  435. let digitsText = digitsInputNumber.properties.text
  436. guard let startPageText = startPageSelect.properties.text else {
  437. return
  438. }
  439. batesModel.digits = Int(digitsText) ?? 1
  440. var tString = "<<#\((digitsText))#\(startPageText)"
  441. if !self.prefixInput.properties.text.isEmpty {
  442. tString += "#\(self.prefixInput.properties.text)"
  443. } else if self.prefixInput.properties.text.isEmpty && !self.suffixInput.properties.text.isEmpty {
  444. tString += "#"
  445. }
  446. if !self.suffixInput.properties.text.isEmpty {
  447. tString += "#\(self.suffixInput.properties.text)"
  448. }
  449. tString += ">>"
  450. if positionSelRow == 0 && positionSelColumn == 0 {
  451. //左下角
  452. batesModel.bottomLeftString = batesModel.bottomLeftString + tString
  453. } else if positionSelRow == 0 && positionSelColumn == 1 {
  454. batesModel.bottomCenterString = batesModel.bottomCenterString + tString
  455. } else if positionSelRow == 0 && positionSelColumn == 2 {
  456. batesModel.bottomRightString = batesModel.bottomRightString + tString
  457. } else if positionSelRow == 1 && positionSelColumn == 0 {
  458. batesModel.topLeftString = batesModel.topLeftString + tString
  459. } else if positionSelRow == 1 && positionSelColumn == 1 {
  460. batesModel.topCenterString = batesModel.topCenterString + tString
  461. } else if positionSelRow == 1 && positionSelColumn == 2 {
  462. batesModel.topRightString = batesModel.topRightString + tString
  463. }
  464. }
  465. reloadData()
  466. delegate?.batesPropertyControllerDidUpdate?(self)
  467. }
  468. }
  469. //MARK: - ComponentCPositionDelegate
  470. extension KMBatesPropertyController: ComponentCPositionDelegate {
  471. func componentCPositionDidChoose(_ view: NSView, _ row: Int, _ column: Int) {
  472. if view == layoutPosition {
  473. view.window?.makeFirstResponder(nil)
  474. positionSelRow = row
  475. positionSelColumn = column
  476. reloadData()
  477. delegate?.batesPropertyControllerDidUpdate?(self)
  478. }
  479. }
  480. }
  481. //MARK: - ComponentTextareaDelegate
  482. extension KMBatesPropertyController: ComponentTextareaDelegate {
  483. func componentTextareaTextDidChange(_ view: ComponentTextarea) {
  484. if positionSelRow == 0 && positionSelColumn == 0 {
  485. //左下角
  486. batesModel.bottomLeftString = layoutTextarea.properties.text
  487. } else if positionSelRow == 0 && positionSelColumn == 1 {
  488. batesModel.bottomCenterString = layoutTextarea.properties.text
  489. } else if positionSelRow == 0 && positionSelColumn == 2 {
  490. batesModel.bottomRightString = layoutTextarea.properties.text
  491. } else if positionSelRow == 1 && positionSelColumn == 0 {
  492. batesModel.topLeftString = layoutTextarea.properties.text
  493. } else if positionSelRow == 1 && positionSelColumn == 1 {
  494. batesModel.topCenterString = layoutTextarea.properties.text
  495. } else if positionSelRow == 1 && positionSelColumn == 2 {
  496. batesModel.topRightString = layoutTextarea.properties.text
  497. }
  498. reloadData()
  499. delegate?.batesPropertyControllerDidUpdate?(self)
  500. }
  501. func componentTextareaTextDidEndEditing(_ view: ComponentTextarea) {
  502. }
  503. }