123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- //
- // CustomStampLeftToolbar.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/6/19.
- //
- import Cocoa
- class CustomStampLeftToolbar: NSView, NibLoadable {
-
- @IBOutlet var arrowBox: NSBox!
- @IBOutlet var arrowBtn: KMButton!
-
- @IBOutlet var textBox: NSBox!
- @IBOutlet var textButton: KMButton!
-
- @IBOutlet var dateBox: NSBox!
- @IBOutlet var dateButton: KMButton!
-
- @IBOutlet var peopleBox: NSBox!
- @IBOutlet var peopleButton: KMButton!
-
- @IBOutlet var imageBox: NSBox!
- @IBOutlet var imageButton: KMButton!
-
- @IBOutlet var stampBox: NSBox!
- @IBOutlet var stampButton: KMButton!
-
- var originalType: CSToolbarType = .none
- var toolbarType: CSToolbarType = .move
-
- var clickHandle: ((_ view: CustomStampLeftToolbar, _ actionType: CSToolbarType, _ string: String)->Void)?
-
- var lastDateItem: NSMenuItem?
- var lastIDItem: NSMenuItem?
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
-
- self.configUI()
-
- self.reloadData()
- }
-
- func configUI() {
- for box in [self.arrowBox, self.textBox, self.dateBox, self.peopleBox, self.imageBox, self.stampBox] {
- box?.cornerRadius = 6
- box?.borderColor = NSColor.clear
- }
-
- let dateNames = ["dd-MMM", "dd/MM/yyyy", "dd.MM.yyyy", "dd-MM-yyyy",
- "dd MMMM yyyy", "EEEE,MMMM d,yyyy", "MM/yy", "M/d/yyyy",
- "MM/dd/yyyy", "MM-dd-yyyy","MMMM dd,yyyy", "yyyy-MM-dd",
- "dd.MM.yyyy hh:mm:ss", "MM-dd-yyyy hh:mm:ss aaa", "MM/dd/yy hh:mm aaa","yyyy.MM.dd hh:mm:ss",
- "hh:mm:ss", "hh:mm:ss aaa"]
- let dateMenu = NSMenu.init()
- for idx in 0...dateNames.count-1 {
- let string = dateNames[idx]
- let menuItem = NSMenuItem.init(title: string, action: #selector(dateMenuItemClick(_:)), keyEquivalent: "")
- menuItem.tag = 1000 + idx
- menuItem.target = self
- dateMenu.addItem(menuItem)
- }
- self.dateBox?.menu = dateMenu
- self.dateBox.menu?.delegate = self
-
- var idNames = ["Name", "Login Name", "Email Address", "Organization Name"]
- let idMenu = NSMenu.init()
- for idx in 0...idNames.count-1 {
- let string = idNames[idx]
- let menuItem = NSMenuItem.init(title: NSLocalizedString(string, comment: ""), action: #selector(idMenuItemClick(_:)), keyEquivalent: "")
- menuItem.tag = 1000 + idx
- menuItem.target = self
- idMenu.addItem(menuItem)
- }
- self.peopleBox?.menu = idMenu
- self.peopleBox.menu?.delegate = self
-
- self.arrowBtn.mouseMoveCallback = {[weak self] mouseEntered in
- if mouseEntered {
- self?.arrowBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- if self?.toolbarType == .move {
- self?.arrowBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- self?.arrowBox.fillColor = NSColor.clear
- }
- }
- }
-
- self.textButton.mouseMoveCallback = {[weak self] mouseEntered in
- if mouseEntered {
- self?.textBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- if self?.toolbarType == .text {
- self?.textBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- self?.textBox.fillColor = NSColor.clear
- }
- }
- }
-
- self.dateButton.mouseMoveCallback = {[weak self] mouseEntered in
- if mouseEntered {
- self?.dateBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- if self?.toolbarType == .dateText {
- self?.dateBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- self?.dateBox.fillColor = NSColor.clear
- }
- }
- }
-
- self.peopleButton.mouseMoveCallback = {[weak self] mouseEntered in
- if mouseEntered {
- self?.peopleBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- if self?.toolbarType == .idText {
- self?.peopleBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- self?.peopleBox.fillColor = NSColor.clear
- }
- }
- }
-
- self.imageButton.mouseMoveCallback = {[weak self] mouseEntered in
- if mouseEntered {
- self?.imageBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- if self?.toolbarType == .image {
- self?.imageBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- self?.imageBox.fillColor = NSColor.clear
- }
- }
- }
-
- self.stampButton.mouseMoveCallback = {[weak self] mouseEntered in
- if mouseEntered {
- self?.stampBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- if self?.toolbarType == .stamp {
- self?.stampBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else {
- self?.stampBox.fillColor = NSColor.clear
- }
- }
- }
-
-
- }
- func reloadData() {
-
- for box in [self.arrowBox, self.textBox, self.dateBox, self.peopleBox, self.imageBox, self.stampBox] {
- box?.fillColor = NSColor.clear
- }
-
- if self.toolbarType == .move {
- self.arrowBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else if self.toolbarType == .text {
- self.textBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else if self.toolbarType == .dateText {
- self.dateBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else if self.toolbarType == .idText {
- self.peopleBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else if self.toolbarType == .image {
- self.imageBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- } else if self.toolbarType == .stamp {
- self.stampBox.fillColor = NSColor(red: 73/255, green: 130/255, blue: 230/255, alpha: 0.2)
- }
- }
-
- //MARK: IBAction
- @IBAction func buttonAction(_ sender: NSButton) {
-
- originalType = self.toolbarType
-
- if sender == self.arrowBtn {
- self.toolbarType = .move
-
- } else if sender == self.textButton {
- self.toolbarType = .text
-
- } else if sender == self.dateButton {
- self.toolbarType = .dateText
-
- if self.lastDateItem == nil {
- self.lastDateItem = self.dateBox.menu?.items.first
- }
- if self.lastDateItem != nil {
- self.dateMenuItemClick(self.lastDateItem!)
- }
-
- } else if sender == self.peopleButton {
- self.toolbarType = .idText
- self.reloadData()
-
- self.lastIDItem = nil
- self.peopleBox.menu?.popUp(positioning: self.peopleBox.menu?.item(at: 0), at: CGPoint(x: 0, y: 15), in: sender)
-
- return
-
- } else if sender == self.imageButton {
- self.toolbarType = .image
- } else if sender == self.stampButton {
- self.toolbarType = .stamp
- }
-
- guard let callBack = self.clickHandle else {
- return
- }
- callBack(self, self.toolbarType, "")
-
- self.reloadData()
-
- }
-
- @objc func idMenuItemClick(_ item: NSMenuItem) {
- var string = item.title
- if item.tag == 1000 {
- string = KMProfileInfo.shared().fullName
- } else if item.tag == 1001 {
- string = NSFullUserName()
- } else if item.tag == 1002 {
- string = KMProfileInfo.shared().email
- } else if item.tag == 1003 {
- string = KMProfileInfo.shared().OrganizeName
- }
- if string.isEmpty {
- string = " "
- }
- self.lastIDItem = item
-
- self.toolbarType = .idText
- self.reloadData()
-
- guard let callBack = self.clickHandle else {
- return
- }
- callBack(self, self.toolbarType, string)
- }
-
- @objc func dateMenuItemClick(_ item: NSMenuItem) {
- for subItem in self.dateBox.menu?.items ?? [] {
- subItem.state = .off
- }
- item.state = .on
-
- let string = item.title
- let date = Date()
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = string
- let dateString = dateFormatter.string(from: date)
- print(dateString)
-
- self.toolbarType = .dateText
- self.reloadData()
-
- guard let callBack = self.clickHandle else {
- return
- }
- callBack(self, self.toolbarType, dateString)
- self.lastDateItem = item
-
- }
-
- }
- extension CustomStampLeftToolbar: NSMenuDelegate {
- func menuWillOpen(_ menu: NSMenu) {
- if menu == self.peopleBox.menu {
-
- } else if menu == self.dateBox.menu {
- if self.lastDateItem == nil {
- self.lastDateItem = self.dateBox.menu?.items.first
- }
- if self.lastDateItem != nil {
- self.dateMenuItemClick(self.lastDateItem!)
- }
- }
-
-
- }
- func menuDidClose(_ menu: NSMenu) {
- if menu == self.peopleBox.menu {
- if self.lastIDItem == nil {
- self.toolbarType = .move
- self.reloadData()
- }
- }
- }
-
-
- }
|