Fix window state events on Windows

This commit fixes the issue we had with window state events not firing
when triggered through Aero Snap.
Instead of listening to command from the system menu (SC_MAXIMIZE etc.),
we use the WM_SIZE event.

This resolves #1381.
This commit is contained in:
Eran Tiktin 2015-09-29 02:20:09 +03:00
parent 7f1cb9f90a
commit ee0f0f6cfc
2 changed files with 73 additions and 34 deletions

View file

@ -169,9 +169,6 @@ NativeWindowViews::NativeWindowViews(
menu_bar_autohide_(false), menu_bar_autohide_(false),
menu_bar_visible_(false), menu_bar_visible_(false),
menu_bar_alt_pressed_(false), menu_bar_alt_pressed_(false),
#if defined(OS_WIN)
is_minimized_(false),
#endif
keyboard_event_handler_(new views::UnhandledKeyboardEventHandler), keyboard_event_handler_(new views::UnhandledKeyboardEventHandler),
use_content_size_(false), use_content_size_(false),
resizable_(true) { resizable_(true) {
@ -228,6 +225,9 @@ NativeWindowViews::NativeWindowViews(
window_->Init(params); window_->Init(params);
bool fullscreen = false;
options.Get(switches::kFullscreen, &fullscreen);
#if defined(USE_X11) #if defined(USE_X11)
// Start monitoring window states. // Start monitoring window states.
window_state_watcher_.reset(new WindowStateWatcher(this)); window_state_watcher_.reset(new WindowStateWatcher(this));
@ -253,8 +253,7 @@ NativeWindowViews::NativeWindowViews(
} }
// Before the window is mapped, there is no SHOW_FULLSCREEN_STATE. // Before the window is mapped, there is no SHOW_FULLSCREEN_STATE.
bool fullscreen = false; if (fullscreen) {
if (options.Get(switches::kFullscreen, & fullscreen) && fullscreen) {
state_atom_list.push_back(GetAtom("_NET_WM_STATE_FULLSCREEN")); state_atom_list.push_back(GetAtom("_NET_WM_STATE_FULLSCREEN"));
} }
@ -278,6 +277,12 @@ NativeWindowViews::NativeWindowViews(
bounds = ContentBoundsToWindowBounds(bounds); bounds = ContentBoundsToWindowBounds(bounds);
#if defined(OS_WIN) #if defined(OS_WIN)
// Save initial window state.
if (fullscreen)
last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
else
last_window_state_ = ui::SHOW_STATE_NORMAL;
if (!has_frame()) { if (!has_frame()) {
// Set Window style so that we get a minimize and maximize animation when // Set Window style so that we get a minimize and maximize animation when
// frameless. // frameless.
@ -391,11 +396,16 @@ bool NativeWindowViews::IsMinimized() {
void NativeWindowViews::SetFullScreen(bool fullscreen) { void NativeWindowViews::SetFullScreen(bool fullscreen) {
#if defined(OS_WIN) #if defined(OS_WIN)
// There is no native fullscreen state on Windows. // There is no native fullscreen state on Windows.
window_->SetFullscreen(fullscreen); if (fullscreen) {
if (fullscreen) last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
NotifyWindowEnterFullScreen(); NotifyWindowEnterFullScreen();
else } else {
last_window_state_ = ui::SHOW_STATE_NORMAL;
NotifyWindowLeaveFullScreen(); NotifyWindowLeaveFullScreen();
}
// We set the new value after notifying, so we can handle the size event
// correctly.
window_->SetFullscreen(fullscreen);
#else #else
if (IsVisible()) if (IsVisible())
window_->SetFullscreen(fullscreen); window_->SetFullscreen(fullscreen);
@ -807,24 +817,8 @@ void NativeWindowViews::OnWidgetMove() {
#if defined(OS_WIN) #if defined(OS_WIN)
bool NativeWindowViews::ExecuteWindowsCommand(int command_id) { bool NativeWindowViews::ExecuteWindowsCommand(int command_id) {
// Windows uses the 4 lower order bits of |command_id| for type-specific
// information so we must exclude this when comparing.
static const int sc_mask = 0xFFF0;
if ((command_id & sc_mask) == SC_MINIMIZE) {
NotifyWindowMinimize();
is_minimized_ = true;
} else if ((command_id & sc_mask) == SC_RESTORE) {
if (is_minimized_)
NotifyWindowRestore();
else
NotifyWindowUnmaximize();
is_minimized_ = false;
} else if ((command_id & sc_mask) == SC_MAXIMIZE) {
NotifyWindowMaximize();
} else {
std::string command = AppCommandToString(command_id); std::string command = AppCommandToString(command_id);
NotifyWindowExecuteWindowsCommand(command); NotifyWindowExecuteWindowsCommand(command);
}
return false; return false;
} }
#endif #endif
@ -844,11 +838,54 @@ void NativeWindowViews::GetDevToolsWindowWMClass(
#if defined(OS_WIN) #if defined(OS_WIN)
bool NativeWindowViews::PreHandleMSG( bool NativeWindowViews::PreHandleMSG(
UINT message, WPARAM w_param, LPARAM l_param, LRESULT* result) { UINT message, WPARAM w_param, LPARAM l_param, LRESULT* result) {
switch (message) {
case WM_COMMAND:
// Handle thumbar button click message. // Handle thumbar button click message.
if (message == WM_COMMAND && HIWORD(w_param) == THBN_CLICKED) if (HIWORD(w_param) == THBN_CLICKED)
return taskbar_host_.HandleThumbarButtonEvent(LOWORD(w_param)); return taskbar_host_.HandleThumbarButtonEvent(LOWORD(w_param));
else
return false; return false;
case WM_SIZE:
// Handle window state change.
HandleSizeEvent(w_param, l_param);
return false;
default:
return false;
}
}
void NativeWindowViews::HandleSizeEvent(WPARAM w_param, LPARAM l_param) {
// Here we handle the WM_SIZE event in order to figure out what is the current
// window state and notify the user accordingly.
switch (w_param) {
case SIZE_MAXIMIZED:
last_window_state_ = ui::SHOW_STATE_MAXIMIZED;
NotifyWindowMaximize();
break;
case SIZE_MINIMIZED:
last_window_state_ = ui::SHOW_STATE_MINIMIZED;
NotifyWindowMinimize();
break;
case SIZE_RESTORED:
if (last_window_state_ == ui::SHOW_STATE_NORMAL)
return;
switch (last_window_state_) {
case ui::SHOW_STATE_MAXIMIZED:
last_window_state_ = ui::SHOW_STATE_NORMAL;
NotifyWindowUnmaximize();
break;
case ui::SHOW_STATE_MINIMIZED:
if (IsFullscreen()) {
last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
NotifyWindowEnterFullScreen();
} else {
last_window_state_ = ui::SHOW_STATE_NORMAL;
NotifyWindowRestore();
}
break;
}
break;
}
} }
#endif #endif

View file

@ -142,6 +142,8 @@ class NativeWindowViews : public NativeWindow,
// MessageHandlerDelegate: // MessageHandlerDelegate:
bool PreHandleMSG( bool PreHandleMSG(
UINT message, WPARAM w_param, LPARAM l_param, LRESULT* result) override; UINT message, WPARAM w_param, LPARAM l_param, LRESULT* result) override;
void HandleSizeEvent(WPARAM w_param, LPARAM l_param);
#endif #endif
// NativeWindow: // NativeWindow:
@ -178,9 +180,9 @@ class NativeWindowViews : public NativeWindow,
#elif defined(OS_WIN) #elif defined(OS_WIN)
// Weak ref. // Weak ref.
AtomDesktopWindowTreeHostWin* atom_desktop_window_tree_host_win_; AtomDesktopWindowTreeHostWin* atom_desktop_window_tree_host_win_;
// Records window was whether restored from minimized state or maximized
// state. ui::WindowShowState last_window_state_;
bool is_minimized_;
// In charge of running taskbar related APIs. // In charge of running taskbar related APIs.
TaskbarHost taskbar_host_; TaskbarHost taskbar_host_;
#endif #endif