Skip to content

Commit

Permalink
Bounds detection improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
xzzz9097 committed Jan 17, 2017
1 parent dd90948 commit a3fd8aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
40 changes: 27 additions & 13 deletions Muse/NSTouch+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,47 @@
@available(OSX 10.12.2, *)
extension NSTouch {

// MARK: Extended variables

// The maximum X treshold for considering
// the touch out of bounds
static let xBoundsThreshold: CGFloat = 25

// MARK: Extended functions

/**
Determines whether a touch is going to go out of the bounds of a view,
that is, if we are too close to its right or left margin.
The margin size is defined in 'xBoundsThreshold'
- parameter view: the view that we're touching
- parameter xThreshold: the x margin size
- parameter yThreshold: the y margin size
- returns: if the touches are going out of the view
*/
func isGoingOutOfXBounds(of view: NSView?) -> Bool {
func isGoingOutOfBounds(of view: NSView?, with xThreshold: CGFloat, _ yThreshold: CGFloat) -> Bool {
guard let view = view else { return false }

return isGoingOutOfXLowerBound(of: view) || isGoingOutOfXUpperBound(of: view)
return isGoingOutOfXBounds(of: view, with: xThreshold) ||
isGoingOutOfYBounds(of: view, with: yThreshold)
}

func isGoingOutOfXBounds(of view: NSView, with threshold: CGFloat) -> Bool {
return isGoingOutOfXLowerBound(of: view, with: threshold) ||
isGoingOutOfXUpperBound(of: view, with: threshold)
}

func isGoingOutOfYBounds(of view: NSView, with threshold: CGFloat) -> Bool {
return isGoingOutOfYLowerBound(of: view, with: threshold) ||
isGoingOutOfXUpperBound(of: view, with: threshold)
}

func isGoingOutOfXLowerBound(of view: NSView, with threshold: CGFloat) -> Bool {
return self.location(in: view).x < threshold
}

func isGoingOutOfXUpperBound(of view: NSView, with threshold: CGFloat) -> Bool {
return self.location(in: view).x > view.bounds.maxX - threshold
}

func isGoingOutOfXLowerBound(of view: NSView) -> Bool {
return self.location(in: view).x < NSTouch.xBoundsThreshold
func isGoingOutOfYLowerBound(of view: NSView, with threshold: CGFloat) -> Bool {
return self.location(in: view).y > threshold
}

func isGoingOutOfXUpperBound(of view: NSView) -> Bool {
return self.location(in: view).x > view.bounds.maxX - NSTouch.xBoundsThreshold
func isGoingOutOfYUpperBound(of view: NSView, with threshold: CGFloat) -> Bool {
return self.location(in: view).y > view.bounds.maxY - threshold
}

}
8 changes: 5 additions & 3 deletions Muse/WindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class WindowController: NSWindowController, NSWindowDelegate {
let kShouldSetTitleOnMenuBar = true
// Constant for setting menu title length
let kMenuItemMaximumLength = 20
// Constant for TouchBar slider bounds
let xSliderBoundsThreshold: CGFloat = 25

// MARK: Outlets

Expand Down Expand Up @@ -94,12 +96,12 @@ class WindowController: NSWindowController, NSWindowDelegate {
let currentEvent = NSApplication.shared().currentEvent else { return }

for event in currentEvent.touches(matching: .touching, in: slider) {
if event.isGoingOutOfXLowerBound(of: slider) {
if event.isGoingOutOfXLowerBound(of: slider, with: xSliderBoundsThreshold) {
// Too far left in the slider -> jump to start
helper.scrub(to: 0)
} else if event.isGoingOutOfXUpperBound(of: slider) {
} else if event.isGoingOutOfXUpperBound(of: slider, with: xSliderBoundsThreshold) {
// Too far right in the slider -> jump to end
helper.scrub(to: 1)
helper.scrub(to: 0.99)
} else {
// Detected touch phase start
helper.scrub(to: slider.doubleValue, touching: true)
Expand Down

0 comments on commit a3fd8aa

Please sign in to comment.