KMPageEditSplitSettingView.swift 16 KB

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