<commit_before>//
//  DynamicBlurView.swift
//  DynamicBlurView
//
//  Created by <NAME> on 2015/04/08.
//  Copyright (c) 2015年 kyohei_ito. All rights reserved.
//

import UIKit
import Accelerate

public class DynamicBlurView: UIView {
    private class BlurLayer: CALayer {
        @NSManaged var blurRadius: CGFloat
        
        override class func needsDisplayForKey(key: String) -> Bool {
            if key == "blurRadius" {
                return true
            }
            return super.needsDisplayForKey(key)
        }
    }
    
    public enum DynamicMode {
        case Tracking   // refresh only scrolling
        case Common     // always refresh
        case None       // not refresh
        
        func mode() -> String {
            switch self {
            case .Tracking:
                return UITrackingRunLoopMode
            case .Common:
                return NSRunLoopCommonModes
            case .None:
                return ""
            }
        }
    }
    
    private var staticImage: UIImage?
    private var fromBlurRadius: CGFloat?
    private var displayLink: CADisplayLink?
    private let DisplayLinkSelector: Selector = "displayDidRefresh:"
    private var blurLayer: BlurLayer {
        return layer as! BlurLayer
    }
    
    private var blurPresentationLayer: BlurLayer {
        if let layer = blurLayer.presentationLayer() as? BlurLayer {
            return layer
        }
        
        return blurLayer
    }
    
    public var blurRadius: CGFloat {
        set { blurLayer.blurRadius = newValue }
        get { return blurLayer.blurRadius }
    }
    
    /// Default is Tracking.
    public var dynamicMode: DynamicMode = .None {
        didSet {
            if dynamicMode != oldValue {
                linkForDisplay()
            }
        }
    }
    
    /// Blend color.
    public var blendColor: UIColor?
    
    /// Default is 3.
    public var iterations: Int = 3
    
    /// Please be on true if the if Layer is not captured. Such as UINavigationBar and UIToolbar. Can be used only with DynamicMode.None.
    public var fullScreenCapture: Bool = false
    
    /// Ratio of radius. Defauot is 1.
    public var blurRatio: CGFloat = 1 {
        didSet {
            if oldValue != blurRatio {
                if let image = staticImage {
                    setCaptureImage(image, radius: blurRadius)
                }
            }
        }
    }
    
    public override class func layerClass() -> AnyClass {
        return BlurLayer.self
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        
        userInteractionEnabled = false
    }
    
    public required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        userInteractionEnabled = false
    }
    
    public override func didMoveToSuperview() {
        super.didMoveToSuperview()
        
        if superview == nil {
            displayLink?.invalidate()
            displayLink = nil
        } else {
            linkForDisplay()
        }
    }
    
    public override func actionForLayer(layer: CALayer!, forKey event: String!) -> CAAction! {
        if event == "blurRadius" {
            fromBlurRadius = nil
            
            if dynamicMode == .None {
                staticImage = capturedImage()
            } else {
                staticImage = nil
            }
            
            if let action = super.actionForLayer(layer, forKey: "backgroundColor") as? CAAnimation {
                fromBlurRadius = blurPresentationLayer.blurRadius
                
                let animation = CABasicAnimation()
                animation.fromValue = fromBlurRadius
                animation.beginTime = action.beginTime
                animation.duration = action.duration
                animation.speed = action.speed
                animation.timeOffset = action.timeOffset<commit_msg>fix uiview animate delay not working<commit_after>//
//  DynamicBlurView.swift
//  DynamicBlurView
//
//  Created by <NAME> on 2015/04/08.
//  Copyright (c) 2015年 kyohei_ito. All rights reserved.
//

import UIKit
import Accelerate

public class DynamicBlurView: UIView {
    private class BlurLayer: CALayer {
        @NSManaged var blurRadius: CGFloat
        
        override class func needsDisplayForKey(key: String) -> Bool {
            if key == "blurRadius" {
                return true
            }
            return super.needsDisplayForKey(key)
        }
    }
    
    public enum DynamicMode {
        case Tracking   // refresh only scrolling
        case Common     // always refresh
        case None       // not refresh
        
        func mode() -> String {
            switch self {
            case .Tracking:
                return UITrackingRunLoopMode
            case .Common:
                return NSRunLoopCommonModes
            case .None:
                return ""
            }
        }
    }
    
    private var staticImage: UIImage?
    private var fromBlurRadius: CGFloat?
    private var displayLink: CADisplayLink?
    private let DisplayLinkSelector: Selector = "displayDidRefresh:"
    private var blurLayer: BlurLayer {
        return layer as! BlurLayer
    }
    
    private var blurPresentationLayer: BlurLayer {
        if let layer = blurLayer.presentationLayer() as? BlurLayer {
            return layer
        }
        
        return blurLayer
    }
    
    public var blurRadius: CGFloat {
        set { blurLayer.blurRadius = newValue }
        get { return blurLayer.blurRadius }
    }
    
    /// Default is Tracking.
    public var dynamicMode: DynamicMode = .None {
        didSet {
            if dynamicMode != oldValue {
                linkForDisplay()
            }
        }
    }
    
    /// Blend color.
    public var blendColor: UIColor?
    
    /// Default is 3.
    public var iterations: Int = 3
    
    /// Please be on true if the if Layer is not captured. Such as UINavigationBar and UIToolbar. Can be used only with DynamicMode.None.
    public var fullScreenCapture: Bool = false
    
    /// Ratio of radius. Defauot is 1.
    public var blurRatio: CGFloat = 1 {
        didSet {
            if oldValue != blurRatio {
                if let image = staticImage {
                    setCaptureImage(image, radius: blurRadius)
                }
            }
        }
    }
    
    public override class func layerClass() -> AnyClass {
        return BlurLayer.self
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        
        userInteractionEnabled = false
    }
    
    public required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        userInteractionEnabled = false
    }
    
    public override func didMoveToSuperview() {
        super.didMoveToSuperview()
        
        if superview == nil {
            displayLink?.invalidate()
            displayLink = nil
        } else {
            linkForDisplay()
        }
    }
    
    public override func actionForLayer(layer: CALayer!, forKey event: String!) -> CAAction! {
        if event == "blurRadius" {
            fromBlurRadius = nil
            
            if dynamicMode == .None {
                staticImage = capturedImage()
            } else {
                staticImage = nil
            }
            
            if let action = super.actionForLayer(layer, forKey: "backgroundColor") as? CAAnimation {
                fromBlurRadius = blurPresentationLayer.blurRadius
                
                let animation = CABasicAnimation()
                animation.fromValue = fromBlurRadius
                animation.beginTime = CACurrentMediaTime() + action.beginTime
                animation.duration = action.duration
                animation.speed = action.speed
                animation.timeOffset = action.timeOffset
{'fraction_non_alphanumeric': 0.06954649100582722, 'fraction_numerical': 0.0040537116797567775, 'mean_word_length': 1.550064599483204, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '1671659', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2009, 'n_words': 573}