Skip to content

Commit

Permalink
🔀 Merge pull request #647 from MrKai77/mrkai77/loop-684-undo-only-res…
Browse files Browse the repository at this point in the history
…tores-the-last-window-movement-with-preview

🐞 Don't record every single step of cycle when preview is disabled
  • Loading branch information
MrKai77 authored Dec 28, 2024
2 parents d7eec27 + f21371f commit cd339a2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
1 change: 0 additions & 1 deletion Loop/Extensions/Notification+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ extension Notification.Name {
static let updateUIDirection = Notification.Name("updateUIDirection")

static let forceCloseLoop = Notification.Name("forceCloseLoop")
static let didLoop = Notification.Name("didLoop")
static let activeStateChanged = Notification.Name("activeStateChanged")

static let systemWindowManagerStateChanged = Notification.Name("systemWindowManagerStateChanged")
Expand Down
1 change: 1 addition & 0 deletions Loop/Luminare/Theming/IconConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ struct IconVew: View {
.font(.caption)
.foregroundColor(.secondary)
.contentTransition(.numericText())
.multilineTextAlignment(.center)

Spacer()
}
Expand Down
30 changes: 20 additions & 10 deletions Loop/Managers/LoopManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ private extension LoopManager {
return
}

// Record the first frame in advance if the preview window is disabled
if let targetWindow,
!WindowRecords.hasBeenRecorded(targetWindow),
!Defaults[.previewVisibility] {
WindowRecords.recordFirst(for: targetWindow)
}

// Only recalculate wallpaper colors if Loop was last triggered over 5 seconds ago
if Defaults[.processWallpaper], lastLoopActivation.distance(to: .now) > 5.0 {
Task {
Expand Down Expand Up @@ -160,23 +167,24 @@ private extension LoopManager {

currentlyPressedModifiers = []

if targetWindow != nil,
screenToResizeOn != nil,
if let targetWindow,
let screenToResizeOn,
forceClose == false,
currentAction.direction != .noAction,
isLoopActive {
if let screenToResizeOn,
Defaults[.previewVisibility] {
if Defaults[.previewVisibility] {
WindowEngine.resize(
targetWindow!,
targetWindow,
to: currentAction,
on: screenToResizeOn
)
} else {
WindowRecords.record(
targetWindow,
currentAction
)
}

// This rotates the menubar icon
Notification.Name.didLoop.post()

// Icon stuff
Defaults[.timesLooped] += 1
IconManager.checkIfUnlockedNewIcon()
Expand Down Expand Up @@ -403,7 +411,8 @@ private extension LoopManager {
WindowEngine.resize(
window,
to: currentAction,
on: screenToResizeOn
on: screenToResizeOn,
shouldRecord: false
)
}
}
Expand Down Expand Up @@ -433,7 +442,8 @@ private extension LoopManager {
WindowEngine.resize(
window,
to: self.currentAction,
on: screenToResizeOn
on: screenToResizeOn,
shouldRecord: false
)
}
}
Expand Down
41 changes: 19 additions & 22 deletions Loop/Window Management/WindowEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ enum WindowEngine {
/// - window: Window to be resized
/// - direction: WindowDirection
/// - screen: Screen the window should be resized on
/// - shouldRecord: only set to false when preview window is disabled (so live preview)
static func resize(
_ window: Window,
to action: WindowAction,
on screen: NSScreen
on screen: NSScreen,
shouldRecord: Bool = true
) {
guard action.direction != .noAction else { return }
let willChangeScreens = ScreenManager.screenContaining(window) != screen
Expand All @@ -30,45 +32,43 @@ enum WindowEngine {
window.activate()
}

// If the action is to hide or minimize, perform the action then return
if action.direction == .hide {
window.toggleHidden()
return
}

if action.direction == .minimize {
window.toggleMinimized()
return
}

if shouldRecord {
WindowRecords.record(window, action)
}

if #available(macOS 15, *), Defaults[.useSystemWindowManagerWhenAvailable], !willChangeScreens {
SystemWindowManager.MoveAndResize.syncPadding()

// System resizes seem to only be able to be performed on the frontmost app
if let systemAction = action.direction.systemEquivalent, let app = window.nsRunningApplication,
app == NSWorkspace.shared.frontmostApplication, let axMenuItem = try? systemAction.getItem(for: app) {
try? axMenuItem.performAction(.press)
WindowRecords.record(window, action)
return
} else {
print("System action not available for \(action.direction)")
}
}

// If window hasn't been recorded yet, record it, so that the user can undo the action
if !WindowRecords.hasBeenRecorded(window) {
WindowRecords.recordFirst(for: window)
}

// If the action is fullscreen, toggle fullscreen then return
if action.direction == .fullscreen {
window.toggleFullscreen()
WindowRecords.record(window, action)
return
}

// Otherwise, we obviously need to disable fullscreen to resize the window
window.fullscreen = false

// If the action is to hide or minimize, perform the action then return
if action.direction == .hide {
window.toggleHidden()
return
}

if action.direction == .minimize {
window.toggleMinimized()
return
}

// Calculate the target frame
let targetFrame = action.getFrame(window: window, bounds: screen.safeScreenFrame, screen: screen)
print("Target window frame: \(targetFrame)")
Expand All @@ -82,8 +82,6 @@ enum WindowEngine {
let enhancedUI = window.enhancedUserInterface
let animate = Defaults[.animateWindowResizes] && !enhancedUI

WindowRecords.record(window, action)

// If the window is one of Loop's windows, resize it using the actual NSWindow, preventing crashes
if window.nsRunningApplication?.bundleIdentifier == Bundle.main.bundleIdentifier,
let window = NSApp.keyWindow ?? NSApp.windows.first(where: { $0.level.rawValue <= NSWindow.Level.floating.rawValue }) {
Expand Down Expand Up @@ -116,7 +114,6 @@ enum WindowEngine {
// Fixes an issue where window isn't resized correctly on multi-monitor setups
// If window is being animated, then the size is very likely to already be correct, as what's really happening is window.setFrame at a really high rate.
if !animate, !window.frame.approximatelyEqual(to: targetFrame) {
print("Backup resizing...")
window.setFrame(targetFrame)
}

Expand Down
5 changes: 5 additions & 0 deletions Loop/Window Management/WindowRecords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ enum WindowRecords {
/// - window: Window to record
/// - action: WindowAction to record
static func record(_ window: Window, _ action: WindowAction) {
/// If the window has not been recorded, record it
if !WindowRecords.hasBeenRecorded(window) {
WindowRecords.recordFirst(for: window)
}

guard
action.direction != .undo, // There is no point in recording undos
let id = WindowRecords.findRecordsID(for: window)
Expand Down

0 comments on commit cd339a2

Please sign in to comment.