123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- //
- // KMToolbarPageInputItemView.swift
- // PDF Reader Pro
- //
- // Created by User-Tangchao on 2024/10/12.
- //
- import Cocoa
- class KMToolbarPageInputItemView: NSView {
- private lazy var contentView_: NSView = {
- let view = NSView()
- return view
- }()
-
- private lazy var inputBox_: NSBox = {
- let box = NSBox()
- box.boxType = .custom
- return box
- }()
-
- private lazy var inputTF_: NSTextField = {
- let view = NSTextField()
- view.formatter = NumberFormatter()
- view.drawsBackground = false
- view.isBordered = false
- view.focusRingType = .none
- view.delegate = self
- return view
- }()
-
- private lazy var numberLabel_: NSTextField = {
- let view = NSTextField(labelWithString: "")
- view.font = KMToolbarMainItemView.textFont
- view.textColor = KMAppearance.subtitleColor()
- return view
- }()
-
- private lazy var titleLabel_: NSTextField = {
- let view = NSTextField(labelWithString: NSLocalizedString("Page", comment: ""))
- view.font = KMToolbarMainItemView.textFont
- view.textColor = KMAppearance.subtitleColor()
- return view
- }()
-
- var totalNumber: Int = 0 {
- didSet {
- self.numberLabel_.stringValue = "/" + "\(self.totalNumber)"
- }
- }
-
- var currentPageIndex: Int = 0 {
- didSet {
- self.inputTF_.stringValue = "\(self.currentPageIndex)"
- }
- }
-
- var enterAction: ((String)->Void)?
-
- convenience init() {
- self.init(frame: NSMakeRect(0, 0, 80, 40))
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- self.initSubviews()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- self.initSubviews()
- }
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
- func initSubviews() {
- self.addSubview(self.contentView_)
-
- self.contentView_.addSubview(self.inputBox_)
- self.contentView_.addSubview(self.numberLabel_)
- self.contentView_.addSubview(self.titleLabel_)
-
- self.inputBox_.contentView?.addSubview(self.inputTF_)
-
- self.contentView_.km_add_inset_constraint()
- self.inputBox_.km_add_leading_constraint(constant: 2)
- self.inputBox_.km_add_top_constraint(constant: 2)
- self.inputBox_.km_add_width_constraint(constant: 45)
- self.inputBox_.km_add_height_constraint(constant: 22)
- self.numberLabel_.km_add_leading_constraint(equalTo: self.inputBox_, attribute: .trailing, constant: 2)
- self.numberLabel_.km_add_centerY_constraint(equalTo: self.inputBox_)
- self.titleLabel_.km_add_top_constraint(equalTo: self.inputBox_, attribute: .bottom, constant: 2)
- self.titleLabel_.km_add_centerX_constraint()
-
- self.inputTF_.km_add_leading_constraint()
- self.inputTF_.km_add_centerX_constraint()
- self.inputTF_.km_add_centerY_constraint()
- }
- }
- extension KMToolbarPageInputItemView: NSTextFieldDelegate {
- // func controlTextDidEndEditing(_ obj: Notification) {
- //
- // }
-
- func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
- switch commandSelector {
- case #selector(NSResponder.insertNewline(_:)):
- if let inputView = control as? NSTextField {
- if inputView == self.inputTF_ {
- self.enterAction?(self.inputTF_.stringValue)
- }
- }
- return true
- default:
- return false
- }
- }
- }
|