<commit_before>//
//  Field.swift
//  BurntCocoaUI
//
//  Created by <NAME> on 25/04/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa


public enum FieldKind : String {
	case popUp = "popUp"
	case text = "text"
	case checkbox = "checkbox"
}

public protocol FieldProtocol {
	var fieldKind: FieldKind { get }
	var label: String? { get }
}

public protocol FieldsProducer {
	associatedtype Field : FieldProtocol
	
	mutating func control(forField field: Field) -> NSView
	
	mutating func cellView(forField field: Field) -> NSView
	mutating func updateStackView(stackView: NSStackView, forFields fields: [Field]) -> [NSView]
}

extension FieldsProducer {
	public mutating func cellView(forField field: Field) -> NSView {
		let control = self.control(forField: field)
		let label: NSTextField? = field.label.map{ text in
			let label = NSTextField()
			label.stringValue = text
			label.selectable = false
			return label
		}
		
		let cellView = NSView(frame: NSRect(origin: .zero, size: NSSize(width: 100.0, height: 36.0)))
		
		cellView.addSubview(control)
		NSLayoutConstraint.activateConstraints([
			NSLayoutConstraint(item: control, attribute: .Leading, relatedBy: .Equal, toItem: cellView, attribute: .Leading, multiplier: 1.0, constant: 8.0),
			NSLayoutConstraint(item: control, attribute: .Trailing, relatedBy: .Equal, toItem: cellView, attribute: .CenterX, multiplier: 1.0, constant: -4.0),
			NSLayoutConstraint(item: control, attribute: .CenterY, relatedBy: .Equal, toItem: cellView, attribute: .CenterY, multiplier: 1.0, constant: 0.0)
		])
		
		if let label = label {
			cellView.addSubview(label)
			NSLayoutConstraint.activateConstraints([
				NSLayoutConstraint(item: control, attribute: .Leading, relatedBy: .Equal, toItem: cellView, attribute: .CenterX, multiplier: 1.0, constant: 4.0),
				NSLayoutConstraint(item: control, attribute: .Trailing, relatedBy: .Equal, toItem: cellView, attribute: .Trailing, multiplier: 1.0, constant: 8.0),
				NSLayoutConstraint(item: control, attribute: .Baseline, relatedBy: .Equal, toItem: control, attribute: .Baseline, multiplier: 1.0, constant: 0.0)
			])
		}
		
		return cellView
	}
	
	public mutating func updateStackView(stackView: NSStackView, forFields fields: [Field?], gapSpacing: CGFloat, gravity: NSStackViewGravity) {
		let viewsWithGaps = fields.map{ $0.map{ cellView(forField: $0) } }
		
		let views = viewsWithGaps.flatMap{ $0 }
		stackView.setViews(views, inGravity: gravity)
		
		for (isGap, view) in zip(viewsWithGaps.map{ $0 == nil }.dropFirst(), viewsWithGaps) {
			if let view = view where isGap {
				stackView.setCustomSpacing(gapSpacing, afterView: view)
			}
		}
	}
}
<commit_msg>Fix constraints<commit_after>//
//  Field.swift
//  BurntCocoaUI
//
//  Created by <NAME> on 25/04/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa


public enum FieldKind : String {
	case popUp = "popUp"
	case text = "text"
	case checkbox = "checkbox"
}

public protocol FieldProtocol {
	var fieldKind: FieldKind { get }
	var label: String? { get }
}

public protocol FieldsProducer {
	associatedtype Field : FieldProtocol
	
	mutating func control(forField field: Field) -> NSView
	
	mutating func cellView(forField field: Field) -> NSView
	mutating func updateStackView(stackView: NSStackView, forFields fields: [Field]) -> [NSView]
}

extension FieldsProducer {
	public mutating func cellView(forField field: Field) -> NSView {
		let control = self.control(forField: field)
		let label: NSTextField? = field.label.map{ text in
			let label = NSTextField()
			label.stringValue = text
			label.selectable = false
			return label
		}
		
		let cellView = NSView(frame: NSRect(origin: .zero, size: NSSize(width: 100.0, height: 36.0)))
		
		cellView.addSubview(control)
		NSLayoutConstraint.activateConstraints([
			NSLayoutConstraint(item: control, attribute: .Leading, relatedBy: .Equal, toItem: cellView, attribute: .Leading, multiplier: 1.0, constant: 8.0),
			NSLayoutConstraint(item: control, attribute: .Trailing, relatedBy: .Equal, toItem: cellView, attribute: .CenterX, multiplier: 1.0, constant: -4.0),
			NSLayoutConstraint(item: control, attribute: .CenterY, relatedBy: .Equal, toItem: cellView, attribute: .CenterY, multiplier: 1.0, constant: 0.0)
		])
		
		if let label = label {
			cellView.addSubview(label)
			NSLayoutConstraint.activateConstraints([
				NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: cellView, attribute: .CenterX, multiplier: 1.0, constant: 4.0),
				NSLayoutConstraint(item: label, attribute: .Trailing, relatedBy: .Equal, toItem: cellView, attribute: .Trailing, multiplier: 1.0, constant: 8.0),
				NSLayoutConstraint(item: label, attribute: .Baseline, relatedBy: .Equal, toItem: control, attribute: .Baseline, multiplier: 1.0, constant: 0.0)
			])
		}
		
		return cellView
	}
	
	public mutating func updateStackView(stackView: NSStackView, forFields fields: [Field?], gapSpacing: CGFloat, gravity: NSStackViewGravity) {
		let viewsWithGaps = fields.map{ $0.map{ cellView(forField: $0) } }
		
		let views = viewsWithGaps.flatMap{ $0 }
		stackView.setViews(views, inGravity: gravity)
		
		for (isGap, view) in zip(viewsWithGaps.map{ $0 == nil }.dropFirst(), viewsWithGaps) {
			if let view = view where isGap {
				stackView.setCustomSpacing(gapSpacing, afterView: view)
			}
		}
	}
}

{'fraction_non_alphanumeric': 0.12056737588652482, 'fraction_numerical': 0.017543859649122806, 'mean_word_length': 4.359, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '627341', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1711, 'n_words': 485}