<commit_before>//
//  USGScrollingTabCell.swift
//  ScrollingTabBar
//
//  Created by <NAME> on 16.07.04.
//  Copyright © 2015-2016 usagimaru. All rights reserved.
//

import UIKit

class USGScrollingTabCell: UICollectionViewCell {
	
	var index: Int = 0
	
	var normalString: NSAttributedString?
	var highlightedString: NSAttributedString?
	var selectedString: NSAttributedString?
	
	weak var collectionView: UICollectionView?
	weak var target: AnyObject?
	var buttonAction: Selector?
	
	override var isHighlighted: Bool {
		didSet {
			setNeedsLayout()
		}
	}
	
	override var isSelected: Bool {
		didSet {
			setNeedsLayout()
		}
	}
	
	@IBOutlet weak var label: UILabel!
	@IBOutlet weak var leftConstraint: NSLayoutConstraint!
	@IBOutlet weak var rightConstraint: NSLayoutConstraint!
	@IBOutlet weak var button: UIButton!
	
	fileprivate static var padding: UIEdgeInsets = UIEdgeInsets.zero
	
	
	override class func initialize() {
		super.initialize()
		
		if let cell = self.nib().instantiate(withOwner: nil, options: nil).first as? USGScrollingTabCell {
			padding = UIEdgeInsetsMake(0, cell.leftConstraint.constant, 0, cell.rightConstraint.constant)
		}
	}
	
	
	class func nib() -> UINib {
		return UINib(nibName: "USGScrollingTabCell", bundle: nil)
	}
	
	class func tabWidth(_ string: NSAttributedString, tabInset: CGFloat) -> CGFloat {
		// 文字列の必要な幅を計算
		let bounds = string.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
		                                         options: [.usesLineFragmentOrigin],
		                                         context: nil)
		
		// 余白をたす。繰上げしないと収まりきらない
		let w = max(ceil(bounds.size.width + padding.left + padding.right) + tabInset * 2.0, 1.0)
		
		return w
	}
	
	
	func setNormalStringWithoutAnimation(_ string: NSAttributedString) {
		normalString = string
		label.attributedText = string
	}
	
	
	override func layoutSubviews() {
		super.layoutSubviews()
		
		guard let collectionView = collectionView else {
			return
		}
		
		guard let indexPath = collectionView.indexPathsForSelectedItems!.first else {
			return
		}
		
		// TODO: fix animation
		setAttributedText(false, indexPath: indexPath)
	}
	
	fileprivate func setAttributedText(_ animated: Bool, indexPath: IndexPath) {
		var str: NSAttributedString? = nil
		
		if isHighlighted {
			str = highlightedString
		}
		else if isSelected == true && index == (indexPath as IndexPath).row {
			str = selectedString
		}
		else if (indexPath as IndexPath).row != index {
			str = normalString
		}
		
		if let str = str {
			label.attributedText = str
			let duration: TimeInterval = isHighlighted == true && animated == true ? 0.2 : 0.0
			UIView.transition(with: label,
			                          duration: duration,
			                          options: [.transitionCrossDissolve, .beginFromCurrentState],
			                          animations: {
										self.label.attributedText = str },
			                          completion: nil)
		}
	}
	
	
	@IBAction fileprivate func buttonAction(_ sender: AnyObject) {
		if let buttonAction = buttonAction {
			target?.perform(buttonAction, with: self)
		}
	}
}<commit_msg>remove caution on perform()<commit_after>//
//  USGScrollingTabCell.swift
//  ScrollingTabBar
//
//  Created by <NAME> on 16.07.04.
//  Copyright © 2015-2016 usagimaru. All rights reserved.
//

import UIKit

class USGScrollingTabCell: UICollectionViewCell {
	
	var index: Int = 0
	
	var normalString: NSAttributedString?
	var highlightedString: NSAttributedString?
	var selectedString: NSAttributedString?
	
	weak var collectionView: UICollectionView?
	weak var target: NSObjectProtocol?
	var buttonAction: Selector?
	
	override var isHighlighted: Bool {
		didSet {
			setNeedsLayout()
		}
	}
	
	override var isSelected: Bool {
		didSet {
			setNeedsLayout()
		}
	}
	
	@IBOutlet weak var label: UILabel!
	@IBOutlet weak var leftConstraint: NSLayoutConstraint!
	@IBOutlet weak var rightConstraint: NSLayoutConstraint!
	@IBOutlet weak var button: UIButton!
	
	fileprivate static var padding: UIEdgeInsets = UIEdgeInsets.zero
	
	
	override class func initialize() {
		super.initialize()
		
		if let cell = self.nib().instantiate(withOwner: nil, options: nil).first as? USGScrollingTabCell {
			padding = UIEdgeInsetsMake(0, cell.leftConstraint.constant, 0, cell.rightConstraint.constant)
		}
	}
	
	
	class func nib() -> UINib {
		return UINib(nibName: "USGScrollingTabCell", bundle: nil)
	}
	
	class func tabWidth(_ string: NSAttributedString, tabInset: CGFloat) -> CGFloat {
		// 文字列の必要な幅を計算
		let bounds = string.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
		                                         options: [.usesLineFragmentOrigin],
		                                         context: nil)
		
		// 余白をたす。繰上げしないと収まりきらない
		let w = max(ceil(bounds.size.width + padding.left + padding.right) + tabInset * 2.0, 1.0)
		
		return w
	}
	
	
	func setNormalStringWithoutAnimation(_ string: NSAttributedString) {
		normalString = string
		label.attributedText = string
	}
	
	
	override func layoutSubviews() {
		super.layoutSubviews()
		
		guard let collectionView = collectionView else {
			return
		}
		
		guard let indexPath = collectionView.indexPathsForSelectedItems!.first else {
			return
		}
		
		// TODO: fix animation
		setAttributedText(false, indexPath: indexPath)
	}
	
	fileprivate func setAttributedText(_ animated: Bool, indexPath: IndexPath) {
		var str: NSAttributedString? = nil
		
		if isHighlighted {
			str = highlightedString
		}
		else if isSelected == true && index == (indexPath as IndexPath).row {
			str = selectedString
		}
		else if (indexPath as IndexPath).row != index {
			str = normalString
		}
		
		if let str = str {
			label.attributedText = str
			let duration: TimeInterval = isHighlighted == true && animated == true ? 0.2 : 0.0
			UIView.transition(with: label,
			                          duration: duration,
			                          options: [.transitionCrossDissolve, .beginFromCurrentState],
			                          animations: {
										self.label.attributedText = str },
			                          completion: nil)
		}
	}
	
	
	@IBAction fileprivate func buttonAction(_ sender: AnyObject) {
		if let buttonAction = buttonAction {
			let _ = target?.perform(buttonAction, with: self)
		}
	}
}
{'fraction_non_alphanumeric': 0.08948440683278483, 'fraction_numerical': 0.007835762419683435, 'mean_word_length': 2.9566026038437694, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5933205', 'n_tokens_mistral': 2287, 'n_tokens_neox': 1931, 'n_words': 492}