Skip to content

Commit

Permalink
Fix Overlay position bug #97
Browse files Browse the repository at this point in the history
  • Loading branch information
SaifAqqad committed Mar 1, 2024
1 parent c5f3c3e commit 0820fea
Showing 1 changed file with 39 additions and 58 deletions.
97 changes: 39 additions & 58 deletions src/UI/Overlay.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
; gui actual size = iconSize + 2*padding
this.options.size += 2 * Overlay.PADDING_SIZE

;set overlay position
; Calculate initial position and export it
this._calculatePos()
config_obj.exportConfig()

this.onDragFunc := ObjBindMethod(this, "_onDrag")
this.onPosChangeFunc := ObjBindMethod(this, "_onPosChange")
Expand Down Expand Up @@ -53,67 +54,45 @@

_calculatePos(){
this.changedPos:= ""
isPositionSet:= 0

if (this.options.pos.Length() > 1) {
; there's multiple positions -> Apply the first available
isPositionSet:= 0

for i, positionConfig in this.options.pos {
if (!this._setPosConfig(positionConfig))
Continue
for i, positionConfig in this.options.pos {
if (!this._setPosConfig(positionConfig))
Continue

this.options.pos[i] := this.relativePosition
isPositionSet := 1
Break
}
this.options.pos[i] := this.relativePosition
Goto, _positionSet
Break
}

; no position was set -> use the first position on primary display
if (!isPositionSet) {
display := DisplayDevices.getPrimary()
; No position was set -> use the first position (or default) on primary display
positionConfig := this.options.pos[1]
primaryDisplay := DisplayDevices.getPrimary()

this.absolutePosition := display.getAbsolutePosition(this.options.pos[1].X, this.options.pos[1].Y)
; First time using the overlay, set the default position
if (positionConfig.X = -1 || positionConfig.Y = -1){
this._setDefaultPos(primaryDisplay, this.options.pos.Length() > 1)
Goto, _positionSet
}

; position is not set -> use default position
if (!this.absolutePosition)
this._setDefaultPos(display)
else
this.relativePosition := this.options.pos[1] := display.getRelativePosition(this.absolutePosition.X, this.absolutePosition.Y)
}
} else {
positionConfig := this.options.pos[1]

if (positionConfig.X = -1 && positionConfig.Y = -1) {
; position is not set -> use default position on primary display
this._setDefaultPos()
} else {
; Try to find the display using it's id or window position
if (positionConfig.DisplayId)
display := DisplayDevices.getById(positionConfig.DisplayId)

if (!display)
display := DisplayDevices.getByPosition(positionConfig.X, positionConfig.Y)

; display wasn't found -> fallback to primary display
if (!display)
display := DisplayDevices.getPrimary()

this.absolutePosition := display.getAbsolutePosition(positionConfig.X, positionConfig.Y)

; position is not set -> use default position
if (!this.absolutePosition)
this._setDefaultPos(display)
else
this.relativePosition := this.options.pos[1] := display.getRelativePosition(this.absolutePosition.X, this.absolutePosition.Y)
}
; Try to clone the first position onto the primary display
if (this._setPosConfig(positionConfig, primaryDisplay)){
this.options.pos.InsertAt(1, this.relativePosition)
Goto, _positionSet
}

; Fallback to the default position
this._setDefaultPos(primaryDisplay, true)

_positionSet:
util_log("[Overlay] Calculated overlay position: [" this.absolutePosition.x ", " this.absolutePosition.y "] on display '" this.absolutePosition.DisplayId "'")
if (this.hwnd && this.shown && this.absolutePosition.x && this.absolutePosition.y)
WinMove, % "ahk_id " this.hwnd, , % this.absolutePosition.x, % this.absolutePosition.y
}

_setPosConfig(positionConfig){
_setPosConfig(positionConfig, display := ""){
; Try to find the display using it's id or window position
if (positionConfig.DisplayId)
if (!display && positionConfig.DisplayId)
display := DisplayDevices.getById(positionConfig.DisplayId)

if (!display)
Expand All @@ -130,14 +109,16 @@
return true
}

_setDefaultPos(display := "") {
if (!display)
display := DisplayDevices.getPrimary()
_setDefaultPos(display, appendDisplay := false) {
this.relativePosition := Overlay.DEFAULT_POSITION.Clone()
this.relativePosition.DisplayId := display.id

this.relativePosition := this.options.pos[1] := Overlay.DEFAULT_POSITION.Clone()
this.options.pos[1].DisplayId := display.id
if (appendDisplay)
this.options.pos.InsertAt(1, this.relativePosition)
else
this.options.pos[1] := this.relativePosition

this.absolutePosition := display.getAbsolutePosition(this.options.pos[1].X, this.options.pos[1].Y)
this.absolutePosition := display.getAbsolutePosition(this.relativePosition.X, this.relativePosition.Y)
}

_createWindow() {
Expand Down Expand Up @@ -248,11 +229,11 @@
this.absolutePosition.X := xPos := this.changedPos.x
this.absolutePosition.Y := yPos := this.changedPos.y

util_log("[Overlay] Overlay position changed to: " xPos ", " yPos)

this.changedPos := ""
newPosDisplay := DisplayDevices.getByPosition(xPos, yPos)

util_log("[Overlay] Overlay position changed to [" xPos ", " yPos "] on display '" newPosDisplay.Id "'")

; Check if we're on the same display
if (newPosDisplay.Id == this.absolutePosition.DisplayId){
relativePos := newPosDisplay.getRelativePosition(xPos, yPos)
Expand Down

0 comments on commit 0820fea

Please sign in to comment.