diff --git a/main.go b/main.go index 9278729..5d3f9ca 100644 --- a/main.go +++ b/main.go @@ -52,50 +52,53 @@ func RestoreWindowPositions(client *hypr.Client) error { } for _, window := range windows { + // ignore all special workspaces because they can't be moved between monitors if window.Workspace.ID < 0 { continue } - //monitor := monitorByID[window.MonitorID] - windowBounds := image.Rect( window.At[0], window.At[1], window.At[0]+window.Size[0], window.At[1]+window.Size[1]) - shouldMonitorBounds := monitorBoundsByID[window.MonitorID] - - if windowBounds.In(shouldMonitorBounds) { + monitorBounds := monitorBoundsByID[window.MonitorID] + if windowBounds.In(monitorBounds) { continue } - // fmt.Println("IMPOSSIBLE WINDOW POSITION DETECTED", window.Title, window.Address) - // fmt.Println("workspace:", window.Workspace.ID) - // fmt.Println("monitor:", shouldMonitorBounds) - // fmt.Println("window:", windowBounds) - - isMonitorBounds := image.Rect(-1, -1, -1, -1) + // retrieve the bounds of the monitor in which the window is actually drawn. + // This can be the default value of Rect(-1, -1, -1, -1) + // if the windows is out of bounds for any monitor + boundsOfActualMonitor := image.Rect(-1, -1, -1, -1) for _, monitorBounds := range monitorBoundsByID { if windowBounds.In(monitorBounds) { - isMonitorBounds = monitorBounds + boundsOfActualMonitor = monitorBounds break } } - diff := windowBounds.Min.Sub(isMonitorBounds.Min) - shouldPos := shouldMonitorBounds.Min.Add(diff) + // calculate new position based on the position of the window and its actual monitor + diff := windowBounds.Min.Sub(boundsOfActualMonitor.Min) + shouldPos := monitorBounds.Min.Add(diff) - if isMonitorBounds == image.Rect(-1, -1, -1, -1) { - shouldPos = shouldMonitorBounds.Min + // use coords [0,0] respective to the wanted monitor + // if window is completely OOB + if boundsOfActualMonitor == image.Rect(-1, -1, -1, -1) { + shouldPos = monitorBounds.Min } - client.DispatchExpectOK(fmt.Sprintf( + cmd := fmt.Sprintf( "movewindowpixel exact %d %d,address:%s", shouldPos.X, shouldPos.Y, - window.Address, - )) + window.Address) + + if err := client.DispatchExpectOK(cmd); err != nil { + log.Printf("invalid position could NOT be restored successfully for window (class: '%s' | title: '%s')\n", window.Class, window.Title) + continue + } log.Printf("invalid position restored for window (class: '%s' | title: '%s')\n", window.Class, window.Title) }