<commit_before>//
//  KeyboardViewController.swift
//  Keyboard
//
//  Created by <NAME> on 6/9/14.
//  Copyright (c) 2014 Apple. All rights reserved.
//

import UIKit
//import KeyboardFramework

let DEBUG = true
var DEBUG_SAVED_SCREENSHOT = false

let layout: Dictionary<String, Double> = [
    "leftGap": 3,
    "rightGap": 3,
    "topGap": 9,
    "bottomGap": 7,
    "keyWidth": 26,
    "keyHeight": 39,
    "keyGap": 6, // 5 for russian, though still 6 on lower row
    "shiftAndBackspaceMaxWidth": 36,
    "specialKeyWidth": 34,
    "doneKeyWidth": 50,
    "spaceWidth": 138,
    "debugWidth": 2
]

// shift/backspace: 72x78, but shrinks to 48x78
// lower row: 68x78, 100x78, 276

class ForwardingView: UIView {
    init(frame: CGRect) {
        super.init(frame: frame)
        self.multipleTouchEnabled = false
    }
    
    override func hitTest(point: CGPoint, withEvent event: UIEvent!) -> UIView! {
        return self
    }
    
    var myView: UIView?
    
    func handleControl(view: UIView?, controlEvent: UIControlEvents) {
        if !view {
            return
        }
        
        if !(view is UIControl) {
            return
        }
        
        let control = view! as UIControl
        
        if (controlEvent & UIControlEvents.TouchDown) {
            control.highlighted = true
        }
        if (controlEvent & UIControlEvents.TouchDownRepeat) {
        }
        if (controlEvent & UIControlEvents.TouchDragInside) {
        }
        if (controlEvent & UIControlEvents.TouchDragOutside) {
        }
        if (controlEvent & UIControlEvents.TouchDragEnter) {
        }
        if (controlEvent & UIControlEvents.TouchDragExit) {
        }
        if (controlEvent & UIControlEvents.TouchUpInside) {
            control.highlighted = false
        }
        if (controlEvent & UIControlEvents.TouchUpOutside) {
            control.highlighted = false
        }
        if (controlEvent & UIControlEvents.TouchCancel) {
        }
    }
    
    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        NSLog("began!")
        
        let touch = touches.anyObject()
        let position = touch.locationInView(self)
        var view = super.hitTest(position, withEvent: event)
        NSLog("view is \(view)")
        
        self.myView = view
        
        self.handleControl(self.myView, controlEvent: .TouchDown)
    }
    
    override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
        NSLog("moved!")
        
        let touch = touches.anyObject()
        let position = touch.locationInView(self)
        var view = super.hitTest(position, withEvent: event)
        NSLog("view is \(view)")
        <commit_msg>It’s a little bonkers, but it seems to work.<commit_after>//
//  KeyboardViewController.swift
//  Keyboard
//
//  Created by <NAME> on 6/9/14.
//  Copyright (c) 2014 Apple. All rights reserved.
//

import UIKit
//import KeyboardFramework

let DEBUG = true
var DEBUG_SAVED_SCREENSHOT = false

let layout: Dictionary<String, Double> = [
    "leftGap": 3,
    "rightGap": 3,
    "topGap": 9,
    "bottomGap": 7,
    "keyWidth": 26,
    "keyHeight": 39,
    "keyGap": 6, // 5 for russian, though still 6 on lower row
    "shiftAndBackspaceMaxWidth": 36,
    "specialKeyWidth": 34,
    "doneKeyWidth": 50,
    "spaceWidth": 138,
    "debugWidth": 2
]

// shift/backspace: 72x78, but shrinks to 48x78
// lower row: 68x78, 100x78, 276

class ForwardingView: UIView {
    init(frame: CGRect) {
        super.init(frame: frame)
        self.multipleTouchEnabled = false
    }
    
    override func hitTest(point: CGPoint, withEvent event: UIEvent!) -> UIView! {
        return self
    }
    
    var myView: UIView?
    
    // TODO: drag up control centre from bottom == stuck
    func handleControl(view: UIView?, controlEvent: UIControlEvents) {
        if !view {
            return
        }
        
        if !(view is UIControl) {
            return
        }
        
        let control = view! as UIControl
        
        let controlEvents = [
            UIControlEvents.TouchDownRepeat,
            UIControlEvents.TouchDragInside,
            UIControlEvents.TouchDragOutside,
            UIControlEvents.TouchDragEnter,
            UIControlEvents.TouchDragExit,
            UIControlEvents.TouchUpInside,
            UIControlEvents.TouchUpOutside,
            UIControlEvents.TouchCancel]
        
        let targets = control.allTargets()
        if targets {
            for target in targets.allObjects { // TODO: Xcode crashes
                var actions = control.actionsForTarget(target, forControlEvent: controlEvent)
                if actions {
                    for action in actions {
                        let selector = Selector(action as String)
                        control.sendAction(selector, to: target, forEvent: nil)
                    }
                }
            }
        }
    }
    
    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        NSLog("began!")
        
        let touch = touches.anyObject()
        let position = touch.locationInView(self)
        var view = super.hitTest(position, withEvent: event)
        NSLog("view is \(view)")
        
        self.myView = view
        
        self.handleControl(self.myView, controlEvent: .TouchDown)
    }
    
    override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
        NSLog("moved!")
        
        let touch = touches.anyObject()
        let position = touch.locationInView(self)
        var view = super.hitTest(position, withEvent: event)
        NSLog("view is \(view)")
        
{'fraction_non_alphanumeric': 0.09849955869373345, 'fraction_numerical': 0.01729920564872021, 'mean_word_length': 1.9962982548915917, 'pattern_counts': {'":': 24, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7643089', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1671, 'n_words': 416}