KMHomeViewController.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. //
  2. // KMHomeViewController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/10/13.
  6. //
  7. import Cocoa
  8. @objc enum KMHomeToolState : Int {
  9. case OpenPDF = 0
  10. case CreatePDF
  11. case Home
  12. case PDFTools
  13. case FavoriteDocuments
  14. case CloudDocuments
  15. }
  16. @objc enum KMAIHomeToolState : Int {
  17. case Home = 0
  18. case AITranslation
  19. case AIRewriting
  20. case AIErrorCorrection
  21. }
  22. @objcMembers class KMHomeViewController: NSViewController {
  23. @IBOutlet weak var leftBox: NSBox!
  24. @IBOutlet weak var rightBox: NSBox!
  25. @IBOutlet weak var homeSplitView: NSSplitView!
  26. @IBOutlet weak var homeRightScrollViewView: NSScrollView!
  27. @IBOutlet weak var rightTopBox: NSBox!
  28. @IBOutlet weak var rightBottomBox: NSBox!
  29. @IBOutlet weak var rightFullBox: NSBox!
  30. @IBOutlet weak var rightTopBoxHeightConstraint: NSLayoutConstraint!
  31. @IBOutlet var fastToolViewController: KMHomeFastToolViewController!
  32. @IBOutlet var historyFileViewController: KMHomeHistoryFileViewController!
  33. @IBOutlet var pdfToolsViewController: KMPDFToolsViewController!
  34. @IBOutlet var cloudDocumentsViewController: KMCloudDocumentsViewController!
  35. @IBOutlet weak var dragView: KMHomeDragView!
  36. // AI 临时UI
  37. @IBOutlet weak var aiHomeBox: KMBox!
  38. @IBOutlet weak var aiHomeImageView: NSImageView!
  39. @IBOutlet weak var aiHomeLabel: NSTextField!
  40. @IBOutlet weak var aiTranslationBox: KMBox!
  41. @IBOutlet weak var aiTranslationImageView: NSImageView!
  42. @IBOutlet weak var aiTranslationLabel: NSTextField!
  43. @IBOutlet weak var aiRewritingBox: KMBox!
  44. @IBOutlet weak var aiRewritingImageView: NSImageView!
  45. @IBOutlet weak var aiRewritingLabel: NSTextField!
  46. @IBOutlet weak var aiErrorCorrectionBox: KMBox!
  47. @IBOutlet weak var aiErrorCorrectionImageView: NSImageView!
  48. @IBOutlet weak var aiErrorCorrectionLabel: NSTextField!
  49. @IBOutlet var aiOpenPDFFilesViewController: KMAIOpenPDFFilesVC!
  50. @IBOutlet var aiTranslationViewController: KMAITranslationVC!
  51. @IBOutlet var aiRewritingViewController: KMAIRewritingVC!
  52. @IBOutlet weak var creatPDFView: KMCreatPDFView!
  53. //广告 互推
  54. @IBOutlet weak var advertisementTableView: KMAdvertisementTableView!
  55. var aiHomeState: KMAIHomeToolState = .Home
  56. let ScrollerViewWidget: CGFloat = 15.0
  57. var homeWindowController : MainWindowController!
  58. // 产品推广数据
  59. var productPromotionPDFProSeries: NSArray!
  60. var productPromotionOthers: NSArray!
  61. var productPromotionData: NSDictionary!
  62. var isShowQuickTour: Bool = false
  63. var myDocument: NSDocument?
  64. var currentWindowController: NSWindowController?
  65. var homeState: KMHomeToolState = .Home
  66. var urlToPDFWindowController: KMURLToPDFWindowController?
  67. var openPDFButtonVC: KMDesignButton!
  68. var createPDFButtonVC: KMDesignButton!
  69. var createPDFImage: KMDesignButton!
  70. var createPDFAddButtonVC: KMDesignButton!
  71. var homeButtonVC: KMTextImageButtonVC!
  72. var pdfToolsButtonVC: KMTextImageButtonVC!
  73. var cloudDocumentsButtonVC: KMTextImageButtonVC!
  74. var pdfSeriesButtonVC: KMTextImageButtonVC!
  75. var pdfOthersButtonVC: KMTextImageButtonVC!
  76. var popover: NSPopover?
  77. var deviceBrowserWC: KMDeviceBrowserWindowController?
  78. var progressController: SKProgressController?
  79. var timer: Timer?
  80. var timerCounter = 0.0
  81. @IBOutlet weak var rightBottonHeight: NSLayoutConstraint!
  82. deinit {
  83. }
  84. override func viewWillAppear() {
  85. super.viewWillAppear()
  86. if !KMLightMemberManager.manager.isLogin() {
  87. KMLightMemberManager.manager.canShowAdvancedView = true
  88. }
  89. }
  90. override func viewDidLoad() {
  91. super.viewDidLoad()
  92. // Do view setup here.
  93. openPDFButtonVC = KMDesignButton.init(withType: .Text)
  94. createPDFButtonVC = KMDesignButton.init(withType: .Text)
  95. createPDFImage = KMDesignButton.init(withType: .Image)
  96. createPDFAddButtonVC = KMDesignButton.init(withType: .Image)
  97. homeButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  98. pdfToolsButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  99. cloudDocumentsButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  100. pdfSeriesButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  101. pdfOthersButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  102. self.homeSplitView.setPosition(270.0, ofDividerAt: 0)
  103. self.leftBox.fillColor = NSColor.km_init(hex: "#F7F8FA")
  104. self.productPromotionPDFProSeries = ["Windows", "iPhone / iPad", "Android"]
  105. self.productPromotionOthers = ["ComPDFKit", "SignFlow", "FiImage Editor", "FiImage Screen", "Free PDF Templates"]
  106. self.productPromotionData = ["Windows": ["Name" : "Windows", "Image" : "icon_home_logo_windows", "link":""],
  107. "iPhone / iPad" : ["Name" : "iPhone / iPad", "Image" : "icon_home_logo_mac", "link":""],
  108. "Android" : ["Name" : "Android", "Image" : "icon_home_logo_android", "link":""],
  109. "ComPDFKit" : ["Name" : "ComPDFKit", "Image" : "icon_home_logo_comPDFKit", "link":""],
  110. "SignFlow" : ["Name" : "SignFlow", "Image" : "icon_home_logo_signflow", "link":""],
  111. "FiImage Editor" : ["Name" : "FiImage Editor", "Image" : "icon_home_logo_editor", "link":""],
  112. "FiImage Screen" : ["Name" : "FiImage Screen", "Image" : "icon_home_logo_screen", "link":""],
  113. "Free PDF Templates" : ["Name" : "Free PDF Templates", "Image" : "icon_home_logo_templates"], "link":""]
  114. NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { (aEvent) -> NSEvent? in
  115. self.otherMouseDown(with: aEvent)
  116. return aEvent
  117. }
  118. self.initNetworkingData()
  119. // self.initLocalization()
  120. // self.initializeUI()
  121. self.initializeUI_DMG()
  122. self.setup()
  123. // refreshUI()
  124. NotificationCenter.default.addObserver(self, selector: #selector(homeFileRectChange(_:)), name: Notification.Name("KMHomeFileRectChange"), object: nil)
  125. }
  126. override func viewDidAppear() {
  127. super.viewDidAppear()
  128. refreshScrollView()
  129. }
  130. override func viewDidLayout() {
  131. super.viewDidLayout()
  132. refreshScrollView()
  133. }
  134. func initNetworkingData() {
  135. KMAdvertisementManager.manager.fetchDataWithResponseObject { [unowned self] data, responseObject, error in
  136. KMPrint("获取广告数据成功")
  137. if data != nil {
  138. advertisementTableView.inputData = data!.recommondContent
  139. advertisementTableView.didSelect = { view, item in
  140. let string = KMAdvertisementModelTransition.transitionLanguage(langeuage: item.linkURL)
  141. if NSWorkspace.shared.open(URL.init(string: string)!) {
  142. NSWorkspace.shared.open(URL.init(string: string)!)
  143. }
  144. }
  145. }
  146. }
  147. }
  148. func setup() {
  149. self.creatPDFView.openPDFAction = { [unowned self] view, sender in
  150. self.openPDFAction(sender)
  151. }
  152. self.creatPDFView.creatPDFAction = { [unowned self] view, sender in
  153. self.creatPDFAction(sender)
  154. }
  155. }
  156. // MARK: Init
  157. func initializeUI() {
  158. // pdfToolsBox.fillColor = .clear
  159. // pdfToolsBox.contentView = pdfToolsButtonVC.view
  160. // pdfToolsButtonVC.target = self
  161. // pdfToolsButtonVC.action = #selector(self.homeToolAction(_:))
  162. // pdfToolsButtonVC.image = NSImage(named: "icon_home_sidebar_tool_false")!
  163. // pdfToolsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_tool_false")!
  164. // pdfToolsButtonVC.image_click = NSImage(named: "icon_home_sidebar_tool_true")!
  165. // pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: pdfToolsBoxHeight)
  166. // pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  167. // pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  168. //
  169. // cloudDocumentsBox.fillColor = .clear
  170. // cloudDocumentsBox.contentView = cloudDocumentsButtonVC.view
  171. // cloudDocumentsButtonVC.target = self
  172. // cloudDocumentsButtonVC.action = #selector(self.homeToolAction(_:))
  173. // cloudDocumentsButtonVC.image = NSImage(named: "icon_home_sidebar_cloud_false")!
  174. // cloudDocumentsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_cloud_false")!
  175. // cloudDocumentsButtonVC.image_click = NSImage(named: "icon_home_sidebar_cloud_true")!
  176. // cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: cloudDocumentsBoxHeight)
  177. // cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  178. // cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  179. //
  180. // pdfSeriesBox.fillColor = .clear
  181. // pdfSeriesBox.contentView = pdfSeriesButtonVC.view
  182. // pdfSeriesButtonVC.target = self
  183. // pdfSeriesButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:))
  184. // pdfSeriesButtonVC.type = .Right
  185. // pdfSeriesButtonVC.canHover = false
  186. // pdfSeriesButtonVC.imageWidth = 12.0
  187. // pdfSeriesButtonVC.imageHeight = 12.0
  188. // pdfSeriesButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  189. // pdfSeriesButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: pdfSeriesBoxHeight)
  190. //
  191. // pdfOthersBox.fillColor = .clear
  192. // pdfOthersBox.contentView = pdfOthersButtonVC.view
  193. // pdfOthersButtonVC.target = self
  194. // pdfOthersButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:))
  195. // pdfOthersButtonVC.type = .Right
  196. // pdfOthersButtonVC.canHover = false
  197. // pdfOthersButtonVC.imageWidth = 12.0
  198. // pdfOthersButtonVC.imageHeight = 12.0
  199. // pdfOthersButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  200. // pdfOthersButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: othersBoxHeightConstraint)
  201. // if self.activitiesBox.isHidden {
  202. // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true)
  203. // } else {
  204. // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: [], isInitialize: true)
  205. // }
  206. self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true)
  207. // self.activitiesBox.contentView = KMProductPromotionViewController.init().view
  208. // activitiesBox.cornerRadius = 8.0
  209. rightTopBox.fillColor = .white
  210. rightBottomBox.fillColor = .white
  211. rightFullBox.fillColor = .white
  212. // productPromotionBox.fillColor = .clear
  213. //
  214. // product_active_scrollView.verticalScrollElasticity = .none
  215. self.refreshRightBoxUI(.Home)
  216. fastToolViewController.delete = self
  217. historyFileViewController.delete = self
  218. pdfToolsViewController.delete = self
  219. dragView.delete = self
  220. }
  221. func initLocalization() {
  222. // self.favoriteDocumentsLabel.stringValue = NSLocalizedString("Favorite Documents", comment: "")
  223. homeButtonVC.stringValue = NSLocalizedString("Home", comment: "")
  224. pdfToolsButtonVC.stringValue = NSLocalizedString("PDF Tools", comment: "")
  225. cloudDocumentsButtonVC.stringValue = NSLocalizedString("Cloud Documents", comment: "")
  226. openPDFButtonVC.stringValue = NSLocalizedString("Open File", comment: "")
  227. // openPDFBox.toolTip = NSLocalizedString("Open PDF", comment: "")
  228. createPDFButtonVC.stringValue = NSLocalizedString("Create PDF", comment: "")
  229. // createPDFBox.toolTip = NSLocalizedString("Create PDF", comment: "")
  230. pdfSeriesButtonVC.stringValue = NSLocalizedString("PDF Pro Series", comment: "")
  231. pdfOthersButtonVC.stringValue = NSLocalizedString("Others", comment: "")
  232. }
  233. // MARK: Action
  234. func initializeUI_DMG() -> Void {
  235. rightTopBox.fillColor = .white
  236. rightBottomBox.fillColor = .white
  237. rightFullBox.fillColor = .white
  238. self.aiHomeBox.fillColor = .clear
  239. self.aiHomeBox.cornerRadius = 8.0
  240. self.aiHomeImageView.image = NSImage(named: "icon_home")
  241. self.aiHomeLabel.stringValue = NSLocalizedString("Home", comment: "")
  242. self.aiHomeLabel.textColor = NSColor.km_init(hex: "#252629")
  243. self.aiHomeLabel.font = NSFont.SFProTextRegularFont(16.0)
  244. self.aiHomeBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  245. if aiHomeState != .Home {
  246. if mouseEntered {
  247. self.aiHomeBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  248. } else {
  249. self.aiHomeBox.fillColor = .clear
  250. }
  251. }
  252. }
  253. self.aiHomeBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  254. if aiHomeState != .Home {
  255. if downEntered {
  256. self.aiHomeState = .Home
  257. self.refreshAIUI()
  258. }
  259. }
  260. }
  261. self.aiTranslationBox.fillColor = .clear
  262. self.aiTranslationBox.cornerRadius = 8.0
  263. self.aiTranslationImageView.image = NSImage(named: "ic_function_other")
  264. self.aiTranslationLabel.stringValue = NSLocalizedString("AI Translation", comment: "")
  265. self.aiTranslationLabel.textColor = NSColor.km_init(hex: "#252629")
  266. self.aiTranslationLabel.font = NSFont.SFProTextRegularFont(16.0)
  267. self.aiTranslationBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  268. if aiHomeState != .AITranslation {
  269. if mouseEntered {
  270. self.aiTranslationBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  271. } else {
  272. self.aiTranslationBox.fillColor = .clear
  273. }
  274. }
  275. }
  276. self.aiTranslationBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  277. if aiHomeState != .AITranslation {
  278. if downEntered {
  279. self.aiHomeState = .AITranslation
  280. self.trackEvent_ai(eventName: "AI Translation")
  281. self.refreshAIUI()
  282. }
  283. }
  284. }
  285. self.aiRewritingBox.fillColor = .clear
  286. self.aiRewritingBox.cornerRadius = 8.0
  287. self.aiRewritingImageView.image = NSImage(named: "ic_ai_Rewriting")
  288. self.aiRewritingLabel.stringValue = NSLocalizedString("AI Rewriting", comment: "")
  289. self.aiRewritingLabel.textColor = NSColor.km_init(hex: "#252629")
  290. self.aiRewritingLabel.font = NSFont.SFProTextRegularFont(16.0)
  291. self.aiRewritingBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  292. if aiHomeState != .AIRewriting {
  293. if mouseEntered {
  294. self.aiRewritingBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  295. } else {
  296. self.aiRewritingBox.fillColor = .clear
  297. }
  298. }
  299. }
  300. self.aiRewritingBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  301. if aiHomeState != .AIRewriting {
  302. if downEntered {
  303. self.aiHomeState = .AIRewriting
  304. self.trackEvent_ai(eventName: "AI Rewriting")
  305. self.refreshAIUI()
  306. }
  307. }
  308. }
  309. self.aiErrorCorrectionBox.fillColor = .clear
  310. self.aiErrorCorrectionBox.cornerRadius = 8.0
  311. self.aiErrorCorrectionImageView.image = NSImage(named: "ic_ai_ErrorCorrection")
  312. self.aiErrorCorrectionLabel.stringValue = NSLocalizedString("AI Error Correction", comment: "")
  313. self.aiErrorCorrectionLabel.textColor = NSColor.km_init(hex: "#252629")
  314. self.aiErrorCorrectionLabel.font = NSFont.SFProTextRegularFont(16.0)
  315. self.aiErrorCorrectionBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  316. if aiHomeState != .AIErrorCorrection {
  317. if mouseEntered {
  318. self.aiErrorCorrectionBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  319. } else {
  320. self.aiErrorCorrectionBox.fillColor = .clear
  321. }
  322. }
  323. }
  324. self.aiErrorCorrectionBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  325. if aiHomeState != .AIErrorCorrection {
  326. if downEntered {
  327. self.aiHomeState = .AIErrorCorrection
  328. self.trackEvent_ai(eventName: "AI Correction")
  329. self.refreshAIUI()
  330. }
  331. }
  332. }
  333. self.refreshAIUI()
  334. historyFileViewController.delete = self
  335. dragView.delete = self
  336. }
  337. // MARK: Action
  338. @IBAction func productPromotionButtonAction(_ sender: NSButton) {
  339. self.productPromotionFoldOrUnfold(sender)
  340. }
  341. // MARK: Public Methods
  342. func savePDFDocument(_ pdf: CPDFDocument, password: String) -> Void {
  343. DispatchQueue.global(qos: .`default`).async {
  344. var isSuccessfully = false
  345. if pdf.isEncrypted {
  346. let dic = [
  347. CPDFDocumentWriteOption.userPasswordOption : password,
  348. CPDFDocumentWriteOption.ownerPasswordOption : password
  349. ]
  350. isSuccessfully = pdf.write(to: pdf.documentURL, withOptions: dic)
  351. } else {
  352. isSuccessfully = pdf.write(to: pdf.documentURL)
  353. }
  354. if !isSuccessfully {
  355. isSuccessfully = NSData(data: pdf.dataRepresentation()).write(to: pdf.documentURL, atomically: true)
  356. }
  357. DispatchQueue.main.sync {
  358. if isSuccessfully {
  359. let workspace = NSWorkspace.shared
  360. let url = URL(fileURLWithPath: pdf.documentURL?.path ?? "")
  361. workspace.activateFileViewerSelecting([url])
  362. } else {
  363. let alert = NSAlert()
  364. alert.alertStyle = .critical
  365. alert.messageText = NSLocalizedString("Failed to insert page(s)!", comment: "")
  366. alert.runModal()
  367. }
  368. }
  369. }
  370. }
  371. // MARK: Private Methods
  372. func refreshUI() -> Void {
  373. homeButtonVC.updateUI()
  374. pdfToolsButtonVC.updateUI()
  375. cloudDocumentsButtonVC.updateUI()
  376. }
  377. func refreshAIUI() -> Void {
  378. self.aiHomeBox.fillColor = .clear
  379. self.aiHomeLabel.font = NSFont.SFProTextRegularFont(16.0)
  380. self.aiTranslationBox.fillColor = .clear
  381. self.aiTranslationLabel.font = NSFont.SFProTextRegularFont(16.0)
  382. self.aiRewritingBox.fillColor = .clear
  383. self.aiRewritingLabel.font = NSFont.SFProTextRegularFont(16.0)
  384. self.aiErrorCorrectionBox.fillColor = .clear
  385. self.aiErrorCorrectionLabel.font = NSFont.SFProTextRegularFont(16.0)
  386. rightFullBox.isHidden = true
  387. rightTopBox.isHidden = true
  388. rightBottomBox.isHidden = true
  389. switch self.aiHomeState {
  390. case .Home:
  391. self.aiHomeBox.fillColor = NSColor.km_init(hex: "#1770F4", alpha: 0.1)
  392. self.aiHomeLabel.font = NSFont.SFProTextSemiboldFont(16.0)
  393. self.rightTopBox.isHidden = false
  394. self.rightBottomBox.isHidden = false
  395. self.rightTopBox.contentView = self.aiOpenPDFFilesViewController.view
  396. self.rightBottomBox.contentView = self.historyFileViewController.view
  397. self.rightFullBox.contentView = nil
  398. self.rightTopBoxHeightConstraint.constant = 388.0
  399. self.dragView.isHidden = false
  400. break
  401. case .AITranslation:
  402. self.aiTranslationBox.fillColor = NSColor.km_init(hex: "#1770F4", alpha: 0.1)
  403. self.aiTranslationLabel.textColor = NSColor.km_init(hex: "#252629")
  404. self.aiTranslationLabel.font = NSFont.SFProTextSemiboldFont(16.0)
  405. self.rightFullBox.isHidden = false
  406. self.rightTopBox.contentView = nil
  407. self.rightBottomBox.contentView = nil
  408. self.rightFullBox.contentView = self.aiTranslationViewController.view
  409. self.dragView.isHidden = false
  410. break
  411. case .AIRewriting:
  412. self.aiRewritingBox.fillColor = NSColor.km_init(hex: "#1770F4", alpha: 0.1)
  413. self.aiRewritingLabel.textColor = NSColor.km_init(hex: "#252629")
  414. self.aiRewritingLabel.font = NSFont.SFProTextSemiboldFont(16.0)
  415. self.rightFullBox.isHidden = false
  416. self.rightTopBox.contentView = nil
  417. self.rightBottomBox.contentView = nil
  418. self.rightFullBox.contentView = self.aiRewritingViewController.view
  419. self.aiRewritingViewController.state = .AIRewriting
  420. self.aiRewritingViewController.initLocalization()
  421. self.aiRewritingViewController.acquisition_uiState()
  422. self.dragView.isHidden = true
  423. break
  424. case .AIErrorCorrection:
  425. self.aiErrorCorrectionBox.fillColor = NSColor.km_init(hex: "#1770F4", alpha: 0.1)
  426. self.aiErrorCorrectionLabel.textColor = NSColor.km_init(hex: "#252629")
  427. self.aiErrorCorrectionLabel.font = NSFont.SFProTextSemiboldFont(16.0)
  428. self.rightFullBox.isHidden = false
  429. self.rightTopBox.contentView = nil
  430. self.rightBottomBox.contentView = nil
  431. self.rightFullBox.contentView = self.aiRewritingViewController.view
  432. self.aiRewritingViewController.state = .AIErrorCorrection
  433. self.aiRewritingViewController.initLocalization()
  434. self.aiRewritingViewController.acquisition_uiState()
  435. self.dragView.isHidden = true
  436. break
  437. default:
  438. break
  439. }
  440. }
  441. func refreshProductActiveSpacing() -> Void {
  442. // let height = self.view.frame.height - 32.0 - openPDFBoxHeight.constant - 16.0 - createPDFBoxHeight.constant - 24.0 - pdfSeriesBoxHeight.constant - pdfProSeriesBoxHeightConstraint.constant - 8 - othersBoxHeightConstraint.constant - pdfOthersBoxHeightConstraint.constant - 242 - 30
  443. //// print("height == %f", height)
  444. // if height > 24.0 {
  445. // self.product_active_spacing.constant = height
  446. // } else {
  447. // self.product_active_spacing.constant = 24.0
  448. // }
  449. }
  450. func fetchDifferentFilePath(filePath: String) -> String {
  451. var resultFilePath = filePath
  452. var index: Int = 0
  453. while (FileManager.default.fileExists(atPath: resultFilePath)) {
  454. index += 1
  455. let path = NSString(string: filePath).deletingPathExtension + "(" + String(index) + ")"
  456. resultFilePath = NSString(string: path).appendingPathExtension(NSString(string: filePath).pathExtension)!
  457. }
  458. return resultFilePath;
  459. }
  460. func showProgressWindow() {
  461. let progress = SKProgressController()
  462. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  463. progress.window?.contentView?.wantsLayer = true
  464. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  465. progress.progressField.textColor = NSColor.white
  466. progress.message = NSLocalizedString("Translating...", comment: "")
  467. progress.closeBlock = { [unowned self] in
  468. }
  469. self.progressController = progress
  470. self.view.window?.beginSheet(progress.window!)
  471. }
  472. func hiddenProgressWindow() {
  473. DispatchQueue.main.async {
  474. self.progressController?.doubleValue = 100.0
  475. }
  476. self.stopTimer()
  477. if (self.progressController != nil) {
  478. self.view.window?.endSheet((self.progressController?.window)!)
  479. self.progressController = nil
  480. }
  481. }
  482. func isFileGreaterThan10MB(atPath filePath: String) -> Bool {
  483. let fileManager = FileManager.default
  484. do {
  485. let fileAttributes = try fileManager.attributesOfItem(atPath: filePath)
  486. if let fileSize = fileAttributes[.size] as? UInt64 {
  487. let megabyteSize = fileSize / (1024 * 1024)
  488. return megabyteSize >= 10
  489. }
  490. } catch {
  491. KMPrint("Error: \(error)")
  492. }
  493. return false
  494. }
  495. func isPDFPageCountExceedsLimit(filePath: String) -> Bool {
  496. let url = URL(fileURLWithPath: filePath)
  497. guard let document = PDFDocument(url: url) else {
  498. return false
  499. }
  500. let pageCount = document.pageCount
  501. return pageCount > 30
  502. }
  503. @objc func timerTick() {
  504. timerCounter += 1.0
  505. self.progressController?.increment(by: 1.0)
  506. if timerCounter >= 95 {
  507. stopTimer()
  508. }
  509. }
  510. func stopTimer() {
  511. timer?.invalidate()
  512. timer = nil
  513. }
  514. // MARK: Common methods
  515. func workSpaceOpenUrl(_ url: NSString) {
  516. let httpUrl: NSURL = NSURL.init(string: url as String)!
  517. let tWorkSpace: NSWorkspace = NSWorkspace.shared
  518. if !tWorkSpace.open(httpUrl as URL) {
  519. tWorkSpace.open(httpUrl as URL)
  520. }
  521. }
  522. }
  523. //MARK: Open Creat PDF
  524. extension KMHomeViewController: NSPopoverDelegate {
  525. }
  526. extension KMHomeViewController {
  527. func openPDFAction(_ sender: KMBox) {
  528. let openPanel = NSOpenPanel()
  529. openPanel.allowedFileTypes = ["pdf", "PDF"]
  530. openPanel.allowsMultipleSelection = false
  531. openPanel.beginSheetModal(for: self.view.window!) { (result) in
  532. if result == NSApplication.ModalResponse.OK {
  533. NSDocumentController.shared.openDocument(withContentsOf: openPanel.url!, display: true) { (document, documentWasAlreadyOpen, error) in
  534. if let error = error {
  535. NSApp.presentError(error)
  536. } else {
  537. self.view.window?.windowController?.close()
  538. }
  539. }
  540. }
  541. }
  542. }
  543. func creatPDFAction(_ sender: KMBox) {
  544. let popViewDataArr: [String] = [NSLocalizedString("New Blank Page", comment: ""),
  545. NSLocalizedString("New From Images", comment: ""),
  546. NSLocalizedString("New From Web Page", comment: ""),
  547. NSLocalizedString("Import From Camera", comment: ""),
  548. NSLocalizedString("Import From Scanner", comment: "")]
  549. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(popViewDataArr)
  550. let createFilePopover: NSPopover = NSPopover.init()
  551. createFilePopover.contentViewController = vc
  552. createFilePopover.delegate = self
  553. createFilePopover.animates = true
  554. createFilePopover.behavior = .semitransient
  555. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  556. createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 10, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .minY)
  557. vc.downCallback = { [unowned self] (downEntered: Bool, count: String) -> Void in
  558. if count == NSLocalizedString("New Blank Page", comment: "") {
  559. self.openBlankPage()
  560. } else if count == NSLocalizedString("Import From Camera", comment: "") {
  561. self.importFromCamera()
  562. } else if count == NSLocalizedString("Import From Scanner", comment: "") {
  563. self.importFromScanner()
  564. } else if count == NSLocalizedString("New From Web Page", comment: "") {
  565. self.importFromWebPage()
  566. } else if count == NSLocalizedString("Import From Scanner", comment: "") {
  567. self.newFromImages()
  568. }
  569. createFilePopover.close()
  570. }
  571. }
  572. func openBlankPage() {
  573. let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
  574. let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath() as String)
  575. let pdfDocument = CPDFDocument()
  576. pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
  577. pdfDocument?.write(to: URL(fileURLWithPath: savePath))
  578. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
  579. if error != nil {
  580. NSApp.presentError(error!)
  581. } else {
  582. if document is KMMainDocument {
  583. let newDocument = document
  584. (newDocument as! KMMainDocument).isNewCreated = true
  585. }
  586. }
  587. }
  588. }
  589. func importFromWebPage() {
  590. self.urlToPDFWindowController = KMURLToPDFWindowController.init(windowNibName: NSNib.Name("KMURLToPDFWindowController"))
  591. self.urlToPDFWindowController?.beginSheetModalForWindow(NSApp.mainWindow!, completionHandler: { filePath in
  592. if FileManager.default.fileExists(atPath: filePath) {
  593. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
  594. if error != nil {
  595. NSApp.presentError(error!)
  596. } else {
  597. if document is KMMainDocument {
  598. (document as! KMMainDocument).isNewCreated = true
  599. }
  600. }
  601. }
  602. }
  603. })
  604. }
  605. func importFromScanner() {
  606. let vc = KMDeviceBrowserWindowController.shared
  607. vc.type = .scanner
  608. vc.importScannerFileCallback = { [weak self](url: NSURL) -> Void in
  609. self?.openFile(withFilePath: url as URL)
  610. }
  611. vc.showWindow(NSApp.mainWindow)
  612. }
  613. func importFromCamera() {
  614. let vc = KMDeviceBrowserWindowController.shared
  615. vc.type = .camera
  616. vc.importCameraFileCallback = { [weak self] (url: NSURL) -> Void in
  617. self?.openFile(withFilePath: url as URL)
  618. }
  619. vc.showWindow(NSApp.mainWindow)
  620. }
  621. func newFromImages() {
  622. let openPanel = NSOpenPanel()
  623. openPanel.allowedFileTypes = KMImageAccessoryController.supportedImageTypes()
  624. // if IAPProductsManager.defaultManager().isAvailableAllFunction {
  625. // openPanel.allowsMultipleSelection = true
  626. // } else {
  627. // openPanel.allowsMultipleSelection = false
  628. // }
  629. openPanel.message = NSLocalizedString("Select images to create a new document. To select multiple files press cmd ⌘ button on the keyboard and click on the target files one by one.", comment: "")
  630. openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  631. if result == .OK {
  632. DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
  633. debugPrint("缺少图片转PDF")
  634. // let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
  635. // var arr: [KMBatchOperateFile] = []
  636. // for url in openPanel.urls {
  637. // let file = KMBatchOperateFile(filePath: url.path, type: .createPDF)
  638. // arr.append(file)
  639. // }
  640. //
  641. // if #available(macOS 10.13, *) {
  642. // baseWindowController.window?.makeKeyAndOrderFront(nil)
  643. // } else {
  644. // baseWindowController.showWindow(nil)
  645. // }
  646. // baseWindowController.checkNeedPasswordSwitch(toOperateType: .createPDF, files: arr)
  647. // if #available(macOS 10.13, *) {
  648. // baseWindowController.release()
  649. // }
  650. }
  651. }
  652. }
  653. }
  654. }