// // 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() } } } }