<commit_before>//
//  LYTabBarCellView.swift
//  LYTabBarView
//
//  Created by <NAME> on 16/3/30.
//  Copyright © 2016年 <NAME>. All rights reserved.
//

import Foundation
import Cocoa

class LYTabItemView: NSView {
    private let titleView = NSTextField(frame: .zero)
    private var closeButton : LYHoverButton!

    var tabBarView : LYTabBarView!
    var tabViewItem : NSTabViewItem!

    // hover effect
    private var hovered = false
    private var trackingArea : NSTrackingArea?

    // style
    var xpadding : CGFloat = 4
    var ypadding : CGFloat = 2
    var closeButtonSize = NSSize(width: 16, height: 16)
    var backgroundColor = NSColor(white: 0.73, alpha: 1)
    var hoverBackgroundColor = NSColor(white: 0.70, alpha: 1)
    var selectedBackgroundColor = NSColor(white: 0.83, alpha: 1)
    var unselectedForegroundColor = NSColor(white: 0.4, alpha: 1)
    var closeButtonHoverBackgroundColor = NSColor(white: 0.65, alpha: 0.6)
    
    var title : NSString {
        get {
            return titleView.stringValue
        }
        set(newTitle) {
            titleView.stringValue = newTitle as String
            self.invalidateIntrinsicContentSize()
        }
    }
    
    // Drag and Drop
    var dragOffset : CGFloat?
    var isDragging = false
    var draggingView : NSImageView?
    var draggingViewLeadingConstraint : NSLayoutConstraint?
    
    func setupViews() {
        self.setContentHuggingPriority(240, forOrientation: .Vertical)
        
        titleView.translatesAutoresizingMaskIntoConstraints = false
        titleView.editable = false
        titleView.alignment = .Center
        titleView.bordered = false
        titleView.drawsBackground = false
        self.addSubview(titleView)
        let padding = xpadding*2+closeButtonSize.width
        titleView.trailingAnchor.constraintGreaterThanOrEqualToAnchor(self.trailingAnchor, constant: -padding).active = true
        titleView.leadingAnchor.constraintGreaterThanOrEqualToAnchor(self.leadingAnchor, constant: padding).active = true
        titleView.centerXAnchor.constraintEqualToAnchor(self.centerXAnchor).active = true
        titleView.topAnchor.constraintEqualToAnchor(self.topAnchor, constant: ypadding).active = true
        titleView.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor, constant: -ypadding).active = true
        
        closeButton = LYTabCloseButton(frame: .zero)
        closeButton.translatesAutoresizingMaskIntoConstraints = false
        closeButton.hoverBackgroundColor = closeButtonHoverBackgroundColor
        closeButton.target = self
        closeButton.action = #selector(closeTab)
        closeButton.heightAnchor.constraintEqualToConstant(closeButtonSize.height).active = true
        closeButton.widthAnchor.constraintEqualToConstant(closeButtonSize.width).active = true
        closeButton.hidden = true
        self.addSubview(closeButton)
        closeButton.trailingAnchor.constraintGreaterThanOrEqualToAnchor(self.titleView.leadingAnchor, constant: -xpadding).active = true
        closeButton.topAnchor.constraintEqualToAnchor(self.topAnchor, constant: ypadding).active = true
        closeButton.leadingAnchor.constraintEqualToAnchor(self.leadingAnchor, constant: xpadding).active = true
        closeButton.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor, constant: -ypadding).active = true
        
    }
    
    override var intrinsicContentSize: NSSize {
        var size = titleView.intrinsicContentSize
        size.height += ypadding * 2
        size.width += xpadding * 3 + closeButtonSize.width
        return size
    }
    
    convenience init(tabViewItem : NSTabViewItem) {
        self.init(frame: .zero)
        self.tabViewItem = tabViewItem
        if let tabViewItem = self.tabViewItem {
            self.title = tabViewItem.label
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupViews()
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setupViews()
    }
    
    override func drawRect(dirtyRect: NSRect) {
        if tabViewItem.tabState == .SelectedTab && !isDragging {
            selectedBackgroundColor.setFill()
            titleView.textColor = NSColor.textColor()
        } else {
            if hovered {
                hoverBackgroundColor.setFill()
            } else {
                backgroundColor.setFill()                
            }
            titleView.textColor = unselectedForegroundColor
        }
        NSRectFill(self.bounds)
        super.drawRect(dirtyRect)
    }
    
    override func mouseDown(theEvent: NSEvent) {
        self.tabBarView.selectTabViewItem(self.tabViewItem)
        
        // setup drag and drop
        setupDragAndDrop(theEvent)
    }
    
    override func updateTrackingAreas() {
        super.updateTrackingAreas()
        
        if let trackingArea = self.trackingArea {
            self.removeTrackingArea(trackingArea)
        }
        
        let options : NSTrackingAreaOptions = [.MouseMoved, .MouseEnteredAndExited, .ActiveAlways, .InVisibleRect]
        self.trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
        self.addTrackingArea(self.trackingArea!)
    }
    
    override func mouseEntered(theEvent: NSEvent) {<commit_msg>tuning color<commit_after>//
//  LYTabBarCellView.swift
//  LYTabBarView
//
//  Created by <NAME> on 16/3/30.
//  Copyright © 2016年 <NAME>. All rights reserved.
//

import Foundation
import Cocoa

class LYTabItemView: NSView {
    private let titleView = NSTextField(frame: .zero)
    private var closeButton : LYHoverButton!

    var tabBarView : LYTabBarView!
    var tabViewItem : NSTabViewItem!

    // hover effect
    private var hovered = false
    private var trackingArea : NSTrackingArea?

    // style
    var xpadding : CGFloat = 4
    var ypadding : CGFloat = 2
    var closeButtonSize = NSSize(width: 16, height: 16)
    var backgroundColor = NSColor(white: 0.73, alpha: 1)
    var hoverBackgroundColor = NSColor(white: 0.70, alpha: 1)
    var selectedBackgroundColor = NSColor(white: 0.83, alpha: 1)
    var unselectedForegroundColor = NSColor(white: 0.4, alpha: 1)
    var closeButtonHoverBackgroundColor = NSColor(white: 0.55, alpha: 0.3)
    
    var title : NSString {
        get {
            return titleView.stringValue
        }
        set(newTitle) {
            titleView.stringValue = newTitle as String
            self.invalidateIntrinsicContentSize()
        }
    }
    
    // Drag and Drop
    var dragOffset : CGFloat?
    var isDragging = false
    var draggingView : NSImageView?
    var draggingViewLeadingConstraint : NSLayoutConstraint?
    
    func setupViews() {
        self.setContentHuggingPriority(240, forOrientation: .Vertical)
        
        titleView.translatesAutoresizingMaskIntoConstraints = false
        titleView.editable = false
        titleView.alignment = .Center
        titleView.bordered = false
        titleView.drawsBackground = false
        self.addSubview(titleView)
        let padding = xpadding*2+closeButtonSize.width
        titleView.trailingAnchor.constraintGreaterThanOrEqualToAnchor(self.trailingAnchor, constant: -padding).active = true
        titleView.leadingAnchor.constraintGreaterThanOrEqualToAnchor(self.leadingAnchor, constant: padding).active = true
        titleView.centerXAnchor.constraintEqualToAnchor(self.centerXAnchor).active = true
        titleView.topAnchor.constraintEqualToAnchor(self.topAnchor, constant: ypadding).active = true
        titleView.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor, constant: -ypadding).active = true
        
        closeButton = LYTabCloseButton(frame: .zero)
        closeButton.translatesAutoresizingMaskIntoConstraints = false
        closeButton.hoverBackgroundColor = closeButtonHoverBackgroundColor
        closeButton.target = self
        closeButton.action = #selector(closeTab)
        closeButton.heightAnchor.constraintEqualToConstant(closeButtonSize.height).active = true
        closeButton.widthAnchor.constraintEqualToConstant(closeButtonSize.width).active = true
        closeButton.hidden = true
        self.addSubview(closeButton)
        closeButton.trailingAnchor.constraintGreaterThanOrEqualToAnchor(self.titleView.leadingAnchor, constant: -xpadding).active = true
        closeButton.topAnchor.constraintEqualToAnchor(self.topAnchor, constant: ypadding).active = true
        closeButton.leadingAnchor.constraintEqualToAnchor(self.leadingAnchor, constant: xpadding).active = true
        closeButton.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor, constant: -ypadding).active = true
        
    }
    
    override var intrinsicContentSize: NSSize {
        var size = titleView.intrinsicContentSize
        size.height += ypadding * 2
        size.width += xpadding * 3 + closeButtonSize.width
        return size
    }
    
    convenience init(tabViewItem : NSTabViewItem) {
        self.init(frame: .zero)
        self.tabViewItem = tabViewItem
        if let tabViewItem = self.tabViewItem {
            self.title = tabViewItem.label
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupViews()
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setupViews()
    }
    
    override func drawRect(dirtyRect: NSRect) {
        if tabViewItem.tabState == .SelectedTab && !isDragging {
            selectedBackgroundColor.setFill()
            titleView.textColor = NSColor.textColor()
        } else {
            if hovered {
                hoverBackgroundColor.setFill()
            } else {
                backgroundColor.setFill()
            }
            titleView.textColor = unselectedForegroundColor
        }
        NSRectFill(self.bounds)
        super.drawRect(dirtyRect)
    }
    
    override func mouseDown(theEvent: NSEvent) {
        self.tabBarView.selectTabViewItem(self.tabViewItem)
        
        // setup drag and drop
        setupDragAndDrop(theEvent)
    }
    
    override func updateTrackingAreas() {
        super.updateTrackingAreas()
        
        if let trackingArea = self.trackingArea {
            self.removeTrackingArea(trackingArea)
        }
        
        let options : NSTrackingAreaOptions = [.MouseMoved, .MouseEnteredAndExited, .ActiveAlways, .InVisibleRect]
        self.trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
        self.addTrackingArea(self.trackingArea!)
    }
    
    override func mouseEntered(theEvent: NSEvent) {
{'fraction_non_alphanumeric': 0.07964684887761811, 'fraction_numerical': 0.007701700009392317, 'mean_word_length': 2.6972222222222224, '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': '5804774', 'n_tokens_mistral': 3014, 'n_tokens_neox': 2933, 'n_words': 587}