<commit_before>//
//  DesktopLyricsController.swift
//  LyricsX
//
//  Created by 邓翔 on 2017/2/4.
//  Copyright © 2017年 ddddxxx. All rights reserved.
//

import Cocoa
import SnapKit
import EasyPreference
import OpenCC

class DesktopLyricsWindowController: NSWindowController {
    
    override func windowDidLoad() {
        let visibleFrame = NSScreen.main()!.visibleFrame
        window?.setFrame(visibleFrame, display: true)
        window?.backgroundColor = .clear
        window?.isOpaque = false
        window?.ignoresMouseEvents = true
        window?.level = Int(CGWindowLevelForKey(.floatingWindow))
        if Preference[DisableLyricsWhenSreenShot] {
            window?.sharingType = .none
        }
    }
    
}

class DesktopLyricsViewController: NSViewController {
    
    @IBOutlet weak var lyricsView: KaraokeLyricsView!
    @IBOutlet weak var lyricsHeightConstraint: NSLayoutConstraint!
    
    private var chineseConverter: ChineseConverter?
    
    var currentLyricsPosition: TimeInterval = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        switch Preference[ChineseConversionIndex] {
        case 1:
            chineseConverter = ChineseConverter(option: [.simplify])
        case 2:
            chineseConverter = ChineseConverter(option: [.traditionalize])
        default:
            chineseConverter = nil
        }
        
        let dfs = UserDefaults.standard
        let transOpt = [NSValueTransformerNameBindingOption: NSValueTransformerName.keyedUnarchiveFromDataTransformerName]
        lyricsView.bind("fontName", to: dfs, withKeyPath: DesktopLyricsFontName.rawValue, options: nil)
        lyricsView.bind("fontSize", to: dfs, withKeyPath: DesktopLyricsFontSize.rawValue, options: nil)
        lyricsView.bind("textColor", to: dfs, withKeyPath: DesktopLyricsColor.rawValue, options: transOpt)
        lyricsView.bind("shadowColor", to: dfs, withKeyPath: DesktopLyricsShadowColor.rawValue, options: transOpt)
        
        lyricsHeightConstraint.constant = CGFloat(Preference[DesktopLyricsHeighFromDock])
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.displayLrc("")
            self.addObserver()
        }
    }
    
    private func addObserver() {
        NotificationCenter.default.addObserver(self, selector: #selector(handlePositionChange), name: .PositionChange, object: nil)
        
        Preference.subscribe(key: DesktopLyricsHeighFromDock) { change in
            self.lyricsHeightConstraint.constant = CGFloat(change.newValue)
        }
        
        Preference.subscribe(key: DesktopLyricsBackgroundColor) { change in
            self.lyricsView.fillColor = change.newValue
        }
        
        Preference.subscribe(key: DesktopLyricsShadowColor) { change in
            self.lyricsView.shadowColor = change.newValue
        }<commit_msg>Fix KaraokeLyricsView reverts its background on program restart.<commit_after>//
//  DesktopLyricsController.swift
//  LyricsX
//
//  Created by 邓翔 on 2017/2/4.
//  Copyright © 2017年 ddddxxx. All rights reserved.
//

import Cocoa
import SnapKit
import EasyPreference
import OpenCC

class DesktopLyricsWindowController: NSWindowController {
    
    override func windowDidLoad() {
        let visibleFrame = NSScreen.main()!.visibleFrame
        window?.setFrame(visibleFrame, display: true)
        window?.backgroundColor = .clear
        window?.isOpaque = false
        window?.ignoresMouseEvents = true
        window?.level = Int(CGWindowLevelForKey(.floatingWindow))
        if Preference[DisableLyricsWhenSreenShot] {
            window?.sharingType = .none
        }
    }
    
}

class DesktopLyricsViewController: NSViewController {
    
    @IBOutlet weak var lyricsView: KaraokeLyricsView!
    @IBOutlet weak var lyricsHeightConstraint: NSLayoutConstraint!
    
    private var chineseConverter: ChineseConverter?
    
    var currentLyricsPosition: TimeInterval = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        switch Preference[ChineseConversionIndex] {
        case 1:
            chineseConverter = ChineseConverter(option: [.simplify])
        case 2:
            chineseConverter = ChineseConverter(option: [.traditionalize])
        default:
            chineseConverter = nil
        }
        
        let dfs = UserDefaults.standard
        let transOpt = [NSValueTransformerNameBindingOption: NSValueTransformerName.keyedUnarchiveFromDataTransformerName]
        lyricsView.bind("fontName", to: dfs, withKeyPath: DesktopLyricsFontName.rawValue, options: nil)
        lyricsView.bind("fontSize", to: dfs, withKeyPath: DesktopLyricsFontSize.rawValue, options: nil)
        lyricsView.bind("textColor", to: dfs, withKeyPath: DesktopLyricsColor.rawValue, options: transOpt)
        lyricsView.bind("shadowColor", to: dfs, withKeyPath: DesktopLyricsShadowColor.rawValue, options: transOpt)
        lyricsView.bind("fillColor", to: dfs, withKeyPath: DesktopLyricsBackgroundColor.rawValue, options: transOpt)
        
        lyricsHeightConstraint.constant = CGFloat(Preference[DesktopLyricsHeighFromDock])
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.displayLrc("")
            self.addObserver()
        }
    }
    
    private func addObserver() {
        NotificationCenter.default.addObserver(self, selector: #selector(handlePositionChange), name: .PositionChange, object: nil)
        
        Preference.subscribe(key: DesktopLyricsHeighFromDock) { change in
            self.lyricsHeightConstraint.constant = CGFloat(change.newValue)
        }
        
        Preference.subscribe(key: DesktopLyricsBackgroundColor) { change in
            self.lyricsView.fillColor = change.newValue
        }
        
        Preference.subscribe(key: DesktopLyricsShadowColor) { change in
            self.lyricsView.shadowColor = change.newValue
        }
{'fraction_non_alphanumeric': 0.0827156334231806, 'fraction_numerical': 0.0047169811320754715, 'mean_word_length': 2.8753263707571803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4801', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1622, 'n_words': 328}