             screenManager.displayLayoutHUD()
         }
     }
+
+    func swapInTab(window: SIWindow) {
+        guard let screen = window.screen() else {
+            return
+        }
+
+        // We do this to avoid triggering tab swapping when just switching focus between apps.
+        // If the window's app is not running by this point then it's not a tab switch.
+        guard let runningApp = NSRunningApplication(processIdentifier: window.processIdentifier()), runningApp.isActive else {
+            return
+        }
+
+        // We take the windows that are being tracked so we can properly detect when a tab switch is a new tab.
+        let applicationWindows = windows.filter { $0.processIdentifier() == window.processIdentifier() }
+
+        for existingWindow in applicationWindows {
+            guard existingWindow != window else {
+                continue
+            }
+
+            let didLeaveScreen = windowIsActive(existingWindow) && !existingWindow.isOnScreen()
+            let isInvalid = existingWindow.windowID() == kCGNullWindowID
+
+            // The window needs to have either left the screen and therefore is being replaced
+            // or be invalid and therefore being removed and can be replaced.
+            guard didLeaveScreen || isInvalid else {
+                continue
+            }
+
+            // We need to tolerate a bit more height because a window that goes from untabbed to tabbed can change
+            // the height of the titlebar (e.g., Terminal)
+            let tolerance = CGRect(x: 10, y: 10, width: 10, height: 30)
+            let isApproximatelyInFrame = existingWindow.frame().approximatelyEqual(to: window.frame(), within: tolerance)
+
+            // If the window is in the same position and is going off screen it is likely a tab being replaced
+            guard isApproximatelyInFrame || isInvalid else {
+                continue
+            }
+
+            // We have to make sure that we haven't had a focus change too recently as that could mean
+            // the window is already active, but just became focused by swapping window focus.
+            // The time is in seconds, and too long a time ends up with quick switches triggering tabs to incorrectly
+            // swap.
+            if let lastFocusChange = lastFocusDate, abs(lastFocusChange.timeIntervalSinceNow) < 0.1 && !isInvalid {
+                continue
+            }
+
+            // Add the new window to be tracked, swap it with the existing window, regenerate cache to account
+            // for the change, and then reflow.
+            addWindow(window)
+            switchWindow(existingWindow, with: window)
+            regenerateActiveIDCache()
+            markScreenForReflow(screen, withChange: .unknown)
+
+            return
+        }
+
+        // If we've reached this point we haven't found any tab to switch out, but this window could still be new.
+        addWindow(window)
+    }
 }
 
 extension WindowManager {

            default:
                break
            }
        }
    }
}

        case applicationActivated
        case windowMoved
        case windowResized
        case mainWindowChanged

        var string: String {
            switch self {
