CustomStampTopToolbar.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // CustomStampTopToolbar.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/6/28.
  6. //
  7. import Cocoa
  8. class CustomStampTopToolbar: NSView, NibLoadable {
  9. @IBOutlet var contendView: NSView!
  10. @IBOutlet var titleLabel: NSTextField!
  11. @IBOutlet var fitWidthBtn: KMButton!
  12. @IBOutlet var increaseBtn: KMButton!
  13. @IBOutlet var reduceBtn: KMButton!
  14. @IBOutlet var zoomView: NSView!
  15. @IBOutlet var zoomBox: NSBox!
  16. @IBOutlet var zoomButton: NSPopUpButton!
  17. @IBOutlet weak var zoomTextField: NSTextField!
  18. @IBOutlet var colorBGView: NSView!
  19. @IBOutlet var seplineView1: NSView!
  20. @IBOutlet var seplineView2: NSView!
  21. var pdfView: CPDFListView!
  22. var colorChooseView: KMColorChooseItem!
  23. override func awakeFromNib() {
  24. super.awakeFromNib()
  25. self.configUI()
  26. self.reloadData()
  27. self.updateViewColor()
  28. }
  29. func configUI() {
  30. self.titleLabel.stringValue = NSLocalizedString("Canvas", comment: "")
  31. self.titleLabel.font = NSFont.SFProTextRegularFont(13)
  32. self.zoomView.wantsLayer = true
  33. self.zoomView.layer?.cornerRadius = 1
  34. self.zoomView.layer?.masksToBounds = true
  35. self.zoomBox.borderWidth = 1
  36. self.zoomTextField.delegate = self
  37. for item in self.zoomButton.itemArray {
  38. let string = item.title
  39. item.title = NSLocalizedString(string, comment: "")
  40. }
  41. self.colorChooseView = KMColorChooseItem.createFromNib()
  42. self.colorChooseView.frame = self.colorBGView.bounds
  43. self.colorChooseView.autoresizingMask = [.width, .height]
  44. self.colorChooseView.customColor = NSColor.white
  45. self.colorChooseView.clickHandle = {[weak self] view, color in
  46. self?.pdfView.setPageBackgroundColorWith(color, viewMode: .other)
  47. }
  48. self.colorBGView.addSubview(self.colorChooseView)
  49. self.seplineView1.wantsLayer = true
  50. self.seplineView2.wantsLayer = true
  51. }
  52. func reloadData() {
  53. guard let pdflistView = self.pdfView else {
  54. return
  55. }
  56. if pdflistView.displayModeCustomColor != nil {
  57. self.colorChooseView.customColor = pdflistView.displayModeCustomColor
  58. } else {
  59. if (pdflistView.viewMode == .normal) {
  60. self.colorChooseView.customColor = NSColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)
  61. } else if (pdflistView.viewMode == .soft) {
  62. self.colorChooseView.customColor = NSColor(red: 245/255, green: 237/255, blue: 209/255, alpha: 1)
  63. } else if (pdflistView.viewMode == .night) {
  64. self.colorChooseView.customColor = NSColor(red: 0, green: 0, blue: 0, alpha: 1)
  65. } else if (pdflistView.viewMode == .green) {
  66. self.colorChooseView.customColor = NSColor(red: 205/255, green: 233/255, blue: 207/255, alpha: 1)
  67. } else {
  68. self.colorChooseView.customColor = pdflistView.displayModeCustomColor
  69. }
  70. }
  71. }
  72. func updateViewColor() {
  73. if KMAppearance.isDarkMode() {
  74. self.titleLabel.textColor = KMAppearance.Layout.h1Color()
  75. self.seplineView1.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.1).cgColor
  76. self.seplineView2.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.1).cgColor
  77. self.zoomBox.borderColor = KMAppearance.Interactive.s0Color()
  78. self.zoomBox.fillColor = KMAppearance.Layout.l1Color()
  79. } else {
  80. self.titleLabel.textColor = NSColor.black.withAlphaComponent(0.85)
  81. self.seplineView1.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.1).cgColor
  82. self.seplineView2.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.1).cgColor
  83. self.zoomBox.borderColor = KMAppearance.Interactive.s0Color()
  84. self.zoomBox.fillColor = KMAppearance.Layout.l1Color()
  85. }
  86. }
  87. func pdfViewScaleChanged() {
  88. guard let pdflistView = self.pdfView else {
  89. return
  90. }
  91. self.zoomTextField.stringValue = String(format: "%.2f%@", pdflistView.scaleFactor * 100, "%")
  92. let selectedTag: Int = self.zoomButton.selectedTag()
  93. switch selectedTag {
  94. case 2:
  95. if self.pdfView.scaleFactor != 1.0 {
  96. self.zoomButton.selectedItem?.state = .off
  97. } else {
  98. self.zoomButton.selectedItem?.state = .on
  99. }
  100. break
  101. case 1:
  102. let pageHeight = self.pdfView.currentPage()!.size.height
  103. let pdfviewHeight = self.pdfView.bounds.size.height
  104. if self.pdfView.scaleFactor != pdfviewHeight/pageHeight {
  105. self.zoomButton.selectedItem?.state = .off
  106. } else {
  107. self.zoomButton.selectedItem?.state = .on
  108. }
  109. break
  110. case 0:
  111. if self.pdfView.autoScales == false {
  112. self.zoomButton.selectedItem?.state = .off
  113. } else {
  114. self.zoomButton.selectedItem?.state = .on
  115. }
  116. break
  117. case 3:
  118. if self.pdfView.scaleFactor != 0.1 {
  119. self.zoomButton.selectedItem?.state = .off
  120. } else {
  121. self.zoomButton.selectedItem?.state = .on
  122. }
  123. break
  124. case 4:
  125. if self.pdfView.scaleFactor != 0.25 {
  126. self.zoomButton.selectedItem?.state = .off
  127. } else {
  128. self.zoomButton.selectedItem?.state = .on
  129. }
  130. break
  131. case 5:
  132. if self.pdfView.scaleFactor != 0.5 {
  133. self.zoomButton.selectedItem?.state = .off
  134. } else {
  135. self.zoomButton.selectedItem?.state = .on
  136. }
  137. break
  138. case 6:
  139. if self.pdfView.scaleFactor != 0.75 {
  140. self.zoomButton.selectedItem?.state = .off
  141. } else {
  142. self.zoomButton.selectedItem?.state = .on
  143. }
  144. break
  145. case 7:
  146. if self.pdfView.scaleFactor != 1.0 {
  147. self.zoomButton.selectedItem?.state = .off
  148. } else {
  149. self.zoomButton.selectedItem?.state = .on
  150. }
  151. break
  152. case 8:
  153. if self.pdfView.scaleFactor != 1.5 {
  154. self.zoomButton.selectedItem?.state = .off
  155. } else {
  156. self.zoomButton.selectedItem?.state = .on
  157. }
  158. break
  159. case 9:
  160. if self.pdfView.scaleFactor != 2.0 {
  161. self.zoomButton.selectedItem?.state = .off
  162. } else {
  163. self.zoomButton.selectedItem?.state = .on
  164. }
  165. break
  166. case 10:
  167. if self.pdfView.scaleFactor != 4.0 {
  168. self.zoomButton.selectedItem?.state = .off
  169. } else {
  170. self.zoomButton.selectedItem?.state = .on
  171. }
  172. break
  173. case 11:
  174. if self.pdfView.scaleFactor != 8.0 {
  175. self.zoomButton.selectedItem?.state = .off
  176. } else {
  177. self.zoomButton.selectedItem?.state = .on
  178. }
  179. break
  180. case 12:
  181. if self.pdfView.scaleFactor != 16.0 {
  182. self.zoomButton.selectedItem?.state = .off
  183. } else {
  184. self.zoomButton.selectedItem?.state = .on
  185. }
  186. break
  187. case 13:
  188. if self.pdfView.scaleFactor != 32.0 {
  189. self.zoomButton.selectedItem?.state = .off
  190. } else {
  191. self.zoomButton.selectedItem?.state = .on
  192. }
  193. break
  194. case 14:
  195. if self.pdfView.scaleFactor != 64.0 {
  196. self.zoomButton.selectedItem?.state = .off
  197. } else {
  198. self.zoomButton.selectedItem?.state = .on
  199. }
  200. break
  201. case 15:
  202. if self.pdfView.scaleFactor != 100.0 {
  203. self.zoomButton.selectedItem?.state = .off
  204. } else {
  205. self.zoomButton.selectedItem?.state = .on
  206. }
  207. break
  208. default:
  209. break
  210. }
  211. }
  212. //MARK: IBAction
  213. @IBAction func fitPageWidthAction(_ sender: Any) {
  214. self.pdfView.autoScales = true
  215. guard let activeAnnot = self.pdfView.activeAnnotation else {
  216. return
  217. }
  218. self.pdfView.setNeedsDisplay(self.pdfView.activeAnnotation)
  219. }
  220. @IBAction func increaseAction(_ sender: Any) {
  221. let zoomSting = self.zoomTextField.stringValue ?? "10"
  222. var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0)
  223. switch scale {
  224. case 0...0.25:
  225. scale += 0.25
  226. case 0.25...3:
  227. scale += 0.25
  228. case 3.1...10:
  229. scale += 0.4
  230. case 10.1...100:
  231. scale += 1
  232. default:
  233. scale += 1
  234. }
  235. self.pdfView.scaleFactor = scale
  236. self.reloadData()
  237. }
  238. @IBAction func reduceAction(_ sender: KMButton) {
  239. let zoomSting = self.zoomTextField.stringValue ?? "10"
  240. var scale = CGFloat((NSString(string: zoomSting)).floatValue / 100.0)
  241. switch scale {
  242. case 0...0.25:
  243. scale = 0
  244. case 0.25...3:
  245. scale -= 0.25
  246. case 3.1...10:
  247. scale -= 0.4
  248. case 10.1...100:
  249. scale -= 1
  250. default:
  251. scale -= 1
  252. }
  253. self.pdfView.scaleFactor = scale
  254. self.reloadData()
  255. }
  256. @IBAction func zoomModelSelectButtonAction(_ sender: NSPopUpButton) {
  257. // self.delegate?.toolbarViewController?(self, zoomModel: sender.selectedTag())
  258. for item in sender.itemArray {
  259. item.state = .off
  260. }
  261. sender.selectedItem?.state = .on
  262. let selectedTag: Int = sender.selectedTag()
  263. switch selectedTag {
  264. case 2:
  265. if self.pdfView.scaleFactor != 1.0 {
  266. self.pdfView.scaleFactor = 1.0
  267. self.pdfView.autoScales = false
  268. }
  269. break
  270. case 1:
  271. let pageHeight = self.pdfView.currentPage()!.size.height
  272. let pdfviewHeight = self.pdfView.bounds.size.height
  273. self.pdfView.scaleFactor = pdfviewHeight/pageHeight
  274. self.pdfView.autoScales = false
  275. break
  276. case 0:
  277. self.pdfView.autoScales = true
  278. break
  279. case 3:
  280. self.pdfView.scaleFactor = 0.1
  281. break
  282. case 4:
  283. self.pdfView.scaleFactor = 0.25
  284. break
  285. case 5:
  286. self.pdfView.scaleFactor = 0.5
  287. break
  288. case 6:
  289. self.pdfView.scaleFactor = 0.75
  290. break
  291. case 7:
  292. self.pdfView.scaleFactor = 1.0
  293. break
  294. case 8:
  295. self.pdfView.scaleFactor = 1.5
  296. break
  297. case 9:
  298. self.pdfView.scaleFactor = 2.0
  299. break
  300. case 10:
  301. self.pdfView.scaleFactor = 4.0
  302. break
  303. case 11:
  304. self.pdfView.scaleFactor = 8.0
  305. break
  306. case 12:
  307. self.pdfView.scaleFactor = 16.0
  308. break
  309. case 13:
  310. self.pdfView.scaleFactor = 32.0
  311. break
  312. case 14:
  313. self.pdfView.scaleFactor = 64.0
  314. break
  315. case 15:
  316. self.pdfView.scaleFactor = 100.0
  317. break
  318. default:
  319. break
  320. }
  321. self.reloadData()
  322. }
  323. override func mouseDown(with event: NSEvent) {
  324. super.mouseDown(with: event)
  325. self.window?.makeFirstResponder(nil)
  326. }
  327. }
  328. extension CustomStampTopToolbar: NSTextFieldDelegate {
  329. func controlTextDidEndEditing(_ obj: Notification) {
  330. let textField = obj.object as? NSTextField
  331. if textField == self.zoomTextField {
  332. guard let zoomString = textField?.stringValue else {
  333. return
  334. }
  335. var scale = CGFloat(textField!.floatValue / 100.0)
  336. if scale > 100 {
  337. scale = 100
  338. }
  339. if scale < 0.1 {
  340. scale = 0.1
  341. }
  342. if (scale <= 0.101) {
  343. self.pdfView.scaleFactor = self.pdfView.minScaleFactor()
  344. } else {
  345. self.pdfView.scaleFactor = scale
  346. }
  347. }
  348. }
  349. }