-
Notifications
You must be signed in to change notification settings - Fork 601
windows: restore window position #123
Changes from 5 commits
8a95c1a
205b585
1528afb
b1b229f
6a3b182
9eb8a2d
8f2b7af
16a750c
d4bd936
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,22 @@ extern CefRefPtr<ClientHandler> g_handler; | |
| #pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length) | ||
| #endif | ||
|
|
||
| // Registry access functions | ||
| bool GetRegistryInt(LPCWSTR pFolder, LPCWSTR pEntry, int* pDefault, int& ret); | ||
| bool WriteRegistryInt (LPCWSTR pFolder, LPCWSTR pEntry, int val); | ||
|
|
||
| // Registry key strings | ||
| #define PREF_BRACKETS_BASE L"Software\\Brackets\\" | ||
| #define PREF_WINPOS_FOLDER L"Window Position" | ||
| #define PREF_LEFT L"Left" | ||
| #define PREF_TOP L"Top" | ||
| #define PREF_WIDTH L"Width" | ||
| #define PREF_HEIGHT L"Height" | ||
|
|
||
| // Window state functions | ||
| void SaveWindowRect(); | ||
| void RestoreWindowRect(int& left, int& top, int& width, int& height); | ||
|
|
||
| // Program entry point function. | ||
| int APIENTRY wWinMain(HINSTANCE hInstance, | ||
| HINSTANCE hPrevInstance, | ||
|
|
@@ -190,6 +206,89 @@ int APIENTRY wWinMain(HINSTANCE hInstance, | |
| return result; | ||
| } | ||
|
|
||
| // get integer value from registry key | ||
| // caller can either use return value to determine success/fail, or pass a default to be used on fail | ||
| bool GetRegistryInt(LPCWSTR pFolder, LPCWSTR pEntry, int* pDefault, int& ret) | ||
| { | ||
| HKEY hKey; | ||
| bool result = false; | ||
|
|
||
| std::wstring key = PREF_BRACKETS_BASE; | ||
| key += pFolder; | ||
|
|
||
| if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, (LPCWSTR)key.c_str(), 0, KEY_READ, &hKey)) | ||
| { | ||
| DWORD dwValue = 0; | ||
| DWORD dwType = 0; | ||
| DWORD dwCount = sizeof(DWORD); | ||
| key = pEntry; | ||
| if (ERROR_SUCCESS == RegQueryValueEx(hKey, (LPCWSTR)key.c_str(), NULL, &dwType, (LPBYTE)&dwValue, &dwCount)) | ||
| { | ||
| result = true; | ||
| ASSERT(dwType == REG_DWORD); | ||
| ASSERT(dwCount == sizeof(dwValue)); | ||
| ret = (int)dwValue; | ||
| } | ||
| RegCloseKey(hKey); | ||
| } | ||
|
|
||
| if (!result) | ||
| { | ||
| // couldn't read value, so use default, if specified | ||
| if (pDefault) | ||
| ret = *pDefault; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| // write integer value to registry key | ||
| bool WriteRegistryInt(LPCWSTR pFolder, LPCWSTR pEntry, int val) | ||
| { | ||
| HKEY hKey; | ||
| bool result = false; | ||
|
|
||
| std::wstring key = PREF_BRACKETS_BASE; | ||
| key += pFolder; | ||
|
|
||
| if (ERROR_SUCCESS == RegCreateKeyEx(HKEY_CURRENT_USER, (LPCWSTR)key.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL)) | ||
| { | ||
| DWORD dwCount = sizeof(int); | ||
| key = pEntry; | ||
| if (ERROR_SUCCESS == RegSetValueEx(hKey, (LPCWSTR)key.c_str(), 0, REG_DWORD, (LPBYTE)&val, dwCount)) | ||
| result = true; | ||
| RegCloseKey(hKey); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| void SaveWindowRect() | ||
| { | ||
| // Save position of active window | ||
| HWND hWnd = GetActiveWindow(); | ||
| if (hWnd) | ||
| { | ||
| RECT rect; | ||
| if (GetWindowRect(hWnd, &rect)) | ||
| { | ||
| WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_LEFT, rect.left); | ||
| WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_TOP, rect.top); | ||
| WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_WIDTH, rect.right - rect.left); | ||
| WriteRegistryInt(PREF_WINPOS_FOLDER, PREF_HEIGHT, rect.bottom - rect.top); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void RestoreWindowRect(int& left, int& top, int& width, int& height) | ||
| { | ||
| GetRegistryInt(PREF_WINPOS_FOLDER, PREF_LEFT, NULL, left); | ||
| GetRegistryInt(PREF_WINPOS_FOLDER, PREF_TOP, NULL, top); | ||
| GetRegistryInt(PREF_WINPOS_FOLDER, PREF_WIDTH, NULL, width); | ||
| GetRegistryInt(PREF_WINPOS_FOLDER, PREF_HEIGHT, NULL, height); | ||
| } | ||
|
|
||
|
|
||
| // | ||
| // FUNCTION: MyRegisterClass() | ||
| // | ||
|
|
@@ -247,9 +346,17 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { | |
|
|
||
| hInst = hInstance; // Store instance handle in our global variable | ||
|
|
||
| hWnd = CreateWindow(szWindowClass, szTitle, | ||
| WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, 0, | ||
| CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); | ||
| // TODO: test this cases: | ||
| // - window in secondary monitor when shutdown, disconnect secondary monitor, restart | ||
|
|
||
| int left = CW_USEDEFAULT; | ||
| int top = 0; | ||
| int width = CW_USEDEFAULT; | ||
| int height = 0; | ||
| RestoreWindowRect(left, top, width, height); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will restore the window rect, but doesn't restore the maximized flag or the "normal" size. If you maximize the window, quit, then restart, the window size is set to the maximized size, but it isn't maximized (and, at least on my Win 7 machine, the top of the titlebar is a bit off the top of the screen). You may need to use GetWindowPlacement/SetWindowPlacement and save/restore the whole WINDOWPLACEMENT structure.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. This is how window states should be restored: restored - Size & position should be remembered on any monitor. maximized - Should be remembered for any monitor. Also, switching back to "restored" size should remember previous size & position. minimized - It seems like we didn't want to open Brackets into a minimized state, so it should use state from previous startup. |
||
|
|
||
| hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, | ||
| left, top, width, height, NULL, NULL, hInstance, NULL); | ||
|
|
||
| if (!hWnd) | ||
| return FALSE; | ||
|
|
@@ -509,6 +616,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, | |
|
|
||
| case WM_CLOSE: | ||
| if (g_handler.get()) { | ||
|
|
||
| SaveWindowRect(); | ||
|
|
||
| // If we already initiated the browser closing, then let default window proc handle it. | ||
| HWND browserHwnd = g_handler->GetBrowser()->GetHost()->GetWindowHandle(); | ||
| HANDLE closing = GetProp(browserHwnd, CLOSING_PROP); | ||
|
|
@@ -566,4 +676,4 @@ CefString AppGetCachePath() { | |
| cachePath += L"\\" GROUP_NAME APP_NAME L"\\cef_data"; | ||
|
|
||
| return CefString(cachePath); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use the GROUP_NAME and APP_NAME constants defined in config.h instead of hard-coding "Brackets"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and PREF_BRACKETS_BASE should be given a more generic name like PREF_APPSHELL_BASE, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.