Skip to content

Commit

Permalink
Drag’n’drop will pass name for frame
Browse files Browse the repository at this point in the history
  • Loading branch information
akaDuality committed Jan 2, 2024
1 parent e71d86f commit a217897
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Shared/Sources/Canvas/CanvasPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ public class CanvasPresenter: DocumentPresenter {
}

// MARK: Image
public func add(image: Image) {
add(image: image, origin: document.artboard.suggestOrigin())
public func add(image: Image, name: String) {
add(image: image, name: name, origin: document.artboard.suggestOrigin())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ open class DocumentPresenter {
}

// MARK:
open func add(image: Image, origin: CGPoint) {
open func add(
image: Image,
name: String?,
origin: CGPoint
) {
document.invalidateQuickViewPreview()

let frame = Frame(image: image,
name: name,
frame: CGRect(origin: origin,
size: image.size))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public class VODesignDocument: AppleDocument, VODesignDocumentProtocol {

displayName = image.name() ?? Date().description

addFrame(with: image, origin: .zero)
addFrame(with: image,
name: image.name(),
origin: .zero)
}

var version: DocumentVersion!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ extension VODesignDocumentProtocol {
?? CGRect(origin: .zero,
size: image?.size ?? defaultFrameSize) // TODO: Add image's scale

let name = frameWrapper.filename ?? UUID().uuidString
let name = frameWrapper.filename ?? UUID().uuidString // TODO: Remove uuidString from here

return Frame(label: name,
imageLocation: .relativeFile(path: "Frame/\(FileName.screen)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public protocol VODesignDocumentProtocol: AnyObject {
extension VODesignDocumentProtocol {
public func addFrame(
with newImage: Image,
name: String?,
origin: CGPoint
) {
let frame = Frame(image: newImage,
name: name,
frame: CGRect(origin: origin,
size: newImage.size))
artboard.append(frame)
Expand Down Expand Up @@ -87,14 +89,16 @@ public protocol PreviewSourceProtocol: AnyObject {
}

extension Frame {
public convenience init(image: Image, frame: CGRect) {
let name = UUID().uuidString // TODO: Create fancy name

// let frame = CGRect(origin: .zero, size: image.size)
public convenience init(
image: Image,
name: String?,
frame: CGRect
) {
let name = name ?? UUID().uuidString

self.init(label: name,
imageLocation: .cache(image: image,
name: UUID().uuidString), // TODO: Make fancy name. Call to ChatGPT!!
name: name),
frame: frame,
elements: [])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CanvasScrollView: NSScrollView {
}
}

// Update after any change of position or magnification
override func reflectScrolledClipView(_ cView: NSClipView) {
super.reflectScrolledClipView(cView)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public class CanvasViewController: NSViewController {
Task {
if let path = await requestImage(),
let image = NSImage(contentsOf: path) {
presenter.add(image: image)
presenter.add(image: image, name: path.lastPathComponent)
}
}
}
Expand Down Expand Up @@ -238,10 +238,12 @@ extension CanvasViewController: NSWindowDelegate {
}

extension CanvasViewController: DragNDropDelegate {
public func didDrag(image: NSImage, locationInWindow: CGPoint) {
public func didDrag(image: NSImage, locationInWindow: CGPoint, name: String?) {
let locationInCanvas = view().contentView.convert(locationInWindow, from: nil)
let shouldAnimate = presenter.document.artboard.frames.count != 0
presenter.add(image: image, origin: locationInCanvas)
presenter.add(image: image,
name: name,
origin: locationInCanvas)
view().fitToWindow(animated: shouldAnimate)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import AppKit

public protocol DragNDropDelegate: AnyObject {
func didDrag(image: NSImage, locationInWindow: CGPoint)
func didDrag(image: NSImage, locationInWindow: CGPoint, name: String?)
func didDrag(path: URL) -> Bool
}

Expand Down Expand Up @@ -137,7 +137,9 @@ open class DragNDropImageView: NSView {
let pasteboard = sender.draggingPasteboard

if let image = sender.image() {
delegate?.didDrag(image: image, locationInWindow: sender.draggingLocation)
delegate?.didDrag(image: image,
locationInWindow: sender.draggingLocation,
name: pasteboard.fileName)
hideTextAndBorder()
return true
}
Expand Down Expand Up @@ -243,9 +245,22 @@ extension CGSize {
}
}

// TODO: Remove duplication
// TODO: Remove duplicationString(data: , encoding: .utf8)
extension NSEvent {
func location(in view: NSView) -> CGPoint {
view.convert(locationInWindow, from: nil)
}
}

extension NSPasteboard {
var fileName: String? {
guard let data = data(forType: .fileURL),
let string = String(data: data, encoding: .utf8),
let url = URL(string: string) else {
return nil
}

return url.lastPathComponent

}
}

0 comments on commit a217897

Please sign in to comment.