|
@@ -22,8 +22,8 @@ class KMSegmentedControl: NSControl {
|
|
|
self._segmentCount = newValue
|
|
|
self.setSegmentCount(self.segmentCount, with: 55)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
private var _selectedSegment: UInt8 = 0
|
|
|
var selectedSegment: UInt8 {
|
|
|
get {
|
|
@@ -34,78 +34,12 @@ class KMSegmentedControl: NSControl {
|
|
|
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
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+ self.reloadData()
|
|
|
}
|
|
|
}
|
|
|
var isBackgroundHighlighted = false
|
|
|
var block: ((_ segmentCount: UInt8) -> Void)?
|
|
|
- var buttonActionHandle: ((_ segmentCount: UInt8) -> Void)?
|
|
|
|
|
|
private var _segments: [NSButton] = []
|
|
|
var segments: [NSButton] {
|
|
@@ -132,8 +66,9 @@ class KMSegmentedControl: NSControl {
|
|
|
super.updateLayer()
|
|
|
|
|
|
if #available(macOS 10.14, *) {
|
|
|
- self.selectedSegment = self._selectedSegment
|
|
|
+// self.selectedSegment = self._selectedSegment
|
|
|
}
|
|
|
+ self.reloadData()
|
|
|
}
|
|
|
|
|
|
// MARK: - Public Methods
|
|
@@ -161,6 +96,7 @@ class KMSegmentedControl: NSControl {
|
|
|
button.isBordered = false
|
|
|
button.imageScaling = .scaleProportionallyDown
|
|
|
button.imagePosition = .imageOnly
|
|
|
+// button.layer?.cornerRadius = 6.0
|
|
|
button.frame = NSMakeRect((Float(i)*width).cgFloat, 0, width.cgFloat, height)
|
|
|
button.tag = Int(i)
|
|
|
button.target = self
|
|
@@ -220,6 +156,19 @@ class KMSegmentedControl: NSControl {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ func reloadData() {
|
|
|
+ let block = {
|
|
|
+ self._reloadData()
|
|
|
+ }
|
|
|
+ if Thread.isMainThread {
|
|
|
+ block()
|
|
|
+ } else {
|
|
|
+ Task { @MainActor in
|
|
|
+ block()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// MARK: - Private Methods
|
|
@@ -227,8 +176,69 @@ class KMSegmentedControl: NSControl {
|
|
|
extension KMSegmentedControl {
|
|
|
@objc private func _buttonAction(_ sender: NSButton) {
|
|
|
self.selectedSegment = UInt8(sender.tag)
|
|
|
- if (self.buttonActionHandle != nil) {
|
|
|
- self.buttonActionHandle!(self.selectedSegment)
|
|
|
+ }
|
|
|
+
|
|
|
+ private func _reloadData() {
|
|
|
+ for (i, button) in self.segments.enumerated() {
|
|
|
+ if (self.isBackgroundHighlighted) {
|
|
|
+ if (i == self.selectedSegment) {
|
|
|
+ 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.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)
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|