KMToolbarItemView.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. //
  2. // KMToolbarItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/10/24.
  6. //
  7. import Cocoa
  8. enum KMToolbarItemViewSelectBackgroundType: Int {
  9. case none = 0
  10. case imageBox
  11. }
  12. class KMToolbarClickButton: NSButton {
  13. weak var clickObject: AnyObject?
  14. }
  15. extension NSControl.ImagePosition {
  16. static let imageExpandLeft: NSControl.ImagePosition = .init(rawValue: 100) ?? .imageLeft
  17. }
  18. private let KMPopOverClosedByWindowNotificationName = "KMPopOverClosedByWindowNotification"
  19. extension KMToolbarItemView {
  20. public static let popOverClosedNotificationName = Notification.Name(KMPopOverClosedByWindowNotificationName)
  21. }
  22. @objcMembers class KMToolbarItemView: NSView {
  23. var menuFormRepresentation: NSMenuItem?
  24. private var _itemIdentifier: String?
  25. var itemIdentifier: String? {
  26. get {
  27. return self._itemIdentifier
  28. }
  29. }
  30. weak var pdfView: CPDFListView?
  31. lazy var clickButton: KMToolbarClickButton = {
  32. let view = KMToolbarClickButton()
  33. view.bezelStyle = .regularSquare
  34. view.isBordered = false
  35. view.imagePosition = .imageOnly
  36. view.clickObject = self
  37. return view
  38. }()
  39. var isSelected = false {
  40. didSet {
  41. if self.itemIdentifier != KMToolbarDividerItemIdentifier {
  42. if (isSelected) {
  43. if(self.image != nil && self.alternateImage != nil) {
  44. if let data = self.selectedImage {
  45. self.imageViewBtn.image = data
  46. } else {
  47. if let data = self.alternateImage {
  48. self.imageViewBtn.image = data
  49. }
  50. }
  51. }
  52. if (self.nameBtn.superview != nil) {
  53. self.nameBtn.setTitleColor(color: Self.fetchTextSelectedColor())
  54. }
  55. if(self.needExpandAction) {
  56. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownSel")
  57. }
  58. } else {
  59. if (self.needExpandAction) {
  60. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  61. }
  62. if let data = self.image {
  63. self.imageViewBtn.image = data
  64. }
  65. if (self.nameBtn.superview != nil) {
  66. self.nameBtn.setTitleColor(color: Self.fetchTextNormalColor())
  67. }
  68. }
  69. self.updateSelectBackground()
  70. }
  71. }
  72. }
  73. var unEnabled = false {
  74. didSet {
  75. self.clickButton.isEnabled = !self.unEnabled
  76. self.nameBtn.isEnabled = !self.unEnabled
  77. self.imageViewBtn.isEnabled = !self.unEnabled
  78. self.needExpandButton.isEnabled = !self.unEnabled
  79. }
  80. }
  81. var isShowCustomToolTip = false {
  82. didSet {
  83. if (self.isShowCustomToolTip) {
  84. self.clickButton.toolTip = ""
  85. }
  86. }
  87. }
  88. var boxImagePosition: NSControl.ImagePosition = .imageLeft {
  89. didSet {
  90. self._layoutView()
  91. self.itemWidth = self._calculateWidth()
  92. }
  93. }
  94. var image: NSImage? {
  95. didSet {
  96. self.imageViewBtn.image = self.image
  97. }
  98. }
  99. var selectedImage: NSImage?
  100. var alternateImage: NSImage?
  101. var titleName: String? {
  102. didSet {
  103. self.nameBtn.title = self.titleName ?? " "
  104. self.nameBtn.setTitleColor(color: Self.fetchTextNormalColor())
  105. self.itemWidth = self._calculateWidth()
  106. }
  107. }
  108. weak var target: AnyObject? {
  109. didSet {
  110. self.clickButton.target = self.target
  111. }
  112. }
  113. var btnAction: Selector? {
  114. didSet {
  115. self.clickButton.action = self.btnAction
  116. }
  117. }
  118. var needExpandAction = false {
  119. didSet {
  120. self.itemWidth = self._calculateWidth()
  121. }
  122. }
  123. var btnTag = 0 {
  124. didSet {
  125. self.clickButton.tag = self.btnTag
  126. }
  127. }
  128. var customizeView: NSView? {
  129. didSet {
  130. self._layoutView()
  131. self.itemWidth = self._calculateWidth()
  132. }
  133. }
  134. private var promptView_: NSView?
  135. var promptView: NSView? {
  136. get {
  137. return self.promptView_
  138. }
  139. }
  140. private var promptIdentifier_: String?
  141. var promptIdentifier: String? {
  142. get {
  143. return self.promptIdentifier_
  144. }
  145. set {
  146. if self.promptIdentifier_ != newValue {
  147. self.promptIdentifier_ = newValue
  148. if let data = KMDataManager.ud_object(forKey: newValue ?? "") as? Bool {
  149. self.isShowPrompt = !data
  150. } else {
  151. self.isShowPrompt = true
  152. }
  153. }
  154. }
  155. }
  156. private var isShowPrompt_: Bool = false
  157. var isShowPrompt: Bool {
  158. get {
  159. return self.isShowPrompt_
  160. }
  161. set {
  162. self.isShowPrompt_ = newValue
  163. self.promptView?.isHidden = !newValue
  164. }
  165. }
  166. var normalBackgroundColor: NSColor = .clear
  167. var selectedBackgroundColor: NSColor = KMAppearance.Status.selColor()
  168. var selectBackgroundType: KMToolbarItemViewSelectBackgroundType = .none
  169. var itemWidth: CGFloat = 0
  170. var itemHeight: CGFloat = 0
  171. var isPopToolTip = false
  172. private var area_: NSTrackingArea?
  173. static let kDividerWidth: CGFloat = 8
  174. static let kHSpace: CGFloat = 4
  175. static let kExpandWidth: CGFloat = 8
  176. static let kRightMargin: CGFloat = 4
  177. static let kImageAboveMinWidth: CGFloat = 32
  178. lazy var imageViewBox: NSBox = {
  179. let view = NSBox()
  180. view.borderWidth = 0
  181. view.contentViewMargins = NSSize.zero
  182. view.boxType = .custom
  183. view.borderColor = .clear
  184. view.cornerRadius = 7.0
  185. return view
  186. }()
  187. lazy var imageViewBtn: NSButton = {
  188. let view = NSButton()
  189. view.bezelStyle = .regularSquare
  190. view.isBordered = false
  191. view.imagePosition = .imageOnly
  192. return view
  193. }()
  194. var nameBtn: NSButton = {
  195. let view = NSButton()
  196. view.bezelStyle = .regularSquare
  197. view.isBordered = false
  198. view.imagePosition = .imageOnly
  199. view.title = ""
  200. return view
  201. }()
  202. var needExpandButton: NSButton = {
  203. let view = NSButton()
  204. view.bezelStyle = .regularSquare
  205. view.isBordered = false
  206. view.imagePosition = .imageOnly
  207. view.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  208. return view
  209. }()
  210. private var _kNormalImage: NSImage?
  211. private var _originalHelpTip: String?
  212. deinit {
  213. if (self.area_ != nil) {
  214. self.removeTrackingArea(self.area_!)
  215. self.area_ = nil
  216. }
  217. NotificationCenter.default.removeObserver(self)
  218. }
  219. class var textFont: NSFont {
  220. get {
  221. .systemFont(ofSize: 12)
  222. }
  223. }
  224. // class var textNormalColor: NSColor {
  225. // get {
  226. // KMAppearance.titleColor()
  227. // }
  228. // }
  229. class func fetchTextNormalColor() -> NSColor {
  230. return KMAppearance.titleColor()
  231. }
  232. class func fetchTextSelectedColor() -> NSColor {
  233. return KMAppearance.titleColor()
  234. }
  235. class var selectedBackgroundColor: NSColor {
  236. get {
  237. return KMAppearance.Status.selColor()
  238. }
  239. }
  240. class var normalBackgroundColor: NSColor {
  241. get {
  242. return .clear
  243. }
  244. }
  245. convenience init(itemIdentifier: String) {
  246. self.init()
  247. self._itemIdentifier = itemIdentifier
  248. self.boxImagePosition = .imageLeft
  249. self.wantsLayer = true
  250. self.layer?.cornerRadius = 5
  251. self.layer?.masksToBounds = true
  252. self.nameBtn.font = Self.textFont
  253. let promptV = NSView()
  254. self.promptView_ = promptV
  255. self.addSubview(promptV)
  256. promptV.wantsLayer = true
  257. promptV.layer?.cornerRadius = 3
  258. promptV.layer?.backgroundColor = NSColor(red: 1, green: 56/255.0, blue: 25/255.0, alpha: 1).cgColor
  259. self.isShowPrompt = false
  260. }
  261. override func draw(_ dirtyRect: NSRect) {
  262. if (self.itemIdentifier == KMToolbarDividerItemIdentifier) {
  263. let context = NSGraphicsContext.current?.cgContext
  264. KMContextSaveGState(context)
  265. KMContextTranslateCTM(context, NSWidth(dirtyRect)/2.0, NSHeight(dirtyRect)/2.0-10)
  266. KMContextMoveToPoint(context, 0, 0)
  267. KMContextAddLineToPoint(context, 0, 20)
  268. KMContextSetStrokeColorWithColor(context, NSColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor)
  269. KMContextStrokePath(context)
  270. KMContextRestoreGState(context)
  271. }
  272. }
  273. override var toolTip: String? {
  274. get {
  275. return self._originalHelpTip
  276. }
  277. set {
  278. self.clickButton.toolTip = newValue ?? ""
  279. self._originalHelpTip = self.clickButton.toolTip
  280. if(self.isShowCustomToolTip) {
  281. self.clickButton.toolTip = ""
  282. }
  283. }
  284. }
  285. override func updateTrackingAreas() {
  286. super.updateTrackingAreas()
  287. let areaBound = NSRect(x: 0, y: 5, width: self.bounds.size.width, height: self.bounds.size.height)
  288. if let _area = self.area_, _area.rect.isEmpty == false {
  289. if (_area.rect.equalTo(areaBound)) {
  290. return
  291. }
  292. }
  293. if (self.area_ != nil) {
  294. self.removeTrackingArea(self.area_!)
  295. self.area_ = nil
  296. }
  297. // inVisibleRect activeInKeyWindow
  298. self.area_ = NSTrackingArea(rect: areaBound, options: [.mouseEnteredAndExited,.mouseMoved, .activeAlways], owner: self)
  299. self.addTrackingArea(self.area_!)
  300. }
  301. override func mouseEntered(with event: NSEvent) {
  302. super.mouseEntered(with: event)
  303. if let data = self.window?.isKeyWindow, !data {
  304. return
  305. }
  306. if (self.itemIdentifier == KMToolbarDividerItemIdentifier || self.customizeView != nil || self.image == nil) {
  307. return
  308. }
  309. if self.unEnabled {
  310. return
  311. }
  312. if (!self.isSelected) {
  313. if self.selectBackgroundType == .none {
  314. self.layer?.backgroundColor = Self.selectedBackgroundColor.cgColor
  315. } else {
  316. self.imageViewBox.fillColor = Self.selectedBackgroundColor
  317. }
  318. if(self.image != nil && self.alternateImage != nil) {
  319. self._kNormalImage = self.image
  320. self.imageViewBtn.image = self.alternateImage
  321. if(self.nameBtn.superview != nil) {
  322. self.nameBtn.setTitleColor(color: Self.fetchTextNormalColor())
  323. }
  324. }
  325. }
  326. }
  327. override func mouseExited(with event: NSEvent) {
  328. super.mouseExited(with: event)
  329. if (!self.isSelected && !self.needExpandAction) {
  330. if self.selectBackgroundType == .none {
  331. self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
  332. } else {
  333. self.imageViewBox.fillColor = self.normalBackgroundColor
  334. }
  335. if(self.image != nil && self.alternateImage != nil) {
  336. self.imageViewBtn.image = self._kNormalImage ?? self.image!
  337. }
  338. }
  339. if(self.needExpandAction && !self.isSelected) {
  340. if self.selectBackgroundType == .none {
  341. self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
  342. } else {
  343. self.imageViewBox.fillColor = self.normalBackgroundColor
  344. }
  345. if(self.image != nil && self.alternateImage != nil) {
  346. self.imageViewBtn.image = self._kNormalImage ?? self.image!
  347. }
  348. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  349. }
  350. if let data = self.window?.isKeyWindow, !data {
  351. return
  352. }
  353. if(self.nameBtn.superview != nil && !self.isSelected) {
  354. self.nameBtn.setTitleColor(color: Self.fetchTextNormalColor())
  355. }
  356. }
  357. public func calculateWidth() -> CGFloat {
  358. let iWidth = self._calculateWidth()
  359. return iWidth
  360. }
  361. func updateSelectBackground() {
  362. if self.selectBackgroundType == .none {
  363. if self.isSelected {
  364. self.layer?.backgroundColor = Self.selectedBackgroundColor.cgColor
  365. } else {
  366. self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
  367. }
  368. } else if self.selectBackgroundType == .imageBox {
  369. if self.isSelected {
  370. self.imageViewBox.fillColor = Self.selectedBackgroundColor
  371. } else {
  372. self.imageViewBox.fillColor = self.normalBackgroundColor
  373. self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
  374. }
  375. }
  376. }
  377. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  378. super.interfaceThemeDidChanged(appearance)
  379. self.nameBtn.setTitleColor(Self.fetchTextNormalColor())
  380. }
  381. }
  382. // MARK: - Private Methods
  383. extension KMToolbarItemView {
  384. private func _layoutView() {
  385. if self.nameBtn.superview != nil {
  386. self.nameBtn.removeFromSuperview()
  387. }
  388. if self.imageViewBox.superview != nil {
  389. self.imageViewBox.removeFromSuperview()
  390. }
  391. if self.imageViewBtn.superview != nil {
  392. self.imageViewBtn.removeFromSuperview()
  393. }
  394. if let view = self.promptView, view.superview != nil {
  395. view.removeFromSuperview()
  396. }
  397. if let view = self.customizeView {
  398. if view.superview != nil {
  399. view.removeFromSuperview()
  400. }
  401. let iWidth = NSWidth(view.bounds)
  402. self.addSubview(view)
  403. view.km_add_leading_constraint()
  404. view.km_add_trailing_constraint()
  405. view.km_add_centerY_constraint()
  406. view.km_add_width_constraint(constant: iWidth)
  407. view.km_add_height_constraint(constant: NSHeight(view.bounds))
  408. self.itemHeight = NSHeight(view.bounds)
  409. return
  410. } else if (self.itemIdentifier == KMToolbarDividerItemIdentifier) {
  411. self.addSubview(self.imageViewBox)
  412. self.imageViewBox.km_add_inset_constraint(inset: NSEdgeInsetsZero)
  413. self.imageViewBox.km_add_width_constraint(constant: Self.kDividerWidth)
  414. self.itemHeight = 40
  415. return
  416. }
  417. let offset = Self.kRightMargin
  418. let offsetY: CGFloat = 2.0
  419. let offsetX = Self.kHSpace
  420. if self.boxImagePosition == .imageOnly {
  421. let iWidth = (self.imageViewBtn.image?.size ?? .zero).width + offsetX * 2
  422. self.addSubview(self.imageViewBox)
  423. self.imageViewBox.km_add_inset_constraint()
  424. self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
  425. self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: 0, bottom: offsetY, right: 0))
  426. self.itemHeight = 24
  427. } else if (self.boxImagePosition == .imageLeft) {
  428. self.addSubview(self.imageViewBox)
  429. self.imageViewBox.km_add_leading_constraint()
  430. self.imageViewBox.km_add_top_constraint()
  431. self.imageViewBox.km_add_bottom_constraint()
  432. self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
  433. self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: 2*offsetX-2, bottom: offsetY, right: 2))
  434. self.addSubview(self.nameBtn)
  435. self.nameBtn.km_add_centerY_constraint()
  436. self.nameBtn.km_add_leading_constraint(equalTo: self.imageViewBox, attribute: .trailing)
  437. if (self.needExpandAction) {
  438. self.nameBtn.km_add_right_constraint(constant: -2*offsetX-Self.kExpandWidth)
  439. } else {
  440. self.nameBtn.km_add_right_constraint(constant: -2*offsetX)
  441. }
  442. if(self.needExpandAction) {
  443. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  444. self.addSubview(self.needExpandButton)
  445. self.needExpandButton.km_add_centerY_constraint()
  446. self.needExpandButton.km_add_width_constraint(constant: Self.kExpandWidth)
  447. self.needExpandButton.km_add_right_constraint(constant: -offset)
  448. }
  449. self.layer?.cornerRadius = 6
  450. self.itemHeight = 24
  451. } else if (self.boxImagePosition == .imageExpandLeft) {
  452. self.addSubview(self.imageViewBox)
  453. self.imageViewBox.km_add_leading_constraint()
  454. self.imageViewBox.km_add_top_constraint()
  455. self.imageViewBox.km_add_bottom_constraint()
  456. self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
  457. self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: offsetX, bottom: offsetY, right: 0))
  458. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  459. self.addSubview(self.needExpandButton)
  460. self.needExpandButton.km_add_centerY_constraint()
  461. self.needExpandButton.km_add_width_constraint(constant: Self.kExpandWidth)
  462. self.needExpandButton.km_add_right_constraint(constant: -offset)
  463. self.addSubview(self.nameBtn)
  464. self.nameBtn.km_add_centerY_constraint()
  465. self.nameBtn.km_add_leading_constraint(equalTo: self.imageViewBox, attribute: .trailing)
  466. self.nameBtn.km_add_trailing_constraint(equalTo: self.needExpandButton, attribute: .leading)
  467. self.itemHeight = 24
  468. } else if (self.boxImagePosition == .imageAbove) {
  469. self.addSubview(self.nameBtn)
  470. self.nameBtn.alignment = .center
  471. self.nameBtn.mas_makeConstraints { make in
  472. make?.left.right().equalTo()(0)
  473. make?.width.greaterThanOrEqualTo()(Self.kImageAboveMinWidth)
  474. make?.height.mas_equalTo()(14)
  475. make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
  476. }
  477. // self.nameBtn.km_add_leading_constraint()
  478. // self.nameBtn.km_add_trailing_constraint()
  479. // self.nameBtn.km_add_bottom_constraint()
  480. self.addSubview(self.imageViewBox)
  481. self.imageViewBox.km_add_top_constraint()
  482. self.imageViewBox.km_add_width_constraint(constant: Self.kImageAboveMinWidth)
  483. self.imageViewBox.km_add_centerX_constraint()
  484. self.imageViewBox.km_add_bottom_constraint(equalTo: self.nameBtn, attribute: .top, constant: 0)
  485. self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
  486. self.imageViewBtn.km_add_inset_constraint(inset: .init(top: 0.5, left: offset, bottom: 0, right: offset))
  487. self.itemHeight = 40
  488. }
  489. self.imageViewBox.borderWidth = 1.0
  490. self.addSubview(self.clickButton)
  491. self.clickButton.km_add_inset_constraint()
  492. if let view = self.promptView {
  493. self.addSubview(view)
  494. }
  495. }
  496. private func _calculateWidth() -> CGFloat {
  497. if let v = self.customizeView {
  498. return NSWidth(v.bounds)
  499. } else if self.itemIdentifier == KMToolbarDividerItemIdentifier {
  500. return Self.kDividerWidth
  501. } else {
  502. if self.boxImagePosition == .imageOnly {
  503. return (self.imageViewBtn.image?.size ?? .zero).width + Self.kHSpace * 2
  504. } else if self.boxImagePosition == .imageLeft {
  505. // Compress 98 = 28(4+20+4) + 62 + 8(4+4)
  506. var iWidth = (self.imageViewBtn.image?.size.width ?? 0) + Self.kHSpace * 2
  507. self.nameBtn.sizeToFit()
  508. iWidth += self.nameBtn.frame.size.width
  509. iWidth += (2 * Self.kHSpace)
  510. if self.needExpandAction {
  511. iWidth += 8
  512. }
  513. return iWidth
  514. } else if self.boxImagePosition == .imageExpandLeft {
  515. // Security 87
  516. var iWidth = (self.imageViewBtn.image?.size.width ?? 0) + Self.kHSpace
  517. self.nameBtn.sizeToFit()
  518. iWidth += self.nameBtn.frame.size.width
  519. iWidth += Self.kExpandWidth
  520. iWidth += Self.kRightMargin
  521. return iWidth
  522. } else if self.boxImagePosition == .imageAbove {
  523. // Panel 33
  524. self.nameBtn.sizeToFit()
  525. let iWidth = NSWidth(self.nameBtn.bounds)
  526. return max(Self.kImageAboveMinWidth, iWidth)
  527. }
  528. }
  529. return 0
  530. }
  531. override func layout() {
  532. super.layout()
  533. if let view = self.promptView, view.superview != nil {
  534. let wh: CGFloat = 6
  535. let y: CGFloat = 3
  536. view.frame = NSMakeRect(NSWidth(self.bounds)-wh, NSHeight(self.bounds)-wh-y, wh, wh)
  537. }
  538. }
  539. }