KMToolbarItemView.swift 22 KB

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