KMURLToPDFWindowController.swift 18 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. //
  280. // } completionHandler: { successArray, failArray in
  281. // if failArray.count < 1 {
  282. // let filePath: String = successArray.first as! String
  283. // if FileManager.default.fileExists(atPath: filePath) {
  284. // self.filePath = filePath
  285. // NSApp.mainWindow!.endSheet(self.window!)
  286. // self.window?.orderOut(self)
  287. // }
  288. // } else {
  289. // let alert = NSAlert()
  290. // alert.alertStyle = .critical
  291. // alert.informativeText = NSLocalizedString("Conversion Failed", comment: "")
  292. // alert.messageText = NSLocalizedString("", comment: "")
  293. // alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  294. // alert.runModal()
  295. // }
  296. // }
  297. // }
  298. }
  299. }
  300. @IBAction func comboBoxItemClick_PageSize(_ sender: KMPopUpButton) {
  301. switch pageSizePopUpButton.indexOfSelectedItem {
  302. case 0:
  303. pageSize = CGSize(width: 0, height: 0)
  304. break
  305. case 1:
  306. pageSize = CGSize(width: 4760, height: 6736)
  307. break
  308. case 2:
  309. pageSize = CGSize(width: 3368, height: 4760)
  310. break
  311. case 3:
  312. pageSize = CGSize(width: 2380, height: 3368)
  313. break
  314. case 4:
  315. pageSize = CGSize(width: 1684, height: 2380)
  316. break
  317. case 5:
  318. pageSize = CGSize(width: 1190, height: 1684)
  319. break
  320. case 6:
  321. pageSize = CGSize(width: 842, height: 1190)
  322. break
  323. case 7:
  324. pageSize = CGSize(width: 595, height: 842)
  325. break
  326. case 8:
  327. pageSize = CGSize(width: 420, height: 595)
  328. break
  329. case 9:
  330. pageSize = CGSize(width: 298, height: 420)
  331. break
  332. default:
  333. pageSize = CGSize(width: 210, height: 298)
  334. break
  335. }
  336. if (pageSizePopUpButton.indexOfSelectedItem != 0) {
  337. stepperVC.text = "0"
  338. stepperVC.enabled = true
  339. } else {
  340. stepperVC.text = "0"
  341. stepperVC.enabled = false
  342. }
  343. refreshUI()
  344. }
  345. }
  346. extension KMURLToPDFWindowController: KMStepperViewDelegate {
  347. func km_controlTextDidEndEditing(_ obj: KMStepperView) {
  348. if obj == stepperVC {
  349. var string: String = obj.text
  350. let unitScanle: Float = (595.0/21.0) * 2.54
  351. if (string.stringToCGFloat() <= 0) {
  352. stepperVC.text = "0"
  353. string = "0"
  354. } else if (Float(string.stringToCGFloat()) * unitScanle > Float(pageSize.width)/2) {
  355. let maxF = pageSize.width / (string.stringToCGFloat() * 2)
  356. stepperVC.text = String(format: "%f",maxF)
  357. string = String(format: "%f",maxF)
  358. }
  359. gap = Float(formatFloat(Float(string.stringToCGFloat()) * unitScanle))!
  360. refreshUI()
  361. }
  362. }
  363. func km_controlTextDidChange(_ obj: KMStepperView) {
  364. if obj == stepperVC {
  365. if (urlButtonVC.state == .Checked) {
  366. if (urlTextfieldVC.stringValue.count > 0) {
  367. // openVC.enabled = true
  368. } else {
  369. // openVC.enabled = false
  370. }
  371. } else {
  372. if (fileTextfieldVC.stringValue.count > 0) {
  373. // openVC.enabled = true
  374. } else {
  375. // openVC.enabled = false
  376. }
  377. }
  378. }
  379. refreshUI()
  380. }
  381. func km_controlClick(_ obj: KMStepperView) {
  382. if (obj.text.stringToCGFloat() < 0) {
  383. stepperVC.text = "0"
  384. }
  385. let unitScanle = (595.0/21.0) * 2.54
  386. let formatter = formatFloat(Float(obj.text.stringToCGFloat() * unitScanle))
  387. gap = Float(formatter)!
  388. refreshUI()
  389. }
  390. }
  391. extension KMURLToPDFWindowController: KMTextfieldVCDelegate {
  392. func km_controlTextDidEndEditing(_ obj: KMTextfieldVC) {
  393. }
  394. func km_controlTextDidChange(_ obj: KMTextfieldVC) {
  395. if urlButtonVC.state == .Checked {
  396. if urlTextfieldVC.stringValue.count > 0 {
  397. openVC.enabled = true
  398. refreshUI()
  399. } else {
  400. openVC.enabled = false
  401. refreshUI()
  402. }
  403. } else {
  404. if fileTextfieldVC.stringValue.count > 0 {
  405. openVC.enabled = true
  406. refreshUI()
  407. } else {
  408. openVC.enabled = false
  409. refreshUI()
  410. }
  411. }
  412. }
  413. }