KMGuideInfoWindowController.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 editPDFPopGuideView: KMEditPDFPopGuideView?
  19. @objc var _type: KMGuideInfoType = .none
  20. @objc var openPanelRect: CGRect = .zero
  21. @objc var rightPanelRect: CGRect = .zero
  22. @objc var _digitalBoxRect: CGRect = .zero
  23. @objc var compareItemRect: CGRect = .zero
  24. @objc var purchaseHandle: ((_ view: KMGuideInfoWindowController)->Void)?
  25. @objc var normalGuideFinishHandle: ((_ view: KMGuideInfoWindowController)->Void)?
  26. @objc var finishHandle: ((_ view: KMGuideInfoWindowController, _ actionType: KMGuideActionType)->Void)?
  27. @objc var openFileToggleHandle: ((_ view: KMGuideInfoWindowController, _ infoType: KMGuideInfoType)->Void)?
  28. @objc var settingCallback: (()->Void)?
  29. var editPDFPopWindowFlag: Bool = false
  30. //MARK: Static Method
  31. @objc static func availableShow(_ type: KMGuideInfoType) -> Bool {
  32. if type == .openFileNormal {
  33. if Self._isArabicLanguage() {
  34. return false
  35. }
  36. if (UserDefaults.standard.object(forKey: kKMGuideInfoOpenFileNormalKey) == nil) {
  37. return true
  38. }
  39. } else if type == .digitalSignGuide {
  40. if Self._isArabicLanguage() {
  41. return false
  42. }
  43. if (UserDefaults.standard.object(forKey: kKMGuideInfoDigitalSignKey) == nil) {
  44. return true
  45. }
  46. } else if type == .pdfCompareGuide {
  47. if Self._isArabicLanguage() {
  48. return false
  49. }
  50. if (UserDefaults.standard.object(forKey: kKMGuideInfoPDFCompareKey) == nil) {
  51. return true
  52. }
  53. } else if type == .convertGuide {
  54. if (UserDefaults.standard.object(forKey: kKMGuideInfoFileConvertKey) == nil) {
  55. return true
  56. }
  57. } else if type == .customColor {
  58. if (UserDefaults.standard.object(forKey: kKMGuideInfoCustomColorKey) == nil) {
  59. return true
  60. }
  61. } else if type == .aiTipIconInfo {
  62. if (UserDefaults.standard.object(forKey: kKMGuideInfoAITipIconViewKey) == nil) {
  63. return true
  64. }
  65. } else if type == .editPDFPopWindow {
  66. if (UserDefaults.standard.object(forKey: kKMGuideInfoEditPDFPopWindowKey) == nil) {
  67. return true
  68. }
  69. } else if type == .measureGuide {
  70. if Self._isArabicLanguage() {
  71. return false
  72. }
  73. if (UserDefaults.standard.object(forKey: kKMGuideInfoMeasureKey) == nil) {
  74. return true
  75. }
  76. }
  77. return false
  78. }
  79. static func setDidShowFor(_ type: KMGuideInfoType) -> Void {
  80. if type == .openFileNormal {
  81. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoOpenFileNormalKey)
  82. UserDefaults.standard.synchronize()
  83. } else if type == .digitalSignGuide {
  84. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoDigitalSignKey)
  85. UserDefaults.standard.synchronize()
  86. } else if type == .pdfCompareGuide {
  87. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoPDFCompareKey)
  88. UserDefaults.standard.synchronize()
  89. } else if type == .convertGuide {
  90. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoFileConvertKey)
  91. UserDefaults.standard.synchronize()
  92. } else if type == .customColor {
  93. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoCustomColorKey)
  94. UserDefaults.standard.synchronize()
  95. } else if type == .aiTipIconInfo {
  96. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoAITipIconViewKey)
  97. UserDefaults.standard.synchronize()
  98. } else if type == .editPDFPopWindow {
  99. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoEditPDFPopWindowKey)
  100. UserDefaults.standard.synchronize()
  101. } else if type == .measureGuide {
  102. UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoMeasureKey)
  103. UserDefaults.standard.synchronize()
  104. }
  105. }
  106. static var currentWindowController: KMGuideInfoWindowController!
  107. @objc static func currentWC() -> KMGuideInfoWindowController {
  108. if currentWindowController != nil {
  109. return currentWindowController
  110. } else {
  111. let guideInfoWC: KMGuideInfoWindowController = KMGuideInfoWindowController.init(windowNibName: "KMGuideInfoWindowController")
  112. currentWindowController = guideInfoWC;
  113. return guideInfoWC
  114. }
  115. }
  116. deinit {
  117. print("deinit Action")
  118. }
  119. //MARK: SystemMethod
  120. override func showWindow(_ sender: Any?) {
  121. super.showWindow(sender)
  122. self.show()
  123. }
  124. override func windowDidLoad() {
  125. super.windowDidLoad()
  126. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  127. self.window?.backgroundColor = NSColor.clear
  128. self.window?.contentView?.wantsLayer = true
  129. self.window?.contentView?.layer?.backgroundColor = NSColor.clear.cgColor;
  130. self.window?.isOpaque = false
  131. self.contendBox.fillColor = NSColor.clear
  132. self.coverView = KMGuideCoverView.createFromNib()
  133. self.coverView.frame = self.contendBox.bounds
  134. self.contendBox.contentView = self.coverView
  135. }
  136. //MARK: Setter
  137. @objc var type: KMGuideInfoType {
  138. set {
  139. _type = newValue;
  140. }
  141. get {
  142. return _type;
  143. }
  144. }
  145. @objc var digitalBoxRect: CGRect {
  146. set {
  147. _digitalBoxRect = newValue;
  148. }
  149. get {
  150. return _digitalBoxRect;
  151. }
  152. }
  153. // MARK: Private Methods
  154. // 是否阿拉伯语言
  155. private static func _isArabicLanguage() -> Bool {
  156. return NSLocalizedString("Right click a color and select “Change Color...“.", comment: "") == "انقر بزر الماوس الأيمن فوق اللون وحدد \"تغيير اللون...\"."
  157. }
  158. //MARK: Public Method
  159. @objc func show() {
  160. if self.type == .openFileNormal {
  161. self.type = .openFileNormalPanel
  162. }
  163. self.loadGuideInfoView()
  164. }
  165. func clearViews() {
  166. let viewsArr = NSArray.init(array: self.coverView.subviews)
  167. for view in viewsArr {
  168. if view is KMOpenFileGuidePanel {
  169. (view as AnyObject).removeFromSuperview()
  170. } else if view is KMOpenFileGuideProperty {
  171. (view as AnyObject).removeFromSuperview()
  172. } else if view is KMOpenFileGuideToolbar {
  173. (view as AnyObject).removeFromSuperview()
  174. } else if view is KMOpenFileFunctionView {
  175. (view as AnyObject).removeFromSuperview()
  176. } else if view is KMConvertGuideView {
  177. (view as AnyObject).removeFromSuperview()
  178. } else if view is KMCustomColorGuideView {
  179. (view as AnyObject).removeFromSuperview()
  180. } else if view is KMAIIconGuideView {
  181. (view as AnyObject).removeFromSuperview()
  182. } else if view is KMEditPDFPopGuideView {
  183. (view as AnyObject).removeFromSuperview()
  184. }
  185. }
  186. self.coverView.display()
  187. if self.window != nil {
  188. var rect = self.window?.frame
  189. rect?.size.width += 1
  190. self.window?.setFrame(rect!, display: true)
  191. }
  192. }
  193. func loadGuideInfoView() {
  194. DispatchQueue.main.async {
  195. self.clearViews()
  196. self.contendBox.contentView = self.coverView
  197. if self.type == .openFileNormalPanel {
  198. if self.openFileGuidePanel == nil {
  199. self.openFileGuidePanel = KMOpenFileGuidePanel.createFromNib()
  200. }
  201. self.openFileGuidePanel.frame = self.coverView.bounds
  202. self.openFileGuidePanel.autoresizingMask = [.width, .height]
  203. self.openFileGuidePanel.clickHandle = {[weak self] view, actionType in
  204. guard let self = self else { return }
  205. if actionType == .skip {
  206. //Skip->数字签名
  207. //Skip->
  208. guard let callBack = self.normalGuideFinishHandle else {
  209. return
  210. }
  211. callBack(self)
  212. if KMGuideInfoWindowController.availableShow(.digitalSignGuide) &&
  213. (NSWidth(self.window!.frame) > NSMinX(self.digitalBoxRect) + 260) &&
  214. NSMinX(self.digitalBoxRect) > 450 {
  215. self.type = .digitalSignGuide
  216. self.loadGuideInfoView()
  217. } else {
  218. guard let callBack = self.finishHandle else {
  219. return
  220. }
  221. callBack(self, .windowNewFinish)
  222. self.closeAction()
  223. }
  224. } else if actionType == .next {
  225. //Next
  226. self.type = .openFileNormalProperty
  227. self.loadGuideInfoView()
  228. // guard let callBack = self.openFileToggleHandle else {
  229. // return
  230. // }
  231. // callBack(self, .openFileNormalProperty)
  232. // self.closeAction()
  233. }
  234. }
  235. self.openFileGuidePanel.circleRect = self.openPanelRect
  236. self.coverView.addSubview(self.openFileGuidePanel)
  237. self.openFileGuidePanel.updateViewColor()
  238. KMGuideInfoWindowController.setDidShowFor(.openFileNormal)//记录布局介绍
  239. } else if self.type == .openFileNormalProperty {
  240. guard let callBack = self.normalGuideFinishHandle else {
  241. return
  242. }
  243. callBack(self)
  244. if self.openFileGuideProperty == nil {
  245. self.openFileGuideProperty = KMOpenFileGuideProperty.createFromNib()
  246. }
  247. self.openFileGuideProperty.frame = self.coverView.bounds
  248. self.openFileGuideProperty.autoresizingMask = [.width, .height]
  249. self.openFileGuideProperty.clickHandle = {[weak self] view, actionType in
  250. guard let self = self else { return }
  251. if actionType == .skip {
  252. //Skip->数字签名
  253. //Skip->
  254. guard let callBack = self.normalGuideFinishHandle else {
  255. return
  256. }
  257. callBack(self)
  258. if KMGuideInfoWindowController.availableShow(.digitalSignGuide) &&
  259. (NSWidth(self.window!.frame) > NSMinX(self.digitalBoxRect) + 260) &&
  260. NSMinX(self.digitalBoxRect) > 450 {
  261. self.type = .digitalSignGuide
  262. self.loadGuideInfoView()
  263. } else {
  264. guard let callBack = self.finishHandle else {
  265. return
  266. }
  267. callBack(self, .windowNewFinish)
  268. self.closeAction()
  269. }
  270. } else if actionType == .previous {
  271. //Previous
  272. self.type = .openFileNormalPanel
  273. self.loadGuideInfoView()
  274. guard let callBack = self.normalGuideFinishHandle else {
  275. return
  276. }
  277. callBack(self)
  278. // guard let callBack = self.openFileToggleHandle else {
  279. // return
  280. // }
  281. // callBack(self, .openFileNormalPanel)
  282. // self.closeAction()
  283. } else if actionType == .next {
  284. //Next
  285. self.type = .openFileNormalToolbar
  286. self.loadGuideInfoView()
  287. // guard let callBack = self.openFileToggleHandle else {
  288. // return
  289. // }
  290. // callBack(self, .openFileNormalToolbar)
  291. // self.closeAction()
  292. }
  293. }
  294. self.openFileGuideProperty.circleRect = self.rightPanelRect
  295. self.coverView.addSubview(self.openFileGuideProperty)
  296. self.openFileGuideProperty.updateViewColor()
  297. } else if self.type == .openFileNormalToolbar {
  298. if self.openFileGuideToolbar == nil {
  299. self.openFileGuideToolbar = KMOpenFileGuideToolbar.createFromNib()
  300. }
  301. self.openFileGuideToolbar.frame = self.coverView.bounds
  302. self.openFileGuideToolbar.autoresizingMask = [.width, .height]
  303. self.openFileGuideToolbar.clickHandle = {[weak self] view, actionType in
  304. guard let self = self else { return }
  305. if actionType == .skip {
  306. //Skip->数字签名
  307. //Skip->
  308. guard let callBack = self.normalGuideFinishHandle else {
  309. return
  310. }
  311. callBack(self)
  312. if KMGuideInfoWindowController.availableShow(.digitalSignGuide) &&
  313. (NSWidth(self.window!.frame) > NSMinX(self.digitalBoxRect) + 260) &&
  314. NSMinX(self.digitalBoxRect) > 450 {
  315. self.type = .digitalSignGuide
  316. self.loadGuideInfoView()
  317. } else {
  318. guard let callBack = self.finishHandle else {
  319. return
  320. }
  321. callBack(self, .windowNewFinish)
  322. self.closeAction()
  323. }
  324. } else if actionType == .previous {
  325. //Next
  326. self.type = .openFileNormalProperty
  327. self.loadGuideInfoView()
  328. // guard let callBack = self.openFileToggleHandle else {
  329. // return
  330. // }
  331. // callBack(self, .openFileNormalProperty)
  332. // self.closeAction()
  333. } else if actionType == .finish {
  334. guard let callBack = self.normalGuideFinishHandle else {
  335. return
  336. }
  337. callBack(self)
  338. if KMGuideInfoWindowController.availableShow(.digitalSignGuide) &&
  339. (NSWidth(self.window!.frame) > NSMinX(self.digitalBoxRect) + 260) &&
  340. NSMinX(self.digitalBoxRect) > 450 {
  341. self.type = .digitalSignGuide
  342. self.loadGuideInfoView()
  343. } else {
  344. guard let callBack = self.finishHandle else {
  345. return
  346. }
  347. callBack(self, .windowNewFinish)
  348. self.closeAction()
  349. }
  350. }
  351. }
  352. self.coverView.addSubview(self.openFileGuideToolbar)
  353. } else if self.type == .digitalSignGuide || self.type == .pdfCompareGuide {
  354. if self.type == .digitalSignGuide {
  355. KMGuideInfoWindowController.setDidShowFor(.digitalSignGuide)//记录数字签名介绍
  356. } else if self.type == .pdfCompareGuide {
  357. KMGuideInfoWindowController.setDidShowFor(.pdfCompareGuide)//记录文件对比介绍
  358. }
  359. if self.openFileFunctionView == nil {
  360. self.openFileFunctionView = KMOpenFileFunctionView.createFromNib()
  361. }
  362. self.openFileFunctionView.circleRect = self.digitalBoxRect
  363. self.openFileFunctionView.frame = self.coverView.bounds
  364. self.openFileFunctionView.autoresizingMask = [.width, .height]
  365. self.openFileFunctionView.type = self.type
  366. self.openFileFunctionView.clickHandle = {[weak self] view, actionType in
  367. guard let self = self else { return }
  368. if self.type == .digitalSignGuide {
  369. guard let callBack = self.finishHandle else {
  370. return
  371. }
  372. callBack(self, .windowDigitalFinish)
  373. if actionType == .getIt {
  374. self.closeAction()
  375. }
  376. } else if self.type == .pdfCompareGuide {
  377. guard let callBack = self.finishHandle else {
  378. return
  379. }
  380. callBack(self, .windowNewFinish)
  381. if actionType == .getIt {
  382. self.closeAction()
  383. }
  384. }
  385. }
  386. self.coverView.addSubview(self.openFileFunctionView)
  387. self.openFileFunctionView.updateViewColor()
  388. } else if self.type == .measureGuide {
  389. KMGuideInfoWindowController.setDidShowFor(.measureGuide)
  390. if self.openFileFunctionView == nil {
  391. self.openFileFunctionView = KMOpenFileFunctionView.createFromNib()
  392. }
  393. self.openFileFunctionView.circleRect = self.digitalBoxRect
  394. self.openFileFunctionView.frame = self.coverView.bounds
  395. self.openFileFunctionView.autoresizingMask = [.width, .height]
  396. self.openFileFunctionView.type = self.type
  397. self.openFileFunctionView.clickHandle = {[weak self] view, actionType in
  398. guard let self = self else { return }
  399. // guard let callBack = self.finishHandle else {
  400. // return
  401. // }
  402. // callBack(self, .windowDigitalFinish)
  403. if actionType == .getIt {
  404. self.closeAction()
  405. }
  406. }
  407. self.coverView.addSubview(self.openFileFunctionView)
  408. self.openFileFunctionView.updateViewColor()
  409. } else if self.type == .convertGuide {
  410. if self.convertGuideView == nil {
  411. self.convertGuideView = KMConvertGuideView.createFromNib()
  412. }
  413. self.convertGuideView.frame = self.coverView.bounds
  414. self.convertGuideView.autoresizingMask = [.width, .height]
  415. self.convertGuideView.clickHandle = {[weak self] view, actionType in
  416. guard let self = self else { return }
  417. if actionType == .skip {
  418. self.closeAction()
  419. } else if actionType == .purchase {
  420. guard let callBack = self.purchaseHandle else {
  421. return
  422. }
  423. callBack(self)
  424. self.closeAction()
  425. }
  426. }
  427. self.convertGuideView.circleRect = self.digitalBoxRect
  428. self.coverView.addSubview(self.convertGuideView)
  429. self.convertGuideView.updateViewColor()
  430. KMGuideInfoWindowController.setDidShowFor(.convertGuide)//记录转档介绍
  431. } else if self.type == .customColor {
  432. if self.customColorGuideView == nil {
  433. self.customColorGuideView = KMCustomColorGuideView.createFromNib()
  434. }
  435. self.customColorGuideView.frame = self.coverView.bounds
  436. self.customColorGuideView.autoresizingMask = [.width, .height]
  437. self.customColorGuideView.clickHandle = {[weak self] view, actionType in
  438. if actionType == .getIt {
  439. self?.closeAction()
  440. }
  441. }
  442. self.customColorGuideView.circleRect = self.digitalBoxRect
  443. self.customColorGuideView.updateViewColor()
  444. self.coverView.addSubview(self.customColorGuideView)
  445. KMGuideInfoWindowController.setDidShowFor(.customColor)//记录自定义颜色介绍
  446. } else if self.type == .aiTipIconInfo {
  447. if self.aiIconGuideView == nil {
  448. self.aiIconGuideView = KMAIIconGuideView.createFromNib()
  449. }
  450. self.aiIconGuideView.frame = self.coverView.bounds
  451. self.aiIconGuideView.autoresizingMask = [.width, .height]
  452. self.aiIconGuideView.clickHandle = {[weak self] view, actionType in
  453. guard let self = self else { return }
  454. if actionType == .getIt {
  455. self.closeAction()
  456. }
  457. }
  458. self.aiIconGuideView.updateViewColor()
  459. self.aiIconGuideView.wantsLayer = true
  460. self.coverView.addSubview(self.aiIconGuideView)
  461. KMGuideInfoWindowController.setDidShowFor(.aiTipIconInfo)//记录自定义颜色介绍
  462. } else if self.type == .editPDFPopWindow {
  463. self.editPDFPopGuideView = KMEditPDFPopGuideView(contentTop: self.editPDFPopWindowFlag)
  464. self.editPDFPopGuideView?.frame.origin = self.digitalBoxRect.origin
  465. // self.editPDFPopGuideView?.autoresizingMask = [.width, .height]
  466. self.editPDFPopGuideView?.itemClick = { [weak self] idx, _ in
  467. self?.closeAction()
  468. if idx == 0 { // 去设置
  469. self?.settingCallback?()
  470. }
  471. }
  472. // self.aiIconGuideView.updateViewColor()
  473. // self.aiIconGuideView.wantsLayer = true
  474. self.coverView.addSubview(self.editPDFPopGuideView!)
  475. KMGuideInfoWindowController.setDidShowFor(.editPDFPopWindow)//记录
  476. }
  477. }
  478. }
  479. func closeAction() {
  480. self.clearViews()
  481. self.close()
  482. }
  483. }
  484. extension KMGuideInfoWindowController: KMInterfaceThemeChangedProtocol {
  485. func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  486. self.editPDFPopGuideView?.interfaceThemeDidChanged(appearance)
  487. }
  488. }