native_window_views.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/native_window_views.h"
  5. #include <string>
  6. #include <vector>
  7. #include "atom/browser/ui/views/menu_bar.h"
  8. #include "atom/browser/ui/views/menu_layout.h"
  9. #include "atom/browser/window_list.h"
  10. #include "atom/common/color_util.h"
  11. #include "atom/common/draggable_region.h"
  12. #include "atom/common/native_mate_converters/image_converter.h"
  13. #include "atom/common/options_switches.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "brightray/browser/inspectable_web_contents.h"
  16. #include "brightray/browser/inspectable_web_contents_view.h"
  17. #include "content/public/browser/browser_thread.h"
  18. #include "content/public/browser/native_web_keyboard_event.h"
  19. #include "native_mate/dictionary.h"
  20. #include "ui/aura/window_tree_host.h"
  21. #include "ui/base/hit_test.h"
  22. #include "ui/gfx/image/image.h"
  23. #include "ui/views/background.h"
  24. #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
  25. #include "ui/views/controls/webview/webview.h"
  26. #include "ui/views/widget/native_widget_private.h"
  27. #include "ui/views/widget/widget.h"
  28. #include "ui/views/window/client_view.h"
  29. #include "ui/wm/core/shadow_types.h"
  30. #include "ui/wm/core/window_util.h"
  31. #if defined(USE_X11)
  32. #include "atom/browser/browser.h"
  33. #include "atom/browser/ui/views/frameless_view.h"
  34. #include "atom/browser/ui/views/global_menu_bar_x11.h"
  35. #include "atom/browser/ui/views/native_frame_view.h"
  36. #include "atom/browser/ui/x/event_disabler.h"
  37. #include "atom/browser/ui/x/window_state_watcher.h"
  38. #include "atom/browser/ui/x/x_window_utils.h"
  39. #include "base/strings/string_util.h"
  40. #include "chrome/browser/ui/libgtk2ui/unity_service.h"
  41. #include "ui/base/x/x11_util.h"
  42. #include "ui/gfx/x/x11_types.h"
  43. #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
  44. #include "ui/views/window/native_frame_view.h"
  45. #elif defined(OS_WIN)
  46. #include "atom/browser/ui/views/win_frame_view.h"
  47. #include "atom/browser/ui/win/atom_desktop_window_tree_host_win.h"
  48. #include "skia/ext/skia_utils_win.h"
  49. #include "ui/base/win/shell.h"
  50. #include "ui/display/display.h"
  51. #include "ui/display/screen.h"
  52. #include "ui/display/win/screen_win.h"
  53. #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
  54. #endif
  55. namespace atom {
  56. namespace {
  57. // The menu bar height in pixels.
  58. #if defined(OS_WIN)
  59. const int kMenuBarHeight = 20;
  60. #else
  61. const int kMenuBarHeight = 25;
  62. #endif
  63. #if defined(OS_WIN)
  64. void FlipWindowStyle(HWND handle, bool on, DWORD flag) {
  65. DWORD style = ::GetWindowLong(handle, GWL_STYLE);
  66. if (on)
  67. style |= flag;
  68. else
  69. style &= ~flag;
  70. ::SetWindowLong(handle, GWL_STYLE, style);
  71. }
  72. #endif
  73. bool IsAltKey(const content::NativeWebKeyboardEvent& event) {
  74. return event.windowsKeyCode == ui::VKEY_MENU;
  75. }
  76. bool IsAltModifier(const content::NativeWebKeyboardEvent& event) {
  77. typedef content::NativeWebKeyboardEvent::Modifiers Modifiers;
  78. int modifiers = event.modifiers;
  79. modifiers &= ~Modifiers::NumLockOn;
  80. modifiers &= ~Modifiers::CapsLockOn;
  81. return (modifiers == Modifiers::AltKey) ||
  82. (modifiers == (Modifiers::AltKey | Modifiers::IsLeft)) ||
  83. (modifiers == (Modifiers::AltKey | Modifiers::IsRight));
  84. }
  85. #if defined(USE_X11)
  86. int SendClientEvent(XDisplay* display, ::Window window, const char* msg) {
  87. XEvent event = {};
  88. event.xclient.type = ClientMessage;
  89. event.xclient.send_event = True;
  90. event.xclient.message_type = XInternAtom(display, msg, False);
  91. event.xclient.window = window;
  92. event.xclient.format = 32;
  93. XSendEvent(display, DefaultRootWindow(display), False,
  94. SubstructureRedirectMask | SubstructureNotifyMask, &event);
  95. XFlush(display);
  96. return True;
  97. }
  98. #endif
  99. class NativeWindowClientView : public views::ClientView {
  100. public:
  101. NativeWindowClientView(views::Widget* widget,
  102. NativeWindowViews* contents_view)
  103. : views::ClientView(widget, contents_view) {
  104. }
  105. virtual ~NativeWindowClientView() {}
  106. bool CanClose() override {
  107. static_cast<NativeWindowViews*>(contents_view())->RequestToClosePage();
  108. return false;
  109. }
  110. private:
  111. DISALLOW_COPY_AND_ASSIGN(NativeWindowClientView);
  112. };
  113. } // namespace
  114. NativeWindowViews::NativeWindowViews(
  115. brightray::InspectableWebContents* web_contents,
  116. const mate::Dictionary& options,
  117. NativeWindow* parent)
  118. : NativeWindow(web_contents, options, parent),
  119. window_(new views::Widget),
  120. web_view_(inspectable_web_contents()->GetView()->GetView()),
  121. menu_bar_autohide_(false),
  122. menu_bar_visible_(false),
  123. menu_bar_alt_pressed_(false),
  124. #if defined(OS_WIN)
  125. checked_for_a11y_support_(false),
  126. thick_frame_(true),
  127. #endif
  128. keyboard_event_handler_(new views::UnhandledKeyboardEventHandler),
  129. disable_count_(0),
  130. use_content_size_(false),
  131. movable_(true),
  132. resizable_(true),
  133. maximizable_(true),
  134. minimizable_(true),
  135. fullscreenable_(true) {
  136. options.Get(options::kTitle, &title_);
  137. options.Get(options::kAutoHideMenuBar, &menu_bar_autohide_);
  138. #if defined(OS_WIN)
  139. // On Windows we rely on the CanResize() to indicate whether window can be
  140. // resized, and it should be set before window is created.
  141. options.Get(options::kResizable, &resizable_);
  142. options.Get(options::kMinimizable, &minimizable_);
  143. options.Get(options::kMaximizable, &maximizable_);
  144. // Transparent window must not have thick frame.
  145. options.Get("thickFrame", &thick_frame_);
  146. if (transparent())
  147. thick_frame_ = false;
  148. #endif
  149. if (enable_larger_than_screen())
  150. // We need to set a default maximum window size here otherwise Windows
  151. // will not allow us to resize the window larger than scree.
  152. // Setting directly to INT_MAX somehow doesn't work, so we just devide
  153. // by 10, which should still be large enough.
  154. SetContentSizeConstraints(extensions::SizeConstraints(
  155. gfx::Size(), gfx::Size(INT_MAX / 10, INT_MAX / 10)));
  156. int width = 800, height = 600;
  157. options.Get(options::kWidth, &width);
  158. options.Get(options::kHeight, &height);
  159. gfx::Rect bounds(0, 0, width, height);
  160. widget_size_ = bounds.size();
  161. window_->AddObserver(this);
  162. views::Widget::InitParams params;
  163. params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  164. params.bounds = bounds;
  165. params.delegate = this;
  166. params.type = views::Widget::InitParams::TYPE_WINDOW;
  167. params.remove_standard_frame = !has_frame();
  168. if (transparent())
  169. params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
  170. // The given window is most likely not rectangular since it uses
  171. // transparency and has no standard frame, don't show a shadow for it.
  172. if (transparent() && !has_frame())
  173. params.shadow_type = views::Widget::InitParams::SHADOW_TYPE_NONE;
  174. bool focusable;
  175. if (options.Get(options::kFocusable, &focusable) && !focusable)
  176. params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
  177. #if defined(OS_WIN)
  178. if (parent)
  179. params.parent = parent->GetNativeWindow();
  180. params.native_widget =
  181. new views::DesktopNativeWidgetAura(window_.get());
  182. atom_desktop_window_tree_host_win_ = new AtomDesktopWindowTreeHostWin(
  183. this,
  184. window_.get(),
  185. static_cast<views::DesktopNativeWidgetAura*>(params.native_widget));
  186. params.desktop_window_tree_host = atom_desktop_window_tree_host_win_;
  187. #elif defined(USE_X11)
  188. std::string name = Browser::Get()->GetName();
  189. // Set WM_WINDOW_ROLE.
  190. params.wm_role_name = "browser-window";
  191. // Set WM_CLASS.
  192. params.wm_class_name = base::ToLowerASCII(name);
  193. params.wm_class_class = name;
  194. #endif
  195. window_->Init(params);
  196. bool fullscreen = false;
  197. options.Get(options::kFullscreen, &fullscreen);
  198. std::string window_type;
  199. options.Get(options::kType, &window_type);
  200. #if defined(USE_X11)
  201. // Start monitoring window states.
  202. window_state_watcher_.reset(new WindowStateWatcher(this));
  203. // Set _GTK_THEME_VARIANT to dark if we have "dark-theme" option set.
  204. bool use_dark_theme = false;
  205. if (options.Get(options::kDarkTheme, &use_dark_theme) && use_dark_theme) {
  206. XDisplay* xdisplay = gfx::GetXDisplay();
  207. XChangeProperty(xdisplay, GetAcceleratedWidget(),
  208. XInternAtom(xdisplay, "_GTK_THEME_VARIANT", False),
  209. XInternAtom(xdisplay, "UTF8_STRING", False),
  210. 8, PropModeReplace,
  211. reinterpret_cast<const unsigned char*>("dark"),
  212. 4);
  213. }
  214. // Before the window is mapped the SetWMSpecState can not work, so we have
  215. // to manually set the _NET_WM_STATE.
  216. std::vector<::Atom> state_atom_list;
  217. bool skip_taskbar = false;
  218. if (options.Get(options::kSkipTaskbar, &skip_taskbar) && skip_taskbar) {
  219. state_atom_list.push_back(GetAtom("_NET_WM_STATE_SKIP_TASKBAR"));
  220. }
  221. // Before the window is mapped, there is no SHOW_FULLSCREEN_STATE.
  222. if (fullscreen) {
  223. state_atom_list.push_back(GetAtom("_NET_WM_STATE_FULLSCREEN"));
  224. }
  225. if (parent) {
  226. SetParentWindow(parent);
  227. // Force using dialog type for child window.
  228. window_type = "dialog";
  229. // Modal window needs the _NET_WM_STATE_MODAL hint.
  230. if (is_modal())
  231. state_atom_list.push_back(GetAtom("_NET_WM_STATE_MODAL"));
  232. }
  233. ui::SetAtomArrayProperty(GetAcceleratedWidget(), "_NET_WM_STATE", "ATOM",
  234. state_atom_list);
  235. // Set the _NET_WM_WINDOW_TYPE.
  236. if (!window_type.empty())
  237. SetWindowType(GetAcceleratedWidget(), window_type);
  238. #endif
  239. // Add web view.
  240. SetLayoutManager(new MenuLayout(this, kMenuBarHeight));
  241. AddChildView(web_view_);
  242. #if defined(OS_WIN)
  243. if (!has_frame()) {
  244. // Set Window style so that we get a minimize and maximize animation when
  245. // frameless.
  246. DWORD frame_style = WS_CAPTION;
  247. if (resizable_)
  248. frame_style |= WS_THICKFRAME;
  249. if (minimizable_)
  250. frame_style |= WS_MINIMIZEBOX;
  251. if (maximizable_)
  252. frame_style |= WS_MAXIMIZEBOX;
  253. // We should not show a frame for transparent window.
  254. if (!thick_frame_)
  255. frame_style &= ~(WS_THICKFRAME | WS_CAPTION);
  256. ::SetWindowLong(GetAcceleratedWidget(), GWL_STYLE, frame_style);
  257. }
  258. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  259. // Window without thick frame has to have WS_EX_COMPOSITED style.
  260. if (!thick_frame_)
  261. ex_style |= WS_EX_COMPOSITED;
  262. if (window_type == "toolbar")
  263. ex_style |= WS_EX_TOOLWINDOW;
  264. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  265. #endif
  266. // TODO(zcbenz): This was used to force using native frame on Windows 2003, we
  267. // should check whether setting it in InitParams can work.
  268. if (has_frame()) {
  269. window_->set_frame_type(views::Widget::FrameType::FRAME_TYPE_FORCE_NATIVE);
  270. window_->FrameTypeChanged();
  271. }
  272. gfx::Size size = bounds.size();
  273. if (has_frame() &&
  274. options.Get(options::kUseContentSize, &use_content_size_) &&
  275. use_content_size_)
  276. size = ContentBoundsToWindowBounds(gfx::Rect(size)).size();
  277. window_->CenterWindow(size);
  278. Layout();
  279. #if defined(OS_WIN)
  280. // Save initial window state.
  281. if (fullscreen)
  282. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  283. else
  284. last_window_state_ = ui::SHOW_STATE_NORMAL;
  285. last_normal_bounds_ = GetBounds();
  286. #endif
  287. }
  288. NativeWindowViews::~NativeWindowViews() {
  289. window_->RemoveObserver(this);
  290. }
  291. void NativeWindowViews::Close() {
  292. if (!IsClosable()) {
  293. WindowList::WindowCloseCancelled(this);
  294. return;
  295. }
  296. window_->Close();
  297. }
  298. void NativeWindowViews::CloseImmediately() {
  299. window_->CloseNow();
  300. }
  301. void NativeWindowViews::Focus(bool focus) {
  302. // For hidden window focus() should do nothing.
  303. if (!IsVisible())
  304. return;
  305. if (focus) {
  306. #if defined(OS_WIN)
  307. window_->Activate();
  308. #elif defined(USE_X11)
  309. // The "Activate" implementation of Chromium is not reliable on Linux.
  310. ::Window window = GetAcceleratedWidget();
  311. XDisplay* xdisplay = gfx::GetXDisplay();
  312. SendClientEvent(xdisplay, window, "_NET_ACTIVE_WINDOW");
  313. XMapRaised(xdisplay, window);
  314. #endif
  315. } else {
  316. window_->Deactivate();
  317. }
  318. }
  319. bool NativeWindowViews::IsFocused() {
  320. return window_->IsActive();
  321. }
  322. void NativeWindowViews::Show() {
  323. if (is_modal() && NativeWindow::parent())
  324. static_cast<NativeWindowViews*>(NativeWindow::parent())->SetEnabled(false);
  325. window_->native_widget_private()->ShowWithWindowState(GetRestoredState());
  326. NotifyWindowShow();
  327. #if defined(USE_X11)
  328. if (global_menu_bar_)
  329. global_menu_bar_->OnWindowMapped();
  330. #endif
  331. }
  332. void NativeWindowViews::ShowInactive() {
  333. window_->ShowInactive();
  334. NotifyWindowShow();
  335. #if defined(USE_X11)
  336. if (global_menu_bar_)
  337. global_menu_bar_->OnWindowMapped();
  338. #endif
  339. }
  340. void NativeWindowViews::Hide() {
  341. if (is_modal() && NativeWindow::parent())
  342. static_cast<NativeWindowViews*>(NativeWindow::parent())->SetEnabled(true);
  343. window_->Hide();
  344. NotifyWindowHide();
  345. #if defined(USE_X11)
  346. if (global_menu_bar_)
  347. global_menu_bar_->OnWindowUnmapped();
  348. #endif
  349. }
  350. bool NativeWindowViews::IsVisible() {
  351. return window_->IsVisible();
  352. }
  353. bool NativeWindowViews::IsEnabled() {
  354. #if defined(OS_WIN)
  355. return ::IsWindowEnabled(GetAcceleratedWidget());
  356. #elif defined(USE_X11)
  357. return !event_disabler_.get();
  358. #endif
  359. }
  360. void NativeWindowViews::Maximize() {
  361. #if defined(OS_WIN)
  362. // For window without WS_THICKFRAME style, we can not call Maximize().
  363. if (!(::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_THICKFRAME)) {
  364. restore_bounds_ = GetBounds();
  365. auto display =
  366. display::Screen::GetScreen()->GetDisplayNearestPoint(GetPosition());
  367. SetBounds(display.work_area(), false);
  368. return;
  369. }
  370. #endif
  371. if (IsVisible())
  372. window_->Maximize();
  373. else
  374. window_->native_widget_private()->ShowWithWindowState(
  375. ui::SHOW_STATE_MAXIMIZED);
  376. }
  377. void NativeWindowViews::Unmaximize() {
  378. #if defined(OS_WIN)
  379. if (!(::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_THICKFRAME)) {
  380. SetBounds(restore_bounds_, false);
  381. return;
  382. }
  383. #endif
  384. window_->Restore();
  385. }
  386. bool NativeWindowViews::IsMaximized() {
  387. return window_->IsMaximized();
  388. }
  389. void NativeWindowViews::Minimize() {
  390. if (IsVisible())
  391. window_->Minimize();
  392. else
  393. window_->native_widget_private()->ShowWithWindowState(
  394. ui::SHOW_STATE_MINIMIZED);
  395. }
  396. void NativeWindowViews::Restore() {
  397. window_->Restore();
  398. }
  399. bool NativeWindowViews::IsMinimized() {
  400. return window_->IsMinimized();
  401. }
  402. void NativeWindowViews::SetFullScreen(bool fullscreen) {
  403. if (!IsFullScreenable())
  404. return;
  405. #if defined(OS_WIN)
  406. // There is no native fullscreen state on Windows.
  407. if (fullscreen) {
  408. last_window_state_ = ui::SHOW_STATE_FULLSCREEN;
  409. NotifyWindowEnterFullScreen();
  410. } else {
  411. last_window_state_ = ui::SHOW_STATE_NORMAL;
  412. NotifyWindowLeaveFullScreen();
  413. }
  414. // For window without WS_THICKFRAME style, we can not call SetFullscreen().
  415. // This path will be used for transparent windows as well.
  416. if (!thick_frame_) {
  417. if (fullscreen) {
  418. restore_bounds_ = GetBounds();
  419. auto display =
  420. display::Screen::GetScreen()->GetDisplayNearestPoint(GetPosition());
  421. SetBounds(display.bounds(), false);
  422. } else {
  423. SetBounds(restore_bounds_, false);
  424. }
  425. return;
  426. }
  427. // We set the new value after notifying, so we can handle the size event
  428. // correctly.
  429. window_->SetFullscreen(fullscreen);
  430. #else
  431. if (IsVisible())
  432. window_->SetFullscreen(fullscreen);
  433. else if (fullscreen)
  434. window_->native_widget_private()->ShowWithWindowState(
  435. ui::SHOW_STATE_FULLSCREEN);
  436. // Auto-hide menubar when in fullscreen.
  437. if (fullscreen)
  438. SetMenuBarVisibility(false);
  439. else
  440. SetMenuBarVisibility(!menu_bar_autohide_);
  441. #endif
  442. }
  443. bool NativeWindowViews::IsFullscreen() const {
  444. return window_->IsFullscreen();
  445. }
  446. void NativeWindowViews::SetBounds(const gfx::Rect& bounds, bool animate) {
  447. #if defined(USE_X11)
  448. // On Linux the minimum and maximum size should be updated with window size
  449. // when window is not resizable.
  450. if (!resizable_) {
  451. SetMaximumSize(bounds.size());
  452. SetMinimumSize(bounds.size());
  453. }
  454. #endif
  455. window_->SetBounds(bounds);
  456. }
  457. gfx::Rect NativeWindowViews::GetBounds() {
  458. #if defined(OS_WIN)
  459. if (IsMinimized())
  460. return window_->GetRestoredBounds();
  461. #endif
  462. return window_->GetWindowBoundsInScreen();
  463. }
  464. gfx::Rect NativeWindowViews::GetContentBounds() {
  465. return web_view_->GetBoundsInScreen();
  466. }
  467. gfx::Size NativeWindowViews::GetContentSize() {
  468. #if defined(OS_WIN)
  469. if (IsMinimized())
  470. return NativeWindow::GetContentSize();
  471. #endif
  472. return web_view_->size();
  473. }
  474. void NativeWindowViews::SetContentSizeConstraints(
  475. const extensions::SizeConstraints& size_constraints) {
  476. NativeWindow::SetContentSizeConstraints(size_constraints);
  477. // widget_delegate() is only available after Init() is called, we make use of
  478. // this to determine whether native widget has initialized.
  479. if (window_ && window_->widget_delegate())
  480. window_->OnSizeConstraintsChanged();
  481. #if defined(USE_X11)
  482. if (resizable_)
  483. old_size_constraints_ = size_constraints;
  484. #endif
  485. }
  486. void NativeWindowViews::SetResizable(bool resizable) {
  487. #if defined(OS_WIN)
  488. if (has_frame())
  489. FlipWindowStyle(GetAcceleratedWidget(), resizable, WS_THICKFRAME);
  490. #elif defined(USE_X11)
  491. if (resizable != resizable_) {
  492. // On Linux there is no "resizable" property of a window, we have to set
  493. // both the minimum and maximum size to the window size to achieve it.
  494. if (resizable) {
  495. SetContentSizeConstraints(old_size_constraints_);
  496. } else {
  497. old_size_constraints_ = GetContentSizeConstraints();
  498. resizable_ = false;
  499. gfx::Size content_size = GetContentSize();
  500. SetContentSizeConstraints(
  501. extensions::SizeConstraints(content_size, content_size));
  502. }
  503. }
  504. #endif
  505. resizable_ = resizable;
  506. }
  507. bool NativeWindowViews::IsResizable() {
  508. #if defined(OS_WIN)
  509. if (has_frame())
  510. return ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_THICKFRAME;
  511. else
  512. return CanResize();
  513. #else
  514. return CanResize();
  515. #endif
  516. }
  517. void NativeWindowViews::SetMovable(bool movable) {
  518. movable_ = movable;
  519. }
  520. bool NativeWindowViews::IsMovable() {
  521. #if defined(OS_WIN)
  522. return movable_;
  523. #else
  524. return true; // Not implemented on Linux.
  525. #endif
  526. }
  527. void NativeWindowViews::SetMinimizable(bool minimizable) {
  528. #if defined(OS_WIN)
  529. FlipWindowStyle(GetAcceleratedWidget(), minimizable, WS_MINIMIZEBOX);
  530. #endif
  531. minimizable_ = minimizable;
  532. }
  533. bool NativeWindowViews::IsMinimizable() {
  534. #if defined(OS_WIN)
  535. return ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_MINIMIZEBOX;
  536. #else
  537. return true; // Not implemented on Linux.
  538. #endif
  539. }
  540. void NativeWindowViews::SetMaximizable(bool maximizable) {
  541. #if defined(OS_WIN)
  542. FlipWindowStyle(GetAcceleratedWidget(), maximizable, WS_MAXIMIZEBOX);
  543. #endif
  544. maximizable_ = maximizable;
  545. }
  546. bool NativeWindowViews::IsMaximizable() {
  547. #if defined(OS_WIN)
  548. return ::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_MAXIMIZEBOX;
  549. #else
  550. return true; // Not implemented on Linux.
  551. #endif
  552. }
  553. void NativeWindowViews::SetFullScreenable(bool fullscreenable) {
  554. fullscreenable_ = fullscreenable;
  555. }
  556. bool NativeWindowViews::IsFullScreenable() {
  557. return fullscreenable_;
  558. }
  559. void NativeWindowViews::SetClosable(bool closable) {
  560. #if defined(OS_WIN)
  561. HMENU menu = GetSystemMenu(GetAcceleratedWidget(), false);
  562. if (closable) {
  563. EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
  564. } else {
  565. EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
  566. }
  567. #endif
  568. }
  569. bool NativeWindowViews::IsClosable() {
  570. #if defined(OS_WIN)
  571. HMENU menu = GetSystemMenu(GetAcceleratedWidget(), false);
  572. MENUITEMINFO info;
  573. memset(&info, 0, sizeof(info));
  574. info.cbSize = sizeof(info);
  575. info.fMask = MIIM_STATE;
  576. if (!GetMenuItemInfo(menu, SC_CLOSE, false, &info)) {
  577. return false;
  578. }
  579. return !(info.fState & MFS_DISABLED);
  580. #elif defined(USE_X11)
  581. return true;
  582. #endif
  583. }
  584. void NativeWindowViews::SetAlwaysOnTop(bool top, const std::string& level) {
  585. window_->SetAlwaysOnTop(top);
  586. }
  587. bool NativeWindowViews::IsAlwaysOnTop() {
  588. return window_->IsAlwaysOnTop();
  589. }
  590. void NativeWindowViews::Center() {
  591. window_->CenterWindow(GetSize());
  592. }
  593. void NativeWindowViews::SetTitle(const std::string& title) {
  594. title_ = title;
  595. window_->UpdateWindowTitle();
  596. }
  597. std::string NativeWindowViews::GetTitle() {
  598. return title_;
  599. }
  600. void NativeWindowViews::FlashFrame(bool flash) {
  601. #if defined(OS_WIN)
  602. // The Chromium's implementation has a bug stopping flash.
  603. if (!flash) {
  604. FLASHWINFO fwi;
  605. fwi.cbSize = sizeof(fwi);
  606. fwi.hwnd = GetAcceleratedWidget();
  607. fwi.dwFlags = FLASHW_STOP;
  608. fwi.uCount = 0;
  609. FlashWindowEx(&fwi);
  610. return;
  611. }
  612. #endif
  613. window_->FlashFrame(flash);
  614. }
  615. void NativeWindowViews::SetSkipTaskbar(bool skip) {
  616. #if defined(OS_WIN)
  617. base::win::ScopedComPtr<ITaskbarList> taskbar;
  618. if (FAILED(taskbar.CreateInstance(CLSID_TaskbarList, NULL,
  619. CLSCTX_INPROC_SERVER)) ||
  620. FAILED(taskbar->HrInit()))
  621. return;
  622. if (skip) {
  623. taskbar->DeleteTab(GetAcceleratedWidget());
  624. } else {
  625. taskbar->AddTab(GetAcceleratedWidget());
  626. taskbar_host_.RestoreThumbarButtons(GetAcceleratedWidget());
  627. }
  628. #elif defined(USE_X11)
  629. SetWMSpecState(GetAcceleratedWidget(), skip,
  630. GetAtom("_NET_WM_STATE_SKIP_TASKBAR"));
  631. #endif
  632. }
  633. void NativeWindowViews::SetKiosk(bool kiosk) {
  634. SetFullScreen(kiosk);
  635. }
  636. bool NativeWindowViews::IsKiosk() {
  637. return IsFullscreen();
  638. }
  639. void NativeWindowViews::SetBackgroundColor(const std::string& color_name) {
  640. // web views' background color.
  641. SkColor background_color = ParseHexColor(color_name);
  642. set_background(views::Background::CreateSolidBackground(background_color));
  643. #if defined(OS_WIN)
  644. // Set the background color of native window.
  645. HBRUSH brush = CreateSolidBrush(skia::SkColorToCOLORREF(background_color));
  646. ULONG_PTR previous_brush = SetClassLongPtr(
  647. GetAcceleratedWidget(),
  648. GCLP_HBRBACKGROUND,
  649. reinterpret_cast<LONG_PTR>(brush));
  650. if (previous_brush)
  651. DeleteObject((HBRUSH)previous_brush);
  652. #endif
  653. }
  654. void NativeWindowViews::SetHasShadow(bool has_shadow) {
  655. wm::SetShadowType(
  656. GetNativeWindow(),
  657. has_shadow ? wm::SHADOW_TYPE_RECTANGULAR : wm::SHADOW_TYPE_NONE);
  658. }
  659. bool NativeWindowViews::HasShadow() {
  660. return wm::GetShadowType(GetNativeWindow()) != wm::SHADOW_TYPE_NONE;
  661. }
  662. void NativeWindowViews::SetIgnoreMouseEvents(bool ignore) {
  663. #if defined(OS_WIN)
  664. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  665. if (ignore)
  666. ex_style |= (WS_EX_TRANSPARENT | WS_EX_LAYERED);
  667. else
  668. ex_style &= ~(WS_EX_TRANSPARENT | WS_EX_LAYERED);
  669. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  670. #elif defined(USE_X11)
  671. if (ignore) {
  672. XRectangle r = {0, 0, 1, 1};
  673. XShapeCombineRectangles(gfx::GetXDisplay(), GetAcceleratedWidget(),
  674. ShapeInput, 0, 0, &r, 1, ShapeSet, YXBanded);
  675. } else {
  676. XShapeCombineMask(gfx::GetXDisplay(), GetAcceleratedWidget(),
  677. ShapeInput, 0, 0, None, ShapeSet);
  678. }
  679. #endif
  680. }
  681. void NativeWindowViews::SetContentProtection(bool enable) {
  682. #if defined(OS_WIN)
  683. DWORD affinity = enable ? WDA_MONITOR : WDA_NONE;
  684. ::SetWindowDisplayAffinity(GetAcceleratedWidget(), affinity);
  685. #endif
  686. }
  687. void NativeWindowViews::SetFocusable(bool focusable) {
  688. #if defined(OS_WIN)
  689. LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
  690. if (focusable)
  691. ex_style &= ~WS_EX_NOACTIVATE;
  692. else
  693. ex_style |= WS_EX_NOACTIVATE;
  694. ::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
  695. SetSkipTaskbar(!focusable);
  696. Focus(false);
  697. #endif
  698. }
  699. void NativeWindowViews::SetMenu(AtomMenuModel* menu_model) {
  700. if (menu_model == nullptr) {
  701. // Remove accelerators
  702. accelerator_table_.clear();
  703. GetFocusManager()->UnregisterAccelerators(this);
  704. // and menu bar.
  705. #if defined(USE_X11)
  706. global_menu_bar_.reset();
  707. #endif
  708. SetMenuBarVisibility(false);
  709. menu_bar_.reset();
  710. return;
  711. }
  712. RegisterAccelerators(menu_model);
  713. #if defined(USE_X11)
  714. if (!global_menu_bar_ && ShouldUseGlobalMenuBar())
  715. global_menu_bar_.reset(new GlobalMenuBarX11(this));
  716. // Use global application menu bar when possible.
  717. if (global_menu_bar_ && global_menu_bar_->IsServerStarted()) {
  718. global_menu_bar_->SetMenu(menu_model);
  719. return;
  720. }
  721. #endif
  722. // Do not show menu bar in frameless window.
  723. if (!has_frame())
  724. return;
  725. if (!menu_bar_) {
  726. gfx::Size content_size = GetContentSize();
  727. menu_bar_.reset(new MenuBar(this));
  728. menu_bar_->set_owned_by_client();
  729. if (!menu_bar_autohide_) {
  730. SetMenuBarVisibility(true);
  731. if (use_content_size_) {
  732. // Enlarge the size constraints for the menu.
  733. extensions::SizeConstraints constraints = GetContentSizeConstraints();
  734. if (constraints.HasMinimumSize()) {
  735. gfx::Size min_size = constraints.GetMinimumSize();
  736. min_size.set_height(min_size.height() + kMenuBarHeight);
  737. constraints.set_minimum_size(min_size);
  738. }
  739. if (constraints.HasMaximumSize()) {
  740. gfx::Size max_size = constraints.GetMaximumSize();
  741. max_size.set_height(max_size.height() + kMenuBarHeight);
  742. constraints.set_maximum_size(max_size);
  743. }
  744. SetContentSizeConstraints(constraints);
  745. // Resize the window to make sure content size is not changed.
  746. SetContentSize(content_size);
  747. }
  748. }
  749. }
  750. menu_bar_->SetMenu(menu_model);
  751. Layout();
  752. }
  753. void NativeWindowViews::SetParentWindow(NativeWindow* parent) {
  754. NativeWindow::SetParentWindow(parent);
  755. #if defined(USE_X11)
  756. XDisplay* xdisplay = gfx::GetXDisplay();
  757. XSetTransientForHint(
  758. xdisplay, GetAcceleratedWidget(),
  759. parent? parent->GetAcceleratedWidget() : DefaultRootWindow(xdisplay));
  760. #elif defined(OS_WIN) && defined(DEBUG)
  761. // Should work, but does not, it seems that the views toolkit doesn't support
  762. // reparenting on desktop.
  763. if (parent) {
  764. ::SetParent(GetAcceleratedWidget(), parent->GetAcceleratedWidget());
  765. views::Widget::ReparentNativeView(GetNativeWindow(),
  766. parent->GetNativeWindow());
  767. wm::AddTransientChild(parent->GetNativeWindow(), GetNativeWindow());
  768. } else {
  769. if (!GetNativeWindow()->parent())
  770. return;
  771. ::SetParent(GetAcceleratedWidget(), NULL);
  772. views::Widget::ReparentNativeView(GetNativeWindow(), nullptr);
  773. wm::RemoveTransientChild(GetNativeWindow()->parent(), GetNativeWindow());
  774. }
  775. #endif
  776. }
  777. gfx::NativeWindow NativeWindowViews::GetNativeWindow() {
  778. return window_->GetNativeWindow();
  779. }
  780. void NativeWindowViews::SetProgressBar(
  781. double progress, NativeWindow::ProgressState state) {
  782. #if defined(OS_WIN)
  783. taskbar_host_.SetProgressBar(GetAcceleratedWidget(), progress, state);
  784. #elif defined(USE_X11)
  785. if (unity::IsRunning()) {
  786. unity::SetProgressFraction(progress);
  787. }
  788. #endif
  789. }
  790. void NativeWindowViews::SetOverlayIcon(const gfx::Image& overlay,
  791. const std::string& description) {
  792. #if defined(OS_WIN)
  793. taskbar_host_.SetOverlayIcon(GetAcceleratedWidget(), overlay, description);
  794. #endif
  795. }
  796. void NativeWindowViews::SetAutoHideMenuBar(bool auto_hide) {
  797. menu_bar_autohide_ = auto_hide;
  798. }
  799. bool NativeWindowViews::IsMenuBarAutoHide() {
  800. return menu_bar_autohide_;
  801. }
  802. void NativeWindowViews::SetMenuBarVisibility(bool visible) {
  803. if (!menu_bar_ || menu_bar_visible_ == visible)
  804. return;
  805. // Always show the accelerator when the auto-hide menu bar shows.
  806. if (menu_bar_autohide_)
  807. menu_bar_->SetAcceleratorVisibility(visible);
  808. menu_bar_visible_ = visible;
  809. if (visible) {
  810. DCHECK_EQ(child_count(), 1);
  811. AddChildView(menu_bar_.get());
  812. } else {
  813. DCHECK_EQ(child_count(), 2);
  814. RemoveChildView(menu_bar_.get());
  815. }
  816. Layout();
  817. }
  818. bool NativeWindowViews::IsMenuBarVisible() {
  819. return menu_bar_visible_;
  820. }
  821. void NativeWindowViews::SetVisibleOnAllWorkspaces(bool visible) {
  822. window_->SetVisibleOnAllWorkspaces(visible);
  823. }
  824. bool NativeWindowViews::IsVisibleOnAllWorkspaces() {
  825. #if defined(USE_X11)
  826. // Use the presence/absence of _NET_WM_STATE_STICKY in _NET_WM_STATE to
  827. // determine whether the current window is visible on all workspaces.
  828. XAtom sticky_atom = GetAtom("_NET_WM_STATE_STICKY");
  829. std::vector<XAtom> wm_states;
  830. ui::GetAtomArrayProperty(GetAcceleratedWidget(), "_NET_WM_STATE", &wm_states);
  831. return std::find(wm_states.begin(),
  832. wm_states.end(), sticky_atom) != wm_states.end();
  833. #endif
  834. return false;
  835. }
  836. gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() {
  837. return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
  838. }
  839. #if defined(OS_WIN)
  840. void NativeWindowViews::SetIcon(HICON window_icon, HICON app_icon) {
  841. // We are responsible for storing the images.
  842. window_icon_ = base::win::ScopedHICON(CopyIcon(window_icon));
  843. app_icon_ = base::win::ScopedHICON(CopyIcon(app_icon));
  844. HWND hwnd = GetAcceleratedWidget();
  845. SendMessage(hwnd, WM_SETICON, ICON_SMALL,
  846. reinterpret_cast<LPARAM>(window_icon_.get()));
  847. SendMessage(hwnd, WM_SETICON, ICON_BIG,
  848. reinterpret_cast<LPARAM>(app_icon_.get()));
  849. }
  850. #elif defined(USE_X11)
  851. void NativeWindowViews::SetIcon(const gfx::ImageSkia& icon) {
  852. views::DesktopWindowTreeHostX11* tree_host =
  853. views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget());
  854. static_cast<views::DesktopWindowTreeHost*>(tree_host)->SetWindowIcons(
  855. icon, icon);
  856. }
  857. #endif
  858. void NativeWindowViews::SetEnabled(bool enable) {
  859. // Handle multiple calls of SetEnabled correctly.
  860. if (enable) {
  861. --disable_count_;
  862. if (disable_count_ != 0)
  863. return;
  864. } else {
  865. ++disable_count_;
  866. if (disable_count_ != 1)
  867. return;
  868. }
  869. #if defined(OS_WIN)
  870. ::EnableWindow(GetAcceleratedWidget(), enable);
  871. #elif defined(USE_X11)
  872. views::DesktopWindowTreeHostX11* tree_host =
  873. views::DesktopWindowTreeHostX11::GetHostForXID(GetAcceleratedWidget());
  874. if (enable) {
  875. tree_host->RemoveEventRewriter(event_disabler_.get());
  876. event_disabler_.reset();
  877. } else {
  878. event_disabler_.reset(new EventDisabler);
  879. tree_host->AddEventRewriter(event_disabler_.get());
  880. }
  881. #endif
  882. }
  883. void NativeWindowViews::OnWidgetActivationChanged(
  884. views::Widget* widget, bool active) {
  885. if (widget != window_.get())
  886. return;
  887. // Post the notification to next tick.
  888. content::BrowserThread::PostTask(
  889. content::BrowserThread::UI, FROM_HERE,
  890. base::Bind(active ? &NativeWindow::NotifyWindowFocus :
  891. &NativeWindow::NotifyWindowBlur,
  892. GetWeakPtr()));
  893. if (active && inspectable_web_contents() &&
  894. !inspectable_web_contents()->IsDevToolsViewShowing())
  895. web_contents()->Focus();
  896. // Hide menu bar when window is blured.
  897. if (!active && menu_bar_autohide_ && menu_bar_visible_)
  898. SetMenuBarVisibility(false);
  899. }
  900. void NativeWindowViews::OnWidgetBoundsChanged(
  901. views::Widget* widget, const gfx::Rect& bounds) {
  902. if (widget != window_.get())
  903. return;
  904. if (widget_size_ != bounds.size()) {
  905. NotifyWindowResize();
  906. widget_size_ = bounds.size();
  907. }
  908. }
  909. void NativeWindowViews::DeleteDelegate() {
  910. if (is_modal() && NativeWindow::parent()) {
  911. NativeWindowViews* parent =
  912. static_cast<NativeWindowViews*>(NativeWindow::parent());
  913. // Enable parent window after current window gets closed.
  914. parent->SetEnabled(true);
  915. // Focus on parent window.
  916. parent->Focus(true);
  917. }
  918. NotifyWindowClosed();
  919. }
  920. views::View* NativeWindowViews::GetInitiallyFocusedView() {
  921. return inspectable_web_contents()->GetView()->GetWebView();
  922. }
  923. bool NativeWindowViews::CanResize() const {
  924. return resizable_;
  925. }
  926. bool NativeWindowViews::CanMaximize() const {
  927. return resizable_ && maximizable_;
  928. }
  929. bool NativeWindowViews::CanMinimize() const {
  930. #if defined(OS_WIN)
  931. return minimizable_;
  932. #elif defined(USE_X11)
  933. return true;
  934. #endif
  935. }
  936. base::string16 NativeWindowViews::GetWindowTitle() const {
  937. return base::UTF8ToUTF16(title_);
  938. }
  939. bool NativeWindowViews::ShouldHandleSystemCommands() const {
  940. return true;
  941. }
  942. views::Widget* NativeWindowViews::GetWidget() {
  943. return window_.get();
  944. }
  945. const views::Widget* NativeWindowViews::GetWidget() const {
  946. return window_.get();
  947. }
  948. views::View* NativeWindowViews::GetContentsView() {
  949. return this;
  950. }
  951. bool NativeWindowViews::ShouldDescendIntoChildForEventHandling(
  952. gfx::NativeView child,
  953. const gfx::Point& location) {
  954. // App window should claim mouse events that fall within the draggable region.
  955. if (draggable_region() &&
  956. draggable_region()->contains(location.x(), location.y()))
  957. return false;
  958. // And the events on border for dragging resizable frameless window.
  959. if (!has_frame() && CanResize()) {
  960. FramelessView* frame = static_cast<FramelessView*>(
  961. window_->non_client_view()->frame_view());
  962. return frame->ResizingBorderHitTest(location) == HTNOWHERE;
  963. }
  964. return true;
  965. }
  966. views::ClientView* NativeWindowViews::CreateClientView(views::Widget* widget) {
  967. return new NativeWindowClientView(widget, this);
  968. }
  969. views::NonClientFrameView* NativeWindowViews::CreateNonClientFrameView(
  970. views::Widget* widget) {
  971. #if defined(OS_WIN)
  972. WinFrameView* frame_view = new WinFrameView;
  973. frame_view->Init(this, widget);
  974. return frame_view;
  975. #else
  976. if (has_frame()) {
  977. return new NativeFrameView(this, widget);
  978. } else {
  979. FramelessView* frame_view = new FramelessView;
  980. frame_view->Init(this, widget);
  981. return frame_view;
  982. }
  983. #endif
  984. }
  985. void NativeWindowViews::OnWidgetMove() {
  986. NotifyWindowMove();
  987. }
  988. gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
  989. const gfx::Rect& bounds) {
  990. if (!has_frame())
  991. return bounds;
  992. gfx::Rect window_bounds(bounds);
  993. #if defined(OS_WIN)
  994. HWND hwnd = GetAcceleratedWidget();
  995. gfx::Rect dpi_bounds = display::win::ScreenWin::DIPToScreenRect(hwnd, bounds);
  996. window_bounds = display::win::ScreenWin::ScreenToDIPRect(
  997. hwnd,
  998. window_->non_client_view()->GetWindowBoundsForClientBounds(dpi_bounds));
  999. #endif
  1000. if (menu_bar_ && menu_bar_visible_) {
  1001. window_bounds.set_y(window_bounds.y() - kMenuBarHeight);
  1002. window_bounds.set_height(window_bounds.height() + kMenuBarHeight);
  1003. }
  1004. return window_bounds;
  1005. }
  1006. gfx::Rect NativeWindowViews::WindowBoundsToContentBounds(
  1007. const gfx::Rect& bounds) {
  1008. if (!has_frame())
  1009. return bounds;
  1010. gfx::Rect content_bounds(bounds);
  1011. #if defined(OS_WIN)
  1012. HWND hwnd = GetAcceleratedWidget();
  1013. content_bounds.set_size(
  1014. display::win::ScreenWin::DIPToScreenSize(hwnd, content_bounds.size()));
  1015. RECT rect;
  1016. SetRectEmpty(&rect);
  1017. DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
  1018. DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  1019. AdjustWindowRectEx(&rect, style, FALSE, ex_style);
  1020. content_bounds.set_width(content_bounds.width() - (rect.right - rect.left));
  1021. content_bounds.set_height(content_bounds.height() - (rect.bottom - rect.top));
  1022. content_bounds.set_size(
  1023. display::win::ScreenWin::ScreenToDIPSize(hwnd, content_bounds.size()));
  1024. #endif
  1025. if (menu_bar_ && menu_bar_visible_) {
  1026. content_bounds.set_y(content_bounds.y() + kMenuBarHeight);
  1027. content_bounds.set_height(content_bounds.height() - kMenuBarHeight);
  1028. }
  1029. return content_bounds;
  1030. }
  1031. void NativeWindowViews::HandleKeyboardEvent(
  1032. content::WebContents*,
  1033. const content::NativeWebKeyboardEvent& event) {
  1034. keyboard_event_handler_->HandleKeyboardEvent(event, GetFocusManager());
  1035. if (!menu_bar_)
  1036. return;
  1037. // Show accelerator when "Alt" is pressed.
  1038. if (menu_bar_visible_ && IsAltKey(event))
  1039. menu_bar_->SetAcceleratorVisibility(
  1040. event.type == blink::WebInputEvent::RawKeyDown);
  1041. // Show the submenu when "Alt+Key" is pressed.
  1042. if (event.type == blink::WebInputEvent::RawKeyDown && !IsAltKey(event) &&
  1043. IsAltModifier(event)) {
  1044. if (!menu_bar_visible_ &&
  1045. (menu_bar_->GetAcceleratorIndex(event.windowsKeyCode) != -1))
  1046. SetMenuBarVisibility(true);
  1047. menu_bar_->ActivateAccelerator(event.windowsKeyCode);
  1048. return;
  1049. }
  1050. if (!menu_bar_autohide_)
  1051. return;
  1052. // Toggle the menu bar only when a single Alt is released.
  1053. if (event.type == blink::WebInputEvent::RawKeyDown && IsAltKey(event)) {
  1054. // When a single Alt is pressed:
  1055. menu_bar_alt_pressed_ = true;
  1056. } else if (event.type == blink::WebInputEvent::KeyUp && IsAltKey(event) &&
  1057. menu_bar_alt_pressed_) {
  1058. // When a single Alt is released right after a Alt is pressed:
  1059. menu_bar_alt_pressed_ = false;
  1060. SetMenuBarVisibility(!menu_bar_visible_);
  1061. } else {
  1062. // When any other keys except single Alt have been pressed/released:
  1063. menu_bar_alt_pressed_ = false;
  1064. }
  1065. }
  1066. gfx::Size NativeWindowViews::GetMinimumSize() {
  1067. return NativeWindow::GetMinimumSize();
  1068. }
  1069. gfx::Size NativeWindowViews::GetMaximumSize() {
  1070. return NativeWindow::GetMaximumSize();
  1071. }
  1072. bool NativeWindowViews::AcceleratorPressed(const ui::Accelerator& accelerator) {
  1073. return accelerator_util::TriggerAcceleratorTableCommand(
  1074. &accelerator_table_, accelerator);
  1075. }
  1076. void NativeWindowViews::RegisterAccelerators(AtomMenuModel* menu_model) {
  1077. // Clear previous accelerators.
  1078. views::FocusManager* focus_manager = GetFocusManager();
  1079. accelerator_table_.clear();
  1080. focus_manager->UnregisterAccelerators(this);
  1081. // Register accelerators with focus manager.
  1082. accelerator_util::GenerateAcceleratorTable(&accelerator_table_, menu_model);
  1083. accelerator_util::AcceleratorTable::const_iterator iter;
  1084. for (iter = accelerator_table_.begin();
  1085. iter != accelerator_table_.end();
  1086. ++iter) {
  1087. focus_manager->RegisterAccelerator(
  1088. iter->first, ui::AcceleratorManager::kNormalPriority, this);
  1089. }
  1090. }
  1091. ui::WindowShowState NativeWindowViews::GetRestoredState() {
  1092. if (IsMaximized())
  1093. return ui::SHOW_STATE_MAXIMIZED;
  1094. if (IsFullscreen())
  1095. return ui::SHOW_STATE_FULLSCREEN;
  1096. return ui::SHOW_STATE_NORMAL;
  1097. }
  1098. // static
  1099. NativeWindow* NativeWindow::Create(
  1100. brightray::InspectableWebContents* inspectable_web_contents,
  1101. const mate::Dictionary& options,
  1102. NativeWindow* parent) {
  1103. return new NativeWindowViews(inspectable_web_contents, options, parent);
  1104. }
  1105. } // namespace atom