Skip to content

Commit

Permalink
🔀 Merge pull request #52 from `MrKai77/45-ability-to-restore-initial-…
Browse files Browse the repository at this point in the history
…window-frame`

✨ #45 Ability to restore initial window frame
  • Loading branch information
MrKai77 authored Oct 4, 2023
2 parents 54d7ace + b26e0e6 commit 77ab0c1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 22 deletions.
4 changes: 4 additions & 0 deletions Loop/Extensions/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ extension Defaults.Keys {
"centerKeybind",
default: [[.kVK_Return]]
)
static let initialFrameKeybind = Key<[Set<CGKeyCode>]>(
"initialFrameKeybind",
default: [[.kVK_ANSI_I]]
)
static let lastDirectionKeybind = Key<[Set<CGKeyCode>]>(
"lastDirectionKeybind",
default: [[.kVK_ANSI_Z]]
Expand Down
22 changes: 15 additions & 7 deletions Loop/Managers/LoopManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class LoopManager {
private let previewController = PreviewController()

private var currentResizingDirection: WindowDirection = .noAction
private var actualCurrentResizingDirection: WindowDirection = .noAction // Used when direction is .lastDirection
private var isLoopShown: Bool = false
private var frontmostWindow: Window?
private var screenWithMouse: NSScreen?
Expand Down Expand Up @@ -69,13 +70,19 @@ class LoopManager {
if let direction = notification.userInfo?["direction"] as? WindowDirection {
self.currentResizingDirection = direction

if let window = self.frontmostWindow,
self.currentResizingDirection == .lastDirection {
if notification.userInfo?["isActualDirection"] as? Bool == nil ||
notification.userInfo?["isActualDirection"] as? Bool == true {
self.actualCurrentResizingDirection = direction
}

if let window = self.frontmostWindow, self.currentResizingDirection == .lastDirection {
// If the user sets .lastDirection as the last direction
self.currentResizingDirection = WindowRecords.getLastDirection(for: window)
DispatchQueue.main.async {
Notification.Name.directionChanged.post(userInfo: ["direction": self.currentResizingDirection])
Notification.Name.directionChanged.post(userInfo: [
"direction": self.currentResizingDirection,
"isActualDirection": false]
)
}
} else {
// Haptic feedback on the trackpad
Expand Down Expand Up @@ -134,8 +141,9 @@ class LoopManager {
}

private func openLoop() {
currentResizingDirection = .noAction
frontmostWindow = nil
self.currentResizingDirection = .noAction
self.actualCurrentResizingDirection = .noAction
self.frontmostWindow = nil

// Loop will only open if accessibility access has been granted
if PermissionsManager.Accessibility.getStatus() {
Expand Down Expand Up @@ -164,11 +172,11 @@ class LoopManager {
self.screenWithMouse != nil &&
forceClose == false &&
self.isLoopShown &&
self.currentResizingDirection != .noAction {
self.actualCurrentResizingDirection != .noAction {

isLoopShown = false

WindowEngine.resize(self.frontmostWindow!, to: self.currentResizingDirection, self.screenWithMouse!)
WindowEngine.resize(self.frontmostWindow!, to: self.actualCurrentResizingDirection, self.screenWithMouse!)
Notification.Name.didLoop.post()
Defaults[.timesLooped] += 1
iconManager.checkIfUnlockedNewIcon()
Expand Down
2 changes: 2 additions & 0 deletions Loop/Preview Window/PreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ struct PreviewView: View {
.padding(previewPadding + previewBorderThickness / 2)
.frame(width: currentResizeDirection == .noAction ? 0 : nil,
height: currentResizeDirection == .noAction ? 0 : nil)
.frame(width: currentResizeDirection == .initialFrame ? 0 : nil,
height: currentResizeDirection == .initialFrame ? 0 : nil)
.frame(width: currentResizeDirection == .center ?
(window?.size.width ?? 10) - previewPadding + previewBorderThickness / 2 :
nil,
Expand Down
14 changes: 9 additions & 5 deletions Loop/Window Management/WindowDirection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ enum WindowDirection: CaseIterable {
case noAction
case maximize
case fullscreen
case center
case lastDirection
case center
case initialFrame

// Halves
case topHalf
Expand Down Expand Up @@ -79,9 +80,10 @@ enum WindowDirection: CaseIterable {
switch self {
case .noAction: nil
case .maximize: "Maximize"
case .fullscreen: "Fullscreen"
case .center: "Center"
case .fullscreen: "Fullscreen"
case .lastDirection: nil
case .center: "Center"
case .initialFrame: "Restore Initial Frame"

case .topHalf: "Top Half"
case .rightHalf: "Right Half"
Expand Down Expand Up @@ -112,8 +114,9 @@ enum WindowDirection: CaseIterable {
case .noAction: [[]]
case .maximize: Defaults[.maximizeKeybind]
case .fullscreen: Defaults[.fullscreenKeybind]
case .center: Defaults[.centerKeybind]
case .lastDirection: Defaults[.lastDirectionKeybind]
case .center: Defaults[.centerKeybind]
case .initialFrame: Defaults[.initialFrameKeybind]

case .topHalf: Defaults[.topHalfKeybind]
case .rightHalf: Defaults[.rightHalfKeybind]
Expand Down Expand Up @@ -256,8 +259,9 @@ enum WindowDirection: CaseIterable {
case .noAction: nil
case .maximize: CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
case .fullscreen: CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
case .center: nil
case .lastDirection: nil
case .center: nil
case .initialFrame: nil

// Halves
case .topHalf: CGRect(x: 0, y: 0, width: 1.0, height: 1.0/2.0)
Expand Down
29 changes: 26 additions & 3 deletions Loop/Window Management/WindowEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ struct WindowEngine {
}

let oldWindowFrame = window.frame
guard let screenFrame = screen.safeScreenFrame,
let currentWindowFrame = WindowEngine.generateWindowFrame(oldWindowFrame, screenFrame, direction) else {
guard let screenFrame = screen.safeScreenFrame, let currentWindowFrame = WindowEngine.generateWindowFrame(
oldWindowFrame,
screenFrame,
direction,
window
) else {
return
}
var targetWindowFrame = WindowEngine.applyPadding(currentWindowFrame, direction)
Expand Down Expand Up @@ -129,7 +133,8 @@ struct WindowEngine {
private static func generateWindowFrame(
_ windowFrame: CGRect,
_ screenFrame: CGRect,
_ direction: WindowDirection
_ direction: WindowDirection,
_ window: Window
) -> CGRect? {
let screenWidth = screenFrame.size.width
let screenHeight = screenFrame.size.height
Expand All @@ -149,6 +154,24 @@ struct WindowEngine {
width: windowFrame.width,
height: windowFrame.height
)
case .lastDirection:
let previousDirection = WindowRecords.getLastDirection(for: window, willResize: true)
if let previousResizeFrame = self.generateWindowFrame(
windowFrame,
screenFrame,
previousDirection,
window
) {
newWindowFrame = previousResizeFrame
} else {
return nil
}
case .initialFrame:
if let initalFrame = WindowRecords.getInitialFrame(for: window) {
newWindowFrame = initalFrame
} else {
return nil
}
default:
guard let frameMultiplyValues = direction.frameMultiplyValues else { return nil}
newWindowFrame.origin.x += screenWidth * frameMultiplyValues.minX
Expand Down
33 changes: 26 additions & 7 deletions Loop/Window Management/WindowRecords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ struct WindowRecords {
var directionRecords: [WindowDirection]
}

static private func getIndex(of window: Window) -> Int? {
guard WindowRecords.hasBeenRecorded(window),
let idx = WindowRecords.records.firstIndex(where: { $0.cgWindowID == window.cgWindowID }) else {
return nil
}
return idx
}

/// This will erase ALL previous records of the window, and start a fresh new record for the selected window.
/// - Parameter window: The window to record
static func recordFirst(for window: Window) {
Expand All @@ -25,14 +33,13 @@ struct WindowRecords {
WindowRecords.Record(
cgWindowID: window.cgWindowID,
initialFrame: window.frame,
directionRecords: [.noAction]
directionRecords: [.initialFrame]
)
)
}

static func recordDirection(_ window: Window, _ direction: WindowDirection) {
guard WindowRecords.hasBeenRecorded(window),
let idx = WindowRecords.records.firstIndex(where: { $0.cgWindowID == window.cgWindowID }) else {
guard let idx = WindowRecords.getIndex(of: window) else {
return
}

Expand All @@ -43,12 +50,24 @@ struct WindowRecords {
return WindowRecords.records.contains(where: { $0.cgWindowID == window.cgWindowID })
}

static func getLastDirection(for window: Window) -> WindowDirection {
guard WindowRecords.hasBeenRecorded(window),
let idx = WindowRecords.records.firstIndex(where: { $0.cgWindowID == window.cgWindowID }) else {
static func getLastDirection(for window: Window, willResize: Bool = false) -> WindowDirection {
guard let idx = WindowRecords.getIndex(of: window),
WindowRecords.records[idx].directionRecords.count > 1 else {
return .noAction
}

return WindowRecords.records[idx].directionRecords[1]
let lastDirection = WindowRecords.records[idx].directionRecords[1]
if willResize && WindowRecords.records[idx].directionRecords.count > 2 {
WindowRecords.records[idx].directionRecords.removeFirst(2)
}
return lastDirection
}

static func getInitialFrame(for window: Window) -> CGRect? {
guard let idx = WindowRecords.getIndex(of: window) else {
return nil
}

return WindowRecords.records[idx].initialFrame
}
}

0 comments on commit 77ab0c1

Please sign in to comment.