KMPageEditSplitSettingView.swift 16 KB

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