123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- //
- // KMRedactTopToolBar.swift
- // PDF Master
- //
- // Created by tangchao on 2023/1/16.
- //
- import Cocoa
- typealias KMRedactTopToolBarItemViewItemClick = (_ index: Int) -> ()
- private class KMRedactTopToolBarItemView: NSView {
- var imageView = NSImageView()
- var titleLabel = NSTextField(labelWithString: "")
- var bottomLine = NSView()
- var button = NSButton()
-
- var itemID: Int = 0
-
- private var myIsSelected: Bool = false
- var isSelected: Bool {
- get {
- return myIsSelected
- }
- set {
- myIsSelected = newValue
-
- if (myIsSelected) {
- self.bottomLine.isHidden = false
- } else {
- self.bottomLine.isHidden = true
- }
- }
- }
-
- var itemClick: KMRedactTopToolBarItemViewItemClick!
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- initSubViews()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- initSubViews()
- }
-
- func initSubViews() {
- self.addSubview(self.imageView)
- self.addSubview(self.titleLabel)
- self.addSubview(self.bottomLine)
-
- self.bottomLine.wantsLayer = true
- self.bottomLine.layer?.backgroundColor = NSColor.black.cgColor
-
- self.addSubview(self.button)
- self.button.isBordered = false
- self.button.title = ""
- self.button.target = self
- self.button.action = #selector(buttonAction)
- }
-
- override func layout() {
- super.layout()
-
- let width: CGFloat = NSWidth(self.bounds)
- let heigth: CGFloat = NSHeight(self.bounds)
-
- let imageSize: NSSize = NSSize(width: 14, height: 15)
- let imageY: CGFloat = (heigth-imageSize.height)*0.5
- self.imageView.frame = NSMakeRect(0, imageY, imageSize.width, imageSize.height)
-
- let titleH: CGFloat = 20
- let titleY: CGFloat = (heigth-titleH)*0.5
- let titleX: CGFloat = self.imageView.frame.maxX+8
- self.titleLabel.frame = NSMakeRect(titleX, titleY, width-titleX, titleH)
-
- self.bottomLine.frame = NSMakeRect(0, 0, width, 1)
-
- self.button.frame = self.bounds
- }
-
- @objc func buttonAction() {
- guard let callback = self.itemClick else {
- return
- }
-
- callback(self.itemID)
- }
- }
- typealias KMRedactTopToolBarItemClick = (_ index: Int) -> ()
- class KMRedactTopToolBar: NSView {
- private var textAndImageItemView = KMRedactTopToolBarItemView()
- private var pageItemView = KMRedactTopToolBarItemView()
-
- private var applyButton = NSButton()
- private var clearButton = NSButton()
- private var closeButton = NSButton()
-
- var itemClick: KMRedactTopToolBarItemClick!
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- initSubViews()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- initSubViews()
- }
-
- public func initSubViews() {
- self.addSubview(self.textAndImageItemView)
- self.addSubview(self.pageItemView)
- self.addSubview(self.applyButton)
- self.addSubview(self.clearButton)
- self.addSubview(self.closeButton)
-
- self.textAndImageItemView.titleLabel.stringValue = NSLocalizedString("Redact Text & Image", comment: "")
- self.textAndImageItemView.itemClick = { [weak self] index in
- self!.selectItem(0)
-
- guard let callback = self!.itemClick else {
- return
- }
-
- callback(4)
- }
- self.pageItemView.titleLabel.stringValue = NSLocalizedString("Redact Page", comment: "")
- self.pageItemView.itemClick = { [weak self] index in
- self!.selectItem(1)
-
- guard let callback = self!.itemClick else {
- return
- }
-
- callback(5)
- }
-
- self.applyButton.isBordered = false
- self.applyButton.title = NSLocalizedString("Redact", comment: "")
- self.applyButton.wantsLayer = true
- self.applyButton.layer?.backgroundColor = NSColor.black.cgColor
- self.applyButton.attributedTitle = NSAttributedString(string: self.applyButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
- self.applyButton.layer?.cornerRadius = 4
- self.applyButton.target = self
- self.applyButton.action = #selector(applyButtonAction)
-
- self.clearButton.isBordered = false
- self.clearButton.title = NSLocalizedString("Erase", comment: "")
- self.clearButton.wantsLayer = true
- self.clearButton.layer?.backgroundColor = NSColor.black.cgColor
- self.clearButton.attributedTitle = NSAttributedString(string: self.clearButton.title, attributes: [NSAttributedString.Key.foregroundColor : NSColor.white])
- self.clearButton.layer?.cornerRadius = 4
- self.clearButton.target = self
- self.clearButton.action = #selector(clearButtonAction)
-
- self.closeButton.isBordered = false
- self.closeButton.title = NSLocalizedString("Exit", comment: "")
- self.closeButton.wantsLayer = true
- self.closeButton.layer?.cornerRadius = 4
- self.closeButton.layer?.borderWidth = 1
- self.closeButton.layer?.borderColor = NSColor.black.cgColor
- self.closeButton.target = self
- self.closeButton.action = #selector(closeButtonAction)
- }
-
- override func layout() {
- super.layout()
-
- let width: CGFloat = NSWidth(self.bounds)
- let height: CGFloat = NSHeight(self.bounds)
-
- let itemX: CGFloat = (width-280)*0.5
- self.textAndImageItemView.frame = NSMakeRect(itemX, 0, 140, height)
- self.pageItemView.frame = NSMakeRect(self.textAndImageItemView.frame.maxX, 0, 140, height)
-
- let buttonH: CGFloat = 24
- let buttonY: CGFloat = (height-buttonH)*0.5
-
- let closeW: CGFloat = 44
- let closeX: CGFloat = width-12-closeW
- let closeY: CGFloat = (height-buttonH)*0.5
- self.closeButton.frame = NSMakeRect(closeX, closeY, closeW, buttonH)
-
- let clearW: CGFloat = 68
- let clearX: CGFloat = self.closeButton.frame.minX-8-clearW
- self.clearButton.frame = NSMakeRect(clearX, buttonY, clearW, buttonH)
-
- let applyW: CGFloat = 68
- let applyX: CGFloat = self.clearButton.frame.minX-8-applyW
- self.applyButton.frame = NSMakeRect(applyX, buttonY, applyW, buttonH)
- }
-
- @objc private func applyButtonAction() {
- guard let callback = self.itemClick else {
- return
- }
-
- callback(3)
- }
-
- @objc private func clearButtonAction() {
- guard let callback = self.itemClick else {
- return
- }
-
- callback(2)
- }
-
- @objc private func closeButtonAction() {
- guard let callback = self.itemClick else {
- return
- }
-
- callback(1)
- }
-
- public func selectItem(_ index: Int) {
- var count: Int = 0
- for item in [self.textAndImageItemView,
- self.pageItemView] {
- if (index == count) {
- item.isSelected = true
- } else {
- item.isSelected = false
- }
- count += 1
- }
- }
-
- }
|