<commit_before>//
//  FullScreenWindow.swift
//  Fuwari
//
//  Created by <NAME> on 2016/12/07.
//  Copyright © 2016年 AppKnop. All rights reserved.
//

import Cocoa
import Carbon

class FullScreenWindow: NSWindow {
    
    var captureDelegate: CaptureDelegate?
    
    private lazy var captureGuideView: CaptureGuideView = {
        let captureGuideView = CaptureGuideView(frame: self.frame)
        return captureGuideView
    }()
    
    override init(contentRect: NSRect, styleMask style: NSWindowStyleMask, backing bufferingType: NSBackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: .borderless, backing: .buffered, defer: false)
        
        isReleasedWhenClosed = true
        displaysWhenScreenProfileChanges = true
        backgroundColor = .clear
        isOpaque = false
        hasShadow = false
        ignoresMouseEvents = false
        acceptsMouseMovedEvents = true
        level = Int(CGWindowLevelForKey(.maximumWindow))
        makeKeyAndOrderFront(self)
        
        contentView = captureGuideView
        
        NotificationCenter.default.addObserver(self, selector: #selector(mouseMoved(with:)), name: Notification.Name(rawValue: Constants.Notification.mouseMoved), object: nil)
        
        NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
            (event: NSEvent) -> NSEvent? in
            if event.keyCode == UInt16(kVK_Escape) {
                self.captureDelegate?.didCanceled()
            }
            return event
        }
    }
    
    func startCapture() {
        orderBack(nil)
        captureGuideView.cursorPoint = NSPoint(x: NSEvent.mouseLocation().x - frame.origin.x, y: NSEvent.mouseLocation().y)
    }
    
    override func mouseMoved(with event: NSEvent) {
        captureGuideView.cursorPoint = NSPoint(x: NSEvent.mouseLocation().x - frame.origin.x, y: NSEvent.mouseLocation().y - frame.origin.y)
    }
    
    override func mouseDown(with event: NSEvent) {
        captureGuideView.startPoint = NSPoint(x: NSEvent.mouseLocation().x - frame.origin.x, y: NSEvent.mouseLocation().y - frame.origin.y)
        captureGuideView.cursorPoint = NSPoint(x: NSEvent.mouseLocation().x - frame.origin.x, y: NSEvent.mouseLocation().y - frame.origin.y)
    }
    
    override func mouseDragged(with event: NSEvent) {
        captureGuideView.cursorPoint = NSPoint(x: NSEvent.mouseLocation().x - frame.origin.x, y: NSEvent.mouseLocation().y - frame.origin.y)
    }
    
    override func mouseUp(with event: NSEvent) {
        capture(rect: captureGuideView.guideWindowRect)
    }
    
    private func capture(rect: NSRect) {
        var upRightPoint = NSPoint.zero
        
        NSScreen.screens()?.forEach {
            upRightPoint = NSPoint(x: max(upRightPoint.x, $0.frame.origin.x + $0.frame.width), y: max(upRightPoint.y, $0.frame.origin.y + $0.frame.height))
        }
        
        var originY = CGFloat(0)
        if frame.origin.y > 0 {
            originY = upRightPoint.y - frame.origin.y - frame.height - (rect.height + rect.origin.y)
        } else {
            originY = NSScreen.main()!.frame.height - frame.origin.y - (rect.origin.y + rect.height)
        }
        
        let cgRect = CGRect(x: rect.origin.x + frame.origin.x, y: originY, width: rect.width, height: rect.height)
        guard let cgImage = CGWindowListCreateImage(cgRect, .optionOnScreenBelowWindow, CGWindowID(windowNumber), .bestResolution) else {
            return
        }
        
        captureGuideView.reset()
        orderOut(nil)
        
        var captureOffsetY = CGFloat(0)
        if frame.origin.y > 0 {
            captureOffsetY = frame.origin.y - (cgRect.origin.y + cgRect.height)
        } else {
            captureOffsetY = -(cgRect.origin.y - upRightPoint.y + rect.height)
        }
        captureDelegate?.didCaptured(rect: CGRect(x: cgRect.origin.x, y: captureOffsetY, width: cgRect.width, height: cgRect.height), image: cgImage)
    }
}

protocol CaptureDelegate {
    func didCanceled()
    func didCaptured(rect: NSRect, image: CGImage)
}
<commit_msg>:bug: Fixed a problem that capture position shifted with some multi-screen<commit_after>//
//  FullScreenWindow.swift
//  Fuwari
//
//  Created by <NAME> on 2016/12/07.
//  Copyright © 2016年 AppKnop. All rights reserved.
//

import Cocoa
import Carbon

class FullScreenWindow: NSWindow {
    
    var captureDelegate: CaptureDelegate?
    
    private lazy var captureGuideView: CaptureGuideView = {
        let captureGuideView = CaptureGuideView(frame: self.frame)
        return captureGuideView
    }()
    
    private var mouseLocation: NSPoint {
        return NSPoint(x: NSEvent.mouseLocation().x - frame.origin.x, y: NSEvent.mouseLocation().y - frame.origin.y)
    }
    
    override init(contentRect: NSRect, styleMask style: NSWindowStyleMask, backing bufferingType: NSBackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: .borderless, backing: .buffered, defer: false)
        
        isReleasedWhenClosed = true
        displaysWhenScreenProfileChanges = true
        backgroundColor = .clear
        isOpaque = false
        hasShadow = false
        ignoresMouseEvents = false
        acceptsMouseMovedEvents = true
        level = Int(CGWindowLevelForKey(.maximumWindow))
        makeKeyAndOrderFront(self)
        
        contentView = captureGuideView
        
        NotificationCenter.default.addObserver(self, selector: #selector(mouseMoved(with:)), name: Notification.Name(rawValue: Constants.Notification.mouseMoved), object: nil)
        
        NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
            (event: NSEvent) -> NSEvent? in
            if event.keyCode == UInt16(kVK_Escape) {
                self.captureDelegate?.didCanceled()
            }
            return event
        }
    }
    
    func startCapture() {
        orderBack(nil)
        captureGuideView.cursorPoint = mouseLocation
    }
    
    override func mouseMoved(with event: NSEvent) {
        captureGuideView.cursorPoint = mouseLocation
    }
    
    override func mouseDown(with event: NSEvent) {
        captureGuideView.startPoint = mouseLocation
        captureGuideView.cursorPoint = mouseLocation
    }
    
    override func mouseDragged(with event: NSEvent) {
        captureGuideView.cursorPoint = mouseLocation
    }
    
    override func mouseUp(with event: NSEvent) {
        capture(rect: captureGuideView.guideWindowRect)
    }
    
    private func capture(rect: NSRect) {
        let mainDisplayBounds = CGDisplayBounds(CGMainDisplayID())
        var captureRect = NSRectToCGRect(convertToScreen(rect))
        captureRect.origin.y = mainDisplayBounds.height - captureRect.origin.y - captureRect.height
        
        guard let cgImage = CGWindowListCreateImage(captureRect, .optionOnScreenBelowWindow, CGWindowID(windowNumber), .bestResolution) else {
            return
        }
        
        captureGuideView.reset()
        orderOut(nil)

        captureDelegate?.didCaptured(rect: rect.offsetBy(dx: frame.origin.x, dy: frame.origin.y), image: cgImage)
    }
}

protocol CaptureDelegate {
    func didCanceled()
    func didCaptured(rect: NSRect, image: CGImage)
}

{'fraction_non_alphanumeric': 0.09673852957435047, 'fraction_numerical': 0.004975124378109453, 'mean_word_length': 2.595131644311972, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7364419', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2128, 'n_words': 438}