KMHeaderFooterPropertyInfoController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // KMHeaderFooterPropertyInfoController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/27.
  6. //
  7. import Cocoa
  8. class KMHeaderFooterPropertyInfoController: NSViewController {
  9. @IBOutlet weak var fontBox: NSBox!
  10. var fontView = KMHeaderFooterFontInfoView()
  11. @IBOutlet weak var marginBox: NSBox!
  12. var marginView = KMHeaderFooterMarginInfoView()
  13. @IBOutlet weak var dateBox: NSBox!
  14. var dateView = KMHeaderFooterDateInfoView()
  15. @IBOutlet weak var pageBox: NSBox!
  16. var pageView = KMHeaderFooterPageInfoView()
  17. @IBOutlet weak var contentBox: NSBox!
  18. var contentView = KMHeaderFooterContentInfoView()
  19. @IBOutlet weak var pageRangeBox: NSBox!
  20. var pagePageView = KMConvertPageRangeSettingItemView.createFromNib()
  21. weak var delegate: KMWatermarkAdjectivePropertyDelegate!
  22. init() {
  23. super.init(nibName: "KMHeaderFooterPropertyInfoController", bundle: nil)
  24. }
  25. required init?(coder: NSCoder) {
  26. super.init(nibName: "KMHeaderFooterPropertyInfoController", bundle: nil)
  27. }
  28. private var myModel: KMHeaderFooterObject!
  29. var model: KMHeaderFooterObject {
  30. get {
  31. return myModel
  32. }
  33. set {
  34. myModel = newValue
  35. self.fontView.model = newValue
  36. self.marginView.model = newValue
  37. self.dateView.model = newValue
  38. self.pageView.leftComboBox.removeAllItems()
  39. self.pageView.leftComboBox.addItems(withObjectValues: KMWatermarkAdjectiveTools.getPageFormats())
  40. if (newValue.pageFormatString.isEmpty) {
  41. self.pageView.leftComboBox.stringValue = KMWatermarkAdjectiveTools.getPageFormats().first!
  42. } else {
  43. self.pageView.leftComboBox.stringValue = newValue.pageFormatString
  44. }
  45. self.pageView.rightComboBox.removeAllItems()
  46. for i in 0 ..< newValue.pageCount {
  47. self.pageView.rightComboBox.addItem(withObjectValue: i+1)
  48. }
  49. if (newValue.startString.isEmpty) {
  50. self.pageView.rightComboBox.stringValue = "1"
  51. } else {
  52. self.pageView.rightComboBox.stringValue = newValue.startString
  53. }
  54. self.contentView.model = newValue
  55. }
  56. }
  57. override func viewWillAppear() {
  58. super.viewWillAppear()
  59. /// 滚动到顶部
  60. DispatchQueue.main.async {
  61. let contentView = self.fontBox.enclosingScrollView!.contentView;
  62. contentView.scroll(to: NSMakePoint(0, NSHeight(self.fontBox.enclosingScrollView!.documentView!.frame)-NSHeight(contentView.frame)))
  63. }
  64. }
  65. override func viewDidLoad() {
  66. super.viewDidLoad()
  67. let fontView = self.fontView
  68. fontView.frame = self.fontBox.contentView!.bounds
  69. fontView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  70. self.fontBox.contentView?.addSubview(fontView)
  71. fontView.itemClick = { [weak self] (index: Int, value: Any) in
  72. if (index == 1) { /// 字体名称
  73. switch self!.model.textFont {
  74. case .font(name: _, size: let size):
  75. self!.model.textFont = .font(name: value as! String, size: size)
  76. default:
  77. break
  78. }
  79. if (self!.delegate != nil) {
  80. self!.delegate.propertyInfoDidChange(model: self!.model)
  81. }
  82. } else if (index == 2) { /// 字体大小
  83. switch self!.model.textFont {
  84. case .font(name: let name, size: _):
  85. self!.model.textFont = .font(name: name, size: CGFloat(value as! Int))
  86. default:
  87. break
  88. }
  89. if (self!.delegate != nil) {
  90. self!.delegate.propertyInfoDidChange(model: self!.model)
  91. }
  92. } else if (index == 3) { /// 字体颜色
  93. let panel = NSColorPanel.shared
  94. panel.setTarget(self!)
  95. panel.setAction(#selector(self!.colorPanelAction))
  96. panel.orderFront(nil)
  97. }
  98. }
  99. let marginView = self.marginView
  100. marginView.frame = self.marginBox.contentView!.bounds
  101. marginView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  102. self.marginBox.contentView?.addSubview(marginView)
  103. marginView.itemClick = { [weak self] (index: Int, value: Any) in
  104. if (index == 1) { /// top
  105. self!.model.topMargin = value as! Int
  106. } else if (index == 2) { /// bottom
  107. self!.model.bottomMargin = value as! Int
  108. } else if (index == 3) { /// left
  109. self!.model.leftMargin = value as! Int
  110. } else if (index == 4) { /// right
  111. self!.model.rightMargin = value as! Int
  112. }
  113. if (self!.delegate != nil) {
  114. self!.delegate.propertyInfoDidChange(model: self!.model)
  115. }
  116. }
  117. let dateView = self.dateView
  118. dateView.frame = self.dateBox.contentView!.bounds
  119. dateView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  120. self.dateBox.contentView?.addSubview(dateView)
  121. dateView.itemClick = { [weak self] (index: Int, value: Any) in
  122. self!.model.dateFormatString = value as! String
  123. if (self!.delegate != nil) {
  124. self!.delegate.propertyInfoDidChange(model: self!.model)
  125. }
  126. }
  127. let pageView = self.pageView
  128. pageView.frame = self.pageBox.contentView!.bounds
  129. pageView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  130. self.pageBox.contentView?.addSubview(pageView)
  131. pageView.leftLabel.stringValue = NSLocalizedString("Page:", comment: "")
  132. pageView.itemClick = { [weak self] (index: Int, value: Any) in
  133. if (index == 1) { /// 页面
  134. self!.model.pageFormatString = KMWatermarkAdjectiveTools.getPageFormats()[value as! Int]
  135. } else if (index == 2) { /// 起始页
  136. self!.model.startString = "\(value as! Int+1)"
  137. }
  138. if (self!.delegate != nil) {
  139. self!.delegate.propertyInfoDidChange(model: self!.model)
  140. }
  141. }
  142. let contentView = self.contentView
  143. contentView.frame = self.contentBox.contentView!.bounds
  144. contentView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  145. self.contentBox.contentView?.addSubview(contentView)
  146. contentView.addAction = { [weak self]
  147. (index: Int, sender: NSButton) in
  148. let menu = NSMenu()
  149. let item = menu.addItem(withTitle: NSLocalizedString("Insert Page Number", comment: ""), action: #selector(self!.insertPageAction), keyEquivalent: "")
  150. item.target = self
  151. let item2 = menu.addItem(withTitle: NSLocalizedString("Insert Date", comment: ""), action: #selector(self!.insertDateAction), keyEquivalent: "")
  152. item2.target = self
  153. menu.popUp(positioning: nil, at: NSPoint(x: sender.bounds.midX, y: sender.bounds.maxY), in: sender)
  154. }
  155. contentView.itemClick = { [weak self] (index: Int, value: Any) in
  156. self!.updateModelForContentInfo(index: index, value: value as! String)
  157. }
  158. let pageRangeView = self.pagePageView
  159. pageRangeView?.frame = self.pageRangeBox.contentView!.bounds
  160. pageRangeView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  161. self.pageRangeBox.contentView?.addSubview(pageRangeView!)
  162. pageRangeView!.pageSizeComboBox.removeAllItems()
  163. pageRangeView!.pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Odd Pages", comment: ""), NSLocalizedString("Even Pages", comment: ""), NSLocalizedString("Customized", comment: "")])
  164. pageRangeView!.pageSizeComboBox.selectItem(at: 0)
  165. pageRangeView?.isTextDidEndCallback = true
  166. pageRangeView?.itemClick = { [unowned self] index, _ in
  167. self.model.pageRangeType = KMWatermarkeModelPageRangeType(rawValue: index)!
  168. if (self.delegate != nil) {
  169. self.delegate.propertyInfoDidChange(model: self.model)
  170. }
  171. }
  172. pageRangeView?.textDidEndEditingCallback = { [weak self] (textString: String) in
  173. let array = KMPageRangeTools.findSelectPage(pageRangeString: textString, pageCount: self!.model.pageCount)
  174. if (array.count <= 0) {
  175. let alert = NSAlert()
  176. alert.alertStyle = .warning
  177. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  178. alert.runModal()
  179. return
  180. }
  181. var pageString: String = ""
  182. for i in 0 ..< array.count {
  183. pageString.append("\(array[i]-1)")
  184. if (i != array.count-1) {
  185. pageString.append(",")
  186. }
  187. }
  188. self!.model.pageRangeString = pageString
  189. if (self!.delegate != nil) {
  190. self!.delegate.propertyInfoDidChange(model: self!.model)
  191. }
  192. }
  193. }
  194. private func updateModelForContentInfo(index: Int, value: String) {
  195. if (index == 1) {
  196. self.model.topLeftString = value
  197. } else if (index == 2) {
  198. self.model.topCenterString = value
  199. } else if (index == 3) {
  200. self.model.topRightString = value
  201. } else if (index == 4) {
  202. self.model.bottomLeftString = value
  203. } else if (index == 5) {
  204. self.model.bottomCenterString = value
  205. } else if (index == 6) {
  206. self.model.bottomRightString = value
  207. }
  208. if (self.delegate != nil) {
  209. self.delegate.propertyInfoDidChange(model: self.model)
  210. }
  211. }
  212. @objc func colorPanelAction(sender: NSColorPanel) {
  213. let color = KMWatermarkAdjectiveTools.parseColor(color: sender.color)
  214. self.model.textColor = .color(red: color.red, green: color.green, blue: color.blue, alpha: color.alpha)
  215. self.fontView.colorView.layer?.backgroundColor = sender.color.cgColor
  216. if (self.delegate != nil) {
  217. self.delegate.propertyInfoDidChange(model: self.model)
  218. }
  219. }
  220. @objc private func insertPageAction(sender: NSMenuItem) {
  221. let string: String = "<<\(self.pageView.leftComboBox.stringValue)>>"
  222. self.contentView.textView.string = "\(self.contentView.textView.string)".appending(string)
  223. self.updateModelForContentInfo(index: self.contentView.indexForSelectedItem, value: self.contentView.textView.string)
  224. }
  225. @objc private func insertDateAction(sender: NSMenuItem) {
  226. let string: String = "<<\(self.myModel.dateFormatString)>>"
  227. self.contentView.textView.string = "\(self.contentView.textView.string)".appending(string)
  228. self.updateModelForContentInfo(index: self.contentView.indexForSelectedItem, value: self.contentView.textView.string)
  229. }
  230. }