<commit_before>//
//  EVTWindow.swift
//  EventsUI
//
//  Created by <NAME> on 02/04/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa
import AVFoundation

open class EVTWindow: NSWindow {
    
    @IBInspectable @objc open var hidesTitlebar = true
    
    // MARK: - Initialization
    
    override init(contentRect: NSRect, styleMask style: NSWindowStyleMask, backing bufferingType: NSBackingStoreType, defer flag: Bool) {
        var effectiveStyle = style
        effectiveStyle.insert(.fullSizeContentView)
        
        super.init(contentRect: contentRect, styleMask: effectiveStyle, backing: bufferingType, defer: flag)
        
        applyCustomizations()
    }
    
    open override func awakeFromNib() {
        super.awakeFromNib()
        
        applyCustomizations()
    }
    
    // MARK: - Custom appearance
    
    open override var effectiveAppearance: NSAppearance {
        return NSAppearance(named: NSAppearanceNameVibrantDark)!
    }
    
    fileprivate var titlebarWidgets: [NSButton]? {
        return titlebarView?.subviews.flatMap { subview in
            guard subview.isKind(of: NSClassFromString("_NSThemeWidget")!) else { return nil }
            return subview as? NSButton
        }
    }
    
    fileprivate func appearanceForWidgets() -> NSAppearance? {
        if allowsPiPMode {
            return NSAppearance(appearanceNamed: "PiPZoom", bundle: Bundle(for: EVTWindow.self))
        } else {
            return NSAppearance(named: NSAppearanceNameAqua)
        }
    }
    
    fileprivate func applyAppearanceToWidgets() {
        let appearance = appearanceForWidgets()
        titlebarWidgets?.forEach { $0.appearance = appearance }
    }
    
    fileprivate var _storedTitlebarView: NSVisualEffectView?
    open var titlebarView: NSVisualEffectView? {
        guard _storedTitlebarView == nil else { return _storedTitlebarView }
        guard let containerClass = NSClassFromString("NSTitlebarContainerView") else { return nil }
        
        guard let containerView = contentView?.superview?.subviews.filter({ $0.isKind(of: containerClass) }).last else { return nil }
        
        guard let titlebar = containerView.subviews.filter({ $0.isKind(of: NSVisualEffectView.self) }).last as? NSVisualEffectView else { return nil }
        
        _storedTitlebarView = titlebar
        
        return _storedTitlebarView
    }
    
    fileprivate var titleTextField: NSTextField?
    fileprivate var titlebarSeparatorLayer: CALayer?
    fileprivate var titlebarGradientLayer: CAGradientLayer?
    
    fileprivate var fullscreenObserver: NSObjectProtocol?
    
    fileprivate func applyCustomizations(_ note: Notification? = nil) {
        titleVisibility = .hidden
        isMovableByWindowBackground = true
        
        titlebarView?.material = .ultraDark
        titlebarView?.state = .active
        
        installTitlebarGradientIfNeeded()
        installTitlebarSeparatorIfNeeded()
        installTitleTextFieldIfNeeded()
        
        installFullscreenObserverIfNeeded()
        
        applyAppearanceToWidgets()
    }
    
    fileprivate func installTitleTextFieldIfNeeded() {
        guard titleTextField == nil && titlebarView != nil else { return }
        
        titleTextField = NSTextField(frame: titlebarView!.bounds)
        titleTextField!.isEditable = false
        titleTextField!.isSelectable = false
        titleTextField!.drawsBackground = false
        titleTextField!.isBezeled = false
        titleTextField!.isBordered = false
        titleTextField!.stringValue = title
        titleTextField!.font = NSFont.titleBarFont(ofSize: 13.0)
        titleTextField!.textColor = NSColor(calibratedWhite: 0.9, alpha: 0.8)
        titleTextField!.alignment = .center
        titleTextField!.translatesAutoresizingMaskIntoConstraints = false
        titleTextField!.lineBreakMode = .byTruncatingMiddle
        titleTextField!.sizeToFit()
        
        titlebarView!.addSubview(titleTextField!)
        titleTextField!.centerYAnchor.constraint(equalTo: titlebarView!.centerYAnchor).isActive = true
        titleTextField!.centerXAnchor.constraint(equalTo: titlebarView!.centerXAnchor).isActive = true
        titleTextField!.leadingAnchor.constraint(greaterThanOrEqualTo: titlebarView!.leadingAnchor, constant: 67.0).isActive = true
        titleTextField!.setContentCompressionResistancePriority(0.1, for: .horizontal)
        
        titleTextField!.layer?.compositingFilter = "lightenBlendMode"
    }
    
    fileprivate func installTitlebarGradientIfNeeded() {
        guard titlebarGradientLayer == nil && titlebarView != nil else { return }
        
        titlebarGradientLayer = CAGradientLayer()
        titlebarGradientLayer!.colors = [NSColor(calibratedWhite: 0.0, alpha: 0.4).cgColor, NSColor.clear.cgColor]
        titlebarGradientLayer!.frame = titlebarView!.bounds
        titlebarGradientLayer!.autoresizingMask = [.layerWidthSizable, .layerHeightSizable]
        titlebarGradientLayer!.compositingFilter = "overlayBlendMode"
        titlebarView?.layer?.insertSublayer(titlebarGradientLayer!, at: 0)
    }
    
    fileprivate func installTitlebarSeparatorIfNeeded() {
        guard titlebarSeparatorLayer == nil && titlebarView != nil else { return }
        
        titlebarSeparatorLayer = CALayer()
        titlebarSeparatorLayer!.backgroundColor = NSColor(calibratedWhite: 0.0, alpha: 0.9).cgColor
        titlebarSeparatorLayer!.frame = CGRect(x: 0.0, y: 0.0, width: titlebarView!.bounds.width, height: 1.0)
        titlebarSeparatorLayer!.autoresizingMask = [.layerWidthSizable, .layerMinYMargin]
        titlebarView?.layer?.addSublayer(titlebarSeparatorLayer!)
    }
    
    fileprivate func installFullscreenObserverIfNeeded() {
        guard fullscreenObserver == nil else { return }
        
        let nc = NotificationCenter.default
        
        // the customizations (especially the title text field ones) have to be reapplied when entering and exiting fullscreen
        nc.addObserver(forName: NSNotification.Name.NSWindowDidEnterFullScreen, object: self, queue: nil, using: applyCustomizations)
        nc.addObserver(forName: NSNotification.Name.NSWindowDidExitFullScreen, object: self, queue: nil, using: applyCustomizations)
    }
    
    open override func makeKeyAndOrderFront(_ sender: Any?) {
        super.makeKeyAndOrderFront(sender)
        
        applyCustomizations()
    }
    
    // MARK: - Titlebar management
    
    public private(set) var titlebarCompanionViews = [NSView]()
    
    public func addTitlebarCompanion(view: NSView) {
        guard titlebarCompanionViews.index(of: view) == nil else { return }
        
        titlebarCompanionViews.append(view)
    }
    
    public func removeTitlebarCompanion(view: NSView) {
        guard let index = titlebarCompanionViews.index(of: view) else { return }
        
        titlebarCompanionViews.remove(at: index)
    }
    
    func hideTitlebar(_ animated: Bool = true) {
        setTitlebarOpacity(0.0, animated: animated)
    }
    
    func showTitlebar(_ animated: Bool = true) {
        setTitlebarOpacity(1.0, animated: animated)
    }
    
    fileprivate func setTitlebarOpacity(_ opacity: CGFloat, animated: Bool) {
        guard hidesTitlebar else { return }
        
        // when the window is in full screen, the titlebar view is in another window (the "toolbar window")
        guard titlebarView?.window == self else { return }
        
        NSAnimationContext.runAnimationGroup({ ctx in
            ctx.duration = animated ? 0.4 : 0.0
            self.titlebarView?.animator().alphaValue = opacity
            self.titlebarCompanionViews.forEach({ $0.animator().alphaValue = opacity })
            }, completionHandler: nil)
    }
    <commit_msg>Using a less suspicious way to customize window buttons suggested by @jnadeau<commit_after>//
//  EVTWindow.swift
//  EventsUI
//
//  Created by <NAME> on 02/04/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa
import AVFoundation

open class EVTWindow: NSWindow {
    
    @IBInspectable @objc open var hidesTitlebar = true
    
    // MARK: - Initialization
    
    override init(contentRect: NSRect, styleMask style: NSWindowStyleMask, backing bufferingType: NSBackingStoreType, defer flag: Bool) {
        var effectiveStyle = style
        effectiveStyle.insert(.fullSizeContentView)
        
        super.init(contentRect: contentRect, styleMask: effectiveStyle, backing: bufferingType, defer: flag)
        
        applyCustomizations()
    }
    
    open override func awakeFromNib() {
        super.awakeFromNib()
        
        applyCustomizations()
    }
    
    // MARK: - Custom appearance
    
    open override var effectiveAppearance: NSAppearance {
        return NSAppearance(named: NSAppearanceNameVibrantDark)!
    }
    
    fileprivate var titlebarWidgets = Set<NSButton>()
    
    fileprivate func appearanceForWidgets() -> NSAppearance? {
        if allowsPiPMode {
            return NSAppearance(appearanceNamed: "PiPZoom", bundle: Bundle(for: EVTWindow.self))
        } else {
            return NSAppearance(named: NSAppearanceNameAqua)
        }
    }
    
    fileprivate func applyAppearanceToWidgets() {
        let appearance = appearanceForWidgets()
        titlebarWidgets.forEach { $0.appearance = appearance }
    }
    
    fileprivate var _storedTitlebarView: NSVisualEffectView?
    open var titlebarView: NSVisualEffectView? {
        guard _storedTitlebarView == nil else { return _storedTitlebarView }
        guard let containerClass = NSClassFromString("NSTitlebarContainerView") else { return nil }
        
        guard let containerView = contentView?.superview?.subviews.filter({ $0.isKind(of: containerClass) }).last else { return nil }
        
        guard let titlebar = containerView.subviews.filter({ $0.isKind(of: NSVisualEffectView.self) }).last as? NSVisualEffectView else { return nil }
        
        _storedTitlebarView = titlebar
        
        return _storedTitlebarView
    }
    
    fileprivate var titleTextField: NSTextField?
    fileprivate var titlebarSeparatorLayer: CALayer?
    fileprivate var titlebarGradientLayer: CAGradientLayer?
    
    fileprivate var fullscreenObserver: NSObjectProtocol?
    
    fileprivate func applyCustomizations(_ note: Notification? = nil) {
        titleVisibility = .hidden
        isMovableByWindowBackground = true
        
        titlebarView?.material = .ultraDark
        titlebarView?.state = .active
        
        installTitlebarGradientIfNeeded()
        installTitlebarSeparatorIfNeeded()
        installTitleTextFieldIfNeeded()
        
        installFullscreenObserverIfNeeded()
        
        applyAppearanceToWidgets()
    }
    
    fileprivate func installTitleTextFieldIfNeeded() {
        guard titleTextField == nil && titlebarView != nil else { return }
        
        titleTextField = NSTextField(frame: titlebarView!.bounds)
        titleTextField!.isEditable = false
        titleTextField!.isSelectable = false
        titleTextField!.drawsBackground = false
        titleTextField!.isBezeled = false
        titleTextField!.isBordered = false
        titleTextField!.stringValue = title
        titleTextField!.font = NSFont.titleBarFont(ofSize: 13.0)
        titleTextField!.textColor = NSColor(calibratedWhite: 0.9, alpha: 0.8)
        titleTextField!.alignment = .center
        titleTextField!.translatesAutoresizingMaskIntoConstraints = false
        titleTextField!.lineBreakMode = .byTruncatingMiddle
        titleTextField!.sizeToFit()
        
        titlebarView!.addSubview(titleTextField!)
        titleTextField!.centerYAnchor.constraint(equalTo: titlebarView!.centerYAnchor).isActive = true
        titleTextField!.centerXAnchor.constraint(equalTo: titlebarView!.centerXAnchor).isActive = true
        titleTextField!.leadingAnchor.constraint(greaterThanOrEqualTo: titlebarView!.leadingAnchor, constant: 67.0).isActive = true
        titleTextField!.setContentCompressionResistancePriority(0.1, for: .horizontal)
        
        titleTextField!.layer?.compositingFilter = "lightenBlendMode"
    }
    
    fileprivate func installTitlebarGradientIfNeeded() {
        guard titlebarGradientLayer == nil && titlebarView != nil else { return }
        
        titlebarGradientLayer = CAGradientLayer()
        titlebarGradientLayer!.colors = [NSColor(calibratedWhite: 0.0, alpha: 0.4).cgColor, NSColor.clear.cgColor]
        titlebarGradientLayer!.frame = titlebarView!.bounds
        titlebarGradientLayer!.autoresizingMask = [.layerWidthSizable, .layerHeightSizable]
        titlebarGradientLayer!.compositingFilter = "overlayBlendMode"
        titlebarView?.layer?.insertSublayer(titlebarGradientLayer!, at: 0)
    }
    
    fileprivate func installTitlebarSeparatorIfNeeded() {
        guard titlebarSeparatorLayer == nil && titlebarView != nil else { return }
        
        titlebarSeparatorLayer = CALayer()
        titlebarSeparatorLayer!.backgroundColor = NSColor(calibratedWhite: 0.0, alpha: 0.9).cgColor
        titlebarSeparatorLayer!.frame = CGRect(x: 0.0, y: 0.0, width: titlebarView!.bounds.width, height: 1.0)
        titlebarSeparatorLayer!.autoresizingMask = [.layerWidthSizable, .layerMinYMargin]
        titlebarView?.layer?.addSublayer(titlebarSeparatorLayer!)
    }
    
    fileprivate func installFullscreenObserverIfNeeded() {
        guard fullscreenObserver == nil else { return }
        
        let nc = NotificationCenter.default
        
        // the customizations (especially the title text field ones) have to be reapplied when entering and exiting fullscreen
        nc.addObserver(forName: NSNotification.Name.NSWindowDidEnterFullScreen, object: self, queue: nil, using: applyCustomizations)
        nc.addObserver(forName: NSNotification.Name.NSWindowDidExitFullScreen, object: self, queue: nil, using: applyCustomizations)
    }
    
    open override func makeKeyAndOrderFront(_ sender: Any?) {
        super.makeKeyAndOrderFront(sender)
        
        applyCustomizations()
    }
    
    // MARK: - Titlebar management
    
    public private(set) var titlebarCompanionViews = [NSView]()
    
    public func addTitlebarCompanion(view: NSView) {
        guard titlebarCompanionViews.index(of: view) == nil else { return }
        
        titlebarCompanionViews.append(view)
    }
    
    public func removeTitlebarCompanion(view: NSView) {
        guard let index = titlebarCompanionViews.index(of: view) else { return }
        
        titlebarCompanionViews.remove(at: index)
    }
    
    func hideTitlebar(_ animated: Bool = true) {
        setTitlebarOpacity(0.0, animated: animated)
    }
    
    func showTitlebar(_ animated: Bool = true) {
        setTitlebarOpacity(1.0, animated: animated)
    }
    
    fileprivate func setTitlebarOpacity(_ opacity: CGFloat, animated: Bool) {
        guard hidesTitlebar else { return }
        
        // when the window is in full screen, the titlebar view is in another window (the "toolbar window")
        guard titlebarView?.window == self else { return }
        
        NSAnimationContext.runAnimationGroup({ ctx in
            ctx.duration = animated ? 0.4 : 0.0
            self.titlebarView?.animator().alphaValue = opacity
            self.titlebarCompanionViews.forEach({ $0.animator().alphaValue = opacity })
            }, completionHandler: nil)
    }
    
    open override func standardWindowButton(_ b: NSWindowButton) -> NSButton? {
        guard let button = super.standardWindowButton(b) else { return nil }
        
        titlebarWidgets.insert(button)
        
        return button
    }
    
{'fraction_non_alphanumeric': 0.08175860533180633, 'fraction_numerical': 0.006235286632308965, 'mean_word_length': 2.8742913482869117, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 11, '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': '3289440', 'n_tokens_mistral': 4387, 'n_tokens_neox': 4217, 'n_words': 934}