123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- //
- // KMWatermarkAdjectiveSegementControl.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/12/16.
- //
- import Cocoa
- class KMWatermarkAdjectiveSegementItemControl: NSControl {
- lazy var button: NSButton = {
- let button = NSButton()
- button.isBordered = false
- button.font = NSFont.systemFont(ofSize: 14)
- button.target = self
- button.action = #selector(buttonClick)
- return button
- }()
-
- private lazy var bottomLine: NSView = {
- let line = NSView()
- line.wantsLayer = true
- line.layer?.backgroundColor = NSColor.black.cgColor
- return line
- }()
-
- private var mySelected: Bool!
- var selected: Bool {
- get {
- return mySelected
- }
- set {
- mySelected = newValue
-
- if (newValue) {
- self.bottomLine.isHidden = false
- self.button.attributedTitle = NSAttributedString(string: self.button.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.black, NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14)])
- } else {
- self.bottomLine.isHidden = true
- self.button.attributedTitle = NSAttributedString(string: self.button.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor(white: 102.0/255.0, alpha: 1.0), NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14)])
- }
- }
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- self.initSubViews()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- self.initSubViews()
- }
-
- func initSubViews() {
- self.addSubview(self.button)
- self.addSubview(self.bottomLine)
- }
-
- override func layout() {
- super.layout()
-
- button.frame = self.bounds
- bottomLine.frame = NSMakeRect(0, 0, NSWidth(self.bounds), 1)
- }
-
- @objc func buttonClick() {
- if ((self.target?.responds(to: self.action))!) {
- self.target?.perform(self.action, with: self)
- }
- }
- }
- typealias KMWatermarkAdjectiveSegementControlItemClick = (_ index: Int) -> ()
- class KMWatermarkAdjectiveSegementControl: NSView {
-
- private var itemControls: Array<KMWatermarkAdjectiveSegementItemControl> = []
-
- private var myItems: Array<String>!
- var items: Array<String> {
- get {
- return myItems
- }
- set {
- myItems = newValue
-
- /// 清理数据
- for itemControl in self.itemControls {
- itemControl.removeFromSuperview()
- }
- self.itemControls.removeAll()
-
- /// 添加数据
- for titleValue in newValue {
- let itemControl = KMWatermarkAdjectiveSegementItemControl()
- itemControl.button.title = titleValue
- itemControl.target = self
- itemControl.action = #selector(itemControlDidSelected)
- self.addSubview(itemControl)
- self.itemControls.append(itemControl)
- }
-
- self.selectItemIndex = mySelectItemIndex
-
- self.layoutSubtreeIfNeeded()
- }
- }
-
- private var mySelectItemIndex: Int = 0
- var selectItemIndex: Int {
- get {
- return mySelectItemIndex
- }
- set {
- mySelectItemIndex = newValue
-
- var i: Int = 0
- for itemControl in itemControls {
- if (i == newValue) {
- itemControl.selected = true
- } else {
- itemControl.selected = false
- }
- i += 1
- }
- }
- }
-
- var itemClick: KMWatermarkAdjectiveSegementControlItemClick!
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- }
-
- override func layout() {
- super.layout()
-
- let width: CGFloat = NSWidth(self.bounds)
- let height: CGFloat = NSHeight(self.bounds)
-
- var i: Int = 0
- var itemW: CGFloat = width * 0.5
- if (self.itemControls.count > 0) {
- itemW = width / CGFloat(self.itemControls.count)
- }
-
- for itemControl in self.itemControls {
- itemControl.frame = NSMakeRect(CGFloat(i) * itemW, 0, itemW, height)
- i += 1
- }
- }
-
- @objc func itemControlDidSelected(sender: KMWatermarkAdjectiveSegementItemControl) {
- var index: Int = 0
- for itemControl in itemControls {
- if (itemControl.isEqual(to: sender)) {
- break
- }
- index += 1
- }
-
- self.selectItemIndex = index;
-
- guard let callback = itemClick else {
- return
- }
-
- callback(self.selectItemIndex)
- }
- }
|