KMPageEditSplitSettingView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //
  2. // KMPageEditSplitSettingView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/1/12.
  6. //
  7. class KMPageEditSplitSettingView: KMPageEditSettingBaseView {
  8. @IBOutlet weak var typeLabel: NSTextField!
  9. @IBOutlet weak var oneFileForOnePageRadio: NSButton!
  10. @IBOutlet weak var oneFileForOnePageView: NSView!
  11. @IBOutlet weak var oneFileForOnePageOtherLabel: NSTextField!
  12. private var oneFileForOnePageVC: KMDesignButton?
  13. private var oneFileForOnePageStepperView: KMDesignStepperView?
  14. @IBOutlet weak var splitMuitiFileRadio: NSButton!
  15. @IBOutlet weak var splitMuitiFileView: NSView!
  16. @IBOutlet weak var splitMuitiFileOtherLabel: NSTextField!
  17. private var splitMuitiFileVC: KMDesignButton?
  18. private var splitMuitiFileStepperView: KMDesignStepperView?
  19. @IBOutlet weak var pageRangeRadio: NSButton!
  20. @IBOutlet weak var pageRangeComboBox: NSComboBox!
  21. private var pageRangeVC: KMDesignButton?
  22. private var pageRangeComboVC: KMDesignSelect?
  23. @IBOutlet weak var nameLabel: NSTextField!
  24. @IBOutlet weak var nameView: NSView!
  25. @IBOutlet weak var nameViewLabel: NSTextField!
  26. @IBOutlet weak var prefixForOrigFileNameCheck: NSButton!
  27. @IBOutlet weak var tagCheck: NSButton!
  28. @IBOutlet weak var sepCheck: NSButton!
  29. @IBOutlet weak var tagView: NSView!
  30. @IBOutlet weak var sepView: NSView!
  31. private var prefixForOrigFileNameVC: KMDesignButton?
  32. private var tagVC: KMDesignButton?
  33. private var sepVC: KMDesignButton?
  34. private var tTextField: KMDesignTextField?
  35. private var sTextField: KMDesignTextField?
  36. var type: Int {
  37. get {
  38. -1
  39. }
  40. set {
  41. if (newValue == 0) {
  42. self.safe_selectRadio(sender: self.oneFileForOnePageVC)
  43. } else if (newValue == 1) {
  44. self.safe_selectRadio(sender: self.splitMuitiFileVC)
  45. } else if (newValue == 2) {
  46. self.safe_selectRadio(sender: self.pageRangeVC)
  47. }
  48. }
  49. }
  50. var pageCount: Int = 0 {
  51. didSet {
  52. self.oneFileForOnePageStepperView?.maxValue = Double(self.pageCount)
  53. self.splitMuitiFileStepperView?.maxValue = Double(self.pageCount)
  54. }
  55. }
  56. override func awakeFromNib() {
  57. super.awakeFromNib()
  58. self.typeLabel.stringValue = NSLocalizedString("Split Method", comment: "")
  59. self.typeLabel.textColor = KMAppearance.subtitleColor()
  60. self.typeLabel.font = .SFProTextSemiboldFont(12)
  61. self.oneFileForOnePageOtherLabel.stringValue = NSLocalizedString("page(s)", comment: "")
  62. self.oneFileForOnePageVC = KMDesignButton(withType: .RadioButton)
  63. self.oneFileForOnePageRadio.addSubview(self.oneFileForOnePageVC!.view)
  64. self.oneFileForOnePageVC?.view.frame = self.oneFileForOnePageRadio.bounds
  65. self.oneFileForOnePageVC?.view.autoresizingMask = [.width, .height]
  66. self.oneFileForOnePageVC?.target = self
  67. self.oneFileForOnePageVC?.action = #selector(oneFileForOnePageAction)
  68. self.oneFileForOnePageVC?.stringValue = NSLocalizedString("Split by every", comment: "")
  69. self.oneFileForOnePageStepperView = KMDesignStepperView()
  70. self.oneFileForOnePageStepperView?.minValue = 1
  71. self.oneFileForOnePageStepperView?.kmValue = 1
  72. self.oneFileForOnePageStepperView?.delegate = self
  73. self.oneFileForOnePageView.addSubview(self.oneFileForOnePageStepperView!)
  74. self.oneFileForOnePageStepperView?.frame = self.oneFileForOnePageView.bounds
  75. self.oneFileForOnePageStepperView?.autoresizingMask = [.width, .height]
  76. self.splitMuitiFileStepperView = KMDesignStepperView()
  77. self.splitMuitiFileStepperView?.minValue = 1
  78. self.splitMuitiFileStepperView?.kmValue = 1
  79. self.splitMuitiFileStepperView?.delegate = self
  80. self.splitMuitiFileView.addSubview(self.splitMuitiFileStepperView!)
  81. self.splitMuitiFileStepperView?.frame = self.splitMuitiFileView.bounds
  82. self.splitMuitiFileStepperView?.autoresizingMask = [.width, .height]
  83. self.splitMuitiFileOtherLabel.stringValue = NSLocalizedString("files", comment: "")
  84. self.splitMuitiFileVC = KMDesignButton(withType: .RadioButton)
  85. self.splitMuitiFileRadio.addSubview(self.splitMuitiFileVC!.view)
  86. self.splitMuitiFileVC?.view.frame = self.splitMuitiFileRadio.bounds
  87. self.splitMuitiFileVC?.view.autoresizingMask = [.width, .height]
  88. self.splitMuitiFileVC?.target = self
  89. self.splitMuitiFileVC?.action = #selector(splitMuitiFileAction)
  90. self.splitMuitiFileVC?.stringValue = NSLocalizedString("Split averagely to", comment: "")
  91. self.pageRangeComboBox.isEnabled = false
  92. self.pageRangeComboVC = KMDesignSelect(withType: .Combox)
  93. self.pageRangeComboBox.addSubview(self.pageRangeComboVC!.view)
  94. self.pageRangeComboVC?.view.frame = self.pageRangeComboBox.bounds
  95. self.pageRangeComboVC?.view.autoresizingMask = [.width, .height]
  96. self.pageRangeComboVC?.delete = self
  97. self.pageRangeComboVC?.addItems(withObjectValues: [KMTools.pageRangeTypeString(pageRange: .odd),
  98. KMTools.pageRangeTypeString(pageRange: .even),
  99. KMTools.pageRangeTypeString(pageRange: .custom)])
  100. self.pageRangeComboVC?.placeholderString = KMTools.pageRangePlaceholderString()
  101. self.pageRangeVC = KMDesignButton(withType: .RadioButton)
  102. self.pageRangeRadio.addSubview(self.pageRangeVC!.view)
  103. self.pageRangeVC?.view.frame = self.pageRangeRadio.bounds
  104. self.pageRangeVC?.view.autoresizingMask = [.width, .height]
  105. self.pageRangeVC?.target = self
  106. self.pageRangeVC?.action = #selector(pageRangeAction)
  107. self.pageRangeVC?.stringValue = NSLocalizedString("Split by page range", comment: "")
  108. self.safe_selectRadio(sender: self.oneFileForOnePageVC)
  109. self.nameLabel.stringValue = NSLocalizedString("File Name", comment: "")
  110. self.nameLabel.textColor = KMAppearance.subtitleColor()
  111. self.nameLabel.font = .SFProTextSemiboldFont(12)
  112. self.nameView.wantsLayer = true
  113. self.nameView.layer?.backgroundColor = KMAppearance.view_bg_dis_color().cgColor
  114. self.nameView.layer?.borderWidth = 1
  115. self.nameView.layer?.borderColor = KMAppearance.view_border_dis_color().cgColor
  116. self.nameView.layer?.cornerRadius = 4
  117. self.nameViewLabel.textColor = KMAppearance.titleColor()
  118. self.nameViewLabel.font = .SFProTextRegularFont(14)
  119. self.nameViewLabel.lineBreakMode = .byTruncatingTail
  120. self.prefixForOrigFileNameVC = KMDesignButton(withType: .CheckBox)
  121. self.prefixForOrigFileNameCheck.addSubview(self.prefixForOrigFileNameVC!.view)
  122. self.prefixForOrigFileNameVC?.view.frame = self.prefixForOrigFileNameCheck.bounds
  123. self.prefixForOrigFileNameVC?.view.autoresizingMask = [.width, .height]
  124. self.prefixForOrigFileNameVC?.target = self
  125. self.prefixForOrigFileNameVC?.action = #selector(prefixForOrigFileNameAction)
  126. self.prefixForOrigFileNameVC?.stringValue = NSLocalizedString("Show original file name in front", comment: "")
  127. self.safe_selectCheck(sender: self.prefixForOrigFileNameVC)
  128. self.tagVC = KMDesignButton(withType: .CheckBox)
  129. self.tagCheck.addSubview(self.tagVC!.view)
  130. self.tagVC?.view.frame = self.tagCheck.bounds
  131. self.tagVC?.view.autoresizingMask = [.width, .height]
  132. self.tagVC?.target = self
  133. self.tagVC?.action = #selector(tagAction)
  134. self.tagVC?.stringValue = NSLocalizedString("Label", comment: "")
  135. self.safe_selectCheck(sender: self.tagVC)
  136. self.tTextField = KMDesignTextField(designSize: .medium)
  137. self.tagView.addSubview(self.tTextField!)
  138. self.tTextField?.frame = self.tagView.bounds
  139. self.tTextField?.autoresizingMask = [.width, .height]
  140. self.tTextField?.stringValue = "part"
  141. self.tTextField?.delegate = self
  142. self.sTextField = KMDesignTextField(designSize: .medium)
  143. self.sepView.addSubview(self.sTextField!)
  144. self.sTextField?.frame = self.sepView.bounds
  145. self.sTextField?.autoresizingMask = [.width, .height]
  146. self.sTextField?.stringValue = "-"
  147. self.sTextField?.delegate = self
  148. self.sepVC = KMDesignButton(withType: .CheckBox)
  149. self.sepCheck.addSubview(self.sepVC!.view)
  150. self.sepVC?.view.frame = self.sepCheck.bounds
  151. self.sepVC?.view.autoresizingMask = [.width, .height]
  152. self.sepVC?.target = self
  153. self.sepVC?.action = #selector(sepAction)
  154. self.sepVC?.stringValue = NSLocalizedString("Separator", comment: "")
  155. self.safe_selectCheck(sender: self.sepVC)
  156. for view in [self.oneFileForOnePageView, self.splitMuitiFileView] {
  157. view?.wantsLayer = true
  158. view?.layer?.backgroundColor = NSColor(white: 238.0/255.0, alpha: 1.0).cgColor
  159. view?.layer?.cornerRadius = 2
  160. }
  161. }
  162. @objc private func oneFileForOnePageAction() {
  163. self.safe_selectRadio(sender: self.oneFileForOnePageVC)
  164. guard let callback = self.itemClick else {
  165. return
  166. }
  167. callback(1, 0)
  168. }
  169. @objc private func splitMuitiFileAction() {
  170. self.safe_selectRadio(sender: self.splitMuitiFileVC)
  171. guard let callback = self.itemClick else {
  172. return
  173. }
  174. callback(1, 1)
  175. }
  176. @objc private func pageRangeAction() {
  177. self.safe_selectRadio(sender: self.pageRangeVC)
  178. guard let callback = self.itemClick else {
  179. return
  180. }
  181. callback(1, 2)
  182. }
  183. @objc private func prefixForOrigFileNameAction() {
  184. self.safe_selectCheck(sender: self.prefixForOrigFileNameVC)
  185. guard let callback = self.itemClick else {
  186. return
  187. }
  188. var value = false
  189. if let sender = self.prefixForOrigFileNameVC {
  190. value = sender.state == .Checked
  191. }
  192. callback(6, value)
  193. }
  194. @objc private func tagAction() {
  195. self.safe_selectCheck(sender: self.tagVC)
  196. guard let callback = self.itemClick else {
  197. return
  198. }
  199. var value = false
  200. if let sender = self.tagVC {
  201. value = sender.state == .Checked
  202. }
  203. callback(7, value)
  204. }
  205. @objc private func sepAction() {
  206. self.safe_selectCheck(sender: self.sepVC)
  207. guard let callback = self.itemClick else {
  208. return
  209. }
  210. var value = false
  211. if let sender = self.sepVC {
  212. value = sender.state == .Checked
  213. }
  214. callback(8, value)
  215. }
  216. public func setPageRangeString(_ string: String) {
  217. self.pageRangeComboVC?.stringValue = string
  218. self.pageRangeComboVC?.editable = true
  219. self.window?.makeFirstResponder(self.pageRangeComboVC?.textField)
  220. }
  221. public func updateName(_ name: String) {
  222. self.nameViewLabel.stringValue = name
  223. }
  224. func selectCheck(sender: KMDesignButton) {
  225. sender.state = sender.state == .Checked ? .Norm : .Checked
  226. if (sender.isEqual(to: self.tagVC)) {
  227. self.tTextField?.kmEnabled = sender.state == .Checked
  228. } else if (sender.isEqual(to: self.sepVC)) {
  229. self.sTextField?.kmEnabled = sender.state == .Checked
  230. }
  231. }
  232. // MARK: Private Methods
  233. private func selectRadio(sender: KMDesignButton) {
  234. for radioVC in [self.oneFileForOnePageVC, self.splitMuitiFileVC, self.pageRangeVC] {
  235. if (sender.isEqual(to: radioVC)) {
  236. radioVC?.state = .Checked
  237. } else {
  238. radioVC?.state = .Norm
  239. }
  240. }
  241. if let state = self.oneFileForOnePageVC?.state, state == .Checked {
  242. self.oneFileForOnePageStepperView?.kmEnabled = true
  243. } else {
  244. self.oneFileForOnePageStepperView?.kmEnabled = false
  245. }
  246. if let state = self.splitMuitiFileVC?.state, state == .Checked {
  247. self.splitMuitiFileStepperView?.kmEnabled = true
  248. } else {
  249. self.splitMuitiFileStepperView?.kmEnabled = false
  250. }
  251. if let state = self.pageRangeVC?.state, state == .Checked {
  252. self.pageRangeComboVC?.enabled = true
  253. self.pageRangeComboVC?.editable = true
  254. self.window?.makeFirstResponder(self.pageRangeComboVC?.textField)
  255. } else {
  256. self.pageRangeComboVC?.enabled = false
  257. self.pageRangeComboVC?.editable = false
  258. self.window?.makeFirstResponder(nil)
  259. }
  260. }
  261. private func safe_selectRadio(sender: KMDesignButton?) {
  262. if let _radioVC = sender {
  263. self.selectRadio(sender: _radioVC)
  264. }
  265. }
  266. private func safe_selectCheck(sender: KMDesignButton?) {
  267. if let _checkVC = sender {
  268. self.selectCheck(sender: _checkVC)
  269. }
  270. }
  271. }
  272. extension KMPageEditSplitSettingView: KMTextFieldDelegate {
  273. func km_controlTextDidChange(textField: AnyObject) {
  274. if (textField.isEqual(to: self.tTextField)) {
  275. guard let callback = self.itemClick else {
  276. return
  277. }
  278. callback(9, self.tTextField!.stringValue as Any)
  279. } else if (textField.isEqual(to: self.sTextField)) {
  280. guard let callback = self.itemClick else {
  281. return
  282. }
  283. callback(10, self.sTextField!.stringValue as Any)
  284. } else if (textField.isEqual(to: self.oneFileForOnePageStepperView)) {
  285. guard let callback = self.itemClick else {
  286. return
  287. }
  288. var value: Int = 0
  289. if let _value = self.oneFileForOnePageStepperView?.kmValue {
  290. value = Int(_value)
  291. }
  292. callback(2, value)
  293. } else if (textField.isEqual(to: self.splitMuitiFileStepperView)) {
  294. guard let callback = self.itemClick else {
  295. return
  296. }
  297. var value: Int = 0
  298. if let _value = self.splitMuitiFileStepperView?.kmValue {
  299. value = Int(_value)
  300. }
  301. callback(3, value)
  302. }
  303. }
  304. }
  305. extension KMPageEditSplitSettingView: KMSelectPopButtonDelegate {
  306. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  307. if (obj.isEqual(to: self.pageRangeComboVC)) {
  308. var index = self.pageRangeComboVC!.indexOfSelectedItem
  309. if (index < 0) {
  310. index = 0
  311. }
  312. if index == obj.numberOfItems-1 {
  313. self.pageRangeComboVC?.editable = true
  314. DispatchQueue.main.async {
  315. self.pageRangeComboVC?.stringValue = ""
  316. }
  317. self.window?.makeFirstResponder(self.pageRangeComboVC?.textField)
  318. } else {
  319. self.window?.makeFirstResponder(nil)
  320. self.pageRangeComboVC?.editable = false
  321. }
  322. guard let callback = self.itemClick else {
  323. return
  324. }
  325. callback(4, index)
  326. }
  327. }
  328. func km_controlTextDidChange(_ obj: KMDesignSelect) {
  329. if (obj.isEqual(to: self.pageRangeComboVC)) {
  330. guard let callback = self.itemClick else {
  331. return
  332. }
  333. callback(5, self.pageRangeComboVC!.stringValue)
  334. }
  335. }
  336. }