Skip to content

Add window size and position persistence#22

Merged
forloopcodes merged 5 commits intomainfrom
copilot/remember-window-size-position
Mar 4, 2026
Merged

Add window size and position persistence#22
forloopcodes merged 5 commits intomainfrom
copilot/remember-window-size-position

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 5, 2026

Implements window geometry persistence to registry, matching native Notepad behavior.

Implementation

  • State: Added windowX, windowY, windowWidth, windowHeight to AppState with 640×480 default
  • Storage: New LoadWindowSettings() / SaveWindowSettings() functions write to HKEY_CURRENT_USER\Software\LegacyNotepad
  • Lifecycle: Load on startup before CreateWindowExW(), save on WM_DESTROY using GetWindowPlacement().rcNormalPosition

Edge Cases

  • Multi-monitor: Negative coordinates preserved via static_cast<int>(DWORD) round-trip
  • Off-screen: MonitorFromRect() validation resets to CW_USEDEFAULT if no monitor intersection
  • Maximized state: Uses rcNormalPosition so window doesn't reopen maximized unintentionally

Registry keys: WindowX, WindowY, WindowWidth, WindowHeight (REG_DWORD)

Original prompt

This section details on the original issue you should resolve

<issue_title>Remember window size and maybe position</issue_title>
<issue_description>Hello again!

Not really a bug or an issue but the og notepad remembers the window size and position of the last session. Since you already put font and always on top settings in the registry I guess this can be put there too?

Have a nice day and thanks!</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits February 5, 2026 13:58
Co-authored-by: ForLoopCodes <89027512+ForLoopCodes@users.noreply.github.com>
…narios

Co-authored-by: ForLoopCodes <89027512+ForLoopCodes@users.noreply.github.com>
…et_cast

Co-authored-by: ForLoopCodes <89027512+ForLoopCodes@users.noreply.github.com>
Co-authored-by: ForLoopCodes <89027512+ForLoopCodes@users.noreply.github.com>
Copilot AI changed the title [WIP] Add feature to remember window size and position Add window size and position persistence Feb 5, 2026
@forloopcodes forloopcodes self-requested a review February 5, 2026 15:04
@forloopcodes forloopcodes marked this pull request as ready for review March 4, 2026 11:18
Copilot AI review requested due to automatic review settings March 4, 2026 11:18
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds persistence of the main window’s last normal position and size to the existing registry-backed settings system, so LegacyNotepad restores geometry across launches (similar to native Notepad).

Changes:

  • Added window geometry fields (windowX, windowY, windowWidth, windowHeight) to AppState with defaults.
  • Implemented LoadWindowSettings() / SaveWindowSettings() to read/write geometry under HKCU\Software\LegacyNotepad.
  • Wired geometry load before CreateWindowExW() and geometry save on WM_DESTROY using GetWindowPlacement().rcNormalPosition.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/modules/settings.h Exposes window settings load/save APIs.
src/modules/settings.cpp Implements registry persistence for window X/Y/Width/Height and basic off-screen validation.
src/main.cpp Loads geometry on startup; captures/saves geometry on shutdown; uses persisted geometry when creating the main window.
src/core/types.h Extends AppState with persisted window geometry fields and defaults.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread src/modules/settings.cpp
Comment on lines +148 to +166
DWORD width = 0;
size = sizeof(width);
if (RegQueryValueExW(hKey, WINDOW_WIDTH_VALUE, nullptr, nullptr, reinterpret_cast<LPBYTE>(&width), &size) == ERROR_SUCCESS)
{
if (width > 0)
{
g_state.windowWidth = static_cast<int>(width);
}
}

DWORD height = 0;
size = sizeof(height);
if (RegQueryValueExW(hKey, WINDOW_HEIGHT_VALUE, nullptr, nullptr, reinterpret_cast<LPBYTE>(&height), &size) == ERROR_SUCCESS)
{
if (height > 0)
{
g_state.windowHeight = static_cast<int>(height);
}
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoadWindowSettings() assigns registry REG_DWORD width/height directly to int after only checking > 0. If the stored DWORD exceeds INT_MAX, the cast can wrap to a negative int, which can make CreateWindowExW fail or create invalid geometry. Please bound-check the DWORDs (e.g., <= INT_MAX and within a reasonable min/max) before casting/assigning, and consider resetting to defaults when out of range.

Copilot uses AI. Check for mistakes.
Comment thread src/modules/settings.cpp
Comment on lines +171 to +173
// Validate that the window position is visible on at least one monitor
RECT rc = {g_state.windowX, g_state.windowY, g_state.windowX + g_state.windowWidth, g_state.windowY + g_state.windowHeight};
HMONITOR hMonitor = MonitorFromRect(&rc, MONITOR_DEFAULTTONULL);
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RECT construction uses g_state.windowX + g_state.windowWidth and g_state.windowY + g_state.windowHeight in signed arithmetic. If registry values are large/corrupted, these additions can overflow int, which is undefined behavior in C++. Consider using a wider intermediate type and clamping to LONG range (or validating inputs) before building the RECT for MonitorFromRect().

Copilot uses AI. Check for mistakes.
@forloopcodes forloopcodes merged commit c9b6c78 into main Mar 4, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remember window size and maybe position

3 participants