1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- //
- // KMEditPDFToolbarView.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2024/6/25.
- //
- import Cocoa
- @objc protocol KMEditPDFToolbarViewDelegate: NSObjectProtocol {
- @objc optional func numberOfItems(in toolbarView: KMEditPDFToolbarView) -> Int
- @objc optional func toolbarView(_ toolbarView: KMEditPDFToolbarView, viewFor index: Int) -> NSView?
-
- @objc optional func toolbarView(_ toolbarView: KMEditPDFToolbarView, sizeForItemAt index: Int) -> NSSize
- // @objc optional func toolbarView(_ toolbarView: KMEditPDFToolbarView, insetForSectionAt section: Int) -> NSEdgeInsets
- // @objc optional func toolbarView(_ toolbarView: KMEditPDFToolbarView, minimumLineSpacingForSectionAt section: Int) -> CGFloat
- // @objc optional func toolbarView(_ toolbarView: KMEditPDFToolbarView, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
- }
- class KMEditPDFToolbarView: NSView {
- weak var delegate: KMEditPDFToolbarViewDelegate?
-
- var itemViews: [NSView] = []
-
- convenience init() {
- self.init(frame: .zero)
- }
-
- var inset: NSEdgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) {
- didSet {
- self.needsLayout = true
- }
- }
-
- func reloadData() {
- for sv in self.subviews {
- sv.removeFromSuperview()
- }
- self.itemViews.removeAll()
-
- guard let num = self.delegate?.numberOfItems?(in: self) else {
- return
- }
- for i in 0 ..< num {
- if let view = self.delegate?.toolbarView?(self, viewFor: i) {
- self.addSubview(view)
- self.itemViews.append(view)
- }
- }
-
- self.needsLayout = true
- }
-
- override func layout() {
- super.layout()
-
- let height = NSHeight(self.bounds)
-
- let leftMargin = self.inset.left
- let topMargin = self.inset.top
- var x = leftMargin
- let vSpace: CGFloat = 3
- for (i, sv) in self.subviews.enumerated() {
- if let size = self.delegate?.toolbarView?(self, sizeForItemAt: i) {
- let y = topMargin
- var frame = NSRect(x: x, y: y, width: size.width, height: size.height)
- x += (size.width + vSpace)
- sv.frame = frame
- }
- }
- }
- }
|