Skip to content

Commit e601295

Browse files
authored
Update WindowManager.ahk
1 parent 261b263 commit e601295

File tree

1 file changed

+38
-110
lines changed

1 file changed

+38
-110
lines changed

Lib/v2/WindowManager.ahk

Lines changed: 38 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,35 @@
11
#Requires AutoHotkey v2.0
22

3-
; ============================================================================
4-
; WindowManager.ahk - Window manipulation and fullscreen utilities
5-
; Version: 2.0.0 (Migrated to AHK v2)
6-
; ============================================================================
3+
; WindowManager – borderless/fullscreen utilities
74

8-
; ============================================================================
9-
; ToggleFakeFullscreen(winTitle := "A") - Toggle borderless fullscreen
10-
; Removes/restores window decorations and maximizes to simulate fullscreen
11-
;
12-
; Parameters:
13-
; winTitle - Window to affect (default: active window)
14-
; ============================================================================
15-
ToggleFakeFullscreen(winTitle := "A") {
16-
; Check if window is currently in fake fullscreen by checking for titlebar
17-
currentStyle := WinGetStyle(winTitle)
18-
hasTitleBar := currentStyle & 0xC00000
19-
20-
if (hasTitleBar) {
21-
; Enter fake fullscreen - remove window decorations (combined: 0xC00000 | 0x800000 | 0x400000 | 0x40000 = 0xEC0000)
22-
WinSetStyle(-0xEC0000, winTitle) ; Remove all decorations in single operation
5+
static STYLE_DECORATIONS := 0xEC0000 ; title/menu/frame/sysmodal combined
6+
static STYLE_UNDECORATED := -0xEC0000
237

24-
; Maximize the window
8+
ToggleFakeFullscreen(winTitle := "A") {
9+
current := WinGetStyle(winTitle)
10+
hasTitle := current & 0xC00000
11+
if hasTitle {
12+
WinSetStyle(STYLE_UNDECORATED, winTitle)
2513
WinMove(0, 0, A_ScreenWidth, A_ScreenHeight, winTitle)
2614
} else {
27-
; Exit fake fullscreen - restore window decorations (combined)
28-
WinSetStyle(0xEC0000, winTitle) ; Restore all decorations in single operation
29-
30-
; Restore window to non-maximized state
15+
WinSetStyle(STYLE_DECORATIONS, winTitle)
3116
WinRestore(winTitle)
3217
}
3318
}
3419

35-
; ============================================================================
36-
; SetWindowBorderless(winTitle) - Remove window borders and decorations
37-
; Parameters:
38-
; winTitle - Window to affect
39-
; ============================================================================
4020
SetWindowBorderless(winTitle) {
41-
; Remove all decorations in single operation (0xC00000 | 0x800000 | 0x400000 | 0x40000 = 0xEC0000)
42-
WinSetStyle(-0xEC0000, winTitle)
21+
WinSetStyle(STYLE_UNDECORATED, winTitle)
4322
}
4423

45-
; ============================================================================
46-
; MaximizeWindow(winTitle) - Maximize window to full screen size
47-
; Parameters:
48-
; winTitle - Window to affect
49-
; ============================================================================
5024
MaximizeWindow(winTitle) {
5125
WinMove(0, 0, A_ScreenWidth, A_ScreenHeight, winTitle)
5226
}
5327

54-
; ============================================================================
55-
; MakeFullscreen(winTitle) - Combine borderless + maximize
56-
; Parameters:
57-
; winTitle - Window to affect
58-
; ============================================================================
5928
MakeFullscreen(winTitle) {
6029
SetWindowBorderless(winTitle)
6130
MaximizeWindow(winTitle)
6231
}
6332

64-
; ============================================================================
65-
; WaitForWindow(winTitle, timeout := 30) - Wait for window with timeout
66-
; Returns: true if window found, false if timeout
67-
;
68-
; Parameters:
69-
; winTitle - Window to wait for
70-
; timeout - Timeout in seconds (default: 30 seconds)
71-
; ============================================================================
7233
WaitForWindow(winTitle, timeout := 30) {
7334
try {
7435
WinWait(winTitle, , timeout)
@@ -78,89 +39,56 @@ WaitForWindow(winTitle, timeout := 30) {
7839
}
7940
}
8041

81-
; ============================================================================
82-
; WaitForProcess(processName, timeout := 30) - Wait for process with timeout
83-
; Returns: true if process found, false if timeout
84-
;
85-
; Parameters:
86-
; processName - Process to wait for (e.g., "notepad.exe")
87-
; timeout - Timeout in seconds (default: 30 seconds)
88-
; ============================================================================
8942
WaitForProcess(processName, timeout := 30) {
90-
startTime := A_TickCount
91-
timeoutMs := timeout * 1000
92-
93-
while (!ProcessExist(processName)) {
94-
if (A_TickCount - startTime > timeoutMs)
43+
start := A_TickCount
44+
limit := timeout * 1000
45+
while !ProcessExist(processName) {
46+
if (A_TickCount - start > limit)
9547
return false
96-
9748
Sleep(100)
9849
}
99-
10050
return true
10151
}
10252

103-
; ============================================================================
104-
; Multi-monitor aware fullscreen functions
105-
; ============================================================================
106-
10753
GetMonitorAtPos(x, y) {
108-
; Monitor number at position x,y or -1 if x,y outside monitors
109-
monitorCount := MonitorGetCount()
110-
111-
Loop monitorCount {
112-
MonitorGet(A_Index, &areaLeft, &areaTop, &areaRight, &areaBottom)
113-
if (areaLeft <= x && x <= areaRight && areaTop <= y && y <= areaBottom)
54+
count := MonitorGetCount()
55+
Loop count {
56+
MonitorGet(A_Index, &l, &t, &r, &b)
57+
if (l <= x && x <= r && t <= y && y <= b)
11458
return A_Index
11559
}
11660
return -1
11761
}
11862

11963
GetMonitorActiveWindow(winTitle := "A") {
120-
; Get Monitor number at the center position of the specified window
121-
WinGetPos(&x, &y, &width, &height, winTitle)
122-
return GetMonitorAtPos(x + width/2, y + height/2)
64+
WinGetPos(&x, &y, &w, &h, winTitle)
65+
return GetMonitorAtPos(x + w/2, y + h/2)
12366
}
12467

12568
ToggleFakeFullscreenMultiMonitor(winTitle := "A") {
126-
; Toggle borderless fullscreen with multi-monitor support
127-
static WINDOW_STYLE_UNDECORATED := -0xC40000
128-
static savedInfo := Map()
129-
69+
static saved := Map()
13070
id := WinGetID(winTitle)
13171

132-
if (savedInfo.Has(id)) {
133-
; Restore saved window state
134-
inf := savedInfo[id]
135-
WinSetStyle(inf["style"], "ahk_id " . id)
136-
WinMove(inf["x"], inf["y"], inf["width"], inf["height"], "ahk_id " . id)
137-
savedInfo.Delete(id)
138-
} else {
139-
; Save current state and make fullscreen
140-
inf := Map()
141-
inf["style"] := WinGetStyle(winTitle)
142-
WinGetPos(&ltmpX, &ltmpY, &ltmpWidth, &ltmpHeight, "ahk_id " . id)
143-
inf["x"] := ltmpX
144-
inf["y"] := ltmpY
145-
inf["width"] := ltmpWidth
146-
inf["height"] := ltmpHeight
147-
savedInfo[id] := inf
72+
if saved.Has(id) {
73+
inf := saved[id]
74+
WinSetStyle(inf.style, "ahk_id " . id)
75+
WinMove(inf.x, inf.y, inf.w, inf.h, "ahk_id " . id)
76+
saved.Delete(id)
77+
return
78+
}
14879

149-
WinSetStyle(WINDOW_STYLE_UNDECORATED, "ahk_id " . id)
80+
inf := Map()
81+
inf.style := WinGetStyle(winTitle)
82+
WinGetPos(&ix, &iy, &iw, &ih, "ahk_id " . id)
83+
inf.x := ix, inf.y := iy, inf.w := iw, inf.h := ih
84+
saved[id] := inf
15085

151-
; Get the monitor where window is currently located
152-
mon := GetMonitorActiveWindow("ahk_id " . id)
153-
MonitorGet(mon, &monLeft, &monTop, &monRight, &monBottom)
154-
WinMove(monLeft, monTop, monRight - monLeft, monBottom - monTop, winTitle)
155-
}
86+
WinSetStyle(STYLE_UNDECORATED, "ahk_id " . id)
87+
mon := GetMonitorActiveWindow("ahk_id " . id)
88+
MonitorGet(mon, &ml, &mt, &mr, &mb)
89+
WinMove(ml, mt, mr - ml, mb - mt, winTitle)
15690
}
15791

158-
; ============================================================================
159-
; RestoreWindowBorders(winTitle) - Restore standard window decorations
160-
; Parameters:
161-
; winTitle - Window to affect
162-
; ============================================================================
16392
RestoreWindowBorders(winTitle) {
164-
; Restore all decorations in single operation (0xC00000 | 0x800000 | 0x400000 | 0x40000 = 0xEC0000)
165-
WinSetStyle(0xEC0000, winTitle)
93+
WinSetStyle(STYLE_DECORATIONS, winTitle)
16694
}

0 commit comments

Comments
 (0)