LogViewController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // LogViewController.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2023/4/19.
  6. //
  7. import Cocoa
  8. let kMaxLogLength = 81920 as Int
  9. let kTitleBarHeight = 26 as CGFloat
  10. class LogViewController: NSViewController, NSWindowDelegate {
  11. @IBOutlet var m_scrollView:NSScrollView!;
  12. @IBOutlet var m_logTV:NSTextView!;
  13. @IBOutlet var m_clearBtn:NSButton!;
  14. @IBOutlet var m_window:NSWindow!;
  15. var m_logString = ""
  16. var m_isVisable = false;
  17. static var __logVC:LogViewController? = nil
  18. class func shared() -> LogViewController {
  19. if nil == __logVC {
  20. var objects : NSArray!
  21. Bundle.main.loadNibNamed("LogViewController", owner: nil, topLevelObjects: &objects)
  22. for tView in objects {
  23. if let tv = tView as? LogViewController {
  24. __logVC = tv
  25. }
  26. }
  27. }
  28. return __logVC!
  29. }
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. // Do view setup here.
  33. }
  34. ///
  35. func isVisable() -> Bool {
  36. return m_isVisable
  37. }
  38. // interface
  39. func appendLog(_ log:String?) {
  40. if (log != nil) {
  41. m_logString.append(log!);
  42. var showLog = m_logString
  43. if (showLog.lengthOfBytes(using: .utf8) > kMaxLogLength) {
  44. showLog = (m_logString as NSString).substring(from: m_logString.lengthOfBytes(using: .utf8) - kMaxLogLength);
  45. }
  46. let attString = NSAttributedString(string: showLog,
  47. attributes: [.foregroundColor : NSColor(white: 1, alpha: 1),
  48. .strokeColor : NSColor(white: 1, alpha: 1),
  49. .font:NSFont.systemFont(ofSize: 12)])
  50. let size = attString.boundingRect(with: NSMakeSize(self.m_logTV.frame.size.width, CGFLOAT_MAX))
  51. if (Thread.isMainThread) {
  52. self.m_logTV.string = "";
  53. self.m_logTV.textColor = NSColor.white
  54. self.m_logTV.textStorage?.insert(attString, at: 0)
  55. self.m_logTV.scrollRangeToVisible(NSMakeRange(self.m_logTV.string.count, 0))
  56. self.m_logTV.scroll(NSPoint(x: 0, y: -size.height))
  57. }else {
  58. DispatchQueue.main.sync {
  59. self.m_logTV.string = "";
  60. self.m_logTV.textColor = NSColor.white
  61. self.m_logTV.textStorage?.insert(attString, at: 0)
  62. self.m_logTV.scrollRangeToVisible(NSMakeRange(self.m_logTV.string.count, 0))
  63. }
  64. }
  65. }
  66. }
  67. func updateFrame(_ rect:CGRect) {
  68. let screenSize = NSScreen.main!.frame.size
  69. if (screenSize.width - CGRectGetMaxX(rect) > self.view.frame.size.width) {
  70. self.m_window.setFrame(CGRectMake(CGRectGetMaxX(rect), CGRectGetMinY(rect),
  71. self.view.frame.size.width,
  72. self.view.frame.size.height + kTitleBarHeight),
  73. display: self.m_window.isVisible);
  74. }else if (CGRectGetMinX(rect) > self.view.frame.size.width) {
  75. self.m_window.setFrame(CGRectMake(CGRectGetMinX(rect) - self.view.frame.size.width, CGRectGetMinY(rect),
  76. self.view.frame.size.width,
  77. self.view.frame.size.height + kTitleBarHeight),
  78. display: self.m_window.isVisible);
  79. }else {
  80. self.m_window.setFrame(CGRectMake((screenSize.width - self.view.frame.size.width)/2,
  81. (screenSize.height - self.view.frame.size.height + kTitleBarHeight)/2,
  82. self.view.frame.size.width,
  83. self.view.frame.size.height + kTitleBarHeight),
  84. display: self.m_window.isVisible);
  85. }
  86. }
  87. // IBAction
  88. @IBAction func clearLog(_ sender:NSButton) {
  89. m_logString = ""
  90. self.m_logTV.string = "";
  91. }
  92. @IBAction func showLogWindow(_ sender:AnyObject) {
  93. self.m_window.setIsVisible(true)
  94. m_isVisable = true;
  95. }
  96. @IBAction func hideLogWindow(_ sender:AnyObject) {
  97. self.m_window.setIsVisible(false)
  98. m_isVisable = false;
  99. }
  100. ///NSWindowDelegate
  101. @MainActor func windowShouldClose(_ sender: NSWindow) -> Bool {
  102. self.hideLogWindow(sender)
  103. return false
  104. }
  105. }