native_window_views.cc 40 KB

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