KMLinkWebView.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // KMLinkWebView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/18.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc public protocol KMLinkWebViewDelegate: AnyObject {
  10. @objc optional func kmLinkWebViewDidGo(_ view: KMLinkWebView, _ webString: String)
  11. }
  12. public class KMLinkWebView: BaseXibView {
  13. @IBOutlet var contendBox: NSBox!
  14. @IBOutlet var inputTextarea: ComponentTextarea!
  15. @IBOutlet var goButton: ComponentButton!
  16. weak open var delegate: KMLinkWebViewDelegate?
  17. //MARK: - func
  18. public override func draw(_ dirtyRect: NSRect) {
  19. super.draw(dirtyRect)
  20. // Drawing code here.
  21. }
  22. public required init?(coder decoder: NSCoder) {
  23. super.init(coder: decoder)
  24. }
  25. override init(frame frameRect: NSRect) {
  26. super.init(frame: frameRect)
  27. }
  28. public override func awakeFromNib() {
  29. super.awakeFromNib()
  30. self.setUpUI()
  31. }
  32. func setUpUI() {
  33. inputTextarea.properties = ComponentTextareaProperty(size: .m,
  34. state: .normal,
  35. isError: false,
  36. placeholderString: "http://www.pdfreaderpro.com",
  37. totalCount: -1,
  38. text: "",
  39. isDisabled:false)
  40. inputTextarea.delegate = self
  41. goButton.properties = ComponentButtonProperty(type: .default_tertiary,
  42. size: .s,
  43. buttonText: KMLocalizedString("Go"))
  44. goButton.setTarget(self, action: #selector(buttonClicked(_:)))
  45. goButton.isHidden = true
  46. }
  47. @objc func buttonClicked(_ sender: NSView) {
  48. delegate?.kmLinkWebViewDidGo?(self, inputTextarea.properties.text)
  49. }
  50. //MARK: - MouseEvent
  51. public override func mouseDown(with event: NSEvent) {
  52. super.mouseDown(with: event)
  53. self.window?.makeFirstResponder(nil)
  54. }
  55. }
  56. //MARK: - ComponentTextareaDelegate
  57. extension KMLinkWebView: ComponentTextareaDelegate {
  58. public func componentTextareaTextDidBeginEditing(_ view: ComponentTextarea) {
  59. }
  60. public func componentTextareaTextDidChange(_ view: ComponentTextarea) {
  61. if view.properties.text.count > 0 {
  62. goButton.isHidden = false
  63. } else {
  64. goButton.isHidden = true
  65. }
  66. }
  67. public func componentTextareaTextDidEndEditing(_ view: ComponentTextarea) {
  68. }
  69. }