1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 19:47:41 +03:00
vimr/VimR/Workspace/WorkspaceToolButton.swift

160 lines
4.3 KiB
Swift
Raw Normal View History

2016-09-18 21:49:42 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
2016-11-14 00:16:56 +03:00
class WorkspaceToolButton: NSView, NSDraggingSource, NSPasteboardItemDataProvider {
static fileprivate let toolUti = "com.qvacua.vimr.tool"
2016-09-18 21:49:42 +03:00
2016-09-25 18:50:33 +03:00
static fileprivate let titlePadding = CGSize(width: 8, height: 2)
2016-09-18 21:49:42 +03:00
2016-09-25 18:50:33 +03:00
fileprivate let title: NSAttributedString
fileprivate var trackingArea = NSTrackingArea()
2016-09-25 01:44:10 +03:00
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - API
2016-09-18 21:49:42 +03:00
var location = WorkspaceBarLocation.left
var isSelected: Bool {
return self.tool?.isSelected ?? false
}
weak var tool: WorkspaceTool?
2016-09-25 01:44:10 +03:00
init(title: String) {
self.title = NSAttributedString(string: title, attributes: [
2016-09-25 18:50:33 +03:00
NSFontAttributeName: NSFont.systemFont(ofSize: 11)
2016-09-25 01:44:10 +03:00
])
super.init(frame: CGRect.zero)
self.translatesAutoresizingMaskIntoConstraints = false
self.wantsLayer = true
}
func highlight() {
2016-09-25 18:50:33 +03:00
self.layer?.backgroundColor = NSColor.controlShadowColor.cgColor
2016-09-18 21:49:42 +03:00
}
2016-09-25 01:44:10 +03:00
func dehighlight() {
2016-09-25 18:50:33 +03:00
self.layer?.backgroundColor = NSColor.clear.cgColor
2016-09-25 01:44:10 +03:00
}
}
// MARK: - NSView
extension WorkspaceToolButton {
2016-09-18 21:49:42 +03:00
override var intrinsicContentSize: NSSize {
let titleSize = self.title.size()
let padding = WorkspaceToolButton.titlePadding
switch self.location {
case .top, .bottom:
return CGSize(width: titleSize.width + 2 * padding.width, height: titleSize.height + 2 * padding.height)
case .right, .left:
return CGSize(width: titleSize.height + 2 * padding.height, height: titleSize.width + 2 * padding.width)
}
}
2016-09-25 18:50:33 +03:00
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
2016-09-18 21:49:42 +03:00
let padding = WorkspaceToolButton.titlePadding
switch self.location {
case .top, .bottom:
2016-09-25 18:50:33 +03:00
self.title.draw(at: CGPoint(x: padding.width, y: padding.height))
2016-09-18 21:49:42 +03:00
case .right:
self.title.draw(at: CGPoint(x: padding.height, y: self.bounds.height - padding.width), angle: -CGFloat(M_PI_2))
case .left:
self.title.draw(at: CGPoint(x: self.bounds.width - padding.height, y: padding.width), angle: CGFloat(M_PI_2))
}
}
override func updateTrackingAreas() {
self.removeTrackingArea(self.trackingArea)
self.trackingArea = NSTrackingArea(rect: self.bounds,
2016-09-25 18:50:33 +03:00
options: [.mouseEnteredAndExited, .activeInActiveApp],
2016-09-18 21:49:42 +03:00
owner: self,
userInfo: nil)
self.addTrackingArea(self.trackingArea)
super.updateTrackingAreas()
}
2016-09-25 18:50:33 +03:00
override func mouseDown(with event: NSEvent) {
2016-11-14 00:16:56 +03:00
guard let nextEvent = self.window!.nextEvent(matching: [NSLeftMouseUpMask, NSLeftMouseDraggedMask]) else {
return
}
switch nextEvent.type {
case NSLeftMouseUp:
self.tool?.toggle()
return
case NSLeftMouseDragged:
let pasteboardItem = NSPasteboardItem()
pasteboardItem.setDataProvider(self, forTypes: [WorkspaceToolButton.toolUti])
let draggingItem = NSDraggingItem(pasteboardWriter: pasteboardItem)
draggingItem.setDraggingFrame(self.bounds, contents:self.snapshot())
self.beginDraggingSession(with: [draggingItem], event: nextEvent, source: self)
return
default:
return
}
2016-09-18 21:49:42 +03:00
}
2016-09-25 18:50:33 +03:00
override func mouseEntered(with _: NSEvent) {
2016-09-18 21:49:42 +03:00
if self.isSelected {
return
}
self.highlight()
}
2016-09-25 18:50:33 +03:00
override func mouseExited(with _: NSEvent) {
2016-09-18 21:49:42 +03:00
if self.isSelected {
return
}
self.dehighlight()
}
2016-11-14 00:16:56 +03:00
@objc(draggingSession:sourceOperationMaskForDraggingContext:)
func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor ctx: NSDraggingContext) -> NSDragOperation {
return .generic
}
// https://www.raywenderlich.com/136272/drag-and-drop-tutorial-for-macos
fileprivate func snapshot() -> NSImage {
let pdfData = self.dataWithPDF(inside: self.bounds)
let image = NSImage(data: pdfData)
return image ?? NSImage()
}
}
// MARK: - NSPasteboardItemDataProvider
extension WorkspaceToolButton {
func pasteboard(_ pasteboardOptional: NSPasteboard?, item: NSPasteboardItem, provideDataForType type: String) {
guard let pasteboard = pasteboardOptional else {
return
}
guard type == WorkspaceToolButton.toolUti else {
return
}
pasteboard.writeObjects([self.tool!.uuid as NSString])
2016-11-14 00:16:56 +03:00
}
2016-09-18 21:49:42 +03:00
}