KMHomeViewController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. override func viewWillAppear() {
  105. super.viewWillAppear()
  106. if !KMLightMemberManager.manager.isLogin() {
  107. KMLightMemberManager.manager.canShowAdvancedView = true
  108. }
  109. }
  110. override func viewDidLoad() {
  111. super.viewDidLoad()
  112. // Do view setup here.
  113. openPDFButtonVC = KMDesignButton.init(withType: .Text)
  114. createPDFButtonVC = KMDesignButton.init(withType: .Text)
  115. createPDFImage = KMDesignButton.init(withType: .Image)
  116. createPDFAddButtonVC = KMDesignButton.init(withType: .Image)
  117. homeButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  118. pdfToolsButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  119. cloudDocumentsButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  120. pdfSeriesButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  121. pdfOthersButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  122. self.homeSplitView.setPosition(270.0, ofDividerAt: 0)
  123. self.leftBox.fillColor = NSColor(hex: "#F7F8FA")
  124. self.productPromotionPDFProSeries = ["Windows", "iPhone / iPad", "Android"]
  125. self.productPromotionOthers = ["ComPDFKit", "SignFlow", "FiImage Editor", "FiImage Screen", "Free PDF Templates"]
  126. self.productPromotionData = ["Windows": ["Name" : "Windows", "Image" : "icon_home_logo_windows", "link":""],
  127. "iPhone / iPad" : ["Name" : "iPhone / iPad", "Image" : "icon_home_logo_mac", "link":""],
  128. "Android" : ["Name" : "Android", "Image" : "icon_home_logo_android", "link":""],
  129. "ComPDFKit" : ["Name" : "ComPDFKit", "Image" : "icon_home_logo_comPDFKit", "link":""],
  130. "SignFlow" : ["Name" : "SignFlow", "Image" : "icon_home_logo_signflow", "link":""],
  131. "FiImage Editor" : ["Name" : "FiImage Editor", "Image" : "icon_home_logo_editor", "link":""],
  132. "FiImage Screen" : ["Name" : "FiImage Screen", "Image" : "icon_home_logo_screen", "link":""],
  133. "Free PDF Templates" : ["Name" : "Free PDF Templates", "Image" : "icon_home_logo_templates"], "link":""]
  134. NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { (aEvent) -> NSEvent? in
  135. self.otherMouseDown(with: aEvent)
  136. return aEvent
  137. }
  138. // self.initNetworkingData()
  139. // self.initLocalization()
  140. // self.initializeUI()
  141. self.initializeUI_DMG()
  142. // refreshUI()
  143. NotificationCenter.default.addObserver(self, selector: #selector(homeFileRectChange(_:)), name: Notification.Name("KMHomeFileRectChange"), object: nil)
  144. }
  145. func initNetworkingData() {
  146. KMAdvertisementManager.manager.fetchDataWithResponseObject { [unowned self] data, responseObject, error in
  147. print("获取广告数据成功")
  148. // print(error)
  149. // print(responseObject)
  150. if data != nil {
  151. for model in data! {
  152. guard let sectionContent = model.content else { break }
  153. if model.showType == .list {
  154. for section in sectionContent {
  155. print("list section 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: section.sectionTitle?.language))
  156. guard let itemContent = section.content else { break }
  157. for item in itemContent {
  158. print("item 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.title?.language))
  159. print("item 图片链接" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.imageURL?.normal?.universal))
  160. print("item 点击链接" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.linkURL))
  161. print("item tips" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tips))
  162. print("item actionType" + String(Int(item.actionType.rawValue)))
  163. }
  164. }
  165. }
  166. if model.showType == .view {
  167. for section in sectionContent {
  168. print("view section 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: section.sectionTitle?.language))
  169. guard let itemContent = section.content else { break }
  170. for item in itemContent {
  171. print("item 标题 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.title?.language))
  172. print("item 图片链接 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.imageURL?.normal?.universal))
  173. print("item 点击链接 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.linkURL))
  174. print("item tips = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tips))
  175. print("item actionType = " + String(Int(item.actionType.rawValue)))
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. // MARK: Init
  184. func initializeUI() {
  185. openPDFBox.fillColor = .clear
  186. openPDFBox.contentView = openPDFButtonVC.view
  187. openPDFButtonVC.target = self
  188. openPDFButtonVC.action = #selector(self.homeToolAction(_:))
  189. openPDFButtonVC.button(type: .Brand, size: .l, height: openPDFBoxHeight)
  190. createPDFBox.fillColor = .clear
  191. createPDFBox.contentView = createPDFButtonVC.view
  192. createPDFButtonVC.target = self
  193. createPDFButtonVC.action = #selector(self.homeToolAction(_:))
  194. createPDFButtonVC.button(type: .Sec, size: .l, height: createPDFBoxHeight)
  195. createPDFButtonVC.textButtonBox.move = { [unowned self](mouseEntered: Bool) -> Void in
  196. if mouseEntered {
  197. if self.createPDFButtonVC.state != .Sel && self.createPDFButtonVC.canHover {
  198. self.createPDFButtonVC.state = .Hov
  199. self.createPDFButtonVC.updateUI()
  200. self.createPDFImage.state = .Hov
  201. self.createPDFImage.updateUI()
  202. self.createPDFImage.canHover = false
  203. }
  204. } else {
  205. if self.createPDFButtonVC.state != .Sel && self.createPDFButtonVC.canHover {
  206. self.createPDFButtonVC.state = .Norm
  207. self.createPDFButtonVC.updateUI()
  208. self.createPDFImage.state = .Norm
  209. self.createPDFImage.updateUI()
  210. self.createPDFImage.canHover = true
  211. }
  212. }
  213. }
  214. createPDFView.contentView = createPDFImage.view
  215. createPDFImage.image = NSImage(named: "icon_btn_arrow_gray_down_s_norm_false")!
  216. createPDFImage.target = self
  217. createPDFImage.action = #selector(self.homeToolAction(_:))
  218. createPDFImage.button(type: .Icon_Fill, size: .m, height: createPDFViewHeight)
  219. homeBox.fillColor = .clear
  220. homeBox.contentView = homeButtonVC.view
  221. homeButtonVC.target = self
  222. homeButtonVC.action = #selector(self.homeToolAction(_:))
  223. homeButtonVC.image = NSImage(named: "icon_home_sidebar_home_false")!
  224. homeButtonVC.image_hover = NSImage(named: "icon_home_sidebar_home_false")!
  225. homeButtonVC.image_click = NSImage(named: "icon_home_sidebar_home_true")!
  226. homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: homeBoxHeight)
  227. homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  228. homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  229. pdfToolsBox.fillColor = .clear
  230. pdfToolsBox.contentView = pdfToolsButtonVC.view
  231. pdfToolsButtonVC.target = self
  232. pdfToolsButtonVC.action = #selector(self.homeToolAction(_:))
  233. pdfToolsButtonVC.image = NSImage(named: "icon_home_sidebar_tool_false")!
  234. pdfToolsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_tool_false")!
  235. pdfToolsButtonVC.image_click = NSImage(named: "icon_home_sidebar_tool_true")!
  236. pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: pdfToolsBoxHeight)
  237. pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  238. pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  239. cloudDocumentsBox.fillColor = .clear
  240. cloudDocumentsBox.contentView = cloudDocumentsButtonVC.view
  241. cloudDocumentsButtonVC.target = self
  242. cloudDocumentsButtonVC.action = #selector(self.homeToolAction(_:))
  243. cloudDocumentsButtonVC.image = NSImage(named: "icon_home_sidebar_cloud_false")!
  244. cloudDocumentsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_cloud_false")!
  245. cloudDocumentsButtonVC.image_click = NSImage(named: "icon_home_sidebar_cloud_true")!
  246. cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: cloudDocumentsBoxHeight)
  247. cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  248. cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  249. pdfSeriesBox.fillColor = .clear
  250. pdfSeriesBox.contentView = pdfSeriesButtonVC.view
  251. pdfSeriesButtonVC.target = self
  252. pdfSeriesButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:))
  253. pdfSeriesButtonVC.type = .Right
  254. pdfSeriesButtonVC.canHover = false
  255. pdfSeriesButtonVC.imageWidth = 12.0
  256. pdfSeriesButtonVC.imageHeight = 12.0
  257. pdfSeriesButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  258. pdfSeriesButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: pdfSeriesBoxHeight)
  259. pdfOthersBox.fillColor = .clear
  260. pdfOthersBox.contentView = pdfOthersButtonVC.view
  261. pdfOthersButtonVC.target = self
  262. pdfOthersButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:))
  263. pdfOthersButtonVC.type = .Right
  264. pdfOthersButtonVC.canHover = false
  265. pdfOthersButtonVC.imageWidth = 12.0
  266. pdfOthersButtonVC.imageHeight = 12.0
  267. pdfOthersButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  268. pdfOthersButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: othersBoxHeightConstraint)
  269. // if self.activitiesBox.isHidden {
  270. // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true)
  271. // } else {
  272. // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: [], isInitialize: true)
  273. // }
  274. self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true)
  275. self.activitiesBox.contentView = KMProductPromotionViewController.init().view
  276. activitiesBox.cornerRadius = 8.0
  277. rightTopBox.fillColor = .white
  278. rightBottomBox.fillColor = .white
  279. rightFullBox.fillColor = .white
  280. productPromotionBox.fillColor = .clear
  281. product_active_scrollView.verticalScrollElasticity = .none
  282. self.refreshRightBoxUI(.Home)
  283. fastToolViewController.delete = self
  284. historyFileViewController.delete = self
  285. pdfToolsViewController.delete = self
  286. dragView.delete = self
  287. }
  288. func initLocalization() {
  289. self.favoriteDocumentsLabel.stringValue = NSLocalizedString("Favorite Documents", comment: "")
  290. homeButtonVC.stringValue = NSLocalizedString("Home", comment: "")
  291. pdfToolsButtonVC.stringValue = NSLocalizedString("PDF Tools", comment: "")
  292. cloudDocumentsButtonVC.stringValue = NSLocalizedString("Cloud Documents", comment: "")
  293. openPDFButtonVC.stringValue = NSLocalizedString("Open File", comment: "")
  294. openPDFBox.toolTip = NSLocalizedString("Open PDF", comment: "")
  295. createPDFButtonVC.stringValue = NSLocalizedString("Create PDF", comment: "")
  296. createPDFBox.toolTip = NSLocalizedString("Create PDF", comment: "")
  297. pdfSeriesButtonVC.stringValue = NSLocalizedString("PDF Pro Series", comment: "")
  298. pdfOthersButtonVC.stringValue = NSLocalizedString("Others", comment: "")
  299. }
  300. // MARK: Action
  301. func initializeUI_DMG() -> Void {
  302. rightTopBox.fillColor = .white
  303. rightBottomBox.fillColor = .white
  304. rightFullBox.fillColor = .white
  305. self.aiHomeBox.fillColor = .clear
  306. self.aiHomeBox.cornerRadius = 8.0
  307. self.aiHomeImageView.image = NSImage(named: "icon_home")
  308. self.aiHomeLabel.stringValue = NSLocalizedString("Home", comment: "")
  309. self.aiHomeLabel.textColor = NSColor(hex: "#252629")
  310. self.aiHomeLabel.font = NSFont.SFProTextRegular(16.0)
  311. self.aiHomeBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  312. if aiHomeState != .Home {
  313. if mouseEntered {
  314. self.aiHomeBox.fillColor = NSColor(hex: "#EDEEF0")
  315. } else {
  316. self.aiHomeBox.fillColor = .clear
  317. }
  318. }
  319. }
  320. self.aiHomeBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  321. if aiHomeState != .Home {
  322. if downEntered {
  323. self.aiHomeState = .Home
  324. self.refreshAIUI()
  325. }
  326. }
  327. }
  328. self.aiTranslationBox.fillColor = .clear
  329. self.aiTranslationBox.cornerRadius = 8.0
  330. self.aiTranslationImageView.image = NSImage(named: "ic_function_other")
  331. self.aiTranslationLabel.stringValue = NSLocalizedString("AI Translation", comment: "")
  332. self.aiTranslationLabel.textColor = NSColor(hex: "#252629")
  333. self.aiTranslationLabel.font = NSFont.SFProTextRegular(16.0)
  334. self.aiTranslationBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  335. if aiHomeState != .AITranslation {
  336. if mouseEntered {
  337. self.aiTranslationBox.fillColor = NSColor(hex: "#EDEEF0")
  338. } else {
  339. self.aiTranslationBox.fillColor = .clear
  340. }
  341. }
  342. }
  343. self.aiTranslationBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  344. if aiHomeState != .AITranslation {
  345. if downEntered {
  346. self.aiHomeState = .AITranslation
  347. self.refreshAIUI()
  348. }
  349. }
  350. }
  351. self.aiRewritingBox.fillColor = .clear
  352. self.aiRewritingBox.cornerRadius = 8.0
  353. self.aiRewritingImageView.image = NSImage(named: "ic_ai_Rewriting")
  354. self.aiRewritingLabel.stringValue = NSLocalizedString("AI Rewriting", comment: "")
  355. self.aiRewritingLabel.textColor = NSColor(hex: "#252629")
  356. self.aiRewritingLabel.font = NSFont.SFProTextRegular(16.0)
  357. self.aiRewritingBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  358. if aiHomeState != .AIRewriting {
  359. if mouseEntered {
  360. self.aiRewritingBox.fillColor = NSColor(hex: "#EDEEF0")
  361. } else {
  362. self.aiRewritingBox.fillColor = .clear
  363. }
  364. }
  365. }
  366. self.aiRewritingBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  367. if aiHomeState != .AIRewriting {
  368. if downEntered {
  369. self.aiHomeState = .AIRewriting
  370. self.refreshAIUI()
  371. }
  372. }
  373. }
  374. self.aiErrorCorrectionBox.fillColor = .clear
  375. self.aiErrorCorrectionBox.cornerRadius = 8.0
  376. self.aiErrorCorrectionImageView.image = NSImage(named: "ic_ai_ErrorCorrection")
  377. self.aiErrorCorrectionLabel.stringValue = NSLocalizedString("AI Error Correction", comment: "")
  378. self.aiErrorCorrectionLabel.textColor = NSColor(hex: "#252629")
  379. self.aiErrorCorrectionLabel.font = NSFont.SFProTextRegular(16.0)
  380. self.aiErrorCorrectionBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in
  381. if aiHomeState != .AIErrorCorrection {
  382. if mouseEntered {
  383. self.aiErrorCorrectionBox.fillColor = NSColor(hex: "#EDEEF0")
  384. } else {
  385. self.aiErrorCorrectionBox.fillColor = .clear
  386. }
  387. }
  388. }
  389. self.aiErrorCorrectionBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
  390. if aiHomeState != .AIErrorCorrection {
  391. if downEntered {
  392. self.aiHomeState = .AIErrorCorrection
  393. self.refreshAIUI()
  394. }
  395. }
  396. }
  397. self.refreshAIUI()
  398. historyFileViewController.delete = self
  399. dragView.delete = self
  400. }
  401. // MARK: Action
  402. @IBAction func productPromotionButtonAction(_ sender: NSButton) {
  403. self.productPromotionFoldOrUnfold(sender)
  404. }
  405. // MARK: Public Methods
  406. func savePDFDocument(_ pdf: CPDFDocument, password: String) -> Void {
  407. DispatchQueue.global(qos: .`default`).async {
  408. var isSuccessfully = false
  409. if pdf.isEncrypted {
  410. let dic = [
  411. CPDFDocumentWriteOption.userPasswordOption : password,
  412. CPDFDocumentWriteOption.ownerPasswordOption : password
  413. ]
  414. isSuccessfully = pdf.write(to: pdf.documentURL, withOptions: dic)
  415. } else {
  416. isSuccessfully = pdf.write(to: pdf.documentURL)
  417. }
  418. if !isSuccessfully {
  419. isSuccessfully = NSData(data: pdf.dataRepresentation()).write(to: pdf.documentURL, atomically: true)
  420. }
  421. DispatchQueue.main.sync {
  422. if isSuccessfully {
  423. let workspace = NSWorkspace.shared
  424. let url = URL(fileURLWithPath: pdf.documentURL?.path ?? "")
  425. workspace.activateFileViewerSelecting([url])
  426. } else {
  427. let alert = NSAlert()
  428. alert.alertStyle = .critical
  429. alert.messageText = NSLocalizedString("Failed to insert page(s)!", comment: "")
  430. alert.runModal()
  431. }
  432. }
  433. }
  434. }
  435. // MARK: Private Methods
  436. func refreshUI() -> Void {
  437. homeButtonVC.updateUI()
  438. pdfToolsButtonVC.updateUI()
  439. cloudDocumentsButtonVC.updateUI()
  440. }
  441. func refreshAIUI() -> Void {
  442. self.aiHomeBox.fillColor = .clear
  443. self.aiHomeLabel.font = NSFont.SFProTextRegular(16.0)
  444. self.aiTranslationBox.fillColor = .clear
  445. self.aiTranslationLabel.font = NSFont.SFProTextRegular(16.0)
  446. self.aiRewritingBox.fillColor = .clear
  447. self.aiRewritingLabel.font = NSFont.SFProTextRegular(16.0)
  448. self.aiErrorCorrectionBox.fillColor = .clear
  449. self.aiErrorCorrectionLabel.font = NSFont.SFProTextRegular(16.0)
  450. rightFullBox.isHidden = true
  451. rightTopBox.isHidden = true
  452. rightBottomBox.isHidden = true
  453. switch self.aiHomeState {
  454. case .Home:
  455. self.aiHomeBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1)
  456. self.aiHomeLabel.font = NSFont.SFProTextSemibold(16.0)
  457. self.rightTopBox.isHidden = false
  458. self.rightBottomBox.isHidden = false
  459. self.rightTopBox.contentView = self.aiOpenPDFFilesViewController.view
  460. self.rightBottomBox.contentView = self.historyFileViewController.view
  461. self.rightFullBox.contentView = nil
  462. self.rightTopBoxHeightConstraint.constant = 388.0
  463. break
  464. case .AITranslation:
  465. self.aiTranslationBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1)
  466. self.aiTranslationLabel.textColor = NSColor(hex: "#252629")
  467. self.aiTranslationLabel.font = NSFont.SFProTextSemibold(16.0)
  468. self.rightFullBox.isHidden = false
  469. self.rightTopBox.contentView = nil
  470. self.rightBottomBox.contentView = nil
  471. self.rightFullBox.contentView = self.aiTranslationViewController.view
  472. break
  473. case .AIRewriting:
  474. self.aiRewritingBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1)
  475. self.aiRewritingLabel.textColor = NSColor(hex: "#252629")
  476. self.aiRewritingLabel.font = NSFont.SFProTextSemibold(16.0)
  477. self.rightFullBox.isHidden = false
  478. self.rightTopBox.contentView = nil
  479. self.rightBottomBox.contentView = nil
  480. self.rightFullBox.contentView = self.aiRewritingViewController.view
  481. self.aiRewritingViewController.state = .AIRewriting
  482. self.aiRewritingViewController.initLocalization()
  483. break
  484. case .AIErrorCorrection:
  485. self.aiErrorCorrectionBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1)
  486. self.aiErrorCorrectionLabel.textColor = NSColor(hex: "#252629")
  487. self.aiErrorCorrectionLabel.font = NSFont.SFProTextSemibold(16.0)
  488. self.rightFullBox.isHidden = false
  489. self.rightTopBox.contentView = nil
  490. self.rightBottomBox.contentView = nil
  491. self.rightFullBox.contentView = self.aiRewritingViewController.view
  492. self.aiRewritingViewController.state = .AIErrorCorrection
  493. self.aiRewritingViewController.initLocalization()
  494. break
  495. default:
  496. break
  497. }
  498. }
  499. func refreshProductActiveSpacing() -> Void {
  500. 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
  501. // print("height == %f", height)
  502. if height > 24.0 {
  503. self.product_active_spacing.constant = height
  504. } else {
  505. self.product_active_spacing.constant = 24.0
  506. }
  507. }
  508. func fetchDifferentFilePath(filePath: String) -> String {
  509. var resultFilePath = filePath
  510. var index: Int = 0
  511. while (FileManager.default.fileExists(atPath: resultFilePath)) {
  512. index += 1
  513. let path = NSString(string: filePath).deletingPathExtension + "(" + String(index) + ")"
  514. resultFilePath = NSString(string: path).appendingPathExtension(NSString(string: filePath).pathExtension)!
  515. }
  516. return resultFilePath;
  517. }
  518. // MARK: Common methods
  519. func workSpaceOpenUrl(_ url: NSString) {
  520. let httpUrl: NSURL = NSURL.init(string: url as String)!
  521. let tWorkSpace: NSWorkspace = NSWorkspace.shared
  522. if !tWorkSpace.open(httpUrl as URL) {
  523. tWorkSpace.open(httpUrl as URL)
  524. }
  525. }
  526. func createPopoverAction(_ data: [String]) {
  527. if (self.popover != nil && self.popover!.isShown) {
  528. self.popover?.close()
  529. self.popover = nil
  530. return
  531. }
  532. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(data)
  533. let createFilePopover: NSPopover = NSPopover.init()
  534. self.popover = createFilePopover
  535. createFilePopover.contentViewController = vc
  536. createFilePopover.animates = true
  537. createFilePopover.behavior = .semitransient
  538. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  539. 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)
  540. vc.customBoxWidthLayoutConstraint.constant = createPDFBox.frame.width
  541. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  542. if downEntered {
  543. self?.popover?.close()
  544. self?.popover = nil
  545. self?.popoverItemAction(count)
  546. }
  547. }
  548. }
  549. }