KMHomeViewController.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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", "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. "SignFlow" : ["Name" : "SignFlow", "Image" : "icon_home_logo_signflow", "link":""],
  108. "FiImage Editor" : ["Name" : "FiImage Editor", "Image" : "icon_home_logo_editor", "link":""],
  109. "FiImage Screen" : ["Name" : "FiImage Screen", "Image" : "icon_home_logo_screen", "link":""],
  110. "Free PDF Templates" : ["Name" : "Free PDF Templates", "Image" : "icon_home_logo_templates"], "link":""]
  111. NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { (aEvent) -> NSEvent? in
  112. self.otherMouseDown(with: aEvent)
  113. return aEvent
  114. }
  115. self.initNetworkingData()
  116. self.initLocalization()
  117. self.initializeUI()
  118. refreshUI()
  119. NotificationCenter.default.addObserver(self, selector: #selector(homeFileRectChange(_:)), name: Notification.Name("KMHomeFileRectChange"), object: nil)
  120. }
  121. func initNetworkingData() {
  122. KMAdvertisementManager.manager.fetchDataWithResponseObject { [unowned self] data, responseObject, error in
  123. print("获取广告数据成功")
  124. // print(error)
  125. // print(responseObject)
  126. if data != nil {
  127. for model in data! {
  128. guard let sectionContent = model.content else { break }
  129. if model.showType == .list {
  130. for section in sectionContent {
  131. print("list section 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: section.sectionTitle?.language))
  132. guard let itemContent = section.content else { break }
  133. for item in itemContent {
  134. print("item 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.title?.language))
  135. print("item 图片链接" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.imageURL?.normal?.universal))
  136. print("item 点击链接" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.linkURL))
  137. print("item tips" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tips))
  138. print("item actionType" + String(Int(item.actionType.rawValue)))
  139. }
  140. }
  141. }
  142. if model.showType == .view {
  143. for section in sectionContent {
  144. print("view section 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: section.sectionTitle?.language))
  145. guard let itemContent = section.content else { break }
  146. for item in itemContent {
  147. print("item 标题 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.title?.language))
  148. print("item 图片链接 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.imageURL?.normal?.universal))
  149. print("item 点击链接 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.linkURL))
  150. print("item tips = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tips))
  151. print("item actionType = " + String(Int(item.actionType.rawValue)))
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. // MARK: Init
  160. func initializeUI() {
  161. openPDFBox.fillColor = .clear
  162. openPDFBox.contentView = openPDFButtonVC.view
  163. openPDFButtonVC.target = self
  164. openPDFButtonVC.action = #selector(self.homeToolAction(_:))
  165. openPDFButtonVC.button(type: .Brand, size: .l, height: openPDFBoxHeight)
  166. createPDFBox.fillColor = .clear
  167. createPDFBox.contentView = createPDFButtonVC.view
  168. createPDFButtonVC.target = self
  169. createPDFButtonVC.action = #selector(self.homeToolAction(_:))
  170. createPDFButtonVC.button(type: .Sec, size: .l, height: createPDFBoxHeight)
  171. createPDFButtonVC.textButtonBox.move = { [unowned self](mouseEntered: Bool) -> Void in
  172. if mouseEntered {
  173. if self.createPDFButtonVC.state != .Sel && self.createPDFButtonVC.canHover {
  174. self.createPDFButtonVC.state = .Hov
  175. self.createPDFButtonVC.updateUI()
  176. self.createPDFImage.state = .Hov
  177. self.createPDFImage.updateUI()
  178. self.createPDFImage.canHover = false
  179. }
  180. } else {
  181. if self.createPDFButtonVC.state != .Sel && self.createPDFButtonVC.canHover {
  182. self.createPDFButtonVC.state = .Norm
  183. self.createPDFButtonVC.updateUI()
  184. self.createPDFImage.state = .Norm
  185. self.createPDFImage.updateUI()
  186. self.createPDFImage.canHover = true
  187. }
  188. }
  189. }
  190. createPDFView.contentView = createPDFImage.view
  191. createPDFImage.image = NSImage(named: "icon_btn_arrow_gray_down_s_norm_false")!
  192. createPDFImage.target = self
  193. createPDFImage.action = #selector(self.homeToolAction(_:))
  194. createPDFImage.button(type: .Icon_Fill, size: .m, height: createPDFViewHeight)
  195. homeBox.fillColor = .clear
  196. homeBox.contentView = homeButtonVC.view
  197. homeButtonVC.target = self
  198. homeButtonVC.action = #selector(self.homeToolAction(_:))
  199. homeButtonVC.image = NSImage(named: "icon_home_sidebar_home_false")!
  200. homeButtonVC.image_hover = NSImage(named: "icon_home_sidebar_home_false")!
  201. homeButtonVC.image_click = NSImage(named: "icon_home_sidebar_home_true")!
  202. homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: homeBoxHeight)
  203. homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  204. homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  205. pdfToolsBox.fillColor = .clear
  206. pdfToolsBox.contentView = pdfToolsButtonVC.view
  207. pdfToolsButtonVC.target = self
  208. pdfToolsButtonVC.action = #selector(self.homeToolAction(_:))
  209. pdfToolsButtonVC.image = NSImage(named: "icon_home_sidebar_tool_false")!
  210. pdfToolsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_tool_false")!
  211. pdfToolsButtonVC.image_click = NSImage(named: "icon_home_sidebar_tool_true")!
  212. pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: pdfToolsBoxHeight)
  213. pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  214. pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  215. cloudDocumentsBox.fillColor = .clear
  216. cloudDocumentsBox.contentView = cloudDocumentsButtonVC.view
  217. cloudDocumentsButtonVC.target = self
  218. cloudDocumentsButtonVC.action = #selector(self.homeToolAction(_:))
  219. cloudDocumentsButtonVC.image = NSImage(named: "icon_home_sidebar_cloud_false")!
  220. cloudDocumentsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_cloud_false")!
  221. cloudDocumentsButtonVC.image_click = NSImage(named: "icon_home_sidebar_cloud_true")!
  222. cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: cloudDocumentsBoxHeight)
  223. cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov)
  224. cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel)
  225. pdfSeriesBox.fillColor = .clear
  226. pdfSeriesBox.contentView = pdfSeriesButtonVC.view
  227. pdfSeriesButtonVC.target = self
  228. pdfSeriesButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:))
  229. pdfSeriesButtonVC.type = .Right
  230. pdfSeriesButtonVC.canHover = false
  231. pdfSeriesButtonVC.imageWidth = 12.0
  232. pdfSeriesButtonVC.imageHeight = 12.0
  233. pdfSeriesButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  234. pdfSeriesButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: pdfSeriesBoxHeight)
  235. pdfOthersBox.fillColor = .clear
  236. pdfOthersBox.contentView = pdfOthersButtonVC.view
  237. pdfOthersButtonVC.target = self
  238. pdfOthersButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:))
  239. pdfOthersButtonVC.type = .Right
  240. pdfOthersButtonVC.canHover = false
  241. pdfOthersButtonVC.imageWidth = 12.0
  242. pdfOthersButtonVC.imageHeight = 12.0
  243. pdfOthersButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  244. pdfOthersButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: othersBoxHeightConstraint)
  245. // if self.activitiesBox.isHidden {
  246. // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true)
  247. // } else {
  248. // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: [], isInitialize: true)
  249. // }
  250. self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true)
  251. self.activitiesBox.contentView = KMProductPromotionViewController.init().view
  252. activitiesBox.cornerRadius = 8.0
  253. rightTopBox.fillColor = .white
  254. rightBottomBox.fillColor = .white
  255. rightFullBox.fillColor = .white
  256. productPromotionBox.fillColor = .clear
  257. product_active_scrollView.verticalScrollElasticity = .none
  258. self.refreshRightBoxUI(.Home)
  259. fastToolViewController.delete = self
  260. historyFileViewController.delete = self
  261. pdfToolsViewController.delete = self
  262. dragView.delete = self
  263. }
  264. func initLocalization() {
  265. self.favoriteDocumentsLabel.stringValue = NSLocalizedString("Favorite Documents", comment: "")
  266. homeButtonVC.stringValue = NSLocalizedString("Home", comment: "")
  267. pdfToolsButtonVC.stringValue = NSLocalizedString("PDF Tools", comment: "")
  268. cloudDocumentsButtonVC.stringValue = NSLocalizedString("Cloud Documents", comment: "")
  269. openPDFButtonVC.stringValue = NSLocalizedString("Open File", comment: "")
  270. openPDFBox.toolTip = NSLocalizedString("Open PDF", comment: "")
  271. createPDFButtonVC.stringValue = NSLocalizedString("Create PDF", comment: "")
  272. createPDFBox.toolTip = NSLocalizedString("Create PDF", comment: "")
  273. pdfSeriesButtonVC.stringValue = NSLocalizedString("PDF Pro Series", comment: "")
  274. pdfOthersButtonVC.stringValue = NSLocalizedString("Others", comment: "")
  275. }
  276. // MARK: Action
  277. @IBAction func productPromotionButtonAction(_ sender: NSButton) {
  278. self.productPromotionFoldOrUnfold(sender)
  279. }
  280. // MARK: Public Methods
  281. func savePDFDocument(_ pdf: CPDFDocument, password: String) -> Void {
  282. DispatchQueue.global(qos: .`default`).async {
  283. var isSuccessfully = false
  284. if pdf.isEncrypted {
  285. let dic = [
  286. CPDFDocumentWriteOption.userPasswordOption : password,
  287. CPDFDocumentWriteOption.ownerPasswordOption : password
  288. ]
  289. isSuccessfully = pdf.write(to: pdf.documentURL, withOptions: dic)
  290. } else {
  291. isSuccessfully = pdf.write(to: pdf.documentURL)
  292. }
  293. if !isSuccessfully {
  294. isSuccessfully = NSData(data: pdf.dataRepresentation()).write(to: pdf.documentURL, atomically: true)
  295. }
  296. DispatchQueue.main.sync {
  297. if isSuccessfully {
  298. let workspace = NSWorkspace.shared
  299. let url = URL(fileURLWithPath: pdf.documentURL?.path ?? "")
  300. workspace.activateFileViewerSelecting([url])
  301. } else {
  302. let alert = NSAlert()
  303. alert.alertStyle = .critical
  304. alert.messageText = NSLocalizedString("Failed to insert page(s)!", comment: "")
  305. alert.runModal()
  306. }
  307. }
  308. }
  309. }
  310. // MARK: Private Methods
  311. func refreshUI() -> Void {
  312. homeButtonVC.updateUI()
  313. pdfToolsButtonVC.updateUI()
  314. cloudDocumentsButtonVC.updateUI()
  315. }
  316. func refreshProductActiveSpacing() -> Void {
  317. 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
  318. // print("height == %f", height)
  319. if height > 24.0 {
  320. self.product_active_spacing.constant = height
  321. } else {
  322. self.product_active_spacing.constant = 24.0
  323. }
  324. }
  325. func fetchDifferentFilePath(filePath: String) -> String {
  326. var resultFilePath = filePath
  327. var index: Int = 0
  328. while (FileManager.default.fileExists(atPath: resultFilePath)) {
  329. index += 1
  330. let path = NSString(string: filePath).deletingPathExtension + "(" + String(index) + ")"
  331. resultFilePath = NSString(string: path).appendingPathExtension(NSString(string: filePath).pathExtension)!
  332. }
  333. return resultFilePath;
  334. }
  335. // MARK: Common methods
  336. func workSpaceOpenUrl(_ url: NSString) {
  337. let httpUrl: NSURL = NSURL.init(string: url as String)!
  338. let tWorkSpace: NSWorkspace = NSWorkspace.shared
  339. if !tWorkSpace.open(httpUrl as URL) {
  340. tWorkSpace.open(httpUrl as URL)
  341. }
  342. }
  343. func createPopoverAction(_ data: [String]) {
  344. if (self.popover != nil && self.popover!.isShown) {
  345. self.popover?.close()
  346. self.popover = nil
  347. return
  348. }
  349. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(data)
  350. let createFilePopover: NSPopover = NSPopover.init()
  351. self.popover = createFilePopover
  352. createFilePopover.contentViewController = vc
  353. createFilePopover.animates = true
  354. createFilePopover.behavior = .semitransient
  355. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  356. 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)
  357. vc.customBoxWidthLayoutConstraint.constant = createPDFBox.frame.width
  358. vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in
  359. if downEntered {
  360. self?.popover?.close()
  361. self?.popover = nil
  362. self?.popoverItemAction(count)
  363. }
  364. }
  365. }
  366. }