<commit_before>//
//  SketchView.swift
//  EulerOSX
//
//  Created by <NAME> on 7/24/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

#if os(OSX)
   import AppKit
   
   protocol SketchViewDelegate: class {
      func didDrag(_ figureName: String?, fromPoint: ViewPoint, toPoint: ViewPoint)
   }
   
   /// The sketch canvas using `NSView`.
   open class SketchView: NSView {
      open weak var sketch: Sketch?
      weak var delegate: SketchViewDelegate?
      
      /// The name of the figure being dragged.
      fileprivate var dragFigureName: String?
      
      /// The previous drag location
      fileprivate var prevDragLocation: ViewPoint?
      
      /// The location where the drag started.
      fileprivate var dragStartLocation: ViewPoint!
      
      /// The sketch origin in the view coordinates.
      var origin: ViewPoint {
         guard let sketch = sketch else { return .zero }
         let origin = sketch.origin
         return ViewPoint(x: origin.x, y: origin.y)
      }
      
      override open func draw(_ dirtyRect: NSRect) {
         guard let sketch = sketch else { return }
         
         let context = NSGraphicsContext.current()!.cgContext
         
         NSColor.white.setFill()
         context.fill(bounds)
         
         context.translateBy(x: origin.x, y: origin.y)
         
         sketch.draw(context)
      }
      
      /// Returns the event point in the view coordinates.
      func viewPointFromEvent(_ theEvent: NSEvent) -> ViewPoint {
         var res = convert(theEvent.locationInWindow, from: nil)
         res.x = round(res.x)
         res.y = round(res.y)
         return res
      }
      
      /// Returns the event point in the sketch coordinates.
      func sketchPointFromEvent(_ theEvent: NSEvent) -> HSPoint {
         return sketchPointFromViewPoint(viewPointFromEvent(theEvent))
      }
      
      /// Converts a point from the view coordinates to sketch coordinates.
      func sketchPointFromViewPoint(_ viewPoint: ViewPoint) -> HSPoint {
         return HSPoint(Double(viewPoint.x - origin.x), Double(viewPoint.y - origin.y))
      }
      
      /// Converts a point from the sketch coordinates to view coordinates.
      func viewPointFromSketchPoint(_ sketchPoint: HSPoint) -> ViewPoint {
         return ViewPoint(x: CGFloat(sketchPoint.x) + origin.x, y: CGFloat(sketchPoint.y) + origin.y)
      }
      
      override open func mouseDown(with theEvent: NSEvent) {
         guard let sketch = sketch else { return }
         
         prevDragLocation = viewPointFromEvent(theEvent)
         dragStartLocation = prevDragLocation
         (dragFigureName, _) = sketch.closestDraggableFigureFromViewPoint(prevDragLocation!, minDistance: 10.0)
      }
      
      override open func mouseDragged(with theEvent: NSEvent) {
         guard let sketch = sketch else { return }

         let loc = viewPointFromEvent(theEvent)
         sketch.drag(dragFigureName, fromViewPoint: prevDragLocation!, toViewPoint: loc)
         needsDisplay = true
         prevDragLocation = loc
      }
      
      override open func mouseUp(with theEvent: NSEvent) {
         let dragEndLocation = viewPointFromEvent(theEvent)
         delegate?.didDrag(dragFigureName, fromPoint: dragStartLocation, toPoint: dragEndLocation)
         Swift.print(sketchPointFromViewPoint(dragEndLocation).description)
         prevDragLocation = nil
         dragFigureName = nil
      }
   }
   
#endif<commit_msg>Add the name of the dragged figure after the drag ends.<commit_after>//
//  SketchView.swift
//  EulerOSX
//
//  Created by <NAME> on 7/24/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

#if os(OSX)
   import AppKit
   
   protocol SketchViewDelegate: class {
      func didDrag(_ figureName: String?, fromPoint: ViewPoint, toPoint: ViewPoint)
   }
   
   /// The sketch canvas using `NSView`.
   open class SketchView: NSView {
      open weak var sketch: Sketch?
      weak var delegate: SketchViewDelegate?
      
      /// The name of the figure being dragged.
      fileprivate var dragFigureName: String?
      
      /// The previous drag location
      fileprivate var prevDragLocation: ViewPoint?
      
      /// The location where the drag started.
      fileprivate var dragStartLocation: ViewPoint!
      
      /// The sketch origin in the view coordinates.
      var origin: ViewPoint {
         guard let sketch = sketch else { return .zero }
         let origin = sketch.origin
         return ViewPoint(x: origin.x, y: origin.y)
      }
      
      override open func draw(_ dirtyRect: NSRect) {
         guard let sketch = sketch else { return }
         
         let context = NSGraphicsContext.current()!.cgContext
         
         NSColor.white.setFill()
         context.fill(bounds)
         
         context.translateBy(x: origin.x, y: origin.y)
         
         sketch.draw(context)
      }
      
      /// Returns the event point in the view coordinates.
      func viewPointFromEvent(_ theEvent: NSEvent) -> ViewPoint {
         var res = convert(theEvent.locationInWindow, from: nil)
         res.x = round(res.x)
         res.y = round(res.y)
         return res
      }
      
      /// Returns the event point in the sketch coordinates.
      func sketchPointFromEvent(_ theEvent: NSEvent) -> HSPoint {
         return sketchPointFromViewPoint(viewPointFromEvent(theEvent))
      }
      
      /// Converts a point from the view coordinates to sketch coordinates.
      func sketchPointFromViewPoint(_ viewPoint: ViewPoint) -> HSPoint {
         return HSPoint(Double(viewPoint.x - origin.x), Double(viewPoint.y - origin.y))
      }
      
      /// Converts a point from the sketch coordinates to view coordinates.
      func viewPointFromSketchPoint(_ sketchPoint: HSPoint) -> ViewPoint {
         return ViewPoint(x: CGFloat(sketchPoint.x) + origin.x, y: CGFloat(sketchPoint.y) + origin.y)
      }
      
      override open func mouseDown(with theEvent: NSEvent) {
         guard let sketch = sketch else { return }
         
         prevDragLocation = viewPointFromEvent(theEvent)
         dragStartLocation = prevDragLocation
         (dragFigureName, _) = sketch.closestDraggableFigureFromViewPoint(prevDragLocation!, minDistance: 10.0)
      }
      
      override open func mouseDragged(with theEvent: NSEvent) {
         guard let sketch = sketch else { return }

         let loc = viewPointFromEvent(theEvent)
         sketch.drag(dragFigureName, fromViewPoint: prevDragLocation!, toViewPoint: loc)
         needsDisplay = true
         prevDragLocation = loc
      }
      
      override open func mouseUp(with theEvent: NSEvent) {
         let dragEndLocation = viewPointFromEvent(theEvent)
         delegate?.didDrag(dragFigureName, fromPoint: dragStartLocation, toPoint: dragEndLocation)
         Swift.print(sketchPointFromViewPoint(dragEndLocation).description)
         if let dragFigureName = dragFigureName {
            Swift.print("finished dragging \(dragFigureName)")
         }
         prevDragLocation = nil
         dragFigureName = nil
      }
   }
   
#endif
{'fraction_non_alphanumeric': 0.08174157303370787, 'fraction_numerical': 0.0033707865168539327, 'mean_word_length': 2.2605311355311355, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '4031387', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1927, 'n_words': 554}