From 32b692b60c36dbfe145d649417ca6939af79187a Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 6 Jun 2016 16:53:32 +0900 Subject: [PATCH] Fix NativeWindow::Focus not moving focus to window Chromium's implementation is somehow not working as expected, the new implementation follows the behavior of wmctrl. --- atom/browser/native_window_views.cc | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index e699892d4ac5..6a0919635f3e 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -89,6 +89,21 @@ bool IsAltModifier(const content::NativeWebKeyboardEvent& event) { (modifiers == (Modifiers::AltKey | Modifiers::IsRight)); } +#if defined(USE_X11) +int SendClientEvent(XDisplay* display, ::Window window, const char* msg) { + XEvent event = {}; + event.xclient.type = ClientMessage; + event.xclient.send_event = True; + event.xclient.message_type = XInternAtom(display, msg, False); + event.xclient.window = window; + event.xclient.format = 32; + XSendEvent(display, DefaultRootWindow(display), False, + SubstructureRedirectMask | SubstructureNotifyMask, &event); + XFlush(display); + return True; +} +#endif + class NativeWindowClientView : public views::ClientView { public: NativeWindowClientView(views::Widget* widget, @@ -298,10 +313,19 @@ void NativeWindowViews::CloseImmediately() { } void NativeWindowViews::Focus(bool focus) { - if (focus) + if (focus) { +#if defined(OS_WIN) window_->Activate(); - else +#elif defined(USE_X11) + // The "Activate" implementation of Chromium is not reliable on Linux. + ::Window window = GetAcceleratedWidget(); + XDisplay* xdisplay = gfx::GetXDisplay(); + SendClientEvent(xdisplay, window, "_NET_ACTIVE_WINDOW"); + XMapRaised(xdisplay, window); +#endif + } else { window_->Deactivate(); + } } bool NativeWindowViews::IsFocused() {