<commit_before>//
//  Input.swift
//  Few
//
//  Created by <NAME> on 9/4/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import AppKit

public class Input<S>: Element {
	private var textField: NSTextField?
	
	private var text: String
	
	private let trampoline = TargetActionTrampoline()
	
	private weak var typedComponent: Component<S>?
	
	public convenience init(text: String, fn: (String, S) -> S) {
		self.init(text: text, action: { str, component in
			component.state = fn(str, component.state)
		})
	}

	public init(text: String, action: (String, Component<S>) -> ()) {
		self.text = text
		super.init()
		
		self.trampoline.action = { [unowned self] in
			self.text = self.textField!.stringValue
			if let component = self.typedComponent {
				action(self.text, component)
			}
		}
	}

	// MARK: Element
	
	public override func applyDiff(other: Element) {
		if textField == nil { return }
		
		let otherInput = other as Input
		if text != otherInput.text {
			text = otherInput.text
			textField!.stringValue = text
		}
		
		frame = CGRectZero
		
		super.applyDiff(other)
	}
	
	public override func realize(component: Component<S>, parentView: NSView) {
		let field = NSTextField(frame: frame)
		field.editable = true
//		field.drawsBackground = false
//		field.bordered = false
		field.stringValue = text
		field.target = trampoline
		field.action = trampoline.selector
		textField = field
		
		super.realize(component, parentView: parentView)
	}
	
	public override func getContentView() -> NSView? {
		return textField
	}
	
	public override func getIntrinsicSize() -> CGSize {
		var size = CGSizeZero
		if let textField = textField {
			let originalFrame = textField.frame
			textField.sizeToFit()
			size = textField.bounds.size
			textField.frame = originalFrame
		}
		return size
	}
}
<commit_msg>Actually get the text changes.<commit_after>//
//  Input.swift
//  Few
//
//  Created by <NAME> on 9/4/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import AppKit

private class InputDelegate: NSObject, NSTextFieldDelegate {
	var action: (() -> ())?

	override func controlTextDidChange(notification: NSNotification!) {
		action?();
	}
}

public class Input<S>: Element {
	private var textField: NSTextField?
	
	private var text: String
	
	private let inputDelegate = InputDelegate()

	public convenience init(text: String, fn: (String, S) -> S) {
		self.init(text: text, action: { str, component in
			component.state = fn(str, component.state)
		})
	}

	public init(text: String, action: (String, Component<S>) -> ()) {
		self.text = text
		super.init()
		
		self.inputDelegate.action = { [unowned self] in
			self.text = self.textField!.stringValue
			let component: Component<S>? = self.getComponent()
			if component != nil {
				action(self.text, component!)
			}
		}
	}

	// MARK: Element
	
	public override func applyDiff(other: Element) {
		if textField == nil { return }
		
		let otherInput = other as Input
		if text != otherInput.text {
			text = otherInput.text
			textField!.stringValue = text
		}
		
		frame = CGRectZero
		
		super.applyDiff(other)
	}
	
	public override func realize(component: Component<S>, parentView: NSView) {
		let field = NSTextField(frame: frame)
		field.editable = true
		field.stringValue = text
		field.delegate = inputDelegate
		textField = field
		
		super.realize(component, parentView: parentView)
	}
	
	public override func getContentView() -> NSView? {
		return textField
	}
	
	public override func getIntrinsicSize() -> CGSize {
		var size = CGSizeZero
		if let textField = textField {
			let originalFrame = textField.frame
			textField.sizeToFit()
			size = textField.bounds.size
			textField.frame = originalFrame
		}
		return size
	}
}

{'fraction_non_alphanumeric': 0.11247008774262164, 'fraction_numerical': 0.00425418771603297, 'mean_word_length': 3.3846153846153846, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 24, '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': '2076902', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1166, 'n_words': 330}