Skip to content

Commit

Permalink
Improve resizing and moving windows
Browse files Browse the repository at this point in the history
Improve move and resize algorithms when setting a new size to the window
  • Loading branch information
zenangst committed May 21, 2020
1 parent 4dfb07e commit d7d7eb9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
32 changes: 21 additions & 11 deletions Source/MouseController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ final class MouseController {
return
}

self.delta = mouse.location

switch state {
case .ended:
endSession()
Expand Down Expand Up @@ -86,24 +88,24 @@ final class MouseController {
}

private func move(_ elementWindow: AccessibilityElement, with mouse: Mouse) {
guard var windowPosition = elementWindow.position,
let delta = self.delta else { return }
let point = mouse.location

guard var windowPosition = elementWindow.position else { return }

if let delta = self.delta {
let newDelta = CGPoint(x: delta.x - point.x, y: delta.y - point.y)
windowPosition.x -= newDelta.x
windowPosition.y -= newDelta.y
elementWindow.position = windowPosition
}
let x: CGFloat = delta.x.round(nearest: 1.0) - point.x.round(nearest: 1.0)
let y: CGFloat = delta.y.round(nearest: 1.0) - point.y.round(nearest: 1.0)
let newDelta = CGPoint(x: x, y: y)
windowPosition.x -= newDelta.x
windowPosition.y -= newDelta.y
elementWindow.position = windowPosition
}

private func resize(_ elementWindow: AccessibilityElement, with mouse: Mouse) {
guard var windowSize = elementWindow.size,
let delta = self.delta else { return }

let point = mouse.location
let newDelta = CGPoint(x: round(delta.x - point.x), y: round(delta.y - point.y))
let x: CGFloat = delta.x.round(nearest: 1.0) - point.x.round(nearest: 1.0)
let y: CGFloat = delta.y.round(nearest: 1.0) - point.y.round(nearest: 1.0)
let newDelta = CGPoint(x: x, y: y)

switch resizeBehavior {
case .standard:
Expand Down Expand Up @@ -166,3 +168,11 @@ fileprivate extension CGRect {
return .fourth
}
}

extension CGFloat {
func round(nearest: CGFloat) -> CGFloat {
let n = 1 / nearest
let numberToRound = self * n
return numberToRound.rounded() / n
}
}
2 changes: 1 addition & 1 deletion XcodeGen/MouseDef.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ targets:
settings:
base:
PRODUCT_BUNDLE_IDENTIFIER: "com.zenangst.MouseDef"
MARKETING_VERSION: "0.0.3"
MARKETING_VERSION: "0.0.4"
CURRENT_PROJECT_VERSION: 1
INFOPLIST_FILE: "Resources/Info.plist"
ASSETCATALOG_COMPILER_APPICON_NAME: "Assets"
Expand Down

0 comments on commit d7d7eb9

Please sign in to comment.