electron/atom/browser/native_window_views.cc

1018 строки
30 KiB
C++
Исходник Обычный вид История

// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/native_window_views.h"
2014-07-21 18:03:58 +04:00
#if defined(OS_WIN)
#include <shobjidl.h>
#endif
#include <string>
#include <vector>
2015-02-11 04:14:26 +03:00
2014-07-16 18:10:10 +04:00
#include "atom/browser/ui/views/menu_bar.h"
#include "atom/browser/ui/views/menu_layout.h"
#include "atom/common/draggable_region.h"
#include "atom/common/options_switches.h"
#include "base/strings/utf_string_conversions.h"
2015-06-25 08:08:11 +03:00
#include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h"
2014-07-04 12:54:10 +04:00
#include "content/public/browser/native_web_keyboard_event.h"
#include "native_mate/dictionary.h"
2014-07-21 18:03:58 +04:00
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/hit_test.h"
2015-02-07 04:00:26 +03:00
#include "ui/gfx/image/image.h"
#include "ui/views/background.h"
#include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/window/client_view.h"
2014-12-16 04:28:51 +03:00
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/widget.h"
2014-12-24 03:55:57 +03:00
#include "ui/wm/core/shadow_types.h"
#if defined(USE_X11)
#include "atom/browser/browser.h"
#include "atom/browser/ui/views/global_menu_bar_x11.h"
2014-07-24 14:06:27 +04:00
#include "atom/browser/ui/views/frameless_view.h"
2015-07-24 11:04:54 +03:00
#include "atom/browser/ui/views/native_frame_view.h"
#include "atom/browser/ui/x/window_state_watcher.h"
2014-08-26 09:37:37 +04:00
#include "atom/browser/ui/x/x_window_utils.h"
#include "base/environment.h"
#include "base/nix/xdg_util.h"
2014-11-24 10:27:36 +03:00
#include "base/strings/string_util.h"
#include "chrome/browser/ui/libgtk2ui/unity_service.h"
#include "dbus/bus.h"
#include "dbus/object_proxy.h"
#include "dbus/message.h"
#include "ui/base/x/x11_util.h"
#include "ui/gfx/x/x11_types.h"
2014-07-24 14:06:27 +04:00
#include "ui/views/window/native_frame_view.h"
#elif defined(OS_WIN)
#include "atom/browser/ui/views/win_frame_view.h"
2014-07-21 18:03:58 +04:00
#include "base/win/scoped_comptr.h"
2014-09-17 05:42:47 +04:00
#include "base/win/windows_version.h"
#include "ui/base/win/shell.h"
2015-02-07 22:57:00 +03:00
#include "ui/gfx/icon_util.h"
2015-02-11 04:14:26 +03:00
#include "ui/gfx/win/dpi.h"
2014-09-17 05:42:47 +04:00
#include "ui/views/win/hwnd_util.h"
#endif
namespace atom {
namespace {
2014-07-16 18:10:10 +04:00
// The menu bar height in pixels.
#if defined(OS_WIN)
const int kMenuBarHeight = 20;
#else
2014-07-16 18:10:10 +04:00
const int kMenuBarHeight = 25;
#endif
2014-07-16 18:10:10 +04:00
#if defined(USE_X11)
// Returns true if the bus name "com.canonical.AppMenu.Registrar" is available.
bool ShouldUseGlobalMenuBar() {
dbus::Bus::Options options;
scoped_refptr<dbus::Bus> bus(new dbus::Bus(options));
dbus::ObjectProxy* object_proxy =
bus->GetObjectProxy(DBUS_SERVICE_DBUS, dbus::ObjectPath(DBUS_PATH_DBUS));
dbus::MethodCall method_call(DBUS_INTERFACE_DBUS, "ListNames");
scoped_ptr<dbus::Response> response(object_proxy->CallMethodAndBlock(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
if (!response) {
bus->ShutdownAndBlock();
return false;
}
dbus::MessageReader reader(response.get());
dbus::MessageReader array_reader(NULL);
if (!reader.PopArray(&array_reader)) {
bus->ShutdownAndBlock();
return false;
}
while (array_reader.HasMoreData()) {
std::string name;
if (array_reader.PopString(&name) &&
name == "com.canonical.AppMenu.Registrar") {
bus->ShutdownAndBlock();
return true;
}
}
bus->ShutdownAndBlock();
return false;
}
#endif
bool IsAltKey(const content::NativeWebKeyboardEvent& event) {
#if defined(USE_X11)
// 164 and 165 represent VK_LALT and VK_RALT.
return event.windowsKeyCode == 164 || event.windowsKeyCode == 165;
#else
return event.windowsKeyCode == ui::VKEY_MENU;
#endif
}
bool IsAltModifier(const content::NativeWebKeyboardEvent& event) {
typedef content::NativeWebKeyboardEvent::Modifiers Modifiers;
int modifiers = event.modifiers;
modifiers &= ~Modifiers::NumLockOn;
modifiers &= ~Modifiers::CapsLockOn;
return (modifiers == Modifiers::AltKey) ||
(modifiers == (Modifiers::AltKey | Modifiers::IsLeft)) ||
(modifiers == (Modifiers::AltKey | Modifiers::IsRight));
}
2015-06-26 06:16:50 +03:00
#if defined(OS_WIN)
// Convert Win32 WM_APPCOMMANDS to strings.
const char* AppCommandToString(int command_id) {
switch (command_id) {
case APPCOMMAND_BROWSER_BACKWARD : return "browser-backward";
case APPCOMMAND_BROWSER_FORWARD : return "browser-forward";
case APPCOMMAND_BROWSER_REFRESH : return "browser-refresh";
case APPCOMMAND_BROWSER_STOP : return "browser-stop";
case APPCOMMAND_BROWSER_SEARCH : return "browser-search";
case APPCOMMAND_BROWSER_FAVORITES : return "browser-favorites";
case APPCOMMAND_BROWSER_HOME : return "browser-home";
case APPCOMMAND_VOLUME_MUTE : return "volume-mute";
case APPCOMMAND_VOLUME_DOWN : return "volume-down";
case APPCOMMAND_VOLUME_UP : return "volume-up";
case APPCOMMAND_MEDIA_NEXTTRACK : return "media-nexttrack";
case APPCOMMAND_MEDIA_PREVIOUSTRACK : return "media-previoustrack";
case APPCOMMAND_MEDIA_STOP : return "media-stop";
case APPCOMMAND_MEDIA_PLAY_PAUSE : return "media-play_pause";
case APPCOMMAND_LAUNCH_MAIL : return "launch-mail";
case APPCOMMAND_LAUNCH_MEDIA_SELECT : return "launch-media-select";
case APPCOMMAND_LAUNCH_APP1 : return "launch-app1";
case APPCOMMAND_LAUNCH_APP2 : return "launch-app2";
case APPCOMMAND_BASS_DOWN : return "bass-down";
case APPCOMMAND_BASS_BOOST : return "bass-boost";
case APPCOMMAND_BASS_UP : return "bass-up";
case APPCOMMAND_TREBLE_DOWN : return "treble-down";
case APPCOMMAND_TREBLE_UP : return "treble-up";
case APPCOMMAND_MICROPHONE_VOLUME_MUTE : return "microphone-volume-mute";
case APPCOMMAND_MICROPHONE_VOLUME_DOWN : return "microphone-volume-down";
case APPCOMMAND_MICROPHONE_VOLUME_UP : return "microphone-volume-up";
case APPCOMMAND_HELP : return "help";
case APPCOMMAND_FIND : return "find";
case APPCOMMAND_NEW : return "new";
case APPCOMMAND_OPEN : return "open";
case APPCOMMAND_CLOSE : return "close";
case APPCOMMAND_SAVE : return "save";
case APPCOMMAND_PRINT : return "print";
case APPCOMMAND_UNDO : return "undo";
case APPCOMMAND_REDO : return "redo";
case APPCOMMAND_COPY : return "copy";
case APPCOMMAND_CUT : return "cut";
case APPCOMMAND_PASTE : return "paste";
case APPCOMMAND_REPLY_TO_MAIL : return "reply-to-mail";
case APPCOMMAND_FORWARD_MAIL : return "forward-mail";
case APPCOMMAND_SEND_MAIL : return "send-mail";
case APPCOMMAND_SPELL_CHECK : return "spell-check";
case APPCOMMAND_MIC_ON_OFF_TOGGLE : return "mic-on-off-toggle";
case APPCOMMAND_CORRECTION_LIST : return "correction-list";
case APPCOMMAND_MEDIA_PLAY : return "media-play";
case APPCOMMAND_MEDIA_PAUSE : return "media-pause";
case APPCOMMAND_MEDIA_RECORD : return "media-record";
case APPCOMMAND_MEDIA_FAST_FORWARD : return "media-fast-forward";
case APPCOMMAND_MEDIA_REWIND : return "media-rewind";
case APPCOMMAND_MEDIA_CHANNEL_UP : return "media-channel-up";
case APPCOMMAND_MEDIA_CHANNEL_DOWN : return "media-channel-down";
case APPCOMMAND_DELETE : return "delete";
case APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE:
return "dictate-or-command-control-toggle";
default:
2015-07-31 21:14:50 +03:00
return "unknown";
2015-06-26 06:16:50 +03:00
}
}
#endif
class NativeWindowClientView : public views::ClientView {
public:
NativeWindowClientView(views::Widget* widget,
NativeWindowViews* contents_view)
: views::ClientView(widget, contents_view) {
}
virtual ~NativeWindowClientView() {}
2015-01-10 04:24:36 +03:00
bool CanClose() override {
static_cast<NativeWindowViews*>(contents_view())->RequestToClosePage();
return false;
}
private:
DISALLOW_COPY_AND_ASSIGN(NativeWindowClientView);
};
} // namespace
NativeWindowViews::NativeWindowViews(
brightray::InspectableWebContents* web_contents,
const mate::Dictionary& options)
: NativeWindow(web_contents, options),
window_(new views::Widget),
2014-07-04 12:54:10 +04:00
web_view_(inspectable_web_contents()->GetView()->GetView()),
menu_bar_autohide_(false),
2014-08-07 12:48:30 +04:00
menu_bar_visible_(false),
menu_bar_alt_pressed_(false),
2014-11-25 09:28:34 +03:00
#if defined(OS_WIN)
is_minimized_(false),
#endif
keyboard_event_handler_(new views::UnhandledKeyboardEventHandler),
use_content_size_(false),
resizable_(true) {
options.Get(switches::kTitle, &title_);
options.Get(switches::kAutoHideMenuBar, &menu_bar_autohide_);
2014-08-26 09:37:37 +04:00
#if defined(OS_WIN)
// On Windows we rely on the CanResize() to indicate whether window can be
// resized, and it should be set before window is created.
options.Get(switches::kResizable, &resizable_);
#endif
if (enable_larger_than_screen_)
// We need to set a default maximum window size here otherwise Windows
// will not allow us to resize the window larger than scree.
// Setting directly to INT_MAX somehow doesn't work, so we just devide
// by 10, which should still be large enough.
2014-08-17 08:57:22 +04:00
maximum_size_.SetSize(INT_MAX / 10, INT_MAX / 10);
int width = 800, height = 600;
options.Get(switches::kWidth, &width);
options.Get(switches::kHeight, &height);
gfx::Rect bounds(0, 0, width, height);
2015-05-20 11:37:13 +03:00
widget_size_ = bounds.size();
2014-07-08 08:55:33 +04:00
window_->AddObserver(this);
views::Widget::InitParams params;
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = bounds;
params.delegate = this;
2014-07-07 12:10:34 +04:00
params.type = views::Widget::InitParams::TYPE_WINDOW;
2014-07-24 14:06:27 +04:00
params.remove_standard_frame = !has_frame_;
2014-07-07 13:22:22 +04:00
2014-12-24 03:55:57 +03:00
if (transparent_)
params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
2014-07-21 18:03:58 +04:00
#if defined(USE_X11)
2014-11-24 10:27:36 +03:00
std::string name = Browser::Get()->GetName();
// Set WM_WINDOW_ROLE.
2014-11-24 10:27:36 +03:00
params.wm_role_name = "browser-window";
// Set WM_CLASS.
2014-11-24 10:27:36 +03:00
params.wm_class_name = base::StringToLowerASCII(name);
params.wm_class_class = name;
2014-07-21 18:03:58 +04:00
#endif
2014-07-07 13:22:22 +04:00
window_->Init(params);
#if defined(USE_X11)
2014-11-25 06:46:30 +03:00
// Start monitoring window states.
window_state_watcher_.reset(new WindowStateWatcher(this));
// Set _GTK_THEME_VARIANT to dark if we have "dark-theme" option set.
bool use_dark_theme = false;
if (options.Get(switches::kDarkTheme, &use_dark_theme) && use_dark_theme) {
XDisplay* xdisplay = gfx::GetXDisplay();
XChangeProperty(xdisplay, GetAcceleratedWidget(),
XInternAtom(xdisplay, "_GTK_THEME_VARIANT", False),
XInternAtom(xdisplay, "UTF8_STRING", False),
8, PropModeReplace,
reinterpret_cast<const unsigned char*>("dark"),
4);
}
// Before the window is mapped the SetWMSpecState can not work, so we have
// to manually set the _NET_WM_STATE.
std::vector<::Atom> state_atom_list;
bool skip_taskbar = false;
if (options.Get(switches::kSkipTaskbar, &skip_taskbar) && skip_taskbar) {
state_atom_list.push_back(GetAtom("_NET_WM_STATE_SKIP_TASKBAR"));
}
// Before the window is mapped, there is no SHOW_FULLSCREEN_STATE.
bool fullscreen = false;
if (options.Get(switches::kFullscreen, & fullscreen) && fullscreen) {
state_atom_list.push_back(GetAtom("_NET_WM_STATE_FULLSCREEN"));
}
ui::SetAtomArrayProperty(GetAcceleratedWidget(), "_NET_WM_STATE", "ATOM",
state_atom_list);
// Set the _NET_WM_WINDOW_TYPE.
std::string window_type;
if (options.Get(switches::kType, &window_type))
SetWindowType(GetAcceleratedWidget(), window_type);
#endif
2014-07-04 08:32:03 +04:00
// Add web view.
SetLayoutManager(new MenuLayout(this, kMenuBarHeight));
2014-07-04 08:32:03 +04:00
set_background(views::Background::CreateStandardPanelBackground());
AddChildView(web_view_);
if (has_frame_ &&
options.Get(switches::kUseContentSize, &use_content_size_) &&
use_content_size_)
2014-07-16 18:10:10 +04:00
bounds = ContentBoundsToWindowBounds(bounds);
2014-11-07 21:34:52 +03:00
#if defined(OS_WIN)
2014-12-24 04:24:04 +03:00
if (!has_frame_) {
2014-11-11 21:09:54 +03:00
// Set Window style so that we get a minimize and maximize animation when
// frameless.
DWORD frame_style = WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX |
WS_CAPTION;
2014-12-24 04:24:04 +03:00
// We should not show a frame for transparent window.
if (transparent_)
frame_style &= ~(WS_THICKFRAME | WS_CAPTION);
::SetWindowLong(GetAcceleratedWidget(), GWL_STYLE, frame_style);
}
2014-12-24 03:55:57 +03:00
if (transparent_) {
// Transparent window on Windows has to have WS_EX_COMPOSITED style.
LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
ex_style |= WS_EX_COMPOSITED;
::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
}
2014-11-07 21:34:52 +03:00
#endif
2014-11-12 15:32:14 +03:00
// TODO(zcbenz): This was used to force using native frame on Windows 2003, we
// should check whether setting it in InitParams can work.
2014-11-12 09:11:25 +03:00
if (has_frame_) {
window_->set_frame_type(views::Widget::FrameType::FRAME_TYPE_FORCE_NATIVE);
window_->FrameTypeChanged();
}
2014-12-24 03:55:57 +03:00
// The given window is most likely not rectangular since it uses
// transparency and has no standard frame, don't show a shadow for it.
if (transparent_ && !has_frame_)
wm::SetShadowType(GetNativeWindow(), wm::SHADOW_TYPE_NONE);
window_->UpdateWindowIcon();
2014-07-04 08:32:03 +04:00
window_->CenterWindow(bounds.size());
Layout();
}
NativeWindowViews::~NativeWindowViews() {
2014-07-08 08:55:33 +04:00
window_->RemoveObserver(this);
}
void NativeWindowViews::Close() {
window_->Close();
}
void NativeWindowViews::CloseImmediately() {
window_->CloseNow();
}
void NativeWindowViews::Focus(bool focus) {
if (focus)
window_->Activate();
else
window_->Deactivate();
}
bool NativeWindowViews::IsFocused() {
return window_->IsActive();
}
void NativeWindowViews::Show() {
window_->native_widget_private()->ShowWithWindowState(GetRestoredState());
2014-10-17 18:51:20 +04:00
}
void NativeWindowViews::ShowInactive() {
window_->ShowInactive();
}
void NativeWindowViews::Hide() {
window_->Hide();
}
bool NativeWindowViews::IsVisible() {
return window_->IsVisible();
}
void NativeWindowViews::Maximize() {
if (IsVisible())
window_->Maximize();
else
2014-12-16 03:57:04 +03:00
window_->native_widget_private()->ShowWithWindowState(
ui::SHOW_STATE_MAXIMIZED);
}
void NativeWindowViews::Unmaximize() {
window_->Restore();
}
bool NativeWindowViews::IsMaximized() {
return window_->IsMaximized();
}
void NativeWindowViews::Minimize() {
if (IsVisible())
window_->Minimize();
else
2014-12-16 03:57:04 +03:00
window_->native_widget_private()->ShowWithWindowState(
ui::SHOW_STATE_MINIMIZED);
}
void NativeWindowViews::Restore() {
window_->Restore();
}
2014-07-26 09:58:26 +04:00
bool NativeWindowViews::IsMinimized() {
return window_->IsMinimized();
}
2014-11-25 09:34:14 +03:00
void NativeWindowViews::SetFullScreen(bool fullscreen) {
2014-11-25 09:28:34 +03:00
#if defined(OS_WIN)
// There is no native fullscreen state on Windows.
window_->SetFullscreen(fullscreen);
2014-11-25 09:28:34 +03:00
if (fullscreen)
NotifyWindowEnterFullScreen();
else
NotifyWindowLeaveFullScreen();
#else
if (IsVisible())
window_->SetFullscreen(fullscreen);
else
window_->native_widget_private()->ShowWithWindowState(
ui::SHOW_STATE_FULLSCREEN);
2014-11-25 09:28:34 +03:00
#endif
}
2015-04-21 16:35:36 +03:00
bool NativeWindowViews::IsFullscreen() const {
return window_->IsFullscreen();
}
void NativeWindowViews::SetBounds(const gfx::Rect& bounds) {
#if defined(USE_X11)
// On Linux the minimum and maximum size should be updated with window size
// when window is not resizable.
if (!resizable_) {
SetMaximumSize(bounds.size());
SetMinimumSize(bounds.size());
}
#endif
window_->SetBounds(bounds);
}
gfx::Rect NativeWindowViews::GetBounds() {
#if defined(OS_WIN)
if (IsMinimized())
return window_->GetRestoredBounds();
#endif
return window_->GetWindowBoundsInScreen();
}
void NativeWindowViews::SetContentSize(const gfx::Size& size) {
2014-07-04 08:32:03 +04:00
if (!has_frame_) {
NativeWindow::SetSize(size);
2014-07-04 08:32:03 +04:00
return;
}
gfx::Rect bounds = window_->GetWindowBoundsInScreen();
bounds.set_size(size);
SetBounds(ContentBoundsToWindowBounds(bounds));
}
gfx::Size NativeWindowViews::GetContentSize() {
2014-07-04 08:32:03 +04:00
if (!has_frame_)
return GetSize();
2014-07-16 18:10:10 +04:00
gfx::Size content_size =
window_->non_client_view()->frame_view()->GetBoundsForClientView().size();
2014-08-07 12:48:30 +04:00
if (menu_bar_ && menu_bar_visible_)
2014-07-16 18:10:10 +04:00
content_size.set_height(content_size.height() - kMenuBarHeight);
return content_size;
}
void NativeWindowViews::SetMinimumSize(const gfx::Size& size) {
minimum_size_ = size;
}
gfx::Size NativeWindowViews::GetMinimumSize() {
return minimum_size_;
}
void NativeWindowViews::SetMaximumSize(const gfx::Size& size) {
maximum_size_ = size;
}
gfx::Size NativeWindowViews::GetMaximumSize() {
return maximum_size_;
}
void NativeWindowViews::SetResizable(bool resizable) {
#if defined(OS_WIN)
// WS_MAXIMIZEBOX => Maximize button
// WS_MINIMIZEBOX => Minimize button
// WS_THICKFRAME => Resize handle
DWORD style = ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE);
if (resizable)
style |= WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THICKFRAME;
else
style = (style & ~(WS_MAXIMIZEBOX | WS_THICKFRAME)) | WS_MINIMIZEBOX;
::SetWindowLong(GetAcceleratedWidget(), GWL_STYLE, style);
2014-08-26 09:37:37 +04:00
#elif defined(USE_X11)
if (resizable != resizable_) {
// On Linux there is no "resizable" property of a window, we have to set
// both the minimum and maximum size to the window size to achieve it.
if (resizable) {
SetMaximumSize(gfx::Size());
SetMinimumSize(gfx::Size());
} else {
SetMaximumSize(GetSize());
SetMinimumSize(GetSize());
}
}
#endif
2014-08-26 09:37:37 +04:00
resizable_ = resizable;
}
bool NativeWindowViews::IsResizable() {
return resizable_;
}
void NativeWindowViews::SetAlwaysOnTop(bool top) {
window_->SetAlwaysOnTop(top);
}
bool NativeWindowViews::IsAlwaysOnTop() {
2014-07-04 08:32:03 +04:00
return window_->IsAlwaysOnTop();
}
void NativeWindowViews::Center() {
window_->CenterWindow(GetSize());
}
void NativeWindowViews::SetTitle(const std::string& title) {
title_ = title;
window_->UpdateWindowTitle();
}
std::string NativeWindowViews::GetTitle() {
return title_;
}
void NativeWindowViews::FlashFrame(bool flash) {
#if defined(OS_WIN)
// The Chromium's implementation has a bug stopping flash.
if (!flash) {
FLASHWINFO fwi;
fwi.cbSize = sizeof(fwi);
fwi.hwnd = GetAcceleratedWidget();
fwi.dwFlags = FLASHW_STOP;
fwi.uCount = 0;
FlashWindowEx(&fwi);
return;
}
#endif
window_->FlashFrame(flash);
}
void NativeWindowViews::SetSkipTaskbar(bool skip) {
2014-07-07 13:22:22 +04:00
#if defined(OS_WIN)
2014-07-21 18:03:58 +04:00
base::win::ScopedComPtr<ITaskbarList> taskbar;
if (FAILED(taskbar.CreateInstance(CLSID_TaskbarList, NULL,
CLSCTX_INPROC_SERVER)) ||
FAILED(taskbar->HrInit()))
return;
if (skip)
taskbar->DeleteTab(GetAcceleratedWidget());
else
taskbar->AddTab(GetAcceleratedWidget());
#elif defined(USE_X11)
SetWMSpecState(GetAcceleratedWidget(), skip,
GetAtom("_NET_WM_STATE_SKIP_TASKBAR"));
2014-07-07 13:22:22 +04:00
#endif
}
void NativeWindowViews::SetKiosk(bool kiosk) {
2014-11-25 09:34:14 +03:00
SetFullScreen(kiosk);
}
bool NativeWindowViews::IsKiosk() {
return IsFullscreen();
}
2014-07-04 12:54:10 +04:00
void NativeWindowViews::SetMenu(ui::MenuModel* menu_model) {
2015-06-04 11:10:19 +03:00
if (menu_model == nullptr) {
// Remove accelerators
accelerator_table_.clear();
GetFocusManager()->UnregisterAccelerators(this);
// and menu bar.
#if defined(USE_X11)
global_menu_bar_.reset();
#endif
SetMenuBarVisibility(false);
menu_bar_.reset();
return;
}
2014-07-04 12:54:10 +04:00
RegisterAccelerators(menu_model);
#if defined(USE_X11)
if (!global_menu_bar_ && ShouldUseGlobalMenuBar())
global_menu_bar_.reset(new GlobalMenuBarX11(this));
2014-07-16 18:10:10 +04:00
// Use global application menu bar when possible.
if (global_menu_bar_ && global_menu_bar_->IsServerStarted()) {
2014-07-16 18:10:10 +04:00
global_menu_bar_->SetMenu(menu_model);
return;
}
#endif
2014-07-16 18:10:10 +04:00
// Do not show menu bar in frameless window.
if (!has_frame_)
return;
if (!menu_bar_) {
gfx::Size content_size = GetContentSize();
menu_bar_.reset(new MenuBar);
menu_bar_->set_owned_by_client();
if (!menu_bar_autohide_) {
SetMenuBarVisibility(true);
if (use_content_size_)
SetContentSize(content_size);
}
2014-07-16 18:10:10 +04:00
}
2014-07-17 10:23:28 +04:00
menu_bar_->SetMenu(menu_model);
Layout();
2014-07-04 12:54:10 +04:00
}
gfx::NativeWindow NativeWindowViews::GetNativeWindow() {
return window_->GetNativeWindow();
}
2014-09-17 05:42:47 +04:00
void NativeWindowViews::SetProgressBar(double progress) {
#if defined(OS_WIN)
if (base::win::GetVersion() < base::win::VERSION_WIN7)
return;
base::win::ScopedComPtr<ITaskbarList3> taskbar;
if (FAILED(taskbar.CreateInstance(CLSID_TaskbarList, NULL,
CLSCTX_INPROC_SERVER) ||
FAILED(taskbar->HrInit()))) {
return;
}
HWND frame = views::HWNDForNativeWindow(GetNativeWindow());
if (progress > 1.0) {
taskbar->SetProgressState(frame, TBPF_INDETERMINATE);
} else if (progress < 0) {
taskbar->SetProgressState(frame, TBPF_NOPROGRESS);
} else if (progress >= 0) {
2014-09-18 12:17:49 +04:00
taskbar->SetProgressValue(frame,
static_cast<int>(progress * 100),
100);
2014-09-17 05:42:47 +04:00
}
2014-09-18 15:12:24 +04:00
#elif defined(USE_X11)
if (unity::IsRunning()) {
unity::SetProgressFraction(progress);
}
2014-09-17 05:42:47 +04:00
#endif
}
2015-02-11 04:14:26 +03:00
void NativeWindowViews::SetOverlayIcon(const gfx::Image& overlay,
const std::string& description) {
#if defined(OS_WIN)
if (base::win::GetVersion() < base::win::VERSION_WIN7)
return;
base::win::ScopedComPtr<ITaskbarList3> taskbar;
if (FAILED(taskbar.CreateInstance(CLSID_TaskbarList, NULL,
CLSCTX_INPROC_SERVER) ||
FAILED(taskbar->HrInit()))) {
return;
}
HWND frame = views::HWNDForNativeWindow(GetNativeWindow());
2015-02-07 04:00:38 +03:00
std::wstring wstr = std::wstring(description.begin(), description.end());
taskbar->SetOverlayIcon(frame,
2015-02-11 04:14:26 +03:00
IconUtil::CreateHICONFromSkBitmap(overlay.AsBitmap()),
wstr.c_str());
#endif
}
2014-11-12 15:32:14 +03:00
void NativeWindowViews::SetAutoHideMenuBar(bool auto_hide) {
menu_bar_autohide_ = auto_hide;
}
bool NativeWindowViews::IsMenuBarAutoHide() {
return menu_bar_autohide_;
}
void NativeWindowViews::SetMenuBarVisibility(bool visible) {
if (!menu_bar_ || menu_bar_visible_ == visible)
return;
// Always show the accelerator when the auto-hide menu bar shows.
if (menu_bar_autohide_)
menu_bar_->SetAcceleratorVisibility(visible);
menu_bar_visible_ = visible;
if (visible) {
DCHECK_EQ(child_count(), 1);
AddChildView(menu_bar_.get());
} else {
DCHECK_EQ(child_count(), 2);
RemoveChildView(menu_bar_.get());
}
Layout();
}
bool NativeWindowViews::IsMenuBarVisible() {
return menu_bar_visible_;
}
void NativeWindowViews::SetVisibleOnAllWorkspaces(bool visible) {
window_->SetVisibleOnAllWorkspaces(visible);
}
bool NativeWindowViews::IsVisibleOnAllWorkspaces() {
2015-03-27 14:41:07 +03:00
#if defined(USE_X11)
// Use the presence/absence of _NET_WM_STATE_STICKY in _NET_WM_STATE to
// determine whether the current window is visible on all workspaces.
2015-03-29 16:35:40 +03:00
XAtom sticky_atom = GetAtom("_NET_WM_STATE_STICKY");
std::vector<XAtom> wm_states;
ui::GetAtomArrayProperty(GetAcceleratedWidget(), "_NET_WM_STATE", &wm_states);
return std::find(wm_states.begin(),
wm_states.end(), sticky_atom) != wm_states.end();
2015-03-27 14:41:07 +03:00
#endif
return false;
}
2014-07-21 18:03:58 +04:00
gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() {
return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
}
2014-07-08 08:55:33 +04:00
void NativeWindowViews::OnWidgetActivationChanged(
views::Widget* widget, bool active) {
if (widget != window_.get())
return;
if (active)
NotifyWindowFocus();
else
NotifyWindowBlur();
2015-06-25 09:54:00 +03:00
if (active && inspectable_web_contents() &&
2015-06-08 08:19:56 +03:00
!inspectable_web_contents()->IsDevToolsViewShowing())
2015-06-25 09:54:00 +03:00
web_contents()->Focus();
2014-08-07 10:48:02 +04:00
// Hide menu bar when window is blured.
2014-11-12 15:32:14 +03:00
if (!active && menu_bar_autohide_ && menu_bar_visible_)
SetMenuBarVisibility(false);
2014-07-08 08:55:33 +04:00
}
2015-05-09 18:55:10 +03:00
void NativeWindowViews::OnWidgetBoundsChanged(
views::Widget* widget, const gfx::Rect& bounds) {
if (widget != window_.get())
return;
if (widget_size_ != bounds.size()) {
NotifyWindowResize();
widget_size_ = bounds.size();
}
}
void NativeWindowViews::DeleteDelegate() {
NotifyWindowClosed();
}
views::View* NativeWindowViews::GetInitiallyFocusedView() {
2014-07-04 12:54:10 +04:00
return inspectable_web_contents()->GetView()->GetWebView();
}
bool NativeWindowViews::CanResize() const {
return resizable_;
}
bool NativeWindowViews::CanMaximize() const {
return resizable_;
}
bool NativeWindowViews::CanMinimize() const {
return true;
}
base::string16 NativeWindowViews::GetWindowTitle() const {
return base::UTF8ToUTF16(title_);
}
bool NativeWindowViews::ShouldHandleSystemCommands() const {
return true;
}
gfx::ImageSkia NativeWindowViews::GetWindowAppIcon() {
2014-08-11 06:06:46 +04:00
return icon_;
}
gfx::ImageSkia NativeWindowViews::GetWindowIcon() {
return GetWindowAppIcon();
}
views::Widget* NativeWindowViews::GetWidget() {
return window_.get();
}
const views::Widget* NativeWindowViews::GetWidget() const {
return window_.get();
}
views::View* NativeWindowViews::GetContentsView() {
return this;
}
bool NativeWindowViews::ShouldDescendIntoChildForEventHandling(
gfx::NativeView child,
const gfx::Point& location) {
// App window should claim mouse events that fall within the draggable region.
if (draggable_region() &&
draggable_region()->contains(location.x(), location.y()))
return false;
// And the events on border for dragging resizable frameless window.
if (!has_frame_ && CanResize()) {
2014-07-16 11:33:40 +04:00
FramelessView* frame = static_cast<FramelessView*>(
window_->non_client_view()->frame_view());
return frame->ResizingBorderHitTest(location) == HTNOWHERE;
}
return true;
}
views::ClientView* NativeWindowViews::CreateClientView(views::Widget* widget) {
return new NativeWindowClientView(widget, this);
}
2014-07-04 08:32:03 +04:00
views::NonClientFrameView* NativeWindowViews::CreateNonClientFrameView(
views::Widget* widget) {
2014-07-24 14:06:27 +04:00
#if defined(OS_WIN)
2014-11-12 09:33:30 +03:00
WinFrameView* frame_view = new WinFrameView;
frame_view->Init(this, widget);
return frame_view;
#else
2014-07-24 14:06:27 +04:00
if (has_frame_) {
2015-07-24 11:04:54 +03:00
return new NativeFrameView(this, widget);
2014-07-24 14:06:27 +04:00
} else {
2014-11-12 09:33:30 +03:00
FramelessView* frame_view = new FramelessView;
2014-07-24 14:06:27 +04:00
frame_view->Init(this, widget);
return frame_view;
}
#endif
2014-07-04 08:32:03 +04:00
}
void NativeWindowViews::OnWidgetMove() {
NotifyWindowMove();
}
2014-11-25 09:28:34 +03:00
#if defined(OS_WIN)
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);
2015-06-26 06:16:50 +03:00
FOR_EACH_OBSERVER(NativeWindowObserver,
observers_,
OnExecuteWindowsCommand(command));
2014-11-25 09:28:34 +03:00
}
return false;
}
#endif
gfx::ImageSkia NativeWindowViews::GetDevToolsWindowIcon() {
return GetWindowAppIcon();
}
#if defined(USE_X11)
void NativeWindowViews::GetDevToolsWindowWMClass(
std::string* name, std::string* class_name) {
*class_name = Browser::Get()->GetName();
*name = base::StringToLowerASCII(*class_name);
}
#endif
2014-07-04 12:54:10 +04:00
void NativeWindowViews::HandleKeyboardEvent(
content::WebContents*,
const content::NativeWebKeyboardEvent& event) {
keyboard_event_handler_->HandleKeyboardEvent(event, GetFocusManager());
2014-08-18 12:14:28 +04:00
if (!menu_bar_)
return;
// Show accelerator when "Alt" is pressed.
if (menu_bar_visible_ && IsAltKey(event))
menu_bar_->SetAcceleratorVisibility(
event.type == blink::WebInputEvent::RawKeyDown);
// Show the submenu when "Alt+Key" is pressed.
if (event.type == blink::WebInputEvent::RawKeyDown && !IsAltKey(event) &&
IsAltModifier(event)) {
if (!menu_bar_visible_ &&
2014-11-12 15:32:14 +03:00
(menu_bar_->GetAcceleratorIndex(event.windowsKeyCode) != -1))
2014-08-18 12:14:28 +04:00
SetMenuBarVisibility(true);
menu_bar_->ActivateAccelerator(event.windowsKeyCode);
return;
}
if (!menu_bar_autohide_)
return;
// Toggle the menu bar only when a single Alt is released.
if (event.type == blink::WebInputEvent::RawKeyDown && IsAltKey(event)) {
// When a single Alt is pressed:
menu_bar_alt_pressed_ = true;
} else if (event.type == blink::WebInputEvent::KeyUp && IsAltKey(event) &&
#if defined(USE_X11)
event.modifiers == 0 &&
#endif
menu_bar_alt_pressed_) {
// When a single Alt is released right after a Alt is pressed:
menu_bar_alt_pressed_ = false;
2014-08-07 12:48:30 +04:00
SetMenuBarVisibility(!menu_bar_visible_);
} else {
// When any other keys except single Alt have been pressed/released:
menu_bar_alt_pressed_ = false;
}
2014-07-04 12:54:10 +04:00
}
bool NativeWindowViews::AcceleratorPressed(const ui::Accelerator& accelerator) {
return accelerator_util::TriggerAcceleratorTableCommand(
&accelerator_table_, accelerator);
}
void NativeWindowViews::RegisterAccelerators(ui::MenuModel* menu_model) {
// Clear previous accelerators.
views::FocusManager* focus_manager = GetFocusManager();
accelerator_table_.clear();
focus_manager->UnregisterAccelerators(this);
// Register accelerators with focus manager.
accelerator_util::GenerateAcceleratorTable(&accelerator_table_, menu_model);
accelerator_util::AcceleratorTable::const_iterator iter;
for (iter = accelerator_table_.begin();
iter != accelerator_table_.end();
++iter) {
focus_manager->RegisterAccelerator(
iter->first, ui::AcceleratorManager::kNormalPriority, this);
}
}
2014-07-16 18:10:10 +04:00
gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
const gfx::Rect& bounds) {
gfx::Point origin = bounds.origin();
#if defined(OS_WIN)
gfx::Rect dpi_bounds = gfx::win::DIPToScreenRect(bounds);
gfx::Rect window_bounds = gfx::win::ScreenToDIPRect(
window_->non_client_view()->GetWindowBoundsForClientBounds(dpi_bounds));
#else
2014-07-16 18:10:10 +04:00
gfx::Rect window_bounds =
window_->non_client_view()->GetWindowBoundsForClientBounds(bounds);
#endif
// The window's position would also be changed, but we only want to change
// the size.
window_bounds.set_origin(origin);
2014-08-07 12:48:30 +04:00
if (menu_bar_ && menu_bar_visible_)
2014-07-16 18:10:10 +04:00
window_bounds.set_height(window_bounds.height() + kMenuBarHeight);
return window_bounds;
}
ui::WindowShowState NativeWindowViews::GetRestoredState() {
if (IsMaximized())
return ui::SHOW_STATE_MAXIMIZED;
if (IsFullscreen())
return ui::SHOW_STATE_FULLSCREEN;
return ui::SHOW_STATE_NORMAL;
}
// static
NativeWindow* NativeWindow::Create(
brightray::InspectableWebContents* inspectable_web_contents,
const mate::Dictionary& options) {
return new NativeWindowViews(inspectable_web_contents, options);
}
} // namespace atom