<commit_before>//
//  MOSwitch.swift
//  MOSwitch
//
//  Created by <NAME> on 6/14/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa


@IBDesignable
public class MOSwitch: NSControl {
    
    public var on = false
    
    public func setOn(on: Bool, animated: Bool) {
        
        let bgColor = on ? onColor : offColor
        backgroundView.layer?.backgroundColor = bgColor.CGColor

        if animated {
            animate(on)
        }
        
        self.on = on
    }
    
    public let thumbPadding: CGFloat = 0.0
    
    public var mediaTimingFunction = kCAMediaTimingFunctionEaseIn
    public var thumbAnimationDuration = 0.20
    
    public var radius: CGFloat {
        return self.bounds.size.height / 2
    }
    
    @IBInspectable public var onColor: NSColor = NSColor(red: 19/255.0, green: 232/255.0, blue: 89/255.0, alpha: 1.0)
    
    @IBInspectable public var offColor: NSColor = NSColor(red: 203/255.0, green: 203/255.0, blue: 203/255.0, alpha: 1.0) {
        didSet { backgroundView.layer?.backgroundColor = offColor.CGColor }
    }
    
    @IBInspectable public var thumbColor: NSColor = NSColor(red: 226/255.0, green: 226/255.0, blue: 226/255.0, alpha: 1.0) {
        didSet { thumbView.layer?.backgroundColor = thumbColor.CGColor }
    }
    
    public var backgroundView: NSView!
    public var thumbView: MOThumbView!
    
    public var dragVelocityGain: CGFloat = 0.3
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    public override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setup()
    }
    
    private func setup() {
        
        // Draw the backgroundView
        backgroundView = NSView(frame: self.bounds)
        backgroundView.wantsLayer = true

        if let layer = backgroundView.layer {
            layer.backgroundColor = offColor.CGColor
            layer.cornerRadius = radius
        }
        
        self.addSubview(backgroundView)
        
        // Draw the knobView
        let thumbOrigin = CGPoint(x: backgroundView.bounds.origin.x + thumbPadding,
                                 y: backgroundView.bounds.origin.y + thumbPadding)
        
        let diameter = backgroundView.bounds.size.height - thumbPadding * 2
        let thumbSize = CGSize(width: diameter, height: diameter)
        let thumbFrame = CGRect(origin: thumbOrigin, size: thumbSize)
        
        thumbView = MOThumbView(frame: thumbFrame)
        thumbView.wantsLayer = true
        
        if let layer = thumbView.layer {
            layer.backgroundColor = thumbColor.CGColor
            layer.cornerRadius = radius
        }
                
        let panRecognizer = NSPanGestureRecognizer(target: self, action: #selector(MOSwitch.panned(_:)))
        let clickRecognizer = NSClickGestureRecognizer(target: self, action: #selector(MOSwitch.clicked(_:)))
        
        thumbView.addGestureRecognizer(panRecognizer)
        backgroundView.addGestureRecognizer(clickRecognizer)
        
        backgroundView.addSubview(thumbView)

    }
    
    
    // MARK: - NSGesture handlers.
    
    @objc private func clicked(click: NSClickGestureRecognizer) {
        setOn(!on, animated: true)
    }
    
    @objc private func panned(pan: NSPanGestureRecognizer) {
        switch pan.state {
        case .Began:
            break
        case .Changed:
            updatePosition(pan)
            
            let x = thumbView.frame.origin.x
            let thumbViewWidth = thumbView.bounds.size.width
            let backgroundViewWidth = backgroundView.bounds.size.width
            if x + thumbViewWidth/2 >= backgroundViewWidth/2 {
                setOn(true, animated: false)
            } else {
                setOn(false, animated: false)
            }
            break
        case .Ended:
            moveToNearestSwitchPosition()
            break
        default:
            break
        }<commit_msg>Added NSEvent handling to MOSwitch<commit_after>//
//  MOSwitch.swift
//  MOSwitch
//
//  Created by <NAME> on 6/14/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Cocoa


@IBDesignable
public class MOSwitch: NSControl {
    
    public var on = false
    
    public func setOn(on: Bool, animated: Bool) {
        
        let bgColor = on ? onColor : offColor
        backgroundView.layer?.backgroundColor = bgColor.CGColor

        if animated {
            animate(on)
        }
        
        self.on = on
    }
    
    public let thumbPadding: CGFloat = 0.0
    
    public var mediaTimingFunction = kCAMediaTimingFunctionEaseIn
    public var thumbAnimationDuration = 0.20
    
    public var radius: CGFloat {
        return self.bounds.size.height / 2
    }
    
    @IBInspectable public var onColor: NSColor = NSColor(red: 19/255.0, green: 232/255.0, blue: 89/255.0, alpha: 1.0)
    
    @IBInspectable public var offColor: NSColor = NSColor(red: 203/255.0, green: 203/255.0, blue: 203/255.0, alpha: 1.0) {
        didSet { backgroundView.layer?.backgroundColor = offColor.CGColor }
    }
    
    @IBInspectable public var thumbColor: NSColor = NSColor(red: 226/255.0, green: 226/255.0, blue: 226/255.0, alpha: 1.0) {
        didSet { thumbView.layer?.backgroundColor = thumbColor.CGColor }
    }
    
    public var backgroundView: NSView!
    public var thumbView: MOThumbView!
    
    public var dragVelocityGain: CGFloat = 0.3
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    public override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setup()
    }
    
    private func setup() {
        
        // Draw the backgroundView
        backgroundView = NSView(frame: self.bounds)
        backgroundView.wantsLayer = true

        if let layer = backgroundView.layer {
            layer.backgroundColor = offColor.CGColor
            layer.cornerRadius = radius
        }
        
        self.addSubview(backgroundView)
        
        // Draw the knobView
        let thumbOrigin = CGPoint(x: backgroundView.bounds.origin.x + thumbPadding,
                                 y: backgroundView.bounds.origin.y + thumbPadding)
        
        let diameter = backgroundView.bounds.size.height - thumbPadding * 2
        let thumbSize = CGSize(width: diameter, height: diameter)
        let thumbFrame = CGRect(origin: thumbOrigin, size: thumbSize)
        
        thumbView = MOThumbView(frame: thumbFrame)
        thumbView.wantsLayer = true
        
        if let layer = thumbView.layer {
            layer.backgroundColor = thumbColor.CGColor
            layer.cornerRadius = radius
        }
                
        let panRecognizer = NSPanGestureRecognizer(target: self, action: #selector(MOSwitch.panned(_:)))
        let clickRecognizer = NSClickGestureRecognizer(target: self, action: #selector(MOSwitch.clicked(_:)))
        
        thumbView.addGestureRecognizer(panRecognizer)
        backgroundView.addGestureRecognizer(clickRecognizer)
        
        backgroundView.addSubview(thumbView)

    }
    
    
    // MARK: - NSGesture handlers.
    
    @objc private func clicked(click: NSClickGestureRecognizer) {
        setOn(!on, animated: true)
        sendAction(self.action, to: self.target)
    }
    
    @objc private func panned(pan: NSPanGestureRecognizer) {
        switch pan.state {
        case .Began:
            break
        case .Changed:
            updatePosition(pan)
            
            let x = thumbView.frame.origin.x
            let thumbViewWidth = thumbView.bounds.size.width
            let backgroundViewWidth = backgroundView.bounds.size.width
            if x + thumbViewWidth/2 >= backgroundViewWidth/2 {
                setOn(true, animated: false)
            } else {
                setOn(false, animated: false)
            }
            break
        case .Ended:
            sendAction(self.action, to: self.target)
            moveToNearestSwitchPosition()
            break
        default:
            break
        }
{'fraction_non_alphanumeric': 0.0863664404688464, 'fraction_numerical': 0.021468229487970388, 'mean_word_length': 1.9465648854961832, 'pattern_counts': {'":': 0, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6791247', 'n_tokens_mistral': 2528, 'n_tokens_neox': 2333, 'n_words': 538}