KMHeaderFooterManagerWindowController.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // KMHeaderFooterManagerWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/6.
  6. //
  7. import Cocoa
  8. typealias HeaderFooterOperateCallBack = (_ obj: KMHeaderFooterObject) -> ()
  9. class KMHeaderFooterManagerWindowController: NSWindowController{
  10. var operateCallBack: HeaderFooterOperateCallBack?
  11. var isBates: Bool = false
  12. var onlyManagerTemplate: Bool = false
  13. lazy var filePath: String = {
  14. var str = Bundle.main.path(forResource: NSLocalizedString("Quick Start Guide.pdf", comment: ""), ofType: "")
  15. return str ?? ""
  16. }()
  17. var password: String = ""
  18. var type:KMBatchModifyTemplateType = .Add
  19. var pdfdocument: CPDFDocument?
  20. lazy var headerFooterObj: KMHeaderFooterObject? = {
  21. let obj = KMHeaderFooterObject()
  22. // obj.isBates = self.isBates
  23. if self.isBates {
  24. // obj.id = KMHeaderFooterManager.defaultManager.fetchBatesAvailableName()
  25. }else {
  26. // obj.id = KMHeaderFooterManager.defaultManager.fetchHeaderFooterAvailableName()
  27. }
  28. // initialID = obj.id
  29. return obj
  30. }()
  31. var originalHeaderFooterObj: KMHeaderFooterObject?
  32. lazy var dateFormatArray: Array<Any> = {
  33. let arr = KMHeaderFooterManager.getdateFormatArray()
  34. return arr
  35. }()
  36. var initialID: String?
  37. @IBOutlet var pdfView: KMWatermarkPDFView!
  38. @IBOutlet var currentPageIndexTextField: NSTextField!
  39. @IBOutlet var totalPageCountLabel: NSTextField!
  40. @IBOutlet var textBox: NSBox!
  41. @IBOutlet var textLabel: NSTextField!
  42. @IBOutlet var fontSizeLabel: NSTextField!
  43. @IBOutlet var fontSizeCombobox: NSComboBox!
  44. @IBOutlet var textColorLabel: NSTextField!
  45. @IBOutlet var textColorWell: NSColorWell!
  46. @IBOutlet var marginBox: NSBox!
  47. @IBOutlet var marginLabel: NSTextField!
  48. @IBOutlet var topMarginLabel: NSTextField!
  49. @IBOutlet var topMargintextField: NSTextField!
  50. @IBOutlet var topMarginStepper: NSStepper!
  51. @IBOutlet var bottomMarginLabel: NSTextField!
  52. @IBOutlet var bottomMarginTextField: NSTextField!
  53. @IBOutlet var bottomMarginStepper: NSStepper!
  54. @IBOutlet var leftMarginLabel: NSTextField!
  55. @IBOutlet var leftMarginTextField: NSTextField!
  56. @IBOutlet var leftMarginStepper: NSStepper!
  57. @IBOutlet var rightMarginLabel: NSTextField!
  58. @IBOutlet var rightMarginTextField: NSTextField!
  59. @IBOutlet var rightMarginStepper: NSStepper!
  60. @IBOutlet var formatBox: NSBox!
  61. @IBOutlet var formatLabel: NSTextField!
  62. @IBOutlet var dateFormateLabel: NSTextField!
  63. @IBOutlet var dateFormateCombobox: NSComboBox!
  64. @IBOutlet var pageLabel: NSTextField!
  65. @IBOutlet var pageCombobox: NSComboBox!
  66. @IBOutlet var beginPageLabel: NSTextField!
  67. @IBOutlet var beginPageCombobox: NSComboBox!
  68. @IBOutlet var topLeftLabel: NSTextField!
  69. @IBOutlet var topLeftTextView: NSTextView!
  70. @IBOutlet var topCenterLabel: NSTextField!
  71. @IBOutlet var topCenterTextView: NSTextView!
  72. @IBOutlet var topRightLabel: NSTextField!
  73. @IBOutlet var topRightTextView: NSTextView!
  74. @IBOutlet var bottomLeftLabel: NSTextField!
  75. @IBOutlet var bottomLeftTextView: NSTextView!
  76. @IBOutlet var bottomCenterLabel: NSTextField!
  77. @IBOutlet var BottomCenterTextView: NSTextView!
  78. @IBOutlet var bottomRightLabel: NSTextField!
  79. @IBOutlet var bottomRightTextView: NSTextView!
  80. @IBOutlet var pageRangeLabel: NSTextField!
  81. @IBOutlet var pageRangeCombobox: NSComboBox!
  82. @IBOutlet var saveToTemplateButton: NSButton!
  83. @IBOutlet var templateNameLabel: NSTextField!
  84. @IBOutlet var templateNameTextField: NSTextField!
  85. @IBOutlet var topLeftButton: NSButton!
  86. @IBOutlet var topCentButton: NSButton!
  87. @IBOutlet var topRightButton: NSButton!
  88. @IBOutlet var bottomLeftButton: NSButton!
  89. @IBOutlet var bottomCenterButton: NSButton!
  90. @IBOutlet var bottomRightButton: NSButton!
  91. @IBOutlet var batchButton: NSButton!
  92. @IBOutlet var cancelButton: NSButton!
  93. @IBOutlet var applyButton: NSButton!
  94. @IBOutlet var batesBox: NSBox!
  95. @IBOutlet var prefixLabel: NSTextField!
  96. @IBOutlet var prefixTextField: NSTextField!
  97. @IBOutlet var suffixLabel: NSTextField!
  98. @IBOutlet var suffixTextField: NSTextField!
  99. @IBOutlet var batesNumberLabel: NSTextField!
  100. @IBOutlet var batesNumberTextField: NSTextField!
  101. @IBOutlet var batesNumberStepper: NSStepper!
  102. @IBOutlet var batesStartPageLabel: NSTextField!
  103. @IBOutlet var batesStartpageCombobox: NSComboBox!
  104. @IBOutlet var constraint: NSLayoutConstraint!
  105. deinit {
  106. NotificationCenter.default.removeObserver(self)
  107. DistributedNotificationCenter.default().removeObserver(self)
  108. }
  109. convenience init(baseFile:String, obj:KMHeaderFooterObject, password:String, type:KMBatchModifyTemplateType) {
  110. self.init(windowNibName: "KMHeaderFooterManagerWindowController")
  111. self.filePath = baseFile
  112. self.password = password
  113. self.headerFooterObj = obj
  114. self.originalHeaderFooterObj = obj
  115. // self.initialID = obj.id
  116. self.type = type
  117. self.pdfdocument = CPDFDocument(url: URL(fileURLWithPath: self.filePath))
  118. if let data = self.pdfdocument?.isLocked, data {
  119. self.pdfdocument?.unlock(withPassword: password)
  120. }
  121. if let data = self.pdfdocument?.isLocked, data {
  122. return
  123. }
  124. }
  125. override func windowDidLoad() {
  126. super.windowDidLoad()
  127. configuUI()
  128. loadData()
  129. NotificationCenter.default.addObserver(self, selector: #selector(pageChangeNotification(notification:)), name: NSNotification.Name.CPDFViewPageChanged, object: nil)
  130. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: Notification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  131. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  132. }
  133. func configuUI() {
  134. topLeftButton.wantsLayer = true
  135. topCentButton.wantsLayer = true
  136. topRightButton.wantsLayer = true
  137. bottomLeftButton.wantsLayer = true
  138. bottomCenterButton.wantsLayer = true
  139. bottomRightButton.wantsLayer = true
  140. topLeftButton.layer?.borderWidth = 1.0
  141. topCentButton.layer?.borderWidth = 1.0
  142. topRightButton.layer?.borderWidth = 1.0
  143. bottomLeftButton.layer?.borderWidth = 1.0
  144. bottomCenterButton.layer?.borderWidth = 1.0
  145. bottomRightButton.layer?.borderWidth = 1.0
  146. topLeftButton.layer?.cornerRadius = 1.0
  147. topCentButton.layer?.cornerRadius = 1.0
  148. topRightButton.layer?.cornerRadius = 1.0
  149. bottomLeftButton.layer?.cornerRadius = 1.0
  150. bottomCenterButton.layer?.cornerRadius = 1.0
  151. bottomRightButton.layer?.cornerRadius = 1.0
  152. updateViewColor()
  153. let textViewArr: [NSTextView] = [topLeftTextView, topCenterTextView, topRightTextView, bottomLeftTextView, BottomCenterTextView, bottomRightTextView]
  154. for textView in textViewArr {
  155. textView.wantsLayer = true
  156. // textView.layer?.borderWidth = 1.0
  157. // textView.layer?.borderColor = NSColor.gridColor.cgColor
  158. }
  159. // textColorWell.color = headerFooterObj?.getTextColor() ?? .black
  160. pdfView.document = pdfdocument
  161. // pdfView. = headerFooterObj
  162. pdfView.autoScales = true
  163. // pdfView.documentView?.enclosingScrollView?.hasVerticalScroller = false
  164. // pdfView.documentView?.enclosingScrollView?.hasHorizontalScroller = false
  165. //
  166. // currentPageIndexTextField.stringValue = NSLocalizedString("1", comment: "")
  167. // batesStartPageLabel.stringValue = String(format: "%@:", NSLocalizedString("Start Page Number", comment: ""))
  168. // let numberFormatter = currentPageIndexTextField.formatter as? NSNumberFormatter
  169. // numberFormatter?.maximum = NSNumber(value: pdfdocument.pageCount)
  170. // totalPageCountLabel.stringValue = String(format: "/ %ld", pdfdocument.pageCount)
  171. //
  172. // previewLabel.stringValue = NSLocalizedString("Preview", comment: "")
  173. textLabel.stringValue = NSLocalizedString("Font Size", comment: "")
  174. fontSizeLabel.stringValue = String(format: "%@:", NSLocalizedString("Size", comment: ""))
  175. textColorLabel.stringValue = String(format: "%@:", NSLocalizedString("Color", comment: ""))
  176. marginLabel.stringValue = NSLocalizedString("Margin", comment: "")
  177. topMarginLabel.stringValue = String(format: "%@:", NSLocalizedString("Top", comment: ""))
  178. bottomMarginLabel.stringValue = String(format: "%@:", NSLocalizedString("Bottom", comment: ""))
  179. leftMarginLabel.stringValue = String(format: "%@:", NSLocalizedString("Left", comment: ""))
  180. rightMarginLabel.stringValue = String(format: "%@:", NSLocalizedString("Right", comment: ""))
  181. topMarginLabel.toolTip = String(format: "%@:", NSLocalizedString("Top", comment: ""))
  182. bottomMarginLabel.toolTip = String(format: "%@:", NSLocalizedString("Bottom", comment: ""))
  183. leftMarginLabel.toolTip = String(format: "%@:", NSLocalizedString("Left", comment: ""))
  184. rightMarginLabel.toolTip = String(format: "%@:", NSLocalizedString("Right", comment: ""))
  185. topMarginLabel.allowsExpansionToolTips = true
  186. bottomMarginLabel.allowsExpansionToolTips = true
  187. leftMarginLabel.allowsExpansionToolTips = true
  188. rightMarginLabel.allowsExpansionToolTips = true
  189. formatLabel.stringValue = NSLocalizedString("Page Number and Date Format", comment: "")
  190. dateFormateLabel.stringValue = String(format: "%@:", NSLocalizedString("Date", comment: ""))
  191. pageLabel.stringValue = String(format: "%@:", NSLocalizedString("Page", comment: ""))
  192. beginPageLabel.stringValue = String(format: "%@:", NSLocalizedString("Start Page Number", comment: ""))
  193. formatLabel.toolTip = NSLocalizedString("Page Number and Date Format", comment: "")
  194. dateFormateLabel.toolTip = String(format: "%@:", NSLocalizedString("Date", comment: ""))
  195. pageLabel.toolTip = String(format: "%@:", NSLocalizedString("Page", comment: ""))
  196. beginPageLabel.toolTip = String(format: "%@:", NSLocalizedString("Start Page Number", comment: ""))
  197. topLeftLabel.stringValue = NSLocalizedString("Left Header Text", comment: "")
  198. topCenterLabel.stringValue = NSLocalizedString("Center Header Text", comment: "")
  199. topRightLabel.stringValue = NSLocalizedString("Right Header Text", comment: "")
  200. bottomLeftLabel.stringValue = NSLocalizedString("Left Footer Text", comment: "")
  201. bottomCenterLabel.stringValue = NSLocalizedString("Center Footer Text", comment: "")
  202. bottomRightLabel.stringValue = NSLocalizedString("Right Footer Text", comment: "")
  203. topLeftLabel.toolTip = NSLocalizedString("Left Header Text", comment: "")
  204. topCenterLabel.toolTip = NSLocalizedString("Center Header Text", comment: "")
  205. topRightLabel.toolTip = NSLocalizedString("Right Header Text", comment: "")
  206. bottomLeftLabel.toolTip = NSLocalizedString("Left Footer Text", comment: "")
  207. }
  208. func loadData(){
  209. self.leftMarginTextField.stringValue = String(format: "%ld", self.headerFooterObj?.leftMargin ?? 0)
  210. self.rightMarginTextField.stringValue = String(format: "%ld", self.headerFooterObj?.rightMargin ?? 0)
  211. self.topMargintextField.stringValue = String(format: "%ld", self.headerFooterObj?.topMargin ?? 0)
  212. self.bottomMarginTextField.stringValue = String(format: "%ld", self.headerFooterObj?.bottomMargin ?? 0)
  213. self.leftMarginStepper.integerValue = Int(self.headerFooterObj?.leftMargin ?? 0)
  214. self.rightMarginStepper.integerValue = Int(self.headerFooterObj?.rightMargin ?? 0)
  215. self.topMarginStepper.integerValue = Int(self.headerFooterObj?.topMargin ?? 0)
  216. self.bottomMarginStepper.integerValue = Int(self.headerFooterObj?.bottomMargin ?? 0)
  217. var fontSizeArray = NSMutableArray()
  218. for i in 0..<self.fontSizeCombobox.objectValues.count {
  219. let obj = self.fontSizeCombobox.objectValues[i] as! String
  220. fontSizeArray.add(NSNumber(integerLiteral: Int(obj) ?? 0))
  221. }
  222. self.fontSizeCombobox.removeAllItems()
  223. self.fontSizeCombobox.addItems(withObjectValues: fontSizeArray as! [Any])
  224. // let index = self.fontSizeCombobox.indexOfItem(withObjectValue: self.headerFooterObj?.getTextFontSize() ?? 0.0)
  225. // if (index == NSNotFound) {
  226. // self.fontSizeCombobox.selectItem(at: index)
  227. // } else {
  228. // self.fontSizeCombobox.integerValue = Int(self.headerFooterObj?.getTextFontSize() ?? 0)
  229. // }
  230. self.dateFormateCombobox.removeAllItems()
  231. self.dateFormateCombobox.addItems(withObjectValues: self.dateFormatArray)
  232. let obj = self.headerFooterObj?.dateFormatString
  233. var isContain: Bool = (((self.dateFormateCombobox.objectValues as? [String])?.contains(obj ?? "")) != nil)
  234. if isContain {
  235. self.dateFormateCombobox.selectItem(withObjectValue: obj)
  236. }
  237. let obj1 = self.headerFooterObj?.pageFormatString
  238. var isContain1: Bool = (((self.pageCombobox.objectValues as? [String])?.contains(obj1 ?? "")) != nil)
  239. if isContain1{
  240. self.pageCombobox.selectItem(withObjectValue: obj1)
  241. } else {
  242. self.pageCombobox.stringValue = ""
  243. }
  244. var pages = [String]()
  245. for i in 0..<(self.pdfdocument?.pageCount ?? 0) {
  246. pages.append("\(i+1)")
  247. }
  248. self.beginPageCombobox.formatter = TextFieldFormatter()
  249. self.beginPageCombobox.isEditable = false
  250. self.beginPageCombobox.removeAllItems()
  251. self.beginPageCombobox.addItems(withObjectValues: pages)
  252. let obj2 = self.headerFooterObj?.startString
  253. var isContain2: Bool = (((self.beginPageCombobox.objectValues as? [String])?.contains(obj2 ?? "")) != nil)
  254. if isContain2{
  255. self.beginPageCombobox.selectItem(withObjectValue: obj2)
  256. }
  257. self.batesStartpageCombobox.formatter = TextFieldFormatter()
  258. self.batesStartpageCombobox.isEditable = false
  259. self.batesStartpageCombobox.removeAllItems()
  260. self.batesStartpageCombobox.addItems(withObjectValues: pages)
  261. let obj3 = self.headerFooterObj?.startString
  262. var isContain3: Bool = (((self.batesStartpageCombobox.objectValues as? [String])?.contains(obj3 ?? "")) != nil)
  263. if isContain3 {
  264. self.batesStartpageCombobox.selectItem(withObjectValue: obj3)
  265. }
  266. // if .all == self.headerFooterObj?.pageRangeType {
  267. // self.pageRangeCombobox.isEditable = false
  268. // self.pageRangeCombobox.selectItem(at: 0)
  269. // } else if .odd == self.headerFooterObj?.pageRangeType {
  270. // self.pageRangeCombobox.isEditable = false
  271. // self.pageRangeCombobox.selectItem(at: 1)
  272. // } else if .even == self.headerFooterObj?.pageRangeType {
  273. // self.pageRangeCombobox.isEditable = false
  274. // self.pageRangeCombobox.selectItem(at: 2)
  275. // } else if .other == self.headerFooterObj?.pageRangeType {
  276. // self.pageRangeCombobox.isEditable = true
  277. // self.pageRangeCombobox.selectItem(at: 3)
  278. // self.pageRangeCombobox.stringValue = self.headerFooterObj?.pageRangeString ?? ""
  279. // let sting = self.headerFooterObj?.pageRangeString
  280. // if sting?.count ?? 0 > 0 {
  281. // let array = sting?.components(separatedBy: ",") ?? []
  282. // var pagesString = ""
  283. // for (i, index) in array.enumerated() {
  284. // if i == array.count - 1 {
  285. // pagesString += "(Int(index)! + 1)"
  286. // } else {
  287. // pagesString += "(Int(index)! + 1),"
  288. // }
  289. // }
  290. // self.pageRangeCombobox.stringValue = pagesString
  291. // }
  292. // }
  293. self.topLeftTextView.string = self.headerFooterObj?.topLeftString ?? ""
  294. self.topCenterTextView.string = self.headerFooterObj?.topCenterString ?? ""
  295. self.topRightTextView.string = self.headerFooterObj?.topRightString ?? ""
  296. self.bottomLeftTextView.string = self.headerFooterObj?.bottomLeftString ?? ""
  297. self.BottomCenterTextView.string = self.headerFooterObj?.bottomCenterString ?? ""
  298. self.bottomRightTextView.string = self.headerFooterObj?.bottomRightString ?? ""
  299. // self.templateNameTextField.stringValue = self.headerFooterObj?.id ?? ""
  300. if self.topLeftTextView.string.count <= 0 && self.topCenterTextView.string.count <= 0 && self.topRightTextView.string.count <= 0 && self.bottomLeftTextView.string.count <= 0 && self.BottomCenterTextView.string.count <= 0 && self.bottomRightTextView.string.count <= 0 {
  301. self.applyButton.isEnabled = false
  302. } else {
  303. self.applyButton.isEnabled = true
  304. }
  305. }
  306. @objc func pageChangeNotification(notification: Notification) {
  307. currentPageIndexTextField.stringValue = "\(pdfView.currentPage().pageIndex() + 1)"
  308. }
  309. @objc func themeChanged(notification: Notification) {
  310. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  311. self.updateViewColor()
  312. }
  313. }
  314. func updateViewColor() {
  315. if KMAppearance.isDarkMode() {
  316. topLeftButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  317. topCentButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  318. topRightButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  319. bottomLeftButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  320. bottomCenterButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  321. bottomRightButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  322. topLeftButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor
  323. topCentButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor
  324. topRightButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor
  325. bottomLeftButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor
  326. bottomCenterButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor
  327. bottomRightButton.layer?.backgroundColor = NSColor(red: 54/255.0, green: 54/255.0, blue: 54/255.0, alpha: 0.85).cgColor
  328. } else {
  329. topLeftButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  330. topCentButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  331. topRightButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  332. bottomLeftButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  333. bottomCenterButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  334. bottomRightButton.layer?.borderColor = NSColor.white.withAlphaComponent(0.3).cgColor
  335. topLeftButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor
  336. topCentButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor
  337. topRightButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor
  338. bottomLeftButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor
  339. bottomCenterButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor
  340. bottomRightButton.layer?.backgroundColor = NSColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 0.85).cgColor
  341. }
  342. }
  343. }