KMPrintChooseView.swift 12 KB

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