<commit_before>//
//  KeyboardConnector.swift
//  TransliteratingKeyboard
//
//  Created by <NAME> on 7/19/14.
//  Copyright (c) 2014 Apple. All rights reserved.
//

import UIKit

protocol Connectable {
    func attachmentPoints(direction: Direction) -> (CGPoint, CGPoint)
    func attachmentDirection() -> Direction?
    func attach(direction: Direction?) // call with nil to detach
}

// TODO: Xcode crashes
// <ConnectableView: UIView where ConnectableView: Connectable>
class KeyboardConnector: UIView, KeyboardView {
    
    var start: UIView
    var end: UIView
    var startDir: Direction
    var endDir: Direction
    
    // TODO: temporary fix for Swift compiler crash
    var startConnectable: Connectable
    var endConnectable: Connectable
    var convertedStartPoints: (CGPoint, CGPoint)!
    var convertedEndPoints: (CGPoint, CGPoint)!
    
    var color: UIColor { didSet { self.setNeedsDisplay() }}
    var underColor: UIColor { didSet { self.setNeedsDisplay() }}
    var borderColor: UIColor { didSet { self.setNeedsDisplay() }}
    var drawUnder: Bool { didSet { self.setNeedsDisplay() }}
    var drawBorder: Bool { didSet { self.setNeedsDisplay() }}
    var drawShadow: Bool { didSet { self.setNeedsDisplay() }}
    
    var _offset: CGPoint
    
    // TODO: until bug is fixed, make sure start/end and startConnectable/endConnectable are the same object
    init(start: UIView, end: UIView, startConnectable: Connectable, endConnectable: Connectable, startDirection: Direction, endDirection: Direction) {
        self.start = start
        self.end = end
        self.startDir = startDirection
        self.endDir = endDirection
        self.startConnectable = startConnectable
        self.endConnectable = endConnectable
        
        self.color = UIColor.whiteColor()
        self.underColor = UIColor.grayColor()
        self.borderColor = UIColor.blackColor()
        self.drawUnder = true
        self.drawBorder = true
        self.drawShadow = true
        
        self._offset = CGPointZero
        
        super.init(frame: CGRectZero)
        
        self.backgroundColor = UIColor.clearColor()
    }
    
    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        self.resizeFrame()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        resizeFrame()
    }
    
    func generateConvertedPoints() {
        if !self.superview {
            return
        }
        
        let startPoints = self.startConnectable.attachmentPoints(self.startDir)
        let endPoints = self.endConnectable.attachmentPoints(self.endDir)
        
        self.convertedStartPoints = (
            self.superview.convertPoint(startPoints.0, fromView: self.start),
            self.superview.convertPoint(startPoints.1, fromView: self.start))
        self.convertedEndPoints = (<commit_msg>Fixed popups turning on and off rapidly.<commit_after>//
//  KeyboardConnector.swift
//  TransliteratingKeyboard
//
//  Created by <NAME> on 7/19/14.
//  Copyright (c) 2014 Apple. All rights reserved.
//

import UIKit

protocol Connectable {
    func attachmentPoints(direction: Direction) -> (CGPoint, CGPoint)
    func attachmentDirection() -> Direction?
    func attach(direction: Direction?) // call with nil to detach
}

// TODO: Xcode crashes
// <ConnectableView: UIView where ConnectableView: Connectable>
class KeyboardConnector: UIView, KeyboardView {
    
    var start: UIView
    var end: UIView
    var startDir: Direction
    var endDir: Direction
    
    // TODO: temporary fix for Swift compiler crash
    var startConnectable: Connectable
    var endConnectable: Connectable
    var convertedStartPoints: (CGPoint, CGPoint)!
    var convertedEndPoints: (CGPoint, CGPoint)!
    
    var color: UIColor { didSet { self.setNeedsDisplay() }}
    var underColor: UIColor { didSet { self.setNeedsDisplay() }}
    var borderColor: UIColor { didSet { self.setNeedsDisplay() }}
    var drawUnder: Bool { didSet { self.setNeedsDisplay() }}
    var drawBorder: Bool { didSet { self.setNeedsDisplay() }}
    var drawShadow: Bool { didSet { self.setNeedsDisplay() }}
    
    var _offset: CGPoint
    
    // TODO: until bug is fixed, make sure start/end and startConnectable/endConnectable are the same object
    init(start: UIView, end: UIView, startConnectable: Connectable, endConnectable: Connectable, startDirection: Direction, endDirection: Direction) {
        self.start = start
        self.end = end
        self.startDir = startDirection
        self.endDir = endDirection
        self.startConnectable = startConnectable
        self.endConnectable = endConnectable
        
        self.color = UIColor.whiteColor()
        self.underColor = UIColor.grayColor()
        self.borderColor = UIColor.blackColor()
        self.drawUnder = true
        self.drawBorder = true
        self.drawShadow = true
        
        self._offset = CGPointZero
        
        super.init(frame: CGRectZero)
        
        self.userInteractionEnabled = false // TODO: why is this even necessary if it's under all the other views?
        self.backgroundColor = UIColor.clearColor()
    }
    
    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        self.resizeFrame()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        resizeFrame()
    }
    
    func generateConvertedPoints() {
        if !self.superview {
            return
        }
        
        let startPoints = self.startConnectable.attachmentPoints(self.startDir)
        let endPoints = self.endConnectable.attachmentPoints(self.endDir)
        
        self.convertedStartPoints = (
            self.superview.convertPoint(startPoints.0, fromView: self.start),
            self.superview.convertPoint(startPoints.1, fromView: self.start))
        self.convertedEndPoints = (
{'fraction_non_alphanumeric': 0.0880952380952381, 'fraction_numerical': 0.0037414965986394557, 'mean_word_length': 2.6664588528678306, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '5373224', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1643, 'n_words': 417}