Переглянути джерело

【2025】【Layout】工具栏自定义完善

niehaoyu 4 місяців тому
батько
коміт
d674cc171e

+ 72 - 6
PDF Office/PDF Master/KMClass/KMPDFViewController/Toolbar/KMPDFSecToolbarController.swift

@@ -22,33 +22,94 @@ class KMPDFSecToolbarController: NSViewController {
     
     @IBOutlet var rightContendView: NSView!
     
+    @IBOutlet var moreToolsBGView: NSView!
     @IBOutlet var moreToolsView: ComponentDropdownTool!
     
-    
+    @IBOutlet var contendLeftConst: NSLayoutConstraint!
     @IBOutlet var contendViewWidthConst: NSLayoutConstraint!
     @IBOutlet var rightContendViewWidthConst: NSLayoutConstraint!
     
     private var propertys: [Any] = []
+    private var propertysWidthValue: CGFloat = 0
+    private var lastPropertyWidth: CGFloat = 0
+    private var hidePropertys: [Any] = []
     
     private var rightPropertys: [Any] = []
     
+    
     weak open var delegate: KMPDFSecToolbarControllerDelegate?
     
+    override func viewWillLayout() {
+        super.viewWillLayout()
+        
+        let contendXValue = CGRectGetWidth(view.frame)/2 - propertysWidthValue/2
+        contendLeftConst.constant = max(0, contendXValue)
+        
+        if contendXValue <= 0 {
+            contendViewWidthConst.constant = CGRectGetWidth(view.frame)
+        } else {
+            contendViewWidthConst.constant = propertysWidthValue
+        }
+        
+        hidePropertys.removeAll()
+        moreToolsBGView.isHidden = true
+        let subviews = infoContendView.subviews
+        for view in subviews {
+            if CGRectGetMidX(view.frame) > CGRectGetWidth(self.view.frame) - lastPropertyWidth {
+                moreToolsBGView.isHidden = false
+                view.isHidden = true
+                if view is ComponentButton {
+                    hidePropertys.append((view as! ComponentButton).properties)
+                } else if view is ComponentDropdownTool {
+                    hidePropertys.append((view as! ComponentDropdownTool).properties)
+                }
+            } else {
+                view.isHidden = false
+            }
+        }
+        
+        NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(setUpHidePropertys), object: nil)
+        self.perform(#selector(setUpHidePropertys), with: nil, afterDelay: 0.35)
+
+    }
+    
     override func viewDidLoad() {
         super.viewDidLoad()
         // Do view setup here.
         
-        
         setUpProperty()
+        
     }
     
     func setUpProperty() {
+        contendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
+        moreToolsBGView.wantsLayer = true
+        moreToolsBGView.layer?.backgroundColor = contendBox.fillColor.cgColor
+        moreToolsBGView.isHidden = true
+        
         moreToolsView.properties = ComponentDropdownToolProperty(state: .normal, leftIcon: NSImage(named: "toolbar_doubleArrow_right"))
+        moreToolsView.delegate = self
         
-            
     }
     
-    
+    @objc func setUpHidePropertys() {
+        
+        var menuItemArr: [ComponentMenuitemProperty] = []
+        
+        for item in hidePropertys {
+            if item is ComponentButtonProperty {
+                let menuItem = ComponentMenuitemProperty(type: .normal, text: (item as! ComponentButtonProperty).buttonText ?? "", identifier: (item as! ComponentButtonProperty).identifier)
+                menuItemArr.append(menuItem)
+            } else if item is ComponentDropdownToolProperty {
+                let menuItem = ComponentMenuitemProperty(type: .normal, text: (item as! ComponentDropdownToolProperty).text ?? "testtest", identifier: "")
+                if let subItems = (item as! ComponentDropdownToolProperty).menuItemArr {
+                    menuItem.subPropertys = subItems
+                }
+                menuItemArr.append(menuItem)
+            }
+        }
+        moreToolsView.properties.menuItemArr = menuItemArr
+    }
     
     //MARK: - reload
     public func reloadMainPropertys(_ arr: [Any]) {
@@ -93,6 +154,7 @@ class KMPDFSecToolbarController: NSViewController {
                 
                 itemXvalue += CGRectGetWidth(button.frame)
                 itemXvalue += 12
+                lastPropertyWidth = CGRectGetWidth(button.frame)
                 
             } else if property is ComponentDividerProperty {
                 let divider = ComponentDivider.init()
@@ -102,6 +164,7 @@ class KMPDFSecToolbarController: NSViewController {
                 
                 itemXvalue += 1
                 itemXvalue += 12
+                lastPropertyWidth = CGRectGetWidth(divider.frame)
                 
             } else if property is ComponentDropdownToolProperty {
                 let dropdownTool = ComponentDropdownTool.init()
@@ -112,12 +175,15 @@ class KMPDFSecToolbarController: NSViewController {
                 
                 itemXvalue += CGRectGetWidth(dropdownTool.frame)
                 itemXvalue += 12
+                lastPropertyWidth = CGRectGetWidth(dropdownTool.frame)
                 
             }
         }
         itemXvalue -= 12
         
-        contendViewWidthConst.constant = itemXvalue
+        propertysWidthValue = itemXvalue
+        contendViewWidthConst.constant = propertysWidthValue
+        contendLeftConst.constant = CGRectGetWidth(view.frame)/2 - propertysWidthValue/2
          
     }
     
@@ -181,7 +247,6 @@ class KMPDFSecToolbarController: NSViewController {
         }
     }
     
-    
     //MARK: -
     @objc func buttonClicked(_ sender: ComponentButton) {
         delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, sender.properties)
@@ -193,6 +258,7 @@ extension KMPDFSecToolbarController: ComponentDropdownToolDelegate {
     func componentDropdownToolDidClicked(_ view: ComponentDropdownTool, menuItem: ComponentMenuitemProperty?) {
         if let property = menuItem {
             property.itemSelected = false
+            property.state = .normal
             
             delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, property)
         }

+ 21 - 13
PDF Office/PDF Master/KMClass/KMPDFViewController/Toolbar/KMPDFSecToolbarController.xib

@@ -9,9 +9,11 @@
         <customObject id="-2" userLabel="File's Owner" customClass="KMPDFSecToolbarController" customModule="PDF_Reader_Pro" customModuleProvider="target">
             <connections>
                 <outlet property="contendBox" destination="P8Z-Rm-axf" id="Br4-ac-jf1"/>
+                <outlet property="contendLeftConst" destination="ctJ-67-3NM" id="Fqh-8C-a9a"/>
                 <outlet property="contendViewWidthConst" destination="rhT-gz-dCs" id="xoo-Sk-gaJ"/>
                 <outlet property="infoContendView" destination="B0p-2V-BSk" id="Rv1-tU-7jL"/>
-                <outlet property="moreToolsView" destination="lzh-oR-jCt" id="KIv-bN-k23"/>
+                <outlet property="moreToolsBGView" destination="lzh-oR-jCt" id="Hx5-DB-omQ"/>
+                <outlet property="moreToolsView" destination="zRe-Wx-6kc" id="g1s-gB-axF"/>
                 <outlet property="rightContendView" destination="PUC-EB-gWY" id="jfI-08-Gj8"/>
                 <outlet property="rightContendViewWidthConst" destination="sev-BN-gT8" id="Bk4-l1-pu4"/>
                 <outlet property="view" destination="Hz6-mo-xeY" id="0bl-1N-x8E"/>
@@ -20,51 +22,57 @@
         <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
         <customObject id="-3" userLabel="Application" customClass="NSObject"/>
         <customView id="Hz6-mo-xeY">
-            <rect key="frame" x="0.0" y="0.0" width="646" height="51"/>
+            <rect key="frame" x="0.0" y="0.0" width="460" height="51"/>
             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
             <subviews>
                 <box boxType="custom" borderWidth="0.0" title="Box" translatesAutoresizingMaskIntoConstraints="NO" id="P8Z-Rm-axf">
-                    <rect key="frame" x="0.0" y="0.0" width="646" height="51"/>
+                    <rect key="frame" x="0.0" y="0.0" width="460" height="51"/>
                     <view key="contentView" id="a2M-IY-za4">
-                        <rect key="frame" x="0.0" y="0.0" width="646" height="51"/>
+                        <rect key="frame" x="0.0" y="0.0" width="460" height="51"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                         <subviews>
                             <customView translatesAutoresizingMaskIntoConstraints="NO" id="MdP-Di-AAN" customClass="ComponentDivider" customModule="KMComponentLibrary">
-                                <rect key="frame" x="0.0" y="0.0" width="646" height="1"/>
+                                <rect key="frame" x="0.0" y="0.0" width="460" height="1"/>
                                 <constraints>
                                     <constraint firstAttribute="height" constant="1" id="XC0-ta-Kkw"/>
                                 </constraints>
                             </customView>
                             <customView translatesAutoresizingMaskIntoConstraints="NO" id="B0p-2V-BSk">
-                                <rect key="frame" x="242" y="0.0" width="163" height="51"/>
+                                <rect key="frame" x="0.0" y="0.0" width="163" height="51"/>
                                 <constraints>
                                     <constraint firstAttribute="width" constant="163" id="rhT-gz-dCs"/>
                                 </constraints>
                             </customView>
                             <customView translatesAutoresizingMaskIntoConstraints="NO" id="PUC-EB-gWY">
-                                <rect key="frame" x="562" y="12" width="68" height="28"/>
+                                <rect key="frame" x="376" y="12" width="68" height="28"/>
                                 <constraints>
                                     <constraint firstAttribute="width" constant="68" id="sev-BN-gT8"/>
                                     <constraint firstAttribute="height" constant="28" id="vcx-ZU-5K1"/>
                                 </constraints>
                             </customView>
-                            <customView translatesAutoresizingMaskIntoConstraints="NO" id="lzh-oR-jCt" customClass="ComponentDropdownTool" customModule="KMComponentLibrary">
-                                <rect key="frame" x="603" y="12" width="28" height="28"/>
+                            <customView translatesAutoresizingMaskIntoConstraints="NO" id="lzh-oR-jCt">
+                                <rect key="frame" x="417" y="12" width="43" height="28"/>
+                                <subviews>
+                                    <customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="zRe-Wx-6kc" customClass="ComponentDropdownTool" customModule="KMComponentLibrary">
+                                        <rect key="frame" x="0.0" y="0.0" width="28" height="28"/>
+                                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" heightSizable="YES"/>
+                                    </customView>
+                                </subviews>
                                 <constraints>
                                     <constraint firstAttribute="height" constant="28" id="20C-q9-3Ye"/>
-                                    <constraint firstAttribute="width" constant="28" id="Btn-VM-T25"/>
+                                    <constraint firstAttribute="width" constant="43" id="Btn-VM-T25"/>
                                 </constraints>
                             </customView>
                         </subviews>
                         <constraints>
-                            <constraint firstItem="B0p-2V-BSk" firstAttribute="centerX" secondItem="a2M-IY-za4" secondAttribute="centerX" id="E0L-Cw-Hmk"/>
                             <constraint firstAttribute="trailing" secondItem="MdP-Di-AAN" secondAttribute="trailing" id="HdE-QG-M1b"/>
                             <constraint firstItem="B0p-2V-BSk" firstAttribute="top" secondItem="a2M-IY-za4" secondAttribute="top" id="LBR-5H-FMH"/>
-                            <constraint firstAttribute="trailing" secondItem="lzh-oR-jCt" secondAttribute="trailing" constant="15" id="RXf-PE-FvV"/>
+                            <constraint firstAttribute="trailing" secondItem="lzh-oR-jCt" secondAttribute="trailing" id="RXf-PE-FvV"/>
                             <constraint firstAttribute="bottom" secondItem="MdP-Di-AAN" secondAttribute="bottom" id="V6B-7Y-cGg"/>
                             <constraint firstItem="lzh-oR-jCt" firstAttribute="centerY" secondItem="a2M-IY-za4" secondAttribute="centerY" id="YsD-7L-ksP"/>
                             <constraint firstAttribute="trailing" secondItem="PUC-EB-gWY" secondAttribute="trailing" constant="16" id="aHb-zg-g8f"/>
                             <constraint firstItem="PUC-EB-gWY" firstAttribute="centerY" secondItem="a2M-IY-za4" secondAttribute="centerY" id="aR1-K7-6QV"/>
+                            <constraint firstItem="B0p-2V-BSk" firstAttribute="leading" secondItem="a2M-IY-za4" secondAttribute="leading" id="ctJ-67-3NM"/>
                             <constraint firstAttribute="bottom" secondItem="B0p-2V-BSk" secondAttribute="bottom" id="qEU-4J-OfL"/>
                             <constraint firstItem="MdP-Di-AAN" firstAttribute="leading" secondItem="a2M-IY-za4" secondAttribute="leading" id="sFF-IC-k5Z"/>
                         </constraints>
@@ -77,7 +85,7 @@
                 <constraint firstItem="P8Z-Rm-axf" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" id="frA-RP-Hta"/>
                 <constraint firstAttribute="trailing" secondItem="P8Z-Rm-axf" secondAttribute="trailing" id="i2r-0P-Usw"/>
             </constraints>
-            <point key="canvasLocation" x="188" y="-59.5"/>
+            <point key="canvasLocation" x="336" y="-59.5"/>
         </customView>
     </objects>
 </document>

+ 35 - 20
PDF Office/PDF Master/KMClass/KMPDFViewController/Toolbar/KMPDFToolbarController.swift

@@ -80,34 +80,24 @@ class KMPDFToolbarController: NSViewController {
     override func viewWillLayout() {
         super.viewWillLayout()
         
-        let pointX: CGFloat = (CGRectGetWidth(view.frame) - tabsView.viewWidth)/2
-        
-        var xValue: CGFloat = 0
-        
-        if pointX-12-264 > 10 {
-            xValue = pointX - 12 - 264
-            tabsLeftConst.constant = xValue
-            tabsRightConst.constant = CGRectGetWidth(view.frame) - tabsView.viewWidth - 12 - 264 - rightViewWidthConst.constant - 12 - xValue
-        } else {
-            xValue = 10
-            let rightXValue = CGRectGetWidth(view.frame) - tabsView.viewWidth - 12 - 264 - rightViewWidthConst.constant - 12 - xValue
-            
-            if rightXValue < 10 {
-                tabsMoreButton.superview?.isHidden = false
-                tabsRightConst.constant = 44
-            } else {
-                tabsMoreButton.superview?.isHidden = true
-                tabsRightConst.constant = rightXValue
-            }
-        }
+        refreshTabsScrollViewUI()
     }
     
     override func viewDidLayout() {
         super.viewDidLayout()
         
+        refreshTabsScrollViewUI()
+        
         refreshTabsScrollViewVisiableRect()
     }
     
+    override func viewDidAppear() {
+        super.viewDidAppear()
+        
+        refreshTabsScrollViewUI()
+        
+    }
+    
     override func viewDidLoad() {
         super.viewDidLoad()
         // Do view setup here.
@@ -260,6 +250,31 @@ class KMPDFToolbarController: NSViewController {
         
     }
     
+    func refreshTabsScrollViewUI() {
+        let pointX: CGFloat = (CGRectGetWidth(view.frame) - tabsView.viewWidth)/2
+        
+        var xValue: CGFloat = 0
+        
+        if pointX-12-264 > 10 {
+            xValue = pointX - 12 - 264
+            tabsLeftConst.constant = xValue
+            tabsRightConst.constant = CGRectGetWidth(view.frame) - tabsView.viewWidth - 12 - 264 - rightViewWidthConst.constant - 12 - xValue
+            
+            tabsMoreButton.superview?.isHidden = true
+        } else {
+            xValue = 10
+            let rightXValue = CGRectGetWidth(view.frame) - tabsView.viewWidth - 12 - 264 - rightViewWidthConst.constant - 12 - xValue
+            
+            if rightXValue < 10 {
+                tabsMoreButton.superview?.isHidden = false
+                tabsRightConst.constant = 44
+            } else {
+                tabsMoreButton.superview?.isHidden = true
+                tabsRightConst.constant = rightXValue
+            }
+        }
+    }
+    
     func refreshTabsScrollViewVisiableRect() {
         guard let toolbarManager = self.toolbarManager, let viewManager = self.viewManager else {
             return