// // KMToolbarZoomItemView.swift // PDF Reader Pro // // Created by tangchao on 2023/11/28. // import Cocoa class KMToolbarZoomItemView: NSView { lazy var boxView: NSView = { let view = NSView() view.frame = NSMakeRect(0, 16, 70, 22) return view }() weak var zoomView: NSView? lazy var titleLabel: NSTextField = { let label = NSTextField(labelWithString: NSLocalizedString("Zoom", comment: "")) label.frame = NSMakeRect(0, 0, 70, 14) label.alignment = .right label.font = KMToolbarMainItemView.textFont label.textColor = KMAppearance.subtitleColor() return label }() lazy var zoomInButton: NSButton = { let button = KMCoverButton() button.wantsLayer = true button.layer?.cornerRadius = 4 button.isBordered = false button.title = "" button.image = NSImage(named: "KMImageNameUXIconToolbarZoominNor") button.target = self button.action = #selector(_buttonClicked) button.coverAction = { cbtn, caction in if caction == .enter { cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor } else if caction == .exit { cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor } } return button }() lazy var zoomOutButton: NSButton = { let button = KMCoverButton() button.isBordered = false button.wantsLayer = true button.layer?.cornerRadius = 4 button.title = "" button.image = NSImage(named: "KMImageNameUXIconToolbarZoomoutNor") button.target = self button.action = #selector(_buttonClicked) button.coverAction = { cbtn, caction in if caction == .enter { cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor } else if caction == .exit { cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor } } return button }() var callback: KMCommonClickBlock? convenience init(zoomView: NSView?) { let vspace: CGFloat = 2 let zoomButtonW: CGFloat = 20 self.init(frame: NSMakeRect(0, 0, 70+2*zoomButtonW+vspace, 40)) self.zoomView = zoomView self.addSubview(self.boxView) if let zview = self.zoomView { self.boxView.addSubview(zview) self.zoomView?.frame = NSMakeRect(10, 3, NSWidth(zview.frame), NSHeight(zview.frame)) } self.addSubview(self.titleLabel) self.addSubview(self.zoomInButton) self.addSubview(self.zoomOutButton) self.zoomOutButton.frame = NSMakeRect(CGRectGetMaxX(self.boxView.frame)+vspace, 17, zoomButtonW, zoomButtonW) self.zoomInButton.frame = NSMakeRect(CGRectGetMaxX(self.zoomOutButton.frame), 17, zoomButtonW, zoomButtonW) self.boxView.wantsLayer = true self.boxView.layer?.borderWidth = 1 self.boxView.layer?.borderColor = NSColor.lightGray.cgColor } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) } } // MARK: - Private Methods extension KMToolbarZoomItemView { @objc private func _buttonClicked(_ sender: NSButton) { var idx = 1 if self.zoomInButton.isEqual(to: sender) { idx = 2 } guard let block = self.callback else { return } block(idx) } }