KMURLToPDFWindowController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //
  2. // KMURLToPDFWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/1/29.
  6. //
  7. import Cocoa
  8. class KMURLToPDFWindowController: NSWindowController {
  9. @IBOutlet weak var title: NSTextField!
  10. @IBOutlet weak var urlView: NSView!
  11. @IBOutlet weak var urlTextFieldBox: NSBox!
  12. @IBOutlet weak var fileView: NSView!
  13. @IBOutlet weak var fileTextFieldBox: NSBox!
  14. @IBOutlet weak var fileButtonBox: NSBox!
  15. @IBOutlet weak var pageConfigurationView: NSView!
  16. @IBOutlet weak var pageConfigurationLabel: NSTextField!
  17. @IBOutlet weak var pageSizeLabel: NSTextField!
  18. @IBOutlet weak var pageSizePopUpButton: KMPopUpButton!
  19. @IBOutlet weak var spacingLanel: NSTextField!
  20. @IBOutlet weak var stepperBox: NSBox!
  21. @IBOutlet weak var mmLabel: NSTextField!
  22. @IBOutlet weak var cancelView: NSView!
  23. @IBOutlet weak var openView: NSView!
  24. var urlButtonVC: KMDesignButton!
  25. var urlTextfieldVC: KMTextfieldVC!
  26. var fileButtonVC: KMDesignButton!
  27. var fileTextfieldVC: KMTextfieldVC!
  28. var openFileVC: KMDesignButton!
  29. var stepperVC: KMStepperView!
  30. var cancelVC: KMDesignButton!
  31. var openVC: KMDesignButton!
  32. var filePath: String = ""
  33. var gap: Float = 0.0
  34. var pageSize: CGSize = NSZeroSize
  35. override func windowDidLoad() {
  36. super.windowDidLoad()
  37. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  38. urlButtonVC = KMDesignButton.init(withType: .RadioButton)
  39. urlTextfieldVC = KMTextfieldVC.init(nibName: "KMTextfieldVC", bundle: nil)
  40. fileButtonVC = KMDesignButton.init(withType: .RadioButton)
  41. fileTextfieldVC = KMTextfieldVC.init(nibName: "KMTextfieldVC", bundle: nil)
  42. openFileVC = KMDesignButton.init(withType: .Image)
  43. stepperVC = KMStepperView.init(nibName: "KMStepperView", bundle: nil)
  44. cancelVC = KMDesignButton.init(withType: .Text)
  45. openVC = KMDesignButton.init(withType: .Text)
  46. pageSizePopUpButton.removeAllItems()
  47. self.initializeUI()
  48. self.initLocalization()
  49. refreshUI()
  50. }
  51. // MARK: Init
  52. func initializeUI() {
  53. self.window?.backgroundColor = .white
  54. title.font = NSFont.boldSystemFont(ofSize: 16.0)
  55. title.textColor = NSColor(red: 37/255.0, green: 41/255.0, blue: 41/255.0, alpha: 1.0)
  56. urlView.addSubview(urlButtonVC.view)
  57. urlButtonVC.image = NSImage(named: "KMRadioButtonUnSelect")!
  58. urlButtonVC.target = self
  59. urlButtonVC.action = #selector(buttonChangeType)
  60. urlButtonVC.state = .Checked
  61. // let itemSpacing = KMDesignToken.shared.itemSpacing(withToken: "checkbox-radio.bg.def")
  62. // urlButtonVC.radio_label = itemSpacing.stringToCGFloat()
  63. // let typography = KMDesignToken.shared.typography(withToken: "checkbox-radio.mac-text.def")
  64. // let fill = KMDesignToken.shared.fill(withToken: "checkbox-radio.mac-text.def")
  65. urlTextFieldBox.contentView = urlTextfieldVC.view
  66. urlTextfieldVC.borderColor = .gray
  67. urlTextfieldVC.borderWidth = 1.0
  68. urlTextfieldVC.cornerRadius = 4.0
  69. urlTextfieldVC.width = 400.0
  70. urlTextfieldVC.height = 32.0
  71. urlTextfieldVC.editable = true
  72. urlTextfieldVC.becomeFirstResponder = true
  73. urlTextfieldVC.delete = self
  74. fileView.addSubview(fileButtonVC.view)
  75. fileButtonVC.image = NSImage(named: "KMRadioButtonUnSelect")!
  76. fileButtonVC.target = self
  77. fileButtonVC.action = #selector(buttonChangeType)
  78. fileTextFieldBox.contentView = fileTextfieldVC.view
  79. fileTextfieldVC.borderColor = .gray
  80. fileTextfieldVC.borderWidth = 1.0
  81. fileTextfieldVC.cornerRadius = 4.0
  82. fileTextfieldVC.width = 360.0
  83. fileTextfieldVC.height = 32.0
  84. fileTextfieldVC.editable = false
  85. fileTextfieldVC.delete = self
  86. fileButtonBox.contentView = openFileVC.view
  87. openFileVC.image = NSImage(named: "KMFileIcon")!
  88. openFileVC.borderColor = .gray
  89. openFileVC.borderWidth = 1.0
  90. openFileVC.cornerRadius = 4.0
  91. openFileVC.width = 32.0
  92. openFileVC.height = 32.0
  93. openFileVC.target = self
  94. openFileVC.action = #selector(selectFileAction)
  95. openFileVC.enabled = false
  96. stepperBox.contentView = stepperVC.view
  97. stepperVC.borderColor = .gray
  98. stepperVC.borderWidth = 1.0
  99. stepperVC.cornerRadius = 4.0
  100. stepperVC.width = 72.0
  101. stepperVC.height = 32.0
  102. stepperVC.delete = self
  103. stepperVC.enabled = false
  104. cancelView.addSubview(cancelVC.view)
  105. cancelVC.borderColor = .gray
  106. cancelVC.borderWidth = 1.0
  107. cancelVC.cornerRadius = 4.0
  108. cancelVC.width = 80.0
  109. cancelVC.height = 32.0
  110. cancelVC.textColor = .black
  111. cancelVC.target = self
  112. cancelVC.action = #selector(cancelAction)
  113. openView.addSubview(openVC.view)
  114. openVC.borderColor = .gray
  115. openVC.borderWidth = 1.0
  116. openVC.cornerRadius = 4.0
  117. openVC.width = 80.0
  118. openVC.height = 32.0
  119. openVC.background = NSColor(red: 23/255.0, green: 112/255.0, blue: 244/255.0, alpha: 1.0)
  120. openVC.textColor = .white
  121. openVC.target = self
  122. openVC.action = #selector(startAction)
  123. }
  124. func initLocalization() {
  125. title.stringValue = NSLocalizedString("New From Web Page", comment: "")
  126. urlButtonVC.stringValue = NSLocalizedString("URL", comment: "")
  127. urlTextfieldVC.placeholderString = NSLocalizedString("https://pdfreaderpro.com", comment: "")
  128. fileButtonVC.stringValue = NSLocalizedString("File", comment: "")
  129. fileTextfieldVC.placeholderString = NSLocalizedString("Select Files(.html,.webarchive)", comment: "")
  130. pageConfigurationLabel.stringValue = NSLocalizedString("Page Configuration", comment: "")
  131. pageSizeLabel.stringValue = NSLocalizedString("Page Size:", comment: "")
  132. spacingLanel.stringValue = NSLocalizedString("Spacing:", comment: "")
  133. mmLabel.stringValue = NSLocalizedString("mm:", comment: "")
  134. cancelVC.stringValue = NSLocalizedString("Cancel", comment: "")
  135. openVC.stringValue = NSLocalizedString("Open", comment: "")
  136. pageSizePopUpButton.addItems(withTitles: [NSLocalizedString("Automatic resizing", comment: ""),
  137. "4A0 1682 × 2378 mm",
  138. "2A0 1189 × 1682 mm",
  139. "A0 841 × 1189 mm",
  140. "A1 594 × 841 mm",
  141. "A2 420 × 594 mm",
  142. "A3 297 × 420 mm",
  143. "A4 210 × 297 mm",
  144. "A5 148 × 210 mm",
  145. "A6 105 × 148 mm",
  146. "A7 74 × 105 mm",
  147. "A8 52 × 74 mm",
  148. "A9 37 × 52 mm",
  149. "A10 26 × 37 mm"])
  150. }
  151. func refreshUI() -> Void {
  152. urlButtonVC.updateUI()
  153. urlTextfieldVC.updateUI()
  154. fileButtonVC.updateUI()
  155. fileTextfieldVC.updateUI()
  156. openFileVC.updateUI()
  157. stepperVC.updateUI()
  158. cancelVC.updateUI()
  159. openVC.updateUI()
  160. }
  161. // MARK: Private Methods
  162. func kUrlToPDFFolderPath() -> String {
  163. // let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
  164. // let append2 = searchPath!.stringByAppendingPathComponent("WebPage")
  165. let append1: NSString = "WebPage"
  166. let append2 = append1.kUrlToPDFFolderPath()
  167. return append2!
  168. }
  169. @objc func beginSheetModal(for window: NSWindow, completionHandler handler: ((_ filePath: String) -> Void)?) {
  170. window.beginSheet(self.window!) { [weak self] returnCode in
  171. if self != nil {
  172. handler!(self!.filePath)
  173. }
  174. }
  175. }
  176. func formatFloat(_ f: Float) -> String {
  177. if (fmodf(f, 1) == 0) { //如果有一位小数点
  178. return String(format: "%.0f",f)
  179. } else if (fmodf(f*10, 1) == 0) { //如果有两位小数点
  180. return String(format: "%.1f",f)
  181. } else {
  182. return String(format: "%.2f",f)
  183. }
  184. }
  185. // MARK: Action
  186. @objc func buttonChangeType(_ sender: NSButton) -> Void {
  187. if (sender == urlButtonVC.button) {
  188. if urlButtonVC.state == .Norm {
  189. urlButtonVC.image = NSImage(named: "KMRadioButtonSelect")!
  190. urlButtonVC.state = .Checked
  191. fileButtonVC.image = NSImage(named: "KMRadioButtonUnSelect")!
  192. fileButtonVC.state = .Norm
  193. urlTextfieldVC.editable = true
  194. urlTextfieldVC.becomeFirstResponder = true
  195. fileTextfieldVC.stringValue = ""
  196. openFileVC.enabled = false
  197. if urlTextfieldVC.stringValue.count > 0 {
  198. // openVC.enabled = true
  199. } else {
  200. // openVC.enabled = false
  201. }
  202. }
  203. } else if (sender == fileButtonVC.button) {
  204. if fileButtonVC.state == .Norm {
  205. urlButtonVC.image = NSImage(named: "KMRadioButtonUnSelect")!
  206. urlButtonVC.state = .Norm
  207. fileButtonVC.image = NSImage(named: "KMRadioButtonSelect")!
  208. fileButtonVC.state = .Checked
  209. urlTextfieldVC.editable = false
  210. urlTextfieldVC.stringValue = ""
  211. openFileVC.enabled = true
  212. if fileTextfieldVC.stringValue.count > 0 {
  213. // openVC.enabled = true
  214. } else {
  215. // openVC.enabled = false
  216. }
  217. }
  218. }
  219. refreshUI()
  220. }
  221. @objc func selectFileAction(_ sender: NSButton) -> Void {
  222. urlButtonVC.state = .Norm
  223. fileButtonVC.state = .Checked
  224. urlTextfieldVC.editable = false
  225. urlTextfieldVC.stringValue = ""
  226. // openVC.enabled = false
  227. let openPanel = NSOpenPanel()
  228. openPanel.canChooseFiles = true
  229. openPanel.canChooseDirectories = false
  230. openPanel.allowsMultipleSelection = false
  231. openPanel.allowedFileTypes = ["HTML","html","webarchive","htm"]
  232. openPanel.beginSheetModal(for: self.window!) { [self] response in
  233. if response == .OK {
  234. fileTextfieldVC.stringValue = openPanel.url!.path
  235. // openVC.enabled = true
  236. refreshUI()
  237. }
  238. }
  239. }
  240. @objc func cancelAction(_ sender: NSButton) -> Void {
  241. if (sender == cancelVC.button) {
  242. NSApp.mainWindow!.endSheet(self.window!)
  243. self.window?.orderOut(self)
  244. }
  245. }
  246. @objc func startAction(_ sender: NSButton) -> Void {
  247. if (sender == openVC.button) {
  248. if FileManager.default.fileExists(atPath: kUrlToPDFFolderPath()) {
  249. try? FileManager.default.createDirectory(atPath: kUrlToPDFFolderPath(), withIntermediateDirectories: false)
  250. }
  251. var url: URL
  252. var fileName: String = ""
  253. if urlButtonVC.state == .Checked {
  254. let urlstring = urlTextfieldVC.stringValue
  255. var tUrl = URL(string: urlstring)
  256. if tUrl?.scheme == nil {
  257. tUrl = URL(string: String(format: "http://%@", urlstring))
  258. }
  259. url = tUrl!
  260. } else {
  261. url = URL(fileURLWithPath: fileTextfieldVC.stringValue)
  262. fileName = fileTextfieldVC.stringValue.lastPathComponent.deletingPathExtension.lastPathComponent
  263. }
  264. var string = stepperVC.text
  265. let unitScanle: Float = (595.0/21.0) * 2.54
  266. if Float(string)! <= 0 {
  267. stepperVC.text = "0"
  268. string = "0"
  269. } else if Float(string)! * unitScanle > Float(pageSize.width)/2 {
  270. let maxF = pageSize.width/(string.stringToCGFloat() * 2)
  271. stepperVC.text = String(format: "%f",maxF)
  272. string = String(format: "%f",maxF)
  273. }
  274. gap = Float(formatFloat(Float(string.stringToCGFloat()) * unitScanle))!
  275. if url != nil {
  276. let convert = KMConvertURLToPDF.shareInstance()
  277. convert.fileName = fileName
  278. convert.convertUrl(toPDF: [url], toPath: kUrlToPDFFolderPath(), pageSize: pageSize, gap: CGFloat(gap)) { value in
  279. } completionHandler: { successArray, failArray in
  280. if failArray.count < 1 {
  281. let filePath: String = successArray.first as! String
  282. if FileManager.default.fileExists(atPath: filePath) {
  283. self.filePath = filePath
  284. NSApp.mainWindow!.endSheet(self.window!)
  285. self.window?.orderOut(self)
  286. }
  287. } else {
  288. let alert = NSAlert()
  289. alert.alertStyle = .critical
  290. alert.informativeText = NSLocalizedString("Conversion Failed", comment: "")
  291. alert.messageText = NSLocalizedString("", comment: "")
  292. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  293. alert.runModal()
  294. }
  295. }
  296. }
  297. }
  298. }
  299. @IBAction func comboBoxItemClick_PageSize(_ sender: KMPopUpButton) {
  300. switch pageSizePopUpButton.indexOfSelectedItem {
  301. case 0:
  302. pageSize = CGSize(width: 0, height: 0)
  303. break
  304. case 1:
  305. pageSize = CGSize(width: 4760, height: 6736)
  306. break
  307. case 2:
  308. pageSize = CGSize(width: 3368, height: 4760)
  309. break
  310. case 3:
  311. pageSize = CGSize(width: 2380, height: 3368)
  312. break
  313. case 4:
  314. pageSize = CGSize(width: 1684, height: 2380)
  315. break
  316. case 5:
  317. pageSize = CGSize(width: 1190, height: 1684)
  318. break
  319. case 6:
  320. pageSize = CGSize(width: 842, height: 1190)
  321. break
  322. case 7:
  323. pageSize = CGSize(width: 595, height: 842)
  324. break
  325. case 8:
  326. pageSize = CGSize(width: 420, height: 595)
  327. break
  328. case 9:
  329. pageSize = CGSize(width: 298, height: 420)
  330. break
  331. default:
  332. pageSize = CGSize(width: 210, height: 298)
  333. break
  334. }
  335. if (pageSizePopUpButton.indexOfSelectedItem != 0) {
  336. stepperVC.text = "0"
  337. stepperVC.enabled = true
  338. } else {
  339. stepperVC.text = "0"
  340. stepperVC.enabled = false
  341. }
  342. refreshUI()
  343. }
  344. }
  345. extension KMURLToPDFWindowController: KMStepperViewDelegate {
  346. func km_controlTextDidEndEditing(_ obj: KMStepperView) {
  347. if obj == stepperVC {
  348. var string: String = obj.text
  349. let unitScanle: Float = (595.0/21.0) * 2.54
  350. if (string.stringToCGFloat() <= 0) {
  351. stepperVC.text = "0"
  352. string = "0"
  353. } else if (Float(string.stringToCGFloat()) * unitScanle > Float(pageSize.width)/2) {
  354. let maxF = pageSize.width / (string.stringToCGFloat() * 2)
  355. stepperVC.text = String(format: "%f",maxF)
  356. string = String(format: "%f",maxF)
  357. }
  358. gap = Float(formatFloat(Float(string.stringToCGFloat()) * unitScanle))!
  359. refreshUI()
  360. }
  361. }
  362. func km_controlTextDidChange(_ obj: KMStepperView) {
  363. if obj == stepperVC {
  364. if (urlButtonVC.state == .Checked) {
  365. if (urlTextfieldVC.stringValue.count > 0) {
  366. // openVC.enabled = true
  367. } else {
  368. // openVC.enabled = false
  369. }
  370. } else {
  371. if (fileTextfieldVC.stringValue.count > 0) {
  372. // openVC.enabled = true
  373. } else {
  374. // openVC.enabled = false
  375. }
  376. }
  377. }
  378. refreshUI()
  379. }
  380. func km_controlClick(_ obj: KMStepperView) {
  381. if (obj.text.stringToCGFloat() < 0) {
  382. stepperVC.text = "0"
  383. }
  384. let unitScanle = (595.0/21.0) * 2.54
  385. let formatter = formatFloat(Float(obj.text.stringToCGFloat() * unitScanle))
  386. gap = Float(formatter)!
  387. refreshUI()
  388. }
  389. }
  390. extension KMURLToPDFWindowController: KMTextfieldVCDelegate {
  391. func km_controlTextDidEndEditing(_ obj: KMTextfieldVC) {
  392. }
  393. func km_controlTextDidChange(_ obj: KMTextfieldVC) {
  394. if urlButtonVC.state == .Checked {
  395. if urlTextfieldVC.stringValue.count > 0 {
  396. openVC.enabled = true
  397. refreshUI()
  398. } else {
  399. openVC.enabled = false
  400. refreshUI()
  401. }
  402. } else {
  403. if fileTextfieldVC.stringValue.count > 0 {
  404. openVC.enabled = true
  405. refreshUI()
  406. } else {
  407. openVC.enabled = false
  408. refreshUI()
  409. }
  410. }
  411. }
  412. }