-
-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…dth-or-height-of-window Double/Halve the Width/Height of a window
- Loading branch information
Showing
8 changed files
with
185 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Rectangle/WindowCalculation/ChangeWindowDimensionCalculation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// MinimumWindowDimensionAware.swift | ||
// Rectangle | ||
// | ||
// Created by Isaac Young on 23/04/24. | ||
// Copyright © 2024 Ryan Hanson. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol ChangeWindowDimensionCalculation { | ||
func resizedWindowRectIsTooSmall(windowRect: CGRect, visibleFrameOfScreen: CGRect) -> Bool; | ||
} | ||
|
||
extension ChangeWindowDimensionCalculation { | ||
private func minimumWindowWidth() -> CGFloat { | ||
let defaultWidth = Defaults.minimumWindowWidth.value | ||
return (defaultWidth <= 0 || defaultWidth > 1) | ||
? 0.25 | ||
: CGFloat(defaultWidth) | ||
} | ||
|
||
private func minimumWindowHeight() -> CGFloat { | ||
let defaultHeight = Defaults.minimumWindowHeight.value | ||
return (defaultHeight <= 0 || defaultHeight > 1) | ||
? 0.25 | ||
: CGFloat(defaultHeight) | ||
} | ||
|
||
func resizedWindowRectIsTooSmall(windowRect: CGRect, visibleFrameOfScreen: CGRect) -> Bool { | ||
let minimumWindowRectWidth = floor(visibleFrameOfScreen.width * minimumWindowWidth()) | ||
let minimumWindowRectHeight = floor(visibleFrameOfScreen.height * minimumWindowHeight()) | ||
return (windowRect.width <= minimumWindowRectWidth) || (windowRect.height <= minimumWindowRectHeight) | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
Rectangle/WindowCalculation/HalfOrDoubleDimensionCalculation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// HalfOrDoubleDimensionCalculation.swift | ||
// Rectangle | ||
// | ||
// Created by Isaac Young on 23/04/24. | ||
// Copyright © 2024 Ryan Hanson. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class HalfOrDoubleDimensionCalculation: WindowCalculation, ChangeWindowDimensionCalculation { | ||
|
||
override func calculateRect(_ params: RectCalculationParameters) -> RectResult { | ||
let window = params.window | ||
|
||
let resizedWindowRect = resized(window.rect, with: params.action) | ||
|
||
var resizedAndMovedWindowRect = repositionedIfRequired(original: window.rect, resized: resizedWindowRect, after: params.action) | ||
|
||
let visibleFrameOfScreen = params.visibleFrameOfScreen | ||
if isSizeReducing(params.action), resizedWindowRectIsTooSmall(windowRect: resizedAndMovedWindowRect, visibleFrameOfScreen: visibleFrameOfScreen) { | ||
resizedAndMovedWindowRect = window.rect | ||
} | ||
return RectResult(resizedAndMovedWindowRect) | ||
} | ||
|
||
private func isSizeReducing(_ action: WindowAction) -> Bool { | ||
switch (action) { | ||
case .halveHeightUp, .halveHeightDown, .halveWidthLeft, .halveWidthRight: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
private func resized(_ windowRect: CGRect, with action: WindowAction) -> CGRect { | ||
var resized = windowRect | ||
switch (action) { | ||
case .halveHeightUp, .halveHeightDown: | ||
resized.size.height = resized.height * 0.5 | ||
case .halveWidthLeft, .halveWidthRight: | ||
resized.size.width = resized.width * 0.5 | ||
case .doubleHeightUp, .doubleHeightDown: | ||
resized.size.height = resized.height * 2.0 | ||
case .doubleWidthLeft, .doubleWidthRight: | ||
resized.size.width = resized.width * 2.0 | ||
default: | ||
break | ||
} | ||
return resized | ||
} | ||
|
||
private func repositionedIfRequired(original originalWindowRect: CGRect, resized resizedWindowRect: CGRect, after action: WindowAction) -> CGRect { | ||
switch (action) { | ||
case .halveHeightUp: | ||
return resizedWindowRect.offsetBy(dx: 0, dy: resizedWindowRect.height) | ||
case .halveWidthRight: | ||
return resizedWindowRect.offsetBy(dx: resizedWindowRect.width, dy: 0) | ||
case .doubleHeightDown: | ||
return resizedWindowRect.offsetBy(dx: 0, dy: -originalWindowRect.height) | ||
case .doubleWidthLeft: | ||
return resizedWindowRect.offsetBy(dx: -originalWindowRect.width, dy: 0) | ||
default: | ||
return resizedWindowRect | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters