         }
         
         layoutBarTop.constant = offsetY
+        
+        switch drag
+        {
+        case Drag.stand:
+            
+            let offsetX:CGFloat = -scrollView.contentOffset.x
+            let controlsWidth:CGFloat
+            
+            if offsetX < 0
+            {
+                controlsWidth = 0
+            }
+            else
+            {
+                let extraDelta:CGFloat = offsetX - kControlsExtraThreshold
+                let extraWidth:CGFloat
+                
+                if extraDelta > 0
+                {
+                    extraWidth = kExtraSpeed * extraDelta
+                }
+                else
+                {
+                    extraWidth = 0
+                }
+                
+                controlsWidth = offsetX + extraWidth
+            }
+            
+            layoutControlsWidth.constant = controlsWidth
+            
+            break
+            
+        case Drag.restart:
+            
+            restartingScroll()
+            
+            break
+            
+        case Drag.avoid:
+            break
+        }
+    }
+    
+    func scrollViewDidEndDecelerating(_ scrollView:UIScrollView)
+    {
+        switch drag
+        {
+        case Drag.restart:
+            
+            drag = Drag.stand
+            
+            break
+            
+        case Drag.avoid:
+            
+            drag = Drag.restart
+            
+            break
+            
+        default:
+            
+            break
+        }
+    }
+    
+    func scrollViewDidEndDragging(_ scrollView:UIScrollView, willDecelerate decelerate:Bool)
+    {
+        let controlsWidth:CGFloat = layoutControlsWidth.constant
+        
+        if controlsWidth > kControlsMinThreshold
+        {
+            drag = Drag.avoid
+            
+            let newControlsWidth:CGFloat
+            
+            if controlsWidth > kControlsMenuThreshold
+            {
+                newControlsWidth = kControlsMaxThreshold
+            }
+            else
+            {
+                newControlsWidth = kControlsMenuThreshold
+            }
+            
+            layoutControlsWidth.constant = newControlsWidth
+            layoutCollectionLeft.constant = newControlsWidth
+            
+            UIView.animate(
+                withDuration:kAnimationDuration)
+            { [weak self] in
+                
+                self?.layoutIfNeeded()
+            }
+        }
+        else
+        {
+            drag = Drag.stand
+        }
     }
     
     func numberOfSections(in collectionView:UICollectionView) -> Int

    private weak var layoutBarTop:NSLayoutConstraint!
    private weak var layoutCollectionLeft:NSLayoutConstraint!
    private weak var layoutControlsWidth:NSLayoutConstraint!
    private let kBarHeight:CGFloat = 210
    private let kDeselectTime:TimeInterval = 0.2
    
    override init(controller:CController)
    {
        super.init(controller:controller)
        self.controller = controller as? CLinearEquationsProject
        

    
    func collectionView(_ collectionView:UICollectionView, didSelectItemAt indexPath:IndexPath)
    {
        if drag == Drag.restart
        {
            restartingScroll()
        }
        else
        {
            let item:MLinearEquationsProjectRowItem = modelAtIndex(index:indexPath)
            item.selected(controller:controller)
            
            DispatchQueue.main.asyncAfter(
                deadline:DispatchTime.now() + kDeselectTime)
            { [weak collectionView] in
                
                collectionView?.selectItem(
                    at:nil,
                    animated:true,
                    scrollPosition:UICollectionViewScrollPosition())
            }
        }
    }
}
