native_window_views.cc 37 KB

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