Browse Source

Draggable clipview: we go again!

pull/101/head
Abhishek 4 years ago
parent
commit
ef38a4f3eb
  1. 4
      Clocker/Clocker/en.lproj/Panel.xib
  2. 64
      Clocker/Panel/Rate Controller/UpcomingEventView.swift

4
Clocker/Clocker/en.lproj/Panel.xib

@ -315,11 +315,11 @@
<subviews>
<scrollView wantsLayer="YES" borderType="none" autohidesScrollers="YES" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasVerticalScroller="NO" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="htc-pO-AqH">
<rect key="frame" x="5" y="30" width="340" height="50"/>
<clipView key="contentView" drawsBackground="NO" id="N1e-zE-F86">
<clipView key="contentView" drawsBackground="NO" id="N1e-zE-F86" customClass="DraggableClipView" customModule="Clocker" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="340" height="50"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView focusRingType="none" id="lxA-64-3QU">
<collectionView focusRingType="none" allowsEmptySelection="NO" id="lxA-64-3QU">
<rect key="frame" x="0.0" y="0.0" width="340" height="50"/>
<autoresizingMask key="autoresizingMask" heightSizable="YES"/>
<collectionViewFlowLayout key="collectionViewLayout" scrollDirection="horizontal" id="mgM-vQ-fB7">

64
Clocker/Panel/Rate Controller/UpcomingEventView.swift

@ -85,3 +85,67 @@ class ThinScroller: NSScroller {
path.fill()
}
}
class DraggableClipView: NSClipView {
private var clickPoint: NSPoint!
private var originalOrigin: NSPoint!
private var trackingArea: NSTrackingArea?
override func mouseDown(with event: NSEvent) {
print("2")
super.mouseDown(with: event)
clickPoint = event.locationInWindow
originalOrigin = bounds.origin
print("Click point \(clickPoint!)")
let keepOn = true
while keepOn {
let newEvent = window?.nextEvent(matching: [.leftMouseDragged, .leftMouseUp])
switch newEvent?.type {
case .leftMouseDragged:
print("Hello 1")
let scale = (superview as? NSScrollView)?.magnification ?? 1.0
let newPoint = event.locationInWindow
let newOrigin = NSPoint(x: originalOrigin.x + (clickPoint.x - newPoint.x) / scale,
y: originalOrigin.y + (clickPoint.y - newPoint.y) / scale)
let constrainedRect = constrainBoundsRect(NSRect(origin: newOrigin, size: bounds.size))
scroll(to: constrainedRect.origin)
superview?.reflectScrolledClipView(self)
return
default:
print("Hello2")
}
}
}
override func mouseDragged(with event: NSEvent) {
print("1")
super.mouseDragged(with: event)
// Account for a magnified parent scrollview.
let scale = (superview as? NSScrollView)?.magnification ?? 1.0
let newPoint = event.locationInWindow
let newOrigin = NSPoint(x: originalOrigin.x + (clickPoint.x - newPoint.x) / scale,
y: originalOrigin.y + (clickPoint.y - newPoint.y) / scale)
let constrainedRect = constrainBoundsRect(NSRect(origin: newOrigin, size: bounds.size))
scroll(to: constrainedRect.origin)
superview?.reflectScrolledClipView(self)
}
override func mouseUp(with event: NSEvent) {
print("3")
super.mouseUp(with: event)
clickPoint = nil
}
override func updateTrackingAreas() {
super.updateTrackingAreas()
if let trackingArea = self.trackingArea {
removeTrackingArea(trackingArea)
}
let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeAlways, .enabledDuringMouseDrag, .inVisibleRect, .activeInKeyWindow]
let trackingArea = NSTrackingArea(rect: bounds, options: options, owner: self, userInfo: nil)
addTrackingArea(trackingArea)
}
}

Loading…
Cancel
Save