<commit_before>//
//  TabItemView.swift
//  WWDC
//
//  Created by <NAME> on 22/04/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Cocoa

extension NSStackView {
    
    var computedContentSize: CGSize {
        switch orientation {
        case .horizontal:
            let height = arrangedSubviews.map({ $0.bounds.height }).max() ?? CGFloat(0)
            let width = arrangedSubviews.reduce(CGFloat(0), { $0 + $1.intrinsicContentSize.width + self.spacing })
            
            return CGSize(width: width - spacing, height: height)
        case .vertical:
            let width = arrangedSubviews.map({ $0.bounds.width }).max() ?? CGFloat(0)
            let height = arrangedSubviews.reduce(CGFloat(0), { $0 + $1.intrinsicContentSize.height + self.spacing })
            
            return CGSize(width: width, height: height - spacing)
        }
    }
    
}

final class TabItemView: NSView {
    
    var target: Any?
    var action: Selector?
    
    var controllerIdentifier: String = ""
    
    var title: String? {
        didSet {
            titleLabel.stringValue = title ?? ""
            titleLabel.sizeToFit()
            sizeToFit()
        }
    }
    
    var image: NSImage? {
        didSet {
            if state == NSOffState {
                imageView.image = image
                sizeToFit()
            }
        }
    }
    
    var alternateImage: NSImage? {
        didSet {
            if state == NSOnState {
                imageView.image = alternateImage
                sizeToFit()
            }
        }
    }
    
    var state: Int = NSOffState {
        didSet {
            if state == NSOnState {
                imageView.tintColor = .toolbarTintActive
                imageView.image = alternateImage ?? image
                titleLabel.textColor = .toolbarTintActive
                titleLabel.font = NSFont.systemFont(ofSize: 14, weight: NSFontWeightMedium)
            } else {
                imageView.tintColor = .toolbarTint
                imageView.image = image
                titleLabel.textColor = .toolbarTint
                titleLabel.font = NSFont.systemFont(ofSize: 14)
            }
        }
    }
    
    lazy var imageView: MaskImageView = {
        let v = MaskImageView()
        
        v.tintColor = .toolbarTint
        v.widthAnchor.constraint(equalToConstant: 20).isActive = true
        v.heightAnchor.constraint(equalToConstant: 15).isActive = true
        
        return v
    }()
    
    lazy var titleLabel: NSTextField = {
        let l = NSTextField(labelWithString: "")
        
        l.font = NSFont.systemFont(ofSize: 14)
        l.textColor = .toolbarTint
        l.cell?.backgroundStyle = .dark
        
        return l
    }()
    
    lazy var stackView: NSStackView = {
        let v = NSStackView(views: [self.imageView, self.titleLabel])
        
        v.orientation = .horizontal
        v.spacing = 5
        v.alignment = .centerY
        v.distribution = .equalCentering
        
        return v
    }()
    
    override var intrinsicContentSize: NSSize {
        get {
            var s = stackView.computedContentSize
            s.width += 12
            return s
        }
        set {
            
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        wantsLayer = true
        
        addSubview(stackView)
        stackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        stackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
    
    func sizeToFit() {
        frame = NSRect(x: frame.origin.x, y: frame.origin.y, width: intrinsicContentSize.width, height: intrinsicContentSize.height)
    }
    
    override func mouseDown(with event: NSEvent) {
        if let target = target, let action = action {
            NSApp.sendAction(action, to: target, from: self)
        }
    }
    
}
<commit_msg>Improved main tab items alignment and spacing<commit_after>//
//  TabItemView.swift
//  WWDC
//
//  Created by <NAME> on 22/04/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Cocoa

extension NSStackView {
    
    var computedContentSize: CGSize {
        switch orientation {
        case .horizontal:
            let height = arrangedSubviews.map({ $0.bounds.height }).max() ?? CGFloat(0)
            let width = arrangedSubviews.reduce(CGFloat(0), { $0 + $1.intrinsicContentSize.width + self.spacing })
            
            return CGSize(width: width - spacing, height: height)
        case .vertical:
            let width = arrangedSubviews.map({ $0.bounds.width }).max() ?? CGFloat(0)
            let height = arrangedSubviews.reduce(CGFloat(0), { $0 + $1.intrinsicContentSize.height + self.spacing })
            
            return CGSize(width: width, height: height - spacing)
        }
    }
    
}

final class TabItemView: NSView {
    
    var target: Any?
    var action: Selector?
    
    var controllerIdentifier: String = ""
    
    var title: String? {
        didSet {
            titleLabel.stringValue = title ?? ""
            titleLabel.sizeToFit()
            sizeToFit()
        }
    }
    
    var image: NSImage? {
        didSet {
            if state == NSOffState {
                imageView.image = image
                sizeToFit()
            }
        }
    }
    
    var alternateImage: NSImage? {
        didSet {
            if state == NSOnState {
                imageView.image = alternateImage
                sizeToFit()
            }
        }
    }
    
    var state: Int = NSOffState {
        didSet {
            if state == NSOnState {
                imageView.tintColor = .toolbarTintActive
                imageView.image = alternateImage ?? image
                titleLabel.textColor = .toolbarTintActive
                titleLabel.font = NSFont.systemFont(ofSize: 14, weight: NSFontWeightMedium)
            } else {
                imageView.tintColor = .toolbarTint
                imageView.image = image
                titleLabel.textColor = .toolbarTint
                titleLabel.font = NSFont.systemFont(ofSize: 14)
            }
        }
    }
    
    lazy var imageView: MaskImageView = {
        let v = MaskImageView()
        
        v.tintColor = .toolbarTint
        v.widthAnchor.constraint(equalToConstant: 20).isActive = true
        v.heightAnchor.constraint(equalToConstant: 15).isActive = true
        
        return v
    }()
    
    lazy var titleLabel: NSTextField = {
        let l = NSTextField(labelWithString: "")
        
        l.font = NSFont.systemFont(ofSize: 14)
        l.textColor = .toolbarTint
        l.cell?.backgroundStyle = .dark
        
        return l
    }()
    
    lazy var stackView: NSStackView = {
        let v = NSStackView(views: [self.imageView, self.titleLabel])
        
        v.orientation = .horizontal
        v.spacing = 10
        v.alignment = .centerY
        v.distribution = .equalCentering
        
        return v
    }()
    
    override var intrinsicContentSize: NSSize {
        get {
            var s = stackView.computedContentSize
            s.width += 29
            return s
        }
        set {
            
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        wantsLayer = true
        
        addSubview(stackView)
        stackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        stackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        
        titleLabel.centerYAnchor.constraint(equalTo: stackView.centerYAnchor, constant: -1).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
    
    func sizeToFit() {
        frame = NSRect(x: frame.origin.x, y: frame.origin.y, width: intrinsicContentSize.width, height: intrinsicContentSize.height)
    }
    
    override func mouseDown(with event: NSEvent) {
        if let target = target, let action = action {
            NSApp.sendAction(action, to: target, from: self)
        }
    }
    
}

{'fraction_non_alphanumeric': 0.09295808966861599, 'fraction_numerical': 0.008284600389863547, 'mean_word_length': 1.62688, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6921832', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2313, 'n_words': 505}