Skip to content

Commit ede827b

Browse files
authored
Merge pull request #131 from Stateford/fix-toggle-style-bug
Enhance window management: add functionality to restore original bord…
2 parents 4b8bd07 + f60830d commit ede827b

2 files changed

Lines changed: 26 additions & 25 deletions

File tree

.claude/settings.local.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
"Bash(\"build/tests/Release/display-lock-unittests.exe\")",
88
"Bash(\"display-lock-unittests.exe\")",
99
"Bash(\"./display-lock-unittests.exe\")",
10-
<<<<<<< Updated upstream
11-
"Bash(xxd:*)"
12-
=======
1310
"Bash(xxd:*)",
1411
"Bash(\"C:\\\\Users\\\\japos\\\\git\\\\Display-Lock\\\\build\\\\tests\\\\Debug\\\\display-lock-unittests.exe\")",
1512
"Bash(iconv:*)",
@@ -27,7 +24,6 @@
2724
"Bash(python:*)",
2825
"Bash(ls:*)",
2926
"Bash(./display-lock-unittests.exe:*)"
30-
>>>>>>> Stashed changes
3127
]
3228
}
3329
}

src/components/win.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct PREVIOUSRECT
2525
int x;
2626
int y;
2727
LONG_PTR previousStyle;
28+
LONG_PTR previousExStyle;
2829
LONG_PTR currentStyle;
2930
};
3031

@@ -103,13 +104,23 @@ inline BOOL checkResizeStyle(HWND activeWindow)
103104
return (GetWindowLongPtr(activeWindow, GWL_STYLE) & WS_SIZEBOX);
104105
}
105106

106-
// toggles borderlessWindow
107-
inline void toggleBorderlessWindow(HWND activeWindow)
107+
// removes border styles to make window borderless
108+
inline void removeBorderStyles(HWND activeWindow)
108109
{
109-
// XOR WS_OVERLAPPED, WS_THICKFRAME, WS_SYSMENU, WS_CAPTION
110-
SetWindowLongPtr(activeWindow, GWL_STYLE, GetWindowLongPtr(activeWindow, GWL_STYLE) ^ WS_OVERLAPPED ^ WS_THICKFRAME ^ WS_SYSMENU ^ WS_CAPTION);
111-
// XOR WS_EX_WINDOWEDGE
112-
SetWindowLongPtr(activeWindow, GWL_EXSTYLE, GetWindowLongPtr(activeWindow, GWL_EXSTYLE) ^ WS_EX_WINDOWEDGE);
110+
const LONG_PTR mask = WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU | WS_CAPTION;
111+
const LONG_PTR exMask = WS_EX_WINDOWEDGE;
112+
113+
SetWindowLongPtr(activeWindow, GWL_STYLE, GetWindowLongPtr(activeWindow, GWL_STYLE) & ~mask);
114+
SetWindowLongPtr(activeWindow, GWL_EXSTYLE, GetWindowLongPtr(activeWindow, GWL_EXSTYLE) & ~exMask);
115+
SetWindowPos(activeWindow, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
116+
}
117+
118+
// restores original border styles
119+
inline void restoreBorderStyles(HWND activeWindow, LONG_PTR originalStyle, LONG_PTR originalExStyle)
120+
{
121+
SetWindowLongPtr(activeWindow, GWL_STYLE, originalStyle);
122+
SetWindowLongPtr(activeWindow, GWL_EXSTYLE, originalExStyle);
123+
SetWindowPos(activeWindow, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
113124
}
114125

115126
// resizes borderless window
@@ -125,7 +136,7 @@ void resizeBorderless(WINDOW activeWindow, PREVIOUSRECT *prev)
125136
prev->x = activeWindow.size.left;
126137
prev->y = activeWindow.size.top;
127138

128-
SetWindowPos(activeWindow.hWnd, NULL, prev->x, prev->y, prev->width, prev->height, 0);
139+
SetWindowPos(activeWindow.hWnd, NULL, prev->x, prev->y, prev->width, prev->height, SWP_FRAMECHANGED);
129140
}
130141

131142
// enable fullscreen
@@ -140,13 +151,13 @@ void enableFullScreen(WINDOW activeWindow, PREVIOUSRECT *prev)
140151
prev->x = activeWindow.size.left;
141152
prev->y = activeWindow.size.top;
142153

143-
SetWindowPos(activeWindow.hWnd, NULL, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), 0);
154+
SetWindowPos(activeWindow.hWnd, NULL, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_FRAMECHANGED);
144155
}
145156

146157
// disable full screen
147158
inline void disableFullScreen(WINDOW activeWindow, PREVIOUSRECT *prev)
148159
{
149-
SetWindowPos(activeWindow.hWnd, NULL, prev->x, prev->y, prev->width, prev->height, 0);
160+
SetWindowPos(activeWindow.hWnd, NULL, prev->x, prev->y, prev->width, prev->height, SWP_FRAMECHANGED);
150161
}
151162

152163
// check if process is still running
@@ -168,10 +179,11 @@ int CALLBACK cursorLock(void *arguments)
168179
PREVIOUSRECT previousRect;
169180

170181
previousRect.previousStyle = GetWindowLongPtr(selectedWindow.hWnd, GWL_STYLE);
182+
previousRect.previousExStyle = GetWindowLongPtr(selectedWindow.hWnd, GWL_EXSTYLE);
171183

172184
if (settings->borderless)
173185
{
174-
toggleBorderlessWindow(selectedWindow.hWnd);
186+
removeBorderStyles(selectedWindow.hWnd);
175187
if (!settings->fullScreen)
176188
resizeBorderless(selectedWindow, &previousRect);
177189
}
@@ -218,10 +230,11 @@ int CALLBACK cursorLock(void *arguments)
218230
break;
219231
}
220232

221-
// check fi the window style has changed
233+
// check if the window style has changed
222234
if (previousRect.currentStyle != GetWindowLongPtr(selectedWindow.hWnd, GWL_STYLE))
223235
{
224236
SetWindowLongPtr(selectedWindow.hWnd, GWL_STYLE, previousRect.currentStyle);
237+
SetWindowPos(selectedWindow.hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
225238

226239
if (settings->borderless && !settings->fullScreen)
227240
resizeBorderless(selectedWindow, &previousRect);
@@ -243,16 +256,8 @@ int CALLBACK cursorLock(void *arguments)
243256
Sleep(1);
244257
}
245258

246-
// if window style was changed, change it back
247-
if (styleChanged)
248-
SetWindowLongPtr(selectedWindow.hWnd, GWL_STYLE, previousRect.previousStyle);
249-
250-
if (settings->borderless)
251-
{
252-
toggleBorderlessWindow(selectedWindow.hWnd);
253-
if (!settings->fullScreen)
254-
resizeBorderless(selectedWindow, &previousRect);
255-
}
259+
// Restore original styles (handles borderless, styleChanged, etc. all at once)
260+
restoreBorderStyles(selectedWindow.hWnd, previousRect.previousStyle, previousRect.previousExStyle);
256261

257262
if (settings->fullScreen)
258263
disableFullScreen(selectedWindow, &previousRect);

0 commit comments

Comments
 (0)