// // KMScreenShotMaskViewController.swift // PDF Reader Pro // // Created by liujiajie on 2024/1/24. // import Cocoa let keditViewGap = 5 class KMScreenShotMaskViewController: NSViewController{ @IBOutlet var screenShotMaskView: KMScreenShotMaskView! var editViewController: KMScreenShotEditViewController! var holeDragImageView: NSImageView! var callBack: captureScreenCallBack? convenience init(callB: @escaping captureScreenCallBack) { self.init(nibName: "KMScreenShotMaskViewController", bundle: nil) self.callBack = callB } override func loadView() { super.loadView() initViews() handlerCallBack() } func initViews() { view.wantsLayer = true view.layer?.backgroundColor = NSColor.clear.cgColor screenShotMaskView.frame = view.frame editViewController = KMScreenShotEditViewController(nibName: "KMScreenShotEditViewController", bundle: nil) view.addSubview(editViewController.view) editViewController.view.isHidden = true holeDragImageView = NSImageView(frame: NSRect(x: 0, y: 0, width: 40, height: 40)) view.addSubview(holeDragImageView) holeDragImageView.isHidden = true holeDragImageView.image = NSImage(named: "drag.png") } func handlerCallBack() { self.screenShotMaskView.callBack = self.callBack self.screenShotMaskView.frameCallBack = {[weak self] rect in if self?.editViewController.view.isHidden == true { self?.editViewController.view.isHidden = false } if self?.holeDragImageView.isHidden == true { self?.holeDragImageView.isHidden = false } self?.editViewController.updateSize(withRect: rect) self?.updateEditViewFrame(with: rect) self?.updateHoleButtonPosition(rect) } self.editViewController.buttonCallBack = { [weak self] captureOrNot in self?.holeDragImageView.isHidden = true if captureOrNot { self?.screenShotMaskView.captureImage() } else { self?.screenShotMaskView.endImageCapture() } } self.editViewController.sizeCallBack = { [weak self] size in self?.screenShotMaskView.updateholeSize(newSize: size) self?.updateHoleButtonPositionWithHoleSize(holeSize: size) } self.screenShotMaskView.detachCallBack = { [weak self] event in let cursorPoint = KMScreenShotHandler.screenPosition(for: event) self?.screenShotMaskView.holeDragMode = NSPointInRect(cursorPoint, NSMakeRect((self?.screenShotMaskView.borderFrame.origin.x ?? 0) + 4, (self?.screenShotMaskView.borderFrame.origin.y ?? 0) + 4, (self?.screenShotMaskView.borderFrame.size.width ?? 0) - 8, (self?.screenShotMaskView.borderFrame.size.height ?? 0) - 8)) } } func beginImageCapture(_ isCaptureWindow: Bool) { self.holeDragImageView.isHidden = true if isCaptureWindow { self.editViewController.view.isHidden = true } self.screenShotMaskView.beginImageCapture(isCaptureWindow: isCaptureWindow) } func updateHoleButtonPositionWithHoleSize(holeSize: CGSize) { self.holeDragImageView.frame = NSMakeRect(self.screenShotMaskView.borderFrame.origin.x + holeSize.width/2 - self.holeDragImageView.frame.size.width/2, self.screenShotMaskView.borderFrame.origin.y + holeSize.height/2 - self.holeDragImageView.frame.size.height/2, self.holeDragImageView.frame.size.width, self.holeDragImageView.frame.size.height) } func updateHoleButtonPosition(_ rect: NSRect) { self.holeDragImageView.frame = NSMakeRect(NSMidX(rect) - self.holeDragImageView.frame.size.width/2, NSMidY(rect) - self.holeDragImageView.frame.size.height/2, self.holeDragImageView.frame.size.width, self.holeDragImageView.frame.size.height) } func updateEditViewFrame(with aRect: NSRect) { let viewWidth = self.editViewController.view.frame.size.width let viewHeight = self.editViewController.view.frame.size.height var newRect = NSMakeRect(NSMinX(aRect), NSMinY(aRect) - viewHeight - CGFloat(keditViewGap), viewWidth, viewHeight) if NSMinY(newRect) < 0 { newRect.origin.x = NSMinX(newRect) - viewWidth - CGFloat(keditViewGap) newRect.origin.y = NSMinY(aRect) + CGFloat(keditViewGap) if newRect.origin.x < 0 { newRect.origin.x = NSMinX(aRect) + CGFloat(keditViewGap) } } let screenFrame = NSScreen.screens.first!.frame if NSMaxX(screenFrame) < NSMaxX(newRect) { newRect.origin.x = screenFrame.size.width - viewWidth } if NSMaxY(screenFrame) < NSMaxY(newRect) { newRect.origin.y = screenFrame.size.height - viewHeight } self.editViewController.view.frame = newRect } }