Skip to content

Commit

Permalink
Added gaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Hanson committed Jan 14, 2020
1 parent c5410b4 commit 6c71ab3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Rectangle.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
98C2755E231FF6A9009B9292 /* EventMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98C2755D231FF6A9009B9292 /* EventMonitor.swift */; };
98C27561231FFA5F009B9292 /* SnappingManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98C27560231FFA5F009B9292 /* SnappingManager.swift */; };
98C275672322E2DA009B9292 /* WindowHistory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98C275662322E2DA009B9292 /* WindowHistory.swift */; };
98C6DEF023CE191700CC0C1E /* GapCalculation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98C6DEEF23CE191700CC0C1E /* GapCalculation.swift */; };
98FA9497235A2D7600F95C4F /* RepeatedExecutionsCalculation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98FA9496235A2D7600F95C4F /* RepeatedExecutionsCalculation.swift */; };
CC0B7937429AC28C21ABF5B4 /* Pods_RectangleLauncher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F2D8480CC730811C953FC1B6 /* Pods_RectangleLauncher.framework */; };
F0A0DFB36FCC3FCE6E184500 /* Pods_Rectangle.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 20A533B9F2D3215AC7B85D1F /* Pods_Rectangle.framework */; };
Expand Down Expand Up @@ -188,6 +189,7 @@
98C2755D231FF6A9009B9292 /* EventMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventMonitor.swift; sourceTree = "<group>"; };
98C27560231FFA5F009B9292 /* SnappingManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnappingManager.swift; sourceTree = "<group>"; };
98C275662322E2DA009B9292 /* WindowHistory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowHistory.swift; sourceTree = "<group>"; };
98C6DEEF23CE191700CC0C1E /* GapCalculation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GapCalculation.swift; sourceTree = "<group>"; };
98ED1C722393DEE600CD0955 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/Main.strings; sourceTree = "<group>"; };
98ED1C732393DEE600CD0955 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/Main.strings; sourceTree = "<group>"; };
98ED1C742393DEF200CD0955 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Main.strings"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -299,6 +301,7 @@
988D067C22EB4E17004EABD7 /* AlmostMaximizeCalculation.swift */,
98C1008D230B9EF6006E5344 /* NextPrevDisplayCalculation.swift */,
98FA9496235A2D7600F95C4F /* RepeatedExecutionsCalculation.swift */,
98C6DEEF23CE191700CC0C1E /* GapCalculation.swift */,
);
path = WindowCalculation;
sourceTree = "<group>";
Expand Down Expand Up @@ -730,6 +733,7 @@
9824702C22AFA22E0037B409 /* AccessibilityWindowController.swift in Sources */,
988D066122EB4C7C004EABD7 /* FirstThirdCalculation.swift in Sources */,
988D067F22EB4EDE004EABD7 /* MoveLeftRightCalculation.swift in Sources */,
98C6DEF023CE191700CC0C1E /* GapCalculation.swift in Sources */,
98910B3E231130AF0066EC23 /* SettingsViewController.swift in Sources */,
9824700D22AF9B7D0037B409 /* AppDelegate.swift in Sources */,
9824704F22B189250037B409 /* BestEffortWindowMover.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Rectangle/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Defaults {
static let windowSnapping = OptionalBoolDefault(key: "windowSnapping")
static let almostMaximizeHeight = FloatDefault(key: "almostMaximizeHeight")
static let almostMaximizeWidth = FloatDefault(key: "almostMaximizeWidth")
static let gapSize = FloatDefault(key: "gapSize")
}

class BoolDefault {
Expand Down
10 changes: 10 additions & 0 deletions Rectangle/WindowAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ enum WindowAction: Int {
}
}

var gapEdge: Edge {
switch self {
case .leftHalf: return .right
case .rightHalf: return .left
case .bottomHalf: return .bottom
case .topHalf: return .top
default:
return .none
}
}
}

struct Shortcut {
Expand Down
68 changes: 68 additions & 0 deletions Rectangle/WindowCalculation/GapCalculation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// GapCalculation.swift
// Rectangle
//
// Created by Ryan Hanson on 1/14/20.
// Copyright © 2020 Ryan Hanson. All rights reserved.
//

import Foundation

class GapCalculation {

static func applyGaps(_ rect: CGRect, sharedEdges: Edge = .none, gapSize: Float) -> CGRect {

let cgGapSize = CGFloat(gapSize)
var withGaps = rect.insetBy(dx: cgGapSize, dy: cgGapSize)

if (sharedEdges.contains(.left)) {
withGaps = CGRect(
x: withGaps.origin.x - (cgGapSize / 2),
y: withGaps.origin.y,
width: withGaps.width + (cgGapSize / 2),
height: withGaps.height
)
}

if (sharedEdges.contains(.right)) {
withGaps = CGRect(
x: withGaps.origin.x,
y: withGaps.origin.y,
width: withGaps.width + (cgGapSize / 2),
height: withGaps.height
)
}

if (sharedEdges.contains(.top)) {
withGaps = CGRect(
x: withGaps.origin.x,
y: withGaps.origin.y - (cgGapSize / 2),
width: withGaps.width,
height: withGaps.height + (cgGapSize / 2)
)
}

if (sharedEdges.contains(.bottom)) {
withGaps = CGRect(
x: withGaps.origin.x,
y: withGaps.origin.y,
width: withGaps.width,
height: withGaps.height + (cgGapSize / 2)
)
}

return withGaps
}
}

struct Edge: OptionSet {
let rawValue: Int

static let left = Edge(rawValue: 1 << 0)
static let right = Edge(rawValue: 1 << 1)
static let top = Edge(rawValue: 1 << 2)
static let bottom = Edge(rawValue: 1 << 3)

static let all: Edge = [.left, .right, .top, .bottom]
static let none: Edge = []
}
2 changes: 1 addition & 1 deletion Rectangle/WindowCalculation/WindowCalculation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension WindowCalculation {
}

struct WindowCalculationResult {
let rect: CGRect
var rect: CGRect
let screen: NSScreen
let resultingAction: WindowAction
}
Expand Down
7 changes: 6 additions & 1 deletion Rectangle/WindowManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class WindowManager {
private let fixedSizeWindowMoverChain: [WindowMover]
private let windowCalculationFactory: WindowCalculationFactory
private let windowHistory: WindowHistory
private let gapSize = Defaults.gapSize.value

init(windowCalculationFactory: WindowCalculationFactory, windowHistory: WindowHistory) {
self.windowCalculationFactory = windowCalculationFactory
Expand Down Expand Up @@ -85,10 +86,14 @@ class WindowManager {

let windowCalculation = windowCalculationFactory.calculation(for: action)

guard let calcResult = windowCalculation?.calculate(currentNormalizedRect, lastAction: lastRectangleAction, usableScreens: usableScreens, action: action) else {
guard var calcResult = windowCalculation?.calculate(currentNormalizedRect, lastAction: lastRectangleAction, usableScreens: usableScreens, action: action) else {
NSSound.beep()
return
}

if gapSize > 0, calcResult.resultingAction.gapEdge != .none {
calcResult.rect = GapCalculation.applyGaps(calcResult.rect, sharedEdges: calcResult.resultingAction.gapEdge, gapSize: gapSize)
}

let newNormalizedRect = AccessibilityElement.normalizeCoordinatesOf(calcResult.rect, frameOfScreen: usableScreens.frameOfCurrentScreen)

Expand Down

0 comments on commit 6c71ab3

Please sign in to comment.