Browse Source

【综合】KMToolbarItemView去除Masonry依赖的部分

tangchao 1 year ago
parent
commit
c1784a58a5

+ 0 - 64
PDF Office/PDF Master.xcodeproj/xcuserdata/kdanmobile.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -420,69 +420,5 @@
             landmarkType = "7">
          </BreakpointContent>
       </BreakpointProxy>
-      <BreakpointProxy
-         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
-         <BreakpointContent
-            uuid = "DD275567-F268-4715-837D-63AE0EA12542"
-            shouldBeEnabled = "Yes"
-            ignoreCount = "0"
-            continueAfterRunningActions = "No"
-            filePath = "PDF Master/Class/PDFWindowController/Toolbar/KMToolbarItemView.swift"
-            startingColumnNumber = "9223372036854775807"
-            endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "388"
-            endingLineNumber = "388"
-            landmarkName = "_windowClosedPop(sender:)"
-            landmarkType = "7">
-         </BreakpointContent>
-      </BreakpointProxy>
-      <BreakpointProxy
-         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
-         <BreakpointContent
-            uuid = "D5F63B48-A4E7-4EF4-AE93-7E42DEBFA4B0"
-            shouldBeEnabled = "Yes"
-            ignoreCount = "0"
-            continueAfterRunningActions = "No"
-            filePath = "PDF Master/Class/PDFWindowController/Toolbar/KMToolbarItemView.swift"
-            startingColumnNumber = "9223372036854775807"
-            endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "387"
-            endingLineNumber = "387"
-            landmarkName = "_windowClosedPop(sender:)"
-            landmarkType = "7">
-            <Locations>
-               <Location
-                  uuid = "D5F63B48-A4E7-4EF4-AE93-7E42DEBFA4B0 - e7ce9e83ae992573"
-                  shouldBeEnabled = "Yes"
-                  ignoreCount = "0"
-                  continueAfterRunningActions = "No"
-                  symbolName = "PDF_Master.KMToolbarItemView._windowClosedPop(sender: Foundation.Notification) -&gt; ()"
-                  moduleName = "PDF Master"
-                  usesParentBreakpointCondition = "Yes"
-                  urlString = "file:///Users/kdanmobile/work/tangchao/git/PDFOffice/PDF%20Office/PDF%20Master/Class/PDFWindowController/Toolbar/KMToolbarItemView.swift"
-                  startingColumnNumber = "9223372036854775807"
-                  endingColumnNumber = "9223372036854775807"
-                  startingLineNumber = "387"
-                  endingLineNumber = "387"
-                  offsetFromSymbolStart = "104">
-               </Location>
-               <Location
-                  uuid = "D5F63B48-A4E7-4EF4-AE93-7E42DEBFA4B0 - e7ce9e83ae992573"
-                  shouldBeEnabled = "Yes"
-                  ignoreCount = "0"
-                  continueAfterRunningActions = "No"
-                  symbolName = "PDF_Master.KMToolbarItemView._windowClosedPop(sender: Foundation.Notification) -&gt; ()"
-                  moduleName = "PDF Master"
-                  usesParentBreakpointCondition = "Yes"
-                  urlString = "file:///Users/kdanmobile/work/tangchao/git/PDFOffice/PDF%20Office/PDF%20Master/Class/PDFWindowController/Toolbar/KMToolbarItemView.swift"
-                  startingColumnNumber = "9223372036854775807"
-                  endingColumnNumber = "9223372036854775807"
-                  startingLineNumber = "387"
-                  endingLineNumber = "387"
-                  offsetFromSymbolStart = "844">
-               </Location>
-            </Locations>
-         </BreakpointContent>
-      </BreakpointProxy>
    </Breakpoints>
 </Bucket>

+ 41 - 18
PDF Office/PDF Master/Class/Common/Category/NSView+KMExtension.swift

@@ -117,6 +117,25 @@ extension NSView {
     }
 }
 
+// MARK: - KMExtension
+
+extension NSView {
+    func isElderView(to sv: NSView?) -> Bool {
+        guard let _sv = sv else {
+            return false
+        }
+        
+        var ev = _sv.superview
+        while let _ev = ev {
+            if _ev.isEqual(to: self) {
+                return true
+            }
+            ev = _ev.superview
+        }
+        return false
+    }
+}
+
 // MARK: - layout code
 
 extension NSView {
@@ -180,54 +199,42 @@ extension NSView {
     
     public func km_add_leading_constraint_r(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute = .leading, constant c: CGFloat = 0) -> NSLayoutConstraint? {
         let constraint = self.km_leading_constraint(equalTo: targetV, attribute: a, constant: c)
-        if let _constraint = constraint {
-            self.superview?.addConstraint(_constraint)
-        }
+        self._addConstraint(constraint, to: targetV)
         
         return constraint
     }
     
     public func km_add_trailing_constraint_r(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute = .trailing, constant c: CGFloat = 0) -> NSLayoutConstraint? {
         let constraint = self.km_trailing_constraint(equalTo: targetV, attribute: a, constant: c)
-        if let _constraint = constraint {
-            self.superview?.addConstraint(_constraint)
-        }
+        self._addConstraint(constraint, to: targetV)
         
         return constraint
     }
     
     public func km_add_left_constraint_r(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute = .left, constant c: CGFloat = 0) -> NSLayoutConstraint? {
         let constraint = self.km_left_constraint(equalTo: targetV, attribute: a, constant: c)
-        if let _constraint = constraint {
-            self.superview?.addConstraint(_constraint)
-        }
+        self._addConstraint(constraint, to: targetV)
         
         return constraint
     }
     
     public func km_add_right_constraint_r(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute = .right, constant c: CGFloat = 0) -> NSLayoutConstraint? {
         let constraint = self.km_right_constraint(equalTo: targetV, attribute: a, constant: c)
-        if let _constraint = constraint {
-            self.superview?.addConstraint(_constraint)
-        }
+        self._addConstraint(constraint, to: targetV)
         
         return constraint
     }
     
     public func km_add_top_constraint_r(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute = .top, constant c: CGFloat = 0) -> NSLayoutConstraint? {
         let constraint = self.km_top_constraint(equalTo: targetV, attribute: a, constant: c)
-        if let _constraint = constraint {
-            self.superview?.addConstraint(_constraint)
-        }
+        self._addConstraint(constraint, to: targetV)
 
         return constraint
     }
     
     public func km_add_bottom_constraint_r(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute = .bottom, constant c: CGFloat = 0) -> NSLayoutConstraint? {
         let constraint = self.km_bottom_constraint(equalTo: targetV, attribute: a, constant: c)
-        if let _constraint = constraint {
-            self.superview?.addConstraint(_constraint)
-        }
+        self._addConstraint(constraint, to: targetV)
         
         return constraint
     }
@@ -625,5 +632,21 @@ extension NSView {
         }
         return false
     }
+    
+    private func _addConstraint(_ c: NSLayoutConstraint?, to targetV: NSView?) {
+        guard let _constraint = c else {
+            Swift.debugPrint("addConstraint failure, c == nil")
+            return
+        }
+        if let _targetV = targetV {
+            if _targetV.isElderView(to: self) { /// 是否为长辈视图(父类,父类的父类......)
+                _targetV.addConstraint(_constraint)
+            } else {
+                self.superview?.addConstraint(_constraint)
+            }
+        } else {
+            self.superview?.addConstraint(_constraint)
+        }
+    }
 }
 

+ 0 - 1
PDF Office/PDF Master/Class/Common/Category/NSWindow+KMExtension.swift

@@ -43,7 +43,6 @@ extension NSWindow {
         }
         if (need) {
             if (CGRectContainsPoint(self.sourcesRect, point) == false) {
-                KMPrint("kk-point: \(point), \(self.sourcesRect)")
                 if self.popover != nil {
                     NotificationCenter.default.post(name: Notification.Name("KMPopOverClosedByWindowNotification"), object: self.popover!)
                     self.popover?.close()

+ 123 - 213
PDF Office/PDF Master/Class/PDFWindowController/Toolbar/KMToolbarItemView.swift

@@ -21,7 +21,6 @@ extension KMToolbarItemView {
 }
 
 @objcMembers class KMToolbarItemView: NSView {
-    
     var menuFormRepresentation: NSMenuItem?
     
     private var _itemIdentifier: String?
@@ -32,25 +31,18 @@ extension KMToolbarItemView {
     }
     
     lazy var clickButton: KMToolbarClickButton = {
-        let _clickButton = KMToolbarClickButton()
-        _clickButton.bezelStyle = .regularSquare
-        _clickButton.isBordered = false
-        _clickButton.imagePosition = .imageOnly
-        _clickButton.clickObject = self
-        return _clickButton
+        let view = KMToolbarClickButton()
+        view.bezelStyle = .regularSquare
+        view.isBordered = false
+        view.imagePosition = .imageOnly
+        view.clickObject = self
+        return view
     }()
     
     var isSelected = false {
         didSet {
             if self.itemIdentifier != KMToolbarDividerItemIdentifier {
                 if (isSelected) {
-                    if (isMainTool) {
-//                        self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
-                        self.imageViewBox.fillColor = self.normalBackgroundColor
-                        self.nameBtn.setTitleColor(color: KMAppearance.titleColor(), font: .SFProTextSemiboldFont(14))
-                        self.linView.isHidden = false
-                        return
-                    }
 //                    self.layer?.backgroundColor = self.selectedBackgroundColor.cgColor
                     self.imageViewBox.fillColor = self.selectedBackgroundColor
                     if(self.image != nil && self.alternateImage != nil) {
@@ -67,13 +59,6 @@ extension KMToolbarItemView {
                         self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownSel")
                     }
                 } else {
-                    if (isMainTool) {
-//                        self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
-                        self.imageViewBox.fillColor = self.normalBackgroundColor
-                        self.nameBtn.setTitleColor(color: NSColor(red: 97.0/255.0, green: 100.0/255.0, blue: 105.0/255.0, alpha: 1.0),font: .SFProTextRegularFont(14))
-                        self.linView.isHidden = true
-                        return
-                    }
 //                    self.layer?.backgroundColor = self.normalBackgroundColor.cgColor
                     self.imageViewBox.fillColor = self.normalBackgroundColor
                     if (self.needExpandAction) {
@@ -148,58 +133,40 @@ extension KMToolbarItemView {
             self._layoutView()
         }
     }
-    var isMainTool = false {
-        didSet {
-            if (isMainTool) {
-                self.nameBtn.setTitleColor(color: NSColor(red: 97.0/255.0, green: 100.0/255.0, blue: 105.0/255.0, alpha: 1), font: .SFProTextRegularFont(14))
-            } else {
-                self.nameBtn.setTitleColor(color: KMAppearance.titleColor())
-            }
-            self._layoutView()
-        }
-    }
-    
-    var linView: NSView = {
-        let _linView = NSView()
-        _linView.wantsLayer = true
-        _linView.layer?.backgroundColor = NSColor(red: 23/255.0, green: 112/255.0, blue: 244/255.0, alpha: 1).cgColor
-        _linView.layer?.cornerRadius = 2
-        return _linView
-    }()
   
     var normalBackgroundColor: NSColor = .clear
     var selectedBackgroundColor: NSColor = KMAppearance.Status.selColor()
     
-    var imageViewBox: NSBox = {
-        let _imageViewBox = NSBox()
-        _imageViewBox.borderWidth = 0
-        _imageViewBox.contentViewMargins = NSZeroSize
-        _imageViewBox.boxType = .custom
-        return _imageViewBox
+    lazy var imageViewBox: NSBox = {
+        let view = NSBox()
+        view.borderWidth = 0
+        view.contentViewMargins = NSSize.zero
+        view.boxType = .custom
+        return view
     }()
     private lazy var imageViewBtn: NSButton = {
-        let _imageViewBtn = NSButton()
-        _imageViewBtn.bezelStyle = .regularSquare
-        _imageViewBtn.isBordered = false
-        _imageViewBtn.imagePosition = .imageOnly
-        return _imageViewBtn
+        let view = NSButton()
+        view.bezelStyle = .regularSquare
+        view.isBordered = false
+        view.imagePosition = .imageOnly
+        return view
     }()
     private var nameBtn: NSButton = {
-        let _nameBtn = NSButton()
-        _nameBtn.bezelStyle = .regularSquare
-        _nameBtn.isBordered = false
-        _nameBtn.imagePosition = .imageOnly
-        _nameBtn.font = .systemFont(ofSize: 12)
-        _nameBtn.title = ""
-        return _nameBtn
+        let view = NSButton()
+        view.bezelStyle = .regularSquare
+        view.isBordered = false
+        view.imagePosition = .imageOnly
+        view.font = .systemFont(ofSize: 12)
+        view.title = ""
+        return view
     }()
     private var needExpandButton: NSButton = {
-        let _needExpandButton = NSButton()
-        _needExpandButton.bezelStyle = .regularSquare
-        _needExpandButton.isBordered = false
-        _needExpandButton.imagePosition = .imageOnly
-        _needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
-        return _needExpandButton
+        let view = NSButton()
+        view.bezelStyle = .regularSquare
+        view.isBordered = false
+        view.imagePosition = .imageOnly
+        view.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
+        return view
     }()
     private var _popOver: NSPopover?
     var popOver: NSPopover? {
@@ -240,9 +207,6 @@ extension KMToolbarItemView {
         self.layer?.cornerRadius = 5
         self.layer?.masksToBounds = true
         
-//        self.normalBackgroundColor = .clear
-//        self.selectedBackgroundColor = NSColor(red: 223.0/255.0, green: 225.0/255.0, blue: 229.0/255.0, alpha: 1)
-        
         self._addTrackingArea()
         NotificationCenter.default.addObserver(self, selector: #selector(_windowClosedPop), name: KMToolbarItemView.popOverClosedNotificationName, object: nil)
     }
@@ -257,9 +221,6 @@ extension KMToolbarItemView {
         self.layer?.cornerRadius = 4
         self.layer?.masksToBounds = true
         
-//        self.normalBackgroundColor = .clear
-//        self.selectedBackgroundColor = NSColor(red: 223.0/255.0, green: 225.0/255.0, blue: 229.0/255.0, alpha: 1)
-        
         self._addTrackingArea()
         self._layoutView()
         if (popMenuViewController != nil) {
@@ -293,13 +254,6 @@ extension KMToolbarItemView {
         }
     }
     
-    // MARK: - Private Methods
-    
-    private func _addTrackingArea() {
-        let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeInKeyWindow], owner: self)
-        self.addTrackingArea(trackingArea)
-    }
-    
     override func mouseEntered(with event: NSEvent) {
         super.mouseEntered(with: event)
         
@@ -360,7 +314,7 @@ extension KMToolbarItemView {
             return
         }
         
-        if(self.nameBtn.superview != nil && !self.isSelected && !self.isMainTool) {
+        if(self.nameBtn.superview != nil && !self.isSelected) {
             self.nameBtn.setTitleColor(color: KMAppearance.titleColor())
         }
         
@@ -369,24 +323,14 @@ extension KMToolbarItemView {
             self._closePop()
         }
     }
-    
-    @objc private func _showHUDHint() {
-        //    KMToolbarItemPopViewController *popViewController = [[[KMToolbarItemPopViewController alloc] init] autorelease];
-        //    self.popOver = [[[NSPopover alloc] init] autorelease];
-        //    self.popOver.contentViewController = popViewController;
-        //    self.popOver.animates = NO;
-        //    self.popOver.behavior = NSPopoverBehaviorSemitransient;
-        //    self.popOver.backgroundColor = [KMAppearance KMBluegrey01Color];
-        //
-        //    self.popOver.contentSize = popViewController.view.frame.size;
-        //    [popViewController updateWithHelpTip:self.originalHelpTip];
-        //    [self.popOver showRelativeToRect:self.bounds ofView:self preferredEdge:NSRectEdgeMinY];
-    }
-    
-    @objc private func _windowClosedPop(sender: Notification) {
-        if let data = self._popOver?.isEqual(to: sender.object), data {
-            self.popOver = nil
-        }
+}
+
+// MARK: - Private Methods
+
+extension KMToolbarItemView {
+    private func _addTrackingArea() {
+        let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeInKeyWindow], owner: self)
+        self.addTrackingArea(trackingArea)
     }
     
     private func _showPop(_ sender: NSView) {
@@ -419,17 +363,29 @@ extension KMToolbarItemView {
         self.popOver = nil
     }
     
+    @objc private func _showHUDHint() {
+        //    KMToolbarItemPopViewController *popViewController = [[[KMToolbarItemPopViewController alloc] init] autorelease];
+        //    self.popOver = [[[NSPopover alloc] init] autorelease];
+        //    self.popOver.contentViewController = popViewController;
+        //    self.popOver.animates = NO;
+        //    self.popOver.behavior = NSPopoverBehaviorSemitransient;
+        //    self.popOver.backgroundColor = [KMAppearance KMBluegrey01Color];
+        //
+        //    self.popOver.contentSize = popViewController.view.frame.size;
+        //    [popViewController updateWithHelpTip:self.originalHelpTip];
+        //    [self.popOver showRelativeToRect:self.bounds ofView:self preferredEdge:NSRectEdgeMinY];
+    }
+    
+    @objc private func _windowClosedPop(sender: Notification) {
+        if let data = self._popOver?.isEqual(to: sender.object), data {
+            self.popOver = nil
+        }
+    }
+    
     private func _layoutView() {
-        let offset = 4.0
-        let offsetY = 2.0
-        let offsetX = 4.0
         if self.nameBtn.superview != nil {
             self.nameBtn.removeFromSuperview()
         }
-        if self.linView.superview != nil {
-            self.linView.removeFromSuperview()
-        }
-        
         if self.imageViewBox.superview != nil {
             self.imageViewBox.removeFromSuperview()
         }
@@ -437,160 +393,114 @@ extension KMToolbarItemView {
             self.imageViewBtn.removeFromSuperview()
         }
         
-        if self.customizeView != nil {
-            if self.customizeView?.superview != nil {
-                self.customizeView?.removeFromSuperview()
+        if let view = self.customizeView {
+            if view.superview != nil {
+                view.removeFromSuperview()
             }
-            self.addSubview(self.customizeView!)
-            
-            self.customizeView?.mas_makeConstraints({ make in
+            self.addSubview(view)
+            view.mas_makeConstraints({ make in
                 make?.left.right().equalTo()(0)
                 make?.centerY.equalTo()(0)
-                make?.width.offset()(self.customizeView!.frame.size.width)
-                make?.height.offset()(self.customizeView!.frame.size.height)
+                make?.width.offset()(NSWidth(view.bounds))
+                make?.height.offset()(NSHeight(view.bounds))
             })
             return
-        }
-        if self.boxImagePosition == .imageOnly && self.itemIdentifier != KMToolbarDividerItemIdentifier {
+        } else if (self.itemIdentifier == KMToolbarDividerItemIdentifier) {
             self.addSubview(self.imageViewBox)
             self.imageViewBox.mas_makeConstraints { make in
-                make?.left.right().top().bottom().equalTo()(0)
+                make?.top.equalTo()(0)
+                make?.width.equalTo()(8)
+                make?.left.equalTo()(0)
+                make?.right.equalTo()(0)
+                make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
             }
+            return
+        }
+        
+        let offset = 4.0
+        let offsetY = 2.0
+        let offsetX = 4.0
+        if self.boxImagePosition == .imageOnly {
+            self.addSubview(self.imageViewBox)
+            self.imageViewBox.km_add_inset_constraint()
             
             self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
-            self.imageViewBtn.mas_makeConstraints { make in
-                make?.left.equalTo()(self.imageViewBox.mas_left)?.offset()(offsetX)
-                make?.top.equalTo()(self.imageViewBox.mas_top)?.offset()(offsetY)
-                make?.bottom.equalTo()(self.imageViewBox.mas_bottom)?.offset()(-offsetY)
-                make?.right.equalTo()(self.imageViewBox.mas_right)?.offset()(-offsetX)
-            }
+            self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: offsetX, bottom: offsetY, right: offsetX))
         } else if (self.boxImagePosition == .imageLeft) {
             self.addSubview(self.imageViewBox)
-            self.imageViewBox.mas_makeConstraints { make in
-                make?.left.top().bottom().equalTo()(0)
-            }
+            self.imageViewBox.km_add_leading_constraint()
+            self.imageViewBox.km_add_top_constraint()
+            self.imageViewBox.km_add_bottom_constraint()
             
             self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
-            self.imageViewBtn.mas_makeConstraints { make in
-                make?.left.equalTo()(self.imageViewBox.mas_left)?.offset()(2 * offsetX)
-                make?.top.equalTo()(self.imageViewBox.mas_top)?.offset()(offsetY)
-                make?.bottom.equalTo()(self.imageViewBox.mas_bottom)?.offset()(-offsetY)
-                make?.right.equalTo()(0)
-            }
+            self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: 2*offsetX, bottom: offsetY, right: 0))
             
             self.addSubview(self.nameBtn)
-            self.nameBtn.mas_makeConstraints { make in
-                make?.centerY.equalTo()(0)
-                make?.left.equalTo()(self.imageViewBox.mas_right)?.offset()(0)
-                if (self.needExpandAction) {
-                    make?.right.equalTo()(self.mas_right)?.offset()(-2*offsetX-8)
-                } else {
-                    make?.right.equalTo()(self.mas_right)?.offset()(-2*offsetX)
-                }
+            self.nameBtn.km_add_centerY_constraint()
+            self.nameBtn.km_add_leading_constraint(equalTo: self.imageViewBox, attribute: .trailing)
+            if (self.needExpandAction) {
+                self.nameBtn.km_add_right_constraint(constant: -2*offsetX-8)
+            } else {
+                self.nameBtn.km_add_right_constraint(constant: -2*offsetX)
             }
             
             if(self.needExpandAction) {
                 self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
                 self.addSubview(self.needExpandButton)
-                self.needExpandButton.mas_makeConstraints { make in
-                    make?.centerY.equalTo()(0)
-                    make?.width.height().equalTo()(8)
-                    make?.right.equalTo()(self.mas_right)?.offset()(-offset)
-                }
+                self.needExpandButton.km_add_centerY_constraint()
+                self.needExpandButton.km_add_width_constraint(constant: 8)
+                self.needExpandButton.km_add_right_constraint(constant: -offset)
             }
             
             self.layer?.cornerRadius = 6
         } else if (self.boxImagePosition == .imageExpandLeft) {
             self.addSubview(self.imageViewBox)
-            self.imageViewBox.mas_makeConstraints { make in
-                make?.left.top().bottom().equalTo()(0)
-            }
+            self.imageViewBox.km_add_leading_constraint()
+            self.imageViewBox.km_add_top_constraint()
+            self.imageViewBox.km_add_bottom_constraint()
 
             self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
-            self.imageViewBtn.mas_makeConstraints { make in
-                make?.left.equalTo()(self.imageViewBox.mas_left)?.offset()(offsetX)
-                make?.top.equalTo()(self.imageViewBox.mas_top)?.offset()(offsetY)
-                make?.bottom.equalTo()(self.imageViewBox.mas_bottom)?.offset()(-offsetY)
-                make?.right.equalTo()(0)
-            }
+            self.imageViewBtn.km_add_inset_constraint(equalTo: self.imageViewBox, inset: NSEdgeInsets(top: offsetY, left: offsetX, bottom: offsetY, right: 0))
 
             self.needExpandButton.image = NSImage(named: "KMImageNameUXIconBtnTriDownNor")
             self.addSubview(self.needExpandButton)
-            self.needExpandButton.mas_makeConstraints { make in
-                make?.centerY.equalTo()(0)
-                make?.width.height().equalTo()(8)
-                make?.right.equalTo()(self.mas_right)?.offset()(-offset)
-            }
+            self.needExpandButton.km_add_centerY_constraint()
+            self.needExpandButton.km_add_width_constraint(constant: 8)
+            self.needExpandButton.km_add_right_constraint(constant: -offset)
 
             self.addSubview(self.nameBtn)
-            self.nameBtn.mas_makeConstraints { make in
-                make?.centerY.equalTo()(0)
-                make?.left.equalTo()(self.imageViewBox.mas_right)?.offset()(0)
-                make?.right.equalTo()(self.needExpandButton.mas_left)?.offset()(0)
-            }
+            self.nameBtn.km_add_centerY_constraint()
+            self.nameBtn.km_add_leading_constraint(equalTo: self.imageViewBox, attribute: .trailing)
+            self.nameBtn.km_add_trailing_constraint(equalTo: self.needExpandButton, attribute: .leading)
         } else if (self.boxImagePosition == .imageAbove) {
             self.addSubview(self.nameBtn)
             self.nameBtn.alignment = .center
-            self.nameBtn.mas_makeConstraints { make in
-                make?.left.right().equalTo()(0)
-                make?.width.greaterThanOrEqualTo()(32)
-                make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
-            }
+//            self.nameBtn.mas_makeConstraints { make in
+//                make?.left.right().equalTo()(0)
+//                make?.width.greaterThanOrEqualTo()(32)
+//                make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
+//            }
+            self.nameBtn.km_add_leading_constraint()
+            self.nameBtn.km_add_trailing_constraint()
+            self.nameBtn.km_add_bottom_constraint()
             
             self.addSubview(self.imageViewBox)
-            self.imageViewBox.mas_makeConstraints { make in
-                make?.top.equalTo()(0)
-                make?.width.equalTo()(32)
-                make?.centerX.equalTo()(0)
-                make?.bottom.equalTo()(nameBtn.mas_top)?.offset()(0)
-            }
+            self.imageViewBox.km_add_top_constraint()
+            self.imageViewBox.km_add_width_constraint(constant: 32)
+            self.imageViewBox.km_add_centerX_constraint()
+            self.imageViewBox.km_add_bottom_constraint(equalTo: self.nameBtn, attribute: .top, constant: 0)
         
             self.imageViewBox.contentView?.addSubview(self.imageViewBtn)
-            self.imageViewBtn.mas_makeConstraints { make in
-                make?.left.equalTo()(offset)
-                make?.right.equalTo()(-offset)
-                make?.top.equalTo()(0)
-                make?.bottom.equalTo()(0)
-            }
-        } else {
-            if (self.itemIdentifier == KMToolbarDividerItemIdentifier) {
-                self.addSubview(self.imageViewBox)
-                self.imageViewBox.mas_makeConstraints { make in
-                    make?.top.equalTo()(0)
-                    make?.width.equalTo()(8)
-                    make?.left.equalTo()(0)
-                    make?.right.equalTo()(0)
-                    make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
-                }
-            }
-
+            self.imageViewBtn.km_add_inset_constraint(inset: .init(top: 0, left: offset, bottom: 0, right: offset))
         }
 
-        if self.nameBtn.superview != nil && self.isMainTool {
-            self.addSubview(self.linView)
-            self.linView.mas_makeConstraints { make in
-                make?.width.offset()(32)
-                make?.height.offset()(3)
-                make?.centerX.equalTo()(self.mas_centerX)
-                make?.bottom.equalTo()(self.mas_bottom)?.offset()(0)
-            }
+        self.nameBtn.font = .systemFont(ofSize: 12)
+        self.imageViewBox.borderColor = .clear
+        self.imageViewBox.borderWidth = 1.0
+        self.imageViewBox.cornerRadius = 7.0
             
-            self.nameBtn.font = .systemFont(ofSize: 14)
-            self.linView.isHidden = true
-        } else {
-            self.nameBtn.font = .systemFont(ofSize: 12)
-        }
-        
-        if self.itemIdentifier != KMToolbarDividerItemIdentifier {
-            self.imageViewBox.borderColor = .clear
-            self.imageViewBox.borderWidth = 1.0
-            self.imageViewBox.cornerRadius = 7.0
-            
-            self.addSubview(self.clickButton)
-            self.clickButton.mas_makeConstraints { make in
-                make?.left.right().top().bottom().equalTo()(0)
-            }
-        }
+        self.addSubview(self.clickButton)
+        self.clickButton.km_add_inset_constraint()
     }
 }