123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- //
- // KMPDFSecToolbarController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/10/8.
- //
- import Cocoa
- import KMComponentLibrary
- @objc protocol KMPDFSecToolbarControllerDelegate: AnyObject {
-
- @objc optional func kmPDFSecToolbarControllerDidItemClicked(_ controller: KMPDFSecToolbarController, _ property: Any)
-
- }
- class KMPDFSecToolbarController: NSViewController {
- @IBOutlet var contendBox: NSBox!
-
- @IBOutlet var infoContendView: NSView!
-
- @IBOutlet var rightContendView: NSView!
-
- @IBOutlet var moreToolsView: ComponentDropdownTool!
-
-
- @IBOutlet var contendViewWidthConst: NSLayoutConstraint!
- @IBOutlet var rightContendViewWidthConst: NSLayoutConstraint!
-
- private var propertys: [Any] = []
-
- private var rightPropertys: [Any] = []
-
- weak open var delegate: KMPDFSecToolbarControllerDelegate?
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
-
- setUpProperty()
- }
-
- func setUpProperty() {
- moreToolsView.properties = ComponentDropdownToolProperty(state: .normal, leftIcon: NSImage(named: "toolbar_doubleArrow_right"))
-
-
- }
-
-
-
- //MARK: - reload
- public func reloadMainPropertys(_ arr: [Any]) {
- propertys.removeAll()
-
- for item in arr {
- propertys.append(item)
- }
-
- reloadMainView()
- }
-
- public func reloadRightPropertys(_ arr: [Any]) {
- rightPropertys.removeAll()
-
- for item in arr {
- rightPropertys.append(item)
- }
-
- reloadRightView()
- }
-
- func reloadMainView() {
- let subviews = infoContendView.subviews
- for view in subviews {
- view.removeFromSuperview()
- }
-
- var itemXvalue: CGFloat = 0
- for property in propertys {
- if property is ComponentButtonProperty {
- let buttonProperty = (property as! ComponentButtonProperty)
- let button = ComponentButton.init()
- button.properties = buttonProperty
- if buttonProperty.onlyIcon {
- button.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, 28, 28)
- } else {
- button.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, button.properties.propertyInfo.viewWidth, 28)
- }
- button.setTarget(self, action: #selector(buttonClicked(_:)))
- infoContendView.addSubview(button)
-
- itemXvalue += CGRectGetWidth(button.frame)
- itemXvalue += 12
-
- } else if property is ComponentDividerProperty {
- let divider = ComponentDivider.init()
- divider.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-8, 1, 16)
- divider.properties = (property as! ComponentDividerProperty)
- infoContendView.addSubview(divider)
-
- itemXvalue += 1
- itemXvalue += 12
-
- } else if property is ComponentDropdownToolProperty {
- let dropdownTool = ComponentDropdownTool.init()
- dropdownTool.properties = property as! ComponentDropdownToolProperty
- dropdownTool.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-14, dropdownTool.properties.propertyInfo.viewWidth, 28)
- dropdownTool.delegate = self
- infoContendView.addSubview(dropdownTool)
-
- itemXvalue += CGRectGetWidth(dropdownTool.frame)
- itemXvalue += 12
-
- }
- }
- itemXvalue -= 12
-
- contendViewWidthConst.constant = itemXvalue
-
- }
-
- func reloadRightView() {
- let subviews = rightContendView.subviews
- for view in subviews {
- view.removeFromSuperview()
- }
-
- var itemXvalue: CGFloat = 0
- for property in rightPropertys {
- if property is ComponentButtonProperty {
- let buttonProperty = (property as! ComponentButtonProperty)
- let button = ComponentButton.init()
- button.properties = buttonProperty
- if buttonProperty.onlyIcon {
- button.frame = CGRectMake(itemXvalue, 0, 28, 28)
- } else {
- button.frame = CGRectMake(itemXvalue, 0, button.properties.propertyInfo.viewWidth, 28)
- }
- button.setTarget(self, action: #selector(buttonClicked(_:)))
- rightContendView.addSubview(button)
-
- itemXvalue += CGRectGetWidth(button.frame)
- itemXvalue += 12
-
- } else if property is ComponentDividerProperty {
- let divider = ComponentDivider.init()
- divider.frame = CGRectMake(itemXvalue, CGRectGetHeight(infoContendView.frame)/2-8, 1, 16)
- divider.properties = (property as! ComponentDividerProperty)
- rightContendView.addSubview(divider)
-
- itemXvalue += 1
- itemXvalue += 12
-
- }
- }
- itemXvalue -= 12
-
- rightContendViewWidthConst.constant = itemXvalue
-
- }
-
- func refreshMainview() {
- let subviews = infoContendView.subviews
- for view in subviews {
- if view is ComponentButton {
- (view as! ComponentButton).reloadData()
- } else if view is ComponentDropdownTool {
- (view as! ComponentDropdownTool).reloadData()
- }
- }
- }
-
- func refreshRightview() {
- let subviews = rightContendView.subviews
- for view in subviews {
- if view is ComponentButton {
- (view as! ComponentButton).reloadData()
- }
- }
- }
-
-
- //MARK: -
- @objc func buttonClicked(_ sender: ComponentButton) {
- delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, sender.properties)
-
- }
- }
- extension KMPDFSecToolbarController: ComponentDropdownToolDelegate {
- func componentDropdownToolDidClicked(_ view: ComponentDropdownTool, menuItem: ComponentMenuitemProperty?) {
- if let property = menuItem {
- property.itemSelected = false
-
- delegate?.kmPDFSecToolbarControllerDidItemClicked?(self, property)
- }
- }
- }
|