123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- //
- // LogViewController.swift
- // KdanAuto
- //
- // Created by 朱东勇 on 2023/4/19.
- //
- import Cocoa
- let kMaxLogLength = 81920 as Int
- let kTitleBarHeight = 26 as CGFloat
- class LogViewController: NSViewController, NSWindowDelegate {
- @IBOutlet var m_scrollView:NSScrollView!;
- @IBOutlet var m_logTV:NSTextView!;
- @IBOutlet var m_clearBtn:NSButton!;
-
- @IBOutlet var m_window:NSWindow!;
- var m_logString = ""
- var m_isVisable = false;
-
- static var __logVC:LogViewController? = nil
- class func shared() -> LogViewController {
- if nil == __logVC {
- var objects : NSArray!
-
- Bundle.main.loadNibNamed("LogViewController", owner: nil, topLevelObjects: &objects)
-
- for tView in objects {
- if let tv = tView as? LogViewController {
- __logVC = tv
- }
- }
- }
-
- return __logVC!
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
- }
-
- ///
- func isVisable() -> Bool {
- return m_isVisable
- }
-
- // interface
- func appendLog(_ log:String?) {
- if (log != nil) {
- m_logString.append(log!);
-
- var showLog = m_logString
- if (showLog.lengthOfBytes(using: .utf8) > kMaxLogLength) {
- showLog = (m_logString as NSString).substring(from: m_logString.lengthOfBytes(using: .utf8) - kMaxLogLength);
- }
- let attString = NSAttributedString(string: showLog,
- attributes: [.foregroundColor : NSColor(white: 1, alpha: 1),
- .strokeColor : NSColor(white: 1, alpha: 1),
- .font:NSFont.systemFont(ofSize: 12)])
-
- let size = attString.boundingRect(with: NSMakeSize(self.m_logTV.frame.size.width, CGFLOAT_MAX))
- if (Thread.isMainThread) {
- self.m_logTV.string = "";
- self.m_logTV.textColor = NSColor.white
- self.m_logTV.textStorage?.insert(attString, at: 0)
- self.m_logTV.scrollRangeToVisible(NSMakeRange(self.m_logTV.string.count, 0))
- self.m_logTV.scroll(NSPoint(x: 0, y: -size.height))
- }else {
- DispatchQueue.main.sync {
- self.m_logTV.string = "";
- self.m_logTV.textColor = NSColor.white
- self.m_logTV.textStorage?.insert(attString, at: 0)
- self.m_logTV.scrollRangeToVisible(NSMakeRange(self.m_logTV.string.count, 0))
- }
- }
- }
- }
-
- func updateFrame(_ rect:CGRect) {
- let screenSize = NSScreen.main!.frame.size
-
- if (screenSize.width - CGRectGetMaxX(rect) > self.view.frame.size.width) {
- self.m_window.setFrame(CGRectMake(CGRectGetMaxX(rect), CGRectGetMinY(rect),
- self.view.frame.size.width,
- self.view.frame.size.height + kTitleBarHeight),
- display: self.m_window.isVisible);
- }else if (CGRectGetMinX(rect) > self.view.frame.size.width) {
- self.m_window.setFrame(CGRectMake(CGRectGetMinX(rect) - self.view.frame.size.width, CGRectGetMinY(rect),
- self.view.frame.size.width,
- self.view.frame.size.height + kTitleBarHeight),
- display: self.m_window.isVisible);
- }else {
- self.m_window.setFrame(CGRectMake((screenSize.width - self.view.frame.size.width)/2,
- (screenSize.height - self.view.frame.size.height + kTitleBarHeight)/2,
- self.view.frame.size.width,
- self.view.frame.size.height + kTitleBarHeight),
- display: self.m_window.isVisible);
- }
- }
-
- // IBAction
- @IBAction func clearLog(_ sender:NSButton) {
- m_logString = ""
- self.m_logTV.string = "";
- }
-
- @IBAction func showLogWindow(_ sender:AnyObject) {
- self.m_window.setIsVisible(true)
- m_isVisable = true;
- }
-
- @IBAction func hideLogWindow(_ sender:AnyObject) {
- self.m_window.setIsVisible(false)
- m_isVisable = false;
- }
-
- ///NSWindowDelegate
- @MainActor func windowShouldClose(_ sender: NSWindow) -> Bool {
- self.hideLogWindow(sender)
-
- return false
- }
-
- }
|