This commit is contained in:
Milarin 2024-04-05 13:49:20 +02:00
parent 6a2ec1a957
commit 18dbd68f04

41
main.go
View File

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