KMToolbarItemView.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. if let data = self.customizeView {
  80. self.subviewsdEnable(view: data, isEnable: !self.unEnabled)
  81. }
  82. }
  83. }
  84. var isShowCustomToolTip = false {
  85. didSet {
  86. if (self.isShowCustomToolTip) {
  87. self.clickButton.toolTip = ""
  88. }
  89. }
  90. }
  91. var boxImagePosition: NSControl.ImagePosition = .imageLeft {
  92. didSet {
  93. self._layoutView()
  94. self.itemWidth = self._calculateWidth()
  95. }
  96. }
  97. var image: NSImage? {
  98. didSet {
  99. self.imageViewBtn.image = self.image
  100. }
  101. }
  102. var selectedImage: NSImage?
  103. var alternateImage: NSImage?
  104. var titleName: String? {
  105. didSet {
  106. self.nameBtn.title = self.titleName ?? " "
  107. self.nameBtn.setTitleColor(color: Self.fetchTextNormalColor())
  108. self.itemWidth = self._calculateWidth()
  109. }
  110. }
  111. weak var target: AnyObject? {
  112. didSet {
  113. self.clickButton.target = self.target
  114. }
  115. }
  116. var btnAction: Selector? {
  117. didSet {
  118. self.clickButton.action = self.btnAction
  119. }
  120. }
  121. var needExpandAction = false {
  122. didSet {
  123. self.itemWidth = self._calculateWidth()
  124. }
  125. }
  126. var btnTag = 0 {
  127. didSet {
  128. self.clickButton.tag = self.btnTag
  129. }
  130. }
  131. var customizeView: NSView? {
  132. didSet {
  133. self._layoutView()
  134. self.itemWidth = self._calculateWidth()
  135. }
  136. }
  137. // var isShowPrompt: Bool {
  138. //
  139. // }
  140. private var promptView_: NSView?
  141. var promptView: NSView? {
  142. get {
  143. return self.promptView_
  144. }
  145. }
  146. private var promptIdentifier_: String?
  147. var promptIdentifier: String? {
  148. get {
  149. return self.promptIdentifier_
  150. }
  151. set {
  152. if self.promptIdentifier_ != newValue {
  153. self.promptIdentifier_ = newValue
  154. if let data = KMDataManager.ud_object(forKey: newValue ?? "") as? Bool {
  155. self.isShowPrompt = !data
  156. } else {
  157. self.isShowPrompt = true
  158. }
  159. }
  160. }
  161. }
  162. private var isShowPrompt_: Bool = false
  163. var isShowPrompt: Bool {
  164. get {
  165. return self.isShowPrompt_
  166. }
  167. set {
  168. self.isShowPrompt_ = newValue
  169. self.promptView?.isHidden = !newValue
  170. }
  171. }
  172. var normalBackgroundColor: NSColor = .clear
  173. var selectedBackgroundColor: NSColor = KMAppearance.Status.selColor()
  174. var selectBackgroundType: KMToolbarItemViewSelectBackgroundType = .none
  175. var itemWidth: CGFloat = 0
  176. var itemHeight: CGFloat = 0
  177. var isPopToolTip = false
  178. private var area_: NSTrackingArea?
  179. static let kDividerWidth: CGFloat = 8
  180. static let kHSpace: CGFloat = 4
  181. static let kExpandWidth: CGFloat = 8
  182. static let kRightMargin: CGFloat = 4
  183. static let kImageAboveMinWidth: CGFloat = 32
  184. lazy var imageViewBox: NSBox = {
  185. let view = NSBox()
  186. view.borderWidth = 0
  187. view.contentViewMargins = NSSize.zero
  188. view.boxType = .custom
  189. view.borderColor = .clear
  190. view.cornerRadius = 7.0
  191. return view
  192. }()
  193. lazy var imageViewBtn: NSButton = {
  194. let view = NSButton()
  195. view.bezelStyle = .regularSquare
  196. view.isBordered = false
  197. view.imagePosition = .imageOnly
  198. return view
  199. }()
  200. var nameBtn: NSButton = {
  201. let view = NSButton()
  202. view.bezelStyle = .regularSquare
  203. view.isBordered = false
  204. view.imagePosition = .imageOnly
  205. view.title = ""
  206. return view
  207. }()
  208. private var needExpandButton: NSButton = {
  209. let view = NSButton()
  210. view.bezelStyle = .regularSquare
  211. view.isBordered = false
  212. view.imagePosition = .imageOnly
  213. view.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  214. return view
  215. }()
  216. private var _popOver: NSPopover?
  217. var popOver: NSPopover? {
  218. get {
  219. return self._popOver
  220. }
  221. set {
  222. if self._popOver == nil || self._popOver!.isEqual(to: newValue) == false {
  223. self._popOver = newValue
  224. if (newValue != nil) {
  225. self.layer?.backgroundColor = Self.selectedBackgroundColor.cgColor
  226. } else {
  227. self._updateSelectBackground()
  228. }
  229. }
  230. }
  231. }
  232. private var _menuViewController: KMCustomButtonPopMenuViewController?
  233. private var _kNormalImage: NSImage?
  234. private var _originalHelpTip: String?
  235. deinit {
  236. if (self.area_ != nil) {
  237. self.removeTrackingArea(self.area_!)
  238. self.area_ = nil
  239. }
  240. NotificationCenter.default.removeObserver(self)
  241. }
  242. class var textFont: NSFont {
  243. get {
  244. .systemFont(ofSize: 12)
  245. }
  246. }
  247. // class var textNormalColor: NSColor {
  248. // get {
  249. // KMAppearance.titleColor()
  250. // }
  251. // }
  252. class func fetchTextNormalColor() -> NSColor {
  253. return KMAppearance.titleColor()
  254. }
  255. class func fetchTextSelectedColor() -> NSColor {
  256. return KMAppearance.titleColor()
  257. }
  258. class var selectedBackgroundColor: NSColor {
  259. get {
  260. return KMAppearance.Status.selColor()
  261. }
  262. }
  263. class var normalBackgroundColor: NSColor {
  264. get {
  265. return .clear
  266. }
  267. }
  268. convenience init(itemIdentifier: String) {
  269. self.init()
  270. self._itemIdentifier = itemIdentifier
  271. self.boxImagePosition = .imageLeft
  272. self.wantsLayer = true
  273. self.layer?.cornerRadius = 5
  274. self.layer?.masksToBounds = true
  275. self.nameBtn.font = Self.textFont
  276. let promptV = NSView()
  277. self.promptView_ = promptV
  278. self.addSubview(promptV)
  279. promptV.wantsLayer = true
  280. promptV.layer?.cornerRadius = 3
  281. promptV.layer?.backgroundColor = NSColor(red: 1, green: 56/255.0, blue: 25/255.0, alpha: 1).cgColor
  282. self.isShowPrompt = false
  283. self._addTrackingArea()
  284. NotificationCenter.default.addObserver(self, selector: #selector(_windowClosedPop), name: KMToolbarItemView.popOverClosedNotificationName, object: nil)
  285. }
  286. convenience init(itemIdentifier: String, postition imagePositionImagePosition: NSControl.ImagePosition, withPopMenu popMenuViewController: KMCustomButtonPopMenuViewController?) {
  287. self.init()
  288. self.boxImagePosition = imagePositionImagePosition
  289. self._menuViewController = popMenuViewController
  290. self._itemIdentifier = itemIdentifier
  291. self.wantsLayer = true
  292. self.layer?.cornerRadius = 4
  293. self.layer?.masksToBounds = true
  294. self.nameBtn.font = Self.textFont
  295. let promptV = NSView()
  296. self.promptView_ = promptV
  297. self.addSubview(promptV)
  298. promptV.wantsLayer = true
  299. promptV.layer?.cornerRadius = 3
  300. promptV.layer?.backgroundColor = NSColor(red: 1, green: 56/255.0, blue: 25/255.0, alpha: 1).cgColor
  301. self.isShowPrompt = false
  302. self._addTrackingArea()
  303. self._layoutView()
  304. if (popMenuViewController != nil) {
  305. NotificationCenter.default.addObserver(self, selector: #selector(_windowClosedPop), name: KMToolbarItemView.popOverClosedNotificationName, object: nil)
  306. }
  307. }
  308. override func draw(_ dirtyRect: NSRect) {
  309. if (self.itemIdentifier == KMToolbarDividerItemIdentifier) {
  310. let context = NSGraphicsContext.current?.cgContext
  311. KMContextSaveGState(context)
  312. KMContextTranslateCTM(context, CGRectGetWidth(dirtyRect)/2.0, CGRectGetHeight(dirtyRect)/2.0-10)
  313. KMContextMoveToPoint(context, 0, 0)
  314. KMContextAddLineToPoint(context, 0, 20)
  315. KMContextSetStrokeColorWithColor(context, NSColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor)
  316. KMContextStrokePath(context)
  317. KMContextRestoreGState(context)
  318. }
  319. }
  320. override var toolTip: String? {
  321. get {
  322. return self._originalHelpTip
  323. }
  324. set {
  325. self.clickButton.toolTip = newValue ?? ""
  326. self._originalHelpTip = self.clickButton.toolTip
  327. if(self.isShowCustomToolTip) {
  328. self.clickButton.toolTip = ""
  329. }
  330. }
  331. }
  332. override func updateTrackingAreas() {
  333. super.updateTrackingAreas()
  334. let areaBound = NSRect(x: 0, y: 5, width: self.bounds.size.width, height: self.bounds.size.height)
  335. if let _area = self.area_, _area.rect.isEmpty == false {
  336. if (_area.rect.equalTo(areaBound)) {
  337. return
  338. }
  339. }
  340. if (self.area_ != nil) {
  341. self.removeTrackingArea(self.area_!)
  342. self.area_ = nil
  343. }
  344. // inVisibleRect activeInKeyWindow
  345. self.area_ = NSTrackingArea(rect: areaBound, options: [.mouseEnteredAndExited,.mouseMoved, .activeAlways], owner: self)
  346. self.addTrackingArea(self.area_!)
  347. }
  348. override func mouseEntered(with event: NSEvent) {
  349. super.mouseEntered(with: event)
  350. guard let _window = self.window else {
  351. return
  352. }
  353. if (!_window.isKeyWindow) {
  354. return
  355. }
  356. if (self.itemIdentifier == KMToolbarDividerItemIdentifier || self.customizeView != nil || self.image == nil) {
  357. return
  358. }
  359. if self.unEnabled {
  360. return
  361. }
  362. if (!self.isSelected) {
  363. if self.selectBackgroundType == .none {
  364. self.layer?.backgroundColor = Self.selectedBackgroundColor.cgColor
  365. } else {
  366. self.imageViewBox.fillColor = Self.selectedBackgroundColor
  367. }
  368. if(self.image != nil && self.alternateImage != nil) {
  369. self._kNormalImage = self.image
  370. self.imageViewBtn.image = self.alternateImage
  371. if(self.nameBtn.superview != nil) {
  372. self.nameBtn.setTitleColor(color: Self.fetchTextNormalColor())
  373. }
  374. }
  375. }
  376. if self.isPopToolTip {
  377. self._showPopTip(self)
  378. }else {
  379. if (self.needExpandAction) {
  380. // KMImageNameUXIconBtnTriDownSel
  381. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  382. self._showPop(self)
  383. }else if (self.isShowCustomToolTip) {
  384. self.perform(#selector(_showHUDHint), with: nil, afterDelay: 0.1)
  385. }
  386. }
  387. }
  388. override func mouseExited(with event: NSEvent) {
  389. super.mouseExited(with: event)
  390. if (!self.isSelected && !self.needExpandAction) {
  391. if self.selectBackgroundType == .none {
  392. self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
  393. } else {
  394. self.imageViewBox.fillColor = self.normalBackgroundColor
  395. }
  396. if(self.image != nil && self.alternateImage != nil) {
  397. self.imageViewBtn.image = self._kNormalImage ?? self.image!
  398. }
  399. }
  400. if(self.needExpandAction && !self.isSelected) {
  401. if self.selectBackgroundType == .none {
  402. self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
  403. } else {
  404. self.imageViewBox.fillColor = self.normalBackgroundColor
  405. }
  406. if(self.image != nil && self.alternateImage != nil) {
  407. self.imageViewBtn.image = self._kNormalImage ?? self.image!
  408. }
  409. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  410. }
  411. guard let _window = self.window else {
  412. return
  413. }
  414. if (!_window.isKeyWindow) {
  415. return
  416. }
  417. if(self.nameBtn.superview != nil && !self.isSelected) {
  418. self.nameBtn.setTitleColor(color: Self.fetchTextNormalColor())
  419. }
  420. if self.isPopToolTip {
  421. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showHUDHint), object: nil)
  422. self._closePop()
  423. }else{
  424. if (self.isShowCustomToolTip && !self.needExpandAction) {
  425. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(_showHUDHint), object: nil)
  426. self._closePop()
  427. }
  428. }
  429. }
  430. public func calculateWidth() -> CGFloat {
  431. let iWidth = self._calculateWidth()
  432. return iWidth
  433. }
  434. private func _updateSelectBackground() {
  435. if self.selectBackgroundType == .none {
  436. if self.isSelected {
  437. self.layer?.backgroundColor = Self.selectedBackgroundColor.cgColor
  438. } else {
  439. self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
  440. }
  441. } else if self.selectBackgroundType == .imageBox {
  442. if self.isSelected {
  443. self.imageViewBox.fillColor = Self.selectedBackgroundColor
  444. } else {
  445. self.imageViewBox.fillColor = self.normalBackgroundColor
  446. self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
  447. }
  448. }
  449. }
  450. override func updateLayer() {
  451. super.updateLayer()
  452. //
  453. // self.wantsLayer = true
  454. // self.layer?.backgroundColor = KMAppearance.Layout.l_2Color().cgColor
  455. }
  456. }
  457. // MARK: - Private Methods
  458. extension KMToolbarItemView {
  459. private func _addTrackingArea() {
  460. // let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeInKeyWindow], owner: self)
  461. // self.addTrackingArea(trackingArea)
  462. }
  463. private func _showPop(_ sender: NSView) {
  464. if (self._popOver != nil) {
  465. return
  466. }
  467. let menuViewController = KMCustomButtonPopMenuViewController()
  468. menuViewController.delegate = self
  469. menuViewController.dataSources = self
  470. self.popOver = NSPopover()
  471. self.popOver?.delegate = self
  472. self.popOver?.contentViewController = menuViewController
  473. self.popOver?.animates = false
  474. self.popOver?.behavior = .semitransient
  475. self.popOver?.contentSize = menuViewController.view.frame.size
  476. var sourcesRect = sender.bounds
  477. sourcesRect = sender.convert(sourcesRect, to: nil)
  478. sourcesRect.origin.y -= 20
  479. sourcesRect.size.height += 20
  480. self.window?.popover = self.popOver
  481. self.window?.sourcesRect = sourcesRect
  482. self.popOver?.show(relativeTo: CGRectInset(self.bounds, 0, 5), of: self, preferredEdge: .minY)
  483. }
  484. func _showPopTip(_ sender: NSView) {
  485. if (self._popOver != nil) {
  486. return
  487. }
  488. var sourcesRect = sender.bounds
  489. sourcesRect = sender.convert(sourcesRect, to: nil)
  490. sourcesRect.origin.y -= 20
  491. sourcesRect.size.height += 20
  492. // self.window?.popover = self.popOver
  493. // self.window?.sourcesRect = sourcesRect
  494. let popViewController = KMToolbarItemPopViewController()
  495. self.popOver = NSPopover()
  496. self.popOver?.contentViewController = popViewController
  497. self.popOver?.animates = false
  498. self.popOver?.behavior = .semitransient
  499. self.popOver?.contentSize = popViewController.view.frame.size
  500. popViewController.updateWithHelpTip(helpTip: self.toolTip ?? "")
  501. // self.window?.popover = self.popOver
  502. // self.window?.sourcesRect = sourcesRect
  503. self.popOver?.show(relativeTo: CGRectInset(self.bounds, 0, 5), of: self, preferredEdge: .minY)
  504. }
  505. private func _closePop() {
  506. self.popOver?.close()
  507. self.popOver = nil
  508. }
  509. @objc private func _showHUDHint() {
  510. // KMToolbarItemPopViewController *popViewController = [[[KMToolbarItemPopViewController alloc] init] autorelease];
  511. // self.popOver = [[[NSPopover alloc] init] autorelease];
  512. // self.popOver.contentViewController = popViewController;
  513. // self.popOver.animates = NO;
  514. // self.popOver.behavior = NSPopoverBehaviorSemitransient;
  515. // self.popOver.backgroundColor = [KMAppearance KMBluegrey01Color];
  516. //
  517. // self.popOver.contentSize = popViewController.view.frame.size;
  518. // [popViewController updateWithHelpTip:self.originalHelpTip];
  519. // [self.popOver showRelativeToRect:self.bounds ofView:self preferredEdge:NSRectEdgeMinY];
  520. }
  521. @objc private func _windowClosedPop(sender: Notification) {
  522. if let data = self._popOver?.isEqual(to: sender.object), data {
  523. self.popOver = nil
  524. }
  525. }
  526. private func _layoutView() {
  527. if self.nameBtn.superview != nil {
  528. self.nameBtn.removeFromSuperview()
  529. }
  530. if self.imageViewBox.superview != nil {
  531. self.imageViewBox.removeFromSuperview()
  532. }
  533. if self.imageViewBtn.superview != nil {
  534. self.imageViewBtn.removeFromSuperview()
  535. }
  536. if let view = self.promptView, view.superview != nil {
  537. view.removeFromSuperview()
  538. }
  539. if let view = self.customizeView {
  540. if view.superview != nil {
  541. view.removeFromSuperview()
  542. }
  543. let iWidth = NSWidth(view.bounds)
  544. self.addSubview(view)
  545. view.km_add_leading_constraint()
  546. view.km_add_trailing_constraint()
  547. view.km_add_centerY_constraint()
  548. view.km_add_width_constraint(constant: iWidth)
  549. view.km_add_height_constraint(constant: NSHeight(view.bounds))
  550. self.itemHeight = NSHeight(view.bounds)
  551. return
  552. } else if (self.itemIdentifier == KMToolbarDividerItemIdentifier) {
  553. self.addSubview(self.imageViewBox)
  554. self.imageViewBox.km_add_inset_constraint(inset: NSEdgeInsetsZero)
  555. self.imageViewBox.km_add_width_constraint(constant: Self.kDividerWidth)
  556. self.itemHeight = 40
  557. return
  558. }
  559. let offset = Self.kRightMargin
  560. let offsetY: CGFloat = 2.0
  561. let offsetX = Self.kHSpace
  562. if self.boxImagePosition == .imageOnly {
  563. let iWidth = (self.imageViewBtn.image?.size ?? .zero).width + offsetX * 2
  564. self.addSubview(self.imageViewBox)
  565. self.imageViewBox.km_add_inset_constraint()
  566. self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
  567. self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: 0, bottom: offsetY, right: 0))
  568. self.itemHeight = 24
  569. } else if (self.boxImagePosition == .imageLeft) {
  570. self.addSubview(self.imageViewBox)
  571. self.imageViewBox.km_add_leading_constraint()
  572. self.imageViewBox.km_add_top_constraint()
  573. self.imageViewBox.km_add_bottom_constraint()
  574. self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
  575. self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: 2*offsetX-2, bottom: offsetY, right: 2))
  576. self.addSubview(self.nameBtn)
  577. self.nameBtn.km_add_centerY_constraint()
  578. self.nameBtn.km_add_leading_constraint(equalTo: self.imageViewBox, attribute: .trailing)
  579. if (self.needExpandAction) {
  580. self.nameBtn.km_add_right_constraint(constant: -2*offsetX-Self.kExpandWidth)
  581. } else {
  582. self.nameBtn.km_add_right_constraint(constant: -2*offsetX)
  583. }
  584. if(self.needExpandAction) {
  585. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  586. self.addSubview(self.needExpandButton)
  587. self.needExpandButton.km_add_centerY_constraint()
  588. self.needExpandButton.km_add_width_constraint(constant: Self.kExpandWidth)
  589. self.needExpandButton.km_add_right_constraint(constant: -offset)
  590. }
  591. self.layer?.cornerRadius = 6
  592. self.itemHeight = 24
  593. } else if (self.boxImagePosition == .imageExpandLeft) {
  594. self.addSubview(self.imageViewBox)
  595. self.imageViewBox.km_add_leading_constraint()
  596. self.imageViewBox.km_add_top_constraint()
  597. self.imageViewBox.km_add_bottom_constraint()
  598. self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
  599. self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: offsetX, bottom: offsetY, right: 0))
  600. self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
  601. self.addSubview(self.needExpandButton)
  602. self.needExpandButton.km_add_centerY_constraint()
  603. self.needExpandButton.km_add_width_constraint(constant: Self.kExpandWidth)
  604. self.needExpandButton.km_add_right_constraint(constant: -offset)
  605. self.addSubview(self.nameBtn)
  606. self.nameBtn.km_add_centerY_constraint()
  607. self.nameBtn.km_add_leading_constraint(equalTo: self.imageViewBox, attribute: .trailing)
  608. self.nameBtn.km_add_trailing_constraint(equalTo: self.needExpandButton, attribute: .leading)
  609. self.itemHeight = 24
  610. } else if (self.boxImagePosition == .imageAbove) {
  611. self.addSubview(self.nameBtn)
  612. self.nameBtn.alignment = .center
  613. self.nameBtn.mas_makeConstraints { make in
  614. make?.left.right().equalTo()(0)
  615. make?.width.greaterThanOrEqualTo()(Self.kImageAboveMinWidth)
  616. make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
  617. }
  618. // self.nameBtn.km_add_leading_constraint()
  619. // self.nameBtn.km_add_trailing_constraint()
  620. // self.nameBtn.km_add_bottom_constraint()
  621. self.addSubview(self.imageViewBox)
  622. self.imageViewBox.km_add_top_constraint()
  623. self.imageViewBox.km_add_width_constraint(constant: Self.kImageAboveMinWidth)
  624. self.imageViewBox.km_add_centerX_constraint()
  625. self.imageViewBox.km_add_bottom_constraint(equalTo: self.nameBtn, attribute: .top, constant: 0)
  626. self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
  627. self.imageViewBtn.km_add_inset_constraint(inset: .init(top: 0.5, left: offset, bottom: 0, right: offset))
  628. self.itemHeight = 40
  629. }
  630. self.imageViewBox.borderWidth = 1.0
  631. self.addSubview(self.clickButton)
  632. self.clickButton.km_add_inset_constraint()
  633. if let view = self.promptView {
  634. self.addSubview(view)
  635. }
  636. }
  637. private func _calculateWidth() -> CGFloat {
  638. if let v = self.customizeView {
  639. return NSWidth(v.bounds)
  640. } else if self.itemIdentifier == KMToolbarDividerItemIdentifier {
  641. return Self.kDividerWidth
  642. } else {
  643. if self.boxImagePosition == .imageOnly {
  644. return (self.imageViewBtn.image?.size ?? .zero).width + Self.kHSpace * 2
  645. } else if self.boxImagePosition == .imageLeft {
  646. // Compress 98 = 28(4+20+4) + 62 + 8(4+4)
  647. var iWidth = (self.imageViewBtn.image?.size.width ?? 0) + Self.kHSpace * 2
  648. self.nameBtn.sizeToFit()
  649. iWidth += self.nameBtn.frame.size.width
  650. iWidth += (2 * Self.kHSpace)
  651. if self.needExpandAction {
  652. iWidth += 8
  653. }
  654. return iWidth
  655. } else if self.boxImagePosition == .imageExpandLeft {
  656. // Security 87
  657. var iWidth = (self.imageViewBtn.image?.size.width ?? 0) + Self.kHSpace
  658. self.nameBtn.sizeToFit()
  659. iWidth += self.nameBtn.frame.size.width
  660. iWidth += Self.kExpandWidth
  661. iWidth += Self.kRightMargin
  662. return iWidth
  663. } else if self.boxImagePosition == .imageAbove {
  664. // Panel 33
  665. self.nameBtn.sizeToFit()
  666. let iWidth = NSWidth(self.nameBtn.bounds)
  667. return max(Self.kImageAboveMinWidth, iWidth)
  668. }
  669. }
  670. return 0
  671. }
  672. override func layout() {
  673. super.layout()
  674. if let view = self.promptView, view.superview != nil {
  675. let wh: CGFloat = 6
  676. let y: CGFloat = 3
  677. view.frame = NSMakeRect(NSWidth(self.bounds)-wh, NSHeight(self.bounds)-wh-y, wh, wh)
  678. }
  679. }
  680. }
  681. extension KMToolbarItemView: NSPopoverDelegate {
  682. func popoverDidClose(_ notification: Notification) {
  683. if let data = self.popOver?.isEqual(to: notification.object), data {
  684. self._closePop()
  685. }
  686. }
  687. }
  688. extension KMToolbarItemView: KMCustomButtonPopMenuViewControllerDelegate, KMCustomButtonPopMenuViewControllerDataSources {
  689. func customViewButtonPopDidSelectIndex(_ index: Int) {
  690. self._closePop()
  691. // if (self.itemIdentifier == KMToolbarPageEditPageRangeItemIdentifier) {
  692. if let items = self.menuFormRepresentation?.submenu?.items {
  693. let item = items[index]
  694. _ = item.target?.perform(item.action, with: item)
  695. }
  696. // }
  697. }
  698. func numberOfLine() -> Int {
  699. // if (self.itemIdentifier == KMToolbarPageEditPageRangeItemIdentifier) {
  700. if let items = self.menuFormRepresentation?.submenu?.items {
  701. return items.count
  702. }
  703. // }
  704. return 0
  705. }
  706. func stringForLine(at index: Int) -> String? {
  707. // if (self.itemIdentifier == KMToolbarPageEditPageRangeItemIdentifier) {
  708. if let items = self.menuFormRepresentation?.submenu?.items {
  709. return items[index].title
  710. }
  711. // }
  712. return nil
  713. }
  714. func needInsertSeperateLine(at index: Int) -> Bool {
  715. if let items = self.menuFormRepresentation?.submenu?.items {
  716. return items[index].isSeparatorItem
  717. }
  718. return false
  719. }
  720. func needHightLightLine(at index: Int) -> Bool {
  721. return false
  722. }
  723. func imageForLine(at index: Int) -> NSImage? {
  724. if let items = self.menuFormRepresentation?.submenu?.items {
  725. return items[index].image
  726. }
  727. return nil
  728. }
  729. func itemEnable(at index: Int) -> Bool {
  730. if self.itemIdentifier == KMToolbarToolFormAlignIdentifier {
  731. if index <= 6 {
  732. if let cnt = self.pdfView?.activeAnnotations.count, cnt >= 2 {
  733. return true
  734. }
  735. } else {
  736. if let cnt = self.pdfView?.activeAnnotations.count, cnt >= 3 {
  737. return true
  738. }
  739. }
  740. return false
  741. }
  742. return true
  743. }
  744. }
  745. extension KMToolbarItemView {
  746. func subviewsdEnable(view: NSView, isEnable: Bool) {
  747. if let data = view as? NSButton {
  748. data.isEnabled = isEnable
  749. }
  750. if let data = (view as? NSTextField) {
  751. data.isEnabled = isEnable
  752. }
  753. for subview in view.subviews {
  754. if subview.subviews.count != 0 {
  755. self.subviewsdEnable(view: subview, isEnable: isEnable)
  756. }
  757. if let data = subview as? NSButton {
  758. data.isEnabled = isEnable
  759. }
  760. if let data = (subview as? NSTextField) {
  761. data.isEnabled = isEnable
  762. }
  763. }
  764. }
  765. }