Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

fixes initial placement, move and size issues #498

Merged
merged 3 commits into from
Jan 13, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions appshell/cef_host_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,20 @@ BOOL cef_host_window::HandleSize(BOOL bMinimize)
}
SetProp(L"WasMinimized", (HANDLE)bMinimize);
#endif
NotifyWindowMovedOrResized();
return FALSE;
}

void cef_host_window::NotifyWindowMovedOrResized()
{
if (GetBrowser() && GetBrowser()->GetHost()) {
GetBrowser()->GetHost()->NotifyMoveOrResizeStarted();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

calling this was something that needed to be added whenever the app is moved or resized

}
}

BOOL cef_host_window::HandleMoveOrMoving()
{
NotifyWindowMovedOrResized();
return FALSE;
}

Expand Down Expand Up @@ -198,6 +211,11 @@ LRESULT cef_host_window::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
if (HandleInitMenuPopup((HMENU)wParam))
return 0L;
break;
case WM_MOVING:
case WM_MOVE:
if (HandleMoveOrMoving())
return 0L;
break;
}


Expand Down
2 changes: 2 additions & 0 deletions appshell/cef_host_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ class cef_host_window : public cef_host_window_base
// Message Handlers
BOOL HandleInitMenuPopup(HMENU hMenuPopup);
BOOL HandleSize(BOOL bMinimize);
BOOL HandleMoveOrMoving();

// Command Implementation
BOOL DoCommand(UINT commandId, CefRefPtr<CommandCallback> callback = 0);
BOOL DoCommand(const CefString& commandString, CefRefPtr<CommandCallback> callback = 0);

// Implementation
virtual void DoRepaintClientArea();
void NotifyWindowMovedOrResized();

// Helper to get a command string from command id
CefString GetCommandString(UINT commandId);
Expand Down
82 changes: 82 additions & 0 deletions appshell/cef_main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,60 @@ LPCWSTR cef_main_window::GetBracketsWindowTitleText()
return szTitle;
}

void cef_main_window::EnsureWindowRectVisibility(int& left, int& top, int& width, int& height, int showCmd)
{
static const int kWindowFrameSize = 8;

// don't check if we're already letting
// Windows determine the window placement
if (left == CW_USEDEFAULT &&
top == CW_USEDEFAULT &&
width == CW_USEDEFAULT &&
height == CW_USEDEFAULT) {
return;
}

// The virtual display is the bounding rect of all monitors
// see http://msdn.microsoft.com/en-us/library/dd162729(v=vs.85).aspx

int xScreen = ::GetSystemMetrics(SM_XVIRTUALSCREEN);
int yScreen = ::GetSystemMetrics(SM_YVIRTUALSCREEN);
int cxScreen = ::GetSystemMetrics(SM_CXVIRTUALSCREEN);
int cyScreen = ::GetSystemMetrics(SM_CYVIRTUALSCREEN);

// make a copy, we need to adjust the comparison
// if it's a maximized window because the OS move the
// origin of the window by 8 pixes to releive the borders
// from the monitor for legacy apps
int xLeft = left;
int xTop = top;
int xWidth = width;
int xHeight = height;

if (showCmd == SW_MAXIMIZE) {
xLeft += kWindowFrameSize;
xTop += kWindowFrameSize;
xWidth -= kWindowFrameSize * 2;
xHeight -= kWindowFrameSize * 2;
}

// Make sure the window fits inside the virtual screen.
// If it doesn't then we let windows decide the window placement
if (xLeft < xScreen ||
xTop < yScreen ||
xLeft + xWidth > xScreen + cxScreen ||
xTop + xHeight > yScreen + cyScreen) {

// something was off-screen so reposition
// to the default window placement
left = CW_USEDEFAULT;
top = CW_USEDEFAULT;
width = CW_USEDEFAULT;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can original width and height be maintained? i.e. only set left and top to default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, this has to work for both the maximized size / placement and the restored size / placement otherwise you run into that odd case so, IMO, it's better to just let the OS decide initial placement and then let the user move it wherever they want.

This eliminates incorrectly computing the placement or placing the window on a secondary monitor (in the case they have more than 2 monitors and disconnect one or rearrange monitors.)

height = CW_USEDEFAULT;
}
}


// Create Method. Call this to create a cef_main_window instance
BOOL cef_main_window::Create()
{
Expand All @@ -108,10 +162,14 @@ BOOL cef_main_window::Create()
int top = CW_USEDEFAULT;
int width = CW_USEDEFAULT;
int height = CW_USEDEFAULT;

int showCmd = SW_SHOW;

LoadWindowRestoreRect(left, top, width, height, showCmd);

// make sure the window is visible
EnsureWindowRectVisibility(left, top, width, height, showCmd);

DWORD styles = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_EX_COMPOSITED;

if (showCmd == SW_MAXIMIZE)
Expand Down Expand Up @@ -353,6 +411,30 @@ void cef_main_window::RestoreWindowPlacement(int showCmd)
GetRegistryInt(::kWindowPostionFolder, ::kPrefRestoreRight, NULL, (int&)wp.rcNormalPosition.right);
GetRegistryInt(::kWindowPostionFolder, ::kPrefRestoreBottom, NULL, (int&)wp.rcNormalPosition.bottom);

::NormalizeRect(wp.rcNormalPosition);

int left = wp.rcNormalPosition.left;
int top = wp.rcNormalPosition.top;
int width = wp.rcNormalPosition.right - left;
int height = wp.rcNormalPosition.bottom - top;

EnsureWindowRectVisibility(left, top, width, height, SW_SHOWNORMAL);

wp.rcNormalPosition.left = left;
wp.rcNormalPosition.top = top;

if (width != CW_USEDEFAULT) {
wp.rcNormalPosition.right = wp.rcNormalPosition.left + width;
} else {
wp.rcNormalPosition.right = CW_USEDEFAULT;
}

if (height != CW_USEDEFAULT) {
wp.rcNormalPosition.bottom = wp.rcNormalPosition.top + height;
} else {
wp.rcNormalPosition.bottom = CW_USEDEFAULT;
}

// This returns FALSE on failure but not sure what we could do in that case
SetWindowPlacement(&wp);
}
Expand Down
1 change: 1 addition & 0 deletions appshell/cef_main_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class cef_main_window : public cef_host_window
void SaveWindowRestoreRect();
void LoadWindowRestoreRect(int& left, int& top, int& width, int& height, int& showCmd);
void RestoreWindowPlacement(int showCmd);
void EnsureWindowRectVisibility(int& left, int& top, int& width, int& height, int showCmd);

// Message Handlers
BOOL HandleEraseBackground(HDC hdc);
Expand Down