// // KMConvertSettingLimitTipView.swift // PDF Master // // Created by tangchao on 2023/6/26. // import Cocoa class KMConvertSettingLimitTipView: NSView { private var _contentView = NSView() private var _iconIv = NSImageView() private var _textField = NSTextField(labelWithString: "") private var _funcBox = NSBox() private var _funcButtonVC: KMDesignButton? var contentView: NSView { get { return self._contentView } } var itemClick: KMCommonClickBlock? override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.initSubViews() self.initDefaultValue() } required init?(coder: NSCoder) { super.init(coder: coder) self.initSubViews() self.initDefaultValue() } func initSubViews() { self.addSubview(self.contentView) self.contentView.addSubview(self._iconIv) self.contentView.addSubview(self._textField) self.contentView.addSubview(self._funcBox) self._funcButtonVC = KMDesignButton(withType: .Text) self._funcBox.contentView = self._funcButtonVC!.view self._funcButtonVC?.stringValue = NSLocalizedString("Unlock", comment: "") self._funcButtonVC?.button(type: .Text, size: .m) self._funcButtonVC?.target = self self._funcButtonVC?.action = #selector(funcAction) let _ = self.contentView.km_add_leading_constraint(attribute: .leading, constant: 0) let _ = self.contentView.km_add_trailing_constraint(attribute: .trailing, constant: 0) let _ = self.contentView.km_add_top_constraint(attribute: .top, constant: 0) let _ = self.contentView.km_add_bottom_constraint(attribute: .bottom, constant: 0) let _ = self._iconIv.km_add_leading_constraint(attribute: .leading, constant: 10) let _ = self._iconIv.km_add_centerY_constraint(attribute: .centerY, constant: 0) let _ = self._textField.km_add_leading_constraint(equalTo: self._iconIv, attribute: .trailing, constant: 5) let _ = self._textField.km_add_centerY_constraint(equalTo: self._iconIv, attribute: .centerY, constant: 0) let _ = self._funcBox.km_add_leading_constraint(equalTo: self._textField, attribute: .trailing, constant: 0) let _ = self._funcBox.km_add_centerY_constraint(equalTo: self._iconIv, attribute: .centerY, constant: 0) let _ = self._funcBox.km_add_width_constraint(attribute: .width, constant: 60) let _ = self._funcBox.km_add_height_constraint(attribute: .height, constant: 30) // self._funcBox.km_add_centerY_constraint(attribute: .left, constant: 0) } func initDefaultValue() { self._iconIv.image = NSImage(named: "KMImageNameTipNormal") self._textField.stringValue = NSLocalizedString("Convert the first 10 pages for free", comment: "") self._textField.font = .SFProTextRegular(12) self._textField.textColor = KMAppearance.titleColor() self._funcBox.boxType = .custom self._funcBox.titlePosition = .noTitle self._funcBox.borderWidth = 0.0 } @objc private func funcAction() { guard let callback = self.itemClick else { return } callback(1) } } extension NSView { public func km_add_leading_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { let constraint = self.km_leading_constraint(equalTo: targetV, attribute: a, constant: c) if let _constraint = constraint { self.superview?.addConstraint(_constraint) } return constraint } public func km_add_trailing_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { let constraint = self.km_trailing_constraint(equalTo: targetV, attribute: a, constant: c) if let _constraint = constraint { self.superview?.addConstraint(_constraint) } return constraint } public func km_add_left_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { let constraint = self.km_left_constraint(equalTo: targetV, attribute: a, constant: c) if let _constraint = constraint { self.superview?.addConstraint(_constraint) } return constraint } public func km_add_right_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { let constraint = self.km_right_constraint(equalTo: targetV, attribute: a, constant: c) if let _constraint = constraint { self.superview?.addConstraint(_constraint) } return constraint } public func km_add_top_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { let constraint = self.km_top_constraint(equalTo: targetV, attribute: a, constant: c) if let _constraint = constraint { self.superview?.addConstraint(_constraint) } return constraint } public func km_add_bottom_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { let constraint = self.km_bottom_constraint(equalTo: targetV, attribute: a, constant: c) if let _constraint = constraint { self.superview?.addConstraint(_constraint) } return constraint } public func km_add_width_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint { let constraint = self.km_width_constraint(equalTo: targetV, attribute: a, constant: c) if let _ = targetV { self.superview?.addConstraint(constraint) } else { self.addConstraint(constraint) } return constraint } public func km_add_height_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint { let constraint = self.km_height_constraint(equalTo: targetV, attribute: a, constant: c) if let _ = targetV { self.superview?.addConstraint(constraint) } else { self.addConstraint(constraint) } return constraint } public func km_add_centerX_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { let constraint = self.km_centerX_constraint(equalTo: targetV, attribute: a, constant: c) if let _constraint = constraint { self.superview?.addConstraint(_constraint) } return constraint } public func km_add_centerY_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { let constraint = self.km_centerY_constraint(equalTo: targetV, attribute: a, constant: c) if let _constraint = constraint { self.superview?.addConstraint(_constraint) } return constraint } // MARK: - NSLayoutXAxisAnchor public func km_leading_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { if (!self._km_layout_is_x_anchor(attribute: a)) { Swift.debugPrint(a) #if DEBUG assert(false, "need NSLayoutXAxisAnchor. eg leading | trailing | left | right | centerX.") #endif return nil } self._km_layout_setup() var _targetV: NSView? if let _ = targetV { _targetV = targetV } else { _targetV = self.superview } var constraint: NSLayoutConstraint? if (a == .leading) { constraint = self.leadingAnchor.constraint(equalTo: _targetV!.leadingAnchor, constant: c) } else if (a == .trailing) { constraint = self.leadingAnchor.constraint(equalTo: _targetV!.trailingAnchor, constant: c) } else if (a == .left) { constraint = self.leadingAnchor.constraint(equalTo: _targetV!.leftAnchor, constant: c) } else if (a == .right) { constraint = self.leadingAnchor.constraint(equalTo: _targetV!.rightAnchor, constant: c) } else if (a == .centerX) { constraint = self.leadingAnchor.constraint(equalTo: _targetV!.centerXAnchor, constant: c) } return constraint! } public func km_trailing_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { if (!self._km_layout_is_x_anchor(attribute: a)) { Swift.debugPrint(a) #if DEBUG assert(false, "need NSLayoutXAxisAnchor. eg leading | trailing | left | right | centerX.") #endif return nil } self._km_layout_setup() var _targetV: NSView? if let _ = targetV { _targetV = targetV } else { _targetV = self.superview } var constraint: NSLayoutConstraint? if (a == .leading) { constraint = self.trailingAnchor.constraint(equalTo: _targetV!.leadingAnchor, constant: c) } else if (a == .trailing) { constraint = self.trailingAnchor.constraint(equalTo: _targetV!.trailingAnchor, constant: c) } else if (a == .left) { constraint = self.trailingAnchor.constraint(equalTo: _targetV!.leftAnchor, constant: c) } else if (a == .right) { constraint = self.trailingAnchor.constraint(equalTo: _targetV!.rightAnchor, constant: c) } else if (a == .centerX) { constraint = self.trailingAnchor.constraint(equalTo: _targetV!.centerXAnchor, constant: c) } return constraint } public func km_left_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { if (!self._km_layout_is_x_anchor(attribute: a)) { Swift.debugPrint(a) #if DEBUG assert(false, "need NSLayoutXAxisAnchor. eg leading | trailing | left | right | centerX.") #endif return nil } self._km_layout_setup() var _targetV: NSView? if let _ = targetV { _targetV = targetV } else { _targetV = self.superview } var constraint: NSLayoutConstraint? if (a == .leading) { constraint = self.leftAnchor.constraint(equalTo: _targetV!.leadingAnchor, constant: c) } else if (a == .trailing) { constraint = self.leftAnchor.constraint(equalTo: _targetV!.trailingAnchor, constant: c) } else if (a == .left) { constraint = self.leftAnchor.constraint(equalTo: _targetV!.leftAnchor, constant: c) } else if (a == .right) { constraint = self.leftAnchor.constraint(equalTo: _targetV!.rightAnchor, constant: c) } else if (a == .centerX) { constraint = self.leftAnchor.constraint(equalTo: _targetV!.centerXAnchor, constant: c) } return constraint } public func km_right_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { if (!self._km_layout_is_x_anchor(attribute: a)) { Swift.debugPrint(a) #if DEBUG assert(false, "need NSLayoutXAxisAnchor. eg leading | trailing | left | right | centerX.") #endif return nil } self._km_layout_setup() var _targetV: NSView? if let _ = targetV { _targetV = targetV } else { _targetV = self.superview } var constraint: NSLayoutConstraint? if (a == .leading) { constraint = self.rightAnchor.constraint(equalTo: _targetV!.leadingAnchor, constant: c) } else if (a == .trailing) { constraint = self.rightAnchor.constraint(equalTo: _targetV!.trailingAnchor, constant: c) } else if (a == .left) { constraint = self.rightAnchor.constraint(equalTo: _targetV!.leftAnchor, constant: c) } else if (a == .right) { constraint = self.rightAnchor.constraint(equalTo: _targetV!.rightAnchor, constant: c) } else if (a == .centerX) { constraint = self.rightAnchor.constraint(equalTo: _targetV!.centerXAnchor, constant: c) } return constraint } public func km_centerX_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { if (!self._km_layout_is_x_anchor(attribute: a)) { Swift.debugPrint(a) #if DEBUG assert(false, "need NSLayoutXAxisAnchor. eg leading | trailing | left | right | centerX.") #endif return nil } self._km_layout_setup() var constraint: NSLayoutConstraint? var _targetV: NSView? if let _ = targetV { _targetV = targetV } else { _targetV = self.superview } if (a == .leading) { constraint = self.centerXAnchor.constraint(equalTo: _targetV!.leadingAnchor, constant: c) } else if (a == .trailing) { constraint = self.centerXAnchor.constraint(equalTo: _targetV!.trailingAnchor, constant: c) } else if (a == .left) { constraint = self.centerXAnchor.constraint(equalTo: _targetV!.leftAnchor, constant: c) } else if (a == .right) { constraint = self.centerXAnchor.constraint(equalTo: _targetV!.rightAnchor, constant: c) } else if (a == .centerX) { constraint = self.centerXAnchor.constraint(equalTo: _targetV!.centerXAnchor, constant: c) } return constraint } // MARK: - NSLayoutYAxisAnchor public func km_top_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { if (!self._km_layout_is_y_anchor(attribute: a)) { Swift.debugPrint(a) #if DEBUG assert(false, "need NSLayoutYAxisAnchor. eg top | bottom | centerY.") #endif return nil } self._km_layout_setup() var _targetV: NSView? if let _ = targetV { _targetV = targetV } else { _targetV = self.superview } var constraint: NSLayoutConstraint? if (a == .top) { constraint = self.topAnchor.constraint(equalTo: _targetV!.topAnchor, constant: c) } else if (a == .bottom) { constraint = self.topAnchor.constraint(equalTo: _targetV!.bottomAnchor, constant: c) } else if (a == .centerY) { constraint = self.topAnchor.constraint(equalTo: _targetV!.centerYAnchor, constant: c) } return constraint } public func km_bottom_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { if (!self._km_layout_is_y_anchor(attribute: a)) { Swift.debugPrint(a) #if DEBUG assert(false, "need NSLayoutYAxisAnchor. eg top | bottom | centerY.") #endif return nil } self._km_layout_setup() var _targetV: NSView? if let _ = targetV { _targetV = targetV } else { _targetV = self.superview } var constraint: NSLayoutConstraint? if (a == .top) { constraint = self.bottomAnchor.constraint(equalTo: _targetV!.topAnchor, constant: c) } else if (a == .bottom) { constraint = self.bottomAnchor.constraint(equalTo: _targetV!.bottomAnchor, constant: c) } else if (a == .centerY) { constraint = self.bottomAnchor.constraint(equalTo: _targetV!.centerYAnchor, constant: c) } return constraint } public func km_centerY_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint? { if (!self._km_layout_is_y_anchor(attribute: a)) { Swift.debugPrint(a) #if DEBUG assert(false, "need NSLayoutYAxisAnchor. eg top | bottom | centerY.") #endif return nil } self._km_layout_setup() var constraint: NSLayoutConstraint? var _targetV: NSView? if let _ = targetV { _targetV = targetV } else { _targetV = self.superview } if (a == .top) { constraint = self.centerYAnchor.constraint(equalTo: _targetV!.topAnchor, constant: c) } else if (a == .bottom) { constraint = self.centerYAnchor.constraint(equalTo: _targetV!.bottomAnchor, constant: c) } else if (a == .centerY) { constraint = self.centerYAnchor.constraint(equalTo: _targetV!.centerYAnchor, constant: c) } return constraint } // MARK: - NSLayoutDimension public func km_width_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint { self._km_layout_setup() var constraint: NSLayoutConstraint? if (a == .top) { // constraint = self.widthAnchor.constraint(equalTo: targetV!.topAnchor, constant: c) } else if (a == .bottom) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.bottomAnchor, constant: c) } else if (a == .leading) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.leadingAnchor, constant: c) } else if (a == .trailing) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.trailingAnchor, constant: c) } else if (a == .left) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.leftAnchor, constant: c) } else if (a == .right) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.rightAnchor, constant: c) } else if (a == .width) { if let _targetV = targetV { constraint = self.widthAnchor.constraint(equalTo: _targetV.widthAnchor, constant: c) } else { constraint = self.widthAnchor.constraint(equalToConstant: c) } } else if (a == .height) { if let _targetV = targetV { constraint = self.widthAnchor.constraint(equalTo: _targetV.heightAnchor, constant: c) } else { constraint = self.widthAnchor.constraint(equalToConstant: c) } } else if (a == .centerX) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.centerXAnchor, constant: c) } else if (a == .centerY) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.centerYAnchor, constant: c) } return constraint! } public func km_height_constraint(equalTo targetV: NSView? = nil, attribute a: NSLayoutConstraint.Attribute, constant c: CGFloat) -> NSLayoutConstraint { self._km_layout_setup() var constraint: NSLayoutConstraint? if (a == .top) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.topAnchor, constant: c) } else if (a == .bottom) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.bottomAnchor, constant: c) } else if (a == .leading) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.leadingAnchor, constant: c) } else if (a == .trailing) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.trailingAnchor, constant: c) } else if (a == .left) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.leftAnchor, constant: c) } else if (a == .right) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.rightAnchor, constant: c) } else if (a == .width) { if let _targetV = targetV { constraint = self.heightAnchor.constraint(equalTo: _targetV.widthAnchor, constant: c) } else { constraint = self.heightAnchor.constraint(equalToConstant: c) } } else if (a == .height) { if let _targetV = targetV { constraint = self.heightAnchor.constraint(equalTo: _targetV.heightAnchor, constant: c) } else { constraint = self.heightAnchor.constraint(equalToConstant: c) } } else if (a == .centerX) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.centerXAnchor, constant: c) } else if (a == .centerY) { // constraint = self.widthAnchor.constraint(equalTo: _targetV!.centerYAnchor, constant: c) } return constraint! } // MARK: - Private Methods private func _km_layout_setup() { self.translatesAutoresizingMaskIntoConstraints = false } private func _km_layout_is_x_anchor(attribute a: NSLayoutConstraint.Attribute) -> Bool { if (a == .leading || a == .trailing || a == .left || a == .right || a == .centerX) { return true } return false } private func _km_layout_is_y_anchor(attribute a: NSLayoutConstraint.Attribute) -> Bool { if (a == .top || a == .bottom || a == .centerY) { return true } return false } private func _km_layout_is_dimension_anchor(attribute a: NSLayoutConstraint.Attribute) -> Bool { if (a == .width || a == .height) { return true } return false } }