diff --git a/Sources/AppBundle/layout/layoutRecursive.swift b/Sources/AppBundle/layout/layoutRecursive.swift index face0a784..1f3b685a5 100644 --- a/Sources/AppBundle/layout/layoutRecursive.swift +++ b/Sources/AppBundle/layout/layoutRecursive.swift @@ -69,16 +69,23 @@ extension Window { @MainActor fileprivate func layoutFloatingWindow(_ context: LayoutContext) async throws { let workspace = context.workspace - let currentMonitor = try await getCenter()?.monitorApproximation // Probably not idempotent - if let currentMonitor, let windowTopLeftCorner = try await getAxTopLeftCorner(), workspace != currentMonitor.activeWorkspace { + let windowRect = try await getAxRect() // Probably not idempotent + let currentMonitor = windowRect?.center.monitorApproximation + if let currentMonitor, let windowRect, workspace != currentMonitor.activeWorkspace { + let windowTopLeftCorner = windowRect.topLeftCorner let xProportion = (windowTopLeftCorner.x - currentMonitor.visibleRect.topLeftX) / currentMonitor.visibleRect.width let yProportion = (windowTopLeftCorner.y - currentMonitor.visibleRect.topLeftY) / currentMonitor.visibleRect.height - let moveTo = workspace.workspaceMonitor - setAxFrame(CGPoint( - x: moveTo.visibleRect.topLeftX + xProportion * moveTo.visibleRect.width, - y: moveTo.visibleRect.topLeftY + yProportion * moveTo.visibleRect.height, - ), nil) + let workspaceRect = workspace.workspaceMonitor.visibleRect + var newX = workspaceRect.topLeftX + xProportion * workspaceRect.width + var newY = workspaceRect.topLeftY + yProportion * workspaceRect.height + + let windowWidth = windowRect.width + let windowHeight = windowRect.height + newX = newX.coerceIn(workspaceRect.minX ... max(workspaceRect.minX, workspaceRect.maxX - windowWidth)) + newY = newY.coerceIn(workspaceRect.minY ... max(workspaceRect.minY, workspaceRect.maxY - windowHeight)) + + setAxFrame(CGPoint(x: newX, y: newY), nil) } if isFullscreen { layoutFullscreen(context) diff --git a/Sources/AppBundle/tree/MacWindow.swift b/Sources/AppBundle/tree/MacWindow.swift index bd702c0e6..a8d70fe92 100644 --- a/Sources/AppBundle/tree/MacWindow.swift +++ b/Sources/AppBundle/tree/MacWindow.swift @@ -163,11 +163,16 @@ final class MacWindow: Window { // Tiling windows should be unhidden with layoutRecursive anyway case .floatingWindow: let workspaceRect = nodeWorkspace.workspaceMonitor.rect - let pointInsideWorkspace = CGPoint( - x: workspaceRect.width * prevUnhiddenProportionalPositionInsideWorkspaceRect.x, - y: workspaceRect.height * prevUnhiddenProportionalPositionInsideWorkspaceRect.y, - ) - setAxFrame(workspaceRect.topLeftCorner + pointInsideWorkspace, nil) + var newX = workspaceRect.topLeftX + workspaceRect.width * prevUnhiddenProportionalPositionInsideWorkspaceRect.x + var newY = workspaceRect.topLeftY + workspaceRect.height * prevUnhiddenProportionalPositionInsideWorkspaceRect.y + // todo we probably should replace lastFloatingSize with proper floating window sizing + // https://github.com/nikitabobko/AeroSpace/issues/1519 + let windowWidth = lastFloatingSize?.width ?? 0 + let windowHeight = lastFloatingSize?.height ?? 0 + newX = newX.coerceIn(workspaceRect.minX ... max(workspaceRect.minX, workspaceRect.maxX - windowWidth)) + newY = newY.coerceIn(workspaceRect.minY ... max(workspaceRect.minY, workspaceRect.maxY - windowHeight)) + + setAxFrame(CGPoint(x: newX, y: newY), nil) case .macosNativeFullscreenWindow, .macosNativeHiddenAppWindow, .macosNativeMinimizedWindow, .macosPopupWindow, .tiling, .rootTilingContainer, .shimContainerRelation: break }