123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- //
- // KMWatermarkAligementView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/1/17.
- //
- import Cocoa
- typealias KMWatermarkAligementViewItemClick = (_ index: Int)->()
- class KMWatermarkAligementView: NSView {
- var leftButton = NSButton()
- var centerButton = NSButton()
- var rightButton = NSButton()
-
- var vLine = NSView()
- var vLine2 = NSView()
-
- var itemClick: KMWatermarkAligementViewItemClick!
-
- private var myIsEnabled: Bool = true
- var isEnabled: Bool {
- get {
- return myIsEnabled
- }
- set {
- myIsEnabled = newValue
-
- for button in [leftButton, centerButton, rightButton] {
- button.isEnabled = newValue
- }
- }
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- initSubViews()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- initSubViews()
- }
-
- func initSubViews() {
- wantsLayer = true
- layer?.borderWidth = 1
- layer?.borderColor = NSColor.black.cgColor
- layer?.cornerRadius = 2
-
- addSubview(leftButton)
- addSubview(centerButton)
- addSubview(rightButton)
-
- addSubview(vLine)
- addSubview(vLine2)
-
- for button in [leftButton, centerButton, rightButton] {
- button.isBordered = false
- button.wantsLayer = true
- button.target = self
- button.action = #selector(buttonAction)
- }
-
- leftButton.title = ""
- centerButton.title = ""
- rightButton.title = ""
-
- for line in [vLine, vLine2] {
- line.wantsLayer = true
- line.layer?.backgroundColor = NSColor.black.cgColor
- }
-
- buttonAction(sender: leftButton)
- }
-
- override func layout() {
- super.layout()
-
- let width: CGFloat = NSWidth(self.bounds)
- let height: CGFloat = NSHeight(self.bounds)
-
- let itemWidth: CGFloat = width / 3
- leftButton.frame = NSMakeRect(0, 0, itemWidth, height)
- centerButton.frame = NSMakeRect(itemWidth, 0, itemWidth, height)
- rightButton.frame = NSMakeRect(2*itemWidth, 0, itemWidth, height)
-
- vLine.frame = NSMakeRect(itemWidth, 0, 1, height)
- vLine2.frame = NSMakeRect(2*itemWidth, 0, 1, height)
- }
-
- @objc func buttonAction(sender: NSButton) {
- dealButtonSelectedState(selectButton: sender)
-
- guard let callback = itemClick else {
- return
- }
-
- var index: Int = 0
- if (sender.isEqual(to: leftButton)) {
- index = 0
- } else if (sender.isEqual(to: centerButton)) {
- index = 1
- } else if (sender.isEqual(to: rightButton)) {
- index = 2
- }
- callback(index)
- }
-
- func dealButtonSelectedState(selectButton: NSButton) {
- for button in [leftButton, centerButton, rightButton] {
- if (button.isEqual(to: selectButton)) {
- button.wantsLayer = true
- button.layer?.cornerRadius = 4
- button.layer?.borderWidth = 1
- button.layer?.borderColor = NSColor.km_init(hex: "#CED0D4").cgColor
- button.layer?.backgroundColor = NSColor.km_init(hex: "#CED0D499").cgColor
- } else {
- button.wantsLayer = true
- button.layer?.backgroundColor = NSColor.clear.cgColor
- button.layer?.borderColor = NSColor.clear.cgColor
- }
-
- }
- if (selectButton.isEqual(to: self.leftButton)) {
- self.leftButton.image = NSImage(named: "KMImageNameEditPDFAlignLeftSelect")
- self.leftButton.wantsLayer = true
- self.leftButton.layer?.backgroundColor = NSColor.km_init(hex: "#CED0D499").cgColor
-
- self.centerButton.image = NSImage(named: "KMImageNameEditPDFAlignCenter")
- self.rightButton.image = NSImage(named: "KMImageNameEditPDFAlignRight")
- } else if (selectButton.isEqual(to: self.centerButton)) {
- self.centerButton.image = NSImage(named: "KMImageNameEditPDFAlignCenterSelect")
-
- self.leftButton.image = NSImage(named: "KMImageNameEditPDFAlignLeft")
- self.rightButton.image = NSImage(named: "KMImageNameEditPDFAlignRight")
- } else if (selectButton.isEqual(to: self.rightButton)) {
- self.rightButton.image = NSImage(named: "KMImageNameEditPDFAlignRightSelect")
-
- self.centerButton.image = NSImage(named: "KMImageNameEditPDFAlignCenter")
- self.leftButton.image = NSImage(named: "KMImageNameEditPDFAlignLeft")
- }
- }
-
- func selectIndex(index: Int) {
- if (index == 0) {
- dealButtonSelectedState(selectButton: leftButton)
- } else if (index == 1) {
- dealButtonSelectedState(selectButton: centerButton)
- } else if (index == 2) {
- dealButtonSelectedState(selectButton: rightButton)
- }
- }
- }
|