KMPrintChooseView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // KMPrintChooseView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2022/12/8.
  6. //
  7. import Cocoa
  8. typealias KMPrintChooseViewDataDidChange = (_ view: KMPrintChooseView, _ data: KMPrintModel) -> Void
  9. class KMPrintChooseView: KMBaseXibView {
  10. @IBOutlet weak var printerLabel: NSTextField!
  11. @IBOutlet weak var printerButton: KMTextfieldButton!
  12. @IBOutlet weak var pageSetButton: NSButton!
  13. @IBOutlet weak var blackAndWhiteButton: KMImageTitleButton!
  14. @IBOutlet weak var pageNumberLabel: NSTextField!
  15. @IBOutlet weak var textFieldStepperView: KMTextFieldStepperView!
  16. @IBOutlet weak var printerSetLabel: NSTextField!
  17. @IBOutlet weak var pageRangeLabel: NSTextField!
  18. @IBOutlet weak var pageRangeButton: KMTextfieldButton!
  19. @IBOutlet weak var printDirectionLabel: NSTextField!
  20. @IBOutlet weak var printDirectionButton: KMTextfieldButton!
  21. @IBOutlet weak var printContentLabel: NSTextField!
  22. @IBOutlet weak var printContentButton: KMTextfieldButton!
  23. @IBOutlet weak var choosePageSizeView: KMPrintChoosePageSizeView!
  24. @IBOutlet weak var reversePagesButton: KMImageTitleButton!
  25. weak var delegate: KMPrintChooseViewDelegate?
  26. lazy var presenter: KMPrintChoosePresenter = KMPrintChoosePresenter()
  27. lazy var outputData: KMPrintModel = KMPrintModel()
  28. var inputPageRange: KMPrintPageRange = KMPrintPageRange() {
  29. didSet {
  30. self.outputData.page.range = inputPageRange
  31. self.pageRangeButton.stringValue = self.outputData.page.range.type.rawValue
  32. self.presenter.pageRangeAction(inputPageRange.type.rawValue)
  33. }
  34. }
  35. var inputData: URL? {
  36. didSet {
  37. self.presenter.initPresenter(delegate: self, data: inputData!)
  38. self.choosePageSizeView.inputData = self.presenter.model.page.operation
  39. }
  40. }
  41. deinit {
  42. self.delegate = nil
  43. }
  44. override func draw(_ dirtyRect: NSRect) {
  45. super.draw(dirtyRect)
  46. // Drawing code here.
  47. }
  48. override func setup() {
  49. super.setup()
  50. //打印机名称
  51. self.printerButton.data = NSPrinter.printerNames
  52. //页面范围
  53. self.pageRangeButton.data = KMPrintPageRange.PageRangeType.allValues()
  54. //打印方向
  55. self.printDirectionButton.data = KMPrintPaperDirectionType.allValues()
  56. //打印内容
  57. self.printContentButton.data = KMPrintContentType.allValues()
  58. let model: KMBaseTextFieldModel = KMBaseTextFieldModel()
  59. model.minValue = 1
  60. model.maxValue = 100
  61. self.textFieldStepperView.model = model
  62. self.textFieldStepperView.valueDidChange = { [weak self] view, value in
  63. self?.presenter.pageNumberAction(copies: Int(value)!)
  64. }
  65. self.choosePageSizeView.dataDidChange = { [weak self] view, data in
  66. //数据传递
  67. self?.presenter.printDealModelAction(data: data)
  68. }
  69. self.printerLabel.stringValue = NSLocalizedString("Print", comment: "") + ":"
  70. self.printerLabel.font = NSFont.SFProTextRegularFont(14.0)
  71. self.printerLabel.textColor = NSColor.km_init(hex: "#252629")
  72. self.pageRangeLabel.stringValue = NSLocalizedString("Print", comment: "") + ":"
  73. self.pageRangeLabel.font = NSFont.SFProTextRegularFont(14.0)
  74. self.pageRangeLabel.textColor = NSColor.km_init(hex: "#252629")
  75. self.pageNumberLabel.stringValue = NSLocalizedString("Print", comment: "") + ":"
  76. self.pageNumberLabel.font = NSFont.SFProTextRegularFont(14.0)
  77. self.pageNumberLabel.textColor = NSColor.km_init(hex: "#252629")
  78. self.printerSetLabel.stringValue = NSLocalizedString("Print Settings", comment: "") + ":"
  79. self.printerSetLabel.font = NSFont.SFProTextRegularFont(12.0)
  80. self.printerSetLabel.textColor = NSColor.km_init(hex: "#616469")
  81. self.printContentLabel.stringValue = NSLocalizedString("Contents", comment: "") + ":"
  82. self.printContentLabel.font = NSFont.SFProTextRegularFont(14.0)
  83. self.printContentLabel.textColor = NSColor.km_init(hex: "#252629")
  84. self.printDirectionLabel.stringValue = NSLocalizedString("Direction", comment: "") + ":"
  85. self.printDirectionLabel.font = NSFont.SFProTextRegularFont(14.0)
  86. self.printDirectionLabel.textColor = NSColor.km_init(hex: "#252629")
  87. self.pageSetButton.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  88. self.pageSetButton.stringValue = NSLocalizedString("Page Settings", comment: "") + ":"
  89. self.pageSetButton.font = NSFont.SFProTextRegularFont(14.0)
  90. self.pageSetButton.contentTintColor = NSColor.km_init(hex: "#252629")
  91. self.printerButton.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  92. self.printerButton.stringValue = NSLocalizedString("EPSON L4260 Series", comment: "") + ":"
  93. self.printerButton.font = NSFont.SFProTextRegularFont(14.0)
  94. self.printerButton.textColor = NSColor.km_init(hex: "#252629")
  95. self.printerButton.mouseDownAction = {[weak self] (button, itemString) in
  96. self?.presenter.printerAction(itemString)
  97. }
  98. self.pageRangeButton.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  99. self.pageRangeButton.stringValue = NSLocalizedString("All pages", comment: "") + ":"
  100. self.pageRangeButton.font = NSFont.SFProTextRegularFont(14.0)
  101. self.pageRangeButton.textColor = NSColor.km_init(hex: "#252629")
  102. self.pageRangeButton.mouseDownAction = {[weak self] (button, itemString) in
  103. self?.presenter.pageRangeAction(itemString)
  104. }
  105. self.printDirectionButton.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  106. self.printDirectionButton.stringValue = NSLocalizedString("Portrait", comment: "") + ":"
  107. self.printDirectionButton.font = NSFont.SFProTextRegularFont(14.0)
  108. self.printDirectionButton.textColor = NSColor.km_init(hex: "#252629")
  109. self.printDirectionButton.mouseDownAction = {[weak self] (button, itemString) in
  110. self?.presenter.printDirectionAction(itemString)
  111. }
  112. self.printContentButton.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  113. self.printContentButton.stringValue = NSLocalizedString("Document and Markups", comment: "") + ":"
  114. self.printContentButton.font = NSFont.SFProTextRegularFont(14.0)
  115. self.printContentButton.textColor = NSColor.km_init(hex: "#252629")
  116. self.printContentButton.mouseDownAction = {[weak self] (button, itemString) in
  117. self?.presenter.printContentAction(itemString)
  118. }
  119. self.blackAndWhiteButton.title = NSLocalizedString("Black and white", comment: "")
  120. self.blackAndWhiteButton.imageName = "btn_checkbox_unsel_on"
  121. self.blackAndWhiteButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  122. self.blackAndWhiteButton.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  123. self.blackAndWhiteButton.action = { [weak self] (view, button) in
  124. self?.presenter.blackAndWhiteAction(button)
  125. }
  126. self.reversePagesButton.title = NSLocalizedString("Reverse pages", comment: "")
  127. self.reversePagesButton.imageName = "btn_checkbox_unsel_on"
  128. self.reversePagesButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  129. self.reversePagesButton.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  130. self.reversePagesButton.action = { [weak self] (view, button) in
  131. self?.presenter.printReverseAction(button)
  132. }
  133. }
  134. //刷新界面UI 和 数据
  135. override func reloadData() {
  136. super.reloadData()
  137. self.printerButton.stringValue = self.outputData.printer.name
  138. self.pageSetButton.title = self.outputData.paper.info.type.rawValue
  139. self.textFieldStepperView.stringValue = self.outputData.printer.copies.description
  140. self.pageRangeButton.stringValue = self.outputData.page.range.type.rawValue
  141. self.printDirectionButton.stringValue = self.outputData.paper.direction.rawValue
  142. self.printContentButton.stringValue = self.outputData.page.contentType.rawValue
  143. if self.outputData.printer.options.contains(.blackAndWhite) {
  144. self.blackAndWhiteButton.imageName = "btn_checkbox_sel_on"
  145. } else {
  146. self.blackAndWhiteButton.imageName = "btn_checkbox_unsel_on"
  147. }
  148. if self.outputData.page.range.reversePrintOrder {
  149. self.reversePagesButton.imageName = "btn_checkbox_sel_on"
  150. } else {
  151. self.reversePagesButton.imageName = "btn_checkbox_unsel_on"
  152. }
  153. }
  154. }
  155. protocol KMPrintChooseViewAction {}
  156. extension KMPrintChooseView: KMPrintChooseViewAction {
  157. @IBAction func pageSetAction(_ sender: NSButton) {
  158. let printPaperSetWindowController: KMPrintPaperSetWindowController = KMPrintPaperSetWindowController.init(windowNibName: "KMPrintPaperSetWindowController")
  159. NSApp.mainWindow?.beginCriticalSheet(printPaperSetWindowController.window!)
  160. weak var weakSelf = self
  161. printPaperSetWindowController.data = self.outputData.paper.info
  162. printPaperSetWindowController.completion = { window, data in
  163. weakSelf!.presenter.pageSetAction(data)
  164. }
  165. }
  166. }
  167. extension KMPrintChooseView: KMPrintChoosePresenterDelegate {
  168. func printDealModelChange(sender: Any, data: KMPrintModel) {
  169. self.reloadData()
  170. if (self.delegate != nil) {
  171. self.delegate?.printDealModelChange(sender: self, data: data)
  172. }
  173. }
  174. func showData(sender: Any, data: KMPrintModel) {
  175. self.outputData = data
  176. self.reloadData()
  177. }
  178. func printerChange(sender: Any, data: KMPrintModel) {
  179. self.reloadData()
  180. if self.delegate != nil {
  181. self.delegate?.printerChange(sender: self, data: data)
  182. }
  183. }
  184. func pageSetChange(sender: Any, data: KMPrintModel) {
  185. self.reloadData()
  186. if self.delegate != nil {
  187. self.delegate?.pageSetChange(sender: self, data: data)
  188. }
  189. }
  190. func pageNumberChange(sender: Any, data: KMPrintModel) {
  191. self.reloadData()
  192. if self.delegate != nil {
  193. self.delegate?.pageNumberChange(sender: self, data: data)
  194. }
  195. }
  196. func blackAndWhiteChange(sender: Any, data: KMPrintModel) {
  197. self.reloadData()
  198. if self.delegate != nil {
  199. self.delegate?.blackAndWhiteChange(sender: self, data: data)
  200. }
  201. }
  202. func printOnBothSidesChange(sender: Any, data: KMPrintModel) {
  203. self.reloadData()
  204. if self.delegate != nil {
  205. self.delegate?.printOnBothSidesChange(sender: self, data: data)
  206. }
  207. }
  208. func pageRangeChange(sender: Any, data: KMPrintModel) {
  209. self.reloadData()
  210. if self.delegate != nil {
  211. self.delegate?.pageRangeChange(sender: self, data: data)
  212. }
  213. }
  214. func printDirectionChange(sender: Any, data: KMPrintModel) {
  215. self.reloadData()
  216. if self.delegate != nil {
  217. self.delegate?.printDirectionChange(sender: self, data: data)
  218. }
  219. }
  220. func printContentChange(sender: Any, data: KMPrintModel) {
  221. self.reloadData()
  222. if self.delegate != nil {
  223. self.delegate?.printContentChange(sender: self, data: data)
  224. }
  225. }
  226. func printReverseChange(sender: Any, data: KMPrintModel) {
  227. self.reloadData()
  228. if self.delegate != nil {
  229. self.delegate?.printReverseChange(sender: self, data: data)
  230. }
  231. }
  232. func showData(presenter: KMPrintChoosePresenter, data: KMPrintModel) {
  233. self.outputData = data
  234. self.reloadData()
  235. }
  236. }
  237. protocol KMPrintChooseViewDelegate: KMPrintChoosePresenterDelegate {
  238. }