KMHomeViewController.swift 33 KB

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