<commit_before>//
//  PanelViewController+Resize.swift
//  PanelKit
//
//  Created by <NAME> on 08/10/2017.
//  Copyright © 2017 Silver Fox. All rights reserved.
//

import UIKit

extension PanelViewController {
	
	private var canResize: Bool {
		
		guard let contentDelegate = contentDelegate else {
			return false
		}
		
		let preferredSize = contentDelegate.preferredPanelContentSize
		let minSize = contentDelegate.minimumPanelContentSize
		let maxSize = contentDelegate.maximumPanelContentSize

		
		if preferredSize == minSize && preferredSize == maxSize {
			return false
		}
		
		return true
	}
	
	func showResizeHandleIfNeeded(animated: Bool = true) {
		
		guard canResize else {
			return
		}
		
		func show() {
			
			self.resizeCornerHandle.transform = .identity

		}
		
		self.resizeCornerHandle.alpha = 1

		if animated {
	
			UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1.0, options: [], animations: {
				
				self.resizeCornerHandle.transform = .identity

			}, completion: nil)
			
		} else {
			
			show()
		}
		
	}
	
	func hideResizeHandle(animated: Bool = true) {
		
		func hide() {
			
			var transform = CGAffineTransform.identity
			transform = transform.translatedBy(x: -44, y: -44)
			
			self.resizeCornerHandle.transform = transform
			self.resizeCornerHandle.alpha = 0
			
		}
		
		if animated {
			
			UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1.0, options: [], animations: {

				hide()

			}, completion: nil)
			
		} else {
			
			hide()
		}
		
	}
	
	func configureResizeHandle() {
		
		let resizeHandle = resizeCornerHandle
		resizeHandle.backgroundColor = .clear
		resizeHandle.translatesAutoresizingMaskIntoConstraints = false
		
		resizeHandle.tintColor = .white
		resizeHandle.transform = CGAffineTransform(rotationAngle: .pi/2 * 2)
		
		let recognizer = UIPanGestureRecognizer(target: self, action: #selector(didDragCornerHandle(_:)))
		resizeHandle.addGestureRecognizer(recognizer)
		
		let resizeHandleTapGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didTapCornerHandle(_ :)))
		resizeHandleTapGestureRecognizer.minimumPressDuration = 0
		resizeHandleTapGestureRecognizer.delegate = self
		
		resizeHandle.addGestureRecognizer(resizeHandleTapGestureRecognizer)
		
		cornerHandleDidBecomeInactive(animated: false)
		hideResizeHandle(animated: false)
		
	}
	
	private func cornerHandleDidBecomeActive() {
		
		resizeCornerHandle.cornerHandleDidBecomeActive()
		
	}
	
	private func cornerHandleDidBecomeInactive(animated: Bool = true) {
		
		resizeCornerHandle.cornerHandleDidBecomeInactive(animated: animated)
		
	}
	
	@objc private func didTapCornerHandle(_ recognizer: UILongPressGestureRecognizer) {
		
		if recognizer.state == .began {
			
			cornerHandleDidBecomeActive()
			
		} else if recognizer.state == .failed || recognizer.state == .ended {
			
			cornerHandleDidBecomeInactive()
			
		}
		
	}
	
	@objc private func didDragCornerHandle(_ recognizer: UIPanGestureRecognizer) {
		
		guard let contentDelegate = contentDelegate else {
			return
		}
		
		guard let viewToMove = self.view else {
			return
		}
		
		if recognizer.state == .began {
			
			cornerHandleDidBecomeActive()
			
			startFrame = viewToMove.frame
			startDragPosition = recognizer.location(in: self.view)
			
		} else if recognizer.state == .changed, let startDragPosition = startDragPosition, let startFrame = startFrame {
			
			let newPosition = recognizer.location(in: self.view)
			
			var newFrame = startFrame
			
			let proposedWidth = newFrame.size.width + (newPosition.x - startDragPosition.x)
			let proposedHeight = newFrame.size.height + (newPosition.y - startDragPosition.y)
			
			let maxWidth = contentDelegate.maximumPanelContentSize.width
			let minWidth = contentDelegate.minimumPanelContentSize.width
			
			let maxHeight = contentDelegate.maximumPanelContentSize.height
			let minHeight = contentDelegate.minimumPanelContentSize.height
			
			let newWidth: CGFloat
			
			if proposedWidth > maxWidth {
				newWidth = maxWidth
			} else if proposedWidth < minWidth {
				newWidth = minWidth
			} else {
				newWidth = proposedWidth
			}
			
			let newHeight: CGFloat
			
			if proposedHeight > maxHeight {
				newHeight = maxHeight
			} else if proposedHeight < minHeight {
				newHeight = minHeight
			} else {
				newHeight = proposedHeight
			}
			
			newFrame.size.width = newWidth
			newFrame.size.height = newHeight
			
			self.manager?.updateFrame(for: self, to: newFrame)
			
		} else if recognizer.state == .ended {
			
			cornerHandleDidBecomeInactive()
			
		}<commit_msg>Prevent a panel from intersecting with a pinned panel when resizing.<commit_after>//
//  PanelViewController+Resize.swift
//  PanelKit
//
//  Created by <NAME> on 08/10/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

extension PanelViewController {
	
	private var canResize: Bool {
		
		guard let contentDelegate = contentDelegate else {
			return false
		}
		
		let preferredSize = contentDelegate.preferredPanelContentSize
		let minSize = contentDelegate.minimumPanelContentSize
		let maxSize = contentDelegate.maximumPanelContentSize

		
		if preferredSize == minSize && preferredSize == maxSize {
			return false
		}
		
		return true
	}
	
	func showResizeHandleIfNeeded(animated: Bool = true) {
		
		guard canResize else {
			return
		}
		
		func show() {
			
			self.resizeCornerHandle.transform = .identity

		}
		
		self.resizeCornerHandle.alpha = 1

		if animated {
	
			UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1.0, options: [], animations: {
				
				self.resizeCornerHandle.transform = .identity

			}, completion: nil)
			
		} else {
			
			show()
		}
		
	}
	
	func hideResizeHandle(animated: Bool = true) {
		
		func hide() {
			
			var transform = CGAffineTransform.identity
			transform = transform.translatedBy(x: -44, y: -44)
			
			self.resizeCornerHandle.transform = transform
			self.resizeCornerHandle.alpha = 0
			
		}
		
		if animated {
			
			UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1.0, options: [], animations: {

				hide()

			}, completion: nil)
			
		} else {
			
			hide()
		}
		
	}
	
	func configureResizeHandle() {
		
		let resizeHandle = resizeCornerHandle
		resizeHandle.backgroundColor = .clear
		resizeHandle.translatesAutoresizingMaskIntoConstraints = false
		
		resizeHandle.tintColor = .white
		resizeHandle.transform = CGAffineTransform(rotationAngle: .pi/2 * 2)
		
		let recognizer = UIPanGestureRecognizer(target: self, action: #selector(didDragCornerHandle(_:)))
		resizeHandle.addGestureRecognizer(recognizer)
		
		let resizeHandleTapGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didTapCornerHandle(_ :)))
		resizeHandleTapGestureRecognizer.minimumPressDuration = 0
		resizeHandleTapGestureRecognizer.delegate = self
		
		resizeHandle.addGestureRecognizer(resizeHandleTapGestureRecognizer)
		
		cornerHandleDidBecomeInactive(animated: false)
		hideResizeHandle(animated: false)
		
	}
	
	private func cornerHandleDidBecomeActive() {
		
		resizeCornerHandle.cornerHandleDidBecomeActive()
		
	}
	
	private func cornerHandleDidBecomeInactive(animated: Bool = true) {
		
		resizeCornerHandle.cornerHandleDidBecomeInactive(animated: animated)
		
	}
	
	@objc private func didTapCornerHandle(_ recognizer: UILongPressGestureRecognizer) {
		
		if recognizer.state == .began {
			
			cornerHandleDidBecomeActive()
			
		} else if recognizer.state == .failed || recognizer.state == .ended {
			
			cornerHandleDidBecomeInactive()
			
		}
		
	}
	
	@objc private func didDragCornerHandle(_ recognizer: UIPanGestureRecognizer) {
		
		guard let contentDelegate = contentDelegate else {
			return
		}
		
		guard let manager = self.manager else {
			return
		}
		
		guard let viewToMove = self.view else {
			return
		}
		
		if recognizer.state == .began {
			
			cornerHandleDidBecomeActive()
			
			startFrame = viewToMove.frame
			startDragPosition = recognizer.location(in: self.view)
			
		} else if recognizer.state == .changed, let startDragPosition = startDragPosition, let startFrame = startFrame {
			
			let newPosition = recognizer.location(in: self.view)
			
			var newFrame = startFrame
			
			let proposedWidth = newFrame.size.width + (newPosition.x - startDragPosition.x)
			let proposedHeight = newFrame.size.height + (newPosition.y - startDragPosition.y)
			
			let maxWidth: CGFloat
			
			if let panelPinnedRight = manager.panelPinnedRight {
				
				// Prevent a panel from intersecting with a pinned panel when resizing.
				
				let wrapperWidth = manager.panelContentWrapperView.frame.width
				
				let theoreticalMaxWidth = (wrapperWidth - self.view.frame.minX) - panelPinnedRight.view.frame.width
				
				maxWidth = min(contentDelegate.maximumPanelContentSize.width, theoreticalMaxWidth)
				
			} else {
				
				maxWidth = contentDelegate.maximumPanelContentSize.width

			}
			
			let minWidth = contentDelegate.minimumPanelContentSize.width
			
			let maxHeight = contentDelegate.maximumPanelContentSize.height
			let minHeight = contentDelegate.minimumPanelContentSize.height
			
			let newWidth: CGFloat
			
			if proposedWidth > maxWidth {
				newWidth = maxWidth
			} else if proposedWidth < minWidth {
				newWidth = minWidth
			} else {
				newWidth = proposedWidth
			}
			
			let newHeight: CGFloat
			
			if proposedHeight > maxHeight {
				newHeight = maxHeight
			} else if proposedHeight < minHeight {
				newHeight = minHeight
			} else {
				newHeight = proposedHeight
			}
			
			newFrame.size.width = newWidth
			newFrame.size.height = newHeight
			
			manager.updateFrame(for: self, to: newFrame)
			
		} else if recognizer.state == .ended {
			
			cornerHandleDidBecomeInactive()
			
		}
{'fraction_non_alphanumeric': 0.08173562058526741, 'fraction_numerical': 0.007467204843592331, 'mean_word_length': 3.430487259722843, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '4765079', 'n_tokens_mistral': 3803, 'n_tokens_neox': 2900, 'n_words': 632}