KMHomeViewController.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. @objcMembers class KMHomeViewController: NSViewController {
  18. @IBOutlet weak var leftBox: NSBox!
  19. @IBOutlet weak var rightBox: NSBox!
  20. @IBOutlet weak var homeSplitView: NSSplitView!
  21. @IBOutlet weak var homeRightScrollViewView: NSScrollView!
  22. @IBOutlet weak var openPDFBox: NSBox! // Open PDF
  23. @IBOutlet weak var openPDFBoxHeight: NSLayoutConstraint!
  24. @IBOutlet weak var createPDFBox: NSBox! // Create PDF
  25. @IBOutlet weak var createPDFView: NSBox! // Create PDF
  26. @IBOutlet weak var createPDFBoxHeight: NSLayoutConstraint!
  27. @IBOutlet weak var createPDFViewHeight: NSLayoutConstraint!
  28. @IBOutlet weak var homeBox: NSBox! // Home
  29. @IBOutlet weak var homeBoxHeight: NSLayoutConstraint!
  30. @IBOutlet weak var pdfToolsBox: NSBox! // PDF Tools
  31. @IBOutlet weak var pdfToolsBoxHeight: NSLayoutConstraint!
  32. @IBOutlet weak var favoriteDocumentsBox: KMBox! // Favorite Documents
  33. @IBOutlet weak var favoriteDocumentsLabel: NSTextField!
  34. @IBOutlet weak var favoriteDocumentsImageView: NSImageView!
  35. @IBOutlet weak var cloudDocumentsBox: NSBox! // Cloud Documents
  36. @IBOutlet weak var cloudDocumentsBoxHeight: NSLayoutConstraint!
  37. @IBOutlet weak var productPromotionBox: KMBox! // 产品推广
  38. @IBOutlet weak var pdfSeriesBox: NSBox!
  39. @IBOutlet weak var pdfSeriesBoxHeight: NSLayoutConstraint!
  40. @IBOutlet weak var pdfProSeriesBox: NSBox!
  41. @IBOutlet weak var pdfProSeriesBoxHeightConstraint: NSLayoutConstraint!
  42. @IBOutlet weak var pdfOthersBox: NSBox!
  43. @IBOutlet weak var pdfOthersBoxHeightConstraint: NSLayoutConstraint!
  44. @IBOutlet weak var othersBox: NSBox!
  45. @IBOutlet weak var othersBoxHeightConstraint: NSLayoutConstraint!
  46. @IBOutlet weak var activitiesBox: KMBox! // 活动
  47. @IBOutlet weak var product_active_box: NSBox!
  48. @IBOutlet weak var product_active_scrollView: NSScrollView!
  49. @IBOutlet weak var product_active_spacing: NSLayoutConstraint!
  50. @IBOutlet weak var rightTopBox: NSBox!
  51. @IBOutlet weak var rightBottomBox: NSBox!
  52. @IBOutlet weak var rightFullBox: NSBox!
  53. @IBOutlet weak var rightTopBoxHeightConstraint: NSLayoutConstraint!
  54. @IBOutlet var fastToolViewController: KMHomeFastToolViewController!
  55. @IBOutlet var historyFileViewController: KMHomeHistoryFileViewController!
  56. @IBOutlet var pdfToolsViewController: KMPDFToolsViewController!
  57. @IBOutlet var cloudDocumentsViewController: KMCloudDocumentsViewController!
  58. @IBOutlet weak var dragView: KMHomeDragView!
  59. let ScrollerViewWidget: CGFloat = 15.0
  60. var homeWindowController : MainWindowController!
  61. // 产品推广数据
  62. var productPromotionPDFProSeries: NSArray!
  63. var productPromotionOthers: NSArray!
  64. var productPromotionData: NSDictionary!
  65. var isShowQuickTour: Bool = false
  66. var myDocument: NSDocument?
  67. var currentWindowController: NSWindowController?
  68. var homeState: KMHomeToolState = .Home
  69. var urlToPDFWindowController: KMURLToPDFWindowController?
  70. var openPDFButtonVC: KMDesignButton!
  71. var createPDFButtonVC: KMDesignButton!
  72. var createPDFImage: KMDesignButton!
  73. var createPDFAddButtonVC: KMDesignButton!
  74. var homeButtonVC: KMTextImageButtonVC!
  75. var pdfToolsButtonVC: KMTextImageButtonVC!
  76. var cloudDocumentsButtonVC: KMTextImageButtonVC!
  77. var pdfSeriesButtonVC: KMTextImageButtonVC!
  78. var pdfOthersButtonVC: KMTextImageButtonVC!
  79. var popover: NSPopover?
  80. var deviceBrowserWC: KMDeviceBrowserWindowController?
  81. override func viewWillAppear() {
  82. super.viewWillAppear()
  83. if !KMLightMemberManager.manager.isLogin() {
  84. KMLightMemberManager.manager.canShowAdvancedView = true
  85. }
  86. }
  87. override func viewDidLoad() {
  88. super.viewDidLoad()
  89. // Do view setup here.
  90. openPDFButtonVC = KMDesignButton.init(withType: .Text)
  91. createPDFButtonVC = KMDesignButton.init(withType: .Text)
  92. createPDFImage = KMDesignButton.init(withType: .Image)
  93. createPDFAddButtonVC = KMDesignButton.init(withType: .Image)
  94. homeButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  95. pdfToolsButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  96. cloudDocumentsButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  97. pdfSeriesButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  98. pdfOthersButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil)
  99. self.homeSplitView.setPosition(270.0, ofDividerAt: 0)
  100. self.leftBox.fillColor = NSColor(hex: "#F7F8FA")
  101. self.productPromotionPDFProSeries = ["Windows", "iPhone / iPad", "Android"]
  102. self.productPromotionOthers = ["ComPDFKit", "ComVideoKit", "SignFlow", "FiImage Editor", "FiImage Screen", "Free PDF Templates"]
  103. self.productPromotionData = ["Windows": ["Name" : "Windows", "Image" : "icon_home_logo_windows", "link":""],
  104. "iPhone / iPad" : ["Name" : "iPhone / iPad", "Image" : "icon_home_logo_mac", "link":""],
  105. "Android" : ["Name" : "Android", "Image" : "icon_home_logo_android", "link":""],
  106. "ComPDFKit" : ["Name" : "ComPDFKit", "Image" : "icon_home_logo_comPDFKit", "link":""],
  107. "ComVideoKit" : ["Name" : "ComVideoKit", "Image" : "icon_home_logo_comVideoKit", "link":""],
  108. "SignFlow" : ["Name" : "SignFlow", "Image" : "icon_home_logo_signflow", "link":""],
  109. "FiImage Editor" : ["Name" : "FiImage Editor", "Image" : "icon_home_logo_editor", "link":""],
  110. "FiImage Screen" : ["Name" : "FiImage Screen", "Image" : "icon_home_logo_screen", "link":""],
  111. "Free PDF Templates" : ["Name" : "Free PDF Templates", "Image" : "icon_home_logo_templates"], "link":""]
  112. NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { (aEvent) -> NSEvent? in
  113. self.otherMouseDown(with: aEvent)
  114. return aEvent
  115. }
  116. self.initNetworkingData()
  117. self.initLocalization()
  118. self.initializeUI()
  119. refreshUI()
  120. NotificationCenter.default.addObserver(self, selector: #selector(homeFileRectChange(_:)), name: Notification.Name("KMHomeFileRectChange"), object: nil)
  121. }
  122. func initNetworkingData() {
  123. KMAdvertisementManager.manager.fetchDataWithResponseObject { [unowned self] data, responseObject, error in
  124. print("获取广告数据成功")
  125. // print(error)
  126. // print(responseObject)
  127. if data != nil {
  128. for model in data! {
  129. guard let sectionContent = model.content else { break }
  130. if model.showType == .list {
  131. for section in sectionContent {
  132. print("list section 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: section.sectionTitle?.language))
  133. guard let itemContent = section.content else { break }
  134. for item in itemContent {
  135. print("item 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.title?.language))
  136. print("item 图片链接" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.imageURL?.normal?.universal))
  137. print("item 点击链接" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.linkURL))
  138. print("item tips" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tips))
  139. print("item actionType" + String(Int(item.actionType.rawValue)))
  140. }
  141. }
  142. }
  143. if model.showType == .view {
  144. for section in sectionContent {
  145. print("view section 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: section.sectionTitle?.language))
  146. guard let itemContent = section.content else { break }
  147. for item in itemContent {
  148. print("item 标题 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.title?.language))
  149. print("item 图片链接 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.imageURL?.normal?.universal))
  150. print("item 点击链接 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.linkURL))
  151. print("item tips = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tips))
  152. print("item actionType = " + String(Int(item.actionType.rawValue)))
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. // MARK: Init
  161. func initializeUI() {
  162. openPDFBox.fillColor = .clear
  163. openPDFBox.contentView = openPDFButtonVC.view
  164. openPDFButtonVC.target = self
  165. openPDFButtonVC.action = #selector(self.homeToolAction(_:))
  166. openPDFButtonVC.button(type: .Brand, size: .l, height: openPDFBoxHeight)
  167. createPDFBox.fillColor = .clear
  168. createPDFBox.contentView = createPDFButtonVC.view
  169. createPDFButtonVC.target = self
  170. createPDFButtonVC.action = #selector(self.homeToolAction(_:))
  171. createPDFButtonVC.button(type: .Sec, size: .l, height: createPDFBoxHeight)
  172. createPDFButtonVC.textButtonBox.move = { [unowned self](mouseEntered: Bool) -> Void in
  173. if self != nil {
  174. if mouseEntered {
  175. if self.createPDFButtonVC.state != .Sel && self.createPDFButtonVC.canHover {
  176. self.createPDFButtonVC.state = .Hov
  177. self.createPDFButtonVC.updateUI()
  178. self.createPDFImage.state = .Hov
  179. self.createPDFImage.updateUI()
  180. self.createPDFImage.canHover = false
  181. }
  182. } else {
  183. if self.createPDFButtonVC.state != .Sel && self.createPDFButtonVC.canHover {
  184. self.createPDFButtonVC.state = .Norm
  185. self.createPDFButtonVC.updateUI()
  186. self.createPDFImage.state = .Norm
  187. self.createPDFImage.updateUI()
  188. self.createPDFImage.canHover = true
  189. }
  190. }
  191. }
  192. }
  193. createPDFView.contentView = createPDFImage.view
  194. createPDFImage.image = NSImage(named: "icon_btn_arrow_gray_down_s_norm_false")!
  195. createPDFImage.target = self
  196. createPDFImage.action = #selector(self.homeToolAction(_:))
  197. createPDFImage.button(type: .Icon_Fill, size: .m, height: createPDFViewHeight)
  198. homeBox.fillColor = .clear
  199. homeBox.contentView = homeButtonVC.view
  200. homeButtonVC.target = self
  201. homeButtonVC.action = #selector(self.homeToolAction(_:))
  202. homeButtonVC.image = NSImage(named: "icon_home_sidebar_home_false")!
  203. homeButtonVC.image_hover = NSImage(named: "icon_home_sidebar_home_false")!
  204. homeButtonVC.image_click = NSImage(named: "icon_home_sidebar_home_true")!
  205. homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: homeBoxHeight)
  206. homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  207. homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  208. pdfToolsBox.fillColor = .clear
  209. pdfToolsBox.contentView = pdfToolsButtonVC.view
  210. pdfToolsButtonVC.target = self
  211. pdfToolsButtonVC.action = #selector(self.homeToolAction(_:))
  212. pdfToolsButtonVC.image = NSImage(named: "icon_home_sidebar_tool_false")!
  213. pdfToolsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_tool_false")!
  214. pdfToolsButtonVC.image_click = NSImage(named: "icon_home_sidebar_tool_true")!
  215. pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: pdfToolsBoxHeight)
  216. pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  217. pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  218. cloudDocumentsBox.fillColor = .clear
  219. cloudDocumentsBox.contentView = cloudDocumentsButtonVC.view
  220. cloudDocumentsButtonVC.target = self
  221. cloudDocumentsButtonVC.action = #selector(self.homeToolAction(_:))
  222. cloudDocumentsButtonVC.image = NSImage(named: "icon_home_sidebar_cloud_false")!
  223. cloudDocumentsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_cloud_false")!
  224. cloudDocumentsButtonVC.image_click = NSImage(named: "icon_home_sidebar_cloud_true")!
  225. cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: cloudDocumentsBoxHeight)
  226. cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  227. cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  228. pdfSeriesBox.fillColor = .clear
  229. pdfSeriesBox.contentView = pdfSeriesButtonVC.view
  230. pdfSeriesButtonVC.target = self
  231. pdfSeriesButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:))
  232. pdfSeriesButtonVC.type = .Right
  233. pdfSeriesButtonVC.canHover = false
  234. pdfSeriesButtonVC.imageWidth = 12.0
  235. pdfSeriesButtonVC.imageHeight = 12.0
  236. pdfSeriesButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  237. pdfSeriesButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: pdfSeriesBoxHeight)
  238. pdfOthersBox.fillColor = .clear
  239. pdfOthersBox.contentView = pdfOthersButtonVC.view
  240. pdfOthersButtonVC.target = self
  241. pdfOthersButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:))
  242. pdfOthersButtonVC.type = .Right
  243. pdfOthersButtonVC.canHover = false
  244. pdfOthersButtonVC.imageWidth = 12.0
  245. pdfOthersButtonVC.imageHeight = 12.0
  246. pdfOthersButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  247. pdfOthersButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: othersBoxHeightConstraint)
  248. // if self.activitiesBox.isHidden {
  249. // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true)
  250. // } else {
  251. // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: [], isInitialize: true)
  252. // }
  253. self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true)
  254. self.activitiesBox.contentView = KMProductPromotionViewController.init().view
  255. activitiesBox.cornerRadius = 8.0
  256. rightTopBox.fillColor = .white
  257. rightBottomBox.fillColor = .white
  258. rightFullBox.fillColor = .white
  259. productPromotionBox.fillColor = .clear
  260. product_active_scrollView.verticalScrollElasticity = .none
  261. self.refreshRightBoxUI(.Home)
  262. fastToolViewController.delete = self
  263. historyFileViewController.delete = self
  264. pdfToolsViewController.delete = self
  265. dragView.delete = self
  266. }
  267. func initLocalization() {
  268. self.favoriteDocumentsLabel.stringValue = NSLocalizedString("Favorite Documents", comment: "")
  269. homeButtonVC.stringValue = NSLocalizedString("Home", comment: "")
  270. pdfToolsButtonVC.stringValue = NSLocalizedString("PDF Tools", comment: "")
  271. cloudDocumentsButtonVC.stringValue = NSLocalizedString("Cloud Documents", comment: "")
  272. openPDFButtonVC.stringValue = NSLocalizedString("Open File", comment: "")
  273. openPDFBox.toolTip = NSLocalizedString("Open PDF", comment: "")
  274. createPDFButtonVC.stringValue = NSLocalizedString("Create PDF", comment: "")
  275. createPDFBox.toolTip = NSLocalizedString("Create PDF", comment: "")
  276. pdfSeriesButtonVC.stringValue = NSLocalizedString("PDF Pro Series", comment: "")
  277. pdfOthersButtonVC.stringValue = NSLocalizedString("Others", comment: "")
  278. }
  279. // MARK: Action
  280. @IBAction func productPromotionButtonAction(_ sender: NSButton) {
  281. self.productPromotionFoldOrUnfold(sender)
  282. }
  283. // MARK: Public Methods
  284. func savePDFDocument(_ pdf: CPDFDocument, password: String) -> Void {
  285. DispatchQueue.global(qos: .`default`).async {
  286. var isSuccessfully = false
  287. if pdf.isEncrypted {
  288. let dic = [
  289. CPDFDocumentWriteOption.userPasswordOption : password,
  290. CPDFDocumentWriteOption.ownerPasswordOption : password
  291. ]
  292. isSuccessfully = pdf.write(to: pdf.documentURL, withOptions: dic)
  293. } else {
  294. isSuccessfully = pdf.write(to: pdf.documentURL)
  295. }
  296. if !isSuccessfully {
  297. isSuccessfully = NSData(data: pdf.dataRepresentation()).write(to: pdf.documentURL, atomically: true)
  298. }
  299. DispatchQueue.main.sync {
  300. if isSuccessfully {
  301. let workspace = NSWorkspace.shared
  302. let url = URL(fileURLWithPath: pdf.documentURL?.path ?? "")
  303. workspace.activateFileViewerSelecting([url])
  304. } else {
  305. let alert = NSAlert()
  306. alert.alertStyle = .critical
  307. alert.messageText = NSLocalizedString("Failed to insert page(s)!", comment: "")
  308. alert.runModal()
  309. }
  310. }
  311. }
  312. }
  313. // MARK: Private Methods
  314. func refreshUI() -> Void {
  315. homeButtonVC.updateUI()
  316. pdfToolsButtonVC.updateUI()
  317. cloudDocumentsButtonVC.updateUI()
  318. }
  319. func refreshProductActiveSpacing() -> Void {
  320. 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
  321. // print("height == %f", height)
  322. if height > 24.0 {
  323. self.product_active_spacing.constant = height
  324. } else {
  325. self.product_active_spacing.constant = 24.0
  326. }
  327. }
  328. func fetchDifferentFilePath(filePath: String) -> String {
  329. var resultFilePath = filePath
  330. var index: Int = 0
  331. while (FileManager.default.fileExists(atPath: resultFilePath)) {
  332. index += 1
  333. let path = NSString(string: filePath).deletingPathExtension + "(" + String(index) + ")"
  334. resultFilePath = NSString(string: path).appendingPathExtension(NSString(string: filePath).pathExtension)!
  335. }
  336. return resultFilePath;
  337. }
  338. // MARK: Common methods
  339. func workSpaceOpenUrl(_ url: NSString) {
  340. let httpUrl: NSURL = NSURL.init(string: url as String)!
  341. let tWorkSpace: NSWorkspace = NSWorkspace.shared
  342. if !tWorkSpace.open(httpUrl as URL) {
  343. tWorkSpace.open(httpUrl as URL)
  344. }
  345. }
  346. func createPopoverAction(_ data: [String]) {
  347. if (self.popover != nil && self.popover!.isShown) {
  348. self.popover?.close()
  349. self.popover = nil
  350. return
  351. }
  352. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(data)
  353. let createFilePopover: NSPopover = NSPopover.init()
  354. self.popover = createFilePopover
  355. createFilePopover.contentViewController = vc
  356. createFilePopover.animates = true
  357. createFilePopover.behavior = .semitransient
  358. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  359. 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)
  360. vc.customBoxWidthLayoutConstraint.constant = createPDFBox.frame.width
  361. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  362. if downEntered {
  363. self?.popover?.close()
  364. self?.popover = nil
  365. self?.popoverItemAction(count)
  366. }
  367. }
  368. }
  369. }