KMGuideInfoWindowController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. //
  2. // KMGuideInfoWindowController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/12/5.
  6. //
  7. import Cocoa
  8. class KMGuideInfoWindowController: NSWindowController {
  9. @IBOutlet weak var contendBox: NSBox!
  10. @objc var coverView: KMGuideCoverView!
  11. @objc var openFileGuidePanel: KMOpenFileGuidePanel!
  12. @objc var openFileGuideProperty: KMOpenFileGuideProperty!
  13. @objc var openFileGuideToolbar: KMOpenFileGuideToolbar!
  14. @objc var openFileFunctionView: KMOpenFileFunctionView!
  15. @objc var convertGuideView: KMConvertGuideView!
  16. @objc var customColorGuideView: KMCustomColorGuideView!
  17. @objc var aiIconGuideView: KMAIIconGuideView!
  18. @objc var _type: KMGuideInfoType = .none
  19. @objc var openPanelRect: CGRect = .zero
  20. @objc var rightPanelRect: CGRect = .zero
  21. @objc var _digitalBoxRect: CGRect = .zero
  22. @objc var compareItemRect: CGRect = .zero
  23. @objc var purchaseHandle: ((_ view: KMGuideInfoWindowController)->Void)?
  24. @objc var normalGuideFinishHandle: ((_ view: KMGuideInfoWindowController)->Void)?
  25. @objc var finishHandle: ((_ view: KMGuideInfoWindowController, _ actionType: KMGuideActionType)->Void)?
  26. @objc var openFileToggleHandle: ((_ view: KMGuideInfoWindowController, _ infoType: KMGuideInfoType)->Void)?
  27. //MARK: Static Method
  28. @objc static func availableShow(_ type: KMGuideInfoType) -> Bool {
  29. if type == .openFileNormal {
  30. if (UserDefaults.standard.object(forKey: kKMGuideInfoOpenFileNormalKey) == nil) {
  31. return true
  32. }
  33. } else if type == .digitalSignGuide {
  34. if (UserDefaults.standard.object(forKey: kKMGuideInfoDigitalSignKey) == nil) {
  35. return true
  36. }
  37. } else if type == .pdfCompareGuide {
  38. if (UserDefaults.standard.object(forKey: kKMGuideInfoPDFCompareKey) == nil) {
  39. return true
  40. }
  41. } else if type == .convertGuide {
  42. if (UserDefaults.standard.object(forKey: kKMGuideInfoFileConvertKey) == nil) {
  43. return true
  44. }
  45. } else if type == .customColor {
  46. if (UserDefaults.standard.object(forKey: kKMGuideInfoCustomColorKey) == nil) {
  47. return true
  48. }
  49. } else if type == .aiTipIconInfo {
  50. if (UserDefaults.standard.object(forKey: kKMGuideInfoAITipIconViewKey) == nil) {
  51. return true
  52. }
  53. }
  54. return false
  55. }
  56. static func setDidShowFor(_ type: KMGuideInfoType) -> Void {
  57. if type == .openFileNormal {
  58. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoOpenFileNormalKey)
  59. UserDefaults.standard.synchronize()
  60. } else if type == .digitalSignGuide {
  61. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoDigitalSignKey)
  62. UserDefaults.standard.synchronize()
  63. } else if type == .pdfCompareGuide {
  64. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoPDFCompareKey)
  65. UserDefaults.standard.synchronize()
  66. } else if type == .convertGuide {
  67. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoFileConvertKey)
  68. UserDefaults.standard.synchronize()
  69. } else if type == .customColor {
  70. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoCustomColorKey)
  71. UserDefaults.standard.synchronize()
  72. } else if type == .aiTipIconInfo {
  73. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoAITipIconViewKey)
  74. UserDefaults.standard.synchronize()
  75. }
  76. }
  77. @objc static func currentWC() -> KMGuideInfoWindowController {
  78. if let controller: KMGuideInfoWindowController = KMGuideInfoWindowController.fetchSampleController() {
  79. return controller
  80. } else {
  81. let guideInfoWC: KMGuideInfoWindowController = KMGuideInfoWindowController.init(windowNibName: "KMGuideInfoWindowController")
  82. return guideInfoWC
  83. }
  84. }
  85. static func fetchSampleController() -> KMGuideInfoWindowController? {
  86. for window in NSApp.windows {
  87. let controller = window.windowController
  88. if controller is KMGuideInfoWindowController {
  89. return controller as? KMGuideInfoWindowController
  90. }
  91. }
  92. return nil
  93. }
  94. //MARK: SystemMethod
  95. override func showWindow(_ sender: Any?) {
  96. super.showWindow(sender)
  97. self.show()
  98. }
  99. override func windowDidLoad() {
  100. super.windowDidLoad()
  101. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  102. self.window?.backgroundColor = NSColor.clear
  103. self.window?.contentView?.wantsLayer = true
  104. self.window?.contentView?.layer?.backgroundColor = NSColor.clear.cgColor;
  105. self.window?.isOpaque = false
  106. self.contendBox.fillColor = NSColor.clear
  107. self.coverView = KMGuideCoverView.createFromNib()
  108. self.coverView.frame = self.contendBox.bounds
  109. self.contendBox.contentView = self.coverView
  110. }
  111. //MARK: Setter
  112. @objc var type: KMGuideInfoType {
  113. set {
  114. _type = newValue;
  115. }
  116. get {
  117. return _type;
  118. }
  119. }
  120. @objc var digitalBoxRect: CGRect {
  121. set {
  122. _digitalBoxRect = newValue;
  123. }
  124. get {
  125. return _digitalBoxRect;
  126. }
  127. }
  128. //MARK: Public Method
  129. @objc func show() {
  130. if self.type == .openFileNormal {
  131. self.type = .openFileNormalPanel
  132. }
  133. self.loadGuideInfoView()
  134. }
  135. func clearViews() {
  136. let viewsArr = NSArray.init(array: self.coverView.subviews)
  137. for view in viewsArr {
  138. if view is KMOpenFileGuidePanel {
  139. (view as AnyObject).removeFromSuperview()
  140. } else if view is KMOpenFileGuideProperty {
  141. (view as AnyObject).removeFromSuperview()
  142. } else if view is KMOpenFileGuideToolbar {
  143. (view as AnyObject).removeFromSuperview()
  144. } else if view is KMOpenFileFunctionView {
  145. (view as AnyObject).removeFromSuperview()
  146. } else if view is KMConvertGuideView {
  147. (view as AnyObject).removeFromSuperview()
  148. } else if view is KMCustomColorGuideView {
  149. (view as AnyObject).removeFromSuperview()
  150. } else if view is KMAIIconGuideView {
  151. (view as AnyObject).removeFromSuperview()
  152. }
  153. }
  154. self.coverView.display()
  155. if self.window != nil {
  156. var rect = self.window?.frame
  157. rect?.size.width += 1
  158. self.window?.setFrame(rect!, display: true)
  159. }
  160. }
  161. func loadGuideInfoView() {
  162. DispatchQueue.main.async {
  163. self.clearViews()
  164. self.contendBox.contentView = self.coverView
  165. if self.type == .openFileNormalPanel {
  166. if self.openFileGuidePanel == nil {
  167. self.openFileGuidePanel = KMOpenFileGuidePanel.createFromNib()
  168. }
  169. self.openFileGuidePanel.frame = self.coverView.bounds
  170. self.openFileGuidePanel.autoresizingMask = [.width, .height]
  171. self.openFileGuidePanel.clickHandle = {[unowned self] view, actionType in
  172. if actionType == .skip {
  173. //Skip->数字签名
  174. //Skip->
  175. guard let callBack = self.normalGuideFinishHandle else {
  176. return
  177. }
  178. callBack(self)
  179. if KMGuideInfoWindowController.availableShow(.digitalSignGuide) &&
  180. (CGRectGetWidth(self.window!.frame) > CGRectGetMinX(self.digitalBoxRect) + 260) &&
  181. CGRectGetMinX(self.digitalBoxRect) > 450 {
  182. self.type = .digitalSignGuide
  183. self.loadGuideInfoView()
  184. } else {
  185. guard let callBack = self.finishHandle else {
  186. return
  187. }
  188. callBack(self, .windowNewFinish)
  189. self.closeAction()
  190. }
  191. } else if actionType == .next {
  192. //Next
  193. self.type = .openFileNormalProperty
  194. self.loadGuideInfoView()
  195. // guard let callBack = self.openFileToggleHandle else {
  196. // return
  197. // }
  198. // callBack(self, .openFileNormalProperty)
  199. // self.closeAction()
  200. }
  201. }
  202. self.openFileGuidePanel.circleRect = self.openPanelRect
  203. self.coverView.addSubview(self.openFileGuidePanel)
  204. self.openFileGuidePanel.updateViewColor()
  205. KMGuideInfoWindowController.setDidShowFor(.openFileNormal)//记录布局介绍
  206. } else if self.type == .openFileNormalProperty {
  207. guard let callBack = self.normalGuideFinishHandle else {
  208. return
  209. }
  210. callBack(self)
  211. if self.openFileGuideProperty == nil {
  212. self.openFileGuideProperty = KMOpenFileGuideProperty.createFromNib()
  213. }
  214. self.openFileGuideProperty.frame = self.coverView.bounds
  215. self.openFileGuideProperty.autoresizingMask = [.width, .height]
  216. self.openFileGuideProperty.clickHandle = {[unowned self] view, actionType in
  217. if actionType == .skip {
  218. //Skip->数字签名
  219. //Skip->
  220. guard let callBack = self.normalGuideFinishHandle else {
  221. return
  222. }
  223. callBack(self)
  224. if KMGuideInfoWindowController.availableShow(.digitalSignGuide) &&
  225. (CGRectGetWidth(self.window!.frame) > CGRectGetMinX(self.digitalBoxRect) + 260) &&
  226. CGRectGetMinX(self.digitalBoxRect) > 450 {
  227. self.type = .digitalSignGuide
  228. self.loadGuideInfoView()
  229. } else {
  230. guard let callBack = self.finishHandle else {
  231. return
  232. }
  233. callBack(self, .windowNewFinish)
  234. self.closeAction()
  235. }
  236. } else if actionType == .previous {
  237. //Previous
  238. self.type = .openFileNormalPanel
  239. self.loadGuideInfoView()
  240. guard let callBack = self.normalGuideFinishHandle else {
  241. return
  242. }
  243. callBack(self)
  244. // guard let callBack = self.openFileToggleHandle else {
  245. // return
  246. // }
  247. // callBack(self, .openFileNormalPanel)
  248. // self.closeAction()
  249. } else if actionType == .next {
  250. //Next
  251. self.type = .openFileNormalToolbar
  252. self.loadGuideInfoView()
  253. // guard let callBack = self.openFileToggleHandle else {
  254. // return
  255. // }
  256. // callBack(self, .openFileNormalToolbar)
  257. // self.closeAction()
  258. }
  259. }
  260. self.openFileGuideProperty.circleRect = self.rightPanelRect
  261. self.coverView.addSubview(self.openFileGuideProperty)
  262. self.openFileGuideProperty.updateViewColor()
  263. } else if self.type == .openFileNormalToolbar {
  264. if self.openFileGuideToolbar == nil {
  265. self.openFileGuideToolbar = KMOpenFileGuideToolbar.createFromNib()
  266. }
  267. self.openFileGuideToolbar.frame = self.coverView.bounds
  268. self.openFileGuideToolbar.autoresizingMask = [.width, .height]
  269. self.openFileGuideToolbar.clickHandle = {[unowned self] view, actionType in
  270. if actionType == .skip {
  271. //Skip->数字签名
  272. //Skip->
  273. guard let callBack = self.normalGuideFinishHandle else {
  274. return
  275. }
  276. callBack(self)
  277. if KMGuideInfoWindowController.availableShow(.digitalSignGuide) &&
  278. (CGRectGetWidth(self.window!.frame) > CGRectGetMinX(self.digitalBoxRect) + 260) &&
  279. CGRectGetMinX(self.digitalBoxRect) > 450 {
  280. self.type = .digitalSignGuide
  281. self.loadGuideInfoView()
  282. } else {
  283. guard let callBack = self.finishHandle else {
  284. return
  285. }
  286. callBack(self, .windowNewFinish)
  287. self.closeAction()
  288. }
  289. } else if actionType == .previous {
  290. //Next
  291. self.type = .openFileNormalProperty
  292. self.loadGuideInfoView()
  293. // guard let callBack = self.openFileToggleHandle else {
  294. // return
  295. // }
  296. // callBack(self, .openFileNormalProperty)
  297. // self.closeAction()
  298. } else if actionType == .finish {
  299. guard let callBack = self.normalGuideFinishHandle else {
  300. return
  301. }
  302. callBack(self)
  303. if KMGuideInfoWindowController.availableShow(.digitalSignGuide) &&
  304. (CGRectGetWidth(self.window!.frame) > CGRectGetMinX(self.digitalBoxRect) + 260) &&
  305. CGRectGetMinX(self.digitalBoxRect) > 450 {
  306. self.type = .digitalSignGuide
  307. self.loadGuideInfoView()
  308. } else {
  309. guard let callBack = self.finishHandle else {
  310. return
  311. }
  312. callBack(self, .windowNewFinish)
  313. self.closeAction()
  314. }
  315. }
  316. }
  317. self.coverView.addSubview(self.openFileGuideToolbar)
  318. } else if self.type == .digitalSignGuide || self.type == .pdfCompareGuide {
  319. if self.type == .digitalSignGuide {
  320. KMGuideInfoWindowController.setDidShowFor(.digitalSignGuide)//记录数字签名介绍
  321. } else if self.type == .pdfCompareGuide {
  322. KMGuideInfoWindowController.setDidShowFor(.pdfCompareGuide)//记录文件对比介绍
  323. }
  324. if self.openFileFunctionView == nil {
  325. self.openFileFunctionView = KMOpenFileFunctionView.createFromNib()
  326. }
  327. self.openFileFunctionView.circleRect = self.digitalBoxRect
  328. self.openFileFunctionView.frame = self.coverView.bounds
  329. self.openFileFunctionView.autoresizingMask = [.width, .height]
  330. self.openFileFunctionView.type = self.type
  331. self.openFileFunctionView.clickHandle = {[unowned self] view, actionType in
  332. if self.type == .digitalSignGuide {
  333. guard let callBack = self.finishHandle else {
  334. return
  335. }
  336. callBack(self, .windowDigitalFinish)
  337. if actionType == .getIt {
  338. self.closeAction()
  339. }
  340. } else if self.type == .pdfCompareGuide {
  341. if actionType == .getIt {
  342. self.closeAction()
  343. }
  344. }
  345. }
  346. self.coverView.addSubview(self.openFileFunctionView)
  347. self.openFileFunctionView.updateViewColor()
  348. } else if self.type == .convertGuide {
  349. if self.convertGuideView == nil {
  350. self.convertGuideView = KMConvertGuideView.createFromNib()
  351. }
  352. self.convertGuideView.frame = self.coverView.bounds
  353. self.convertGuideView.autoresizingMask = [.width, .height]
  354. self.convertGuideView.clickHandle = {[unowned self] view, actionType in
  355. if actionType == .skip {
  356. self.closeAction()
  357. } else if actionType == .purchase {
  358. guard let callBack = self.purchaseHandle else {
  359. return
  360. }
  361. callBack(self)
  362. self.closeAction()
  363. }
  364. }
  365. self.convertGuideView.circleRect = self.digitalBoxRect
  366. self.coverView.addSubview(self.convertGuideView)
  367. self.convertGuideView.updateViewColor()
  368. KMGuideInfoWindowController.setDidShowFor(.convertGuide)//记录转档介绍
  369. } else if self.type == .customColor {
  370. if self.customColorGuideView == nil {
  371. self.customColorGuideView = KMCustomColorGuideView.createFromNib()
  372. }
  373. self.customColorGuideView.frame = self.coverView.bounds
  374. self.customColorGuideView.autoresizingMask = [.width, .height]
  375. self.customColorGuideView.clickHandle = {[unowned self] view, actionType in
  376. if actionType == .getIt {
  377. self.closeAction()
  378. }
  379. }
  380. self.customColorGuideView.circleRect = self.digitalBoxRect
  381. self.customColorGuideView.updateViewColor()
  382. self.coverView.addSubview(self.customColorGuideView)
  383. KMGuideInfoWindowController.setDidShowFor(.customColor)//记录自定义颜色介绍
  384. } else if self.type == .aiTipIconInfo {
  385. if self.aiIconGuideView == nil {
  386. self.aiIconGuideView = KMAIIconGuideView.createFromNib()
  387. }
  388. self.aiIconGuideView.frame = self.coverView.bounds
  389. self.aiIconGuideView.autoresizingMask = [.width, .height]
  390. self.aiIconGuideView.clickHandle = {[unowned self] view, actionType in
  391. if actionType == .getIt {
  392. self.closeAction()
  393. }
  394. }
  395. self.aiIconGuideView.updateViewColor()
  396. self.aiIconGuideView.wantsLayer = true
  397. self.coverView.addSubview(self.aiIconGuideView)
  398. KMGuideInfoWindowController.setDidShowFor(.aiTipIconInfo)//记录自定义颜色介绍
  399. }
  400. }
  401. }
  402. func closeAction() {
  403. self.clearViews()
  404. self.close()
  405. }
  406. }