Skip to content

Commit

Permalink
🔀 Merge pull request #61 from `MrKai77/60-reset-windows-resize-histor…
Browse files Browse the repository at this point in the history
…y-once-it-has-been-movedresized`

✨ #60 Reset Window's resize history once it has been moved/resized
  • Loading branch information
MrKai77 authored Oct 21, 2023
2 parents 2a8e642 + e5868d2 commit 37b6794
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 27 deletions.
8 changes: 6 additions & 2 deletions Loop/Window Management/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ class Window {
return CGRect(origin: self.position, size: self.size)
}

func setFrame(_ rect: CGRect, animate: Bool = false) {
func setFrame(_ rect: CGRect, animate: Bool = false, completionHandler: (() -> Void)? = nil) {
if animate {
let animation = WindowTransformAnimation(rect, window: self)
let animation = WindowTransformAnimation(rect, window: self, completionHandler: completionHandler)
animation.startInBackground()
} else {
self.setPosition(rect.origin)
self.setSize(rect.size)

if let completionHandler = completionHandler {
completionHandler()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Loop/Window Management/WindowDirection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ enum WindowDirection: CaseIterable, Identifiable {

func getActualDirection(window: Window) -> WindowDirection {
let lastDirection: WindowDirection = WindowRecords.getLastDirection(for: window, offset: 0, canBeCycling: true)
print(lastDirection)

var actualDirection: WindowDirection = self
switch self {
case .cycleTop:
Expand Down
12 changes: 7 additions & 5 deletions Loop/Window Management/WindowEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ struct WindowEngine {
targetWindowFrame.origin.y = screen.frame.maxY - minSize.height - Defaults[.windowPadding]
}

window.setFrame(targetWindowFrame, animate: true)
WindowRecords.recordDirection(window, actualDirection)
window.setFrame(targetWindowFrame, animate: true) {
WindowRecords.recordDirection(window, actualDirection)
}
}
} else {
window.setFrame(targetWindowFrame)
WindowEngine.handleSizeConstrainedWindow(window: window, screenFrame: screenFrame)
WindowRecords.recordDirection(window, actualDirection)
window.setFrame(targetWindowFrame) {
WindowEngine.handleSizeConstrainedWindow(window: window, screenFrame: screenFrame)
WindowRecords.recordDirection(window, actualDirection)
}
}
}

Expand Down
42 changes: 24 additions & 18 deletions Loop/Window Management/WindowRecords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct WindowRecords {
struct Record {
var cgWindowID: CGWindowID
var initialFrame: CGRect
var currentFrame: CGRect
var directionRecords: [DirectionRecord]
}

Expand All @@ -26,38 +27,43 @@ struct WindowRecords {
var isCycling: Bool
}

static private func getIndex(of window: Window) -> Int? {
guard WindowRecords.hasBeenRecorded(window),
let idx = WindowRecords.records.firstIndex(where: { $0.cgWindowID == window.cgWindowID }) else {
static func hasBeenRecorded(_ window: Window) -> Bool {
return WindowRecords.records.contains(where: { record in
return record.cgWindowID == window.cgWindowID && record.currentFrame == window.frame
})
}

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

/// 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) {

WindowRecords.records.removeAll(where: { $0.cgWindowID == window.cgWindowID })
let frame = window.frame

WindowRecords.records.append(
WindowRecords.Record(
cgWindowID: window.cgWindowID,
initialFrame: window.frame,
initialFrame: frame,
currentFrame: frame,
directionRecords: [DirectionRecord(.initialFrame)]
)
)
}

static func recordDirection(_ window: Window, _ direction: WindowDirection, isCycling: Bool = false) {
guard let idx = WindowRecords.getIndex(of: window) else {
guard let id = WindowRecords.findRecordsID(for: window) else {
return
}

WindowRecords.records[idx].directionRecords.insert(DirectionRecord(direction, isCycling), at: 0)
}

static func hasBeenRecorded(_ window: Window) -> Bool {
return WindowRecords.records.contains(where: { $0.cgWindowID == window.cgWindowID })
WindowRecords.records[id].directionRecords.insert(DirectionRecord(direction, isCycling), at: 0)
WindowRecords.records[id].currentFrame = window.frame
}

static func getLastDirection(
Expand All @@ -66,11 +72,11 @@ struct WindowRecords {
offset: Int = 1,
canBeCycling: Bool = false
) -> WindowDirection {
guard let idx = WindowRecords.getIndex(of: window),
WindowRecords.records[idx].directionRecords.count > offset else {
guard let id = WindowRecords.findRecordsID(for: window),
WindowRecords.records[id].directionRecords.count > offset else {
return .noAction
}
let directionRecords = WindowRecords.records[idx].directionRecords
let directionRecords = WindowRecords.records[id].directionRecords
var lastDirection = directionRecords[offset]
var actualOffset = offset

Expand All @@ -79,18 +85,18 @@ struct WindowRecords {
lastDirection = directionRecords[actualOffset]
}

if willResize && WindowRecords.records[idx].directionRecords.count > actualOffset + 1 {
WindowRecords.records[idx].directionRecords.removeFirst(actualOffset + 1)
if willResize && WindowRecords.records[id].directionRecords.count > actualOffset + 1 {
WindowRecords.records[id].directionRecords.removeFirst(actualOffset + 1)
}

return lastDirection.direction
}

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

return WindowRecords.records[idx].initialFrame
return WindowRecords.records[id].initialFrame
}
}
8 changes: 7 additions & 1 deletion Loop/Window Management/WindowTransformAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class WindowTransformAnimation: NSAnimation {
private var targetFrame: CGRect
private let oldFrame: CGRect
private let window: Window
private let completionHandler: (() -> Void)?

init(_ newRect: CGRect, window: Window) {
init(_ newRect: CGRect, window: Window, completionHandler: (() -> Void)? = nil) {
self.targetFrame = newRect
self.oldFrame = window.frame
self.window = window
self.completionHandler = completionHandler
super.init(duration: 0.3, animationCurve: .linear)
self.frameRate = 60.0
self.animationBlockingMode = .nonblocking
Expand Down Expand Up @@ -44,6 +46,10 @@ class WindowTransformAnimation: NSAnimation {
)

window.setFrame(newFrame)

if let completionHandler = completionHandler, currentProgress == 1 {
completionHandler()
}
}
}
}

0 comments on commit 37b6794

Please sign in to comment.