<commit_before>//
//  PreferenceDisplayViewController.swift
//
//  This file is part of LyricsX
//  Copyright (C) 2017 <NAME> - https://github.com/ddddxxx/LyricsX
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import Cocoa

class PreferenceDisplayViewController: NSViewController, NSWindowDelegate {
    
    var karaokeFont = NSFont(name: defaults[.DesktopLyricsFontName], size: CGFloat(defaults[.DesktopLyricsFontSize])) ?? NSFont.labelFont(ofSize: CGFloat(defaults[.DesktopLyricsFontSize]))
    var hudFont = NSFont(name: defaults[.LyricsWindowFontName], size: CGFloat(defaults[.LyricsWindowFontSize])) ?? NSFont.labelFont(ofSize: CGFloat(defaults[.LyricsWindowFontSize]))
    
    // TODO: ugly code
    var isSettingKaraokeFont = true
    
    override func viewDidLoad() {
        _ = PreferenceDisplayViewController.swizzler
        super.viewDidLoad()
    }
    
    override func viewDidDisappear() {
        let fontManger = NSFontManager.shared
        if fontManger.target === self {
            fontManger.target = nil
            NSFontPanel.shared.close()
        }
    }
    
    override func changeFont(_ sender: Any?) {
        guard let manager = sender as? NSFontManager else {
            return
        }
        
        if isSettingKaraokeFont {
            karaokeFont = manager.convert(karaokeFont)
            var fallback = defaults[.DesktopLyricsFontNameFallback]
            if let index = fallback.index(of: defaults[.DesktopLyricsFontName]) {
                fallback.remove(at: index)
            }
            fallback.insert(defaults[.DesktopLyricsFontName], at: 0)
            defaults[.DesktopLyricsFontNameFallback] = Array(fallback.prefix(fontNameFallbackCountMax))
            defaults[.DesktopLyricsFontName] = karaokeFont.fontName
            defaults[.DesktopLyricsFontSize] = Int(karaokeFont.pointSize)
        } else {
            hudFont = manager.convert(hudFont)
            defaults[.LyricsWindowFontName] = hudFont.fontName
            defaults[.LyricsWindowFontSize] = Int(hudFont.pointSize)
        }
    }
    
    static let swizzler: () = {
        let cls = PreferenceDisplayViewController.self<commit_msg>fix: avoid overwriting font fallback with the same font family<commit_after>//
//  PreferenceDisplayViewController.swift
//
//  This file is part of LyricsX
//  Copyright (C) 2017 <NAME> - https://github.com/ddddxxx/LyricsX
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import Cocoa

class PreferenceDisplayViewController: NSViewController, NSWindowDelegate {
    
    var karaokeFont = NSFont(name: defaults[.DesktopLyricsFontName], size: CGFloat(defaults[.DesktopLyricsFontSize])) ?? NSFont.labelFont(ofSize: CGFloat(defaults[.DesktopLyricsFontSize]))
    var hudFont = NSFont(name: defaults[.LyricsWindowFontName], size: CGFloat(defaults[.LyricsWindowFontSize])) ?? NSFont.labelFont(ofSize: CGFloat(defaults[.LyricsWindowFontSize]))
    
    // TODO: ugly code
    var isSettingKaraokeFont = true
    
    override func viewDidLoad() {
        _ = PreferenceDisplayViewController.swizzler
        super.viewDidLoad()
    }
    
    override func viewDidDisappear() {
        let fontManger = NSFontManager.shared
        if fontManger.target === self {
            fontManger.target = nil
            NSFontPanel.shared.close()
        }
    }
    
    override func changeFont(_ sender: Any?) {
        guard let manager = sender as? NSFontManager else {
            return
        }
        
        if isSettingKaraokeFont {
            let previousFontFamily = karaokeFont.familyName
            karaokeFont = manager.convert(karaokeFont)
            if previousFontFamily != karaokeFont.familyName {
                // guarantee different font family of font fallback
                var fallback = defaults[.DesktopLyricsFontNameFallback]
                if let index = fallback.index(of: defaults[.DesktopLyricsFontName]) {
                    fallback.remove(at: index)
                }
                fallback.insert(defaults[.DesktopLyricsFontName], at: 0)
                defaults[.DesktopLyricsFontNameFallback] = Array(fallback.prefix(fontNameFallbackCountMax))
            }
            defaults[.DesktopLyricsFontName] = karaokeFont.fontName
            defaults[.DesktopLyricsFontSize] = Int(karaokeFont.pointSize)
        } else {
            hudFont = manager.convert(hudFont)
            defaults[.LyricsWindowFontName] = hudFont.fontName
            defaults[.LyricsWindowFontSize] = Int(hudFont.pointSize)
        }
    }
    
    static let swizzler: () = {
        let cls = PreferenceDisplayViewController.self
{'fraction_non_alphanumeric': 0.08671808054841473, 'fraction_numerical': 0.002056555269922879, 'mean_word_length': 2.7171974522292994, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '418726', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1595, 'n_words': 433}