KMHomeViewController.swift 32 KB

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