From 61535ded8fa9cfca007e54c932030b90f3cc7df8 Mon Sep 17 00:00:00 2001 From: Kai Azim <68963405+MrKai77@users.noreply.github.com> Date: Mon, 11 Sep 2023 21:19:31 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Much=20smaller=20code=20for=20`g?= =?UTF-8?q?enerateWindowFrame()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Loop/Window Management/WindowDirection.swift | 34 +++++++++++ Loop/Window Management/WindowEngine.swift | 63 +++++++------------- 2 files changed, 54 insertions(+), 43 deletions(-) diff --git a/Loop/Window Management/WindowDirection.swift b/Loop/Window Management/WindowDirection.swift index 16ac8e6f..8350412f 100644 --- a/Loop/Window Management/WindowDirection.swift +++ b/Loop/Window Management/WindowDirection.swift @@ -243,4 +243,38 @@ enum WindowDirection: CaseIterable { return newDirection } + + var frameMultiplyValues: CGRect? { + switch self { + case .noAction: nil + case .maximize: CGRect(x: 0, y: 0, width: 1.0, height: 1.0) + case .center: nil + + // Halves + case .topHalf: CGRect(x: 0, y: 0, width: 1.0, height: 1.0/2.0) + case .rightHalf: CGRect(x: 1.0/2.0, y: 0, width: 1.0/2.0, height: 1.0) + case .bottomHalf: CGRect(x: 0, y: 1.0/2.0, width: 1.0, height: 1.0/2.0) + case .leftHalf: CGRect(x: 0, y: 0, width: 1.0/2.0, height: 1.0) + + // Quarters + case .topLeftQuarter: CGRect(x: 0, y: 0, width: 1.0/2.0, height: 1.0/2.0) + case .topRightQuarter: CGRect(x: 1.0/2.0, y: 0, width: 1.0/2.0, height: 1.0/2.0) + case .bottomRightQuarter: CGRect(x: 1.0/2.0, y: 1.0/2.0, width: 1.0/2.0, height: 1.0/2.0) + case .bottomLeftQuarter: CGRect(x: 0, y: 1.0/2.0, width: 1.0/2.0, height: 1.0/2.0) + + // Thirds (Horizontal) + case .rightThird: CGRect(x: 2.0/3.0, y: 0, width: 1.0/3.0, height: 1.0) + case .rightTwoThirds: CGRect(x: 1.0/3.0, y: 0, width: 2.0/3.0, height: 1.0) + case .horizontalCenterThird: CGRect(x: 1.0/3.0, y: 0, width: 1.0/3.0, height: 1.0) + case .leftThird: CGRect(x: 0, y: 0, width: 1.0/3.0, height: 1.0) + case .leftTwoThirds: CGRect(x: 0, y: 0, width: 2.0/3.0, height: 1.0) + + // Thirds (Vertical) + case .topThird: CGRect(x: 0, y: 0, width: 1.0, height: 1.0/3.0) + case .topTwoThirds: CGRect(x: 0, y: 0, width: 1.0, height: 2.0/3.0) + case .verticalCenterThird: CGRect(x: 0, y: 1.0/3.0, width: 1.0, height: 1.0/3.0) + case .bottomThird: CGRect(x: 0, y: 2.0/3.0, width: 1.0, height: 1.0/3.0) + case .bottomTwoThirds: CGRect(x: 0, y: 1.0/3.0, width: 1.0, height: 2.0/3.0) + } + } } diff --git a/Loop/Window Management/WindowEngine.swift b/Loop/Window Management/WindowEngine.swift index a8e994d1..f878ded3 100644 --- a/Loop/Window Management/WindowEngine.swift +++ b/Loop/Window Management/WindowEngine.swift @@ -105,61 +105,38 @@ struct WindowEngine { /// - screenFrame: The frame of the screen you want the window to be resized on /// - direction: WindowDirection /// - Returns: A CGRect of the generated frame. If direction was .noAction, nil is returned. - private static func generateWindowFrame(_ windowFrame: CGRect, _ screenFrame: CGRect, _ direction: WindowDirection) -> CGRect? { + private static func generateWindowFrame( + _ windowFrame: CGRect, + _ screenFrame: CGRect, + _ direction: WindowDirection + ) -> CGRect? { let screenWidth = screenFrame.size.width let screenHeight = screenFrame.size.height - let screenX = screenFrame.origin.x - let screenY = screenFrame.origin.y + + var newWindowFrame: CGRect = CGRect( + x: screenFrame.origin.x, + y: screenFrame.origin.y, + width: 0, + height: 0 + ) switch direction { - case .maximize: - return CGRect(x: screenX, y: screenY, width: screenWidth, height: screenHeight) case .center: - return CGRect( + newWindowFrame = CGRect( x: screenFrame.midX - windowFrame.width/2, y: screenFrame.midY - windowFrame.height/2, width: windowFrame.width, height: windowFrame.height ) - case .topHalf: - return CGRect(x: screenX, y: screenY, width: screenWidth, height: screenHeight/2) - case .rightHalf: - return CGRect(x: screenX+screenWidth/2, y: screenY, width: screenWidth/2, height: screenHeight) - case .bottomHalf: - return CGRect(x: screenX, y: screenY+screenHeight/2, width: screenWidth, height: screenHeight/2) - case .leftHalf: - return CGRect(x: screenX, y: screenY, width: screenWidth/2, height: screenHeight) - case .topRightQuarter: - return CGRect(x: screenX+screenWidth/2, y: screenY, width: screenWidth/2, height: screenHeight/2) - case .topLeftQuarter: - return CGRect(x: screenX, y: screenY, width: screenWidth/2, height: screenHeight/2) - case .bottomRightQuarter: - return CGRect(x: screenX+screenWidth/2, y: screenY+screenHeight/2, width: screenWidth/2, height: screenHeight/2) - case .bottomLeftQuarter: - return CGRect(x: screenX, y: screenY+screenHeight/2, width: screenWidth/2, height: screenHeight/2) - case .rightThird: - return CGRect(x: screenX+2*screenWidth/3, y: screenY, width: screenWidth/3, height: screenHeight) - case .rightTwoThirds: - return CGRect(x: screenX+screenWidth/3, y: screenY, width: 2*screenWidth/3, height: screenHeight) - case .horizontalCenterThird: - return CGRect(x: screenX+screenWidth/3, y: screenY, width: screenWidth/3, height: screenHeight) - case .leftThird: - return CGRect(x: screenX, y: screenY, width: screenWidth/3, height: screenHeight) - case .leftTwoThirds: - return CGRect(x: screenX, y: screenY, width: 2*screenWidth/3, height: screenHeight) - case .topThird: - return CGRect(x: screenX, y: screenY, width: screenWidth, height: screenHeight/3) - case .topTwoThirds: - return CGRect(x: screenX, y: screenY, width: screenWidth, height: 2*screenHeight/3) - case .verticalCenterThird: - return CGRect(x: screenX, y: screenY+screenHeight/3, width: screenWidth, height: screenHeight/3) - case .bottomThird: - return CGRect(x: screenX, y: screenY+2*screenHeight/3, width: screenWidth, height: screenHeight/3) - case .bottomTwoThirds: - return CGRect(x: screenX, y: screenY+screenHeight/3, width: screenWidth, height: 2*screenHeight/3) default: - return nil + guard let frameMultiplyValues = direction.frameMultiplyValues else { return nil} + newWindowFrame.origin.x += screenWidth * frameMultiplyValues.minX + newWindowFrame.origin.y += screenHeight * frameMultiplyValues.minY + newWindowFrame.size.width += screenWidth * frameMultiplyValues.width + newWindowFrame.size.height += screenHeight * frameMultiplyValues.height } + + return newWindowFrame } /// Apply padding on a CGRect, using the provided WindowDirection