KMBatchTableCellView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // KMBatchTableCellView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/10/27.
  6. //
  7. import Cocoa
  8. typealias batchTableCellComboboxMouseDownCallback = (_ mouseDown: Bool) -> ()
  9. typealias batchTableCellRemoveFileCallBack = (_ file: KMBatchOperateFile) -> ()
  10. @objc enum KMBatchTableCellViewType: Int {
  11. case Size = 0
  12. case PageRange
  13. case FileName
  14. case Status
  15. case DPI
  16. case Dimensions
  17. }
  18. class KMBatchTableCellCombobox: KMComboBox{
  19. var mouseDownCallback: batchTableCellComboboxMouseDownCallback?
  20. override func mouseDown(with event: NSEvent) {
  21. super.mouseDown(with: event)
  22. if (self.mouseDownCallback != nil) {
  23. self.mouseDownCallback!(true)
  24. }
  25. }
  26. }
  27. class KMBatchTableCellView: NSTableCellView,NSComboBoxDelegate{
  28. var removeFileCallBack: batchTableCellRemoveFileCallBack?
  29. var pageRangeCombobox: KMBatchTableCellCombobox?
  30. var errorTextField: NSTextField?
  31. var statusView: NSView?
  32. var removeButton: KMButton?
  33. var progressIndicator: NSProgressIndicator?
  34. var type: KMBatchTableCellViewType?
  35. var file: KMBatchOperateFile?
  36. var DPIComboBox: KMBatchTableCellCombobox?
  37. var indicateImageView: NSImageView?
  38. var comboBoxContent: String?
  39. deinit {
  40. DistributedNotificationCenter.default().removeObserver(self)
  41. }
  42. convenience init(type: KMBatchTableCellViewType) {
  43. self.init()
  44. switch type {
  45. case .Size:
  46. configuUIForSize()
  47. case .Dimensions:
  48. configuUIForSize()
  49. case .PageRange:
  50. configuUIForPageRange()
  51. case .FileName:
  52. configuUIForFileName()
  53. case .Status:
  54. configuUIForStatus()
  55. case .DPI:
  56. configuUIForDPI()
  57. default:
  58. break
  59. }
  60. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  61. self.type = type
  62. }
  63. func configuUIForSize() {
  64. let tf = NSTextField(frame: .zero)
  65. self.textField = tf
  66. self.textField?.isBordered = false
  67. self.textField?.drawsBackground = true
  68. self.textField?.backgroundColor = NSColor.clear
  69. self.textField?.isEditable = false
  70. self.addSubview(self.textField!)
  71. self.textField?.mas_makeConstraints({ make in
  72. make?.top.equalTo()(self)?.offset()(13)
  73. make?.left.equalTo()(self)
  74. })
  75. self.textField?.textColor = KMAppearance.Layout.h0Color()
  76. self.textField?.font = NSFont.systemFont(ofSize: 12)
  77. }
  78. func configuUIForPageRange() {
  79. self.pageRangeCombobox = KMBatchTableCellCombobox(frame: .zero)
  80. self.pageRangeCombobox?.focusRingType = .none
  81. self.pageRangeCombobox?.type = .none
  82. self.pageRangeCombobox?.wantsLayer = true
  83. self.pageRangeCombobox?.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  84. self.pageRangeCombobox?.layer?.borderWidth = 1.0
  85. self.pageRangeCombobox?.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
  86. self.pageRangeCombobox?.layer?.cornerRadius = 1.0
  87. self.pageRangeCombobox?.backgroundColor = KMAppearance.Layout.l1Color()
  88. self.addSubview(self.pageRangeCombobox!)
  89. self.pageRangeCombobox?.mas_makeConstraints({ make in
  90. make?.left.equalTo()(self)
  91. make?.right.equalTo()(self)?.offset()(-20)
  92. make?.top.equalTo()(self)?.offset()(10)
  93. })
  94. let choiceArray = [
  95. " \(NSLocalizedString("All Pages", comment: ""))",
  96. " \(NSLocalizedString("Odd Pages Only", comment: ""))",
  97. " \(NSLocalizedString("Even Pages Only", comment: ""))",
  98. " \(NSLocalizedString("e.g. 1,3-5,10", comment: ""))"
  99. ]
  100. self.pageRangeCombobox?.addItems(withObjectValues: choiceArray)
  101. let placeholderString = " \(NSLocalizedString("e.g. 1,3-5,10", comment: ""))"
  102. // self.pageRangeCombobox!.cell.placeholderString = placeholderString
  103. self.pageRangeCombobox?.selectItem(at: 0)
  104. self.pageRangeCombobox?.isEditable = false
  105. self.pageRangeCombobox?.delegate = self
  106. self.comboBoxContent = placeholderString
  107. self.updateViewColor()
  108. self.pageRangeCombobox?.mouseDownCallback = { [weak self] mouseDown in
  109. let itemIdex = self?.pageRangeCombobox?.indexOfSelectedItem ?? 0
  110. if itemIdex == 3 {//KMBatchOperatePageChoice.Input
  111. if self?.comboBoxContent == placeholderString {
  112. self?.pageRangeCombobox?.stringValue = ""
  113. } else {
  114. self?.pageRangeCombobox?.stringValue = (self?.comboBoxContent) ?? ""
  115. }
  116. }
  117. }
  118. }
  119. func configuUIForFileName() {
  120. var tf = NSTextField(frame: .zero)
  121. self.textField = tf
  122. self.textField?.isBordered = false
  123. self.textField?.drawsBackground = true
  124. self.textField?.backgroundColor = NSColor.clear
  125. self.textField?.isEditable = false
  126. self.textField?.maximumNumberOfLines = 1
  127. self.textField?.lineBreakMode = .byTruncatingMiddle
  128. self.addSubview(self.textField!)
  129. self.textField?.mas_makeConstraints({ make in
  130. make?.top.equalTo()(self)?.offset()(13)
  131. make?.left.equalTo()(self)
  132. make?.right.equalTo()(self)?.offset()(-10)
  133. })
  134. self.textField?.font = NSFont.systemFont(ofSize: 12)
  135. self.textField?.textColor = KMAppearance.Layout.h0Color()
  136. self.errorTextField = NSTextField(frame: .zero)
  137. self.errorTextField?.isBordered = false
  138. self.errorTextField?.drawsBackground = true
  139. self.errorTextField?.backgroundColor = NSColor.clear
  140. self.errorTextField?.isEditable = false
  141. self.errorTextField?.font = NSFont.systemFont(ofSize: 12)
  142. self.errorTextField?.textColor = KMAppearance.Status.errColor()
  143. self.errorTextField?.lineBreakMode = .byTruncatingMiddle
  144. self.addSubview(self.errorTextField!)
  145. self.errorTextField?.mas_makeConstraints({ make in
  146. make?.top.equalTo()(self.textField?.mas_bottom)
  147. make?.left.equalTo()(self)
  148. make?.bottom.equalTo()(self)?.offset()(-11)
  149. make?.right.equalTo()(self.mas_right)
  150. })
  151. }
  152. func configuUIForStatus() {
  153. self.statusView = NSView(frame: .zero)
  154. self.statusView?.wantsLayer = true
  155. self.addSubview(self.statusView!)
  156. self.statusView?.mas_makeConstraints({ make in
  157. make?.left.equalTo()(self)
  158. make?.top.equalTo()(self)?.offset()(12)
  159. make?.width.equalTo()(16)
  160. make?.height.equalTo()(16)
  161. })
  162. self.indicateImageView = NSImageView(frame: .zero)
  163. self.statusView?.addSubview(self.indicateImageView!)
  164. self.indicateImageView?.mas_makeConstraints({ make in
  165. make?.edges.equalTo()(self.statusView)
  166. })
  167. self.progressIndicator = NSProgressIndicator(frame: .zero)
  168. self.statusView?.addSubview(self.progressIndicator!)
  169. self.progressIndicator?.mas_makeConstraints({ make in
  170. make?.edges.equalTo()(self.statusView)
  171. })
  172. self.progressIndicator?.maxValue = 1.0
  173. self.progressIndicator?.minValue = 0
  174. self.progressIndicator?.style = .spinning
  175. self.progressIndicator?.controlSize = .small
  176. self.removeButton = KMButton(image: NSImage(named: KMImageNameUXIconBtnCloseNor)!, target: self, action: #selector(deleteFile(sender:)))
  177. self.removeButton?.isBordered = false
  178. self.removeButton?.isHidden = true
  179. self.addSubview(self.removeButton!)
  180. self.removeButton?.mas_makeConstraints({ make in
  181. // make?.left.equalTo()(self)?.offset()(10)
  182. make?.centerY.equalTo()(self)
  183. make?.right.equalTo()(self)?.offset()(0)
  184. make?.width.equalTo()(20)
  185. make?.height.equalTo()(20)
  186. })
  187. self.removeButton!.mouseMoveCallback = { [weak self] mouseEntered in
  188. if mouseEntered {
  189. self?.removeButton?.image = NSImage(named: KMImageNameUXIconBtnCloseHov)
  190. } else {
  191. self?.removeButton?.image = NSImage(named: KMImageNameUXIconBtnCloseNor)
  192. }
  193. }
  194. }
  195. func configuUIForDPI() {
  196. self.DPIComboBox = KMBatchTableCellCombobox(frame: .zero)
  197. self.DPIComboBox?.focusRingType = .none
  198. self.DPIComboBox?.type = .none
  199. self.DPIComboBox?.wantsLayer = true
  200. self.DPIComboBox?.layer?.backgroundColor = KMAppearance.Layout.l1Color().cgColor
  201. self.DPIComboBox?.layer?.borderWidth = 1.0
  202. self.DPIComboBox?.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
  203. self.DPIComboBox?.layer?.cornerRadius = 1.0
  204. self.DPIComboBox?.backgroundColor = KMAppearance.Layout.l1Color()
  205. self.addSubview(self.DPIComboBox!)
  206. let DPIArray = ["50 dpi", "72 dpi", "96 dpi", "150 dpi", "300 dpi", "600 dpi"]
  207. self.DPIComboBox?.addItems(withObjectValues: DPIArray)
  208. self.DPIComboBox?.selectItem(at: 0)
  209. self.DPIComboBox?.isEditable = false
  210. self.DPIComboBox?.delegate = self
  211. self.DPIComboBox?.mas_makeConstraints({ make in
  212. make?.left.equalTo()(self)
  213. make?.right.equalTo()(self)?.offset()(-20)
  214. make?.top.equalTo()(self)?.offset()(11)
  215. make?.bottom.equalTo()(self)?.offset()(-11)
  216. })
  217. }
  218. @objc func themeChanged(notification: NSNotification) {
  219. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  220. self.updateViewColor()
  221. }
  222. }
  223. @objc func deleteFile(sender: Any) {
  224. if (self.removeFileCallBack != nil) {
  225. self.removeFileCallBack!(self.file!)
  226. }
  227. }
  228. func updateViewColor() {
  229. if KMAppearance.isDarkMode() {
  230. self.DPIComboBox?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
  231. self.DPIComboBox?.layer!.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  232. self.DPIComboBox?.layer!.borderColor = NSColor(red: 0.337, green: 0.345, blue: 0.353, alpha: 1).cgColor
  233. self.pageRangeCombobox?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
  234. self.pageRangeCombobox?.layer!.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
  235. self.pageRangeCombobox?.layer!.borderColor = NSColor(red: 0.337, green: 0.345, blue: 0.353, alpha: 1).cgColor
  236. } else {
  237. self.DPIComboBox?.backgroundColor = NSColor.white
  238. self.DPIComboBox?.layer!.backgroundColor = NSColor.white.cgColor
  239. self.DPIComboBox?.layer!.borderColor = NSColor(red: 0.855, green: 0.859, blue: 0.871, alpha: 1).cgColor
  240. self.pageRangeCombobox?.backgroundColor = NSColor.white
  241. self.pageRangeCombobox?.layer!.backgroundColor = NSColor.white.cgColor
  242. self.pageRangeCombobox?.layer!.borderColor = NSColor(red: 0.855, green: 0.859, blue: 0.871, alpha: 1).cgColor
  243. }
  244. }
  245. func updateInterface(_ file: KMBatchOperateFile) {
  246. self.file = file
  247. if self.type == .Size {
  248. self.textField?.stringValue = file.sizeString
  249. } else if self.type == .FileName {
  250. if (file.error != nil) {
  251. self.errorTextField?.stringValue = file.error!.localizedDescription
  252. self.errorTextField?.isHidden = false
  253. } else {
  254. self.errorTextField?.isHidden = true
  255. }
  256. self.textField?.stringValue = file.filePath.lastPathComponent
  257. } else if self.type == .PageRange {
  258. self.pageRangeCombobox?.isEnabled = self.file!.status != .processing
  259. self.pageRangeCombobox?.delegate = nil
  260. self.pageRangeCombobox?.selectItem(at: file.currentOperateInfo?.pageChoice.rawValue ?? 0)
  261. self.pageRangeCombobox?.isEditable = false
  262. if file.currentOperateInfo?.pageChoice == .Input {
  263. self.pageRangeCombobox?.isEditable = true
  264. self.pageRangeCombobox?.stringValue = file.currentOperateInfo?.pageRangeString ?? ""
  265. }
  266. self.pageRangeCombobox?.delegate = self
  267. } else if self.type == .Status {
  268. if file.status == .Waiting {
  269. self.indicateImageView?.isHidden = false
  270. self.progressIndicator?.isHidden = true
  271. self.indicateImageView?.image = NSImage(named: KMImageNameUXIconProgressWaiting)
  272. } else if file.status == .Success {
  273. self.indicateImageView?.isHidden = false
  274. self.progressIndicator?.isHidden = true
  275. self.indicateImageView?.image = NSImage(named: "KMImageNameUXIconProgressComplete")
  276. } else if file.status == .Failed {
  277. self.indicateImageView?.isHidden = false
  278. self.progressIndicator?.isHidden = true
  279. self.indicateImageView?.image = NSImage(named: "KMImageNameUXIconProgressFailure")
  280. } else if file.status == .processing {
  281. self.indicateImageView?.isHidden = true
  282. self.progressIndicator?.isHidden = false
  283. self.progressIndicator?.startAnimation(nil)
  284. } else {
  285. self.indicateImageView?.isHidden = false
  286. self.progressIndicator?.isHidden = true
  287. self.indicateImageView?.image = NSImage(named: KMImageNameUXIconProgressWaiting)
  288. }
  289. } else if self.type == .DPI {
  290. switch self.file?.dpi {
  291. case 50:
  292. self.DPIComboBox?.selectItem(at: 0)
  293. case 72:
  294. self.DPIComboBox?.selectItem(at: 1)
  295. case 96:
  296. self.DPIComboBox?.selectItem(at: 2)
  297. case 150:
  298. self.DPIComboBox?.selectItem(at: 3)
  299. case 300:
  300. self.DPIComboBox?.selectItem(at: 4)
  301. case 600:
  302. self.DPIComboBox?.selectItem(at: 5)
  303. default:
  304. break
  305. }
  306. self.DPIComboBox?.isEnabled = self.file?.status != .processing
  307. } else if self.type == .Dimensions {
  308. }
  309. }
  310. func updateInterface( file: KMBatchOperateFile, progress: Float) {
  311. updateInterface(file)
  312. if progress > 0 && file.status == .processing {
  313. self.progressIndicator?.doubleValue = Double(progress)
  314. self.progressIndicator?.startAnimation(nil)
  315. self.progressIndicator?.isIndeterminate = false
  316. }
  317. }
  318. func updateInterface(isProgress progress: Int) {
  319. if progress == -1 {
  320. self.indicateImageView?.isHidden = false
  321. self.progressIndicator?.isHidden = true
  322. self.indicateImageView?.image = NSImage(named: "KMImageNameUXIconProgressFailure")
  323. } else if progress == 0 {
  324. self.indicateImageView?.isHidden = false
  325. self.progressIndicator?.isHidden = true
  326. self.indicateImageView?.image = NSImage(named: KMImageNameUXIconProgressWaiting)
  327. } else {
  328. self.indicateImageView?.isHidden = false
  329. self.progressIndicator?.isHidden = true
  330. self.indicateImageView?.image = NSImage(named: "KMImageNameUXIconProgressComplete")
  331. }
  332. }
  333. func controlTextDidEndEditing(_ obj: Notification) {
  334. if let data = self.pageRangeCombobox?.isEqual(obj.object), data {
  335. self.file?.currentOperateInfo?.pageRangeString = self.pageRangeCombobox!.stringValue
  336. if self.file?.currentOperateInfo?.pagesArray == nil {
  337. let alert = NSAlert()
  338. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  339. alert.alertStyle = .critical
  340. alert.messageText = self.file!.filePath.lastPathComponent.lastPathComponent + NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  341. alert.beginSheetModal(for: self.window!, completionHandler: nil)
  342. self.pageRangeCombobox?.selectItem(at: 0)
  343. self.file?.currentOperateInfo?.pageChoice = .All
  344. }
  345. }
  346. }
  347. func controlTextDidChange(_ obj: Notification) {
  348. let comboBox: NSComboBox = obj.object as! NSComboBox;
  349. if comboBox.isEqual(self.pageRangeCombobox) {
  350. self.comboBoxContent = comboBox.stringValue;
  351. }
  352. }
  353. func comboBoxSelectionDidChange(_ notification: Notification) {
  354. if notification.object as? NSComboBox == self.pageRangeCombobox {
  355. self.pageRangeCombobox?.delegate = nil
  356. self.file?.currentOperateInfo!.pageChoice = self.tromsformPageChoice(index: self.pageRangeCombobox?.indexOfSelectedItem ?? 0)
  357. if self.pageRangeCombobox?.indexOfSelectedItem == KMBatchOperatePageChoice.Input.rawValue {
  358. self.pageRangeCombobox?.isEditable = true
  359. self.pageRangeCombobox?.isSelectable = true
  360. self.window?.makeFirstResponder(self.pageRangeCombobox)
  361. } else {
  362. self.pageRangeCombobox?.resignFirstResponder()
  363. self.pageRangeCombobox?.isEditable = false
  364. self.pageRangeCombobox?.isSelectable = false
  365. }
  366. self.pageRangeCombobox?.delegate = self
  367. } else if notification.object as? NSComboBox == self.DPIComboBox {
  368. switch self.DPIComboBox?.indexOfSelectedItem
  369. {
  370. case 0:
  371. self.file?.currentConvertParameter?.dpi = 50
  372. case 1:
  373. self.file?.currentConvertParameter?.dpi = 72
  374. case 2:
  375. self.file?.currentConvertParameter?.dpi = 96
  376. case 3:
  377. self.file?.currentConvertParameter?.dpi = 150
  378. case 4:
  379. self.file?.currentConvertParameter?.dpi = 300
  380. case 5:
  381. self.file?.currentConvertParameter?.dpi = 600
  382. default:
  383. break
  384. }
  385. }
  386. }
  387. func tromsformPageChoice(index: Int) -> KMBatchOperatePageChoice {
  388. var pageChoice: KMBatchOperatePageChoice?
  389. switch index {
  390. case 0:
  391. pageChoice = .All
  392. case 1:
  393. pageChoice = .Odd
  394. case 2:
  395. pageChoice = .Even
  396. case 3:
  397. pageChoice = .Input
  398. default:
  399. pageChoice = .All
  400. }
  401. return pageChoice ?? .All
  402. }
  403. }