native_window_views.cc 39 KB

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