Browse Source

【左边栏】KMSegmentedControl OC转Swift

tangchao 1 year ago
parent
commit
da7ed4047e

+ 8 - 0
PDF Office/PDF Master.xcodeproj/project.pbxproj

@@ -3331,6 +3331,9 @@
 		BBC745F7296178BD0072C2ED /* KMCropTools.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC745F6296178BD0072C2ED /* KMCropTools.swift */; };
 		BBC745F8296178BD0072C2ED /* KMCropTools.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC745F6296178BD0072C2ED /* KMCropTools.swift */; };
 		BBC745F9296178BD0072C2ED /* KMCropTools.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC745F6296178BD0072C2ED /* KMCropTools.swift */; };
+		BBC8A75C2B05B55200FA9377 /* KMSegmentedControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC8A75B2B05B55200FA9377 /* KMSegmentedControl.swift */; };
+		BBC8A75D2B05B55200FA9377 /* KMSegmentedControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC8A75B2B05B55200FA9377 /* KMSegmentedControl.swift */; };
+		BBC8A75E2B05B55200FA9377 /* KMSegmentedControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC8A75B2B05B55200FA9377 /* KMSegmentedControl.swift */; };
 		BBCE57102A72712200508EFC /* NSWindowController+KMExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBCE570F2A72712200508EFC /* NSWindowController+KMExtension.swift */; };
 		BBCE57112A72712200508EFC /* NSWindowController+KMExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBCE570F2A72712200508EFC /* NSWindowController+KMExtension.swift */; };
 		BBCE57122A72712200508EFC /* NSWindowController+KMExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBCE570F2A72712200508EFC /* NSWindowController+KMExtension.swift */; };
@@ -5058,6 +5061,7 @@
 		BBC745E9295F067B0072C2ED /* KMCropSettingWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMCropSettingWindowController.xib; sourceTree = "<group>"; };
 		BBC745F1295F0DD00072C2ED /* KMCropSettingPageSizeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMCropSettingPageSizeView.swift; sourceTree = "<group>"; };
 		BBC745F6296178BD0072C2ED /* KMCropTools.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMCropTools.swift; sourceTree = "<group>"; };
+		BBC8A75B2B05B55200FA9377 /* KMSegmentedControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMSegmentedControl.swift; sourceTree = "<group>"; };
 		BBCE570F2A72712200508EFC /* NSWindowController+KMExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSWindowController+KMExtension.swift"; sourceTree = "<group>"; };
 		BBCE57132A72713A00508EFC /* NSViewController+KMExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSViewController+KMExtension.swift"; sourceTree = "<group>"; };
 		BBCE57172A72723600508EFC /* NSResponder+KMExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSResponder+KMExtension.swift"; sourceTree = "<group>"; };
@@ -5516,6 +5520,7 @@
 				8942F7FE29260EC700389627 /* KMLeftMethodMode.swift */,
 				8942F80229262B2E00389627 /* KMSideViewController.swift */,
 				BB0B2CDB2B04B9510088FFD8 /* KMLeftSideViewSearchField.swift */,
+				BBC8A75B2B05B55200FA9377 /* KMSegmentedControl.swift */,
 				899700F128F40308009AF911 /* Annotation */,
 				899700F228F40308009AF911 /* BookMarks */,
 				899700F028F40308009AF911 /* From */,
@@ -11714,6 +11719,7 @@
 				BB6719F52AD2C949003D44D5 /* CPDFRedactAnnotation+PDFListView.swift in Sources */,
 				BBB9B31C299A5D6D004F3235 /* KMCloudOperation.m in Sources */,
 				BB981E592AD4F70D001988CA /* KMWelcomeWindowController.swift in Sources */,
+				BBC8A75C2B05B55200FA9377 /* KMSegmentedControl.swift in Sources */,
 				BB8B173C2907CDD9001C5EA5 /* NibLoadable.swift in Sources */,
 				BBF62C682B033B34007B7E86 /* KMPDFEditExtractWindow.swift in Sources */,
 				BB2F9AB32AFCC2F800F9DD93 /* KMProfileInfoWindowController.swift in Sources */,
@@ -12486,6 +12492,7 @@
 				BB49ED16293F489500C82CA2 /* KMConvertImageSettingView.swift in Sources */,
 				ADBC375E29CC61E100D93208 /* KMReadModelView.swift in Sources */,
 				BB2EDF7A296ECE17003BCF58 /* KMPageEditThumbnailItem.swift in Sources */,
+				BBC8A75D2B05B55200FA9377 /* KMSegmentedControl.swift in Sources */,
 				9FBA0EF628FFC8A0001117AF /* KMCollectionView.swift in Sources */,
 				AD85D1A52AF09864000F4D28 /* KMHomeQuickToolsWindowController.swift in Sources */,
 				ADD1B7072946CBB600C3FFF7 /* KMBaseTextFormatter.swift in Sources */,
@@ -13291,6 +13298,7 @@
 				9FDD0F79294FEF64000C4DAD /* KMConvertPDFManager.swift in Sources */,
 				ADDEEA4B2AD38BDB00EF675D /* KMSignatureHelpViewController.swift in Sources */,
 				9FDD0F7E2952F2D8000C4DAD /* KMBaseParser.swift in Sources */,
+				BBC8A75E2B05B55200FA9377 /* KMSegmentedControl.swift in Sources */,
 				BB1A916C2AFB7868005E5FD8 /* KMConvertWindowController.swift in Sources */,
 				9FCFEC7E2AC579F600EAD2CB /* CPDFListSignatureAnnotation.swift in Sources */,
 				BB8115FD29924A5F0008F536 /* KMSecureEncryptCheckCellView.swift in Sources */,

+ 18 - 17
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/KMLeftSideViewController.swift

@@ -401,7 +401,8 @@ class KMLeftSideViewController: KMSideViewController {
 //    @property (nonatomic, retain) IBOutlet SKTableView *findTableView;
 //    @property (nonatomic, retain) IBOutlet SKTableView *groupedFindTableView;
     
-//    @property (nonatomic, retain) IBOutlet KMSegmentedControl *segmentedControl;
+//    @property (nonatomic, retain)   *;
+    @IBOutlet var segmentedControl: KMSegmentedControl!
     
     @IBOutlet weak var leftListView: NSView!
     
@@ -696,22 +697,22 @@ class KMLeftSideViewController: KMSideViewController {
 //        [searchViewController.segmentedControl setSelectedSegment:0];
 //        [mainController bind:@"findPaneState" toObject:searchViewController.segmentedControl withKeyPath:@"selectedSegment" options:nil];
 
-//        [segmentedControl setSegmentCount:5];
-//        [segmentedControl setImage:[NSImage imageNamed:KMImageNameUXIconSidetabbarThumbnailNor] forSegment:0];
-//        [segmentedControl setImage:[NSImage imageNamed:KMImageNameUXIconSidetabbarOutlineNor] forSegment:1];
-//        [segmentedControl setImage:[NSImage imageNamed:KMImageNameUXIconSidetabbarAnnotationNor] forSegment:2];
-//        [segmentedControl setImage:[NSImage imageNamed:KMImageNameUXIconSidetabbarSnapshotNor] forSegment:3];
-//        [segmentedControl setImage:[NSImage imageNamed:KMImageNameUXIconSidetabbarSearchNor] forSegment:4];
-//        [segmentedControl setToolTip:NSLocalizedString(@"View Thumbnails", nil) forSegment:0];
-//        [segmentedControl setToolTip:NSLocalizedString(@"View Outline", nil) forSegment:1];
-//        [segmentedControl setToolTip:NSLocalizedString(@"View Notes", nil) forSegment:2];
-//        [segmentedControl setToolTip:NSLocalizedString(@"View Snapshots", nil) forSegment:3];
-//        [segmentedControl setToolTip:NSLocalizedString(@"Search", nil) forSegment:4];
-//        [segmentedControl setSelectedSegment:0];
+        self.segmentedControl.segmentCount = 5
+        self.segmentedControl.setImage(NSImage(named: KMImageNameUXIconSidetabbarThumbnailNor)!, for: 0)
+        self.segmentedControl.setImage(NSImage(named: KMImageNameUXIconSidetabbarOutlineNor)!, for: 1)
+        self.segmentedControl.setImage(NSImage(named: KMImageNameUXIconSidetabbarAnnotationNor)!, for: 2)
+        self.segmentedControl.setImage(NSImage(named: KMImageNameUXIconSidetabbarSnapshotNor)!, for: 3)
+        self.segmentedControl.setImage(NSImage(named: KMImageNameUXIconSidetabbarSearchNor)!, for: 4)
+        self.segmentedControl.setToolTip(KMLocalizedString("View Thumbnails", nil), for: 0)
+        self.segmentedControl.setToolTip(KMLocalizedString("View Outline", nil), for: 1)
+        self.segmentedControl.setToolTip(KMLocalizedString("View Notes", nil), for: 2)
+        self.segmentedControl.setToolTip(KMLocalizedString("View Snapshots", nil), for: 3)
+        self.segmentedControl.setToolTip(KMLocalizedString("Search", nil), for: 4)
+        self.segmentedControl.selectedSegment = 0
 //        [segmentedControl bind:@"selectedSegment" toObject:mainController withKeyPath:@"leftSidePaneState" options:nil];
 //        [mainController bind:@"leftSidePaneState" toObject:segmentedControl withKeyPath:@"selectedSegment" options:nil];
-//        segmentedControl.wantsLayer = YES;
-//        segmentedControl.layer.backgroundColor = [KMAppearance KMColor_Layout_L_1].CGColor;
+        self.segmentedControl.wantsLayer = true
+        self.segmentedControl.layer?.backgroundColor = KMAppearance.Layout.l_1Color().cgColor
         
 //        self.button.setHelp(KMLocalizedString("View Thumbnails", "Tool tip message"), for: KMLeftSidePaneState.thumbnail.rawValue)
 //        self.button.setHelp(KMLocalizedString("View Outline", "Tool tip message"), for: KMLeftSidePaneState.outline.rawValue)
@@ -1267,7 +1268,7 @@ extension KMLeftSideViewController {
             self.snapshotSearchField.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
             self.outlineSearchField.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
             self.noteSearchField.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
-//            self.segmentedControl.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
+            self.segmentedControl.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
             
             self.snapshotSearchField.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
             self.outlineSearchField.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1)
@@ -1284,7 +1285,7 @@ extension KMLeftSideViewController {
             self.snapshotSearchField.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
             self.outlineSearchField.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
             self.noteSearchField.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
-            //            self.segmentedControl.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
+            self.segmentedControl.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
             
             self.snapshotSearchField.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1)
             self.outlineSearchField.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1)

+ 192 - 0
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/KMSegmentedControl.swift

@@ -0,0 +1,192 @@
+//
+//  KMSegmentedControl.swift
+//  PDF Master
+//
+//  Created by tangchao on 2023/11/16.
+//
+
+import Cocoa
+
+class KMSegmentedControl: NSControl {
+    
+    private var _segmentCount: UInt8 = 0
+    var segmentCount: UInt8 {
+        get {
+            return self._segmentCount
+        }
+        set {
+            self._segmentCount = newValue
+            self.setSegmentCount(self.segmentCount, with: 55)
+        }
+  
+    }
+    var selectedSegment: UInt8 = 0 {
+        didSet {
+            if (self.block != nil) {
+                self.block!(self.selectedSegment)
+            }
+
+            for i in 0 ..< self.segments.count {
+                let button = self.segments[i]
+                if (self.isBackgroundHighlighted) {
+                    if (i == self.selectedSegment) {
+                        button.wantsLayer = true
+                        button.layer?.cornerRadius = 6.0
+                        if (KMAppearance.isDarkMode()) {
+                            button.layer?.backgroundColor = NSColor(red: 0.133, green: 0.478, blue: 1, alpha: 0.3).cgColor
+                        } else {
+                            button.layer?.backgroundColor = NSColor(red: 0.286, green: 0.510, blue: 0.902, alpha: 0.2).cgColor
+                        }
+
+                        if (i == 0) {
+                            button.image = NSImage(named: KMImageNameUXIconBtnSidebarListPre)
+                        } else if (i == 1) {
+                            button.image = NSImage(named: KMImageNameUXIconBtnSidebarPagePre)
+                        }
+                    } else {
+                        button.wantsLayer = true;
+                        button.layer?.backgroundColor = .clear
+                        if (i == 0) {
+                            button.image = NSImage(named: KMImageNameUXIconBtnSidebarListNor)
+                        } else if (i == 1) {
+                            button.image = NSImage(named: KMImageNameUXIconBtnSidebarPageNor)
+                        }
+                    }
+                } else {
+                    if (i == self.selectedSegment) {
+                        if (i == 0) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarThumbnailSel)
+                        } else if (i == 1) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarOutlineSel)
+                        } else if (i == 2) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarAnnotationSel)
+                        } else if (i == 3) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarSnapshotSel)
+                        } else if (i == 4) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarSearchSel)
+                        }
+                        button.wantsLayer = true
+                        if (KMAppearance.isDarkMode()) {
+                            button.layer?.backgroundColor = NSColor(red: 0.133, green: 0.478, blue: 1.000, alpha: 1).cgColor
+                        } else {
+                            button.layer?.backgroundColor = NSColor(red: 0.286, green: 0.510, blue: 0.902, alpha: 1).cgColor
+                        }
+                    } else {
+                        if (i == 0) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarThumbnailNor)
+                        } else if (i == 1) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarOutlineNor)
+                        } else if (i == 2) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarAnnotationNor)
+                        } else if (i == 3) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarSnapshotNor)
+                        } else if (i == 4) {
+                            button.image = NSImage(named: KMImageNameUXIconSidetabbarSearchNor)
+                        }
+                        button.wantsLayer = true
+                        if (KMAppearance.isDarkMode()) {
+                            button.layer?.backgroundColor = NSColor(red: 0.224, green: 0.235, blue: 0.243, alpha: 1).cgColor
+                        } else {
+                            button.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
+                        }
+                    }
+                }
+            }
+        }
+    }
+    var isBackgroundHighlighted = false
+    var block: ((_ segmentCount: UInt8) -> Void)?
+    var buttonActionHandle: ((_ segmentCount: UInt8) -> Void)?
+    
+    private var _segments: [NSButton] = []
+    var segments: [NSButton] {
+        get {
+            return self._segments
+        }
+    }
+    private var _contentView: NSView?
+    var contentView: NSView? {
+        get {
+            return self._contentView
+        }
+    }
+
+    override func updateLayer() {
+        super.updateLayer()
+        
+        if #available(macOS 10.14, *) {
+            self.selectedSegment = self._segmentCount
+        }
+    }
+    
+    // MARK: - Public Methods
+    
+    func setSegmentCount(_ segmentCount: uint8, with width: Float) {
+        self._segmentCount = segmentCount
+        
+        let height = self.frame.size.height
+        if (self.contentView?.superview != nil) {
+            self.contentView?.removeFromSuperview()
+        }
+        self._contentView = NSView()
+        let x = (self.frame.size.width - (Float(self.segmentCount) * width).cgFloat) / 2.0
+        let y = (self.frame.size.height-height)/2.0
+        self._contentView?.frame = NSMakeRect(x, y, (Float(self.segmentCount)*width).cgFloat, height)
+        self._contentView?.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
+        self.addSubview(self.contentView!)
+        
+        self._segments.removeAll()
+        for i in 0 ..< self.segmentCount {
+            let button = NSButton()
+            button.bezelStyle = .regularSquare
+            button.isBordered = false
+            button.imageScaling = .scaleProportionallyDown
+            button.imagePosition = .imageOnly
+            button.frame = NSMakeRect((Float(i)*width).cgFloat, 0, width.cgFloat, height)
+            button.tag = Int(i)
+            button.target = self
+            button.action = #selector(_buttonAction)
+            self.contentView?.addSubview(button)
+            self._segments.append(button)
+        }
+    }
+    
+    func setImage(_ image: NSImage, for segment: UInt8) {
+        if (segment >= self.segments.count) {
+            return
+        }
+        let button = self.segments[Int(segment)]
+        button.image = image
+        button.alternateImage = image
+    }
+    
+    func setToolTip(_ toolTip: String, for segment: uint8) {
+        if (segment >= self.segments.count) {
+            return
+        }
+        let button = self.segments[Int(segment)]
+        button.toolTip = toolTip
+    }
+}
+
+// MARK: - Private Methods
+
+extension KMSegmentedControl {
+    @objc private func _buttonAction(_ sender: NSButton) {
+        self.selectedSegment = UInt8(sender.tag)
+        if (self.buttonActionHandle != nil) {
+            self.buttonActionHandle!(self.selectedSegment)
+        }
+    }
+    
+    /*
+     - (NSImage *)image:(NSImage *)image withColor:(NSColor *)color {
+         NSImage *newImage = [image copy];
+         [newImage lockFocus];
+         [color set];
+         NSRectFillUsingOperation(NSMakeRect(0, 0, image.size.width, image.size.height), NSCompositingOperationSourceAtop);
+         [newImage unlockFocus];
+         return [newImage autorelease];
+     }
+     */
+}

+ 1 - 1
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/LeftSideView.xib

@@ -75,7 +75,7 @@
                                 <constraint firstAttribute="height" constant="1" id="EgG-kl-uKK"/>
                             </constraints>
                         </box>
-                        <customView id="7Y2-6N-ggP" customClass="KMSegmentedControl">
+                        <customView id="7Y2-6N-ggP" customClass="KMSegmentedControl" customModule="PDF_Master" customModuleProvider="target">
                             <rect key="frame" x="0.0" y="0.0" width="200" height="36"/>
                             <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
                         </customView>